基本需要的头文件:
/* FREERTOS头文件 */
#include "FreeRTOS.h"
#include "task.h"
FreeRTOSConfig.h静态任务宏定义:
#define ?configSUPPORT_STATIC_ALLOCATION ?????????????????????????1
FreeRTOSConfig.h动态任务宏定义:
#define configSUPPORT_DYNAMIC_ALLOCATION?? 1
没有定义必要函数出现的错误:
.\Objects\BASE.axf: Error: L6218E: Undefined symbol vApplicationGetIdleTaskMemory (referred from tasks.o).
.\Objects\BASE.axf: Error: L6218E: Undefined symbol vApplicationGetTimerTaskMemory (referred from timers.o).
必须实现两个钩子函数: ?
static StackType_t IdleTaskStack[configMINIMAL_STACK_SIZE];
static StaticTask_t IdleTaskTCB;
void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer,
StackType_t **ppxIdleTaskStackBuffer,
uint32_t *pulIdleTaskStackSize)
{
*ppxIdleTaskTCBBuffer=&IdleTaskTCB;
*ppxIdleTaskStackBuffer=IdleTaskStack;
*pulIdleTaskStackSize=configMINIMAL_STACK_SIZE;
return;
}
static StackType_t TimerTaskStack[configTIMER_TASK_STACK_DEPTH];
static StaticTask_t TimerTaskTCB;
void vApplicationGetTimerTaskMemory( StaticTask_t ** ppxTimerTaskTCBBuffer,
StackType_t ** ppxTimerTaskStackBuffer,
uint32_t * pulTimerTaskStackSize )
{
*ppxTimerTaskTCBBuffer=&TimerTaskTCB;
*ppxTimerTaskStackBuffer=TimerTaskStack;
*pulTimerTaskStackSize=configTIMER_TASK_STACK_DEPTH;
return;
}
静态创建任务:
/* 函数参数原型
typedef struct xTASK_PARAMETERS
{
TaskFunction_t pvTaskCode; //typedef void (* TaskFunction_t)( void * );
const char * pcName;
configSTACK_DEPTH_TYPE usStackDepth;
void * pvParameters;
UBaseType_t uxPriority;
StackType_t * puxStackBuffer;
MemoryRegion_t xRegions[ portNUM_CONFIGURABLE_REGIONS ];
#if ( ( portUSING_MPU_WRAPPERS == 1 ) && ( configSUPPORT_STATIC_ALLOCATION == 1 ) )
StaticTask_t * const pxTaskBuffer;
#endif
} TaskParameters_t;
*/
#define START_TASK_STACK_SIZE 256
StaticTask_t StartTaskBuffer;
StackType_t StartTaskStack[ START_TASK_STACK_SIZE ];
#define START_TASK_PRIO ( tskIDLE_PRIORITY + 1 )
xTaskCreateStatic(
(TaskFunction_t ) Task_Init, //任务函数
(const char* ) "start_task", //任务名字
(uint32_t ) START_TASK_STACK_SIZE, //任务堆栈
(void* ) 1, //传入任务参数
(UBaseType_t ) START_TASK_PRIO, //任务优先级
(StackType_t* ) StartTaskStack, //任务堆栈
(StaticTask_t* ) &StartTaskBuffer
);
动态创建任务:
/* 函数原型:
BaseType_t xTaskCreate( TaskFunction_t pxTaskCode,
const char * const pcName, /*lint !e971 Unqualified char types are allowed for strings and single characters only. */
const configSTACK_DEPTH_TYPE usStackDepth,
void * const pvParameters,
UBaseType_t uxPriority,
TaskHandle_t * const pxCreatedTask )
*/
#define START_TASK_STACK_SIZE 256
#define START_TASK_PRIO ( tskIDLE_PRIORITY + 1 )
TaskHandle_t Task_Handler; //任务句柄
xTaskCreate( Task_Init,
"start_task",
START_TASK_STACK_SIZE,
NULL,
START_TASK_PRIO,
&Task_Handler );
??
|