Getting Started with the COSIII Real Time Kernel

  • Slides: 13
Download presentation
Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015

Getting Started with the µC/OS-III Real Time Kernel Akos Ledeczi EECE 6354, Fall 2015 Vanderbilt University

Multitasking • Also called multithreading or concurrent programming • Multiple, sequential tasks (or threads)

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 maintain

Multitasking cont’d. • • • Because of the need to respond to timing demands

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.

What is an RTK? • The main task of an RTK is to manage

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.

µC/OS-III: Creating and Initializing an App • Start in main(): – Disable interrupts –

µC/OS-III: Creating and Initializing an App • Start in main(): – Disable interrupts – Initialize OS – Create a single Task using Task. Create() – Start OS: start multitasking and switch to the highest priority enabled task

OS Initialization • Initializes internal data structures • Creates up to 5 Tasks: –

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 • OSTask. Create() • 13 arguments: – Task Control Block (TCB): data

Task Creation • OSTask. Create() • 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

Initial task • • • Initialize hardware and CPU related things Set up tick interrupt (rate = OSCfg_Tick. Rate. Hz) 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,

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

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: OSSem. Create() OSSem. Pend() OSSem. Post() OSSem. Del() OSSem. Set() OSSem. Pend. Abort()

Binary Semaphores • Accessing a printer • Hiding behind an API

Binary Semaphores • Accessing a printer • Hiding behind an API

Binary Semaphores cont’d. OS_SEM My. Sem; void main() { OS_ERR err; … OSSem. Create(&My.

Binary Semaphores cont’d. OS_SEM My. Sem; void main() { OS_ERR err; … OSSem. Create(&My. Sem, ”My Semaphore”, 1, &err); … } void Task 1 (void *p_arg) OS_ERR err; CPU_TS ts; while (1) { … OSSem. Pend(&My. Sem, 0, OS_OPT_PEND_BLOCKING, &ts, &err); /*critical section */ OSSem. Post(&My. Sem, OS_OPT_POST_1, &err); /* check err */ } }

Counting Semaphores • When multiple resources/resource elements are available • E. g. , memory

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