(uC/OS-II学习笔记) 消息邮箱&&消息队列
原文出处:
http://www.cnblogs.com/hebaichuanyeah
与信号量一样,消息邮箱与消息列队都是一种事件块。
消息邮箱可以在任务间实现信息传递。
比如,在任务1中发送一条消息,任务2中接受,并发至串口终端显示。
OS_EVENT * MyMbox; void Task_1(void *pdata) { char SendData[] = "hello world "; #if OS_CRITICAL_METHOD == 3 //关中断的方法为3 OS_CPU_SR cpu_sr; #endif OS_ENTER_CRITICAL(); OS_CPU_SysTickInit(100000000/OS_TICKS_PER_SEC); OS_EXIT_CRITICAL(); OSStatInit(); //统计任务 优先级最低 MyMbox = OSMboxCreate((void*)0); //PIT0_Init(50000000); UART4_Init(115200); init_LED(); pdata = pdata; while(1) { OSTaskResume(6); OSMboxPost(MyMbox,&SendData); OSTimeDly(500); } } void Task_2(void *pdata) { U32 i; U8 err; char * ReceiveData = ""; pdata = pdata; while(1) { ReceiveData = OSMboxPend(MyMbox,10,&err); for(i=0;ReceiveData[i];i++) Uart4_SendByte(ReceiveData[i]); OSTaskSuspend(6); } }
消息队列
OS_EVENT * MyQ; void Task_1(void *pdata) { U8 i; char * MyMessage[5]; void * ArrayOfMsg[5]; MyMessage[0] = "This Message One "; MyMessage[1] = "This Message Two "; MyMessage[2] = "This Message Three "; MyMessage[3] = "This Message Four "; MyMessage[4] = "This Message Five "; #if OS_CRITICAL_METHOD == 3 //关中断的方法为3 OS_CPU_SR cpu_sr; #endif OS_ENTER_CRITICAL(); OS_CPU_SysTickInit(100000000/OS_TICKS_PER_SEC); OS_EXIT_CRITICAL(); OSStatInit(); //统计任务 优先级最低 MyQ = OSQCreate(&ArrayOfMsg[0],5); //PIT0_Init(50000000); UART4_Init(115200); init_LED(); pdata = pdata; while(1) { OSTaskResume(6); for(i=0;i<5;i++) { OSQPost(MyQ,&MyMessage[i][0]); } OSTimeDly(500); } } void Task_2(void *pdata) { U32 i; U8 err,count; char * ReceiveData; pdata = pdata; while(1) { for(count=0;count<5;count++) { ReceiveData = OSQPend(MyQ,0,&err); for(i=0;ReceiveData[i]!='\0';i++) Uart4_SendByte(ReceiveData[i]); } for(i=0;i<100;i++) Uart4_SendByte(' '); OSTaskSuspend(6); } }