更多内容请访问:与华为官方共建的鸿蒙技术社区https://harmonyos.51cto.com的老规矩还是希望能达到以下的效果:从本文开头的前言,会记录一条带动小白的成长之路。当你已经有了成熟的驱动开发经验,那么你可以直接跳过,这篇文章对你没用。作为一个没有接触过任何驱动开发的小白,如果想搞清楚openharmony的HDF驱动框架,大概是自己整理了一下。如有不妥,请留言指出。要彻底理解HDF框架,我觉得第一步肯定是要理解linux驱动,因为linux内核被广泛使用,不管是android,ios,还是openharmony。因为linux内核中用户态和内核态最基本的概念在各种驱动开发中存在的最多。那么有了以上的认识,如题所示,工欲善其事必先利其器。这个设备是linux驱动开发首先要了解的。所以这篇文章就是讲解linux驱动开发。虽然本文是介绍linux驱动的开发,但还是先来了解下HDF的概念吧。我们只需要知道这个概念,因为我们现在的工作就是针对这个概念。那么:HDF驱动框架是什么?HDF(HardwareDriverFoundation)驱动框架为驱动开发者提供驱动框架能力,包括驱动加载、驱动服务管理和驱动消息机制。旨在构建统一的驱动架构平台,为驱动开发者提供更加精准高效的开发环境,力求实现一次开发,多系统部署。什么是驱动程序开发?这个看似无关紧要的问题却很重要。必须从这一步开始弄清楚,如下图:从上图可以看出,单片机的裸机程序可以直接控制硬件,而在linux中没有这个技巧在应用程序中工作。Linux必须通过驱动程序来控制运行的硬件。好的,让我们现在知道这么多。更细化的问题,比如Linux为什么要这样做,这些问题我们暂时保留,一口一个吃。总体架构图1.应用程序编写应用程序的目的主要是测试驱动,测试方法是调用glibc的打开、读取、写入函数。参见下面的代码:#include#include#include#include#include#includeintmain(intargc,char**argv){intfd;charbuf[1024];intlen;intret;if(argc<2){printf("Usage:%s-w\n",argv[0]);printf("%s-r\n",argv[0]);return-1;}fd=open("/dev/hello_linux_dri",O_RDWR);if(fd==-1){printf("cannotopenfile/dev/hello_linux_dri\n");return-1;}printf("open/dev/hello_linux_drisuccess\n");if((0==strcmp(argv[1],"-w"))&&(argc==3)){len=strlen(argv[2])+1;len=len<1024?len:1024;ret=write(fd,argv[2],len);printf("writedriver:%d\n",ret);}else{len=read(fd,buf,1024);printf("readdriver:%d\n",len);buf[1023]='\0';printf("APPread:%s\n",buf);}关闭(fd);返回0;}2。驱动整体逻辑驱动属于模块写法:预先注册自己的函数任务,为以后的某个请求服务,然后其初始化函数会立即结束2.1环境确认因为驱动是内核头文件,调用时,首先需要确认头文件是否存在。下面第一个命令是确认是否有。如果没有,请使用第二个下载它。apt-cachesearchlinux-headers-$(uname-r)sudoapt-getinstalllinux-headers-$(uname-r)2.2驱动框架代码构建代码如下图所示:#include#include#includestaticintmajor=0;staticintker_val=123;staticstructclass*hello_for_class;staticssize_thello_read(structfile*file,char__user*buf,size_tsize,loff_t*offset){printk("%s%sline%d\n",__FILE__,__FUNCTION__,__LINE__);return4;}staticssize_thello_write(structfile*file,constchar__user*buf,size_tsize,loff_t*offset){printk("%s%sline%d\n",__FILE__,__FUNCTION__,__LINE__);return4;}int__inithello_init(void){printk("hello_linux_driinit\n");return0;}void__exithello_exit(void){printk("hello_linux_driexit\n");return;}module_init(hello_init);module_exit(hello_exit);MODULE_LICENSE("GPL");以上是驱动代码的一个基本框架3.注册驱动使用file_operations结构填充register_chrdev函数注册驱动staticstructfile_operationshello_linux_fops={.owner=THIS_MODULE,.read=hello_read,.write=hello_write,};int__inithello_init(void){printk("hello_linux_driinit\n");major=register_chrdev(0,"hello_linux_dri",&hello_linux_fops);return0;}4.注销驱动使用unregister_chrdev函数取消注册的驱动信息void__exithello_exit(void){printk("hello_linux_driexit\n");unregister_chrdev(major,"hello_linux_dri");return;}5.自动生成设备文件使用驱动5.1生成设备文件使用class_create和device_create生成,代码如下如下:int__inithello_init(void){printk("hello_linux_driinit\n");major=register_chrdev(0,"hello_linux_dri",&hello_linux_fops);hello_for_class=class_create(THIS_MODULE,"hello_class");device_create(hello_for_class,NULL,MKDEV(major,0),NULL,"hello_linux_dri");return0;}5.2撤消设备文件使用device_destroy和class_destroy进行撤消,代码如下:void__exithello_exit(void){printk("hello_linux_driexit\n");device_destroy(hello_for_class,MKDEV(专业,0));class_destroy(hello_for_class);unregister_chrdev(major,"hello_linux_dri");return;}效果如下下一篇:点击下方链接下载文章相关附件https://harmonyos.51cto.com/resource/1513更多信息请访问:与华为官方共建的鸿蒙技术社区https://harmonyos.51cto.com