• 沒有找到結果。

Task Control Blocks (OS_TCBs)

在文檔中 µC/OS-II Goals Preface (頁 82-85)

Kernel Structure

3.03 Task Control Blocks (OS_TCBs)

When a task is created, it is assigned a Task Control Block, OS_TCB (see Listing 3.3). A task control block is a data structure that is used by µC/OS-II to maintain the state of a task when it is preempted. When the task regains control of the CPU the task control block allows the task to resume execution exactly where it left off. All OS_TCBs reside in RAM. You will notice that I organized the fields in OS_TCB to allow for data structure packing while maintaining a logical grouping of members. An OS_TCB is initialized when a task is created (see Chapter 4, Task Management).

Below is a description of each field in the OS_TCB data structure.

typedef struct os_tcb {

OS_STK *OSTCBStkPtr;

#if OS_TASK_CREATE_EXT_EN void *OSTCBExtPtr;

OS_STK *OSTCBStkBottom;

INT32U OSTCBStkSize;

INT16U OSTCBOpt;

INT16U OSTCBId;

#endif

struct os_tcb *OSTCBNext;

struct os_tcb *OSTCBPrev;

#if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN || OS_SEM_EN OS_EVENT *OSTCBEventPtr;

#endif

#if (OS_Q_EN && (OS_MAX_QS >= 2)) || OS_MBOX_EN void *OSTCBMsg;

#endif

INT16U OSTCBDly;

INT8U OSTCBStat;

INT8U OSTCBPrio;

INT8U OSTCBX;

INT8U OSTCBY;

INT8U OSTCBBitX;

INT8U OSTCBBitY;

#if OS_TASK_DEL_EN

BOOLEAN OSTCBDelReq;

#endif } OS_TCB;

Listing 3.3, µC/OS-II’ s Task Control Block

OSTCBStkPtr contains a pointer to the current top of stack for the task. µC/OS-II allows each task to have its own stack but just as important, each stack can be of any size. Some commercial kernels assume that all stacks are the same size unless you write complex hooks. T his limitation wastes RAM when all tasks have different stack requirements, because the largest anticipated stack size has to be allocated for all tasks.

OSTCBStkPtr is the only field in the OS_TCB data structure accessed from assembly language code (from the context switching code). Placing OSTCBStkPtr at the first entry in the structure makes accessing this field easier from assembly language code.

OSTCBExtPtr is a pointer to a user definable task control block extension. This allows you or the user of µC/OS -II to extend the task control block without having to change the source code for µC/OS-II.

OSTCBExtPtr is only used by OSTaskCreateExt() and thus, you will need to set OS_TASK_CREATE_EXT_EN to 1 to enable this field. You could create a data structure that contains the name of each task, keep track of the execution time of the task, the number of times a task has been switched-in and more (see Example #3). Note that I decided to place this pointer immediately after the stack pointer in case you need to access this field from assembly language. This makes calculating the offset from the beginning of the data structure easier.

OSTCBStkBottom is a pointer to the task’s stack bottom. If the processor’s stack grows from high memory locations to low memory locations then OSTCBStkBottom will point at the lowest valid memory location for the stack. Similarly, if the processor’s stack grows from low memory locations to high memory locations then OSTCBStkBottom will point at the highest valid stack address. OSTCBStkBottom is used by OSTaskStkChk() to check the size of a task’s stack at run-time. This allows you determine the amount of free stack space available for each stack. Stack checking can only occur if you created a task with OSTaskCreateExt() and thus, you will need to set OS_TASK_CREATE_EXT_EN to 1 to enable this field.

OSTCBStkSize is a variable that holds the size of the stack in number of elements instead of bytes. This means that if a stack contains 1000 entries and each entry is 32-bit wide then the actual size of the stack is 4000 bytes. Similarly, a stack where entries are 16-bit wide would contain 2000 bytes for the same 1000 entries. OSTCBStkSize is used by OSTaskStkChk(). Again, this field is valid only if you set OS_TASK_CREATE_EX T_EN to 1.

OSTCBOpt is a variable that holds ‘options’that can be passed to OSTaskCreateExt() . Because of this, this field is valid only if you set OS_TASK_CREATE_EXT_EN to 1. µC/OS-II currently only defines three options (see uCOS_II.H): OS_TASK_OPT_STK_CHK, OS_TASK_OPT_STK_CLR and, OS_TASK_OPT_SAVE_FP. OS_TASK_OPT_STK_CHK is used to specify to OSTaskCreateExt() that stack checking is enabled for the task being created. OS_TASK_OPT_STK_CLR indicates that the stack needs to be cleared when the task is created. The stack only needs to be cleared if you intend to do stack checking. If you do not specify OS_TASK_OPT_STK_CLR and you create and delete tasks then, the stack checking will report incorrect stack usage. If you never delete a task once it’s created and your startup code clears all RAM then, you can save valuable execution time by NOT specifying this option. Passing OS_TASK_OPT_STK_CLR will increase the execution time of OSTaskCreateExt() because it will clear the content of the stack. The larger you stack, the longer it will take. Finally, OS_TASK_OPT_SAVE_FP tells OSTaskCreateExt() that the task will be doing floating-point computations and, if the processor provides hardware assisted floating -point capability then, the floating-point registers will need to be saved for the task being created and during a context switch.

OSTCBId is a variable that is used to hold an identifier for the task. This field is currently not used and has only been included for future expansion.

OSTCBNext and OSTCBPrev are used to doubly link OS_TCBs. This chain of OS_TCBs is used by OSTimeTick() to update the OSTCBDly field for each task. The OS_TCB for each task is linked when

the task is created and the OS_TCB is removed from the list when the task is deleted. A doubly linked list is used to permit an element in the chain to be quickly inserted or removed.

OSTCBEventPtr is a pointer to an event control block and will be described later (see Chapter 6, Intertask Communication & Synchronization ).

OSTCBMsg is a pointer to a message that is sent to a task. The use of this field will be described later (see Chapter 6, Intertask Communication & Synchronization).

OSTCBDly is used when a task needs to be delayed for a certain number of clock ticks or a task needs to pend for an event to occur with a timeout. In this case, this field contains the number of clock ticks that the task is allowed to wait for the event to occur. When this value is zero the task is not delayed or has no timeout when waiting for an event.

OSTCBStat contains the state of the task. When OSTCBStat is 0, the task is ready to run. Other values can be assigned to OSTCBStat and these values are described in uCOS_II.H.

OSTCBPrio contains the task priority. A high priority task has a low OSTCBPrio value (that is, the lower the number, the higher the actual priority).

OSTCBX, OSTCBY, OSTCBBitX and OSTCBBitY are used to accelerate the process of making a task ready to run, or to make a task wait for an event (to avoid computing these values at runtime). The values for these fields are computed when the task is created or when the task's priority is changed. The values are obtained as follows:

OSTCBY = priority >> 3;

OSTCBBitY = OSMapTbl[priority >> 3];

OSTCBX = priority & 0x07;

OSTCBBi tX = OSMapTbl[priority & 0x07];

Listing 3.4, Calculating OS_TCB members.

OSTCBDelReq is a boolean which is used to indicate whether or not a task requested that the current task be deleted. The use of this field will be described later (see Chapter 4, Task Management).

The maximum number of tasks (OS_MAX_TASKS) that an application can have is specified in OS_CFG.H and determines the number of OS_TCBs allocated by µC/OS-II for your application. You can reduce the amount of RAM needed by setting OS_MAX_TASKS to the number of actual tasks needed in your application. All OS_TCBs are placed in OSTCBTbl[] . Note that µC/OS-II allocates OS_N_SYS_TASKS (see uCOS_II.H) extra OS_TCBs for internal use. One will be used for the idle task while the other will be used for the statistic task (if OS_TASK_STAT_EN is set to 1). When µC/OS-II is initialized, all OS_TCBs in this table are linked in a singly linked list of free OS_TCBs as shown in figure 3-2. When a task is created, the OS_TCB pointed to by OSTCBFreeList is assigned to the task and, OSTCBFreeList is adjusted to point to the next OS_TCB in the chain. When a task is deleted, its OS_TCB is returned to the list of free OS_TCBs.

0

OSTCBFreeList OSTCBNext OSTCBNext OSTCBNext OSTCBNext

OSTCBTbl[0] OSTCBTbl[1] OSTCBTbl[2]

OSTCBTbl[OS_MAX_TASKS+OS_N_SYS_TASKS-1]

Figure 3-2, List of free OS_TCBs

在文檔中 µC/OS-II Goals Preface (頁 82-85)