• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 FuZhou Lockzhiner Electronic Co., Ltd. All rights reserved.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "los_task.h"
17 #include "ohos_init.h"
18 
19 /* 任务的堆栈大小 */
20 #define TASK_STACK_SIZE         2048
21 /* 任务的优先级 */
22 #define MSG_WRITE_TASK_PRIO     24
23 #define MSG_READ_TASK_PRIO      25
24 
25 /* 等待时间 */
26 #define MSG_WRITE_WAIT_MSEC     1000
27 
28 /* 消息队列长度 */
29 #define MSG_QUEUE_LENGTH        16
30 #define BUFFER_LEN              50
31 
32 static unsigned int m_msg_queue;
33 
34 /***************************************************************
35 * 函数名称: msg_write_thread
36 * 说    明: 队列写函数
37 * 参    数: 无
38 * 返 回 值: 无
39 ***************************************************************/
msg_write_thread(void)40 void msg_write_thread(void)
41 {
42     unsigned int data = 0;
43     unsigned int ret = LOS_OK;
44 
45     while (1) {
46         data++;
47         ret = LOS_QueueWrite(m_msg_queue, (void *)&data, sizeof(data), LOS_WAIT_FOREVER);
48         if (LOS_OK != ret) {
49             printf("%s write Message Queue msg fail ret:0x%x\n", __func__, ret);
50         } else {
51             printf("%s write Message Queue msg:%u\n", __func__, data);
52         }
53 
54         /* delay 1s */
55         LOS_Msleep(MSG_WRITE_WAIT_MSEC);
56     }
57 }
58 
59 /***************************************************************
60 * 函数名称: msg_read_thread
61 * 说    明: 队列读函数
62 * 参    数: 无
63 * 返 回 值: 无
64 ***************************************************************/
msg_read_thread(void)65 void msg_read_thread(void)
66 {
67     unsigned int addr;
68     unsigned int ret = LOS_OK;
69     unsigned int *pData = NULL;
70 
71     while (1) {
72         /* wait for message */
73         ret = LOS_QueueRead(m_msg_queue, (void *)&addr, BUFFER_LEN, LOS_WAIT_FOREVER);
74         if (ret == LOS_OK) {
75             pData = addr;
76             printf("%s read Message Queue msg:%u\n", __func__, *pData);
77         } else {
78             printf("%s read Message Queue fail ret:0x%x\n", __func__, ret);
79         }
80     }
81 }
82 
83 /***************************************************************
84 * 函数名称: queue_example
85 * 说    明: 内核队列函数
86 * 参    数: 无
87 * 返 回 值: 无
88 ***************************************************************/
queue_example(void)89 void queue_example(void)
90 {
91     unsigned int thread_id1;
92     unsigned int thread_id2;
93     TSK_INIT_PARAM_S task1 = {0};
94     TSK_INIT_PARAM_S task2 = {0};
95     unsigned int ret = LOS_OK;
96 
97     ret = LOS_QueueCreate("queue", MSG_QUEUE_LENGTH, &m_msg_queue, 0, BUFFER_LEN);
98     if (ret != LOS_OK) {
99         printf("Falied to create Message Queue ret:0x%x\n", ret);
100         return;
101     }
102 
103     task1.pfnTaskEntry = (TSK_ENTRY_FUNC)msg_write_thread;
104     task1.uwStackSize = TASK_STACK_SIZE;
105     task1.pcName = "msg_write_thread";
106     task1.usTaskPrio = MSG_WRITE_TASK_PRIO;
107     ret = LOS_TaskCreate(&thread_id1, &task1);
108     if (ret != LOS_OK) {
109         printf("Falied to create msg_write_thread ret:0x%x\n", ret);
110         return;
111     }
112 
113     task2.pfnTaskEntry = (TSK_ENTRY_FUNC)msg_read_thread;
114     task2.uwStackSize = TASK_STACK_SIZE;
115     task2.pcName = "msg_read_thread";
116     task2.usTaskPrio = MSG_READ_TASK_PRIO;
117     ret = LOS_TaskCreate(&thread_id2, &task2);
118     if (ret != LOS_OK) {
119         printf("Falied to create msg_read_thread ret:0x%x\n", ret);
120         return;
121     }
122 }
123 
124 APP_FEATURE_INIT(queue_example);
125