树莓派4B编写HDF驱动示例

系统
前段时间已经在树莓派4B成功加入了HDF驱动框架,帖子链接。得用HDF编写个自己的驱动来测试下移植的效果。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

前言

前段时间已经在树莓派4B成功加入了HDF驱动框架,帖子链接。得用HDF编写个自己的驱动来测试下移植的效果。不说了代码最实在,看代码吧。

树莓派4B编写HDF驱动示例-鸿蒙HarmonyOS技术社区

1.内核态驱动代码

一个简单的测试驱动,可以认为是串口驱动。因为在linux内核下,所以驱动放置在linux\platform\uart目录下。

drivers\adapter\khdf\linux\platform\uart\my_hdf_uart.c

  1. #include "hdf_device_desc.h"    // HDF框架对驱动开发相关能力接口的头文件 
  2. #include "hdf_log.h"            // HDF 框架提供的日志接口头文件 
  3.  
  4. #define HDF_LOG_TAG "my_hdf_uart"   // 打印日志所包含的标签,如果不定义则用默认定义的HDF_TAG标签 
  5.  
  6. // 【3.1驱动消息机制管理】Dispatch是用来处理用户态发下来的消息 
  7. int32_t MyUartDriverDispatch(struct HdfDeviceIoClient *client, int cmdId, struct HdfSBuf *data, struct HdfSBuf *reply) 
  8.     HDF_LOGE("%s::enter", __func__); 
  9.     HDF_LOGE("get cmdId::%d",cmdId); 
  10.     return HDF_SUCCESS; 
  11.  
  12. // 【1.驱动开发】驱动对外提供的服务能力,将相关的服务接口绑定到HDF框架 
  13. int32_t MyHdfUartDriverBind(struct HdfDeviceObject *deviceObject) 
  14.     HDF_LOGE("%s::enter", __func__); 
  15.  
  16.     // 【2.驱动服务管理】deviceObject为HDF框架给每一个驱动创建的设备对象,用来保存设备相关的私有数据和服务接口 
  17.     if (deviceObject == NULL) { 
  18.         HDF_LOGE("My Uart device object is null!"); 
  19.         return HDF_FAILURE; 
  20.     } 
  21.     static struct IDeviceIoService testService = { 
  22.         .Dispatch = MyUartDriverDispatch,   // 【3.2驱动消息机制管理】在服务实现过程中,实现服务基类成员IDeviceIoService中的Dispatch方法 
  23.         .Open = NULL,                       // 【2.驱动服务管理】驱动提供的其他服务。 
  24.         .Release = NULL
  25.         // .ServiceA = SampleDriverServiceA, 
  26.     }; 
  27.     deviceObject->service = &testService; 
  28.  
  29.     return HDF_SUCCESS; 
  30.  
  31. // 【1.驱动开发】驱动自身业务初始的接口 
  32. int32_t MyHdfUartDriverInit(struct HdfDeviceObject *deviceObject) 
  33.     HDF_LOGE("%s::enter", __func__);  
  34.     HDF_LOGD("Uart driver bind success");        
  35.     return 0; 
  36. static int32_t MyUartParseAndInit(struct HdfDeviceObject *device, const struct DeviceResourceNode *node) 
  37.     HDF_LOGE("%s::enter", __func__); 
  38.     return 0; 
  39.  
  40.  
  41. // 【1.驱动开发】驱动资源释放的接口 
  42. void MyHdfUartDriverRelease(struct HdfDeviceObject *deviceObject) 
  43.     HDF_LOGE("%s::enter", __func__); 
  44.     return
  45.  
  46. // 【1.驱动开发】定义驱动入口的对象,必须为HdfDriverEntry(在hdf_device_desc.h中定义)类型的全局变量 
  47. struct HdfDriverEntry g_myhdfUartDriverEntry = { 
  48.     .moduleVersion = 1, 
  49.     .moduleName = "my_hdf_uart"
  50.     .Bind = MyHdfUartDriverBind, 
  51.     .Init = MyHdfUartDriverInit, 
  52.     .Release = MyHdfUartDriverRelease, 
  53. }; 
  54.  
  55. // 【1.驱动开发】调用HDF_INIT将驱动入口注册到HDF框架中,在加载驱动时HDF框架会先调用Bind函数,再调用Init函数加载该驱动,当Init调用异常时,HDF框架会调用Release释放驱动资源并退出。 
  56. HDF_INIT(g_myhdfUartDriverEntry); 

drivers\adapter\khdf\linux\platform\uart\Makefile:将驱动加入内核编译

  1. - uart_adapter.o 
  2.  
  3. + uart_adapter.o \ 
  4. + my_hdf_uart.o 

2.添加驱动配置文件

vendor\raspberrypi\RPI4B\hdf_config\khdf\device_info\device_info.hcs

  1. # device_uart :: device 下添加 
  2.  
  3. device2 :: deviceNode { 
  4.     policy = 2; 
  5.     permission = 0644; 
  6.     priority = 40; 
  7.     moduleName = "my_hdf_uart"
  8.     serviceName = "my_hdf_uart_service"

3.用户态HDF驱动交互验证

applications\standard\app\myuarttest.c:用户态主程序,主要代码已经添加注释了。

  1. #include <utils/hdf_log.h> 
  2. #include <core/hdf_io_service_if.h> 
  3. #include "hdf_sbuf.h" 
  4.  
  5. #define HDF_LOG_TAG "my_hdf_uart" 
  6. #define SAMPLE_SERVICE_NAME "my_hdf_uart_service" 
  7.  
  8. #define SAMPLE_WRITE_READ 1001   // 【驱动消息机制管理】读写操作码,驱动定义消息处理函数中的cmd类型 
  9.  
  10. int main() 
  11.     HDF_LOGE("%s::enter", __func__); 
  12.     int ret = 0; 
  13.  
  14.     // 用户态获取驱动的服务,获取该服务之后通过服务中的Dispatch方法向驱动发送消息。 
  15.     struct HdfIoService *serv = HdfIoServiceBind(SAMPLE_SERVICE_NAME);  // 【3驱动消息机制管理】用户态(通过服务名)获取服务接口 
  16.     if (serv == NULL) { 
  17.         HDF_LOGE("fail to get service %s", SAMPLE_SERVICE_NAME); 
  18.         return HDF_FAILURE; 
  19.     } 
  20.      
  21.     char *sendData = ""
  22.     struct HdfSBuf *data = HdfSBufObtainDefaultSize();      // 存放要发送的数据 
  23.     if (!HdfSbufWriteString(data, sendData)) {              // 发送的内容赋值 
  24.         HDF_LOGE("fail to write sbuf"); 
  25.         ret = HDF_FAILURE; 
  26.     } 
  27.  
  28.     struct HdfSBuf *reply = HdfSBufObtainDefaultSize();                                         // 存放返回的数据 
  29.     ret = serv->dispatcher->Dispatch(&serv->object, SAMPLE_WRITE_READ, data, reply);        // 发送消息到驱动 
  30.     if (ret != HDF_SUCCESS) { 
  31.         HDF_LOGE("fail to send service call"); 
  32.     } 
  33.  
  34.     HdfIoServiceRecycle(serv); 
  35.     return HDF_SUCCESS; 

applications\standard\app\BUILD.gn:编写构建脚本

  1. import("//build/ohos.gni"
  2. import("//drivers/adapter/uhdf2/uhdf.gni"
  3.  
  4. ohos_executable("myuarttest") { 
  5.   sources = [ 
  6.     "myuarttest.c" 
  7.   ] 
  8.   include_dirs = [     
  9.     "//drivers/framework/include",                                  # <utils/hdf_log.h> <core/hdf_io_service_if.h> 
  10.     "//drivers/adapter/uhdf2/osal/include",                         # hdf_log_adapter.h 
  11.     "//base/hiviewdfx/hilog/interfaces/native/innerkits/include",   # <hilog/log.h> 
  12.     "//drivers/framework/ability/sbuf/include",                     # hdf_sbuf.h 
  13.     "//drivers/framework/include/utils",                            # hdf_base.h 
  14.   ] 
  15.   deps = [ 
  16.     "//drivers/adapter/uhdf2/osal:libhdf_utils",                    # hdf_log_adapter.h 
  17.     "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog"   # <hilog/log.h> 
  18.   ] 
  19.   subsystem_name = "applications" 
  20.   part_name = "prebuilt_hap" 

 applications\standard\hap\ohos.build:最后将app加入编译框架

  1. "//applications/standard/app:myuarttest"

4.测试

最后执行用户态的myuarttest程序,就可以测试驱动是否添加成功了。

树莓派4B编写HDF驱动示例-鸿蒙HarmonyOS技术社区

用户态只需要发送cmdId比如1001,然后内核驱动程序根据cmdID执行相应的操作即可。

想了解更多内容,请访问:

51CTO和华为官方合作共建的鸿蒙技术社区

https://harmonyos.51cto.com

 

责任编辑:jianghua 来源: 鸿蒙社区
相关推荐

2021-11-23 15:35:05

鸿蒙HarmonyOS应用

2021-12-31 10:00:30

鸿蒙HarmonyOS应用

2021-12-09 10:11:04

鸿蒙HarmonyOS应用

2022-06-21 11:02:27

系统移植鸿蒙

2019-03-31 08:00:02

树莓派更新树莓派 Linux

2019-03-24 20:30:18

树莓派Linux

2019-03-12 18:33:57

树莓派Linux

2021-12-22 15:30:24

树莓派Android开发者

2021-09-26 10:11:14

Ubuntu树莓派

2023-03-23 16:02:07

树莓派4GPU调试

2022-08-08 19:35:37

HDF驱动开发鸿蒙

2021-11-17 16:20:49

Linux 系统 树莓派

2019-03-23 19:33:14

树莓派Linux操作系统

2023-08-18 14:34:09

HDF驱动框架

2022-05-16 11:30:39

openEuler树莓派鸿蒙

2022-02-11 09:24:05

树莓派OpenWrt固定IP服务

2021-03-29 21:24:40

树莓派Linux

2021-07-25 10:30:04

树莓派Linux

2018-10-12 11:20:20

树莓派NAS Linux

2020-10-24 20:32:01

Ubuntu树莓派Linux
点赞
收藏

51CTO技术栈公众号