0
点赞
收藏
分享

微信扫一扫

嵌入式linux/鸿蒙开发板(IMX6ULL)开发(十八)Hello驱动初识

正义的杰克船长 2022-03-13 阅读 70

文章目录

1. 嵌入式Linux驱动开发基础知识的引导与说明

1.1打算讲什么、怎么讲?

以几个简单的驱动程序,讲解嵌入式Linux驱动的框架,了解驱动开发的流程、方法,掌握从APP到驱动的调用流程。
会涉及很多种开发板,让你明白“Linux驱动 = 软件框架 + 硬件操作”,让你“一通百通”,掌握了普适性的原理之后,在工作中很容易在其他板子使用这些知识。
以LED驱动为例,会如下讲解:
在这里插入图片描述

1.2 需要做什么准备工作

驱动程序依赖于Linux内核,你为开发板A开发驱动,那就先在Ubuntu中得到、配置、编译开发板A所使用的Linux内核。
请使用git下载本教程的文档、源码,查看如下目录中你所用开发板的高级用户使用手册(有些开发板的手册我们还没编写完,持续更新):
在这里插入图片描述根据手册完成下面操作:
硬件部分:
① 开发板接线:串口线、电源线、网线
② 开发板烧写系统
软件部分:
① 下载Linux内核,Windows和Ubuntu下各放一份
② Windows下:使用Source Insight创建内核源码的工程,这是用来浏览内核、编辑驱动
③ Ubuntu下:安装工具链,配置、编译Linux内核

注意:GIT的使用方法请参考以下章节:
《第1篇 新学习路线、视频介绍、资料下载》
《第二章 资源下载方法》
《2.3 GIT使用简明教程》

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

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

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

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

APP打开文件时,可以得到一个整数,这个整数被称为文件句柄。对于APP的每一个文件句柄,在内核里面都有一个“struct file”与之对应。
在这里插入图片描述可以猜测,我们使用open打开文件时,传入的flags、mode等参数会被记录在内核中对应的struct file结构体里(f_flags、f_mode):

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

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

在这里插入图片描述

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

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

在这里插入图片描述在这里插入图片描述结构体struct file_operations的定义如下:
在这里插入图片描述

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

① 确定主设备号,也可以让内核分配
② 定义自己的file_operations结构体
③ 实现对应的drv_open/drv_read/drv_write等函数,填入file_operations结构体
④ 把file_operations结构体告诉内核:register_chrdev
⑤ 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数
⑥ 有入口函数就应该有出口函数:卸载驱动程序时,出口函数调用unregister_chrdev
⑦ 其他完善:提供设备信息,自动创建设备节点:class_create, device_create

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

2.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>
02
03 #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>
17
18 /* 1. 确定主设备号 */
19 static int major = 0;
20 static char kernel_buf[1024];
21 static struct class *hello_class;
22
23
24 #define MIN(a, b) (a < b ? a : b)
25
26 /* 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 {
29      int err;
30      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
31      err = copy_to_user(buf, kernel_buf, MIN(1024, size));
32      return MIN(1024, size);
33 }
34
35 static ssize_t hello_drv_write (struct file *file, const char __user *buf, size_t size, loff_t *offset)
36 {
37      int err;
38      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
39      err = copy_from_user(kernel_buf, buf, MIN(1024, size));
40      return MIN(1024, size);
41 }
42
43 static int hello_drv_open (struct inode *node, struct file *file)
44 {
45      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
46      return 0;
47 }
48
49 static int hello_drv_close (struct inode *node, struct file *file)
50 {
51      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
52      return 0;
53 }
54
55 /* 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 };
63
64 /* 4. 把file_operations结构体告诉内核:注册驱动程序 */
65 /* 5. 谁来注册驱动程序啊?得有一个入口函数:安装驱动程序时,就会去调用这个入口函数 */
66 static int __init hello_init(void)
67 {
68      int err;
69
70      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
71      major = register_chrdev(0, "hello", &hello_drv);  /* /dev/hello */
72
73
74      hello_class = class_create(THIS_MODULE, "hello_class");
75      err = PTR_ERR(hello_class);
76      if (IS_ERR(hello_class)) {
77              printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
78              unregister_chrdev(major, "hello");
79              return -1;
80      }
81
82      device_create(hello_class, NULL, MKDEV(major, 0), NULL, "hello"); /* /dev/hello */
83
84      return 0;
85 }
86
87 /* 6. 有入口函数就有出口函数:卸载驱动程序时就会去调用这个出口函数 */
88 static void __exit hello_exit(void)
89 {
90      printk("%s %s line %d\n", __FILE__, __FUNCTION__, __LINE__);
91      device_destroy(hello_class, MKDEV(major, 0));
92      class_destroy(hello_class);
93      unregister_chrdev(major, "hello");
94 }
95
96
97 /* 7. 其他完善:提供设备信息,自动创建设备节点 */
98
99 module_init(hello_init);
100 module_exit(hello_exit);
101
102 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函数。

2.4.2 写测试程序

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

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

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

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

hello_drv_test.c源码如下:

01
02 #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>
08
09 /*
10  * ./hello_drv_test -w abc
11  * ./hello_drv_test -r
12  */
13 int main(int argc, char **argv)
14 {
15      int fd;
16      char buf[1024];
17      int len;
18
19      /* 1. 判断参数 */
20      if (argc < 2)
21      {
22              printf("Usage: %s -w <string>\n", argv[0]);
23              printf("       %s -r\n", argv[0]);
24              return -1;
25      }
26
27      /* 2. 打开文件 */
28      fd = open("/dev/hello", O_RDWR);
29      if (fd == -1)
30      {
31              printf("can not open file /dev/hello\n");
32              return -1;
33      }
34
35      /* 3. 写文件或读文件 */
36      if ((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      }
42      else
43      {
44              len = read(fd, buf, 1024);
45              buf[1023] = '\0';
46              printf("APP read : %s\n", buf);
47      }
48
49      close(fd);
50
51      return 0;
52 }
53

2.4.3 测试

A. 编写驱动程序的Makefile
驱动程序中包含了很多头文件,这些头文件来自内核,不同的ARM板它的某些头文件可能不同。所以编译驱动程序时,需要指定板子所用的内核的源码路径。
要编译哪个文件?这也需要指定,设置obj-m变量即可
怎么把.c文件编译为驱动程序.ko?这要借助内核的顶层Makefile。
本驱动程序的Makefile内容如下:

01
02 # 1. 使用不同的开发板内核时, 一定要修改KERN_DIR
03 # 2. KERN_DIR中的内核要事先配置、编译, 为了能编译内核, 要先设置下列环境变量:
04 # 2.1 ARCH,          比如: export ARCH=arm64
05 # 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-2017.05-x86_64_aarch64-linux-gnu/bin
07 # 注意: 不同的开发板不同的编译器上述3个环境变量不一定相同,
08 #       请参考各开发板的高级用户使用手册
09
10 KERN_DIR = /home/book/100ask_roc-rk3399-pc/linux-4.4
11
12 all:
13      make -C $(KERN_DIR) M=`pwd` modules
14      $(CROSS_COMPILE)gcc -o hello_drv_test hello_drv_test.c
15
16 clean:
17      make -C $(KERN_DIR) M=`pwd` modules clean
18      rm -rf modules.order
19      rm -f hello_drv_test
20
21 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 root     root      236,   0 Jan 18 08:55 /dev/hello
# ./hello_drv_test        // 查看测试程序的用法
Usage: ./hello_drv_test -w <string>
       ./hello_drv_test -r
# ./hello_drv_test -w wiki.100ask.net    // 往驱动程序中写入字符串
[  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 30
APP read : wiki.100ask.net
[  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浅析

3.硬件知识_LED原理图

当我们学习C语言的时候,我们会写个Hello程序。
那当我们写ARM程序,也该有一个简单的程序引领我们入门,这个程序就是点亮LED。

我们怎样去点亮一个LED呢?
分为三步:
1.看原理图,确定控制LED的引脚;
2.看主芯片的芯片手册,确定如何设置控制这个引脚;
3.写程序;

3.1 先来讲讲怎么看原理图

LED样子有很多种,像插脚的,贴片的。
在这里插入图片描述它们长得完全不一样,因此我们在原理图中将它抽象出来。

点亮LED需要通电源,同时为了保护LED,加个电阻减小电流。
控制LED灯的亮灭,可以手动开关LED,但在电子系统中,不可能让人来控制开关,通过编程,利用芯片的引脚去控制开关。

在这里插入图片描述LED的驱动方式,常见的有四种。
方式1:使用引脚输出3.3V点亮LED,输出0V熄灭LED。
方式2:使用引脚拉低到0V点亮LED,输出3.3V熄灭LED。

有的芯片为了省电等原因,其引脚驱动能力不足,这时可以使用三极管驱动。
方式3:使用引脚输出1.2V点亮LED,输出0V熄灭LED。
方式4:使用引脚输出0V点亮LED,输出1.2V熄灭LED。

在这里插入图片描述由此,主芯片引脚输出高电平/低电平,即可改变LED状态,而无需关注GPIO引脚输出的是3.3V还是1.2V。
所以简称输出1或0:
逻辑1–>高电平
逻辑0–>低电平

举报

相关推荐

0 条评论