810 likes | 1.02k Views
Embedded System Presentation. Nguyễn Trần Quang Nguyễn Công Vũ. µC/OS-II. Outline. Introduction to µC/OS-II Kernel Structure Task Management Time Management Semaphore Management Mutual Exclusion Semaphores Event Flag Management Message Mailbox Management Message Queue Management
E N D
Embedded System Presentation NguyễnTrầnQuang NguyễnCôngVũ µC/OS-II
µC/OS-II µC/OS-II
Outline • Introduction to µC/OS-II • Kernel Structure • Task Management • Time Management • Semaphore Management • Mutual Exclusion Semaphores • Event Flag Management • Message Mailbox Management • Message Queue Management • Memory Management Micro C_OS
Outline • Introduction to µC/OS-II • Kernel Structure • Task Management • Time Management • Semaphore Management • Mutual Exclusion Semaphores • Event Flag Management • Message Mailbox Management • Message Queue Management • Memory Management Micro C_OS
At the Beginning • Written By J. Labrosse • First published in 1992 • µC/OS-II is a trade mark of Micrium. • 1000s applications are using it all over the world. • A good starting point to experience real-time OS • Simple but yet very powerful Micro C_OS
µC/OS-II in Literature • Four books were published to explain the internals: • “μC/OS The Real-Time Kernel”, in 1992 • “μC/OS-II The Real-Time Kernel”, in 1998 • “μC/OS-II The Real-Time Kernel”, Second Edition in 2002 • “μC/OS-III The Real-Time Kernel”, in 2009 Micro C_OS
µC/OS-II History µC/OS V1.08 µC/OS-II V2.00 µC/OS-II V2.52 µC/OS-II V2.86 µC/OS-III µC/OS V1.08 + Memory Manager + Stack Checking + CPU Load Checking µC/OS-II V2.00 + Safety Critical + Mutexes + Semaphores + Event Flags µC/OS-II V2.52 + Timers + 250 Tasks + MMU & MPU µC/OS-II V2.86 + RR Scheduling + Infinite # of Tasks & Services Micro C_OS
µC/OS-II Features • Source code • Portable • Preemptive • Multitasking • Task stack • Services • Interrupt management • Robust & reliable • … Micro C_OS
Source Code • High quality • Neat • Consistent • Organized • Commented • MISRA-C compliant Micro C_OS
Source Code • Highly portable ANSI C • Assembly is kept minimum • Supports 8-, 16-, 32-, 64- bit processors • Ported over 100 different processors • All ports are freely available at Micrium website Micro C_OS
Amr Ali Abdel-Naby@2010 Introduction to uCOS-II V2.6 Micro C_OS
Preemptive • Fully preemptive • Always runs the highest priority task that is ready to run Micro C_OS
Multitasking • Manages up to 64 tasks • 8 tasks are used by µC/OS-II • 56 tasks are left to applications • A unique task/priority • RR and FIFO are not supported Micro C_OS
Task Stacks • Each task has its stack • μC/OS-II allows different stack sizes • Stack checking utility • You can determine & decide how much stack is needed for each task Micro C_OS
Task Stacks • Each task has its stack • μC/OS-II allows different stack sizes • Stack checking utility • You can determine & decide how much stack is needed for each task Micro C_OS
Services • Semaphores • Mutexes • Event flags • Message queues • Mailboxes • Fixed size memory partitions • Task management • Time management Micro C_OS
Interrupt Management • An interrupt can suspend a task execution • The highest priority ready task runs after serving the interrupt • Nested interrupts • Up to 255 level Micro C_OS
Robust & Reliable • Used by many products • Support • Tested & certified in safety critical systems Micro C_OS
Related Products • µC/Probe • µC/TCP-IP • µC/FS • µC/GUI • µC/USB • µC/CAN • µC/Modbus • µC/FL • µC/Building Blocks • µC/OSEK-VDX Micro C_OS
µC/OS-II Market • Distributors all over the word • Huge customer list Micro C_OS
License • Educational • Free • Universities • Non-profitable use • Source Code • If µC/OS will be distributed with your product as a source code • Object Code • If µC/OS will be distributed with your product as a binary • Not royalty free Micro C_OS
Outline • Introduction to µC/OS-II • Kernel Structure • Task Management • Time Management • Semaphore Management • Mutual Exclusion Semaphores • Event Flag Management • Message Mailbox Management • Message Queue Management • Memory Management Micro C_OS
Outline • Introduction to µC/OS-II • Kernel Structure • Task Management • Time Management • Semaphore Management • Mutual Exclusion Semaphores • Event Flag Management • Message Mailbox Management • Message Queue Management • Memory Management Micro C_OS
Kernel Architecture Application SW (Your Code) µC/OS-II (Processor Independent code) µC/OS-II Configuration (Application Specific) µC/OS-II Port (Processor Specific Code) HW Micro C_OS
Critical Sections • 2 macros protect critical sections • They enable/disable interrupts • Can be used by your application • Not a good programming style • Applications may crash • Processor & tool chain specific OS_ENTER_CRITICAL(); /* Critical Code */ OS_EXIT_CRITICAL(); Micro C_OS
Tasks • Distributors all over the word • Huge customer list • Lowest priority is defined as OS_LOWEST_PRIO May be used in the future extension of μC/OS-II 0 1 /* Endless Loop Task */ void My_Task (void * pdata){ for(;;){ /* Your Code */ } } 2 3 … Up to 56 application tasks /* Run To Completion Task */ void My_Task (void * pdata){ /* Your Code */ OSTaskDel(OS_PRIO_SELF); } Priority 60 61 Used by the system 62 63 Micro C_OS
Task States Micro C_OS
Task Control Blocks • µC/OS-II uses TCBs for task management • Every task is assigned a TCB when created Micro C_OS
Task Control Blocks cont’d • A TCB contains: • Task’s priority • Task’s state • A pointer to the task’s top of stack • Other task’s related data • TCBs reside in RAM Micro C_OS
Ready List • The kernel maintains a list of tasks that can be ready to run • The highest priority task is kept at the beginning of the task Micro C_OS
Task Scheduling • Task-level scheduling is performed by OS_Sched • ISR-level scheduling is handled by OSIntExit • Task Level context switch Micro C_OS
Locking & Unlocking the Scheduler • A mechanism used by a task to keep control of the CPU, even if there are higher priority tasks ready • Two kernel services are provided & must be used in pairs: • OSSchedLock & OSSchedUnlock • After calling OSSchedLock, your application should not call a service that suspends execution • Your application will crash HPT LPT LPT HPT is ready here OSSchedLock OSSchedUnlock Micro C_OS
Idle Task • Executed when there is no other task ready to run • Always set to the lowest priority Micro C_OS
Statistics Task • Its priority is higher than IDLE task by 1 • It provides runtime statistics • It is called OS_TaskStat & it is called every second • It tells you how long was the CPU used by your application • To use it, you must call OSStatInit from the first & the only task created during initialization Micro C_OS
Statistics Task void main(void){ OSInit(); ... Create your startup task TaskStart() ... OSStart(); } void TaskStart(void * pdata){ OSStatInit(); ... } Micro C_OS
Interrupts Under µC/OS-II • You should keep ISRs as short as possible • Interrupts either use an interrupt stack or task stack • Stack size must account for: • ISR nesting • Function call nesting • Local variables Micro C_OS
Clock Tick • µC/OS-II requires a periodic time source to keep track of time delays & timeouts • Ticker interrupts must be enabled after starting multitasking • The clock tick is serviced by calling OSTimeTick from the tick ISR void main(void){ OSInit(); ... Enable Ticker Interrupt /* Mistake */ ... OSStart(); } Micro C_OS
µC/OS-II Initialization • µC/OS-II requires that OSInit is called before any other service • It initializes all µC/OS-II variables & data structures • It creates idle & statistics tasks void main(void){ OSInit(); ... OSStart(); } Micro C_OS
Starting µC/OS-II • Multitasking is started by calling OSStart after creating at least 1 task void main(void){ OSInit(); ... Create at least 1 task here ... OSStart(); } Micro C_OS
Obtaining the Current Version • By calling OSVersion Micro C_OS
Outline • Introduction to µC/OS-II • Kernel Structure • Task Management • Time Management • Semaphore Management • Mutual Exclusion Semaphores • Event Flag Management • Message Mailbox Management • Message Queue Management • Memory Management Micro C_OS
Outline • Introduction to µC/OS-II • Kernel Structure • Task Management • Time Management • Semaphore Management • Mutual Exclusion Semaphores • Event Flag Management • Message Mailbox Management • Message Queue Management • Memory Management Micro C_OS
Creating a Task, OSTaskCreate • INT8U OSTaskCreate (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT8U prio) • task: A pointer to the task's code • pdata: A pointer to an optional data area which can be used to pass parameters to the task when the task first executes • ptos: A pointer to the task's top of stack • prio: The task's priority • Return value: • No error • Priority exist • Invalid priority Micro C_OS
Creating a Task, OSTaskCreateExt • INT8U OSTaskCreateExt (void (*task)(void *pd), void *pdata, OS_STK *ptos, INT8U prio, INT16U id, OS_STK *pbos, INT32U stk_size, void *pext, INT16U opt) • OSTaskCreate + • id: Task’s ID, not used currently • pbos: A pointer to the task's bottom of stack • stk_size: A pointer to the task's top of stack • pext: A pointer to a user supplied memory location which is used as a TCB extension • opt: Additional information (or options) about the behavior of the task Micro C_OS
Task Stacks • Each task has its own stack. • It must be: • Declared as OS_STK • Consistent & contiguous memory • It can be allocated static • Or dynamic OS_STK MyTaskSTack[stack_size]; OS_STK *pstk; ... pstk = (OS_STK*)malloc(stack_size); Micro C_OS
Naming a Task, OSTaskNameSet • void OSTaskNameSet (INT8U prio, char *pname, INT8U *err) • prio: The priority of the task that you want to assign a name to • pname: A pointer to an ASCII string that contains the name of the task • err: • No error • Task does not exist. • Name too long • A null pointer is passed for the name. • Invalid priority Micro C_OS
Outline • Introduction to µC/OS-II • Kernel Structure • Task Management • Time Management • Semaphore Management • Mutual Exclusion Semaphores • Event Flag Management • Message Mailbox Management • Message Queue Management • Memory Management Micro C_OS
Outline • Introduction to µC/OS-II • Kernel Structure • Task Management • Time Management • Semaphore Management • Mutual Exclusion Semaphores • Event Flag Management • Message Mailbox Management • Message Queue Management • Memory Management Micro C_OS
Time Management APIs • Available time operations are: • Delaying a task • Resuming a delayed task • System time getting & setting Micro C_OS