本帖最后由 damiaa 于 2023-6-10 21:42 编辑
【极海APM32F407 Tiny Board】玩转FreeRTOS 二 消息队列与键盘和led
程序中用到 xQueue消息队列做键盘和led两个任务的通讯。出现 死机,仿真跑到这里后就死了。
经过。。。的折腾,发现问题是出在xQueueCreate上,因为它放到了任务创建的后面。
两个任务
任务2 处理扫描按键,发现有键key1按下就发送消息0 有键key2按下就发送消息 1
任务1 收到消息0 翻转led2 收到消息1 翻转led3
下面把代码贴一下:
#include "main.h"
#include "Board.h"
#include "stdio.h"
#include <string.h>
#include "FreeRTOS.h"
#include "task.h"
#include "queue.h"
#include "list.h"
#define DATA_BUF_SIZE (32)
typedef enum {FALSE, TRUE} BOOL;
static TaskHandle_t xHandleTaskLedToggle = NULL;
static TaskHandle_t xHandleTaskKey = NULL;
static void UserTaskCreate(void);
void vTaskLedToggle(void *pvParameters);
void vTaskKey(void *pvParameters);
QueueHandle_t xQueue;
typedef struct
{
unsigned char ucMessageID;
} AMessage;
AMessage xMessage;
int main(void){
APM_TINY_LEDInit(LED2);
APM_TINY_LEDInit(LED3);
APM_TINY_PBInit(BUTTON_KEY1,BUTTON_MODE_GPIO);
APM_TINY_PBInit(BUTTON_KEY2,BUTTON_MODE_GPIO);
xQueue = xQueueCreate( 10, sizeof( struct AMessage * ) );
USART_Init();
UserTaskCreate();
vTaskStartScheduler();
while(1);
}
static void UserTaskCreate(void){
xTaskCreate(vTaskLedToggle,
"TaskLedToggle",
512,
NULL,
4,
&xHandleTaskLedToggle);
xTaskCreate(vTaskKey,
"TaskKey",
512,
NULL,
5
&xHandleTaskKey);
}
void vTaskLedToggle(void *pvParameters){
while(1){
AMessage xRxedMessage;
if( xQueue != 0 ){
if( xQueueReceive( xQueue, &( xRxedMessage ), ( TickType_t ) 10 ) ){
printf("RxQueueMessage: id is %d\n",xRxedMessage.ucMessageID);
if(xRxedMessage.ucMessageID ==0)
APM_TINY_LEDToggle(LED2);
else
APM_TINY_LEDToggle(LED3);
}
}
vTaskDelay(200);
}
}
void vTaskKey(void *pvParameters){
AMessage *pxMessage;
while(1){
static uint8_t key1times=0;
static uint8_t key2times=0;
if( BIT_RESET == (uint8_t) APM_TINY_PBGetState(BUTTON_KEY1)){
key1times++;
}
if( BIT_RESET == (uint8_t) APM_TINY_PBGetState(BUTTON_KEY2)){
key2times++;
}
if(key1times >=2){
key1times=0;
xMessage.ucMessageID =0;
pxMessage = &xMessage;
xQueueSend( xQueue, ( void * ) &xMessage, ( TickType_t ) 0 );
}
if(key2times >=2){
key2times=0;
xMessage.ucMessageID =1;
pxMessage = &xMessage;
xQueueSend( xQueue, ( void * ) &xMessage, ( TickType_t ) 0 );
}
vTaskDelay(200);
}
}
11