- 创建任务实例
1.0. 相关的 API函数
BaseType_t xTaskCreate( TaskFunction_t pvTaskCode, /* 指向实现任务功能的函数的指针。 */
const char * const pcName, /* 只是一个辅助工具,为了方便人类阅读。并不是FreeRTOS使用的。 */
uint16_t usStackDepth, /* 内核分配给任务的栈空间大小。比如参数传入100,那么将分配400字节的栈空间(100*4),这是假定栈宽为32位。 */
void *pvParameters, /* 参数类型是指向void的指针,传递到任务中的值。 */
UBaseType_t uxPriority, /* 任务的优先级,可从0(最低优先级)到(configMAX_PRIORITIES - 1) */
TaskHandle_t *pxCreatedTask /* 任务的句柄。 */
/* 返回值:pdPASS 表示任务被成功创建;pdFAIL则表示失败。 */
);
1.1. 方法一
/* 清单1 方法一使用的第一个任务 */
void vTask1(void *pvParameters)
{
const char *pcTaskName = "Task1 is running\r\n";
volatile uint32_t ul; /* 采用 volatile 确保 ul 不会被优化掉。*/
/* 和大多数任务一样,本任务在无限循环中实现。 */
for( ; ; )
{
/* 打印任务的名称。 */
vPrintString(pcTaskName);
/* 延时。非常粗糙的循环。 */
for(ul = 0; ul < mainDELAY_LOOP_COUN