当前位置: 首页 > 科技观察

树莓派4BHDF驱动编写实例

时间:2023-03-22 15:05:12 科技观察

更多内容请访问:与华为官方HDF驱动框架合作建立的鸿蒙技术社区https://harmonyos.51cto.com,贴出链接。你得自己用HDF写驱动来测试移植的效果。不说代码是最现实的,看代码就知道了。1、内核态驱动代码是一个简单的测试驱动,可以认为是一个串口驱动。因为是linux内核下,驱动放在linux\platform\uart目录下。drivers\adapter\khdf\linux\platform\uart\my_hdf_uart.c#include"hdf_device_desc.h"//驱动开发相关能力接口的HDF框架头文件#include"hdf_log.h"//HDF框架提供的日志接口头文件#defineHDF_LOG_TAG"my_hdf_uart"//打印log中包含的tag,如果没有定义,则使用默认的HDF_TAGtag//【3.1Driver消息机制管理】Dispatch用于处理用户态发送的消息int32_tMyUartDriverDispatch(structHdfDeviceIoClient*client,intcmdId,structHdfSBuf*data,structHdfSBuf*reply){HDF_LOGE("%s::enter",__func__);HDF_LOGE("getcmdId::%d",cmdId);returnHDF_SUCCESS;}//【1.驱动开发】驱动对外提供的服务能力,将相关服务接口绑定到HDF框架int32_tMyHdfUartDriverBind(structHdfDeviceObject*deviceObject){HDF_LOGE("%s::enter",__func__);//【2.驱动服务管理】deviceObject是HDF框架为每个驱动创建的设备对象,用于保存设备相关的私有数据和服务接口.Dispatch=MyUartDriverDispatch,//【3.2驱动消息机制管理】在服务实现过程中,实现服务基类成员IDeviceIoService中的Dispatch方法。Open=NULL,//【2.司机服务管理】司机提供的其他服务。.Release=NULL,//.ServiceA=SampleDriverServiceA,};deviceObject->service=&testService;returnHDF_SUCCESS;}//【1.驱动开发】Int32_tMyHdfUartDriverInit(structHdfDeviceObject*deviceObject){HDF_LOGE("%s::enter",__func__);HDF_LOGD("Uartdriverbindsuccess");return0;}staticint32_tMyUartParseAndInit(structHdfDeviceObject*device,conststructDeviceResourceNode*node){HDF_LOGE("%s::enter",__func__);return0;}//【1.驱动开发】驱动资源释放接口voidMyHdfUartDriverRelease(structHdfDeviceObject*deviceObject){HDF_LOGE("%s::enter",__func__);return;}//[1.驱动开发】定义驱动入口的对象必须是HdfDriverEntry(在hdf_device_desc.h中定义)类型全局变量structHdfDriverEntryg_myhdfUartDriverEntry={.moduleVersion=1,.moduleName="my_hdf_uart",.Bind=MyHdfUartDriverBind,.Init=MyHdfUartDriverInit,.Release=MyHdfUartDriverRelease,};//【1.驱动开发】调用HDF_INIT将驱动入口注册到HDF框架中。在加载驱动时,HDF框架会先调用Bind函数,然后调用Init函数加载驱动。当Init调用异常时,HDF框架会调用Release释放驱动资源并退出。HDF_INIT(g_myhdfUartDriverEntry);drivers\adapter\khdf\linux\platform\uart\Makefile:添加驱动到内核编译-uart_adapter.o+uart_adapter.o\+my_hdf_uart.o2。添加驱动配置文件vendor\raspberrypi\RPI4B\hdf_config\khdf\device_info\device_info.hcs#device_uart::deviceadddevice2::deviceNode{policy=2;permission=0644;priority=40;moduleName="my_hdf_uart";serviceName="my_hdf_uart_service";}3.用户态HDF驱动交互验证applications\standard\app\myuarttest.c:用户态主程序,主要代码已注释。#include#include#include"hdf_sbuf.h"#defineHDF_LOG_TAG"my_hdf_uart"#defineSAMPLE_SERVICE_NAME"my_hdf_uart_service"#defineSAMPLE_WRITE_READ1001消息管理]/[drivercode驱动中的cmd类型definitionmessageprocessingfunctionintmain(){HDF_LOGE("%s::enter",__func__);intret=0;//用户态获取driver的service,获取service后,通过service发送serviceDispatch方法驱动程序发送一条消息。structHdfIoService*serv=HdfIoServiceBind(SAMPLE_SERVICE_NAME);//【3-驱动消息机制管理】获取用户态服务接口(通过服务名)if(serv==NULL){HDF_LOGE("failtogetservice%s",SAMPLE_SERVICE_NAME);returnHDF_FAILURE;}char*sendData="";structHdfSBuf*data=HdfSBufObtainDefaultSize();//存储要发送的数据if(!HdfSbufWriteString(data,sendData)){//AssignHDF_LOGE("failtowritesbuf");ret=HDF_FAILURE;}structHdfSBuf*reply=HdfSBufObtainDefaultSize();//存储返回的数据ret=serv->dispatcher->Dispatch(&serv->object,SAMPLE_WRITE_READ,data,reply);//向驱动发送消息if(ret!=HDF_SUCCESS){HDF_LOGE("failtosendservicecall");}HdfIoServiceRecycle(serv);returnHDF_SUCCESS;}applications\standard\app\BUILD.gn:编写构建脚本import("//build/ohos.gni")import("//drivers/adapter/uhdf2/uhdf.gni")ohos_executable("myuarttest"){sources=["myuarttest.c"]include_dirs=["//drivers/framework/include",#"//驱动vers/adapter/uhdf2/osal/include",#hdf_log_adapter.h"//base/hiviewdfx/hilog/interfaces/native/innerkits/include",#"//drivers/framework/ability/sbuf/include",#hdf_sbuf.h"//drivers/framework/include/utils",#hdf_base.h]deps=["//drivers/adapter/uhdf2/osal:libhdf_utils",#hdf_log_adapter.h"//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog"#]subsystem_name="applications"part_name="prebuilt_hap"}applications\standard\hap\ohos.build:最后将app添加到compilationframework+"//applications/standard/app:myuarttest",4.测试完成后,在用户态执行myuarttest程序,测试驱动是否添加成功,用户态只需要发送cmdId比如1001,然后内核驱动根据cmdID执行相应的命令,运行即可。更多内容请访问:https://harmonyos.51cto.com,与华为官方共建的鸿蒙技术社区