HarmonyOS小熊派 | HarmonyOS内核编程开发—事件标志
BearPi-HM_Nano开发板HarmonyOS内核编程开发——事件标志
本示例将演示如何在BearPi-HM_Nano开发板上使用cmsis 2.0 接口使用事件标志同步线程
EventFlags API分析
osEventFlagsNew()
osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr)
描述:
osEventFlagsNew函数创建了一个新的事件标志对象,用于跨线程发送事件,并返回事件标志对象标识符的指针,或者在出现错误时返回NULL。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,但不能在内核初始化 (调用 osKernelInitialize)之前调用该函数。
注意 :不能在中断服务调用该函数
参数:
名字 | 描述 |
---|---|
attr | 事件标志属性;空:默认值. |
osEventFlagsSet()
uint32_t osEventFlagsSet(osEventFlagsId_t ef_id,uint32_t flags)
描述: osEventFlagsSet函数在一个由参数ef_id指定的事件标记对象中设置由参数flags指定的事件标记。
注意 :不能在中断服务调用该函数
参数:
名字 | 描述 |
---|---|
ef_id | 事件标志由osEventFlagsNew获得的ID. |
flags | 指定设置的标志. |
osEventFlagsWait()
uint32_t osEventFlagsWait(osEventFlagsId_t ef_id,uint32_t flags,uint32_t options,uint32_t timeout)
描述: osEventFlagsWait函数挂起当前运行线程,直到设置了由参数ef_id指定的事件对象中的任何或所有由参数flags指定的事件标志。当这些事件标志被设置,函数立即返回。否则,线程将被置于阻塞状态。
注意 :如果参数timeout设置为0,可以从中断服务例程调用
参数:
名字 | 描述 |
---|---|
ef_id | 事件标志由osEventFlagsNew获得的ID. |
flags | 指定要等待的标志. |
options | 指定标记选项. |
timeout | 超时时间,0表示不超时 |
软件设计
软件设计
主要代码分析
在Event_example函数中,通过osEventFlagsNew()函数创建了事件标记ID,Thread_EventReceiver()函数中通过osEventFlagsWait()函数一直将线程置于阻塞状态,等待事件标记。在Thread_EventSender()函数中通过osEventFlagsSet()函数每隔1S设置的标志,实现任务间的同步。
event_yuchuan_example.c
#include <stdio.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"
#define FLAGS_MSK1 0x00000001U
#define FLAGS_MSK2 0x00000002U
#define FLAGS_MSK3 0x00000003U
#define GPIO_LED 2
osEventFlagsId_t evnetId;
void yuchuanSenderEvent(void *argument)
{
(void)argument;
while (1)
{
//osEventFlagsSet(evnetId,FLAGS_MSK1);
osEventFlagsSet(evnetId,FLAGS_MSK2);
osEventFlagsSet(evnetId,FLAGS_MSK3);
osThreadYield();
//设置高电平点亮LED灯
IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE1);
//printf("Yuchuan Thread Sender!!!\n");
osDelay(100);
}
}
void huayingReceiveEvent(void *argument)
{
(void)argument;
uint32_t flag;
while (1)
{
//设置GPIO输出低电平熄灭LED灯
IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE0);
flag = osEventFlagsWait(evnetId,FLAGS_MSK1|FLAGS_MSK2|FLAGS_MSK3,osFlagsWaitAny,osWaitForever);
//flag = osEventFlagsWait(evnetId,FLAGS_MSK1|FLAGS_MSK2|FLAGS_MSK3,osFlagsWaitAll,osWaitForever);
printf("Receive Flags is %d\n",flag);
osDelay(100);
}
}
static void yuchuanEventExample(void)
{
//初始化GPIO
IoTGpioInit(GPIO_LED);
//设置GPIO为输出方向
IoTGpioSetDir(GPIO_LED,IOT_GPIO_DIR_OUT);
evnetId = osEventFlagsNew(NULL);
osThreadAttr_t attr;
attr.attr_bits = 0U;
attr.cb_mem = NULL;
attr.cb_size = 0U;
attr.stack_mem = NULL;
attr.stack_size = 1024 * 4;
attr.priority = 25;
attr.name = "YuchuanSenderThread";
if (osThreadNew((osThreadFunc_t)yuchuanSenderEvent,NULL,&attr) == NULL)
{
/* code */
printf("Falied to Create Sender Event!!!\n");
}
attr.name = "HuayingSenderThread";
if (osThreadNew((osThreadFunc_t)huayingReceiveEvent,NULL,&attr) == NULL)
{
/* code */
printf("Falied to Create Receive Evnet!!!\n");
}
}
APP_FEATURE_INIT(yuchuanEventExample);
编译调试
applications/sample/BearPi/BearPi-HM_Nano/sample/A3_YuchuanEvent/BUILD.gn
static_library("yuchuanEvent"){
sources = [
"event_yuchuan_example.c",
]
include_dirs = [
"//utils/native/lite/include",
"//kernel/liteos_m/kal/cmsis",
"//base/iot_hardware/peripheral/interfaces/kits",
]
}
修改 BUILD.gn 文件
修改 applications\BearPi\BearPi-HM_Nano\sample
路径下 BUILD.gn 文件,指定yuchuanEvent参与编译。
# Copyright (c) 2020 Nanjing Xiaoxiongpai Intelligent Technology Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import("//build/lite/config/component/lite_component.gni")
lite_component("sample") {
features = [
#"A1_kernal_thread:thread_example",
#"A2_kernel_timer:timer_example",
#"A3_kernel_event:event_example",
#"A4_kernel_mutex:mutex_example",
#"A5_kernel_semaphore:semaphore_example",
#"A6_kernel_message:message_example",
#"B1_basic_led_blink:led_example",
#"B2_basic_button:button_example",
#"B3_basic_pwm_led:pwm_example",
#"B4_basic_adc:adc_example",
#"B5_basic_i2c_nfc:i2c_example",
#"B6_basic_uart:uart_example",
#"C1_e53_sf1_mq2:e53_sf1_example",
#"C2_e53_ia1_temp_humi_pls:e53_ia1_example",
#"C3_e53_sc1_pls:e53_sc1_example",
#"C4_e53_sc2_axis:e53_sc2_example",
#"C5_e53_is1_infrared:e53_is1_example",
#"D1_iot_wifi_ap:wifi_ap",
#"D2_iot_wifi_sta_connect:wifi_sta_connect",
#"D3_iot_udp_client:udp_client",
#"D4_iot_tcp_server:tcp_server",
#"D5_iot_mqtt:iot_mqtt",
#"D6_iot_cloud_oc:oc_mqtt",
#"D7_iot_cloud_onenet:onenet_mqtt",
#"D8_iot_cloud_oc_smoke:cloud_oc_smoke",
#"D9_iot_cloud_oc_light:cloud_oc_light",
#"D10_iot_cloud_oc_manhole_cover:cloud_oc_manhole_cover",
#"D11_iot_cloud_oc_infrared:cloud_oc_infrared",
#"D12_iot_cloud_oc_agriculture:cloud_oc_agriculture",
#"D13_iot_cloud_oc_gps:cloud_oc_gps",
#"A1_YuchuanThread:yuchuanThread",
#"A2_YuchuanTimer:yuchuanTimer",
#"A4_YuchuanMutex:yuchuanMutex",
#"A5_YuchuanSemaphore:yuchuanSemaphore",
"A3_YuchuanEvent:yuchuanEvent",
]
}
运行结果
示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,会每隔1S输出一次日志。
ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
FileSystem mount ok.
wifi init success!
hiview init success.Receive Flags is 3
00 00:00:00 0 132 D 0/HIVIEW: log limit init success.
00 00:00:00 0 132 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0308 TaskPool:0xe4b3c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b0314 TaskPool:0xe4b5c
00 00:00:00 0 132 I 1/SAMGR: Init service:0x4b07e0 TaskPool:0xe4b7c
00 00:00:00 0 164 I 1/SAMGR: Init service 0x4b0314 <time: 0ms> success!
00 00:00:00 0 64 I 1/SAMGR: Init service 0x4b0308 <time: 0ms> success!
00 00:00:00 0 8 I 1/SAMGR: Init service 0x4b07e0 <time: 10ms> success!
00 00:00:00 0 8 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 64 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 64 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
Receive Flags is 3
- 点赞
- 收藏
- 关注作者
评论(0)