想了解更多关于开源的应用内容,请访问:
51CTO 开源基础软件社区
https://ost.51cto.com
该程序是流程基于OpenHarmony标准系统编写的基础外设类:GPIO驱动。
目前已在凌蒙派-RK3568开发板跑通。讲解详细资料请参考官网:https://gitee.com/Lockzhiner-Electronics/lockzhiner-rk3568-openharmony/tree/master/samples/b03_platform_device_gpio。何编
详细资料请参考OpenHarmony官网:
GPIO(General-purpose input/output)即通用型输入输出。应用通常,程序GPIO控制器通过分组的流程方式管理所有GPIO管脚,每组GPIO有一个或多个寄存器与之关联,通过读写寄存器完成对GPIO管脚的操作。
GPIO(General-purpose input/output)即通用型输入输出。通常,GPIO控制器通过分组的方式管理所有GPIO管脚,每组GPIO有一个或多个寄存器与之关联,通过读写寄存器完成对GPIO管脚的操作。
GPIO模块各分层作用:
GPIO统一服务模式结构图:
为了保证上层在调用GPIO接口时能够正确的操作GPIO管脚,核心层在//drivers/hdf_core/framework/support/platform/include/gpio/gpio_core.h中定义了以下钩子函数,驱动适配者需要在适配层实现这些函数的具体功能,并与钩子函数挂接,从而完成适配层与核心层的交互。
GpioMethod定义:
struct GpioMethod { int32_t (*request)(struct GpioCntlr *cntlr, uint16_t local); // 【预留】 int32_t (*release)(struct GpioCntlr *cntlr, uint16_t local); // 【预留】 int32_t (*write)(struct GpioCntlr *cntlr, uint16_t local, uint16_t val); int32_t (*read)(struct GpioCntlr *cntlr, uint16_t local, uint16_t *val); int32_t (*setDir)(struct GpioCntlr *cntlr, uint16_t local, uint16_t dir); int32_t (*getDir)(struct GpioCntlr *cntlr, uint16_t local, uint16_t *dir); int32_t (*toIrq)(struct GpioCntlr *cntlr, uint16_t local, uint16_t *irq); // 【预留】 int32_t (*setIrq)(struct GpioCntlr *cntlr, uint16_t local, uint16_t mode, GpioIrqFunc func, void *arg); int32_t (*unsetIrq)(struct GpioCntlr *cntlr, uint16_t local); int32_t (*enableIrq)(struct GpioCntlr *cntlr, uint16_t local); int32_t (*disableIrq)(struct GpioCntlr *cntlr, uint16_t local);}
GpioMethod结构体成员的钩子函数功能说明:
函数成员 | 入参 | 出参 | 返回值 | 功能 |
write | cntlr:结构体指针,核心层GPIO控制器 local:uint16_t类型,GPIO端口标识号 val:uint16_t类型,电平传入值 | 无 | HDF_STATUS相关状态 | GPIO引脚写入电平值 |
read | cntlr:结构体指针,核心层GPIO控制器 local:uint16_t类型,GPIO端口标识 | val:uint16_t类型指针,用于传出电平值。 | HDF_STATUS相关状态 | GPIO引脚读取电平值 |
setDir | cntlr:结构体指针,核心层GPIO控制器 local:uint16_t类型,GPIO端口标识号 dir:uint16_t类型,管脚方向传入值 | 无 | HDF_STATUS相关状态 | 设置GPIO引脚输入/输出方向 |
getDir | cntlr:结构体指针,核心层GPIO控制器 local:uint16_t类型,GPIO端口标识号 | dir:uint16_t类型指针,用于传出管脚方向值 | HDF_STATUS相关状态 | 读GPIO引脚输入/输出方向 |
setIrq | cntlr:结构体指针,核心层GPIO控制器 local:uint16_t类型,GPIO端口标识号 mode:uint16_t类型,表示触发模式(边沿或电平) func:函数指针,中断服务程序; arg:void指针,中断服务程序入参 | 无 | HDF_STATUS相关状态 | 将GPIO引脚设置为中断模式 |
unsetIrq | cntlr:结构体指针,核心层GPIO控制器 local:uint16_t类型,GPIO端口标识号 | 无 | HDF_STATUS相关状态 | 取消GPIO中断设置 |
enableIrq | cntlr:结构体指针,核心层GPIO控制器 local:uint16_t类型,GPIO端口标识号 | 无 | HDF_STATUS相关状态 | 使能GPIO管脚中断 |
disableIrq | cntlr:结构体指针,核心层GPIO控制器 local:uint16_t类型,GPIO端口标识号 | 无 | HDF_STATUS相关状态 | 禁止GPIO管脚中断 |
GPIO驱动API接口功能:
接口名 | 描述 |
GpioGetByName(const char *gpioName) | 获取GPIO管脚ID |
int32_t GpioRead(uint16_t gpio, uint16_t *val) | 读GPIO管脚电平值 |
int32_t GpioWrite(uint16_t gpio, uint16_t val) | 写GPIO管脚电平值 |
int32_t GpioGetDir(uint16_t gpio, uint16_t *dir) | 获取GPIO管脚方向 |
int32_t GpioSetDir(uint16_t gpio, uint16_t dir) | 设置GPIO管脚方向 |
int32_t GpioUnsetIrq(uint16_t gpio, void *arg); | 取消GPIO管脚对应的中断服务函数 |
int32_t GpioSetIrq(uint16_t gpio, uint16_t mode, GpioIrqFunc func, void *arg) | 设置GPIO管脚对应的中断服务函数 |
int32_t GpioEnableIrq(uint16_t gpio) | 使能GPIO管脚中断 |
int32_t GpioDisableIrq(uint16_t gpio) | 禁止GPIO管脚中断 |
GPIO标准API通过GPIO管脚号来操作指定管脚,使用GPIO的一般流程如下图所示:
查看《凌蒙派-RK3568开发板_排针说明表_》(即Git仓库的//docs/board/凌蒙派-RK3568开发板_排针说明表_v1.0.xlsx),选中0_B5(即GPIO0_B5)。
创建config/device_info.hcs,用于GPIO驱动设备描述,具体内容如下:
root { device_info { platform :: host { device_gpio :: device { device0 :: deviceNode { // GPIO控制器信息描述 policy = 2; // 对外发布服务,必须为2,用于定义GPIO管理器的服务 priority = 50; permission = 0644; moduleName = "HDF_PLATFORM_GPIO_MANAGER"; // 这与drivers/hdf_core/framework/support/platform/src/gpio/gpio_service.c的g_gpioServiceEntry.moduleName对应,它主要负责GPIO引脚的管理 serviceName = "HDF_PLATFORM_GPIO_MANAGER"; } device1 :: deviceNode { policy = 0; // 等于0,不需要发布服务 priority = 55; // 驱动驱动优先级 permission = 0644; // 驱动创建设备节点权限 moduleName = "linux_gpio_adapter"; // 用于指定驱动名称,必须是linux_adc_adapter,与drivers/hdf_core/adapter/khdf/linux/platform/gpio/gpio_adapter.c对应 deviceMatchAttr = ""; // 用于配置控制器私有数据,不定义 } } } }}
注意:
编辑//vendor/lockzhiner/rk3568/hdf_config/khdf/hdf.hcs,将device_info.hcs添加配置树中。具体内容如下所示:
#include "../../samples/b03_platform_device_gpio/config/device_info.hcs"
//drivers/hdf_core/adapter/khdf/linux/platform/gpio/gpio_adapter.c已对Linux Gpio子系统进行规范化操作。因此,我们不需要额外的GPIO寄存器操作。
gpio_test.c主要分为两个部分:
(1)对gpio引脚进行读操作。
// GPIO设置为输出ret = GpioSetDir(m_gpio_id, GPIO_DIR_OUT);if (ret != 0) { PRINT_ERROR("GpioSetDir failed and ret = %d\n", ret); return -1;}// GPIO输出电平ret = GpioWrite(m_gpio_id, m_gpio_value);if (ret != 0) { PRINT_ERROR("GpioWrite failed and ret = %d\n", ret); return -1;}
(2)对gpio引脚进行写操作。
// GPIO设置为输出ret = GpioSetDir(m_gpio_id, GPIO_DIR_IN);if (ret != 0) { PRINT_ERROR("GpioSetDir failed and ret = %d\n", ret); return -1;}// 读取GPIO引脚的电平ret = GpioRead(m_gpio_id, &m_gpio_value);if (ret != 0) { PRINT_ERROR("GpioRead failed and ret = %d\n", ret); return -1;}printf("GPIO Read Successful and GPIO = %d, value = %d\n", m_gpio_id, m_gpio_value);
import("//build/ohos.gni")import("//drivers/hdf_core/adapter/uhdf2/uhdf.gni")ohos_executable("rk3568_gpio_test") { sources = [ "gpio_test.c" ] include_dirs = [ "$hdf_framework_path/include", "$hdf_framework_path/include/core", "$hdf_framework_path/include/osal", "$hdf_framework_path/include/platform", "$hdf_framework_path/include/utils", "$hdf_uhdf_path/osal/include", "$hdf_uhdf_path/ipc/include", "//base/hiviewdfx/hilog/interfaces/native/kits/include", "//third_party/bounds_checking_function/include", ] deps = [ "$hdf_uhdf_path/platform:libhdf_platform", "$hdf_uhdf_path/utils:libhdf_utils", "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", ] cflags = [ "-Wall", "-Wextra", "-Werror", "-Wno-format", "-Wno-format-extra-args", ] part_name = "product_rk3568" install_enable = true}
编辑//vendor/lockzhiner/rk3568/samples/BUILD.gn,开启sample编译。具体如下:
"b03_platform_device_gpio/app:rk3568_gpio_test",
建议使用docker编译方法,运行如下:
hb set -root .hb set#选择lockzhiner下的rk3568编译分支。hb build -f
该程序运行结果如下所示:
# rk3568_gpio_test -g 13 -igpio id: 13gpio dir: ingpio value: 0GPIO Read Successful and GPIO = 13, value = 1## # rk3568_gpio_test -g 13 -ogpio id: 13gpio dir: outgpio value: 0#
可将GPIO引脚接入排针中的GND或3V3引脚,查看GPIO输出结果。
想了解更多关于开源的内容,请访问:
51CTO 开源基础软件社区
https://ost.51cto.com
责任编辑:jianghua 来源: 51CTO 开源基础软件社区 GPIO鸿蒙(责任编辑:百科)
天保基建(000965.SZ):2020年净利降49.78% 基本每股收益0.0859元
Android Go 终于来了,Google 想要靠它获取下一个 10 亿用户
聚利宝控股(08527.HK):李朝昌辞任独立非执行董事 3月11日起生效
大别山革命老区正式迎来“高铁时代” 黄黄高铁正线全长126.85公里