1# BearPi-HM_Nano开发板OpenHarmony内核编程开发——事件标志 2本示例将演示如何在BearPi-HM_Nano开发板上使用cmsis 2.0 接口使用事件标志同步线程。 3 4 5## EventFlags API分析 6 7 8### osEventFlagsNew() 9 10```c 11osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr) 12``` 13**描述:** 14 15osEventFlagsNew函数创建了一个新的事件标志对象,用于跨线程发送事件,并返回事件标志对象标识符的指针,或者在出现错误时返回NULL。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,但不能在内核初始化 (调用 osKernelInitialize)之前调用该函数。 16> **注意** :不能在中断服务中调用该函数。 17 18 19**参数:** 20 21|参数名|描述| 22|:--|:------| 23| attr |事件标志属性;空:默认值. | 24 25### osEventFlagsSet() 26 27```c 28uint32_t osEventFlagsSet(osEventFlagsId_t ef_id,uint32_t flags) 29``` 30**描述:** 31osEventFlagsSet函数在一个由参数ef_id指定的事件标志对象中设置由参数flags指定的事件标志。 32 33> **注意** :不能在中断服务中调用该函数。 34 35 36**参数:** 37 38|参数名|描述| 39|:--|:------| 40| ef_id | 由osEventFlagsNew获得的事件标志ID 。 | 41| flags | 指定设置的标志。 | 42 43### osEventFlagsWait() 44 45```c 46uint32_t osEventFlagsWait(osEventFlagsId_t ef_id,uint32_t flags,uint32_t options,uint32_t timeout) 47``` 48**描述:** 49osEventFlagsWait函数挂起当前运行线程,直到设置了由参数ef_id指定的事件对象中的任何或所有由参数flags指定的事件标志。当这些事件标志被设置,函数立即返回。否则,线程将被置于阻塞状态。 50 51> **注意** :如果参数timeout设置为0,可以在中断服务例程中调用。 52 53 54**参数:** 55 56|参数名|描述| 57|:--|:------| 58| ef_id | 由osEventFlagsNew获得的事件标志ID。 | 59| flags | 指定要等待的标志。 | 60| options | 指定标志选项。 | 61| timeout | 超时时间,0表示不超时。 | 62 63 64## 软件设计 65 66**主要代码分析** 67 68在EventExample函数中,通过osEventFlagsNew()函数创建了事件标志ID,EventReceiverThread()函数中通过osEventFlagsWait()函数一直将线程置于阻塞状态,等待从EventSenderThread()函数中通过osEventFlagsSet()函数每隔1s设置的标志,实现任务间的同步。 69 70```c 71/** 72 * @brief Event sender thread used to set event flag 73 * 74 */ 75void EventSenderThread(void) 76{ 77 while (1) { 78 osEventFlagsSet(g_eventFlagsId, FLAGS_MSK1); 79 80 // suspend thread 81 osThreadYield(); 82 83 osDelay(SEND_THREAD_DELAY_1S); 84 } 85} 86 87/** 88 * @brief Event receiver thread blocking wait event flag 89 * 90 */ 91void EventReceiverThread(void) 92{ 93 uint32_t flags; 94 95 while (1) { 96 flags = osEventFlagsWait(g_eventFlagsId, FLAGS_MSK1, osFlagsWaitAny, osWaitForever); 97 printf("Receive Flags is %d\n", flags); 98 } 99} 100 101/** 102 * @brief Main Entry of the Event Example 103 * 104 */ 105static void EventExample(void) 106{ 107 g_eventFlagsId = osEventFlagsNew(NULL); 108 if (g_eventFlagsId == NULL) { 109 printf("Failed to create EventFlags!\n"); 110 } 111 112 osThreadAttr_t attr; 113 114 attr.attr_bits = 0U; 115 attr.cb_mem = NULL; 116 attr.cb_size = 0U; 117 attr.stack_mem = NULL; 118 attr.stack_size = THREAD_STACK_SIZE; 119 attr.priority = THREAD_PRIO; 120 121 attr.name = "EventSenderThread"; 122 if (osThreadNew(EventSenderThread, NULL, &attr) == NULL) { 123 printf("Failed to create EventSenderThread!\n"); 124 } 125 126 attr.name = "EventReceiverThread"; 127 if (osThreadNew(EventReceiverThread, NULL, &attr) == NULL) { 128 printf("Failed to create EventReceiverThread!\n"); 129 } 130} 131 132``` 133 134## 编译调试 135 136### 修改 BUILD.gn 文件 137 138修改 `device\board\bearpi\bearpi_hm_nano\app`路径下 BUILD.gn 文件,指定 `event_example` 参与编译。 139 140```r 141#"A1_kernal_thread:thread_example", 142#"A2_kernel_timer:timer_example", 143"A3_kernel_event:event_example", 144#"A4_kernel_mutex:mutex_example", 145#"A5_kernel_semaphore:semaphore_example", 146#"A6_kernel_message:message_example", 147``` 148 149### 运行结果 150 151示例代码编译烧录后,按下开发板的RESET按键,通过串口助手查看日志,会每隔1s输出一次日志。 152``` 153Receive Flags is 1 154Receive Flags is 1 155Receive Flags is 1 156Receive Flags is 1 157Receive Flags is 1 158``` 159