FreeRTOS任务特性
- 简单
- 没有使用限制
- 支持抢占
- 支持优先级
- 每个任务有拥有堆栈导致了RAM使用量曾大
- 如果使用抢占的话必须仔细考虑重入的问题
FreeRTOS任务状态
- 1运行态
- 2就绪态
- 3阻塞态
- 4挂起态
阻塞态、就绪态、运行态,切换到挂起态:vTaskSuspend() called
阻塞态切换到就绪态:Event
挂起态切换到就绪态:vTaskResume() called
运行态切换到阻塞态:调用阻塞API函数
FreeRTOS任务优先级
任务优先级决定了任务的执行优先级别
范围: 0 ~ configMAX_PRIORITIES-1
数字越大,优先级别越高
FreeRTOS任务实现
任务的具体的工作内容,由用户自己编写
viod vTaskFub(void *pvParameters)
{
while(1)
{
// 工作内容写在这里
vTaskDelay(1000)
}
vTaskDelete(NULL); //删除此任务
}
不能从任务函数中返回或退出,从任务函数中返回或退出会调用configASSERT()
,前提是用户定义了configASSERT()
;
如果一定要从任务函数中退出的话那一定要调用vTaskDelete(NULL)
来删除此任务。
FreeRTOS任务控制块
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. */
#endif
ListItem_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 ) || ( configRECORD_STACK_HIGH_ADDRESS == 1 ) )
StackType_t *pxEndOfStack; /*< Points to the highest valid address for the stack. */
#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 not
used by the FreeRTOS maintainers themselves. FreeRTOS is not
responsible for resulting newlib operation. User must be familiar with
newlib and must provide system-wide implementations of the necessary
stubs. Be warned that (at the time of writing) the current newlib design
implements 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 of
tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE. */
#if( tskSTATIC_AND_DYNAMIC_ALLOCATION_POSSIBLE != 0 ) /*lint !e731 Macro has been consolidated for readability reasons. */
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任务堆栈
任务堆栈用来保存任务现场(CPU寄存器值),创建任务的时候需要指定任务堆栈,任务堆栈的变量类型为StackType_t
,此变量类型如下:
#deinfe portSTACK_TYPE uint32_t
#deinfe portBASE_TYPE long
typedef portSTACK_TYPE StackType_t
注意:创建任务如果分配为50,则实际分配的内存空间为50*4 = 200字节。因为一个uint32_t
占4字节。
堆栈大小的分配,要按照实际情况来判断,例如分配50,然而在任务实现中,定义了一个 uint8_t temp[500];
这显然会导致堆栈溢出。