Niobe开发板OpenHarmony内核编程开发——事件标志

本示例将演示如何在Niobe Wifi IoT开发板上使用cmsis 2.0 接口使用事件标志同步线程

EventFlags API分析

osEventFlagsNew()

    /// Create and Initialize an Event Flags object./// \param[in]     attr          event flags attributes; NULL: default values./// \return event flags ID for reference by other functions or NULL in case of error.
osEventFlagsId_t osEventFlagsNew(const osEventFlagsAttr_t *attr)

描述:

osEventFlagsNew函数创建了一个新的事件标志对象,用于跨线程发送事件,并返回事件标志对象标识符的指针,或者在出现错误时返回NULL。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,但不能在内核初始化 (调用 osKernelInitialize)之前调用该函数。

注意 :不能在中断服务调用该函数

参数:

名字描述
attr事件标志属性;空:默认值.

osEventFlagsSet()

   /// Set the specified Event Flags./// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew./// \param[in]     flags         specifies the flags that shall be set./// \return event flags after setting or error code if highest bit set.
uint32_t osEventFlagsSet(osEventFlagsId_t ef_id,uint32_t flags)

描述:
osEventFlagsSet函数在一个由参数ef_id指定的事件标记对象中设置由参数flags指定的事件标记。

注意 :不能在中断服务调用该函数

参数:

名字描述
ef_id事件标志由osEventFlagsNew获得的ID.
flags指定设置的标志.
return返回设置的事件标志,或者如果高位设置值则返回错误码.

osEventFlagsWait()

       /// Wait for one or more Event Flags to become signaled./// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew./// \param[in]     flags         specifies the flags to wait for./// \param[in]     options       specifies flags options (osFlagsXxxx)./// \param[in]     timeout       \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out./// \return event flags before clearing or error code if highest bit set.
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表示不超时
return返回设置的事件标志,或者如果高位设置值则返回错误码.

软件设计

软件设计

主要代码分析

在OS_Event_example函数中,通过osEventFlagsNew()函数创建了事件标记ID g_event_flags_id,OS_Thread_EventReceiver()函数中通过osEventFlagsWait()函数一直将线程置于阻塞状态,等待事件标记。在OS_Thread_EventSender()函数中通过osEventFlagsSet()函数每隔1S设置的标志,实现任务间的同步。

/***发送事件*\param[in] argument 发送参数*/
void OS_Thread_EventSender(void *argument)
{osEventFlagsId_t flags;(void)argument;while (1){/// Set the specified Event Flags./// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew./// \param[in]     flags         specifies the flags that shall be set./// \return event flags after setting or error code if highest bit set.flags = osEventFlagsSet(g_event_flags_id, FLAGS_MSK1);printf("Sender Flags is %d\n", flags);//挂起线程,让位其他线程调度osThreadYield();osDelay(100);}
}/***  接收事件* \param[in] argument 发送参数
*/
void OS_Thread_EventReceiver(void *argument)
{(void)argument;uint32_t flags;while (1){/// Wait for one or more Event Flags to become signaled./// \param[in]     ef_id         event flags ID obtained by \ref osEventFlagsNew./// \param[in]     flags         specifies the flags to wait for./// \param[in]     options       specifies flags options (osFlagsXxxx)./// \param[in]     timeout       \ref CMSIS_RTOS_TimeOutValue or 0 in case of no time-out./// \return event flags before clearing or error code if highest bit set.flags = osEventFlagsWait(g_event_flags_id, FLAGS_MSK1, osFlagsWaitAny, osWaitForever);printf("Receive Flags is %d\n", flags);}
}/*** 事件测试Example
*/
static void OS_Event_example(void)
{//  ==== Event Flags Management Functions ====/// Create and Initialize an Event Flags object./// \param[in]     attr          event flags attributes; NULL: default values./// \return event flags ID for reference by other functions or NULL in case of error.g_event_flags_id = osEventFlagsNew(NULL);if (g_event_flags_id == NULL){printf("Falied to create EventFlags!\n");return;}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 = "Thread_EventSender";/// Create a thread and add it to Active Threads./// \param[in]     func          thread function./// \param[in]     argument      pointer that is passed to the thread function as start argument./// \param[in]     attr          thread attributes; NULL: default values./// \return thread ID for reference by other functions or NULL in case of error//osThreadId_t osThreadNew (osThreadFunc_t func, void *argument, const osThreadAttr_t *attr);if (osThreadNew(OS_Thread_EventSender, NULL, &attr) == NULL){printf("Falied to create Thread_EventSender!\n");return;}attr.name = "Thread_EventReceiver";if (osThreadNew(OS_Thread_EventReceiver, NULL, &attr) == NULL){printf("Falied to create Thread_EventReceiver!\n");return;}
}

编译调试

修改 BUILD.gn 文件

修改 applications\app路径下 BUILD.gn 文件,指定 os_event_example 参与编译。

#"TW001_OS_helloworld:helloworld_example",
#"TW002_OS_thread:os_thread_example",
#"TW003_OS_timer:os_timer_example",
"TW004_OS_event:os_event_example",
#"TW005_OS_mutex:os_mutex_example",
#"TW006_OS_semp:os_semaphore_example",
#"TW007_OS_message:os_message_example",

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,会每隔1S输出一次日志。

Send Flags is 1
Receive Flags is 1
Send Flags is 1
Receive Flags is 1
Send Flags is 1
Receive Flags is 1
Send Flags is 1
Receive Flags is 1
Send Flags is 1
Receive Flags is 1
Send Flags is 1
Receive Flags is 1

为了能让大家更好的学习鸿蒙(HarmonyOS NEXT)开发技术,这边特意整理了《鸿蒙开发学习手册》(共计890页),希望对大家有所帮助:https://qr21.cn/FV7h05

《鸿蒙开发学习手册》:

如何快速入门:https://qr21.cn/FV7h05

  1. 基本概念
  2. 构建第一个ArkTS应用
  3. ……

开发基础知识:https://qr21.cn/FV7h05

  1. 应用基础知识
  2. 配置文件
  3. 应用数据管理
  4. 应用安全管理
  5. 应用隐私保护
  6. 三方应用调用管控机制
  7. 资源分类与访问
  8. 学习ArkTS语言
  9. ……

基于ArkTS 开发:https://qr21.cn/FV7h05

  1. Ability开发
  2. UI开发
  3. 公共事件与通知
  4. 窗口管理
  5. 媒体
  6. 安全
  7. 网络与链接
  8. 电话服务
  9. 数据管理
  10. 后台任务(Background Task)管理
  11. 设备管理
  12. 设备使用信息统计
  13. DFX
  14. 国际化开发
  15. 折叠屏系列
  16. ……

鸿蒙开发面试真题(含参考答案):https://qr18.cn/F781PH

鸿蒙开发面试大盘集篇(共计319页):https://qr18.cn/F781PH

1.项目开发必备面试题
2.性能优化方向
3.架构方向
4.鸿蒙开发系统底层方向
5.鸿蒙音视频开发方向
6.鸿蒙车载开发方向
7.鸿蒙南向开发方向

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/618377.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【Vue】响应式原理与ref

首先讲讲JS中的Proxy JavaScript 运行环境包含了一些不可枚举、不可写入的对象属性,然而在 ES5 之前开发者无法定义他们自己的不可枚举属性或不可写入属性。ES5 引入 Object.defineProperty() 方法以便开发者在这方面能够像 JS 引擎那样做。 ES6 为了让开发者能进…

Collection与数据结构 二叉树(三):二叉树精选OJ例题(下)

1.二叉树的分层遍历 OJ链接 上面这道题是分层式的层序遍历,每一层有哪些结点都很明确,我们先想一想普通的层序遍历怎么做 /*** 层序遍历* param root*/public void levelOrder1(Node root){Queue<Node> queue new LinkedList<>();queue.offer(root);while (!qu…

每日一题——阶乘计算升级版

题目链接&#xff1a; 6-10 阶乘计算升级版 - 基础编程题目集 (pintia.cn) 题目&#xff1a; 6-10 阶乘计算升级版 分数 20 本题要求实现一个打印非负整数阶乘的函数。 函数接口定义&#xff1a; void Print_Factorial ( const int N ); 其中N是用户传入的参数&#xff…

Java与Kotlin语言的特色之处

一、Java特色之处&#xff1a; 1.多异常捕获 一个try块可能捕获到多个异常&#xff0c;可以使用多个catch块分别处理每个异常&#xff0c;也可以使用一个catch块处理多个异常&#xff08;多个异常使用管道符|分隔&#xff09;。 多个catch块代码&#xff1a; try{ }catch(IOExc…

HashMap底层源码分析

HashMap底层源码分析 HashMap主要是用来存放键值对的&#xff0c;它基于哈希表的Map接口实现&#xff0c;是常用的Java集合之一&#xff0c;是非线程安全的。 HashMap可以存放null的Key和value&#xff0c;但是null作为键只能有一个&#xff0c;作为value可以有多个 方法名称…

浏览器原理---事件循环

浏览器原理 学习浏览器原理对于我们开发是很有必要的 我们可以了解到浏览器内部工作原理对自己的代码进行优化 进程线程 首先了解进程和线程 进程就就是内存在正在进行的应用程序 在内存中独占一个内存空间 并且进程之间是隔离的 可以看到每个应用都有一个进程 占用空间内存…

刷题之Leetcode206题(超级详细)

206.反转链表 力扣题目链接(opens new window)https://leetcode.cn/problems/reverse-linked-list/ 题意&#xff1a;反转一个单链表。 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 思路 如果再定义一个新的链表&#xff0…

AI来了,Spring还会远吗?(Spring AI初体验)

目录 一、创建项目二、first demo1、application.properties2、ChatController3、结果 三、个人思考 一、创建项目 官方文档的Getting Started 最低要求&#xff1a;JDK17 阿里云的Server URL&#xff08;https://start.aliyun.com/&#xff09;搜不到Spring AI&#xff0c;…

dbeaver数据库语言编辑器设置jdbc驱动

打开 dbeaver 软件 数据库 -> 驱动管理器 以mysql为例 双击 MySQL -> 库 -> 添加工件 然后 打开maven组件库 官网 找到mysql驱动对应的maven工件地址 复制进去然后确认就行了 参考 大神博客

vue源码解析——diff算法/双端比对/patchFlag/最长递增子序列

虚拟dom——virtual dom&#xff0c;提供一种简单js对象去代替复杂的 dom 对象&#xff0c;从而优化 dom 操作。virtual dom 是“解决过多的操作 dom 影响性能”的一种解决方案。virtual dom 很多时候都不是最优的操作&#xff0c;但它具有普适性&#xff0c;在效率、可维护性之…

基于人脸识别的发型推荐系统代码实现

1.摘要 本文介绍了一个基于人脸识别技术的发型推荐系统的实现与分析。该系统利用Python编程语言和相关库&#xff0c;结合Face人脸识别API&#xff0c;实现了用户上传照片后的性别识别、脸型分析和发型推荐功能。首先&#xff0c;用户通过Tkinter GUI界面选择上传照片&#xff…

Niobe开发板OpenHarmony内核编程开发——定时器

本示例将演示如何在Niobe Wifi IoT开发板上使用cmsis 2.0 接口进行定时器开发 Timer API分析 osTimerNew() /// Create and Initialize a timer./// \param[in] func function pointer to callback function./// \param[in] type \ref osTimerOnce …