Niobe WiFi IoT开发板OpenHarmony内核编程开发——message

本示例将演示如何在Niobe WiFi IoT开发板上使用cmsis 2.0 接口进行消息队列开发

message API分析

osThreadNew()

c 复制代码
osThreadId_t osThreadNew(osThreadFunc_t	func, void *argument,const osThreadAttr_t *attr )

描述:

函数osThreadNew通过将线程添加到活动线程列表并将其设置为就绪状态来启动线程函数。线程函数的参数使用参数指针*argument传递。当创建的thread函数的优先级高于当前运行的线程时,创建的thread函数立即启动并成为新的运行线程。线程属性是用参数指针attr定义的。属性包括线程优先级、堆栈大小或内存分配的设置。可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,但不能在内核初始化 (调用 osKernelInitialize)之前调用该函数。

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

参数:

名字 描述
func 线程函数.
argument 作为启动参数传递给线程函数的指针
attr 线程属性

osMessageQueueNew()

c 复制代码
osMessageQueueId_t osMessageQueueNew (uint32_t msg_count,uint32_t msg_size,const osMessageQueueAttr_t *attr)

描述:

创建消息队列

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

参数:

名字 描述
msg_count 队列中的最大消息数
msg_size 最大消息大小(以字节为单位)
attr 消息队列属性;空:默认值
返回 消息队列ID

osMessageQueuePut()

c 复制代码
osStatus_t osMessageQueuePut (osMessageQueueId_t mq_id,const void *msg_ptr,uint8_t msg_prio,uint32_t timeout)

描述:

发送消息

注意 :如果参数timeout设置为0,可以从中断服务例程调用

参数:

名字 描述
mq_id 由osMessageQueueNew获得的消息队列ID
msg_ptr 要发送的消息
msg_prio 指优先级
timeout 超时值
返回 0 - 成功,非0 - 失败

osMessageQueueGet()

c 复制代码
osStatus_t osMessageQueueGet (osMessageQueueId_t mq_id,void *msg_ptr,uint8_t *msg_prio,uint32_t timeout)

描述:

获取消息

注意 :如果参数timeout设置为0,可以从中断服务例程调用

参数:

名字 描述
mq_id 由osMessageQueueNew获得的消息队列ID
msg_ptr 指针指向队列中获取消息的缓冲区指针
msg_prio 指优先级
timeout 超时值
返回 0 - 成功,非0 - 失败

软件设计

主要代码分析

在Msg_example函数中,通过osThreadNew()函数创建了Thread_MsgQ1、Thread_MsgQ2两个线程,Thread_MsgQ1、Thread_MsgQ2两个线程启动后会输出打印日志。

c 复制代码
typedef struct
{
    char *buffer;
} MSG_BUF;
MSG_BUF msg_buf;
void Thread_MsgQueue1(void *arg)
{
	msg_buf.buffer = "hello niobe";
	while (1)
	{
		osMessageQueuePut(MsgQueue, &msg_buf.buffer, 0U, 0U);
		//suspend thread
		osThreadYield();
		osDelay(100);
	}
}

void Thread_MsgQueue2(void *arg)
{
	osStatus_t status;

	while (1)
	{
		//wait for message
		status = osMessageQueueGet(MsgQueue, &msg_buf.buffer, NULL, 0U);
		if (status == osOK)
		{
            printf("Thread_MsgQueue2 Get msg: %s\n", msg_buf.buffer);
        }
        else
        {
            osDelay(100);
		}
	}
}

static void Msg_example(void)
{

	MsgQueue = osMessageQueueNew(10, 50, NULL);
	if (MsgQueue == NULL)
	{
		printf("create Message Queue failed!\n");
	}

	osThreadAttr_t attr;

	attr.attr_bits = 0U;
	attr.cb_mem = NULL;
	attr.cb_size = 0U;
	attr.stack_mem = NULL;
	attr.stack_size = 1024 * 10;
	attr.priority = 10;

	attr.name = "Thread_MsgQueue1";
	if (osThreadNew(Thread_MsgQueue1, NULL, &attr) == NULL)
	{
		printf("create Thread_MsgQueue1 failed!\n");
	}
	
	attr.name = "Thread_MsgQueue2";
	if (osThreadNew(Thread_MsgQueue2, NULL, &attr) == NULL)
	{
		printf("create Thread_MsgQueue2 failed!\n");
	}
}

APP_FEATURE_INIT(Msg_example);

编译调试

修改 BUILD.gn 文件

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

r 复制代码
#"TW001_OS_helloworld:helloworld_example",
"TW007_OS_message:Msg_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按键,通过串口助手查看日志

c 复制代码
Thread_MsgQueue2 Get msg:hello niobe

为了能让大家更好的学习鸿蒙(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.鸿蒙南向开发方向

相关推荐
HwJack201 小时前
HarmonyOS APP开发中Feature模块小案例:动态化开发的“瑞士军刀“
华为·harmonyos
鸿蒙程序媛2 小时前
【知识汇总】PixelMap 和 ArrayBuffer 详解
harmonyos
程序猿追2 小时前
HarmonyOS 6.0 网络请求深度解析:从基础调用到生产级封装
网络·华为·harmonyos
互联网散修4 小时前
鸿蒙应用开发UI基础第三十四节:媒体查询核心解析 —— 响应式布局与工具类封装
ui·harmonyos·媒体查询
性感博主在线瞎搞5 小时前
【鸿蒙开发】OpenHarmony与HarmonyOS调用C/C++教程
华为·harmonyos·鸿蒙·鸿蒙系统·openharmony
大雷神6 小时前
HarmonyOS APP<玩转React>开源教程二十三:面试题库功能
harmonyos
程序猿追6 小时前
HarmonyOS 5.0 自定义组件与状态管理实战:用 RelationalStore 构建可复用的任务看板
华为·harmonyos
程序猿追6 小时前
HarmonyOS 6.0 实战:用 Native C++ NDK 开发一款本地计步器应用
c++·华为·harmonyos
程序猿追8 小时前
HarmonyOS 6.0 PC端蓝牙开发全攻略:从设备扫描到数据收发
华为·harmonyos