📚鸿蒙开发往期学习笔录📌:

📌 鸿蒙(OpenHarmony)南向开发保姆级知识点汇总~
📌 鸿蒙应用开发与鸿蒙系统开发哪个更有前景?
📌 嵌入式开发适不适合做鸿蒙南向开发?看完这篇你就了解了~
📌 对于大前端开发来说,转鸿蒙开发究竟是福还是祸?
📌 鸿蒙岗位需求突增!移动端、PC端、IoT到底该怎么选?
📌 记录一场鸿蒙开发岗位面试经历~
📌 持续更新中……


器件驱动移植

本章节讲解如何移植各类器件驱动。

LCD 驱动移植

移植 LCD 驱动的主要工作是编写一个驱动,在驱动中生成模型的实例,并完成注册。
这些 LCD 的驱动被放置在源码目录 //drivers/hdf_core/framework/model/display/driver/panel 中。

  1. 创建 Panel 驱动
    创建 HDF 驱动,在驱动初始化中调用 RegisterPanel 接口注册模型实例。如:
    int32_t LCDxxEntryInit(struct HdfDeviceObject *object)
    {
        struct PanelData *panel = CreateYourPanel();
        // 注册模型实例
        if (RegisterPanel(panel) != HDF_SUCCESS) {
            HDF_LOGE("%s: RegisterPanel failed", __func__);
            return HDF_FAILURE;
        }
        return HDF_SUCCESS;
    }
    struct HdfDriverEntry g_xxxxDevEntry = {
        .moduleVersion = 1,
        .moduleName = "LCD_XXXX",
        .Init = LCDxxEntryInit,
    };
    HDF_INIT(g_xxxxDevEntry);
  1. 配置加载 panel 驱动
    产品的所有设备信息被定义在源码文件 //vendor/vendor_name/product_name/config/device_info/device_info.hcs 中。修改该文件,在 display 的 host 中,名为 device_lcd 的 device 中增加配置。

注意:
moduleName 要与 panel 驱动中的 moduleName 相同。

    root {
        ...
        display :: host {
            device_lcd :: device {
                    deviceN :: deviceNode {
                        policy = 0;
                        priority = 100;
                        preload = 2;
                        moduleName = "LCD_XXXX";
                    }
            }
        }
    }

TP 驱动移植

本节描述如何移植触摸屏驱动。触摸屏的器件驱动被放置在源码目录 //drivers/hdf_core/framework/model/input/driver/touchscreen 中。 移植触摸屏驱动主要工作是向系统注册 ChipDevice 模型实例。

详细的驱动开发指导。

  1. 创建触摸屏器件驱动

在上述 touchscreen 目录中创建名为 touch_ic_name.c 的文件。编写如下内容

    #include "hdf_touch.h"
    static int32_t HdfXXXXChipInit(struct HdfDeviceObject *device)
    {
        ChipDevice *tpImpl = CreateXXXXTpImpl();
        if(RegisterChipDevice(tpImpl) != HDF_SUCCESS) { // 注册ChipDevice模型
            ReleaseXXXXTpImpl(tpImpl);
            return HDF_FAILURE;
        }
        return HDF_SUCCESS;
    }
    struct HdfDriverEntry g_touchXXXXChipEntry = {
        .moduleVersion = 1,
        .moduleName = "HDF_TOUCH_XXXX", // 注意这里的moduleName要与后续的配置完全一致
        .Init = HdfXXXXChipInit,
    };
    HDF_INIT(g_touchXXXXChipEntry);

其中 ChipDevice 中要实现如下方法:

方法 实现说明
int32_t (*Init)(ChipDevice *device) 实现器件初始化
int32_t (*Detect)(ChipDevice *device) 实现器件探测
int32_t (*Suspend)(ChipDevice *device) 实现器件休眠
int32_t (*Resume)(ChipDevice *device) 实现器件唤醒
int32_t (*DataHandle)(ChipDevice *device) 需要实现从器件读取数据,将触摸点数据填写入 device->driver->frameData 中
int32_t (*UpdateFirmware)(ChipDevice *device) 实现固件升级
  1. 配置产品,加载器件驱动

产品的所有设备信息被定义在源码文件 //vendor/vendor_name/product_name/config/device_info/device_info.hcs 中。修改该文件,在名为 input 的 host 中,名为 device_touch_chip 的 device 中增加配置。

  1. 说明:moduleName 要与触摸屏驱动中的 moduleName 相同。
    deviceN :: deviceNode {
        policy = 0;
        priority = 130;
        preload = 0;
        permission = 0660;
        moduleName = "HDF_TOUCH_XXXX";
        deviceMatchAttr = "touch_XXXX_configs";
    }

WLAN 驱动移植

WLAN 驱动分为两部分,一部分负责管理 WLAN 设备,另一个部分负责处理 WLAN 流量。
图 1 OpenHarmony WLAN 结构示意图

如图 1,左半部分负责管理 WLAN 设备,右半部分负责 WLAN 流量。HDF WLAN 分别为这两部分做了抽象,驱动的移植过程可以看做分别实现这两部分所需接口。这些接口有:

接口 定义头文件 接口说明
HdfChipDriverFactory drivers\hdf_core\framework\include\wifi\hdf_wlan_chipdriver_manager.h ChipDriver 的 Factory,用于支持一个芯片多个 WLAN 端口
HdfChipDriver drivers\hdf_core\framework\include\wifi\wifi_module.h 每个 WLAN 端口对应一个 HdfChipDriver,用来管理一个特定端口
NetDeviceInterFace drivers\hdf_core\framework\include\wifi\net_device.h 与协议栈之间的接口,如发送数据、设置网络接口状态等

具体的移植步骤如下:

  1. 创建 HDF WLAN 芯片驱动

在目录 /device/vendor_name/peripheral/wifi/chip_name/ 创建文件 hdf_wlan_chip_name.c。内容模板如下:

    static int32_t HdfWlanXXXChipDriverInit(struct HdfDeviceObject *device) {
        static struct HdfChipDriverFactory factory = CreateChipDriverFactory(); // 需要移植者实现的方法
        struct HdfChipDriverManager *driverMgr = HdfWlanGetChipDriverMgr();
        if (driverMgr->RegChipDriver(&factory) != HDF_SUCCESS) { // 注册驱动工厂
            HDF_LOGE("%s fail: driverMgr is NULL!", __func__);
            return HDF_FAILURE;
        }
        return HDF_SUCCESS;
    }
    struct HdfDriverEntry g_hdfXXXChipEntry = {
        .moduleVersion = 1,
        .Init = HdfWlanXXXChipDriverInit,
        .Release = HdfWlanXXXChipRelease,
        .moduleName = "HDF_WIFI_CHIP_XXX" // 注意:这个名字要与配置一致
    };
    HDF_INIT(g_hdfXXXChipEntry);

在上述代码的 CreateChipDriverFactory 方法中,需要创建一个 HdfChipDriverFactory 类型的对象。该对象提供如下方法:

接口 说明
const char*driverName 当前 driverName
int32_t (*InitChip)(struct HdfWlanDevice *device) 初始化芯片
int32_t (*DeinitChip)(struct HdfWlanDevice *device) 去初始化芯片
void (*ReleaseFactory)(struct HdfChipDriverFactory *factory) 释放 HdfChipDriverFactory 对象
struct HdfChipDriver*(*Build)(struct HdfWlanDevice *device, uint8_t ifIndex) 创建一个 HdfChipDriver;输入参数中,device 是设备信息,ifIndex 是当前创建的接口在这个芯片中的序号
void (*Release)(struct HdfChipDriver *chipDriver) 释放 chipDriver
uint8_t (*GetMaxIFCount)(struct HdfChipDriverFactory *factory) 获取当前芯片支持的最大接口数

其中 Build 方法负责创建一个管理指定网络接口的对象 HdfChipDriver。该对象需要提供方法:

  1. 编写配置文件描述驱动支持的芯片

在产品配置目录下创建芯片的配置文件,保存至源码路径 //vendor/vendor_name/product_name/config/wifi/wlan_chip_chip_name.hcs

该文件模板如下:

    root {
        wlan_config {
            chip_name :& chipList {
                chip_name :: chipInst {
                    match_attr = "hdf_wlan_chips_chip_name"; /* 这是配置匹配属性,用于提供驱动的配置根 */
                    driverName = "driverName"; /* 需要与HdfChipDriverFactory中的driverName相同*/
                    sdio {
                        vendorId = 0xXXXX; /* your vendor id */
                        deviceId = [0xXXXX]; /*your supported devices */
                    }
                }
            }
        }
    }

说明:

路径和文件中的 vendor_name、product_name、chip_name 请替换成实际名称。

vendorId 和 deviceId 需要根据实际芯片的识别码进行填写。

  1. 编写配置文件,加载驱动
    产品的所有设备信息被定义在源码文件 //vendor/vendor_name/product_name/config/device_info/device_info.hcs 中。修改该文件,在名为 network 的 host 中,名为 device_wlan_chips 的 device 中增加配置。模板如下:
    deviceN :: deviceNode {
        policy = 0;
        preload = 2;
        moduleName = "HDF_WLAN_CHIPS";
        deviceMatchAttr = "hdf_wlan_chips_chip_name";
        serviceName = "driverName";
    }

说明:
moduleName 要与 HDF WLAN 芯片驱动中的 moduleName 相同。

  1. 修改 Kconfig 文件,让移植的 WLAN 模组出现再内核配置中

device/vendor_name/drivers/Kconfig 中增加配置菜单,模板如下

    config DRIVERS_HDF_WIFI_chip_name
        bool "Enable chip_name Host driver"
        default n
        depends on DRIVERS_HDF_WLAN   help
          Answer Y to enable chip_name Host driver.

说明:
请替换模板中的 chip_name 为实际的芯片名称。

  1. 修改构建脚本,让驱动参与内核构建

在源码文件 //device/vendor_name/drivers/lite.mk 末尾追加如下内容。

    ifeq ($(LOSCFG_DRIVERS_HDF_WIFI_chip_name), y)
        # 构建完成要链接一个叫hdf_wlan_chipdriver_chip_name的对象,建议按这个命名,防止冲突
        LITEOS_BASELIB += -lhdf_wlan_chipdriver_chip_name
        # 增加构建目录gpio
        LIB_SUBDIRS    += ../peripheral/wifi/chip_name
    endif

说明:
请替换模板中的 chip_name 为实际的芯片名称。

Logo

讨论HarmonyOS开发技术,专注于API与组件、DevEco Studio、测试、元服务和应用上架分发等。

更多推荐