RK3568DAYU开发板-平台驱动开发:GPIO驱动

发布于:2025-05-30 ⋅ 阅读:(21) ⋅ 点赞:(0)

1、概述

程序是基于OpenHarmony5.0.0标准系统编写的基础外设类:GPIO驱动。

系统版本:openharmony5.0.0

开发板:dayu200

编译环境:ubuntu22

部署路径: //sample/01_platform_gpio

2、基础知识

2.1、GPIO简介

GPIO(General-purpose input/output)即通用型输入输出。通常,GPIO控制器通过分组的方式管理所有GPIO管脚,每组GPIO有一个或多个寄存器与之关联,通过读写寄存器完成对GPIO管脚的操作。

2.2、GPIO平台驱动

GPIO(General-purpose input/output)即通用型输入输出。通常,GPIO控制器通过分组的方式管理所有GPIO管脚,每组GPIO有一个或多个寄存器与之关联,通过读写寄存器完成对GPIO管脚的操作。

GPIO模块各分层作用:

  • 接口层提供操作GPIO管脚的标准方法。
  • 核心层主要提供GPIO管脚资源匹配,GPIO管脚控制器的添加、移除以及管理的能力,通过钩子函数与适配层交互,供芯片厂家快速接入HDF框架。
  • 适配层主要是将钩子函数的功能实例化,实现具体的功能。

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管脚中断

2.3、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的一般流程如下图所示:

在这里插入图片描述

3、代码解析

3.1、代码目录

/oh5.0.0/sample/01_platform_gpio$ tree
.
├── app
│   └── gpio_test.c
├── BUILD.gn
├── bundle.json
└── config
    └── device_info.hcs

2 directories, 4 files

3.2、配置文件

3.2.1、device_info.hcs

创建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 = "";                       // 用于配置控制器私有数据,不定义
                }
            }
        }
    }
}

注意:

device_gpio:为配置树对gpio的设备类结点。

device0:是用于启用HDF_PLATFORM_GPIO_MANAGER驱动的,它负责对GPIO进行对外接口管理。

device1:是用于启用linux_gpio_adapter驱动的,它负责对Linux GPIO的读写(即对Linux Gpio子系统进行操作)。

3.3、HDF驱动

//drivers/hdf_core/adapter/khdf/linux/platform/gpio/gpio_adapter.c已对Linux Gpio子系统进行规范化操作。因此,我们不需要额外的GPIO寄存器操作。

下面通过分析GpioWrite函数来看后续的执行过程,首先来看下gpio控制器(GpioCntlr)的类图,如下:

在这里插入图片描述

主要操作过程如下:

drivers\hdf_core\framework\support\platform\src\gpio\gpio_if.c
int32_t GpioWrite(uint16_t gpio, uint16_t val)
    |-->struct GpioCntlr *cntlr = GpioCntlrGetByGpio(gpio)
        |-->struct PlatformManager *gpioMgr =GpioManagerGet()
            |-->static struct PlatformManager *manager = PlatformManagerGet(PLATFORM_MODULE_GPIO)//获取平台管理实例(包含设备列表和增删设备接口)
            |--> if (manager != NULL) {manager->add = GpioManagerAdd;manager->del = GpioManagerDel;//
        |-->struct PlatformDevice *device = PlatformManagerFindDevice(gpioMgr, (void *)(uintptr_t)gpio, GpioCntlrFindMatch)//在 `PlatformManager` 所管理的设备列表中查找符合特定条件的设备用传入的GpioCntlrFindMatch做判断
            |-->GpioCntlrFindMatch(struct PlatformDevice *device, void *data)//函数判断寄存器值是否匹配
            	|-->if (gpio >= cntlr->start && gpio < (cntlr->start + cntlr->count))return true;
        |-->return CONTAINER_OF(device, struct GpioCntlr, device)//返回gpio控制器(GpioCntlr)实例
    |-->ret = GpioCntlrWrite(cntlr, GpioCntlrGetLocal(cntlr, gpio), val);//实际设置引脚状态
        |-->return cntlr->ops->write(cntlr, local, val)//根据函数集的设置会发现实际会调用LinuxGpioWrite函数
	|-->GpioCntlrPut(cntlr)//清空引用计数

最终发现会调用到linux内核的api函数(gpio_set_value_cansleep),详细过程需要首先了解gpio驱动的注册过程,主要是通过适配器(linux_gpio_adapter)完成:

struct HdfDriverEntry g_gpioLinuxDriverEntry = {
    .moduleVersion = 1,
    .Bind = LinuxGpioBind,
    .Init = LinuxGpioInit,
    .Release = LinuxGpioRelease,
    .moduleName = "linux_gpio_adapter",
};
HDF_INIT(g_gpioLinuxDriverEntry);

在驱动框架hdf初始化时会调用匹配函数

static int32_t LinuxGpioInit(struct HdfDeviceObject *device)
|--> (void)gpiochip_find(device, LinuxGpioMatchProbe);//遍历device,根据传入的LinuxGpioMatchProbe,得到对应的chip

匹配的函数LinuxGpioMatchProbe如下

static int LinuxGpioMatchProbe(struct gpio_chip *chip, void *data)
    |-->cntlr = (struct GpioCntlr *)OsalMemCalloc(sizeof(*cntlr))
    |-->cntlr->ops = &g_method;//设置函数集,如下
    |-->cntlr->start = (uint16_t)chip->base;//寄存器基值
    |-->cntlr->count = (uint16_t)chip->ngpio;//引脚个数
    |-->ret = GpioCntlrAdd(cntlr);//

函数集如下:

static struct GpioMethod g_method = {
    .write = LinuxGpioWrite,
    .read = LinuxGpioRead,
    .setDir = LinuxGpioSetDir,
    .getDir = LinuxGpioGetDir,
    .setIrq = LinuxGpioSetIrq,
    .unsetIrq = LinuxGpioUnsetIrq,
    .enableIrq = LinuxGpioEnableIrq,
    .disableIrq = LinuxGpioDisableIrq,
};

在LinuxGpioWrite函数中设置gpio为调用的linux的标准函数(gpio_set_value_cansleep)

static int32_t LinuxGpioWrite(struct GpioCntlr *cntlr, uint16_t local, uint16_t val)
    gpio_set_value_cansleep(cntlr->start + local, val);

所以在int32_t GpioWrite(uint16_t gpio, uint16_t val)函数中最后调用的返回值(return cntlr->ops->write(cntlr, local, val)),实际调用的函数为LinuxGpioWrite,进而实现对linux内核接口的调用。

关于linux内核内部的操作可以参考这篇文章

3.4、应用程序

3.4.1、gpio_test.c

gpio_test.c主要分为两个部分:

  • 对gpio引脚进行读操作。
  • 对gpio引脚进行写操作。

(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);
3.4.2、BUILD.gn
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 = "rk3568_gpio_test"
  install_enable = true
}
3.4.3、参与应用程序编译

bundle.json

{
	"name": "@ohos/rk3568_gpio_test",
	"description": "rk3568_gpio_test example.",
	"version": "3.1",
	"license": "Apache License 2.0",
	"publishAs": "code-segment",
	"segment": {
		"destPath": "sample/01_platform_gpio"
	},
	"dirs": {},
	"scripts": {},
	"component": {
		"name": "rk3568_gpio_test",
		"subsystem": "sample",
		"syscap": [],
		"features": [],
		"adapted_system_type": [
			"mini",
			"small",
			"standard"
		],
		"rom": "10KB",
		"ram": "10KB",
		"deps": {
			"components": [
				"hdf_core",
				"hilog"
			],
			"third_party": []
		},
		"build": {
			"sub_component": [
				"//sample/01_platform_gpio:rk3568_gpio_test"
			],
			"inner_kits": [],
			"test": []
		}
	}
}

4、编译说明

运行如下:

在这里插入图片描述

5、运行结果

(1)将为GPIO0_D5引脚和GPIO3_C2用杜邦线连接,一个引脚设置输出高低电平,另一个引脚用来读取高低电平的方法进行验证。

在这里插入图片描述
在这里插入图片描述

由以下的GPIO配置方法可知GPIO0_D5引脚号为: 3 ∗ 8 + 5 = 29 3*8+5=29 38+5=29,GPIO3_C2引脚号为: 3 ∗ 32 + 2 ∗ 8 + 2 = 114 3*32+2*8+2=114 332+28+2=114

参考以下信息可得出以上引脚号

在这里插入图片描述

通过控制GPIO0_B5来验证HDF驱动程序,该程序运行结果如下所示:

在这里插入图片描述

(2)只验证GPIO电平输出可以通过控制LED灯的控制引脚。

LED灯的原理图如下

在这里插入图片描述

由原理图可知引脚分别为GPIO4_C2和GPIO4_C3和GPIO4_C5,引脚号为146、147和149。

# ./rk3568_gpio_test -g 146 -o -v 1                                            
gpio id: 146
gpio dir: out
gpio value: 1

可通过LED灯的亮灭来验证结果。(高电平时为亮,低电平为灭,我实测的结果为146:绿灯 147:红灯 149:蓝灯,与原理图不符)

参考资料

详细资料请参考官网:


网站公告

今日签到

点亮在社区的每一天
去签到