1.56k likes | 1.73k Views
Introduction to uC/OS-II. http://www.micrium.com/. uC/OS-II. Real-Time Systems Concepts Kernel Structure Task Management Time Management Intertask Communication & Synchronization Memory Management. Real-Time Systems Concepts. Multitasking Kernel Scheduling Mutual Exclusion
E N D
Introduction to uC/OS-II http://www.micrium.com/
uC/OS-II • Real-Time Systems Concepts • Kernel Structure • Task Management • Time Management • Intertask Communication & Synchronization • Memory Management /27
Real-Time Systems Concepts • Multitasking • Kernel • Scheduling • Mutual Exclusion • Message Passing • Interrupt /27
Foreground/Background Systems Interrupt Level Task Level /27
Multitasking • Multitasking • A process of scheduling and switching CPU between several tasks. • Related issues • Context Switch (Task Switch) • Resource Sharing • Critical Section /27
Multitasking /27
Task • A task, or called a thread, a simple program that thinks it has the CPU all to itself. • Each task is assigned a priority, its own set of CPU registers, and its own stack area . • Each task typically is an infinite loop that can be in any one of five states. • Ready, Running, Waiting, ISR, Dormant /27
Task States /27
Kernel • Kernel is the part of a multitasking system responsible for the management of tasks. • Context switching is the fundamental service of a kernel. Non-Preemptive Kernel v.s. Preemptive Kernel /27
Non-Preemptive Kernel • Cooperative multitasking • A non-preemptive kernel allows each task to run until it voluntarily gives up control of the CPU. • An ISR can make a higher priority task ready to run, but the ISR always returns to the interrupted task. • Linux 2.4 is non-preemptive. • Linux 2.6 is preemptive. /27
Preemptive Kernel • A preemptive kernel always executes the highest priority task that is ready to run. • µC/OS-II and most commercial real-time kernels are preemptive. • Much better response time. • Should not use non-reentrant functions, unless the functions are mutual exclusive. /27
Function Reentrancy • A reentrant function is a function that can be used by more than one task without fear of data corruption. • Reentrant functions either use local variables or protected global variables. • OS_ENTER_CRITICAL() • OS_EXIT_CRITICAL() /27
Non-Reentrant Function static int Temp; void swap(int *x, int *y) {Temp = *x;*x = *y;*y = Temp; } Reentrant Function void strcpy(char *dest, char *src) { while (*dest++ = *src++) { ; } *dest = NULL; } Function Reentrancy /27
Scheduling • Round-Robin Scheduling • Tasks executed sequentially • Task Priority Assignment • Static priority • Rate Monotonic (RM) • Dynamic priority • Earliest-Deadline First (EDF) • Time-Driven Scheduling • Pinwheel /27
Round-Robin • Kernel gives control to next task if the current task • has no work to do • completes • reaches the end of time-slice • Not supported in uC/OS-II • O(1) priority preemptive scheduling /27
Mutual Exclusion • Protected shared data of processes. • Exclusive access implementation • Disabling and enabling interrupts • Test-and-Set • Disabling and enabling scheduler • Semaphores • Simple Semaphore • Counting Semaphore • Deadlock – set timeout for a semaphore /27
Using Semaphore /27
Synchronization • Synchronization mechanism is used between tasks or task to ISR. • Unilateral rendezvous • Bilateral rendezvous /27
Event Flags /27
Message Passing • Intertask Communication • Using global variables or sending messages • Only communicate to ISR through global variables • Tasks are not aware when the global variables is changed unless task polls the content. /27
Message Mailboxes • A Message Mailbox, also called a message exchange, is typically a pointer size variable. • Operations • Initial • POST • PEND • necessary to wait for the message being deposited • ACCEPT • Acknowledgement /27
Message Queues • A message queue is used to send one or more messages to a task. • A message queue is an array of message mailboxes. • Generally, FIFO is used. • µC/OS-II allows a task to get messages Last-In-First-Out (LIFO). /27
Interrupt • An interrupt is a hardware mechanism used to inform the CPU that an asynchronous event has occurred. • Interrupt Latency • Interrupt Response • Interrupt Recovery /27
Interrupt Latency • Disabling interrupts affects interrupt latency. • All real-time systems disable interrupts to manipulate critical sections of code. • Re-enable interrupts when the critical section has executed. • Interrupt latency = Maximum time to disable interrupts + Time to start the first instruction in ISR. /27
Interrupt Response • Interrupt Response means time between the reception of the interrupt and the start of the user code which will handle the interrupt. • Interrupt response = Interrupt latency + Time to save CPU context • The worst case forinterrupt response is adopted. /27
Interrupt Recovery • Interrupt recovery is defined as the time required for the processor to return to the interrupted code. • Interrupt recovery = Time to determine if a high priority task is ready + Time to restore the CPU context of the highest priority task + time of executing return-from-interrupt. /27
Non-Maskable Interrupts (NMIs) • Service the most important time-critical ISR • Can not be disabled • Interrupt latency = Time to execution the longest instruction + Time to start execution the NMI ISR • Interrupt response = Interrupt latency + Time to save CPU context • Interrupt recovery = Time to restore CPU context + time of executing return-from-interrupt /27
Clock Tick • A periodical interrupt • Be viewed as heartbeat • Application specific and is generally between 10ms and 200ms • Faster timer causes higher overhead • Delay problem should be considered in real-time kernel. /27
Delaying a Task for 1 Tick 20 mS Tick Interrupt Tick ISR All higher priority tasks Call to delay 1 tick (20 mS) Call to delay 1 tick (20 mS) Call to delay 1 tick (20 mS) Delayed Task t3 t1 t2 (27 mS) (19 mS) (6 mS) /27
Clock Tick • Solve the problem • Increase the clock rate of your microprocessor. • Increase the time between tick interrupts. • Rearrange task priorities. • Avoid using floating-point math. • Get a compiler that performs better code optimization. • Write time-critical code in assembly language. • If possible, upgrade to a faster microprocessor in the same family. /27
Memory Requirement • Be careful to avoid large RAM requirement • Large local arrays • Nested/Recursive function • Interrupt nesting • Stack used by libraries • Too many function arguments /27
Real-Time Kernels • Real-time OS allows real-time application to be designed and expanded easily. • The use of an RTOS simplifies the design process by splitting the application into separate tasks. • Real-time kernel requires more ROM/RAM and 2 to 4 percent overhead. /27
uC/OS-II • Real-Time Systems Concepts • Kernel Structure • Task Management • Time Management • Intertask Communication & Synchronization • Memory Management /27
Kernel Structure • Task Control Blocks • Ready List • Task Scheduling • Interrupt under uC/OS-II /27
Critical Sections • Archive this by disabling interrupt • OS_CPU.H • OS_ENTER_CRITICAL() • OS_EXIT_CRITICAL() /27
Tasks • Up to 64 tasks • Two tasks for system use(idle and statistic) • Priorities 0, 1, 2, 3, OS_LOWEST_PRIO-3, OS_LOWEST_PRIO-2, OS_LOWEST_PRIO-1, OS_LOWEST_PRIO for future use • The lower the priority number, the higher the priority of the task. • The task priority is also the task identifier. /27
Task States /27
Task Control Blocks typedef struct os_tcb { OS_STK *OSTCBStkPtr; #if OS_TASK_CREATE_EXT_EN void *OSTCBExtPtr; OS_STK *OSTCBStkBottom; INT32U OSTCBStkSize; INT16U OSTCBOpt; INT16U OSTCBId; #endif struct os_tcb *OSTCBNext; struct os_tcb *OSTCBPrev; #if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN || OS_SEM_EN OS_EVENT *OSTCBEventPtr; #endif #if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN void *OSTCBMsg; #endif INT16U OSTCBDly; INT8U OSTCBStat; INT8U OSTCBPrio; INT8U OSTCBX; INT8U OSTCBY; INT8U OSTCBBitX; INT8U OSTCBBitY; #if OS_TASK_DEL_EN BOOLEAN OSTCBDelReq; #endif } OS_TCB; /27
OS_TCB Lists • TCBs store in OSTCBTbl[] • All TCBs are initialized and linked when uC/OS-II is initialized /27
Ready List /27
OSRdyGrp and OSRdyTbl[] • Bit 0 in OSRdyGrp is 1 when any bit in OSRdyTbl[0] is 1. • Bit 1 in OSRdyGrp is 1 when any bit in OSRdyTbl[1] is 1. • Bit 2 in OSRdyGrp is 1 when any bit in OSRdyTbl[2] is 1. • Bit 3 in OSRdyGrp is 1 when any bit in OSRdyTbl[3] is 1. • Bit 4 in OSRdyGrp is 1 when any bit in OSRdyTbl[4] is 1. • Bit 5 in OSRdyGrp is 1 when any bit in OSRdyTbl[5] is 1. • Bit 6 in OSRdyGrp is 1 when any bit in OSRdyTbl[6] is 1. • Bit 7 in OSRdyGrp is 1 when any bit in OSRdyTbl[7] is 1. /27