小熊派鸿蒙社区
直播中

小熊派开源社区

4年用户 123经验值
擅长:嵌入式技术 物联网
私信 关注
[经验]

BearPi-HM_Nano开发板鸿蒙OS内核编程开发——消息队列

BearPi-HM_Nano开发板鸿蒙OS内核编程开发——消息队列本示例将演示如何在BearPi-HM_Nano开发板上使用cmsis 2.0 接口通过消息队列进行线程之间交换消息

MessageQueue API分析osMessageQueueNew()osMessageQueueId_t osMessageQueueNew(uint32_t msg_count,uint32_t msg_size,const osMessageQueueAttr_t *attr)

描述:
函数osMessageQueueNew创建并初始化一个消息队列对象。该函数返回消息队列对象标识符,如果出现错误则返回NULL,可以在RTOS启动(调用 osKernelStart)之前安全地调用该函数,也可以在内核初始化 (调用 osKernelInitialize)之前调用该函数。
注意 :不能在中断服务调用该函数
参数:
名字
描述

msg_count
队列中的最大消息数.

msg_size
最大消息大小(以字节为单位).

attr
消息队列属性;空:默认值.
osMessageQueuePut()osStatus_t osMessageQueuePut(osMessageQueueId_t mq_id,const void *msg_ptr,uint8_t msg_prio,uint32_t timeout)       

描述: 函数osMessageQueuePut将msg_ptr指向的消息放入参数mq_id指定的消息队列中。
注意 :如果参数timeout设置为0,可以从中断服务例程调用
参数:
名字
描述

mq_id
由osMessageQueueNew获得的消息队列ID.

msg_ptr
要发送的消息.

msg_prio
指优先级.

timeout
超时值.
osMessageQueueGet()osStatus_t osMessageQueueGet(osMessageQueueId_t mq_id,void *msg_ptr,uint8_t *msg_prio,uint32_t         timeout)

描述: 函数osMessageQueueGet从参数mq_id指定的消息队列中检索消息,并将其保存到参数msg_ptr所指向的缓冲区中。
注意 :如果参数timeout设置为0,可以从中断服务例程调用。
参数:
名字
描述

mq_id
由osMessageQueueNew获得的消息队列ID.

msg_ptr
指针指向队列中获取消息的缓冲区指针.

msg_prio
指优先级.

timeout
超时值.
软件设计软件设计主要代码分析
在Message_example函数中,通过osMessageQueueNew()函数创建了消息队列ID,Thread_MsgQueue1()函数中通过osMessageQueuePut()函数向消息队列中发送消息。在Thread_MsgQueue2()函数中通过osMessageQueueGet()函数读取消息队列中的消息比打印出来。
void Thread_MsgQueue1 (void *argument) {  (void)argument;   msg.Buf = "Hello BearPi-HM_Nano!";                                         // do some work...  msg.Idx    = 0U;  while (1)  {    osMessageQueuePut(mid_MsgQueue, &msg, 0U, 0U);    osThreadYield();                                            // suspend thread       osDelay(100);  }} void Thread_MsgQueue2 (void *argument) {  (void)argument;  osStatus_t status;  while (1) {    // Insert thread code here...    status = osMessageQueueGet(mid_MsgQueue, &msg, NULL, 0U);   // wait for message    if (status == osOK) {      printf("Message Queue Get msg:%sn",msg.Buf);    }  }}static void Message_example (void) {   mid_MsgQueue = osMessageQueueNew(MSGQUEUE_OBJECTS, 100, NULL);  if (mid_MsgQueue == NULL) {    printf("Falied to create Message Queue!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 = 25;  attr.name = "Thread_MsgQueue1";  if (osThreadNew(Thread_MsgQueue1, NULL, &attr) == NULL) {      printf("Falied to create Thread_MsgQueue1!n");  }  attr.name = "Thread_MsgQueue2";  if (osThreadNew(Thread_MsgQueue2, NULL, &attr) == NULL) {      printf("Falied to create Thread_MsgQueue2!n");  }}

编译调试修改 BUILD.gn 文件修改 applicationsBearPiBearPi-HM_Nanosample路径下 BUILD.gn 文件,指定 message_example 参与编译。
#"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",

运行结果示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,会打印从消息队列中获取的消息。
Message Queue Get msg:Hello BearPi-HM_Nano!Message Queue Get msg:Hello BearPi-HM_Nano!Message Queue Get msg:Hello BearPi-HM_Nano!Message Queue Get msg:Hello BearPi-HM_Nano!Message Queue Get msg:Hello BearPi-HM_Nano!


回帖(1)

爱德格尔

2021-9-23 10:11:32
@小熊派,sample目录下提供的A6_kernel_message学习样例代码编译下载运行,一会儿出现程序崩溃现象,串口打印的异常信息如下:
**********watchdog isr**********
**********syserr info start**********
kernel_ver      : Hi3861V100 R001C00SPC025,2020-09-03 18:10:00
**********Exception Information**********
PC Task Name    : Thread_M
PC Task ID      = 8
Cur Task ID     = 8
Task Stack Size = 0x2800
Exception Type  = 0x80000021
**********reg info**********
mepc         = 0x3fa868
mstatus      = 0x1880
mtval        = 0x0
mcause       = 0x80000021
ccause       = 0x0
ra           = 0x4a1722
sp           = 0xfe9f0
gp           = 0x11a9c0
tp           = 0x63000888
t0           = 0x8
t1           = 0xffffffe0
t2           = 0x0
s0           = 0x11d7c0
s1           = 0x4aec44
a0           = 0xfffffffd
a1           = 0x0
a2           = 0xfeaac
a3           = 0x200060a
a4           = 0xd00a0dff
a5           = 0xd00a0dff
a6           = 0xa
a7           = 0x6c
s2           = 0xe4664
s3           = 0x12121212
s4           = 0x11111111
s5           = 0x10101010
s6           = 0x9090909
s7           = 0x8080808
s8           = 0x7070707
s9           = 0x6060606
s10          = 0x5050505
s11          = 0x4040404
t3           = 0x0
t4           = 0x6c6c6548
t5           = 0x0
t6           = 0x16161616
**********memory info**********
Pool Addr    = 0xe8300
Pool Size    = 0x302c0
Fail Count   = 0x0
Peek Size    = 0x15010
Used Size    = 0x15010
**********task info**********
Name         : Thread_M
ID           = 8
Status       = 0x14
Stack Index  = 0x8
Stack Peak   = 0x280
Stack Size   = 0x2800
SP           = 0x119880
Stack        : 0xfc2e0 to 0xfeae0
Real SP      = 0xfe9f0
Stack Overflow  = 0
**********track_info**********
current_item:0x7
item_cnt:10
Index   TrackType   TrackID  CurTime  Data1  Data2
0001 0016 0007 0xbfe 0x3fa84c 0x0
0002 0016 0007 0xbff 0x3f5dfa 0x0
0003 0065 0006 0xc00 0x3f5dfa 0x3f5e78
0004 0065 0008 0xc00 0x3f5e78 0x3f5dfa
0005 0016 0007 0xc00 0x3f89e4 0x0
0006 0065 0002 0xc01 0x3f89e4 0x3f5e78
0007 0065 0008 0xc01 0x3f5e78 0x3f89e4
0008 0016 0007 0xbfb 0x3f5dfa 0x0
0009 0016 0007 0xbfc 0x3fa84c 0x0
0010 0016 0007 0xbfd 0x3fa868 0x0
**********Call Stack**********
Call Stack 0 -- 4a3092 addr:fea9c
Call Stack 1 -- 3f78c0 addr:feacc
Call Stack 2 -- 3f5e24 addr:feadc
**********Call Stack end**********
举报

更多回帖

发帖
×
20
完善资料,
赚取积分