赞
踩
最近熟悉一款基于M4内核的国产芯片,移植FreeRTOS v10.3.1,学习并实践任务启动的流程,做如下实践笔记。
系统的移植部分不做详细说明,移植完成后如下图所示。
代码如下(示例):
//任务必须是死循环,且无返回值。 static void Task1_Crt(void) { printf("create task1.\n"); while(1) { printf("task1.\n"); vTaskDelay(500); } } static void Task2_Crt(void) { printf("create task2.\n"); while(1) { printf("task2.\n"); vTaskDelay(1000); } }
则需要优先定义Stack,栈空间从SRAM分配,地址由编译器决定。
代码如下(示例):
//main.c
static StackType_t CreateAppTask_Stack[128]; //定义任务栈
//FreeRTOSConfig.h
/* Memory allocation related definitions. */
//1: 内存静态分配
#define configSUPPORT_STATIC_ALLOCATION 0
则需定义Heap内存,从SRAM划分一块连续内存,于 heap_x.c 中实现。
代码如下(示例):
//FreeRTOSConfig.h
//1: 内存动态分配,没有配置此选项时系统默认使用动态分配方式
#define configSUPPORT_DYNAMIC_ALLOCATION 1
//堆大小
#define configTOTAL_HEAP_SIZE ( ( size_t ) ( 15 * 1024 ) )
//1: 允许应用程序将堆放在内存中的任意位置
#define configAPPLICATION_ALLOCATED_HEAP 0
//heap_4.c /* Allocate the memory for the heap. */ #if( configAPPLICATION_ALLOCATED_HEAP == 1 ) /* The application writer has already defined the array used for the RTOS heap - probably so it can be placed in a special segment or address. */ extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ]; #else static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ]; #endif /* configAPPLICATION_ALLOCATED_HEAP */ void *pvPortMalloc( size_t xWantedSize ) { //... if( pxEnd == NULL ) { prvHeapInit(); //调用 ucHeap[] } //... }
//tasks.c
void xTaskCreate()
{
//...
pvPortMalloc();
//...
}
实例化TCB空间
//main.c
//CreateAppTask任务控制块
static StaticTask_t CreateAppTask_TCB;
static TaskHandle_t AppTaskCreate_Handle = NULL;
异于静态创建,无需预先定义TCB空间,但需定义一个TCB指针,即Task_handle
//main.c
//创建任务句柄
static TaskHandle_t AppTaskCreate_Handle = NULL;
//main.c //空闲任务 static StackType_t IdleTaskStack[configMINIMAL_STACK_SIZE]; static StaticTask_t IdleTaskTCB; //定时器任务 static StackType_t TimerTaskStack[configTIMER_TASK_STACK_DEPTH]; static StaticTask_t TimerTaskTCB; //开始任务 #define START_TASK_PRIO 1 #define START_STK_SIZE 128 StackType_t StartTaskStack[START_STK_SIZE]; StaticTask_t StartTaskTCB; TaskHandle_t StartTask_Handler; void start_task(void *pvParameters); //任务1变量声明 #define TASK1_TASK_PRIO 2 #define TASK1_STK_SIZE 128 StackType_t Task1TaskStack[TASK1_STK_SIZE]; StaticTask_t Task1TaskTCB; TaskHandle_t Task1Task_Handler; void task1_task(void *pvParameters); //任务2声明变量 #define TASK2_TASK_PRIO 3 #define TASK2_STK_SIZE 128 StackType_t Task2TaskStack[TASK2_STK_SIZE]; StaticTask_t Task2TaskTCB; TaskHandle_t Task2Task_Handler; void task2_task(void *pvParameters); //空闲任务 void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize) { *ppxIdleTaskTCBBuffer=&IdleTaskTCB; *ppxIdleTaskStackBuffer=IdleTaskStack; *pulIdleTaskStackSize=configMINIMAL_STACK_SIZE; } //定时器任务 void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize) { *ppxTimerTaskTCBBuffer=&TimerTaskTCB; *ppxTimerTaskStackBuffer=TimerTaskStack; *pulTimerTaskStackSize=configTIMER_TASK_STACK_DEPTH; }
//main.c
//创建 AppTaskCreate 任务
xReturn = xTaskCreate( (TaskFunction_t )AppTaskCreate,
(const char* )"AppTaskCreate",
(uint16_t )512,
(void* )NULL,
(UBaseType_t )1,
(TaskHandle_t* )&AppTaskCreate_Handle);
if (pdPASS == xReturn)
vTaskStartScheduler();
else
return -1;
static TaskHandle_t Task1Create_Handle = NULL; static TaskHandle_t Task2Create_Handle = NULL; TaskFunction_t AppTaskCreate() { BaseType_t xReturn = pdPASS; xReturn = xTaskCreate( (TaskFunction_t )Task1_Crt, (const char* )"Taks1", (uint16_t )512, (void* )NULL, (UBaseType_t )2, (TaskHandle_t* )&Task1Create_Handle); if(1){}; printf("Re1:%d\n", (int)xReturn); xReturn = xTaskCreate( (TaskFunction_t )Task2_Crt, (const char* )"Task2", (uint16_t )512, (void* )NULL, (UBaseType_t )3, (TaskHandle_t* )&Task2Create_Handle); if(1){}; printf("Re2:%d\n", (int)xReturn); vTaskDelete(AppTaskCreate_Handle); }
/************************************ * Create task, prime conditions are concrete objects, defination of TCB, also the task of creation. * */ static void Task1_Crt(void) { printf("create task1.\n"); while(1) { printf("task1.\n"); vTaskDelay(500); } } static void Task2_Crt(void) { printf("create task2.\n"); while(1) { printf("task2.\n"); vTaskDelay(1000); } } #if !(configSUPPORT_DYNAMIC_ALLOCATION & configSUPPORT_STATIC_ALLOCATION) static TaskHandle_t StartTaskCreate_Handle = NULL; //static/dynamic task static TaskHandle_t Task1Create_Handle = NULL; static TaskHandle_t Task2Create_Handle = NULL; TaskFunction_t StartTaskCreate(void) { BaseType_t xReturn = pdPASS; taskENTER_CRITICAL(); xReturn = xTaskCreate( (TaskFunction_t )Task1_Crt, (const char* )"Taks1", (uint16_t )512, (void* )NULL, (UBaseType_t )2, (TaskHandle_t* )&Task1Create_Handle); if(1){}; printf("Re1:%d\n", (int)xReturn); xReturn = xTaskCreate( (TaskFunction_t )Task2_Crt, (const char* )"Task2", (uint16_t )512, (void* )NULL, (UBaseType_t )3, (TaskHandle_t* )&Task2Create_Handle); if(1){}; printf("Re2:%d\n", (int)xReturn); vTaskDelete(AppTaskCreate_Handle); taskEXIT_CRITICAL(); } #else //idle static StackType_t IdleTaskStack[configMINIMAL_STACK_SIZE]; static StaticTask_t IdleTaskTCB; //timer static StackType_t TimerTaskStack[configTIMER_TASK_STACK_DEPTH]; static StaticTask_t TimerTaskTCB; //start #define START_TASK_PRIO 1 #define START_STK_SIZE 128 StackType_t StartTaskStack[START_STK_SIZE]; StaticTask_t StartTaskTCB; TaskHandle_t StartTask_Handler; //1 #define TASK1_TASK_PRIO 2 #define TASK1_STK_SIZE 128 StackType_t Task1TaskStack[TASK1_STK_SIZE]; StaticTask_t Task1TaskTCB; TaskHandle_t Task1Task_Handler; //2 #define TASK2_TASK_PRIO 3 #define TASK2_STK_SIZE 128 StackType_t Task2TaskStack[TASK2_STK_SIZE]; StaticTask_t Task2TaskTCB; TaskHandle_t Task2Task_Handler; TaskFunction_t StartTaskCreate(void) { taskENTER_CRITICAL(); //1 Task1Task_Handler=xTaskCreateStatic((TaskFunction_t )Task1_Crt, (const char* )"task1_task", (uint32_t )TASK1_STK_SIZE, (void* )NULL, (UBaseType_t )TASK1_TASK_PRIO, (StackType_t* )Task1TaskStack, (StaticTask_t* )&Task1TaskTCB); //2 Task2Task_Handler=xTaskCreateStatic((TaskFunction_t )Task2_Crt, (const char* )"task2_task", (uint32_t )TASK2_STK_SIZE, (void* )NULL, (UBaseType_t )TASK2_TASK_PRIO, (StackType_t* )Task2TaskStack, (StaticTask_t* )&Task2TaskTCB); vTaskDelete(StartTask_Handler); //del start taskEXIT_CRITICAL(); } //idle void vApplicationGetIdleTaskMemory(StaticTask_t **ppxIdleTaskTCBBuffer, StackType_t **ppxIdleTaskStackBuffer, uint32_t *pulIdleTaskStackSize) { *ppxIdleTaskTCBBuffer=&IdleTaskTCB; *ppxIdleTaskStackBuffer=IdleTaskStack; *pulIdleTaskStackSize=configMINIMAL_STACK_SIZE; } //timer void vApplicationGetTimerTaskMemory(StaticTask_t **ppxTimerTaskTCBBuffer, StackType_t **ppxTimerTaskStackBuffer, uint32_t *pulTimerTaskStackSize) { *ppxTimerTaskTCBBuffer=&TimerTaskTCB; *ppxTimerTaskStackBuffer=TimerTaskStack; *pulTimerTaskStackSize=configTIMER_TASK_STACK_DEPTH; } #endif int main() { BaseType_t xReturn = pdPASS; Sys_Init(); Reback_Boot(); UART_Debug_Init(SCI1,g_ips_clk, 115200); Printf_Version(); // App_Demo(); #if !(configSUPPORT_DYNAMIC_ALLOCATION & configSUPPORT_STATIC_ALLOCATION) xReturn = xTaskCreate( (TaskFunction_t )StartTaskCreate, (const char* )"StartTaskCreate", (uint16_t )512, (void* )NULL, (UBaseType_t )1, (TaskHandle_t* )&AppTaskCreate_Handle); #else StartTask_Handler = xTaskCreateStatic((TaskFunction_t )StartTaskCreate, (const char* )"StartTaskCreate", (uint32_t )START_STK_SIZE, (void* )NULL, (UBaseType_t )START_TASK_PRIO, (StackType_t* )StartTaskStack, (StaticTask_t* )&StartTaskTCB); #endif if (pdPASS == xReturn) vTaskStartScheduler(); else return -1; }
赞
踩
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。