0% found this document useful (0 votes)
1K views

uCOS-II Kernel Structure

uCOSII is a real-time kernel that can manage up to 63 tasks. It uses a priority-based preemptive scheduling algorithm. Each task has a Task Control Block (TCB) that contains information like its stack pointer, priority, and state. Interrupts and tasks can access shared resources using methods like disabling interrupts, disabling scheduling, or using semaphores to implement mutual exclusion.

Uploaded by

Vishal Patel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views

uCOS-II Kernel Structure

uCOSII is a real-time kernel that can manage up to 63 tasks. It uses a priority-based preemptive scheduling algorithm. Each task has a Task Control Block (TCB) that contains information like its stack pointer, priority, and state. Interrupts and tasks can access shared resources using methods like disabling interrupts, disabling scheduling, or using semaphores to implement mutual exclusion.

Uploaded by

Vishal Patel
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 43

uCOSII Kernel Structure

936317 許哲宇 936719 丁俊宏

uCOSII Kernel
Memory Management
OUTLINE
 Introduction
 Kernel Structure Overview
 Mutual Exclusion
 Task Management
 Interrupts
 Clock ticks
 uCOSII initialization
Introduction
 µC/OS-II, The Real-Time Kernel is a highly
portable, ROMable, very scalable, preempti
ve real-time, multitasking kernel (RTOS) fo
r microprocessors and microcontrollers.
 µC/OS-II can manage up to 63 application t
asks.
 Over 100 microprocessor ports are availabl
e to DOWNLOAD.
 Reference: http://www.ucos-ii.com/
 Jean J. Labrosse
About µC/OS-II
Structure Overview
Mutual Exclusion
 Critical section
disable interrupt
 OSSchedLock()
disable scheduling
 OSSemPend()
semaphore
Critical section
 Critical Section
void Function(void){
Shared resource region should
implement mutual exclusion
OS_ENTER_CRITICAL();
Disable Interrupt
 /*Operate shared data*/
OS_ENTER_CRITICAL()
OS_EXIT_CRITICAL();
 Enable Interrupt
}
OS_EXIT_CRITICAL()
 Defined in OS_CPU.H
OSSchedLock()
void Function (void)
{
OSSchedLock();

/* You can access shared data in here (interrupts


are recognized) */

OSSchedUnlock();
}
OSSemPend()
OS_EVENT *SharedDataSem;
void Function (void)
{
INT8U err;
OSSemPend(SharedDataSem, 0, &err);
.
. /* You can access shared data in here (int
errupts are recognized) */
.
OSSemPost(SharedDataSem);
}
Task Management
 Task introduction
 Task format
 Task scheduling
Task introduction
 Task is a single instance of program
 Task thinks it has all CPU control itself
 Task has its own stack and own set of CPU
registers backup in its stack
 Task is assigned a unique priority (highest
0 ~ lowest 63)
 Task is an infinite loop and never returns
􀁺 Task has states
 µC/OS-II saves task records in Task Control
Block(TCB)
Task format
Task
void
 is an infinite
YourTask loop.
(void *pdata){
 Return
for (;;) {typeis void because nothing
will
/*be returned.
User Code Text */
 Parameter isSystem
a voidCall*/
OSMboxPend();
/*uCOS-II pointer type
uCOS-II so Call
System
that
/* you
User can
Codepass
Text anything
*/ you want.
OSQPend();
} OSSemPend(); OSTaskDel(OS
_PRIO_SELF); OSTaskSuspen
} d(OS_PRIO_SELF); OSTimeDl
y();
OSTimeDlyHMSM();
Task scheduling
 Task State Transfer Diagram (TSTD)
 Task Control Block (TCB)
 Task Scheduler
 Task Pending
 Task Creation
 Task Deletion
TSTD

♦ Running – task has control of the processor an


d executing its job
♦ Ready – task is ready to execute but its priority
is less than the running task
♦ Waiting – task requires the occurrence of an ev
ent to continue
♦ ISR – task is paused because the processor is
handling an interrupt
♦ Dormant – task resides in memory, but not see
n by the scheduler
TCB
 A TCB contains task stack pointer,
priority number , delay time value , task
state , and some useful information for
this task.

OSTCBStkPtr OSTCBExtPtr OSTCBStkBottom OSTCBStkSize

OSTCBOpt OSTCBId OSTCBNext OSTCBPrev

OSTCBEventPtr OSTCBMsg OSTCBDly OSTCBStat

OSTCBPrio OSTCBX OSTCBY OSTCBBitX

OSTCBBitY OSTCBDelReq
TCB (cont.)
StkPtr ExtPtr StkBottom StkSize
Opt Id Next Prev
EventPtr Msg Dly Stat
Prio X Y BitX
BitY DelReq
Delay time
Event Msg State
Delete request
Stack
OSTCBList
Next Next Next 0
0 Prev Prev Prev Priority information
TCB (cont.)

OSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]

OSTCBFReeList
Task Scheduling
& Context Switch
 In µC/OS-II, task scheduling if performed o
n following conditions:
♦ A task is created/deleted
♦ A task changes state
− On interrupt exit
− On post signal
− On pending event
− On task suspension
 If the scheduler chooses a new task to run,
context switch occurs.
Task Scheduler
 Preemptive Kernel
 Not support time quantum
Task Scheduler (cont.)
 Principle : select the highest priority
task from Ready List.
 How to select the priority task from a
Ready List? Double-linked
List with
Get the highest priority priority field
Modify priority state Ready List Priority

TASK 1 TASK 2 TASK 3 TASK 4


OSTCBList
NEXT NEXT NEXT NEXT 0
0 PREV PREV PREV PREV
Priority Priority Priority Priority
Task Scheduler (cont.)
TASK 1 TASK 2 TASK 3 TASK 4
OSTCBList
NEXT NEXT NEXT NEXT 0
0 PREV PREV PREV PREV
Priority Priority Priority Priority

Method 1
Get the highest priority
Get the First one in the Ready List
Time Complexity : O(1)
Modify priority state
Selection sort for each change
Time Complexity : O(N)
Task Scheduler (cont.)
TASK 1 TASK 2 TASK 3 TASK 4
OSTCBList
NEXT NEXT NEXT NEXT 0
0 PREV PREV PREV PREV
Priority Priority Priority Priority

Method 2
Get the highest priority
Use a Ready Table , a Priority TCB Table and
one variable to select
Time Complexity : O(1)
Modify priority state
Maintain a Ready Table , a Priority TCB Tabke
and one variable
Time Complexity : O(1)
Task Scheduler (cont.)

TASK 1 TASK 2 TASK 3 TASK 4


OSTCBList
NEXT NEXT NEXT NEXT 0
0 PREV PREV PREV PREV
Priority Priority Priority Priority
Task Scheduler (cont.)
Get the highest priority
0 0 0 0 0 1 0 0
OSRdyGrp 0 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1 0 0 1 1 0 1 0 0
0 0 0 0 0 0 0 0
0 1 0 0 0 0 1 0
TASK 1 TASK 2
OSTCBList 1TASK
0 30 TASK 4
0 0 0 1 1
NEXT NEXT 0NEXT
0 0 0NEXT
0 0 00 0
0 PREV PREV PREV PREV
Priority Priority 1 0 0
Priority 0 0
Priority 0 0 0
Task Scheduler (cont.)
Modify priority state

OSRdyGrp |= OSMapTbl[prio >> 3];

OSRdyTbl[prio >> 3] |= OSMapTbl[prio & 0x07];


Task Scheduler (cont.)
OSRdyGrp 0 0 0 0 0 1 0 0
1 0 1 1 1
0 1 0 1 0 0 031 0 0 0 0 0
0 0 1 1 0 1 0 0
1
0 0 0 0 0 0 0 0
Task Priority = 31
0 1 0 0 0 0 1 0
0 0 0 1 1 1 1 1
1 0 0 0 0 0 1 1
3 7 0 0 0 0 0 0 0 0
1 0 0 0 0 0 0 0
OSSched(void)
void OSSched (void)
{
INT8U y;
OS_ENTER_CRITICAL();
if ((OSLockNesting | OSIntNesting) == 0) {
y = OSUnMapTbl[OSRdyGrp];
OSPrioHighRdy = (INT8U)((y << 3) + OSUnMapTbl[OSRdyTbl[y]]);
if (OSPrioHighRdy != OSPrioCur) {
OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy];
OSCtxSwCtr++;
OS_TASK_SW();
}
}
OS_EXIT_CRITICAL();
}
Task Pending
 Task pending occur when interrupt
OSMBoxPend()
OSQPend()
raise.
OSSemPend()
 When waiting some event , message
or semaphore. OSTaskSuspend()
 When one task delays itself.
OSTimeDly()
OSTimeDlyHMSM()
 When one task is preempted by
higher priority task.
Task Creation
 OSTaskCreate()
 OSTaskCreateExt()
 Create task before OSStart() or in Ru
nning state
 But not permit to create task in ISR
OSTaskCreate()

Check priority
number
if (OSRunning)

Check
OSTaskStkInit()
OSTCBPrioTbl

OSTCBInit() OSTaskCtr++ OSSched()


OSTaskCreateExt()

Check priority
OSStackCheck
number
if (OSRunning)

Check
OSTaskStkInit()
OSTCBPrioTbl

OSTCBInit() OSTaskCtr++ OSSched()


Task Deletion

TASK 1 TASK 2 TASK 3 TASK 4


OSTCBList
NEXT NEXT NEXT NEXT 0
0 PREV PREV PREV PREV
Priority Priority Priority Priority
Interrupts
 Interrupt Service Routine
Step1:save CPU registers
Step2:call OSIntEnter () or
OSIntNesting add one directly
Step3:execution the service code
Step4:call OSIntExit()
Step5:restore CPU registers
Step6:call interrupt return instruction
Context switch
Clock ticks
 10-100Hz
 void OSTickISR(void)
 OSTimeTick()
 OSTimeTick() execution time is in pro
portion to number of Task.
TASK 1 TASK 2 TASK 3 TASK 4
OSTCBList
NEXT NEXT NEXT NEXT 0
0 PREV PREV PREV PREV
OSTCBDly OSTCBDly OSTCBDly OSTCBDly
OSTimeTick()
if (ptcb->OSTCBDly != 0) {
if (--ptcb->OSTCBDly == 0) {
if (!(ptcb->OSTCBStat & OS_STAT_SUSPEND)) {
OSRdyGrp |= ptcb->OSTCBBitY;
OSRdyTbl[ptcb->OSTCBY] |= ptcb->OSTCBBitX;
} else {
ptcb->OSTCBDly = 1;
}
}
}
Time Management
 Using clock tick to implement time del
ays and timeouts.
 OSTimeDly()
 OSTimeDlyHMSM()
 OSTimeDlyResume()
 OSTimeGet()
 OSTimeSet()
 Defined in OS_Time.C
OSTimeDly()
void OSTimeDly (INT16U ticks)
{
if (ticks > 0) {

OS_ENTER_CRITICAL();
if ((OSRdyTbl[OSTCBCur->OSTCBY]
&= ~OSTCBCur->OSTCBBitX) == 0) {
OSRdyGrp &= ~OSTCBCur->OSTCBBitY;
}
OSTCBCur->OSTCBDly = ticks;

OS_EXIT_CRITICAL();
OSSched();
uCOSII initialization
 OSIint() initialize all variables and dat
a structure for system.
 Defined in OS_CORE.C
OSPrioCur =0
OSPrioHighRdy =0
OSTCBCur = NULL
OSTCBHighRdy = OL
OSTime =0
OSIntNesting =0
OSLockNesting =0
OSCtxSwCtr =0
OSTaskCtr =2
OSRunning = FALSE
OSCPUUsage =0
OSIdleCtrMax = 0L
OSIdleCtrRun = 0L
OSIdleCtr = 0L
OSStatRdy = FALSE
Free TCB List

Free Event List

Free Queue List

Free Memory List


Create a Task. OSPrioCur =6
Priority = 6 OSPrioHighRdy =6
OSTCBCur
OSTCBHighRdy
OSTime =0
OSIntNesting =0
OSLockNesting =0
OSCtxSwCtr =0
OSTaskCtr =3
OSRunning = TRUE
OSCPUUsage =0
OSIdleCtrMax = 0L
OSIdleCtrRun = 0L
OSIdleCtr = 0L
OSStatRdy = FALSE
Message Mail boxes

Waiting List

You might also like