Kernel Structure• This chapter describes some of the structural
aspects of μC/OS-II. • How μC/OS-II handles access to critical sections
of code,• What a task is, and how μC/OS-II knows about
your tasks,• How tasks are scheduled,• How μC/OS-II can determine how much of CPU
your application is using,• How do to write Interrupt Service Routines (ISRs),• What a clock tick is and how μC/OS-II handles it,• How to initialize μC/OS-II and,• How to start multitasking.
OS_ENTER_CRITICAL() and OS_EXIT_CRITICAL(),
• μC/OS-II like all real-time kernels need to disable interrupts in order to access critical sections of code, and re-enable interrupts when done.
• This allows μC/OS-II to protect critical code from being entered simultaneously from either multiple tasks or ISRs.
• To hide the implementation method chosen by the compiler manufacturer, μC/OS-II defines two macros to disable and enable interrupts:
• OS_ENTER_CRITICAL() and OS_EXIT_CRITICAL• Because these macros are processor specific, they are
found in a file called OS_CPU.H. • Each processor port will thus have its own OS_CPU.H file.
Tasks
• A task is active entity which could do some computations.
• A task is typically an infinite loop function
• Alternatively, the task can delete itself upon completion
• Also, if the task calls OSTaskDel(), the task code doesn’t return back to anything.
Task States
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
Ready List• 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[].
Ready List
Task Scheduling• μ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.
Idle Task• μ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.
Statistics Task
• μ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.
Task Management
• Creating a Task, OSTaskCreate()• OSTaskCreate (void (*task)(void *pd), void
*pdata, OS_STK *ptos, INT8U prio)
Stack Checking, OSTaskStkChk()• It is sometimes necessary to determine how
much stack space a task actually uses.
Deleting a Task, OSTaskDel()• It is sometimes necessary to delete a task.
Deleting a task means that the task will be returned to the DORMANT state
• And does not mean that the code for the task will be deleted.
• The task code is simply no longer scheduled by μC/OS -II.
• You delete a task by calling OSTaskDel()• Requesting a task to delete itself,
OSTaskDelReq()
Changing a Task’s Priority, OSTaskChangePrio()
• When you create a task, you assign the task a priority. At run-time, you can change the priority of any task by calling OSTaskChangePrio().
• In other words, μC/OS-II allows you to change priorities dynamically.
Suspending a Task, OSTaskSuspend()
• It is sometimes useful to explicitly suspend the execution of a task.
• This is accomplished with the OSTaskSuspend() function call.
• A suspended task can only be resumed by calling the OSTaskResume() function call.
Getting Information about a Task, OSTaskQuery()
• Your application can obtain information about itself or other application tasks by calling OSTaskQuery().
• In fact, OSTaskQuery() obtains a copy of the contents of the desired task’s OS_TCB.
Time Management
• This chapter will describe five services that deal with time issues:
1) OSTimeDly(),2) OSTimeDlyHMSM(),3) OSTimeDlyResume(),4) OSTimeGet() and,5) OSTimeSet().The functions described in this chapter are found in
the file OS_TIME.C.
Top Related