Embedded System Presentation Nguyễn Trần Quang Nguyễn Công Vũ 1 µC/OS-II
Micro C_OS 3
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 4
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 5
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 6
µ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 7
µC/OS-II History
µC/OS-IIV2.86
µC/OS-IIV2.52
µC/OS-IIV2.00
µC/OSV1.08
µ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 8
µC/OS-II Features
• Source code• Portable• Preemptive• Multitasking• Task stack• Services• Interrupt management• Robust & reliable• …
Micro C_OS 10
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 12
Preemptive
• Fully preemptive• Always runs the highest priority task that is
ready to run
Micro C_OS 13
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 14
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 15
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 16
Services
• Semaphores• Mutexes• Event flags• Message queues• Mailboxes• Fixed size memory partitions• Task management• Time management
Micro C_OS 17
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 18
Robust & Reliable
• Used by many products• Support• Tested & certified in safety critical systems
Micro C_OS 19
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 21
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 22
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 23
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 24
Kernel Architecture
µC/OS-II Port(Processor Specific Code)
µC/OS-II(Processor Independent
code)
µC/OS-II Configuration(Application Specific)
Application SW(Your Code)
HW
Micro C_OS 25
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 26
Tasks
• Distributors all over the word• Huge customer list
• Lowest priority is defined
as OS_LOWEST_PRIO
/* Endless Loop Task */void My_Task (void * pdata){for(;;){/* Your Code */}}
/* Run To Completion Task */void My_Task (void * pdata){/* Your Code */OSTaskDel(OS_PRIO_SELF);}
Up to 56 application tasks
0
1
2
3
…
60
61
62
63
Pri
ori
ty
Used by the system
May be used in the future
extension of μC/OS-II
Micro C_OS 28
Task Control Blocks
• µC/OS-II uses TCBs for task management• Every task is assigned a TCB when created
Micro C_OS 29
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 30
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 31
Task Scheduling
• Task-level scheduling is performed by OS_Sched
• ISR-level scheduling is handled by OSIntExit• Task Level context switch
Micro C_OS 32
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 crashLPT
HPT
LPT
OSSchedLock OSSchedUnlockHPT is ready here
Micro C_OS 33
Idle Task
• Executed when there is no other task ready to run• Always set to the lowest priority
Micro C_OS 34
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 35
Statistics Task
void main(void){OSInit();...Create your startup task TaskStart()...OSStart();}void TaskStart(void * pdata){OSStatInit();...}
Micro C_OS 36
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 37
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 38
µ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 39
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 41
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 42
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 44
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:o No erroro Priority existo Invalid priority
Micro C_OS 45
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 46
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 47
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:o No erroro Task does not exist.o Name too longo A null pointer is passed for the name.o Invalid priority
Micro C_OS 48
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 49
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 50
Time Management APIs
• Available time operations are:• Delaying a task• Resuming a delayed task• System time getting & setting
Micro C_OS 51
Delaying a Task, OSTimeDly
• void OSTimeDly (INT16U ticks)• ticks: The time delay that the task will be suspended in number of
clock ticks
time10ms
OSTickISR()
All HPT
LPT
5ms
OSTimeDly(1)
Micro C_OS 52
Delaying a Task, OSTimeDlyHMSM
• INT8U OSTimeDlyHMSM (INT8U hours, INT8U minutes, INT8U seconds, INT16U milli)• hours: The number of hours that the task will be delayed • minutes: The number of minutes• seconds: The number of seconds• milli: The number of milliseconds• Return value:o No erroro Zero delayo Invalid minutes, seconds, & milli seconds
Micro C_OS 53
Resuming a Delayed Task, OSTimeDlyResume
• INT8U OSTimeDlyResume (INT8U prio)• prio: Specifies the priority of the task to resume• Return value:o No erroro Invalid priorityo Task is not delayed.o Task does not exist.
Micro C_OS 54
System Time, OSTimeSet
• void OSTimeSet (INT32U ticks)• ticks: Specifies the new value that OSTime needs to take
Micro C_OS 55
System Time, OSTimeGet
• INT32U OSTimeGet (void)• Return value:o The current value of OSTime
Micro C_OS 56
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 57
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 58
Semaphores
• Semaphores are 16 bit unsigned integers used for resource sharing or to signal the occurrence of an event
OSSemCreate()OSSemDel()OSSemPost()
OSSemAccept()OSSemPend()OSSemQuery()
OSSemPost()
N
NOSSemAccept()
ISR
ISR
Task
Task
Micro C_OS 59
Semaphores
Functions:• Creating a Semaphore, OSSemCreate• Deleting a Semaphore, OSSemDel• Waiting on a Semaphore (Blocking), OSSemPend• Signaling a Semaphore, OSSemPost• Getting a Semaphore without Waiting, OSSemAccept• Obtaining the Status of a Semaphore, OSSemQuery• Creating a Semaphore, OSSemCreate• Obtaining the Status of a Semaphore, OSSemQuery
Micro C_OS 60
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 61
Mutexes
• They are used to gain exclusive access to resources.• They reduce priority inversion problem (priority ceiling protocol
is supported).
OSMutexCreate()OSMutexDel()OSMutexPost()
OSMutexPend()OSMutexAccept()OSMutexQuery()
Task Task
Micro C_OS 62
Creating a Mutex, OSMutexCreate
OS_EVENT *OSMutexCreate (INT8U prio, INT8U *err)• prio: Ceiling priroity• err:
o No erroro Called from ISRo Priority existso Invalid priorityo No available resources
• Return value:o Non-null for successful creationo Null for failure
Micro C_OS 63
Deleting a Mutex, OSMutexDel
OS_EVENT *OSMutexDel (OS_EVENT *pevent, INT8U opt, INT8U *err)• pevent: A pointer to the mutex to be deleted• opt: Delete options
o Delete alwayso Delete if no pending tasks
• err:o No erroro Called from ISRo Invalid optiono There are tasks pending.o pevent is null.o pevent is not a semaphore.
Micro C_OS 64
Deleting a Mutex, OSMutexDel cont’d
OS_EVENT *OSMutexDel (OS_EVENT *pevent, INT8U opt, INT8U *err)• Return value: Null if successful
Micro C_OS 65
Waiting on a Mutex (Blocking), OSMutexPend
void OSMutexPend (OS_EVENT *pevent, INT16U timeout, INT8U *err)• pevent: A pointer to the mutex to acquire• timeout:
o 0 wait for evero !0 wait with specific timeout
• err:o No erroro Timeouto pevent is not a mutex.o pevent is null.o Called from ISR
Micro C_OS 66
Signaling a Mutex, OSMutexPost
INT8U OSMutexPost (OS_EVENT *pevent)• pevent: A pointer to the mutex to release• Return value:
o No erroro Not mutex ownero Called from ISRo pevent is not a mutex.o pevent is null.
Micro C_OS 67
Getting a Mutex without Waiting, OSMutexAccept
INT8U OSMutexAccept (OS_EVENT *pevent, INT8U *err)• pevent: A pointer to the mutex to acquire • err:
o No erroro Called from ISRo pevent is not a mutex.o pevent is null.
• Return value:o 1 resource availableo 0 case of error or mutex is no available
Micro C_OS 68
Obtaining the Status of a Mutex, OSMutexQuery
INT8U OSMutexQuery (OS_EVENT *pevent, OS_MUTEX_DATA *pdata)• pevent: A pointer to the desired mutex• pdata: A pointer to the returned mutex information• Return value:
o No erroro Called from ISRo pevent is not a mutex.o pevent is null.
Micro C_OS 69
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 70
Event Flags
• Used for synchronization of tasks with the occurrence of multiples of events
• Events are groupedo 8, 16 or 32 bits per group
• Types of synchronizationo ORed: Any event occurredo ANDed: All events occured
Micro C_OS 72
Event Flags Management APIs
OSFlagPost()
OSFlagCreate ()OSFlagDel()OSFlagPost () OSFlagAccept ()
OSFlagPend ()OSQFlagQuery ()
ISR
Task
ISR
Task
OSFlagAccept ()OSQFlagQuery ()
Micro C_OS 73
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 74
Message Mailboxes
• A message mailbox is a μC/OS-II object that allows a task or an ISR to send pointer-sized variable to another task.
OSMboxCreate()
MessageOSMboxPost()OSMboxAccept()
OSMboxPost()
OSMboxPend()OSMboxAccept()OSMboxQuery()
ISR
Task
Task
Micro C_OS 75
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 76
Message Queues
• A message queue is a μC/OS-II object that allows a task or an ISR to send pointer-sized variables to another task.
OSQCreate()
Message
OSQPost()OSQPostFront()OSQFlush()OSQAccept()OSQPostOpt()
OSQPost()OSQPostFront()OSQFlush()OSQPostOpt()
OSQPend()OSQAccept()OSQQuery()
ISR
Task
Task
Micro C_OS 77
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 78
Memory Management
• Fixed-size memory block managemento To prevent fragmentation
• Multiple partitions can be created with different sizes.
• Blocks allocated from certain partitions must be returned back to the same partitions