130 likes | 251 Views
Getting Started with the µC/OS-III Real Time Kernel. Akos Ledeczi EECE 354, Fall 2012 Vanderbilt University. What is an RTK?. The main task of an RTK is to manage time and the resources of a (typically embedded) computer Multitasking Creation Scheduling Synchronization Communication
E N D
Getting Started with theµC/OS-IIIReal Time Kernel Akos Ledeczi EECE 354, Fall 2012 Vanderbilt University
What is an RTK? • The main task of an RTK is to manage time and the resources of a (typically embedded) computer • Multitasking • Creation • Scheduling • Synchronization • Communication • Resource management: • Memory • IO • Interrupt management • Time management • What is an RTOS? • An RTK plus higher level services such as file system, networking, GUI, etc.
Multitasking • Also called multithreading or concurrent programming • Multiple, sequential tasks (or threads) • Creating the illusion of having multiple CPUs • The task body is just a C function • Each task has its own stack • The same body can be reused for multiple tasks • Synchronization and communication are very important and complicated • Advantages: • Modular code • Manages complexity inherent in RT systems • Cleaner and easier to understand and maintain
Multitasking cont’d. • Because of the need to respond to timing demands made by different stimuli/responses, the system architecture must allow for fast switching between stimulus handlers. • Because of different priorities, unknown ordering and different timing requirements of different stimuli, a simple sequential loop is not usually adequate. • Real-time systems are therefore usually designed as cooperating processes with a real-time kernel controlling these processes.
µC/OS-III: Creating and Initializing an App • Start in main(): • Disable interrupts • Initialize OS • Create a single Task using TaskCreate() • Start OS: start multitasking and switch to the highest priority enabled task
OS Initialization • Initializes internal data structures • Creates up to 5 Tasks: • Idle Task (lowest priority task that runs when nothing else is available for running; it never blocks) • Tick Task (keeps track of time) • Statistics (optional) • Timer (optional) • Interrupt queue management (optional)
Task Creation • OSTaskCreate() • 13 arguments: • Task Control Block (TCB): data structure that the OS uses to manage the task and store all relevant info about it (e.g. stack pointer, priority, pointers to manage queues, etc.) • Name • Function pointer to actual code • Argument for the task (e.g., a pointer to a task-specific memory making the C function reusable for multiple tasks) • Stack pointer, watermark, size • Error code • Etc.
Initial task • Initialize hardware and CPU related things • Set up tick interrupt (rate = OSCfg_TickRateHz) • Enable interrupts • Create additional tasks (optional) • Infinite loop: • Inside there must be a blocking call
Critical Sections • Code that needs to run indivisibly • Access to shared resources, for example, hardware device, shared variable, data structures, etc. • How? • Disable interrupts • Lock the scheduler • Use semaphores • Finer control (on a task by task basis) • More overhead
Semaphores • Dijkstra in 1959 • “Key” to “locked code.” You need to acquire it to access the code. • Semaphore operations are “atomic.” • Binary and counting semaphores • Can be used for resource sharing and synchronization • Functions: OSSemCreate() OSSemPend() OSSemPost() OSSemDel() OSSemSet() OSSemPendAbort()
Binary Semaphores • Accessing a printer • Hiding behind an API
Binary Semaphores cont’d. OS_SEM MySem; void main() { OS_ERR err; … OSSemCreate(&MySem, ”My Semaphore”, 1, &err); … } void Task1 (void *p_arg) OS_ERR err; CPU_TS ts; while (1) { … OSSemPend(&MySem, 0, OS_OPT_PEND_BLOCKING, &ts, &err); /*critical section */ OSSemPost(&MySem, OS_OPT_POST_1, &err); /* check err */ } }
Counting Semaphores • When multiple resources/resource elements are available • E.g., memory pool or circular buffer • Initialize semaphore to the number of items available • Need to manage consumed/available items • Pend() waits on 0, otherwise, decrements counter and returns • Post() increments counter