摘要:搞嵌入式有两个方向,一个是嵌入式软件开发(MCU方向),另一个是嵌入式软件开发(Linux方向)。其中MCU方向基本是裸机开发和RTOS开发。而Linux开发方向又分为驱动开发和应用开发。其中应用开发相比于驱动开发来说简单一些,因为搞驱动你要和Linux内核打交道。而我们普通的单片机开发就是应用开发,和Linux开发没多大区别,单片机你去调别人写好的库,Linux应用你也是调别人的驱动程序。
很多人学习的路线是:单片机到RTOS,再到Linux,这个路线其实是非常好,循序渐进。因为你学了单片机,所以你对RTOS的学习会很容易理解,单片机+RTOS在市面上也可以找到一个很好的工作。因为你学了RTOS,你会发现Linux驱动开发其实和RT-Thread的驱动程序非常像,其实RT-Thread驱动大概率可能是仿Linux驱动而写的。所以如果你现在在学RT-Thread,那么你后面去搞Linux驱动也是非常容易上手。
当然做驱动去之前你还是要学习一下ubuntu操作系统、ARM裸机和linux系统移植,其目的就是为学习嵌入式linux驱动开发做准备。
话不多说先来一个hello驱动程序。
在Linux中,驱动分为三大类:
我使用的Linux内核版本为 4.1.15,其支持设备树Device tree。开发板是正点原子送的Linux-MINI板,你用其他家的板子也是一样的,没有任何影响。
一、字符设备驱动简介
字符设备是Linux驱动中最基本的一类设备驱动,字符设备就是一个一个字节,按照字节流进行读写操作的设备,读写数据是分先后顺序的。比如我们最常见的点灯、按键、IIC、SPI,LCD 等等都是字符设备,这些设备的驱动就叫做字符设备驱动。
那么在Linux下的应用程序是如何调用驱动程序的呢?Linux 应用程序对驱动程序的调用如图所示:
Linux应用程序对驱动程序的调用流程
在Linux 中一切皆为文件,驱动加载成功以后会在/dev目录下生成一个相应的文件,应用程序通过对这个名为/dev/xxx(xxx是具体的驱动文件名字)的文件进行相应的操作即可实现对硬件的操作。
写驱动的人必须要懂linux内核,因为驱动程序就是根据内核的函数去写的,写应用的人不需要懂linux内核,只需要熟悉驱动函数就可以了。
比如现在有个叫做/dev/led的驱动文件,是led灯的驱动文件。应用程序使用open函数来打开文件/dev/led,使用完成以后使用close函数关闭/dev/led 这个文件。open和 close 就是打开和关闭led驱动的函数,如果要点亮或关闭led,那么就使用write 函数来操作,也就是向此驱动写入数据,这个数据就是要关闭还是要打开led的控制参数。如果要获取led 灯的状态,就用 read 函数从驱动中读取相应的状态。
应用程序运行在用户空间,而Linux 驱动属于内核的一部分,因此驱动运行于内核空间。当我们在用户空间想要实现对内核的操作,比如使用open函数打开/dev/led这个驱动,因为用户空间不能直接对内核进行操作,因此必须使用一个叫做“系统调用”的方法来实现从用户空间“陷入”到内核空间,这样才能实现对底层驱动的操作。
open、close、write 和read等这些函数是由C库提供的,在Linux系统中,系统调用作为C库的一部分。当我们调用 open 函数的时候流程如图所示:
open函数调用流程
其中关于C库以及如何通过系统调用“陷入”到内核空间这个我们不用去管,我们关注的是应用程序和具体的驱动,应用程序使用到的函数在具体驱动程序中都有与之对应的函数,比如应用程序中调用了open这个函数,那么在驱动程序中也得有一个名为open的函数。每一个系统调用,在驱动中都有与之对应的一个驱动函数。
在Linux内核文件include/linux/fs.h中有个叫做file_operations的结构体,此结构体就是Linux内核驱动操作函数集合,我们可以将linux内核文件下载下来,然后用source insight打开看看。内容如下所示:
点击此处下载linux内核源码
嵌入式物联 需要学的东西真的非常多,千万不要学错了路线和内容,导致工资要不上去!
无偿分享大家一个资料包,差不多150多G。里面学习内容、面经、项目都比较新也比较全!某鱼上买估计至少要好几十。
点击这里找小助理0元领取:加微信领取资料
Linux内核
struct file_operations {
struct module *owner;
loff_t (*llseek) (struct file *, loff_t, int);
ssize_t (*read) (struct file *, char __user *, size_t, loff_t *);
ssize_t (*write) (struct file *, const char __user *, size_t, loff_t *);
ssize_t (*read_iter) (struct kiocb *, struct iov_iter *);
ssize_t (*write_iter) (struct kiocb *, struct iov_iter *);
int (*iterate) (struct file *, struct dir_context *);
unsigned int (*poll) (struct file *, struct poll_table_struct *);
long (*unlocked_ioctl) (struct file *, unsigned int, unsigned long);
long (*compat_ioctl) (struct file *, unsigned int, unsigned long);
int (*mmap) (struct file *, struct vm_area_struct *);
int (*mremap)(struct file *, struct vm_area_struct *);
int (*open) (struct inode *, struct file *);
int (*flush) (struct file *, fl_owner_t id);
int (*release) (struct inode *, struct file *);
int (*fsync) (struct file *, loff_t, loff_t, int datasync);
int (*aio_fsync) (struct kiocb *, int datasync);
int (*fasync) (int, struct file *, int);
int (*lock) (struct file *, int, struct file_lock *);
ssize_t (*sendpage) (struct file *, struct page *, int, size_t, loff_t *, int);
unsigned long (*get_unmapped_area)(struct file *, unsigned long, unsigned long, unsigned long, unsigned long);
int (*check_flags)(int);
int (*flock) (struct file *, int, struct file_lock *);
ssize_t (*splice_write)(struct pipe_inode_info *, struct file *, loff_t *, size_t, unsigned int);
ssize_t (*splice_read)(struct file *, loff_t *, struct pipe_inode_info *, size_t, unsigned int);
int (*setlease)(struct file *, long, struct file_lock **, void **);
long (*fallocate)(struct file *file, int mode, loff_t offset,
loff_t len);
void (*show_fdinfo)(struct seq_file *m, struct file *f);
#ifndef CONFIG_MMU
unsigned (*mmap_capabilities)(struct file *);
#endif
};
二、字符设备驱动开发
学习裸机或者STM32的时候关于驱动的开发就是初始化相应的外设寄存器,在Linux驱动开发中肯定也是要初始化相应的外设寄存器,这个是毫无疑问的。只是在Linux驱动开发中我们需要按照其规定的框架来编写驱动,所以说学Linux驱动开发重点是学习其驱动框架。
2.1 APP打开的文件在内核中如何表示
APP使用open函数打开文件时,可以得到一个整数,这个整数被称为文件句柄。对于APP的每一个文件句柄,在内核里面都有一个struct file与之对应。
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成员里。
open->struct file
打开字符设备节点时,内核中也有对应的struct file注意这个结构体中的结构体:struct file_operations *f_op,这是由驱动程序提供的。
驱动程序的 struct file
声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!