使能内建消息队列

将OS_CFG_TASK_Q_EN置1

API函数

#if OS_CFG_TASK_Q_EN > 0u
//删除
OS_MSG_QTY OSTaskQFlush (OS_TCB *p_tcb,
OS_ERR *p_err); //等待-1
void *OSTaskQPend (OS_TICK timeout,
OS_OPT opt,
OS_MSG_SIZE *p_msg_size,
CPU_TS *p_ts,
OS_ERR *p_err); //取消等待
CPU_BOOLEAN OSTaskQPendAbort (OS_TCB *p_tcb,
OS_OPT opt,
OS_ERR *p_err); //释放+1
void OSTaskQPost (OS_TCB *p_tcb,
void *p_void,
OS_MSG_SIZE msg_size,
OS_OPT opt,
OS_ERR *p_err);
#endif

使用举例

#define TASK_Q_NUM  4       //发任务内建消息队列的长度

void start_task(void *p_arg)
{
OS_CRITICAL_ENTER(); //进入临界区 //创建定时器
OSTmrCreate((OS_TMR *)&tmr, //定时器
(CPU_CHAR *)"tmr", //定时器名字
(OS_TICK )0, //0ms
(OS_TICK )50, //50*10=500ms
(OS_OPT )OS_OPT_TMR_PERIODIC, //周期模式
(OS_TMR_CALLBACK_PTR)tmr_callback,//定时器回调函数
(void *)0, //参数为0
(OS_ERR *)&err); //返回的错误码 //创建msgpend任务
OSTaskCreate((OS_TCB * )&Msgpend_TaskTCB,
(CPU_CHAR * )"msgpend task",
(OS_TASK_PTR )msgpend_task,
(void * )0,
(OS_PRIO )MSGPEND_TASK_PRIO,
(CPU_STK * )&MSGPEND_TASK_STK[0],
(CPU_STK_SIZE)MSGPEND_STK_SIZE/10,
(CPU_STK_SIZE)MSGPEND_STK_SIZE,
(OS_MSG_QTY )0,
(OS_TICK )0,
(void * )0,
(OS_OPT )OS_OPT_TASK_STK_CHK|OS_OPT_TASK_STK_CLR,
(OS_ERR * )&err); OS_CRITICAL_EXIT(); //退出临界区
} //定时器的回调函数
void tmr_callback(void *p_tmr,void *p_arg)
{
u8 *pbuf;
static u8 msg_num;
OS_ERR err; pbuf = mymalloc(SRAMIN,10); //申请10个字节
if(pbuf) //申请内存成功
{
msg_num++;
sprintf((char*)pbuf,"ALIENTEK %d",msg_num);
printf("tmr_callback: %s\r\n", pbuf);
//发送消息
OSTaskQPost((OS_TCB* )&Msgpend_TaskTCB, //向任务msgpend发送消息
(void* )pbuf,
(OS_MSG_SIZE)10,
(OS_OPT )OS_OPT_POST_FIFO,
(OS_ERR* )&err);
if(err != OS_ERR_NONE)
{
myfree(SRAMIN,pbuf); //释放内存
OSTmrStop(&tmr,OS_OPT_TMR_NONE,0,&err); //停止定时器
printf("TMR STOP!\r\n");
}
}
} //等待消息队列中的消息
void msgpend_task(void *p_arg)
{
u8 *p;
OS_MSG_SIZE size;
OS_ERR err;
while(1)
{
//请求消息
p=OSTaskQPend((OS_TICK )0,
(OS_OPT )OS_OPT_PEND_BLOCKING,
(OS_MSG_SIZE* )&size,
(CPU_TS* )0,
(OS_ERR* )&err);
printf("msgpend_task: %s\r\n", p);
myfree(SRAMIN,p); //释放内存
OSTimeDlyHMSM(0,0,1,0,OS_OPT_TIME_PERIODIC,&err); //延时1s
}
}

实验现象

UCOSIII内建消息队列-LMLPHP

05-14 01:28