4.3 消息队列的使用
<pre><code>#include "wm_include.h"
#define USER_TASK_QUEUE_SIZE 1
#define USER_TASK_QUEUE_TEST 0x01
static tls_os_queue_t *user_task_queue = NULL;
static OS_STK TaskStk1[512];
static OS_STK TaskStk2[512];
static void user_task1(void)
{
while(1)
{
printf("---> send msg\r\n");
// 发送队里消息
tls_os_queue_send(user_task_queue, (void *)USER_TASK_QUEUE_TEST, 0);
tls_os_time_delay(HZ);
}
}
static void user_task2(void)
{
void *msg;
tls_os_status_t ret;
for ( ; ; )
{
// 接收队里消息
ret= tls_os_queue_receive(user_task_queue, (void **)&msg, 0, 0);
if (ret == TLS_OS_SUCCESS)
{
switch((u32)msg)
{
case USER_TASK_QUEUE_TEST: // 收到指定的消息
printf("---> received msg\r\n");
break;
default:
break;
}
}
}
}
void UserMain(void)
{
printf("\n user task \n");
// 创建task1用于发送队列消息
tls_os_task_create(NULL, "task1",
( void (*))user_task1,
NULL,
(void *)TaskStk1, /* task's stack start address */
sizeof(TaskStk1), /* task's stack size, unit:byte */
31,
0);
// 创建task2用于接收队里消息
tls_os_task_create(NULL, "task2",
( void (*))user_task2,
NULL,
(void *)TaskStk2, /* task's stack start address */
sizeof(TaskStk2), /* task's stack size, unit:byte */
32,
0);
// 创建消息队列
tls_os_queue_create(&user_task_queue, USER_TASK_QUEUE_SIZE);
#if DEMO_CONSOLE
CreateDemoTask();
#endif
//用户自己的task
}</code></pre>
<h3>程序效果:</h3>
<p><img src="https://www.showdoc.com.cn/server/api/attachment/visitFile?sign=fae4d8d06a698c750e0105b2d1ab019c&file=file.png" alt="" /></p>