# FreeRTOS创建任务（1）

FreeRTOS操作系统 专栏收录该内容
23 篇文章 5 订阅

## 二、任务状态

FreeRTOS中的任务永远只有运行态、就绪态、阻塞态、挂起态。
1、运行态

2、就绪态

3、阻塞态

4、挂起态

## 三、任务优先级

configMAX_PRIORITIES在文件FreeRTOSConfig.h中有定义，前面我们讲解FreeRTOS系统配置的时候已经讲过了。如果所使用的硬件平台支持类似计算前导零这样的指令（可以通过该指令选择下一个要运行的任务，Cortex-M处理器是支持该指令的），

## 四、任务实现

void vATaskFunction(void *pvParameters){          (1)
for(;;){                                      (2)
--任务应用程序--                           (3)
}
}

• (1)、任务函数本质也是函数，所以肯定有任务名什么的，不过这里要注意：任务函数的返回类型一定要为void类型，也就是无返回值，而且任务的参数也是void指针类型的！任务函数名可以根据实际情况定义。
• (2)、任务的具体执行过程是一个大循环，for(;;)就代表一个循环，作用和while(1)一样，笔者习惯用while(1)。
• (3)、循环里面就是真正的任务代码了，此任务具体要干的活就在这里实现！
• (4)、FreeRTOS的延时函数，此处不一定要用延时函数，其他只要能让FreeRTOS发生任务切换的API函数都可以，比如请求信号量、队列等，甚至直接调用任务调度器。只不过最常用的就是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 )
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 )
#endif

#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

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 )
uint8_t	ucStaticallyAllocated; 		/*< Set to pdTRUE if the task is a statically allocated to ensure no attempt is made to free the memory. */
#endif

uint8_t ucDelayAborted;
#endif

} tskTCB;


## 六、任务堆栈

TaskHandle_t xTaskCreateStatic(	TaskFunction_t pxTaskCode,
const char * const pcName,
const uint32_t ulStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
StackType_t * const puxStackBuffer,
StaticTask_t * const pxTaskBuffer ) /*lint !e971 Unqualified char types are allowed for strings and single characters only. */

• 2
点赞
• 1
评论
• 8
收藏
• 一键三连
• 扫一扫，分享海报

10-23 1860

09-24 638
10-21 2892
09-06 2833
03-24 331
09-06 3737
05-03 3万+