FreeRTOS线程卡死
时间:2024/5/5
问题所在
运行到任务部分代码,线程就卡死
static void AppTaskCreate(void){
uint8_t cnt_size = 0;
taskENTER_CRITICAL(); //进入临界区
testQueue = xQueueCreate(QUEUE_LEN,QUEUE_ITEM_SIZE);
bin_sempher_Handle = xSemaphoreCreateBinary();
cnt_semphr_Handle = xSemaphoreCreateCounting(SEMPHR_CNT_SIZE, 3);
cnt_size = uxSemaphoreGetCount(cnt_semphr_Handle);
Serial_Printf("cnt_semphr: Creat success, size = %d\r\n", cnt_size);
if(NULL != testQueue){
Serial_Printf("testQueue: Creat success\r\n");
}
else
{
Serial_Printf("testQueue: Creat fail\r\n");
}
xReturn = xTaskCreate((TaskFunction_t)Take_Task, //任务函数
(const char* )"Take_TASK", //任务名
(uint32_t) 64, //任务堆栈大小
(void*)NULL, //任务所需参数
(UBaseType_t)2, //任务优先级
&Take_Task_Handle
);
if(xReturn == pdPASS){
Serial_Printf("Take_TASK: Creat success\r\n");
}
else
{
Serial_Printf("Take_TASK: Creat fail\r\n");
}
xReturn = xTaskCreate((TaskFunction_t)Give_Task, //任务函数
(const char* )"Give_TASK", //任务名
(uint32_t) 64, //任务堆栈大小
(void*)NULL, //任务所需参数
(UBaseType_t)2, //任务优先级
&Give_Task_Handle
);
if(xReturn == pdPASS){
Serial_Printf("Give_TASK: Creat success\r\n");
}
else Serial_Printf("Give_TASK: Creat FAIL\r\n");
xReturn = xTaskCreate((TaskFunction_t)Key_Task, //任务函数
(const char* )"KEY_TASK", //任务名
(uint32_t) 64, //任务堆栈大小
(void*)NULL, //任务所需参数
(UBaseType_t)2, //任务优先级
&KEY_Task_Handle
);
if(xReturn == pdPASS){
Serial_Printf("KEY_TASK: Creat success\r\n");
}
else Serial_Printf("KEY_TASK: Creat FAIL\r\n");
Serial_Printf("APPTaskCreate_Handle: DELETE\r\n");
vTaskDelete(APPTaskCreate_Handle);
taskEXIT_CRITICAL(); //退出临界资源
}
static void BSP_Init(void){
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
myUsart_Init();
LED_Init();
LCD_Init();
Key_Init();
}
void print_cnt(){
Serial_Printf("cnt_semphr: size = %d\r\n", (uint8_t)uxSemaphoreGetCount(cnt_semphr_Handle));
}
static void Take_Task(void *parameter){
while(1)
{
xReturn = xSemaphoreTake(bin_sempher_Handle,BLOCK_TIME);
taskENTER_CRITICAL();
if (xReturn == pdPASS){
Serial_Printf("Take_Task: TAKE BIN SEMAPHORE SUCCESS \r\n");
}else {
Serial_Printf("Take_Task: TAKE BIN SEMAPHORE Fail \r\n");
continue;
}
xReturn = xSemaphoreTake(cnt_semphr_Handle,BLOCK_TIME);
if (xReturn == pdPASS){
Serial_Printf("Take_Task: consume SUCCESS \r\n");
print_cnt();
LED1_Turn();
}else {
Serial_Printf("Take_Task: no product \r\n");
}
xReturn = xSemaphoreGive(bin_sempher_Handle);
if (xReturn == pdPASS){
Serial_Printf("Take_Task: Give BIN SEMAPHORE SUCCESS \r\n");
}else {
Serial_Printf("Take_Task: Give BIN SEMAPHORE Fail \r\n");
continue;
}
taskEXIT_CRITICAL();
vTaskDelay(50);
}
vTaskDelete( NULL );
}
static void Give_Task(void *parameter){
uint8_t msg=0;
while(1)
{
xReturn = xSemaphoreTake(bin_sempher_Handle,BLOCK_TIME);
taskENTER_CRITICAL();
if (xReturn == pdPASS){
Serial_Printf("Give_Task: TAKE BIN SEMAPHORE SUCCESS \r\n");
}else {
Serial_Printf("Give_Task: TAKE BIN SEMAPHORE Fail \r\n");
continue;
}
xReturn = xSemaphoreGive(cnt_semphr_Handle);
if (xReturn == pdPASS){
Serial_Printf("Give_Task: produce a product SUCCESS \r\n");
print_cnt();
LED2_Turn();
}else {
Serial_Printf("Give_Task: produce Fail, no \r\n");
}
xReturn = xSemaphoreGive(bin_sempher_Handle);
if (xReturn == pdPASS){
Serial_Printf("Give_Task: Give BIN SEMAPHORE SUCCESS \r\n");
}else {
Serial_Printf("Give_Task: Give BIN SEMAPHORE Fail \r\n");
continue;
}
taskEXIT_CRITICAL();
vTaskDelay(120);
}
vTaskDelete( NULL );
}
static void Key_Task(void *parameter){
uint8_t keyNum = 0, msg=0;
while(1)
{
keyNum=Key_GetNum();
if (keyNum == 1)
{
xReturn = xSemaphoreTake(bin_sempher_Handle,BLOCK_TIME);
if (xReturn == pdPASS){
Serial_Printf("Key_TASK: KEY1 TAKE BIN SEMAPHORE SUCCESS \r\n");
}else {
Serial_Printf("Key_TASK: KEY1 TAKE BIN SEMAPHORE Fail \r\n");
continue;
}
}
else if (keyNum == 2)
{
xReturn = xSemaphoreGive(bin_sempher_Handle);
if (xReturn == pdPASS){
Serial_Printf("Key_TASK: KEY2 Give BIN SEMAPHORE SUCCESS \r\n");
}else {
Serial_Printf("Key_TASK: KEY2 Give BIN SEMAPHORE Fail \r\n");
continue;
}
}
//Serial_Printf("Key_TASK: NO KEY \r\n");
vTaskDelay(50);
}
vTaskDelete( NULL );
}
原因:
任务分配的堆栈空间太小,后续的变量将堆栈空间占满了
解决方法:
将堆栈大小调整到合适
xReturn = xTaskCreate((TaskFunction_t)Take_Task, //任务函数
(const char* )"Take_TASK", //任务名
(uint32_t) 128, //任务堆栈大小
(void*)NULL, //任务所需参数
(UBaseType_t)2, //任务优先级
&Take_Task_Handle
);
if(xReturn == pdPASS){
Serial_Printf("Take_TASK: Creat success\r\n");
}
else
{
Serial_Printf("Take_TASK: Creat fail\r\n");
}
xReturn = xTaskCreate((TaskFunction_t)Give_Task, //任务函数
(const char* )"Give_TASK", //任务名
(uint32_t) 128, //任务堆栈大小
(void*)NULL, //任务所需参数
(UBaseType_t)2, //任务优先级
&Give_Task_Handle
);
if(xReturn == pdPASS){
Serial_Printf("Give_TASK: Creat success\r\n");
}
else Serial_Printf("Give_TASK: Creat FAIL\r\n");
xReturn = xTaskCreate((TaskFunction_t)Key_Task, //任务函数
(const char* )"KEY_TASK", //任务名
(uint32_t) 128, //任务堆栈大小
(void*)NULL, //任务所需参数
(UBaseType_t)2, //任务优先级
&KEY_Task_Handle
);