(本节笔记的实验代码,在这里)
一. 使用字符设备驱动程序
1. 编译和安装驱动程序模块
1.1 将驱动模块源码memdev.c复制到Linux系统中,编写Makefile,编译成内核模块memdev.ko并拷贝到开发板的NFS的rootfs中。
2.2 把memdev.ko加载(insmod)到开发板的Linux系统内核中。
2. 创建设备文件
通过字符设备文件,应用程序可以使用相应的字符设备驱动程序来控制字符设备。创建字符设备文件的方法有两种:1)使用mknod命令,如:【mknod /dev/文件名 c 主设备号 次设备号】;2)使用函数在驱动程序中创建。
//可cat/proc/devices 查看已挂载的设备驱动的主设备号
//而次设备号一般取非负切小于255的整数(0~254)
//memdev.ko模块的功能是模拟一个硬件,通过往设备写入数据,模拟向硬件设备中写入数据
创建设备文件:mknod/dev/memdev0 c 253 0
3. 应用程序通过设备文件访问设备
touch write_mem.c
『
#include <stdio.h>
#include <linux/types.h>
#include <linux/stat.h>
#include <fcntl.h>
int main()
{
int src = 2015;
int fd = 0;
fd = open("/dev/memdev0",O_RDWR);
write(fd, &src, sizeof(int));
close(fd);
return 0;
}
』
对write_mem.c进行交叉编译:arm-linux-gccwrite_men.c -o write_men
把write_men拷贝到rootfs中,在开发板上运行write_men,若出现以下提示:~/bin/sh:./write_mem:notfound,证明开发板Linux系统中并不包含write_mem所需要的库(可通过arm-linux-readelf -d write_mem来检查该应用程序所需要的动态链接库),因此需要加入-static选项进行静态编译。
touch read_mem.c
『
#include <stdio.h>
#include <linux/types.h>
#include <linux/stat.h>
#include <fcntl.h>
int main()
{
int dst = 0;
int fd = 0;
fd = open("/dev/memdev0",0_RDWR);
read(fd, &dst, sizeof(int));
printf("dst is %d\n",dst);
close(fd);
return 0;
}
』
//通过read_mem把写入到设备中的数据读出来。
二. 字符设备驱动模型
在Linux系统中,设备的类型繁多,如:字符设备、块设备、网络接口设备、USB设备、PCI设备、平台设备和混杂设备等,设备类型的不同意味着对应的驱动程序模型的不同。
0. Linux设备驱动模型
0.1 驱动程序初始化
1)分配设备描述符
2)初始化设备描述结构
3)注册设备描述结构
4)硬件初始化
0.2 实现设备操作
0.3 驱动注销
1. 驱动程序初始化
1.0 基本概念
1.0.1 设备描述结构cdev //#include <linux/cdev.h>
任何一种驱动模型中,设备都会用内核中的一种结构来描述,而字符设备在内核中使用struct cdev来进行描述,具体内容如下:
struct cdev{
struct kobject kobj;
struct module *cwner;
※ const struct file_operations *ops; //设备操作集
structlist_head list;
※ dev_t dev; //设备号