1 /*
2 * Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
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 <stdio.h>
17 #include <string.h>
18 #include <unistd.h>
19
20 #include "cmsis_os2.h"
21 #include "ohos_init.h"
22
23 // number of Message Queue Objects
24 #define MSGQUEUE_COUNT 16
25 #define MSGQUEUE_SIZE 100
26
27 #define THREAD_STACK_SIZE (1024 * 10)
28 #define THREAD_PRIO 25
29
30 #define THREAD_DELAY_1S 100
31
32 typedef struct {
33 // object data type
34 char *buf;
35 uint8_t idx;
36 } MSGQUEUE_OBJ_t;
37
38 MSGQUEUE_OBJ_t msg;
39
40 // message queue id
41 osMessageQueueId_t g_msgQueueId;
42
MsgQueue1Thread(void)43 void MsgQueue1Thread(void)
44 {
45 // do some work...
46 msg.buf = "Hello BearPi-HM_Nano!";
47 msg.idx = 0U;
48 while (1) {
49 osMessageQueuePut(g_msgQueueId, &msg, 0U, 0U);
50
51 // suspend thread
52 osThreadYield();
53 osDelay(THREAD_DELAY_1S);
54 }
55 }
56
MsgQueue2Thread(void)57 void MsgQueue2Thread(void)
58 {
59 osStatus_t status;
60
61 while (1) {
62 // wait for message
63 status = osMessageQueueGet(g_msgQueueId, &msg, NULL, osWaitForever);
64 if (status == osOK) {
65 printf("Message Queue Get msg:%s\n", msg.buf);
66 }
67 }
68 }
69
70 /**
71 * @brief Main Entry of the Message Example
72 *
73 */
MessageExample(void)74 static void MessageExample(void)
75 {
76 g_msgQueueId = osMessageQueueNew(MSGQUEUE_COUNT, MSGQUEUE_SIZE, NULL);
77 if (g_msgQueueId == NULL) {
78 printf("Failed to create Message Queue!\n");
79 }
80
81 osThreadAttr_t attr;
82
83 attr.attr_bits = 0U;
84 attr.cb_mem = NULL;
85 attr.cb_size = 0U;
86 attr.stack_mem = NULL;
87 attr.stack_size = THREAD_STACK_SIZE;
88 attr.priority = THREAD_PRIO;
89
90 attr.name = "MsgQueue1Thread";
91 if (osThreadNew(MsgQueue1Thread, NULL, &attr) == NULL) {
92 printf("Failed to create MsgQueue1Thread!\n");
93 }
94
95 attr.name = "MsgQueue2Thread";
96 if (osThreadNew(MsgQueue2Thread, NULL, &attr) == NULL) {
97 printf("Failed to create MsgQueue2Thread!\n");
98 }
99 }
100
101 APP_FEATURE_INIT(MessageExample);
102