1e41f4b71Sopenharmony_ci# PIN 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ci## 概述 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci### 功能简介 6e41f4b71Sopenharmony_ci 7e41f4b71Sopenharmony_ciPIN即管脚控制器,用于统一管理各SoC的管脚资源,对外提供管脚复用功能。 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci### 基本概念 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ciPIN是一个软件层面的概念,目的是为了统一对各SoC的PIN管脚进行管理,对外提供管脚复用功能,配置PIN管脚的电气特性。 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci- SoC(System on Chip) 14e41f4b71Sopenharmony_ci 15e41f4b71Sopenharmony_ci 系统级芯片,又称作片上系统,通常是面向特定用途将微处理器、模拟IP核、数字IP核和存储器集成在单一芯片的标准产品。 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci- 管脚复用 18e41f4b71Sopenharmony_ci 19e41f4b71Sopenharmony_ci 由于芯片自身的引脚数量有限,无法满足日益增多的外接需求。此时可以通过软件层面的寄存器设置,让引脚工作在不同的状态,从而实现相同引脚完成不同功能的目的。 20e41f4b71Sopenharmony_ci 21e41f4b71Sopenharmony_ci### 运作机制 22e41f4b71Sopenharmony_ci 23e41f4b71Sopenharmony_ci在HDF框架中,同类型设备对象较多时(可能同时存在十几个同类型配置器),若采用独立服务模式,则需要配置更多的设备节点,且相关服务会占据更多的内存资源。相反,采用统一服务模式可以使用一个设备服务作为管理器,统一处理所有同类型对象的外部访问(这会在配置文件中有所体现),实现便捷管理和节约资源的目的。PIN模块接口适配模式采用统一服务模式(如图1所示)。 24e41f4b71Sopenharmony_ci 25e41f4b71Sopenharmony_ci在统一模式下,所有的控制器都被核心层统一管理,并由核心层统一发布一个服务供接口层,因此这种模式下驱动无需再为每个控制器发布服务。 26e41f4b71Sopenharmony_ci 27e41f4b71Sopenharmony_ciPIN模块各分层作用: 28e41f4b71Sopenharmony_ci 29e41f4b71Sopenharmony_ci- 接口层提供获取PIN管脚、设置PIN管脚推拉方式、获取PIN管脚推拉方式、设置PIN管脚推拉强度、获取PIN管脚推拉强度、设置PIN管脚功能、获取PIN管脚功能、释放PIN管脚的接口。 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci- 核心层主要提供PIN管脚资源匹配,PIN管脚控制器的添加、移除以及管理的能力,通过钩子函数与适配层交互。 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ci- 适配层主要是将钩子函数的功能实例化,实现具体的功能。 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**图 1** 统一服务模式结构图 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci 38e41f4b71Sopenharmony_ci 39e41f4b71Sopenharmony_ci### 约束与限制 40e41f4b71Sopenharmony_ci 41e41f4b71Sopenharmony_ciPIN模块目前只支持小型系统LiteOS-A内核。 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci## 开发指导 44e41f4b71Sopenharmony_ci 45e41f4b71Sopenharmony_ci### 场景介绍 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ciPIN模块主要用于管脚资源管理。在各SoC对接HDF框架时,需要来适配PIN驱动。下文将介绍如何进行PIN驱动适配。 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_ci### 接口说明 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ci为了保证上层在调用PIN接口时能够正确的操作PIN管脚,核心层在//drivers/hdf_core/framework/support/platform/include/pin/pin_core.h中定义了以下钩子函数,驱动适配者需要在适配层实现这些函数的具体功能,并与钩子函数挂接,从而完成适配层与核心层的交互。 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ciPinCntlrMethod定义: 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci```c 56e41f4b71Sopenharmony_cistruct PinCntlrMethod { 57e41f4b71Sopenharmony_ci int32_t (*SetPinPull)(struct PinCntlr *cntlr, uint32_t index, enum PinPullType pullType); 58e41f4b71Sopenharmony_ci int32_t (*GetPinPull)(struct PinCntlr *cntlr, uint32_t index, enum PinPullType *pullType); 59e41f4b71Sopenharmony_ci int32_t (*SetPinStrength)(struct PinCntlr *cntlr, uint32_t index, uint32_t strength); 60e41f4b71Sopenharmony_ci int32_t (*GetPinStrength)(struct PinCntlr *cntlr, uint32_t index, uint32_t *strength); 61e41f4b71Sopenharmony_ci int32_t (*SetPinFunc)(struct PinCntlr *cntlr, uint32_t index, const char *funcName); 62e41f4b71Sopenharmony_ci int32_t (*GetPinFunc)(struct PinCntlr *cntlr, uint32_t index, const char **funcName); 63e41f4b71Sopenharmony_ci}; 64e41f4b71Sopenharmony_ci``` 65e41f4b71Sopenharmony_ci 66e41f4b71Sopenharmony_ci**表 1** PinCntlrMethod成员的钩子函数功能说明 67e41f4b71Sopenharmony_ci 68e41f4b71Sopenharmony_ci| 成员函数 | 入参 | 出参 | 返回值 | 功能 | 69e41f4b71Sopenharmony_ci| ------------ | ------------------------------------------- | ------ | ---- | ---- | 70e41f4b71Sopenharmony_ci| SetPinPull | cntlr:结构体指针,核心层PIN控制器<br>index:uint32_t类型变量,管脚索引号<br/>pullType:枚举常量,PIN管脚推拉方式 | 无 |HDF_STATUS相关状态|PIN设置管脚推拉方式 | 71e41f4b71Sopenharmony_ci| GetPinPull | cntlr:结构体指针,核心层PIN控制器<br/>index:uint32_t类型变量,管脚索引号 | pullType:枚举常量指针,传出获取的PIN管脚推拉方式 | HDF_STATUS相关状态 | PIN获取管脚推拉方式 | 72e41f4b71Sopenharmony_ci| SetPinStrength | cntlr:结构体指针,核心层PIN控制器<br/>index:uint32_t类型变量,管脚索引号<br/>strength:uint32_t变量,PIN推拉强度 | 无 | HDF_STATUS相关状态 | PIN设置推拉强度 | 73e41f4b71Sopenharmony_ci| GetPinStrength | cntlr:结构体指针,核心层PIN控制器<br/>index:uint32_t类型变量,管脚索引号 | strength:uint32_t变量指针,传出获取的PIN推拉强度 | HDF_STATUS相关状态 | PIN获取推拉强度 | 74e41f4b71Sopenharmony_ci| SetPinFunc | cntlr:结构体指针,核心层PIN控制器<br/>index:uint32_t类型变量,管脚索引号<br/>funcName:char指针常量,传入PIN管脚功能 | 无 | HDF_STATUS相关状态 | PIN设置管脚功能 | 75e41f4b71Sopenharmony_ci| GetPinFunc | cntlr:结构体指针,核心层PIN控制器<br/>index:uint32_t类型变量,管脚索引号 | funcName:char双重指针常量,传出获取的PIN管脚功能 | HDF_STATUS相关状态 | PIN获取管脚功能 | 76e41f4b71Sopenharmony_ci 77e41f4b71Sopenharmony_ci### 开发步骤 78e41f4b71Sopenharmony_ci 79e41f4b71Sopenharmony_ciPIN模块适配HDF框架包含以下四个步骤: 80e41f4b71Sopenharmony_ci 81e41f4b71Sopenharmony_ci- 实例化驱动入口 82e41f4b71Sopenharmony_ci 83e41f4b71Sopenharmony_ci- 配置属性文件 84e41f4b71Sopenharmony_ci 85e41f4b71Sopenharmony_ci- 实例化PIN控制器对象 86e41f4b71Sopenharmony_ci 87e41f4b71Sopenharmony_ci- 驱动调试 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci### 开发实例 90e41f4b71Sopenharmony_ci 91e41f4b71Sopenharmony_ci下方将基于Hi3516DV300开发板以//device/soc/hisilicon/common/platform/pin/pin_hi35xx.c驱动为示例,展示需要驱动适配者提供哪些内容来完整实现设备功能。 92e41f4b71Sopenharmony_ci 93e41f4b71Sopenharmony_ci1. 实例化驱动入口 94e41f4b71Sopenharmony_ci 95e41f4b71Sopenharmony_ci 驱动入口必须为HdfDriverEntry(在hdf_device_desc.h中定义)类型的全局变量,且moduleName要和device_info.hcs中保持一致。HDF框架会将所有加载的驱动的HdfDriverEntry对象首地址汇总,形成一个类似数组的段地址空间,方便上层调用。 96e41f4b71Sopenharmony_ci 97e41f4b71Sopenharmony_ci 一般在加载驱动时HDF会先调用Bind函数,再调用Init函数加载该驱动。当Init调用异常时,HDF框架会调用Release释放驱动资源并退出。 98e41f4b71Sopenharmony_ci 99e41f4b71Sopenharmony_ci PIN驱动入口开发参考: 100e41f4b71Sopenharmony_ci 101e41f4b71Sopenharmony_ci ```c 102e41f4b71Sopenharmony_ci static struct HdfDriverEntry g_hi35xxPinDriverEntry = { 103e41f4b71Sopenharmony_ci .moduleVersion = 1, 104e41f4b71Sopenharmony_ci .Bind = Hi35xxPinBind, // PIN不需要实现Bind,本例是一个空实现,驱动适配者可根据自身需要添加相关操作 105e41f4b71Sopenharmony_ci .Init = Hi35xxPinInit, // 挂接PIN模块Init实例化 106e41f4b71Sopenharmony_ci .Release = Hi35xxPinRelease, // 挂接PIN模块Release实例化 107e41f4b71Sopenharmony_ci .moduleName = "hi35xx_pin_driver", // 【必要且与HCS文件中里面的moduleName匹配】 108e41f4b71Sopenharmony_ci }; 109e41f4b71Sopenharmony_ci HDF_INIT(g_hi35xxPinDriverEntry); // 调用HDF_INIT将驱动入口注册到HDF框架中 110e41f4b71Sopenharmony_ci ``` 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci2. 配置属性文件 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ci 完成驱动入口注册之后,需要在device_info.hcs文件中添加deviceNode信息,deviceNode信息与驱动入口注册相关。本例以两个PIN控制器为例,如有多个器件信息,则需要在device_info.hcs文件增加对应的deviceNode信息,以及在gpio_config.hcs文件中增加对应的器件属性。器件属性值对于驱动适配者的驱动实现以及核心层PinCntlr相关成员的默认值或限制范围有密切关系,比如控制器号,控制器管脚数量、管脚等,需要在pin_config.hcs中配置器件属性。 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci 统一服务模式的特点是device_info.hcs文件中第一个设备节点必须为PIN管理器,其各项参数如表2所示: 117e41f4b71Sopenharmony_ci 118e41f4b71Sopenharmony_ci **表 2** device_info.hcs节点参数说明 119e41f4b71Sopenharmony_ci 120e41f4b71Sopenharmony_ci | 成员名 | 值 | 121e41f4b71Sopenharmony_ci | -------- | -------- | 122e41f4b71Sopenharmony_ci | policy | 驱动服务发布的策略,PIN管理器具体配置为2,表示驱动对内核态和用户态都发布服务 | 123e41f4b71Sopenharmony_ci | priority | 驱动启动优先级(0-200),值越大优先级越低。PIN管理器具体配置为8 | 124e41f4b71Sopenharmony_ci | permission | 驱动创建设备节点权限,PIN管理器具体配置为0664 | 125e41f4b71Sopenharmony_ci | moduleName | 驱动名称,PIN管理器固定为HDF_PLATFORM_PIN_MANAGER | 126e41f4b71Sopenharmony_ci | serviceName | 驱动对外发布服务的名称,PIN管理器服务名设置为HDF_PLATFORM_PIN_MANAGER | 127e41f4b71Sopenharmony_ci | deviceMatchAttr | 驱动私有数据匹配的关键字,PIN管理器没有使用,可忽略 | 128e41f4b71Sopenharmony_ci 129e41f4b71Sopenharmony_ci - device_info.hcs 配置参考: 130e41f4b71Sopenharmony_ci 131e41f4b71Sopenharmony_ci 在//vendor/hisilicon/hispark_taurus/hdf_config/device_info/device_info.hcs文件中添加deviceNode描述。 132e41f4b71Sopenharmony_ci 133e41f4b71Sopenharmony_ci ```c 134e41f4b71Sopenharmony_ci root { 135e41f4b71Sopenharmony_ci device_info { 136e41f4b71Sopenharmony_ci platform :: host { 137e41f4b71Sopenharmony_ci hostName = "platform_host"; 138e41f4b71Sopenharmony_ci priority = 50; 139e41f4b71Sopenharmony_ci device_pin :: device { 140e41f4b71Sopenharmony_ci device0 :: deviceNode { // 用于统一管理PIN并发布服务 141e41f4b71Sopenharmony_ci policy = 2; // 2:用户态可见;1:内核态可见;0:不需要发布服务。 142e41f4b71Sopenharmony_ci priority = 8; // 启动优先级 143e41f4b71Sopenharmony_ci permission = 0644; // 创建设备节点权限 144e41f4b71Sopenharmony_ci moduleName = "HDF_PLATFORM_PIN_MANAGER"; 145e41f4b71Sopenharmony_ci serviceName = "HDF_PLATFORM_PIN_MANAGER"; 146e41f4b71Sopenharmony_ci } 147e41f4b71Sopenharmony_ci device1 :: deviceNode { // 为每一个PIN控制器配置一个HDF设备节点,存在多个时必须添加,否则不用。 148e41f4b71Sopenharmony_ci policy = 0; 149e41f4b71Sopenharmony_ci priority = 10; // 驱动启动优先级 150e41f4b71Sopenharmony_ci permission = 0644; // 驱动创建设备节点权限 151e41f4b71Sopenharmony_ci moduleName = "hi35xx_pin_driver"; // 【必要】用于指定驱动名称,需要与期望的驱动Entry中的moduleName一致。 152e41f4b71Sopenharmony_ci deviceMatchAttr = "hisilicon_hi35xx_pin_0"; // 【必要】用于配置控制器私有数据,要与pin_config.hcs中对应控制器保持一致,具体的控制器信息在pin_config.hcs中。 153e41f4b71Sopenharmony_ci } 154e41f4b71Sopenharmony_ci device2 :: deviceNode { 155e41f4b71Sopenharmony_ci policy = 0; 156e41f4b71Sopenharmony_ci priority = 10; 157e41f4b71Sopenharmony_ci permission = 0644; 158e41f4b71Sopenharmony_ci moduleName = "hi35xx_pin_driver"; 159e41f4b71Sopenharmony_ci deviceMatchAttr = "hisilicon_hi35xx_pin_1"; 160e41f4b71Sopenharmony_ci } 161e41f4b71Sopenharmony_ci ...... // 如果存在多个PIN控制器时【必须】添加节点,否则不用 162e41f4b71Sopenharmony_ci } 163e41f4b71Sopenharmony_ci } 164e41f4b71Sopenharmony_ci } 165e41f4b71Sopenharmony_ci } 166e41f4b71Sopenharmony_ci ``` 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci - pin_config.hcs配置参考: 169e41f4b71Sopenharmony_ci 170e41f4b71Sopenharmony_ci 在//device/soc/hisilicon/hi3516dv300/sdk_liteos/hdf_config/pin/pin_config.hcs文件配置器件属性,其中配置参数如下: 171e41f4b71Sopenharmony_ci 172e41f4b71Sopenharmony_ci ```c 173e41f4b71Sopenharmony_ci root { 174e41f4b71Sopenharmony_ci platform { 175e41f4b71Sopenharmony_ci pin_config_hi35xx { 176e41f4b71Sopenharmony_ci template pin_controller { // 【必要】配置模板配,如果下面节点使用时继承该模板,则节点中未声明的字段会使用该模板中的默认值。 177e41f4b71Sopenharmony_ci number = 0; // 【必要】PIN控制器号 178e41f4b71Sopenharmony_ci regStartBasePhy = 0; // 【必要】寄存器物理基地址起始地址 179e41f4b71Sopenharmony_ci regSize = 0; // 【必要】寄存器位宽 180e41f4b71Sopenharmony_ci pinCount = 0; // 【必要】管脚数量 181e41f4b71Sopenharmony_ci match_attr = ""; 182e41f4b71Sopenharmony_ci template pin_desc { 183e41f4b71Sopenharmony_ci pinName = ""; // 【必要】管脚名称 184e41f4b71Sopenharmony_ci init = 0; // 【必要】寄存器默认值 185e41f4b71Sopenharmony_ci F0 = ""; // 【必要】功能0 186e41f4b71Sopenharmony_ci F1 = ""; // 功能1 187e41f4b71Sopenharmony_ci F2 = ""; // 功能2 188e41f4b71Sopenharmony_ci F3 = ""; // 功能3 189e41f4b71Sopenharmony_ci F4 = ""; // 功能4 190e41f4b71Sopenharmony_ci F5 = ""; // 功能5 191e41f4b71Sopenharmony_ci } 192e41f4b71Sopenharmony_ci } 193e41f4b71Sopenharmony_ci controller_0 :: pin_controller { 194e41f4b71Sopenharmony_ci number = 0; 195e41f4b71Sopenharmony_ci regStartBasePhy = 0x10FF0000; 196e41f4b71Sopenharmony_ci regSize = 0x48; 197e41f4b71Sopenharmony_ci pinCount = 18; 198e41f4b71Sopenharmony_ci match_attr = "hisilicon_hi35xx_pin_0"; 199e41f4b71Sopenharmony_ci T1 :: pin_desc { 200e41f4b71Sopenharmony_ci pinName = "T1"; 201e41f4b71Sopenharmony_ci init = 0x0600; 202e41f4b71Sopenharmony_ci F0 = "EMMC_CLK"; 203e41f4b71Sopenharmony_ci F1 = "SFC_CLK"; 204e41f4b71Sopenharmony_ci F2 = "SFC_BOOT_MODE"; 205e41f4b71Sopenharmony_ci } 206e41f4b71Sopenharmony_ci ...... // 对应管脚控制器下的每个管脚,按实际添加。 207e41f4b71Sopenharmony_ci } 208e41f4b71Sopenharmony_ci ...... // 每个管脚控制器对应一个控制器节点,如存在多个PIN控制器,请依次添加对应的控制器节点。 209e41f4b71Sopenharmony_ci } 210e41f4b71Sopenharmony_ci } 211e41f4b71Sopenharmony_ci } 212e41f4b71Sopenharmony_ci ``` 213e41f4b71Sopenharmony_ci 214e41f4b71Sopenharmony_ci 需要注意的是,新增pin_config.hcs配置文件后,必须在产品对应的hdf.hcs文件中将其包含如下语句所示,否则配置文件无法生效。 215e41f4b71Sopenharmony_ci 216e41f4b71Sopenharmony_ci ```c 217e41f4b71Sopenharmony_ci #include "../../../../device/soc/hisilicon/hi3516dv300/sdk_liteos/hdf_config/pin/pin_config.hcs" // 配置文件相对路径 218e41f4b71Sopenharmony_ci ``` 219e41f4b71Sopenharmony_ci 220e41f4b71Sopenharmony_ci3. 实例化PIN控制器对象 221e41f4b71Sopenharmony_ci 222e41f4b71Sopenharmony_ci 完成配置属性文件之后,下一步就是以核心层PinCntlr对象的初始化为核心,包括驱动适配者自定义结构体(传递参数和数据),实例化PinCntlr成员PinCntlrMethod(让用户可以通过接口来调用驱动底层函数),实现HdfDriverEntry成员函数(Bind、Init、Release)。 223e41f4b71Sopenharmony_ci 224e41f4b71Sopenharmony_ci - 驱动适配者自定义结构体参考 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci 从驱动的角度看,自定义结构体是参数和数据的载体,而且pin_config.hcs文件中的数值会被HDF读入并通过DeviceResourceIface来初始化结构体成员,一些重要数值也会传递给核心层对象。 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci 在Hi35xxPinCntlrInit函数中对PinCntlr成员进行初始化操作。 229e41f4b71Sopenharmony_ci 230e41f4b71Sopenharmony_ci ```c 231e41f4b71Sopenharmony_ci // 驱动适配者自定义管脚描述结构体 232e41f4b71Sopenharmony_ci struct Hi35xxPinDesc { 233e41f4b71Sopenharmony_ci const char *pinName; // 管脚名 234e41f4b71Sopenharmony_ci uint32_t init; // 初始化值 235e41f4b71Sopenharmony_ci uint32_t index; // 管脚索引 236e41f4b71Sopenharmony_ci int32_t pullType; // 管脚推拉方式 237e41f4b71Sopenharmony_ci int32_t strength; // 管脚推拉强度 238e41f4b71Sopenharmony_ci const char *func[HI35XX_PIN_FUNC_MAX]; // 管脚功能名字符串数组 239e41f4b71Sopenharmony_ci }; 240e41f4b71Sopenharmony_ci 241e41f4b71Sopenharmony_ci // 驱动适配者自定义结构体 242e41f4b71Sopenharmony_ci struct Hi35xxPinCntlr { 243e41f4b71Sopenharmony_ci struct PinCntlr cntlr; // 是核心层控制对象,具体描述见下面 244e41f4b71Sopenharmony_ci struct Hi35xxPinDesc *desc; // 驱动适配者自定义管脚描述结构体指针 245e41f4b71Sopenharmony_ci volatile unsigned char *regBase; // 寄存器映射地址 246e41f4b71Sopenharmony_ci uint16_t number; // 管脚控制器编号 247e41f4b71Sopenharmony_ci uint32_t regStartBasePhy; // 寄存器物理基地址起始地址 248e41f4b71Sopenharmony_ci uint32_t regSize; // 寄存器位宽 249e41f4b71Sopenharmony_ci uint32_t pinCount; // 管脚数量 250e41f4b71Sopenharmony_ci }; 251e41f4b71Sopenharmony_ci 252e41f4b71Sopenharmony_ci // PinCntlr是核心层控制器结构体,其中的成员在Init函数中会被赋值。 253e41f4b71Sopenharmony_ci struct PinCntlr { 254e41f4b71Sopenharmony_ci struct IDeviceIoService service; // 驱动服务 255e41f4b71Sopenharmony_ci struct HdfDeviceObject *device; // 驱动设备对象 256e41f4b71Sopenharmony_ci struct PinCntlrMethod *method; // 钩子函数 257e41f4b71Sopenharmony_ci struct DListHead node; // 链表节点 258e41f4b71Sopenharmony_ci OsalSpinlock spin; // 自旋锁 259e41f4b71Sopenharmony_ci uint16_t number; // 管脚控制器编号 260e41f4b71Sopenharmony_ci uint16_t pinCount; // 管脚数量 261e41f4b71Sopenharmony_ci struct PinDesc *pins; // 管脚资源 262e41f4b71Sopenharmony_ci void *priv; // 私有数据 263e41f4b71Sopenharmony_ci }; 264e41f4b71Sopenharmony_ci 265e41f4b71Sopenharmony_ci // PIN管脚控制器初始化 266e41f4b71Sopenharmony_ci static int32_t Hi35xxPinCntlrInit(struct HdfDeviceObject *device, struct Hi35xxPinCntlr *hi35xx) 267e41f4b71Sopenharmony_ci { 268e41f4b71Sopenharmony_ci struct DeviceResourceIface *drsOps = NULL; 269e41f4b71Sopenharmony_ci int32_t ret; 270e41f4b71Sopenharmony_ci // 从hcs文件读取管脚控制器相关属性 271e41f4b71Sopenharmony_ci drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); 272e41f4b71Sopenharmony_ci if (drsOps == NULL || drsOps->GetUint32 == NULL || drsOps->GetUint16 == NULL) { 273e41f4b71Sopenharmony_ci HDF_LOGE("%s: invalid drs ops fail!", __func__); 274e41f4b71Sopenharmony_ci return HDF_FAILURE; 275e41f4b71Sopenharmony_ci } 276e41f4b71Sopenharmony_ci ret = drsOps->GetUint16(device->property, "number", &hi35xx->number, 0); 277e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 278e41f4b71Sopenharmony_ci HDF_LOGE("%s: read number failed", __func__); 279e41f4b71Sopenharmony_ci return ret; 280e41f4b71Sopenharmony_ci } 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci if (hi35xx->number > HI35XX_PIN_MAX_NUMBER) { 283e41f4b71Sopenharmony_ci HDF_LOGE("%s: invalid number:%u", __func__, hi35xx->number); 284e41f4b71Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 285e41f4b71Sopenharmony_ci } 286e41f4b71Sopenharmony_ci ret = drsOps->GetUint32(device->property, "regStartBasePhy", &hi35xx->regStartBasePhy, 0); 287e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 288e41f4b71Sopenharmony_ci HDF_LOGE("%s: read regStartBasePhy failed", __func__); 289e41f4b71Sopenharmony_ci return ret; 290e41f4b71Sopenharmony_ci } 291e41f4b71Sopenharmony_ci ret = drsOps->GetUint32(device->property, "regSize", &hi35xx->regSize, 0); 292e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 293e41f4b71Sopenharmony_ci HDF_LOGE("%s: read regSize failed", __func__); 294e41f4b71Sopenharmony_ci return ret; 295e41f4b71Sopenharmony_ci } 296e41f4b71Sopenharmony_ci ret = drsOps->GetUint32(device->property, "pinCount", &hi35xx->pinCount, 0); 297e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 298e41f4b71Sopenharmony_ci HDF_LOGE("%s: read pinCount failed", __func__); 299e41f4b71Sopenharmony_ci return ret; 300e41f4b71Sopenharmony_ci } 301e41f4b71Sopenharmony_ci if (hi35xx->pinCount > PIN_MAX_CNT_PER_CNTLR) { 302e41f4b71Sopenharmony_ci HDF_LOGE("%s: invalid number:%u", __func__, hi35xx->number); 303e41f4b71Sopenharmony_ci return HDF_ERR_INVALID_PARAM; 304e41f4b71Sopenharmony_ci } 305e41f4b71Sopenharmony_ci // 将读取的值赋值给管脚控制器的成员,完成管脚控制器初始化。 306e41f4b71Sopenharmony_ci hi35xx->cntlr.pinCount = hi35xx->pinCount; 307e41f4b71Sopenharmony_ci hi35xx->cntlr.number = hi35xx->number; 308e41f4b71Sopenharmony_ci hi35xx->regBase = OsalIoRemap(hi35xx->regStartBasePhy, hi35xx->regSize); // 管脚控制器映射 309e41f4b71Sopenharmony_ci if (hi35xx->regBase == NULL) { 310e41f4b71Sopenharmony_ci HDF_LOGE("%s: remap Pin base failed", __func__); 311e41f4b71Sopenharmony_ci return HDF_ERR_IO; 312e41f4b71Sopenharmony_ci } 313e41f4b71Sopenharmony_ci hi35xx->desc = (struct Hi35xxPinDesc *)OsalMemCalloc(sizeof(struct Hi35xxPinDesc) * hi35xx->pinCount); 314e41f4b71Sopenharmony_ci if (hi35xx->desc == NULL) { 315e41f4b71Sopenharmony_ci HDF_LOGE("%s: memcalloc hi35xx desc failed", __func__); 316e41f4b71Sopenharmony_ci return HDF_ERR_MALLOC_FAIL; 317e41f4b71Sopenharmony_ci } 318e41f4b71Sopenharmony_ci hi35xx->cntlr.pins = (struct PinDesc *)OsalMemCalloc(sizeof(struct PinDesc) * hi35xx->pinCount); 319e41f4b71Sopenharmony_ci if (hi35xx->desc == NULL) { 320e41f4b71Sopenharmony_ci HDF_LOGE("%s: memcalloc hi35xx cntlr pins failed", __func__); 321e41f4b71Sopenharmony_ci return HDF_ERR_MALLOC_FAIL; 322e41f4b71Sopenharmony_ci } 323e41f4b71Sopenharmony_ci return HDF_SUCCESS; 324e41f4b71Sopenharmony_ci } 325e41f4b71Sopenharmony_ci ``` 326e41f4b71Sopenharmony_ci 327e41f4b71Sopenharmony_ci - PinCntlr成员钩子函数结构体PinCntlrMethod的实例化。 328e41f4b71Sopenharmony_ci 329e41f4b71Sopenharmony_ci ```c 330e41f4b71Sopenharmony_ci static struct PinCntlrMethod g_method = { 331e41f4b71Sopenharmony_ci .SetPinPull = Hi35xxPinSetPull, // 设置推拉方式 332e41f4b71Sopenharmony_ci .GetPinPull = Hi35xxPinGetPull, // 获取推拉方式 333e41f4b71Sopenharmony_ci .SetPinStrength = Hi35xxPinSetStrength, // 设置推拉强度 334e41f4b71Sopenharmony_ci .GetPinStrength = Hi35xxPinGetStrength, // 获取推拉强度 335e41f4b71Sopenharmony_ci .SetPinFunc = Hi35xxPinSetFunc, // 设置管脚功能 336e41f4b71Sopenharmony_ci .GetPinFunc = Hi35xxPinGetFunc, // 获取管脚功能 337e41f4b71Sopenharmony_ci }; 338e41f4b71Sopenharmony_ci ``` 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci - Init函数开发参考 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci 入参 343e41f4b71Sopenharmony_ci 344e41f4b71Sopenharmony_ci HdfDeviceObject:HDF框架给每一个驱动创建的设备对象,用来保存设备相关的私有数据和服务接口。 345e41f4b71Sopenharmony_ci 346e41f4b71Sopenharmony_ci 返回值: 347e41f4b71Sopenharmony_ci 348e41f4b71Sopenharmony_ci HDF_STATUS相关状态(表3为部分展示,如需使用其他状态,可参考//drivers/hdf_core/interfaces/inner_api/utils/hdf_base.h中HDF_STATUS定义)。 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ci **表 3** HDF_STATUS相关状态说明 351e41f4b71Sopenharmony_ci 352e41f4b71Sopenharmony_ci | 状态(值) | 问题描述 | 353e41f4b71Sopenharmony_ci | -------- | -------- | 354e41f4b71Sopenharmony_ci | HDF_ERR_INVALID_OBJECT | 控制器对象非法 | 355e41f4b71Sopenharmony_ci | HDF_ERR_MALLOC_FAIL | 内存分配失败 | 356e41f4b71Sopenharmony_ci | HDF_ERR_INVALID_PARAM | 参数非法 | 357e41f4b71Sopenharmony_ci | HDF_ERR_IO | I/O 错误 | 358e41f4b71Sopenharmony_ci | HDF_SUCCESS | 初始化成功 | 359e41f4b71Sopenharmony_ci | HDF_FAILURE | 初始化失败 | 360e41f4b71Sopenharmony_ci 361e41f4b71Sopenharmony_ci 函数说明: 362e41f4b71Sopenharmony_ci 363e41f4b71Sopenharmony_ci 初始化自定义结构体对象和PinCntlr成员,并通过调用核心层PinCntlrAdd函数挂载PIN控制器。 364e41f4b71Sopenharmony_ci 365e41f4b71Sopenharmony_ci ```c 366e41f4b71Sopenharmony_ci static int32_t Hi35xxPinReadFunc(struct Hi35xxPinDesc *desc, const struct DeviceResourceNode *node, struct DeviceResourceIface *drsOps) 367e41f4b71Sopenharmony_ci { 368e41f4b71Sopenharmony_ci int32_t ret; 369e41f4b71Sopenharmony_ci uint32_t funcNum = 0; 370e41f4b71Sopenharmony_ci // 从hcs中读取管脚控制器子节点管脚功能名 371e41f4b71Sopenharmony_ci ret = drsOps->GetString(node, "F0", &desc->func[funcNum], "NULL"); 372e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 373e41f4b71Sopenharmony_ci HDF_LOGE("%s: read F0 failed", __func__); 374e41f4b71Sopenharmony_ci return ret; 375e41f4b71Sopenharmony_ci } 376e41f4b71Sopenharmony_ci 377e41f4b71Sopenharmony_ci funcNum++; 378e41f4b71Sopenharmony_ci ret = drsOps->GetString(node, "F1", &desc->func[funcNum], "NULL"); 379e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 380e41f4b71Sopenharmony_ci HDF_LOGE("%s: read F1 failed", __func__); 381e41f4b71Sopenharmony_ci return ret; 382e41f4b71Sopenharmony_ci } 383e41f4b71Sopenharmony_ci 384e41f4b71Sopenharmony_ci funcNum++; 385e41f4b71Sopenharmony_ci ...... 386e41f4b71Sopenharmony_ci return HDF_SUCCESS; 387e41f4b71Sopenharmony_ci } 388e41f4b71Sopenharmony_ci 389e41f4b71Sopenharmony_ci static int32_t Hi35xxPinParsePinNode(const struct DeviceResourceNode *node, struct Hi35xxPinCntlr *hi35xx, int32_t index) 390e41f4b71Sopenharmony_ci { 391e41f4b71Sopenharmony_ci int32_t ret; 392e41f4b71Sopenharmony_ci struct DeviceResourceIface *drsOps = NULL; 393e41f4b71Sopenharmony_ci // 从hcs中读取管脚控制器子节点管脚相关属性 394e41f4b71Sopenharmony_ci drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); 395e41f4b71Sopenharmony_ci if (drsOps == NULL || drsOps->GetUint32 == NULL || drsOps->GetString == NULL) { 396e41f4b71Sopenharmony_ci HDF_LOGE("%s: invalid drs ops fail!", __func__); 397e41f4b71Sopenharmony_ci return HDF_FAILURE; 398e41f4b71Sopenharmony_ci } 399e41f4b71Sopenharmony_ci ret = drsOps->GetString(node, "pinName", &hi35xx->desc[index].pinName, "NULL"); 400e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 401e41f4b71Sopenharmony_ci HDF_LOGE("%s: read pinName failed", __func__); 402e41f4b71Sopenharmony_ci return ret; 403e41f4b71Sopenharmony_ci } 404e41f4b71Sopenharmony_ci ... 405e41f4b71Sopenharmony_ci ret = Hi35xxPinReadFunc(&hi35xx->desc[index], node, drsOps); 406e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 407e41f4b71Sopenharmony_ci HDF_LOGE("%s:Pin read Func failed", __func__); 408e41f4b71Sopenharmony_ci return ret; 409e41f4b71Sopenharmony_ci } 410e41f4b71Sopenharmony_ci hi35xx->cntlr.pins[index].pinName = hi35xx->desc[index].pinName; 411e41f4b71Sopenharmony_ci hi35xx->cntlr.pins[index].priv = (void *)node; 412e41f4b71Sopenharmony_ci ...... 413e41f4b71Sopenharmony_ci return HDF_SUCCESS; 414e41f4b71Sopenharmony_ci } 415e41f4b71Sopenharmony_ci 416e41f4b71Sopenharmony_ci static int32_t Hi35xxPinInit(struct HdfDeviceObject *device) 417e41f4b71Sopenharmony_ci { 418e41f4b71Sopenharmony_ci ...... 419e41f4b71Sopenharmony_ci struct Hi35xxPinCntlr *hi35xx = NULL; 420e41f4b71Sopenharmony_ci ...... 421e41f4b71Sopenharmony_ci ret = Hi35xxPinCntlrInit(device, hi35xx); // 管脚控制器初始化 422e41f4b71Sopenharmony_ci ...... 423e41f4b71Sopenharmony_ci DEV_RES_NODE_FOR_EACH_CHILD_NODE(device->property, childNode) { // 遍历管脚控制器的每个子节点 424e41f4b71Sopenharmony_ci ret = Hi35xxPinParsePinNode(childNode, hi35xx, index); // 解析子节点 425e41f4b71Sopenharmony_ci ...... 426e41f4b71Sopenharmony_ci } 427e41f4b71Sopenharmony_ci 428e41f4b71Sopenharmony_ci hi35xx->cntlr.method = &g_method; // PinCntlrMethod实例化实例化对象的挂载 429e41f4b71Sopenharmony_ci ret = PinCntlrAdd(&hi35xx->cntlr); // 添加控制器 430e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 431e41f4b71Sopenharmony_ci HDF_LOGE("%s: add Pin cntlr: failed", __func__); 432e41f4b71Sopenharmony_ci ret = HDF_FAILURE; 433e41f4b71Sopenharmony_ci } 434e41f4b71Sopenharmony_ci return HDF_SUCCESS; 435e41f4b71Sopenharmony_ci } 436e41f4b71Sopenharmony_ci ``` 437e41f4b71Sopenharmony_ci 438e41f4b71Sopenharmony_ci - Release函数开发参考 439e41f4b71Sopenharmony_ci 440e41f4b71Sopenharmony_ci 入参: 441e41f4b71Sopenharmony_ci 442e41f4b71Sopenharmony_ci HdfDeviceObject:HDF框架给每一个驱动创建的设备对象,用来保存设备相关的私有数据和服务接口。 443e41f4b71Sopenharmony_ci 444e41f4b71Sopenharmony_ci 返回值: 445e41f4b71Sopenharmony_ci 446e41f4b71Sopenharmony_ci 无。 447e41f4b71Sopenharmony_ci 448e41f4b71Sopenharmony_ci 函数说明: 449e41f4b71Sopenharmony_ci 450e41f4b71Sopenharmony_ci 释放内存和删除控制器,该函数需要在驱动入口结构体中赋值给Release接口。当HDF框架调用Init函数初始化驱动失败时,可以调用Release释放驱动资源。 451e41f4b71Sopenharmony_ci 452e41f4b71Sopenharmony_ci ```c 453e41f4b71Sopenharmony_ci static void Hi35xxPinRelease(struct HdfDeviceObject *device) 454e41f4b71Sopenharmony_ci { 455e41f4b71Sopenharmony_ci int32_t ret; 456e41f4b71Sopenharmony_ci uint16_t number; 457e41f4b71Sopenharmony_ci struct PinCntlr *cntlr = NULL; 458e41f4b71Sopenharmony_ci struct Hi35xxPinCntlr *hi35xx = NULL; 459e41f4b71Sopenharmony_ci struct DeviceResourceIface *drsOps = NULL; 460e41f4b71Sopenharmony_ci 461e41f4b71Sopenharmony_ci if (device == NULL || device->property == NULL) { 462e41f4b71Sopenharmony_ci HDF_LOGE("%s: device or property is null", __func__); 463e41f4b71Sopenharmony_ci return; 464e41f4b71Sopenharmony_ci } 465e41f4b71Sopenharmony_ci // 从hcs文件中读取管脚控制器编号 466e41f4b71Sopenharmony_ci drsOps = DeviceResourceGetIfaceInstance(HDF_CONFIG_SOURCE); 467e41f4b71Sopenharmony_ci if (drsOps == NULL || drsOps->GetUint32 == NULL || drsOps->GetString == NULL) { 468e41f4b71Sopenharmony_ci HDF_LOGE("%s: invalid drs ops", __func__); 469e41f4b71Sopenharmony_ci return; 470e41f4b71Sopenharmony_ci } 471e41f4b71Sopenharmony_ci ret = drsOps->GetUint16(device->property, "number", &number, 0); 472e41f4b71Sopenharmony_ci if (ret != HDF_SUCCESS) { 473e41f4b71Sopenharmony_ci HDF_LOGE("%s: read cntlr number failed", __func__); 474e41f4b71Sopenharmony_ci return; 475e41f4b71Sopenharmony_ci } 476e41f4b71Sopenharmony_ci 477e41f4b71Sopenharmony_ci cntlr = PinCntlrGetByNumber(number); // 通过管脚控制器编号获取管脚控制器 478e41f4b71Sopenharmony_ci PinCntlrRemove(cntlr); 479e41f4b71Sopenharmony_ci hi35xx = (struct Hi35xxPinCntlr *)cntlr; 480e41f4b71Sopenharmony_ci if (hi35xx != NULL) { 481e41f4b71Sopenharmony_ci if (hi35xx->regBase != NULL) { 482e41f4b71Sopenharmony_ci OsalIoUnmap((void *)hi35xx->regBase); 483e41f4b71Sopenharmony_ci } 484e41f4b71Sopenharmony_ci OsalMemFree(hi35xx); 485e41f4b71Sopenharmony_ci } 486e41f4b71Sopenharmony_ci } 487e41f4b71Sopenharmony_ci ``` 488e41f4b71Sopenharmony_ci 489e41f4b71Sopenharmony_ci4. 驱动调试 490e41f4b71Sopenharmony_ci 491e41f4b71Sopenharmony_ci 【可选】针对新增驱动程序,建议验证驱动基本功能,例如挂载后的信息反馈,PIN管脚设置推拉方式、推拉强度等。 492