uCOSII Kernel Structure936317 許哲宇 936719 丁俊宏
uCOSII KernelMemory Management
OUTLINE Introduction Kernel Structure Overview Mutual Exclusion Task Management Interrupts Clock ticks uCOSII initialization
Introduction microCOS-II The Real-Time Kernel is a highly portable ROMable very scalable preemptive real-time multitasking kernel (RTOS) for microprocessors and microcontrollers microCOS-II can manage up to 63 application tasks Over 100 microprocessor ports are available to DOWNLOAD Reference httpwwwucos-iicom Jean J Labrosse
About microCOS-II
Structure Overview
Mutual Exclusion Critical sectiondisable interrupt OSSchedLock()disable scheduling OSSemPend()semaphore
Critical section Critical Section
Shared resource region should implement mutual exclusion
Disable InterruptOS_ENTER_CRITICAL()
Enable InterruptOS_EXIT_CRITICAL()
Defined in OS_CPUH
void Function(void) OS_ENTER_CRITICAL() Operate shared data OS_EXIT_CRITICAL()
OSSchedLock()void Function (void) OSSchedLock()
You can access shared data in here (interrupts are recognized)
OSSchedUnlock()
OSSemPend() OS_EVENT SharedDataSemvoid Function (void) INT8U err OSSemPend(SharedDataSem 0 amperr) You can access shared data in here (interrupts 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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
OUTLINE Introduction Kernel Structure Overview Mutual Exclusion Task Management Interrupts Clock ticks uCOSII initialization
Introduction microCOS-II The Real-Time Kernel is a highly portable ROMable very scalable preemptive real-time multitasking kernel (RTOS) for microprocessors and microcontrollers microCOS-II can manage up to 63 application tasks Over 100 microprocessor ports are available to DOWNLOAD Reference httpwwwucos-iicom Jean J Labrosse
About microCOS-II
Structure Overview
Mutual Exclusion Critical sectiondisable interrupt OSSchedLock()disable scheduling OSSemPend()semaphore
Critical section Critical Section
Shared resource region should implement mutual exclusion
Disable InterruptOS_ENTER_CRITICAL()
Enable InterruptOS_EXIT_CRITICAL()
Defined in OS_CPUH
void Function(void) OS_ENTER_CRITICAL() Operate shared data OS_EXIT_CRITICAL()
OSSchedLock()void Function (void) OSSchedLock()
You can access shared data in here (interrupts are recognized)
OSSchedUnlock()
OSSemPend() OS_EVENT SharedDataSemvoid Function (void) INT8U err OSSemPend(SharedDataSem 0 amperr) You can access shared data in here (interrupts 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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Introduction microCOS-II The Real-Time Kernel is a highly portable ROMable very scalable preemptive real-time multitasking kernel (RTOS) for microprocessors and microcontrollers microCOS-II can manage up to 63 application tasks Over 100 microprocessor ports are available to DOWNLOAD Reference httpwwwucos-iicom Jean J Labrosse
About microCOS-II
Structure Overview
Mutual Exclusion Critical sectiondisable interrupt OSSchedLock()disable scheduling OSSemPend()semaphore
Critical section Critical Section
Shared resource region should implement mutual exclusion
Disable InterruptOS_ENTER_CRITICAL()
Enable InterruptOS_EXIT_CRITICAL()
Defined in OS_CPUH
void Function(void) OS_ENTER_CRITICAL() Operate shared data OS_EXIT_CRITICAL()
OSSchedLock()void Function (void) OSSchedLock()
You can access shared data in here (interrupts are recognized)
OSSchedUnlock()
OSSemPend() OS_EVENT SharedDataSemvoid Function (void) INT8U err OSSemPend(SharedDataSem 0 amperr) You can access shared data in here (interrupts 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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
About microCOS-II
Structure Overview
Mutual Exclusion Critical sectiondisable interrupt OSSchedLock()disable scheduling OSSemPend()semaphore
Critical section Critical Section
Shared resource region should implement mutual exclusion
Disable InterruptOS_ENTER_CRITICAL()
Enable InterruptOS_EXIT_CRITICAL()
Defined in OS_CPUH
void Function(void) OS_ENTER_CRITICAL() Operate shared data OS_EXIT_CRITICAL()
OSSchedLock()void Function (void) OSSchedLock()
You can access shared data in here (interrupts are recognized)
OSSchedUnlock()
OSSemPend() OS_EVENT SharedDataSemvoid Function (void) INT8U err OSSemPend(SharedDataSem 0 amperr) You can access shared data in here (interrupts 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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Structure Overview
Mutual Exclusion Critical sectiondisable interrupt OSSchedLock()disable scheduling OSSemPend()semaphore
Critical section Critical Section
Shared resource region should implement mutual exclusion
Disable InterruptOS_ENTER_CRITICAL()
Enable InterruptOS_EXIT_CRITICAL()
Defined in OS_CPUH
void Function(void) OS_ENTER_CRITICAL() Operate shared data OS_EXIT_CRITICAL()
OSSchedLock()void Function (void) OSSchedLock()
You can access shared data in here (interrupts are recognized)
OSSchedUnlock()
OSSemPend() OS_EVENT SharedDataSemvoid Function (void) INT8U err OSSemPend(SharedDataSem 0 amperr) You can access shared data in here (interrupts 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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Mutual Exclusion Critical sectiondisable interrupt OSSchedLock()disable scheduling OSSemPend()semaphore
Critical section Critical Section
Shared resource region should implement mutual exclusion
Disable InterruptOS_ENTER_CRITICAL()
Enable InterruptOS_EXIT_CRITICAL()
Defined in OS_CPUH
void Function(void) OS_ENTER_CRITICAL() Operate shared data OS_EXIT_CRITICAL()
OSSchedLock()void Function (void) OSSchedLock()
You can access shared data in here (interrupts are recognized)
OSSchedUnlock()
OSSemPend() OS_EVENT SharedDataSemvoid Function (void) INT8U err OSSemPend(SharedDataSem 0 amperr) You can access shared data in here (interrupts 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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Critical section Critical Section
Shared resource region should implement mutual exclusion
Disable InterruptOS_ENTER_CRITICAL()
Enable InterruptOS_EXIT_CRITICAL()
Defined in OS_CPUH
void Function(void) OS_ENTER_CRITICAL() Operate shared data OS_EXIT_CRITICAL()
OSSchedLock()void Function (void) OSSchedLock()
You can access shared data in here (interrupts are recognized)
OSSchedUnlock()
OSSemPend() OS_EVENT SharedDataSemvoid Function (void) INT8U err OSSemPend(SharedDataSem 0 amperr) You can access shared data in here (interrupts 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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
OSSchedLock()void Function (void) OSSchedLock()
You can access shared data in here (interrupts are recognized)
OSSchedUnlock()
OSSemPend() OS_EVENT SharedDataSemvoid Function (void) INT8U err OSSemPend(SharedDataSem 0 amperr) You can access shared data in here (interrupts 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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
OSSemPend() OS_EVENT SharedDataSemvoid Function (void) INT8U err OSSemPend(SharedDataSem 0 amperr) You can access shared data in here (interrupts 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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
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 1048698 Task has states microCOS-II saves task records in Task Control Block(TCB)
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task format Task is an infinite loop Return type is void because nothing
will be returned Parameter is a void pointer type so
that you can pass anything you want
void YourTask (void pdata) for () User Code Text uCOS-II System Call User Code Text
OSMboxPend() uCOS-II System Call OSQPend() OSSemPend() OSTaskDel(OS_PRIO_SELF) OSTaskSuspend(OS_PRIO_SELF) OSTimeDly() OSTimeDlyHMSM()
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task scheduling Task State Transfer Diagram (TSTD) Task Control Block (TCB) Task Scheduler Task Pending Task Creation Task Deletion
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
TSTD
diams Running ndash task has control of the processor and executing its job diams Ready ndash task is ready to execute but its priority is less than the running task diams Waiting ndash task requires the occurrence of an event to continue diams ISR ndash task is paused because the processor is handling an interrupt diams Dormant ndash task resides in memory but not seen 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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
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
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
TCB (cont)StkPtr ExtPtr StkBottom StkSize Opt Id Next Prev EventPtr Msg Dly Stat Prio X Y BitX BitY DelReq
Stack
Event Msg
NextPrev NextPrev NextPrevOSTCBList
00
Delay timeState
Delete request
Priority information
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
TCB (cont)
OSTCBFReeListOSTCBtbl[OS_MAX_TASKS+OS_N_SYS_TASK]
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Scheduling amp Context Switch In microCOS-II task scheduling if performed on following conditions diams A task is createddeleted diams A task changes state minus On interrupt exit minus On post signal minus On pending event minus 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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
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
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Scheduler (cont) Principle select the highest priority
task from Ready List How to select the priority task from a
Ready List
Ready List PriorityGet the highest priority
Modify priority state
Double-linked List with
priority field
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 1Get the highest priority
Modify priority state
Get the First one in the Ready List Time Complexity O(1)
Selection sort for each changeTime Complexity O(N)
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Scheduler (cont)TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Method 2Get the highest priority
Modify priority state
Use a Ready Table a Priority TCB Table and one variable to selectTime Complexity O(1)
Maintain a Ready Table a Priority TCB Tabke and one variableTime Complexity O(1)
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Scheduler (cont)
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Scheduler (cont)Get the highest priority
0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1OSRdyGrp
0101101
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Scheduler (cont)Modify priority stateOSRdyGrp |= OSMapTbl[prio gtgt 3]OSRdyTbl[prio gtgt 3] |= OSMapTbl[prio amp 0x07]
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Scheduler (cont)0 0 0 0 0 1 0 00 0 0 0 0 0 0 00 0 1 1 0 1 0 00 0 0 0 0 0 0 00 1 0 0 0 0 1 01 0 0 0 0 0 1 10 0 0 0 0 0 0 01 0 0 0 0 0 0 0
1 0 1 1 0 1 0 1
0 0 0 1 1 1 1 1
OSRdyGrp
Task Priority = 31
3
1
7
1
31
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
OSSched(void)void OSSched (void) INT8U y OS_ENTER_CRITICAL() if ((OSLockNesting | OSIntNesting) == 0) y = OSUnMapTbl[OSRdyGrp] OSPrioHighRdy = (INT8U)((y ltlt 3) + OSUnMapTbl[OSRdyTbl[y]]) if (OSPrioHighRdy = OSPrioCur) OSTCBHighRdy = OSTCBPrioTbl[OSPrioHighRdy] OSCtxSwCtr++ OS_TASK_SW() OS_EXIT_CRITICAL()
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Pending Task pending occur when interrupt
raise When waiting some event message
or semaphore When one task delays itself When one task is preempted by
higher priority task
OSMBoxPend()OSQPend()OSSemPend()
OSTaskSuspend()OSTimeDly()OSTimeDlyHMSM()
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Creation OSTaskCreate() OSTaskCreateExt() Create task before OSStart() or in Running state But not permit to create task in ISR
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
OSTaskCreate() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
OSStackCheck
OSTaskCreateExt() Check priority
number
Check OSTCBPrioTbl OSTaskStkInit()
OSTCBInit() OSTaskCtr++ OSSched()
if (OSRunning)
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Task Deletion
TASK 1
NEXTPREV
Priority
TASK 2
NEXTPREV
Priority
TASK 3
NEXTPREV
Priority
TASK 4
NEXTPREV
Priority0
0OSTCBList
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Interrupts Interrupt Service RoutineStep1save CPU registersStep2call OSIntEnter () or OSIntNesting add one directlyStep3execution the service codeStep4call OSIntExit() Step5restore CPU registersStep6call interrupt return instruction
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Context switch
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Clock ticks 10-100Hz void OSTickISR(void) OSTimeTick() OSTimeTick() execution time is in proportion to number of Task TASK 1
NEXTPREV OSTCBDly
TASK 2NEXTPREV OSTCBDly
TASK 3NEXTPREV OSTCBDly
TASK 4NEXTPREV OSTCBDly 0
0OSTCBList
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
OSTimeTick() if (ptcb-gtOSTCBDly = 0) if (--ptcb-gtOSTCBDly == 0) if ((ptcb-gtOSTCBStat amp OS_STAT_SUSPEND)) OSRdyGrp |= ptcb-gtOSTCBBitY OSRdyTbl[ptcb-gtOSTCBY] |= ptcb-gtOSTCBBitX else ptcb-gtOSTCBDly = 1
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Time Management Using clock tick to implement time delays and timeouts OSTimeDly() OSTimeDlyHMSM() OSTimeDlyResume() OSTimeGet() OSTimeSet() Defined in OS_TimeC
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
OSTimeDly()void OSTimeDly (INT16U ticks) if (ticks gt 0)
OS_ENTER_CRITICAL() if ((OSRdyTbl[OSTCBCur-gtOSTCBY] amp= ~OSTCBCur-gtOSTCBBitX) == 0) OSRdyGrp amp= ~OSTCBCur-gtOSTCBBitY OSTCBCur-gtOSTCBDly = ticks
OS_EXIT_CRITICAL() OSSched()
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
uCOSII initialization OSIint() initialize all variables and data structure for system Defined in OS_COREC
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
OSPrioCur = 0OSPrioHighRdy = 0OSTCBCur = NULLOSTCBHighRdy = OLOSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 2OSRunning = FALSEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Free TCB List
Free Event List
Free Queue List
Free Memory List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Create a TaskPriority = 6
OSPrioCur = 6OSPrioHighRdy = 6OSTCBCur OSTCBHighRdy OSTime = 0OSIntNesting = 0OSLockNesting = 0 OSCtxSwCtr = 0OSTaskCtr = 3OSRunning = TRUEOSCPUUsage = 0OSIdleCtrMax = 0L OSIdleCtrRun = 0LOSIdleCtr = 0LOSStatRdy = FALSE
Message Mail boxes
Waiting List
Message Mail boxes
Waiting List