ENGG4420 ‐‐ CHAPTER 2 ‐‐ LECTURE 3 October‐14‐10 3:10 PM
SYSTEM INITIALIZATION AND STARTING • Every RTOS has some specific steps for system initiali...
SYSTEM INITIALIZATION AND STARTING • Every RTOS has some specific steps for system initialization and starting. Here we present the initialization steps for the uC/OS‐II. • A requirement of uC/OS is that you call OSInit( ) before you call any of uC/OS’s other services. ○ OSInit( ) initializes all uC/OS variables and data structures (see OS_CORE.C). ○ OSInit( ) creates the idle task OS_TaskIdle( ), which is always ready to run. The priority of OS_TaskIdle( ) is always set to OS_LOWEST_PRIO. ○ If OS_TASK_STAT_EN and OS_TASK_CREATE_EXT_EN (see OS_CFG.H) are both set to 1, OSInit( ) also creates the statistic task OS_TaskStat( ) and makes it ready to run. The priority of OS_TaskStat( ) is always set to OS_LOWEST_PRIO‐1.
First
OS_CFG.H OSInit() OS_TaskIdle()
always ready to run
OS_TaskStat() ready to run
CHAPTER 2 BY RADU MURESAN Page 1
Other uC/OS services
Figure
Variables and data structures after calling OSInit().
• This illustration assumes that the following #define constants are set as follows in OS_CFG.H: OS_TASK_STAT_EN = 1; OS_FLAG_EN = 1; OS_LOWEST_PRIO = 63; OS_LOWEST_PRIO1 = 62; OS_MAX_TASKS = 63. • Here we see the values for OSRdyGrp, OSRdyTbl[], OSTCBPrioTbl[] and the initial variable values. • (4) Because both tasks (Idle and Stat) are ready to run, their corresponding bits in OSRdyTbl[ ] are set to 1. • (5) Also, because the bits of both tasks are on the same row in OSRdyTbl[ ] only one bit in OSRdyGrp is 1.
• (1) The task control blocks of the Idle and Stat tasks are chained together in a doubly linked list. • (2) OSTCBList points to the beginning of this chain ‐‐ when a task is created, it is always placed at the beginning of the list. • (3) both ends of the doubly linked list point to NULL (i.e., 0). • Because both tasks are ready to run their corresponding bits in OSRdyTb[] are set to 1; also only one bit in OSRdyGrp is set to 1. ... Q: Why? A: Each bit in OSRdyGrp corresponds to one row in OSRdyTbl[ ].
CHAPTER 2 BY RADU MURESAN Page 3
• uC/OS‐II also initializes five pools of free data structure, as shown in the next slide. Each of these pools is a singly linked list and allows uC/OS‐II to obtain and return an element from and to a pool quickly. Figure: Free Pools OS_TCB OSTCBFreeList
OS_TCB
OSTCBNEXT
OS_EVENT OSEventFreeList
OSEventPtr
OS_Q OSQFreeList
OSFlagFreeList
OSMemFreeList
OSQPtr
OS_TCB
OSTCBNEXT
OSTCBNEXT
OS_EVENT
OS_EVENT
OS_TCB OSTCBNEXT
OS_EVENT
OSEventPtr
OSEventPtr
OSEventPtr
OS_Q
OS_Q
OS_Q
OSQPtr
OSQPtr
OSQPtr
OS_FLAG_GRP
OS_FLAG_GRP
OS_FLAG_GRP
OSFlagWaitList
OSFlagWaitList
OSFlagWaitList
OSFlagWaitList
OS_MEM
OS_MEM
OS_MEM
OS_MEM
OSMemFreeList
OSMemFreeList
OSMemFreeList
0
0
0
OS_FLAG_GRP
OSMemFreeList
0
0
• After OSInit() has been called: ○ OS_TCB pool contains OS_MAX_TASKS entries ○ OS_EVENT pool contains OS_MAX_EVENTS entries ○ OS_Q pool contains OS_MAX_QS entries ○ OS_FLAG_GRP pool contains OS_MAX_FLAGS entries; and ○ OS_MEM pool contains OS_MAX_MEM_PART entries • Each of the free pools are NULL‐pointer terminated ‐‐ their size is defined in OS_CFG.H.
CHAPTER 2 BY RADU MURESAN Page 4
STARTING uC/OS‐II MULTITASKING • You start multitasking by calling OSStart( ). • Before you start uC/OS you must create at least one of your application tasks, as shown below. void main(void) { OSInit( ) /* Initialize uC/OS */ ... Create at least 1 task using OSTaskCreate( ) or OSTaskCreateExt( ); ... OSStart( ); /*Start multitasking. OSStart will not return */ }
void OSStart (void) { INT8U y; INT8U x; if (OSRunning == FALSE) { y = OSUnMapTbl[OSRdyGrp]; x = OSUnMapTbl[OSRdyTbl[y]]; OSPrioHighRdy = (INT8U)((y