全志R128 SDK HAL 模块开发指南之 MSGBOX

2024-05-15 11:04

本文主要是介绍全志R128 SDK HAL 模块开发指南之 MSGBOX,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

MSGBOX

msgbox 用来实现多 CPU 之间通讯,在一些 IC 内部可能同时存在多种核心用来实现多种不同功能,这些不同核心运行不同架构、不同系统,需要通过 MSGBOX 用来实现这些不同系统间通讯。

模块介绍

  • msgbox 为一个双端 fifo 结构,cpu0 从一端写,cpu1 从一端读。
  • rpmsg 为 linux 用来实现通讯的一种框架
  • msgbox 为片上处理器之间提供了中断通讯机制

对于 R128 平台,CPU Remote ID如下

CPURemote ID
ARM M33 Star0
RISC-V C9061
HIFI5 DSP2

模块配置

配置路径如下:

Kernel Setup --->Drivers Setup --->SoC HAL Drivers --->msgbox devices --->[*] enable msgbox driver

源码结构

msgbox/├── msgbox_amp            // msgbox AMP 实现│   ├── Makefile│   └── msgbox_amp.c        ├── platform              // 平台寄存器定义│   ├── msgbox-sun20iw2.h└── platform-msgbox.h     // 公共头文件

模块接口说明

头文件

#include <hal_msgbox.h>

初始化接口

函数原型:

int32_t hal_msgbox_init(void);

参数:

返回值:

  • 0:成功
  • 负数:失败

通道申请接口

函数原型:

uint32_t hal_msgbox_alloc_channel(struct msg_endpoint *edp, int32_t remote, int32_t read, int32_t write);

参数:

  • edp:msgbox的端点
  • remote:远端核心id
  • read:读通道
  • write:写通道

返回值:

  • 0:成功
  • 负数:失败

数据发送接口

函数原型:

uint32_t hal_msgbox_channel_send(struct msg_endpoint *edp, uint8_t *bf, uint32_t len);

参数:

  • edp:msgbox的端点
  • bf:数据buffer
  • len:buffer长度

返回值:

  • 0:成功
  • 负数:失败

通道释放接口

函数原型:

void hal_msgbox_free_channel(struct msg_endpoint *edp);

参数:

  • edp:msgbox的端点

返回值:

  • 0:成功
  • 负数:失败

MSGBOX 申请流程

  1. 使用hal_msgbox_alloc_channel接口申请 msgbox 通道
  2. 填充msg_endpoint接收回调,这个会在 msgbox 的中断函数里调用
  3. 通过hal_msgbox_channel_send进行数据发送
  4. 接收通过中断的方式进行接收,会调用msg_endpoint的回调,无需主动调用

MSGBOX 接收流程

  1. 在接收函数里会首先遍历所有的msg_endpoint,判断当前终端是否有中断发送
  2. irq_msgbox_channel_handler里会读取当前msg_endpoint的寄存器,来判断是否有中断,如果有,则读取数据
  3. 退出中断

MSGBOX 发送流程

  1. 调用hal_msgbox_channel_send接口进行数据发送
  2. msgbox_channel_send_data会判断是远端处理器是哪个,并且计算 local->remote 的系数 N 是多少,这个系数回存放在 to_coef_n 的表格里
  3. 计算完成后往远端的 msgbox 的 fifo 中写数据
  4. 发送完成

模块使用范例

#include <FreeRTOS.h>
#include <queue.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <string.h>
#include <awlog.h>
#include <hal_msgbox.h>#include <console.h>#define RECEIVE_QUEUE_LENGTH 16
#define RECEIVE_QUEUE_WAIT_MS 100struct msgbox_demo {int remote_id;int read_ch;int write_ch;QueueHandle_t recv_queue;
};static void print_help_msg(void)
{printf("\n");printf("USAGE:\n");printf("  hal_msgbox <REQUIRED_ARGUMENTS> [OPTIONS]\n");printf("\n");printf("REQUIRED_ARGUMENTS:\n");printf("  -E REMOTE_ID: specify remote id\n");printf("  -R READ_CH  : specify read channel\n");printf("  -W WRITE_CH : specify write channel\n");printf("OPTIONS:\n");printf("  -s MESSAGE  : send MESSAGE\n");printf("  -r          : receive messages\n");printf("  -t TIMEOUT  : exit in TIMEOUT seconds when receive\n");printf("e.g. (communicate with remote 0, use read channel 3 and write channel 3):\n");printf("  hal_msgbox -E 0 -R 3 -W 3 -s \"hello\" : send string \"hello\"\n");printf("  hal_msgbox -E 0 -R 3 -W 3 -r           : receive messages (default in 10 seconds)\n");printf("  hal_msgbox -E 0 -R 3 -W 3 -r -t 20     : receive messages in 20 seconds\n");printf("\n");
}static int recv_callback(unsigned long data, void *private_data)
{BaseType_t taskwoken = pdFALSE;printf("Receive callback (data: 0x%lx)\n", data);struct msgbox_demo *demo = private_data;BaseType_t ret = xQueueSendFromISR(demo->recv_queue, &data, &taskwoken);if (ret == errQUEUE_FULL) {printf("recv_queue is full\n");return -1;}if (ret == pdPASS) {portYIELD_FROM_ISR(taskwoken);}return 0;
}static int cmd_hal_msgbox(int argc, char *argv[])
{int ret = 0;int c;struct msgbox_demo demo= {.remote_id = -1,.read_ch = -1,.write_ch = -1,.recv_queue = NULL,};struct msg_endpoint ept;TickType_t start_ticks, current_ticks;int do_send = 0;const char *data_send= NULL;int do_recv = 0;int timeout_sec = 10;uint32_t data_recv;if (argc <= 1) {print_help_msg();ret = -1;goto out;}while ((c = getopt(argc, argv, "hs:rt:E:W:R:")) != -1) {switch (c) {case 'h' :print_help_msg();ret = 0;goto out;case 'E':demo.remote_id = atoi(optarg);break;case 'R':demo.read_ch = atoi(optarg);break;case 'W':demo.write_ch = atoi(optarg);break;case 's':do_send = 1;data_send = optarg;break;case 'r':do_recv = 1;break;case 't':timeout_sec = atoi(optarg);break;default:print_help_msg();ret = -1;goto out;}}if (demo.remote_id < 0 || demo.read_ch < 0 || demo.write_ch < 0) {printf("Error. Please specify remote id, read channel and write channel\n");print_help_msg();ret = -1;goto out;}printf("remote id: %d, write channel: %d, read channel: %d\n",demo.remote_id, demo.write_ch, demo.read_ch);if (do_recv) {demo.recv_queue = xQueueCreate(RECEIVE_QUEUE_LENGTH, sizeof(uint32_t));if (!demo.recv_queue) {printf("Failed to create receive queue\n");ret = -1;goto out;}ept.rec = (void *)recv_callback;ept.private = &demo;}ret = hal_msgbox_alloc_channel(&ept, demo.remote_id, demo.read_ch, demo.write_ch);if (ret != 0) {printf("Failed to allocate msgbox channel\n");goto delete_recv_queue;}if (do_send) {ret = hal_msgbox_channel_send(&ept, (unsigned char *)data_send, strlen(data_send));if (ret != 0) {printf("Failed to send message\n");goto free_channel;}}if (do_recv) {printf("hal_msgbox will exit in %d seconds\n", timeout_sec);start_ticks = xTaskGetTickCount();printf("start_ticks: %u\n", start_ticks);while (1) {if (pdTRUE == xQueueReceive(demo.recv_queue, &data_recv,RECEIVE_QUEUE_WAIT_MS / portTICK_PERIOD_MS)) {printf("Received from queue: 0x%x\n", data_recv);}current_ticks = xTaskGetTickCount();if ((current_ticks - start_ticks) * portTICK_PERIOD_MS>= timeout_sec * 1000) {printf("current_ticks: %u\n", current_ticks);break;}}}printf("hal_msgbox exited\n");ret = 0;free_channel:hal_msgbox_free_channel(&ept);
delete_recv_queue:if (do_recv) {vQueueDelete(demo.recv_queue);}
out:return ret;
}FINSH_FUNCTION_EXPORT_CMD(cmd_hal_msgbox, hal_msgbox, hal msgbox);

这篇关于全志R128 SDK HAL 模块开发指南之 MSGBOX的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/991657

相关文章

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

SpringBoot整合OpenFeign的完整指南

《SpringBoot整合OpenFeign的完整指南》OpenFeign是由Netflix开发的一个声明式Web服务客户端,它使得编写HTTP客户端变得更加简单,本文为大家介绍了SpringBoot... 目录什么是OpenFeign环境准备创建 Spring Boot 项目添加依赖启用 OpenFeig

SpringBoot请求参数接收控制指南分享

《SpringBoot请求参数接收控制指南分享》:本文主要介绍SpringBoot请求参数接收控制指南,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Spring Boot 请求参数接收控制指南1. 概述2. 有注解时参数接收方式对比3. 无注解时接收参数默认位置

Go语言开发实现查询IP信息的MCP服务器

《Go语言开发实现查询IP信息的MCP服务器》随着MCP的快速普及和广泛应用,MCP服务器也层出不穷,本文将详细介绍如何在Go语言中使用go-mcp库来开发一个查询IP信息的MCP... 目录前言mcp-ip-geo 服务器目录结构说明查询 IP 信息功能实现工具实现工具管理查询单个 IP 信息工具的实现服

Python的time模块一些常用功能(各种与时间相关的函数)

《Python的time模块一些常用功能(各种与时间相关的函数)》Python的time模块提供了各种与时间相关的函数,包括获取当前时间、处理时间间隔、执行时间测量等,:本文主要介绍Python的... 目录1. 获取当前时间2. 时间格式化3. 延时执行4. 时间戳运算5. 计算代码执行时间6. 转换为指

Python正则表达式语法及re模块中的常用函数详解

《Python正则表达式语法及re模块中的常用函数详解》这篇文章主要给大家介绍了关于Python正则表达式语法及re模块中常用函数的相关资料,正则表达式是一种强大的字符串处理工具,可以用于匹配、切分、... 目录概念、作用和步骤语法re模块中的常用函数总结 概念、作用和步骤概念: 本身也是一个字符串,其中

Python中的getopt模块用法小结

《Python中的getopt模块用法小结》getopt.getopt()函数是Python中用于解析命令行参数的标准库函数,该函数可以从命令行中提取选项和参数,并对它们进行处理,本文详细介绍了Pyt... 目录getopt模块介绍getopt.getopt函数的介绍getopt模块的常用用法getopt模

CentOS7更改默认SSH端口与配置指南

《CentOS7更改默认SSH端口与配置指南》SSH是Linux服务器远程管理的核心工具,其默认监听端口为22,由于端口22众所周知,这也使得服务器容易受到自动化扫描和暴力破解攻击,本文将系统性地介绍... 目录引言为什么要更改 SSH 默认端口?步骤详解:如何更改 Centos 7 的 SSH 默认端口1

SpringBoot多数据源配置完整指南

《SpringBoot多数据源配置完整指南》在复杂的企业应用中,经常需要连接多个数据库,SpringBoot提供了灵活的多数据源配置方式,以下是详细的实现方案,需要的朋友可以参考下... 目录一、基础多数据源配置1. 添加依赖2. 配置多个数据源3. 配置数据源Bean二、JPA多数据源配置1. 配置主数据