1 /*
2 * Copyright (c) 2022 Talkweb 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 "ohos_run.h"
21 #include "cmsis_os2.h"
22
23 #define FLAGS_MSK1 0x00000001U
24 #define TASK_DELAY 1000
25 #define STACK_SIZE 4096
26
27 osEventFlagsId_t g_event_flags_id; // event flags id
28
OS_Thread_EventSender(void * argument)29 void OS_Thread_EventSender(void *argument)
30 {
31 (void *)argument;
32 osEventFlagsId_t flags;
33 printf("Start OS_Thread_EventSender.\n");
34 while (1) {
35 flags = osEventFlagsSet(g_event_flags_id, FLAGS_MSK1);
36 printf("Send Flags is %d\n", flags);
37 osThreadYield();
38 osDelay(TASK_DELAY);
39 }
40 }
41
OS_Thread_EventReceiver(void * argument)42 void OS_Thread_EventReceiver(void *argument)
43 {
44 (void *)argument;
45 printf("Start OS_Thread_EventSender.\n");
46 while (1) {
47 uint32_t flags;
48 flags = osEventFlagsWait(g_event_flags_id, FLAGS_MSK1, osFlagsWaitAny, osWaitForever);
49 printf("Receive Flags is %u\n", flags);
50 }
51 }
52
OS_Event_example(void)53 void OS_Event_example(void)
54 {
55 printf("Start OS_Event_example.\n");
56 g_event_flags_id = osEventFlagsNew(NULL);
57 if (g_event_flags_id == NULL) {
58 printf("Failed to create EventFlags!\n");
59 return;
60 }
61
62 osThreadAttr_t attr;
63
64 attr.attr_bits = 0U;
65 attr.cb_mem = NULL;
66 attr.cb_size = 0U;
67 attr.stack_mem = NULL;
68 attr.stack_size = STACK_SIZE;
69 attr.priority = osPriorityNormal;
70
71 attr.name = "Thread_EventSender";
72 if (osThreadNew(OS_Thread_EventSender, NULL, &attr) == NULL) {
73 printf("Failed to create Thread_EventSender!\n");
74 return;
75 }
76
77 attr.name = "Thread_EventReceiver";
78 if (osThreadNew(OS_Thread_EventReceiver, NULL, &attr) == NULL) {
79 printf("Failed to create Thread_EventReceiver!\n");
80 return;
81 }
82 }
83
84 OHOS_APP_RUN(OS_Event_example);
85