任務特性
在RTOS中,一個實時應用可以作為一個獨立的任務,支持搶占,支持優先級,每個任務都有自己的堆棧,當任務切換時將上下文環境保存在堆棧中,再次調用任務時,取出上下文信息,繼續運行。
任務優先級
每一個任務都可以分配一個從0~(configMAX_PRIORITIES-1)的優先級,configMAX_PRIORITIES在FreeRTOSConfig中有定義
如果所使用的硬件平臺支持類似計算前導零這樣的指令(Cortex-M處理器支持)。并且configUSE_PORT_OPTIMISED_TASK_SELECTION設置為1,那么configMAX_PRIORITIES不能超過32。
其他情況下configMAX_PRIORITIES可以設置成任意值,但是考慮到RAM的消耗,configMAX_PRIORITIES最好設置為一個滿足應用的最小值。
優先級數字越低表示任務的優先級越低,0的優先級最低,configMAX_PRIORITIES-1的優先級最高,空閑任務的優先級最低,為0。當宏configUSE_TIME_SLICING=1時,多個任務可以共用一個優先級,數量不限。此時處于就緒態的優先級相同的任務就會使用時間片輪轉調度器獲取運行時間。
任務控制塊
FreeRTOS每個任務都有一些屬性需要存儲,FreeRTOS把這些屬性集合到一起一起用一個結構體表示,這個結構體叫做任務控制塊:TCB_t,創建任務的時候自動給每個任務分配一個任務控制塊,此結構體在task.c中定義:如下:
typedef struct tskTaskControlBlock
{volatile StackType_t *pxTopOfStack; /*< Points to the location of the last item placed on the tasks stack. THIS MUST BE THE FIRST MEMBER OF THE TCB STRUCT. */#if ( portUSING_MPU_WRAPPERS == 1 )xMPU_SETTINGS xMPUSettings; /*< The MPU settings are defined as part of the port layer. THIS MUST BE THE SECOND MEMBER OF THE TCB STRUCT. */#endifListItem_t xStateListItem; /*< The list that the state list item of a task is reference from denotes the state of that task (Ready, Blocked, Suspended ). */ListItem_t xEventListItem; /*< Used to reference a task from an event list. */UBaseType_t uxPriority; /*< The priority of the task. 0 is the lowest priority. */StackType_t *pxStack; /*< Points to the start of the stack. */char pcTaskName[ configMAX_TASK_NAME_LEN ];/*< Descriptive name given to the task when created. Facilitates debugging only. */ /*lint !e971 Unqualified char types are allowed for strings and single characters only. */#if ( portSTACK_GROWTH > 0 )StackType_t *pxEndOfStack; /*< Points to the end of the stack on architectures where the stack grows up from low memory. */#endif#if ( portCRITICAL_NESTING_IN_TCB == 1 )UBaseType_t uxCriticalNesting; /*< Holds the critical section nesting depth for ports that do not maintain their own count in the port layer. */#endif#if ( configUSE_TRACE_FACILITY == 1 )UBaseType_t uxTCBNumber; /*< Stores a number that increments each time a TCB is created. It allows debuggers to determine when a task has been deleted and then recreated. */UBaseType_t uxTaskNumber; /*< Stores a number specifically for use by third party trace code. */#endif#if ( configUSE_MUTEXES == 1 )UBaseType_t uxBasePriority; /*< The priority last assigned to the task - used by the priority inheritance mechanism. */UBaseType_t uxMutexesHeld;#endif#if ( configUSE_APPLICATION_TASK_TAG == 1 )TaskHookFunction_t pxTaskTag;#endif#if( configNUM_THREAD_LOCAL_STORAGE_POINTERS > 0 )void *pvThreadLocalStoragePointers[ configNUM_THREAD_LOCAL_STORAGE_POINTERS ];#endif#if( configGENERATE_RUN_TIME_STATS == 1 )uint32_t ulRunTimeCounter; /*< Stores the amount of time the task has spent in the Running state. */#endif#if ( configUSE_NEWLIB_REENTRANT == 1 )/* Allocate a Newlib reent structure that is specific to this task.Note Newlib support has been included by popular demand, but is notused by the FreeRTOS maintainers themselves. FreeRTOS is notresponsible for resulting newlib operation. User must be familiar withnewlib and must provide system-wide implementations of the necessarystubs. Be warned that (at the time of writing) the current newlib designimplements a system-wide malloc() that must be provided with locks. */struct _reent xNewLib_reent;#endif#if( configUSE_TASK_NOTIFICATIONS == 1 )volatile uint32_t ulNotifiedValue;volatile uint8_t ucNotifyState;#endif/* See the comments above the definition oftskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */#if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 )uint8_t ucStaticallyAllocated; /*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */#endif#if( INCLUDE_xTaskAbortDelay == 1 )uint8_t ucDelayAborted;#endif} tskTCB;/* The old tskTCB name is maintained above then typedefed to the new TCB_t name
below to enable the use of older kernel aware debuggers. */
typedef tskTCB TCB_t;
任務堆棧
FreeRTOS之所以能正確恢復一個任務的運行就是有任務堆棧,創建任務的時候需要給任務指定堆棧,如果使用xTaskCreate創建任務的話,任務堆棧就會由函數xTaskCreate自動創建。如果使用xTaskCreateStatic創建任務,就需要我們自行定義任務堆棧。
任務堆棧的數據類型是StackType_t,StackType_t本質上是uint32_t類型,在portmacro.h中有定義,所以StackType_t是4字節,實際堆棧大小是我們定義的4倍。