今天终于利用消息队列实现了串口的映射!
要点回顾:
1.刚开始一直用消息邮箱经行映射,后来才发现消息邮箱只能发送一个指针的数据(4个字节),而消息队列可以发送N个消息邮箱的消息!
简言之,消息邮箱只能发送4个字节的数据,消息队列可以发送无限数据!
2.消息队列的发送OSQPost(MsgQueue, (void *)&txmsg[0]); 和消息队列的请求 OSQPend(MsgQueue, 0, &err);
需要放到不同的任务中,否则会出现“假死”现象
本节我建立了四个TASK来接收发送数
int main (void)
{
BSP_IntDisAll(); /* Disable all interrupts until we are ready to accept them */
OSInit(); /* Initialize "uC/OS-II, The Real-Time Kernel" */
OSTaskCreate(start_task,(void *)0,(OS_STK *)&START_TASK_STK[START_STK_SIZE-1],START_TASK_PRIO );//创建起始任务
OSStart(); /* Start multitasking (i.e. give control to uC/OS-II) */
}
void start_task(void *pdata)
{
OS_CPU_SR cpu_sr=0;
pdata = pdata;
// msg_key_rfid=OSMboxCreate((void*)0); //创建消息邮箱
// msg_key_rfid_gprs=OSMboxCreate((void*)0); //创建消息邮箱
// msg_key_gprs_rfid=OSMboxCreate((void*)0); //创建消息邮箱
// sem_beep=OSSemCreate(0); //创建信号量
MsgQueue = OSQCreate(&MsgQueueTbl[0], QUEUE_TABLE_SIZE); //创建消息队列
MsgQueue1 = OSQCreate(&MsgQueueTb2[0], QUEUE_TABLE_SIZE1); //创建消息队列
BSP_Init();
OS_CPU_SysTickInit(); /* Initialize the SysTick. */
OSStatInit(); //初始化统计任务.这里会延时1秒钟左右
GPRS_ON();
Start_GPRS();
OS_ENTER_CRITICAL(); //进入临界区(无法被中断打断)
OSTaskCreate(one_task,(void *)0,(OS_STK*)&ONE_TASK_STK[ONE_STK_SIZE-1],ONE_TASK_PRIO);
OSTaskCreate(two_task,(void *)0,(OS_STK*)&TWO_TASK_STK[TWO_STK_SIZE-1],TWO_TASK_PRIO);
OSTaskCreate(three_task,(void *)0,(OS_STK*)&THREE_TASK_STK[THREE_STK_SIZE-1],THREE_TASK_PRIO);
OSTaskCreate(main_task,(void *)0,(OS_STK*)&MAIN_TASK_STK[MAIN_STK_SIZE-1],MAIN_TASK_PRIO);
OSTaskSuspend(START_TASK_PRIO); //挂起起始任务.
OS_EXIT_CRITICAL(); //退出临界区(可以被中断打断)
}
void one_task(void *pdata)//PC---RFID---GPRS
{
u8 err;
char txmsg[50];
char *rxmsg;
while(1)
{
if(re_RFID)
{
re_RFID=0;
for(u16 i=0;i<20;i++)
txmsg[i]=USART_RX_BUF[i];
OSQPost(MsgQueue, (void *)&txmsg[0]);
OSTimeDlyHMSM(0,0,0,50);
}
OSTimeDlyHMSM(0,0,0,100);
}
OSTimeDlyHMSM(0,0,0,100);
}
void two_task(void *pdata)//GPRS---RFID---PC
{
u8 err;
char txmsg[50];
char *rxmsg;
while(1)
{
OSTimeDlyHMSM(0,0,0,50);
if(re_GPRS)
{
re_GPRS=0;
for(u16 i=0;i<20;i++)
txmsg[i]=GPRS_ReBuffer[i];
OSQPost(MsgQueue1, (void *)&txmsg[0]);
Clear_GPRS_ReBuffer();//清除数据很关键
}
}
OSTimeDlyHMSM(0,0,0,20);
}
void three_task(void *pdata)
{
char txmsg[50];
char *rxmsg;
u8 err;
while(1)
{
OSTimeDlyHMSM(0,0,0,50);
rxmsg = (char *)OSQPend(MsgQueue, 0, &err);
PRS_SendString(rxmsg);
OSTimeDlyHMSM(0,0,0,50);
}
}
void main_task(void *pdata)
{
char txmsg[50];
char *rxmsg;
u32 key=0;
u8 err;
while(1)
{
OSTimeDlyHMSM(0,0,0,50);
rxmsg = (char *)OSQPend(MsgQueue1, 0, &err);
RFID_SendString(rxmsg);
Clear_RFID_ReBuffer();//清除数据很关键
OSTimeDlyHMSM(0,0,0,50);
}
}