uCOS-II Kernel Structure
uCOS-II Kernel Structure
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();
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
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
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.)
Check priority
number
if (OSRunning)
Check
OSTaskStkInit()
OSTCBPrioTbl
Check priority
OSStackCheck
number
if (OSRunning)
Check
OSTaskStkInit()
OSTCBPrioTbl
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
Waiting List