Micrium µc/os II RTOS Introduction (by Jean Labrosse) EE599 001 Fall 2012 J. E. Lumpp
μc/os II μc/os II is a highly portable, ROMable, very scalable, preemptive real time, deterministic, multitasking kernel It can manage up to 64 tasks (56 user tasks available) It has connectivity with μc/guiand μc/fs (GUIand File Systems for μc/os II) It is ported to more than 100 microprocessors and microcontrollers It is simple to use and simple to implement but very effective compared to the price/performance p ratio. It supports all type of processors from 8 bit to 64 bit 2
µc/os Porting 3
Task Management Services Task Creation Task Stack & Stack Checking Task Deletion Change a Task s Priority Suspend and Resume a Task Get Information about a Task 4
Task Features μc/os II can manage up to 64 tasks. The four highest priority tasks and the four lowest priority tasks are reserved for its own use. This leaves us with 56 application tasks. The lower the value of the priority, the higher the priority of the task. (e.g., Rate Monotonic Scheduling) The task priority number also serves as the task identifier 5
Embedded Program w/o RTOS Foreground #2 ISR #2 Foreground #1 ISR #1 ISR #1 Background Task #1 Task #2 Task #3 Infinite loop Time 6
Super Loop Background response time is the background execution time Non deterministic Affected by control flow (if, while ) High latency Changes to code changes timing Affected by if, for, while Poll to see if ISR occurred ISR Task #1 Task #2 Task #3 Task #4 Infinite loop 7
Foreground/Background g Tasks All tasks /functions() have the same priority Code executes in sequence => No Shared Data Bugs If an important event occurs, it s handled at the samepriority as everything else May need to execute the same code often to avoid missing an event Task #1 Task #2 Task #3 Task #4 Infinite it loop 8
RTOS Real Time Operating System Software that manages the time of a microprocessor, microcontroller, or a digital signal processor Prioritizes the work to be done Provides multitasking Provides services to the application Semaphores Message mailboxes and queues Event flags Time delays, timers, andtimeouts Task management Memory management Bandwidthassessment assessment idle time 9
Preemption 1. Pre emptive: Always runs the highest available task. Tasks of identical priority it share CPU time (fully pre emptive with round robin time slicing) 2. Cooperative: Context switches only occur if a task blocks, or explicitly relinquishes CPU control 10
RTOS Advantages Software that manages the time of a microprocessor or microcontroller Ensures that the most important code runs first Allows Multitasking Do more than one thing at the same time Application is broken down into multiple tasks, each handling oneaspect of your application It s like having multiple CPUs Provides valuable services to your application Time delays dl Resource sharing Inter task communication and synchronization 11
RTOS Tasks A task is a function that appears it has the CPU all to itself Each Task has Its own stack space A priority i based on its importance A task contains application code and calls to OS functions 12
Example Task A task is an infinite loop void Task (void *p_arg) { Do something with argument p_arg; Task initialization; for (;;) { /* Processing (Your Code) */ Wait for event; /* Time to expire... */ /* Signal from ISR... */ /* Signal from task... */ /* Processing (Your Code) */ } } 13
Designing with µc/os II High Priority Task Task Task Each Task Importance Task Event Event Task Task Low Priority Task Task Infinite it Loop 14
Task States 15
uc/os II API There are several functions called API of uc/os II using these api functions we can write our application on uc/os II API is used to handle Tasks Semaphores and Mutex Memory Management Timer etc. 16
Kernel Structure How μc/os II handles access to critical sections of code, What a task is, and how μc/os II knows about your tasks, How tasks are scheduled, How μc/os II can determine how much of CPU your application is using, How do to write Interrupt Service Routines (ISRs), What a clock tick is and how μc/os II handles it, How to initialize μc/os II and, How to start multitasking. 17
Tasks 18
Tasks Management Uc os II can manage up to 64 tasks but it is recommended that leave the 4 above and 4 below priorities so effectively we have 56 task It means we can create upto 56 tasks OS_LOWEST_PRIO the lowest priority task is for the idle task Idle task means that if there is no task in ready queue still the cpu will execute this idle task. 19
Task Statistics Uc/os II contains a task that provide run time statistics OS_TaskStat() and is created by uc/os II if you set the configuration constant OS_TASK_STAT_EN to 1 When enabled OS_TaskStat() executes every second and computes the percentage of CPU usages OS_TaskStat() tells you how much CPU time is used by your application as a percentage This value is placed in 8 bit signed integer OSCPUUsage variable Resolution of OSCPUUsage is 1 percent 20
Interrupts under uc/os II μc/os II requires that an Interrupt Service Routine (ISR) be written in assembly language. μc/os II needs to know that you are servicing an ISR and thus, you need to either call OSIntEnter() or, increment the global variable OSIntNesting. μc/os II allows you to nest interrupts because it keeps track of nesting in OSIntNesting. ISR concludes by calling OSIntExit() which decrementsthethe interrupt nesting counter. 21
Interrupts under uc/os II This function notify uc/os II about beginning of of an interrupt Funtions OSIntExit() notify about the ending of an interrupt 22
Task Management Task is either an infinite loop function or a function that deletes itself Task code is not actually deleted, uc os II doesn't know about the task anymore so that code will not run. Task loop like any other C function Task must never return return type of a task must always be void Task functions are described in OS_TASK.C Using the function we can create, delete, change task's priority, suspend, resume and obtain information about a task 23
24
Time Management Clock Tick: A clock tick is a periodic time source to keep track of time delays and time outs. Tick intervals: 10 ~ 100 ms. The faster the tick rate, the higher the overhead imposed on the system. When ever a clock tick occurs μc/os II increments a 32 bit counter The counter starts at zero, and rolls over to 4,294,967,295 (2^32 1) ticks. A task can be delayed and a delayed task can also be resumed 25
Time Management Five services: OSTimeDLY() OSTimeDLYHMSM() S OSTimeDlyResume() OSTimeGet() OSTimeSet() 26
Inter task task communication Inter task or inter process communication in μc/os takes place using Semaphores Message mailbox Message queues Tasks and Interrupt service routines (ISR) can interact with each other through an ECB (event control block) 27
Semaphores A semaphore consists of a wait list and an integer counter. OSSemPend(): Counter ; If the value of the semaphore <0, then the task is blocked and moved to the wait list immediately. A time out t value can be specified. OSSemPost(): Counter++; If the value of the semaphore >= 0, then a task in the wait list is removed from the wait list. Reschedule ifneeded needed.
Single task waiting 29
Multiple tasks waiting and signaling 30
Tasks can wait and signal along with an optional time out 31
Sempahores μc/os IIsemaphores consist of two elements 16 bit unsigned integer count list of tasks waiting for semaphore μc/osii provides Create, post, pend accept and query services 32
μc/os II message mailboxesmailboxes μc/osii object that allows a task or ISR to send a pointer sized variable (pointing to a message) to another task. 33
μc/os II message queues Available services: Create, Post (FIFO), PostFront (LIFO), Pend, Accept, p,query, Flush N = #of entries in the queue: Queue full if Post or PostFront called N times before a Pend or Accept μc/os II / message queues organized as circular buffers. 34
Tasks loops Tasks running under a multitasking kernel should be written in one of two ways: A non returning forever loop. For example: void Task (void *) { DoInitStuff(); while (1) { do this; do that; do the other thing; call OS service (); // e.g. OSTimeDelay, OSSemPend, etc. } } 35
Rate Monotonic Scheduling In Rate Monotonic Scheduling tasks with the highest rate of execution are given the highest priority Assumptions: All tasks are periodic Tasks do not synchronize with one another, share resources, etc. Preemptive scheduling is used (always runs the highest priority task that is ready) Under these assumptions, let n be the number of tasks, Ei be the execution time of task i, and Ti be the period of task i. Then, all deadlines will be met if the following inequality is satisfied: ΣEi / Ti n(21/n 1) 36
Rate Monotonic Scheduling: Example Suppose we have 3 tasks. Task 1 runs at 100 Hz and takes 2 ms. Task 2 runs at 50 Hz and takes 1 ms. Task 3 runs at 66.7 Hz and takes 7 ms. Apply RMS theory (2/10) + (1/20) + (7/15) = 0.717 3(21/3 1) = 0.780 Thus, all the deadlines will be met General Solution? As n, the right hand side of the inequality goes to ln(2)= 0.6931. Thus, you should design 37