21_消息队列
消息队列
消息队列
1、任务级队列处理函数
2、中断级队列处理函数(带中断保护)已经在CMSIS接口中封装
但写入生产速度比消费速度快的时候,容易出现数据被覆盖
邮箱队列
创建、发送、接收、查询、删除
传数值
osEvent event = osMessageGet(myQueue01Handle, osWaitForever);
if(event.status == osEventMessage)
{
printf("data = %d\n", event.value.v);
}
osMessagePut(myQueue01Handle, 10, 0);
传结构体对象
typedef struct
{
uint8_t name;
uint8_t id;
uint8_t age;
}T_Data;
T_Data m_Data;
m_Data.age = 10;
m_Data.id = 2;
m_Data.name = 1;
osMessagePut(myQueue01Handle, (uint32_t)&m_Data, 0);
osEvent event = osMessageGet(myQueue01Handle, osWaitForever);
if(event.status == osEventMessage)
{
T_Data *pData = (T_Data *)event.value.p;
printf("age = %d\n", pData->age);
printf("name = %d\n", pData->name);
printf("id = %d\n", pData->id);
}
邮箱队列
如果put比get快时会出现结构体数据被篡改的问题,这时就需要邮箱队列
osMailQId mailQ01Handle;
osMailQDef(mailQ01, 15, T_Data);
mailQ01Handle = osMailCreate(osMailQ(mailQ01), NULL);
osEvent event = osMailGet(mailQ01Handle, osWaitForever);
if(event.status == osEventMail)
{
T_Data *m_Data = (T_Data *)event.value.p;
printf("age = %d\n", m_Data->age);
printf("name = %d\n", m_Data->name);
printf("id = %d\n", m_Data->id);
}
T_Data m_Data;
m_Data.age = 10;
m_Data.id = 2;
m_Data.name = 1;
printf("Task2 start\n");
osMailPut(mailQ01Handle, &m_Data);
结论: 邮箱队列适合整体大数据, 消息队列适合状态小数据