1 / 156

Introduction to uC/OS-II

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

chacha
Télécharger la présentation

Introduction to uC/OS-II

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Introduction to uC/OS-II http://www.micrium.com/

  2. uC/OS-II • Real-Time Systems Concepts • Kernel Structure • Task Management • Time Management • Intertask Communication & Synchronization • Memory Management /155

  3. Real-Time Systems Concepts • Multitasking • Kernel • Scheduling • Mutual Exclusion • Message Passing • Interrupt /155

  4. Foreground/Background Systems Interrupt Level Task Level /155

  5. Multitasking • Multitasking • A process of scheduling and switching CPU between several tasks. • Related issues • Context Switch (Task Switch) • Resource Sharing • Critical Section /155

  6. Multitasking /155

  7. 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 /155

  8. Task States /155

  9. 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 /155

  10. 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. /155

  11. Non-Preemptive Kernel /155

  12. 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. /155

  13. Preemptive Kernel /155

  14. 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() /155

  15. 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 /155

  16. 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 /155

  17. 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 /155

  18. Priority Inversion Problem /155

  19. Priority Inheritance /155

  20. 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 /155

  21. Using Semaphore /155

  22. Synchronization • Synchronization mechanism is used between tasks or task to ISR. • Unilateral rendezvous • Bilateral rendezvous /155

  23. Unilateral rendezvous /155

  24. Bilateral rendezvous /155

  25. Event Flags /155

  26. 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. /155

  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 /155

  28. 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). /155

  29. Interrupt • An interrupt is a hardware mechanism used to inform the CPU that an asynchronous event has occurred. • Interrupt Latency • Interrupt Response • Interrupt Recovery /155

  30. Interrupt Nesting /155

  31. 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. /155

  32. 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. /155

  33. 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. /155

  34. Non-preemptive Kernel /155

  35. Preemptive Kernel /155

  36. 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 /155

  37. 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. /155

  38. 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) /155

  39. 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. /155

  40. 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 /155

  41. 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. /155

  42. uC/OS-II • Real-Time Systems Concepts • Kernel Structure • Task Management • Time Management • Intertask Communication & Synchronization • Memory Management /155

  43. Kernel Structure • Task Control Blocks • Ready List • Task Scheduling • Interrupt under uC/OS-II /155

  44. Critical Sections • Archive this by disabling interrupt • OS_CPU.H • OS_ENTER_CRITICAL() • OS_EXIT_CRITICAL() /155

  45. 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. /155

  46. Task States /155

  47. 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; /155

  48. OS_TCB Lists • TCBs store in OSTCBTbl[] • All TCBs are initialized and linked when uC/OS-II is initialized /155

  49. Ready List /155

  50. 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. /155

More Related