Linux字符设备驱动开发基础
来源:互联网 发布:正规的网络赚钱平台 编辑:程序博客网 时间:2024/05/30 22:54
Linux驱动:封装对底层硬件的操作,向上层应用提供操作接口
一. 概念介绍
一般用户在应用程序里调用的 open, read, write 函数是 c 库的函数, 这些函数会触发 swi val异常,从而引发系统调用,进入到内核空间, 内核通过VFS(virtual Filesystem)来实现调用不同的驱动函数。
例如:我们有一个函数,
int main(){ int fd1, fd2; int val = 1; fd1 = open("/dev/led", O_RDWR); write(fd1, &val, 4); fd2 = open("hello.txt", O_RDWR); write(fd2, &val, 4);}
函数里相同的open、write函数,引发的不同的行为,一个是操控硬件,一个是写文件。
简单的调用关系如下:
用户 –> 系统调用 –> 驱动程序
open –> sys.open –> led.open write –> sys.write –> led.write
二. 字符设备驱动框架
实现步骤:
- 实现驱动的 led.open, led.write, led.read 操作
- 定义file_operations结构体, 把驱动函数填充到里面
- 把这个结构告诉内核, 通个注册函数 register_chrdev(major, “first_drv”, &first_drv_fops) 来实现
- 谁来调用注册函数 –>驱动的入口函数来调用这个注册函数, first_drv_init
- 修饰一下这个函数入口函数,module_init(first_drv_init)
//第一步:驱动功能实现static int first_drv_open(struct inode *inode,struct file *file){ printk("first_drv_open\n"); return 0;}static ssize_t first_drv_write(struct file *file,const char __user *buf, size_t count,loff_t *ppos){ printk("first_drv_write\n"); return 0;}//第二步:定义结构体,并把驱动函数填充进去static struct file_operations first_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = first_drv_open, .write = first_drv_write,};//第四步:实现驱动入口函数,来调用注册函数int major;static int first_drv_init(void){ //第三步:通过使用注册函数,把结构体告诉内核 major = register_chrdev(0,"first_drv",&first_drv_fops);// 注册,告诉内核 return 0;}static void first_drv_exit(void){ unregister_chrdev(major,"first_drv");//卸载}//第五步:修饰入口函数,及退出函数module_init(first_drv_init);module_exit(first_drv_exit);
三. 关联 [设备号] 与 [设备节点]
设备号要与设备结点关联起来,才能通过open(“/dev/xyz”)方便的操作。
1. 设置主设备号
驱动程序可以自动分配主设备号, 也可以手工指定
// 设置为 0 时是系统自动分配主设备号major = register_chrdev(0, "first_drv", &first_drv_fops); // 通过 [cat /proc/devices] 看一下系统为我们的first_drv分配的设备号是多少// 手动分配 666主设备号给 first_drvregister_chrdev(666, "first_drv", &first_drv_fops);
2. 设置设备节点
当应该程序 执行 open(“/dev/xyz”) 操作时,这个/dev/xyz怎么来的
2.1 手动创建
// 创建设备节点mknod /dev/xyz c(表示是字符设备) 主设备号 次设备号//查看设备信息:ls -l /dev/xyz
2.2 自动创建
mdev – 根据系统信息创建设备节点
static struct class *firstdrv_class;static struct class_device *firstdrv_class_dev;int major;static int first_drv_init(void){ major = register_chrdev(0, "first_drv", &first_drv_fops); //创建设备信息,执行后会出现 /sys/class/firstdrv firstdrv_class = class_create(THIS_MODULE, "firstdrv"); //创建设备节点,就是根据上面的设备信息来的 firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */ return 0;}static void first_drv_exit(void){ unregister_chrdev(major, "first_drv"); //删除节点及信息 class_device_unregister(firstdrv_class_dev); class_destroy(firstdrv_class);}
编译测试驱动:
驱动程序:first_drv.c
#include <linux/module.h> #include <linux/kernel.h> #include <linux/fs.h> #include <linux/init.h> #include <linux/delay.h> #include <asm/uaccess.h> #include <asm/irq.h> #include <asm/io.h> #include <asm/arch/regs-gpio.h> #include <asm/hardware.h>static struct class *firstdrv_class;static struct class_device *firstdrv_class_dev;static int first_drv_open(struct inode *inode,struct file *file){ printk("first_drv_open\n"); return 0;}static ssize_t first_drv_write(struct file *file,const char __user *buf, size_t count,loff_t *ppos){ printk("first_drv_write\n"); return 0;}static struct file_operations first_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = first_drv_open, .write = first_drv_write,};int major;static int first_drv_init(void){ major = register_chrdev(0,"first_drv",&first_drv_fops);// 注册,告诉内核 //printk("first_drv_init\n"); firstdrv_class = class_create(THIS_MODULE, "firstdrv"); firstdrv_class_dev = class_device_create(firstdrv_class, NULL, MKDEV(major, 0), NULL, "xyz"); /* /dev/xyz */ return 0;}static void first_drv_exit(void){ unregister_chrdev(major,"first_drv");//卸载 class_device_unregister(firstdrv_class_dev); class_destroy(firstdrv_class);}module_init(first_drv_init);module_exit(first_drv_exit);MODULE_LICENSE("GPL");
驱动测试程序:
#include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> /* firstdrvtest on * firstdrvtest off */int main(int argc, char **argv){ int fd; int val = 1; fd = open("/dev/xyz",O_RDWR); if(fd < 0) { printf("can't open!\n"); } write(fd,&val,4); return 0;}
Makefile:
KERN_DIR = /work/system/linux-2.6.22.6all: make -C $(KERN_DIR) M=`pwd` modules clean: make -C $(KERN_DIR) M=`pwd` modules clean rm -rf modules.orderobj-m += first_drv.o
测试步骤:
①uboot中挂载文件系统设置: bootcmd=nand read.jffs2 0x30007FC0 kernel; bootm 0x30007FC0
bootargs=noinitrd root=/dev/nfs nfsroot=192.168.2.3:/work/nfs_root/czg ip=192.168.2.5:192.168.2.3:192.168.2.1:255.255.255.0::eth0:off rootfstype=jffs2 init=/linuxrc console=ttySAC0
②将驱动程序和驱动测试程序编译好,并拷贝至NFS文件夹
makecp first_drv.ko /work/nfs_root/czg
arm-linux-gcc -o firstdrvtest firstdrvtest.ccp firstdrvtest /work/nfs_root/czg
③加载驱动程序,并测试
insmod first_drv.ko //卸载:rmmod 查看:lsmod//测试./firstdrvtest
阅读全文
1 0
- 整理--Linux字符设备驱动开发基础
- Linux字符设备驱动开发基础
- Linux设备驱动开发基础---字符设备驱动程序开发
- linux驱动开发-字符设备
- linux字符设备驱动开发
- Linux设备驱动开发基础---字符设备驱动程序开发之mini2440_ADC驱动
- Linux设备驱动开发基础---字符设备驱动程序开发之mini2440_LED驱动
- Linux设备驱动开发基础---字符设备驱动程序开发之基于中断的按键驱动
- Linux 字符设备驱动开发基础(一)—— 编写简单 LED 设备驱动
- Linux 字符设备驱动开发基础(二)—— 编写简单 PWM 设备驱动
- Linux 字符设备驱动开发基础(二)—— 编写简单 PWM 设备驱动
- Linux 字符设备驱动开发基础(一)—— 编写简单 LED 设备驱动
- Linux 字符设备驱动开发基础(一)—— 编写简单 LED 设备驱动
- Linux 字符设备驱动开发基础(二)—— 编写简单 PWM 设备驱动
- Linux 驱动开发-字符设备驱动
- 字符设备驱动 - linux驱动开发
- 【驱动】linux设备驱动·字符设备驱动开发
- Linux设备驱动开发基础
- JVM中的锁
- 标识符
- Android Kotlin —— 语言结合
- C 指针
- jdk卸载后,重新配置,启动eclipse报错
- Linux字符设备驱动开发基础
- linux使用基础
- 中国唯一的图灵奖获得者姚期智,在清华开设的“姚班”有哪些 AI 名徒?
- RTEMS USB support for BBB 文档
- NDK开发学习笔记(2):JNI访问Java中的方法
- frame:通过鼠标拉动两个子页面的宽度
- Python-Matplotlib(4) 基于真实数据集的可视化分析
- Web后端学习之——Java ServletContext 详解
- ACM_字典树模板