第三讲 我的第一个Linux驱动
一、字符设备驱动框架
字符设备驱动的编写主要就是驱动对应的open、close、read。。。其实就是
file_operations结构体的成员变量的实现。
二、驱动模块的加载与卸载
Linux驱动程序可以编译到kernel里面,也就是zImage,也可以编译为模块,.ko。测试的时候只需要加载.ko模块就可以。
编写驱动的时候注意事项!
1、编译驱动的时候需要用到linux内核源码!因此要解压缩linux内核源码,编译linux内核源码。得到zImage和.dtb。需要使用编译后的到的zImage和dtb启动系统。
2、从SD卡启动,SD卡烧写了uboot。uboot通过tftp从ubuntu里面获取zimage和dtb,rootfs也是通过nfs挂在。
3、设置bootcmd和bootargs
bootargs=console=ttymxc0,115200 rw root=/dev/nfs nfsroot=192.168.1.66:/home/zzk/linux/nfs/rootfs ip=192.168.1.50:192.168.1.66:192.168.1.1:255.255.255.0::eth0:off
bootcmd=tftp 80800000 zImage;tftp 83000000 imx6ull-alientek-emmc.dtb;bootz 80800000 - 83000000;
4、将编译出来的.ko文件放到根文件系统里面。加载驱动会用到加载命令:insmod,modprobe。移除驱动使用命令rmmod。对于一个新的模块使用modprobe加载的时候需要先调用一下depmod命令。
5,驱动模块加载成功以后可以使用lsmod查看一下。
6,卸载模块使用rmmod命令
三、字符设备的注册与注销
1、我们需要向系统注册一个字符设备,使用函数register_chrdev。
2、卸载驱动的时候需要注销掉前面注册的字符设备,使用函数unregister_chrdev,注销字符设备。
四、设备号
1,Linux内核使用dev_t。
typedef __kernel_dev_t dev_t;
typedef u32 kernel_dev_t;
typedef unsigned int __u32;
2、Linux内核将设备号分为两部分:主设备号和次设备号。主设备号占用前12位,次设备号占用低20位。
3、设备号的操作函数,或宏
从dev_t获取主设备号和次设备号,MAJOR(dev_t),MINOR(dev_t)。也可以使用主设备号和次设备号构成dev_t,通过MKDEV(major,minor)
五、file_operations的具体实现
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
};
六、字符设备驱动框架的搭建
七、应用程序编写
Linux下一切皆文件,首先要open
八、测 试
1、加载驱动。
modprobe chrdevbase.ko
2,进入/dev查看设备文件,chrdevbase。/dev/chrdevbase。但是实际没有,因为我们没有创建设备节点。
mknod /dev/chrdevbase c 200 0
3、测试
./chrdevbaseAPP /dev/chrdevbase
八、chrdevbase虚拟设备驱动的完善
要求:应用程序可以对驱动读写操作,读的话就是从驱动里面读取字符串,写的话就是应用向驱动写字符串。
1、chrdevbase_read驱动函数编写
驱动给应用传递数据的时候需要用到copy_to_user函数。
第四讲 Linux LED灯驱动实验(直接操作寄存器)
一、地址映射
1、裸机LED灯实验就是操作6ULL的寄存器。
2,Linux驱动开发也可以操作寄存器,Linux不能直接对寄存器物理地址进行读写操作,比如寄存器A物理地址为0X01010101。裸机的时候可以直接对0X01010101这个物理地址进行操作,但是linux下不行。因为linux会使能MMU。
在linux里面操作的都是虚拟地址,所以需要先得到0X01010101这个物理地址对应的虚拟地址。
获得物理物理地址对应的虚拟地址使用ioremap函数。
第一个参数就是物理地址其实大小,第二个参数就是要转化的字节数量。0X01010101,开始10个地址进行转换,
va=ioremap(0X01010101, 10).
卸载驱动的时候:
iounmap(va);
二、LED灯字符设备驱动框架搭建
1、uboot下载系统失败,以前都能成功,突然不能下载怎么解决?
首先,保证正个网段内开发板的IP地址和ubuntu的IP地址是唯一的,测试哪个IP地址有冲突,比如ubuntu的192.168.1.66有被其他设备占用,如果有占用就改一个没被占用的IP地址。
三、驱动程序编写
1、初始化时钟、IO、GPIO等等。
2、初始化完成以后进行测试,但是如果你烧写/用的是正点原子提供的linux内核,这个时候LED灯默认被配置为了心跳灯,必须关闭心跳灯。
四、应用程序编写
五、测试
1、加载驱动
2、创建设备节点
mknod /dev/led c 200 0
第五讲 Linux新字符设备驱动实验
一、新字符设备驱动原理
二、自动创建设备节点
三、文件私有数据
四、实验程序与应用程序编写
五、测试
本文详细介绍了Linux驱动开发的过程,包括字符设备驱动的框架、加载与卸载、设备注册与注销,以及file_operations的实现。此外,还探讨了Linux环境下如何直接操作寄存器进行LED灯驱动实验,涉及地址映射和驱动程序编写,并提供了测试步骤。
1480

被折叠的 条评论
为什么被折叠?



