HarmonyOS小熊派 | HarmonyOS内核编程开发—消息队列

举报
Yuchuan 发表于 2021/07/02 23:06:59 2021/07/02
【摘要】 HarmonyOS内核编程开发——消息队列

BearPi-HM_Nano开发板HarmonyOS内核编程开发——消息队列

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

BearPi-HM_Nano

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()函数读取消息队列中的消息比打印出来。

#include <stdio.h>
#include <unistd.h>
#include <strings.h>
#include "ohos_init.h"
#include "cmsis_os2.h"
#include "iot_gpio.h"
#include "iot_gpio_ex.h"

#define MSGQUEUE_OBJECTS 16
#define GPIO_LED 2

typedef struct{
    char *Buf;

    uint8_t Idx;
}MSGQUEUE_OBJ_t;

MSGQUEUE_OBJ_t msg;

osMessageQueueId_t msgId;

void yuchuanThreadMsgQueue(void *argument)
{
    (void)argument;
    uint8_t num = 0;
    msg.Buf = "Yuchuan Send Message!!!";

    while (1)
    {
        msg.Idx = num++;
        /* code */
        osMessageQueuePut(msgId,&msg,0U,0U);
        //设置GPIO为高电平,点亮LED灯
        //IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE1);

        //线程挂起
        osThreadYield();
        osDelay(100);
    }
    
}

void huayingThreadMsgQueue(void *argument)
{
    (void)argument;
    osStatus_t status;
    uint32_t count;

    while (1)
    {
        /* code */
        count = osMessageQueueGetCount(msgId);
        printf("message queue get count:%d\n",count);
        if (count == MSGQUEUE_OBJECTS)
        {
            /* code */
            osMessageQueueDelete(msgId);
        }
        

        status = osMessageQueueGet(msgId,&msg,NULL,osWaitForever);
        if (status == osOK)
        {
            /* code */
            printf("Huaying Recive MessageQueue msg:%s index:%d\n",msg.Buf,msg.Idx);
            //设置GPIO为低电平,熄灭LED灯
            //IoTGpioSetOutputVal(GPIO_LED,IOT_GPIO_VALUE0);
        }
        osDelay(200);
    }
    
}

static void yuchuanMessageExample(void)
{
    //初始化GPIO
    IoTGpioInit(GPIO_LED);
    //设置GPIO为输出模式
    IoTGpioSetDir(GPIO_LED,IOT_GPIO_DIR_OUT);

    msgId = osMessageQueueNew(MSGQUEUE_OBJECTS,100,NULL);
    if (msgId == NULL)
    {
        /* code */
        printf("Falied to Create MessageQueue!!!\n");
    }
    

    osThreadAttr_t attr;
    attr.attr_bits = 0U;
    attr.cb_mem = NULL;
    attr.cb_size = 0U;
    attr.stack_mem = NULL;
    attr.stack_size = 0U;
    attr.priority = 25;

    attr.name = "YuchuanMessageQueue";
    if (osThreadNew((osThreadFunc_t)yuchuanThreadMsgQueue,NULL,&attr) == NULL)
    {
        /* code */
        printf("Falied to Create YuchuanMessageQueue!!!\n");
    }
    
    attr.name = "HuayingMessageQueue";
    if (osThreadNew((osThreadFunc_t)huayingThreadMsgQueue,NULL,&attr) == NULL)
    {
        /* code */
        printf("Falied to Create HuayingMessageQueue!!!\n");
    }
    
}

APP_FEATURE_INIT(yuchuanMessageExample);

编译调试

//applications/sample/BearPi/BearPi-HM_Nano/sample/A6_YuchuanMessage/BUILD.gn 文件

static_library("yuchuanMessageQueue"){
    sources = [
        "message_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 文件,指定 yuchuanMessageQueue 参与编译。

# 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",
        "A6_YuchuanMessage:yuchuanMessageQueue",
    ]
}

运行结果

示例代码编译烧录代码后,按下开发板的RESET按键,通过串口助手查看日志,会打印从消息队列中获取的消息。

ready to OS start
sdk ver:Hi3861V100R001C00SPC025 2020-09-03 18:10:00
formatting spiffs...
FileSystem mount ok.
wifi init success!

hiview init success.message queue get count:1
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:0
00 00:00:00 0 196 D 0/HIVIEW: log limit init success.
00 00:00:00 0 196 I 1/SAMGR: Bootstrap core services(count:3).
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b0268 TaskPool:0xe4b7c
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b0274 TaskPool:0xe4b9c
00 00:00:00 0 196 I 1/SAMGR: Init service:0x4b0740 TaskPool:0xe4bbc
00 00:00:00 0 228 I 1/SAMGR: Init service 0x4b0274 <time: 0ms> success!
00 00:00:00 0 128 I 1/SAMGR: Init service 0x4b0268 <time: 0ms> success!
00 00:00:00 0 72 I 1/SAMGR: Init service 0x4b0740 <time: 0ms> success!
00 00:00:00 0 72 I 1/SAMGR: Initialized all core system services!
00 00:00:00 0 128 I 1/SAMGR: Bootstrap system and application services(count:0).
00 00:00:00 0 128 I 1/SAMGR: Initialized all system and application services!
00 00:00:00 0 128 I 1/SAMGR: Bootstrap dynamic registered services(count:0).
message queue get count:1
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:1
message queue get count:2
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:2
message queue get count:3
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:3
message queue get count:4
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:4
message queue get count:5
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:5
message queue get count:6
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:6
message queue get count:7
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:7
message queue get count:8
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:8
message queue get count:9
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:9
message queue get count:10
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:10
message queue get count:11
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:11
message queue get count:12
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:12
message queue get count:13
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:13
message queue get count:14
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:14
message queue get count:15
Huaying Recive MessageQueue msg:Yuchuan Send Message!!! index:15
message queue get count:16
message queue get count:16
message queue get count:16
message queue get count:16
message queue get count:16
message queue get count:16

【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱: cloudbbs@huaweicloud.com
  • 点赞
  • 收藏
  • 关注作者

评论(0

0/1000
抱歉,系统识别当前为高风险访问,暂不支持该操作

全部回复

上滑加载中

设置昵称

在此一键设置昵称,即可参与社区互动!

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。

*长度不超过10个汉字或20个英文字符,设置后3个月内不可修改。