700字范文,内容丰富有趣,生活中的好帮手!
700字范文 > 嵌入式linux/鸿蒙开发板(IMX6ULL)开发(二十一)第一次写驱动程序

嵌入式linux/鸿蒙开发板(IMX6ULL)开发(二十一)第一次写驱动程序

时间:2023-06-11 23:02:28

相关推荐

嵌入式linux/鸿蒙开发板(IMX6ULL)开发(二十一)第一次写驱动程序

文章目录

1.Hello驱动(不涉及硬件操作)1.1 APP打开的文件在内核中如何表示1.2 打开字符设备节点时,内核中也有对应的struct file1.3 请猜猜怎么编写驱动程序1.4 请不要啰嗦,表演你的代码吧1.4.1 写驱动程序1.4.2 写测试程序1.4.3 编写makefile 2.5 Hello驱动中的一些补充知识2.5.1 module_init/module_exit的实现2.5.2 register_chrdev的内部实现2.5.3 class_destroy/device_create浅析

1.Hello驱动(不涉及硬件操作)

我们选用的内核都是4.x版本,操作都是类似的:

rk3399 linux 4.4.154rk3288 linux 4.4.154imx6ul linux 4.9.88am3358 linux 4.9.168

也就是说你要用sourceinsight,打开内核源码,在内核源码的工程下继续编写程序。因为在这个编写驱动的过程中要用到很多关于内核的函数。你可以直接去用source工程去看。不要用其他的软件。

把之前的过程重新复现一遍。

1.1 APP打开的文件在内核中如何表示

APP打开文件时,可以得到一个整数,这个整数被称为文件句柄。对于APP的每一个文件句柄,在内核里面都有一个“struct file”与之对应。

可以猜测,我们使用open打开文件时,传入的flagsmode等参数会被记录在内核中对应的struct file结构体里(f_flagsf_mode):

int open(const char *pathname, int flags, mode_t mode);

去读写文件时,文件的当前偏移地址也会保存在struct file结构体的f_pos成员里。

1.2 打开字符设备节点时,内核中也有对应的struct file

注意这个结构体中的结构体:struct file_operations *f_op,这是由驱动程序提供的。

open一个设备节点以后,会返回一个句柄,这个的句柄会在内核中有一个struct file与其对应。flags,以及mode会传参数到struct file中去,并且当使用open打开以后,根据主设备号会在一个叫chadevs[]的数组上找到file_operation,以及其上对应的open,read,write等函数,当app使用read/write的时候,会调用驱动中的write以及read

结构体struct file_operations的定义如下:

1.3 请猜猜怎么编写驱动程序

① 确定主设备号,也可以让内核分配

② 定义自己的file_operations结构体

③ 实现对应的drv_open/drv_read/drv_write等函数,填入file_operations结构体

④把file_operations结构体告诉内核:register_chrdev

⑤谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数

⑥有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev

⑦其他完善:提供设备信息,自动创建设备节点:class_create, device_create

1.4 请不要啰嗦,表演你的代码吧

1.4.1 写驱动程序

参考driver/char中的程序,包含头文件,写框架,传输数据:

A. 驱动中实现open, read, write, release,APP调用这些函数时,都打印内核信息

B. APP调用write函数时,传入的数据保存在驱动中

C.APP调用read函数时,把驱动中保存的数据返回给APP

使用GIT下载所有源码后,本节源码位于如下目录:

01_all_series_quickstart\05_嵌入式Linux驱动开发基础知识\source\01_hello_drv\hello_drv.c

hello_drv.c源码如下:

01 #include <linux/module.h>0203 #include <linux/fs.h>04 #include <linux/errno.h>05 #include <linux/miscdevice.h>06 #include <linux/kernel.h>07 #include <linux/major.h>08 #include <linux/mutex.h>09 #include <linux/proc_fs.h>10 #include <linux/seq_file.h>11 #include <linux/stat.h>12 #include <linux/init.h>13 #include <linux/device.h>14 #include <linux/tty.h>15 #include <linux/kmod.h>16 #include <linux/gfp.h>1718 /* 1. 确定主设备号 */19 static int major = 0;//20 static char kernel_buf[1024];21 static struct class *hello_class;222324 #define MIN(a, b) (a < b ? a : b)2526 /* 3. 实现对应的open/read/write等函数,填入file_operations结构体 */ 27 static ssize_t hello_drv_read (struct file *file, char __user *buf, size_t size, loff_t *offset)28 {29int err;30printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);31err = copy_to_user(buf, kernel_buf, MIN(1024, size));32return MIN(1024, size);33 }3435 static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)36 {37int err;38printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);39err = copy_from_user(kernel_buf, buf, MIN(1024, size));40return MIN(1024, size);41 }4243 static int hello_drv_open (struct inode *node, struct file *file)44 {45printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);46return 0;47 }4849 static int hello_drv_close (struct inode *node, struct file *file)50 {51printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);52return 0;53 }5455 /* 2. 定义自己的file_operations结构体 */56 static struct file_operations hello_drv = {57.owner = THIS_MODULE,58.open = hello_drv_open,59.read = hello_drv_read,60.write = hello_drv_write,61.release = hello_drv_close,62 };6364 /* 4. 把file_operations结构体告诉内核:注册驱动程序 */65 /* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */66 static int __init hello_init(void)67 {68int err;6970printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);71major = register_chrdev(0, "hello", &hello_drv); /* /dev/hello */727374hello_class = class_create(THIS_MODULE, "hello_class");75err = PTR_ERR(hello_class);76if (IS_ERR(hello_class)) {77 printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);78 unregister_chrdev(major, "hello");79 return -1;80}8182device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */8384return 0;85 }8687 /* 6. 有入口函数就有出口函数:卸载驱动程序时就会去调用这个出口函数 */88 static void __exit hello_exit(void)89 {90printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);91device_destroy(hello_class, MKDEV(major, 0));92class_destroy(hello_class);93unregister_chrdev(major, "hello");94 }959697 /* 7. 其他完善:提供设备信息,自动创建设备节点 */9899 module_init(hello_init);100 module_exit(hello_exit);101102 MODULE_LICENSE("GPL");103

阅读一个驱动程序,从它的入口函数开始,第66行就是入口函数。它的主要工作就是第71行,向内核注册一个file_operations结构体:hello_drv,这就是字符设备驱动程序的核心。

file_operations结构体hello_drv在第56行定义,里面提供了open/read/write/release成员,应用程序调用open/read/write/close时就会导致这些成员函数被调用。

file_operations结构体hello_drv中的成员函数都比较简单,大多数只是打印而已。要注意的是,驱动程序和应用程序之间传递数据要使用copy_from_user/copy_to_user函数。

1.4.2 写测试程序

测试程序要实现写、读功能:

A. ./hello_drv_test -w // 把字符串“”发给驱动程序B. ./hello_drv_test -r // 把驱动中保存的字符串读回来

使用GIT下载所有源码后,本节源码位于如下目录:

01_all_series_quickstart\05_嵌入式Linux驱动开发基础知识\source\01_hello_drv\hello_drv_test.c

hello_drv_test.c源码如下:

0102 #include <sys/types.h>03 #include <sys/stat.h>04 #include <fcntl.h>05 #include <unistd.h>06 #include <stdio.h>07 #include <string.h>0809 /*10 * ./hello_drv_test -w abc11 * ./hello_drv_test -r12 */13 int main(int argc, char **argv)14 {15int fd;16char buf[1024];17int len;1819/* 1. 判断参数 */20if (argc < 2)21{22 printf("Usage: %s -w <string>\n", argv[0]);23 printf(" %s -r\n", argv[0]);24 return -1;25}2627/* 2. 打开文件 */28fd = open("/dev/hello", O_RDWR);29if (fd == -1)30{31 printf("can not open file /dev/hello\n");32 return -1;33}3435/* 3. 写文件或读文件 */36if ((0 == strcmp(argv[1], "-w")) && (argc == 3))37{38 len = strlen(argv[2]) + 1;39 len = len < 1024 ? len : 1024;40 write(fd, argv[2], len);41}42else43{44 len = read(fd, buf, 1024);45 buf[1023] = '\0';46 printf("APP read : %s\n", buf);47}4849close(fd);5051return 0;52 }53

1.4.3 编写makefile

A. 编写驱动程序的Makefile

驱动程序中包含了很多头文件,这些头文件来自内核,不同的ARM板它的某些头文件可能不同。所以编译驱动程序时,需要指定板子所用的内核的源码路径。

要编译哪个文件?这也需要指定,设置obj-m变量即可 怎么把.c文件编译为驱动程序.ko?这要借助内核的顶层Makefile。

本驱动程序的Makefile内容如下:

0102 # 1. 使用不同的开发板内核时, 一定要修改KERN_DIR03 # 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:04 # 2.1 ARCH,比如: export ARCH=arm6405 # 2.2 CROSS_COMPILE, 比如: export CROSS_COMPILE=aarch64-linux-gnu-06 # 2.3 PATH,比如: export PATH=$PATH:/home/book/100ask_roc-rk3399-pc/ToolChain-6.3.1/gcc-linaro-6.3.1-.05-x86_64_aarch64-linux-gnu/bin07 # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,08 # 请参考各开发板的高级用户使用手册0910 KERN_DIR = /home/book/100ask_roc-rk3399-pc/linux-4.41112 all:13make -C $(KERN_DIR) M=`pwd` modules14$(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c1516 clean:17make -C $(KERN_DIR) M=`pwd` modules clean18rm -rf modules.order19rm -f hello_drv_test obj-m += hello_drv.o

先设置好交叉编译工具链,编译好你的板子所用的内核,然后修改Makefile指定内核源码路径,最后即可执行make命令编译驱动程序和测试程序。

B. 上机实验

注意:我们是在Ubuntu中编译程序,但是需要在ARM板子上测试。所以需要把程序放到ARM板子上。

启动单板后,可以通过NFS挂载Ubuntu的某个目录,访问该目录中的程序。

测试示例:

① 在Ubuntu上编译好驱动,并它复制到NFS目录:

$ cp *.ko hello_drv_test ~/nfs_rootfs/

② 在ARM板上测试:

# echo "7 4 1 7" > /proc/sys/kernel/printk // 打开内核的打印信息,有些板子默认打开了# ifconfig eth0 192.168.1.10 // 配置ARM板IP,下面是挂载NFS文件系统// 2.如果使用VMware桥接网络,假设Ubuntu IP为192.168.1.100,使用下面命令挂载NFS# mount -t nfs -o nolock,vers=3 192.168.1.100:/home/book/nfs_rootfs /mnt# cd /mnt# insmod hello_drv.ko // 安装驱动程序[ 293.594910] hello_drv: loading out-of-tree module taints kernel.[ 293.616051] /home/book/source/01_hello_drv/hello_drv.c hello_init line 70# ls /dev/hello -l // 驱动程序会生成设备节点crw------- 1 rootroot236, 0 Jan 18 08:55 /dev/hello# ./hello_drv_test // 查看测试程序的用法Usage: ./hello_drv_test -w <string>./hello_drv_test -r# ./hello_drv_test -w// 往驱动程序中写入字符串[ 318.360800] /home/book/source/01_hello_drv/hello_drv.c hello_drv_open line 45[ 318.372570] /home/book/source/01_hello_drv/hello_drv.c hello_drv_write line 38[ 318.382854] /home/book/source/01_hello_drv/hello_drv.c hello_drv_close line 51# ./hello_drv_test -r // 从驱动程序中读出字符串[ 326.177890] /home/book/source/01_hello_drv/hello_drv.c hello_drv_open line 45[ 326.198304] /home/book/source/01_hello_drv/hello_drv.c hello_drv_read line 30APP read : [ 326.214782] /home/book/source/01_hello_drv/hello_drv.c hello_drv_close line 51

注意:如果安装驱动时提示version magic不匹配,或是污染内核(taint),请参考这些章节更新内核:

《第2篇 环境搭建、Linux基本操作、工具使用》

《第九章 开发板的第1个驱动程序》

2.5 Hello驱动中的一些补充知识

2.5.1 module_init/module_exit的实现

2.5.2 register_chrdev的内部实现

2.5.3 class_destroy/device_create浅析

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。