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 #define FLAGS_MSK1 0x00000001U
24 #define THREAD_STACK_SIZE (1024 * 4)
25 #define THREAD_PRIO 25
26 #define SEND_THREAD_DELAY_1S 100
27
28 osEventFlagsId_t g_eventFlagsId; // event flags id
29
30 /**
31 * @brief Event sender thread used to set event flag
32 *
33 */
EventSenderThread(void)34 void EventSenderThread(void)
35 {
36 while (1) {
37 osEventFlagsSet(g_eventFlagsId, FLAGS_MSK1);
38
39 // suspend thread
40 osThreadYield();
41
42 osDelay(SEND_THREAD_DELAY_1S);
43 }
44 }
45
46 /**
47 * @brief Event receiver thread blocking wait event flag
48 *
49 */
EventReceiverThread(void)50 void EventReceiverThread(void)
51 {
52 uint8_t flags;
53
54 while (1) {
55 flags = osEventFlagsWait(g_eventFlagsId, FLAGS_MSK1, osFlagsWaitAny, osWaitForever);
56 printf("Receive Flags is %d\n", flags);
57 }
58 }
59
60 /**
61 * @brief Main Entry of the Event Example
62 *
63 */
EventExample(void)64 static void EventExample(void)
65 {
66 g_eventFlagsId = osEventFlagsNew(NULL);
67 if (g_eventFlagsId == NULL) {
68 printf("Failed to create EventFlags!\n");
69 }
70
71 osThreadAttr_t attr;
72
73 attr.attr_bits = 0U;
74 attr.cb_mem = NULL;
75 attr.cb_size = 0U;
76 attr.stack_mem = NULL;
77 attr.stack_size = THREAD_STACK_SIZE;
78 attr.priority = THREAD_PRIO;
79
80 attr.name = "EventSenderThread";
81 if (osThreadNew(EventSenderThread, NULL, &attr) == NULL) {
82 printf("Failed to create EventSenderThread!\n");
83 }
84
85 attr.name = "EventReceiverThread";
86 if (osThreadNew(EventReceiverThread, NULL, &attr) == NULL) {
87 printf("Failed to create EventReceiverThread!\n");
88 }
89 }
90
91 APP_FEATURE_INIT(EventExample);
92