Rtos 3 & 4

23
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.

description

this slide ll be helpful for those who are in search of real time systems.

Transcript of Rtos 3 & 4

Page 1: Rtos 3 & 4

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.

Page 2: Rtos 3 & 4

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.

Page 3: Rtos 3 & 4

Tasks

• A task is active entity which could do some computations.

• A task is typically an infinite loop function

Page 4: Rtos 3 & 4

• Alternatively, the task can delete itself upon completion

• Also, if the task calls OSTaskDel(), the task code doesn’t return back to anything.

Page 5: Rtos 3 & 4

Task States

Page 6: Rtos 3 & 4

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

Page 7: Rtos 3 & 4
Page 8: Rtos 3 & 4

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[].

Page 9: Rtos 3 & 4

Ready List

Page 10: Rtos 3 & 4

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().

Page 11: Rtos 3 & 4

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.

Page 12: Rtos 3 & 4

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.

Page 13: Rtos 3 & 4

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.

Page 14: Rtos 3 & 4

μC/OS-II Initialization

Page 15: Rtos 3 & 4

Data structures after calling OSInit()

Page 16: Rtos 3 & 4

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.

Page 17: Rtos 3 & 4

Task Management

• Creating a Task, OSTaskCreate()• OSTaskCreate (void (*task)(void *pd), void

*pdata, OS_STK *ptos, INT8U prio)

Page 18: Rtos 3 & 4

Stack Checking, OSTaskStkChk()• It is sometimes necessary to determine how

much stack space a task actually uses.

Page 19: Rtos 3 & 4

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()

Page 20: Rtos 3 & 4

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.

Page 21: Rtos 3 & 4

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.

Page 22: Rtos 3 & 4

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.

Page 23: Rtos 3 & 4

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.