On October 23rd, 2014, we updated our
By continuing to use LinkedIn’s SlideShare service, you agree to the revised terms, so please take a few minutes to review them.
Alternatively, the task can delete itself upon completion
Also, if the task calls OSTaskDel(), the task code doesn’t return back to anything.
Task Control Blocks (OS_TCBs)
When a task is created, it is assigned a Task Control Block, OS_TCB .
A task control block is a data structure that is used by μC/OS-II to maintain the state of a task when it is preempted.
When the task regains control of the CPU the task control block allows the task to resume execution exactly where it left off.
All OS_TCBs reside in RAM.
An OS_TCB is initialized when a task is created
Each task is assigned a unique priority level between 0 and OS_LOWEST_PRIO
Task priority OS_LOWEST_PRIO is always assigned to the idle task when μC/OS-II is initialized.
Each task that is ready to run is placed in a ready list consisting of two variables, OSRdyGrp and OSRdyTbl.
Each bit in OSRdyGrp is used to indicate whenever any task in a group is ready to run.
When a task is ready to run it also sets its corresponding bit in the ready table, OSRdyTbl.
To determine which priority (and thus which task) will run next, the scheduler determines the lowest priority number that has its bit set in OSRdyTbl.
μC/OS-II always executes the highest priority task ready to run.
The determination of which task has the highest priority and thus, which task will be next to run is determined by the scheduler.
Task level scheduling is performed by OSSched().
Locking and Unlocking the Scheduler
The OSSchedLock() function is used to prevent task rescheduling until its counterpart, OSSchedUnlock(), is called.
The task that calls OSSchedLock() keeps control of the CPU even though other higher priority tasks are ready to run.
μC/OS-II alwa ys creates a task the Idle Task which is executed when none of the other tasks is ready to run.
The idle task ( OSTaskIdle()) is always set to the lowest priority, i.e. OS_LOWEST_PRIO.
OSTaskIdle() does nothing but increment a 32 -bit counter called OSIdleCtr. OSIdleCtr is used by the statistics task to determine how much CPU time (in percentage) is actually being consumed by the application software.
μC/OS-II contains a task that provides run-time statistics. This task is called OSTaskStat()
Created if you set the configuration constant OS_TASK_STAT_EN to 1.
When enabled , OSTaskStat() executes every second and computes the percentage of CPU usage.
In other words, OSTaskStat() will tell you how much of the CPU time is used by your application, in percentage.
This value is placed in the variable OSCPUUsage which is a signed 8-bit integer.
μ C/OS-II Initialization
Data structures after calling OSInit()
Starting μ C/OS-II
You start multitasking by calling OSStart() . Before you start μC/OS-II, however, you MUST create at least one of your application tasks as shown in listing
Initializing and Starting μ C/OS-II. Starting multitasking.