第12课第2.1节 字符设备驱动程序之LED驱动程序_编写编译
来源:互联网 发布:linux debug 编辑:程序博客网 时间:2024/04/29 04:17
一.开始写驱动的思路
1.入口函数(驱动从哪里开始执行)
普通C代码的入口函数是mani(),而驱动的入口函数格式是static int 名字_init(void)
例子:static int first_drv_init(void)
2.修饰入口函数(我怎么才知道哪个是入口函数)
module_init(名字_init);//本质是定义一个结构体,这个结构体里面有一个指向入口函数(名字_init)的函数指针
例子:
module_init(first_drv_init);
3.注册驱动(具体实现驱动功能的函数放在一个叫file_openreations的结构体中,再将这个结构体注册告诉内核)
注册一般在入口函数里面调用int register_chrdev( unsignedintmajor, constchar*name, structfile_operations*fops);完成
第一个参数:组设备号,写0由系统自动分配.
第二个参数:驱动名
第三个参数:file_openreations结构体地址
例子:
major = register_chrdev(0, "first_drv", &first_drv_fops);
4.定义一个file_openreations结构体
static struct file_operations first_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = first_drv_open, /*实现open的功能*/
.write = first_drv_write, /*实现write的功能*/
};
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = first_drv_open, /*实现open的功能*/
.write = first_drv_write, /*实现write的功能*/
};
5.写出实现功能的函数
例子:
static int first_drv_open(struct inode *inode, struct file *file)
static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos)
6.出口函数
int 名字_init(void):static void first_drv_exit(void)
出口函数用来做收尾工作,比如取消入口函数register_chrdev()的注册,用unregister_chrdev(major, "first_drv"); 实现卸载
第一个参数:组设备号
第二个参数:驱动名
static void first_drv_exit(void)
7.修饰出口函数
module_exit(first_drv_exit);
二.一个字符设备驱动例子
#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>/*实现open*/static int first_drv_open(struct inode *inode, struct file *file){}/*实现write*/static ssize_t first_drv_write(struct file *file, const char __user *buf, size_t count, loff_t * ppos){}/*定义<span style="font-family: Arial, Helvetica, sans-serif;">file_operations结构体</span>*/static struct file_operations first_drv_fops = { .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ .open = first_drv_open, .write =first_drv_write, };int major;//组设备号/*major作用在VFS(虚拟文件系统)中实际是用一个数组来管理文件,而这个数组就是以组设备号来所谓索引在注册驱动的时候register_chrdev函数会按照major作为索引在VFS对应位置填入file_operation*/static int first_drv_init(void){major = register_chrdev(0, "first_drv", &first_drv_fops); // 注册, 告诉内核.写0是系统自动分配组设备号(找空缺的组设备号)}static void first_drv_exit(void){unregister_chrdev(major, "first_drv"); // 卸载}module_init(first_drv_init);module_exit(first_drv_exit);MODULE_LICENSE("GPL");//需要写这个,我已不知道嘛意思
0 0
- 第12课第2.1节 字符设备驱动程序之LED驱动程序_编写编译
- 第12课第2.3节 字符设备驱动程序之LED驱动程序_操作LED
- 第12课第2.2节 字符设备驱动程序之LED驱动程序_测试改进
- 第12课第4.3节 字符设备驱动程序之中断方式的按键驱动_编写代码
- 第12课第3节 字符设备驱动程序之查询方式的按键驱动程序
- 第一篇 字符设备驱动程序之LED流水灯驱动
- 1_字符设备驱动程序之LED驱动程序
- 字符设备驱动程序的编写_点亮LED灯
- 字符设备驱动程序之LED驱动程序
- 第12课第1节 字符设备驱动程序之概念介绍
- 第12课第5节 字符设备驱动程序之poll机制
- 第12课第6节 字符设备驱动程序之异步通知
- LED字符设备驱动程序
- 第12课第4.1节 字符设备驱动程序之中断方式的按键驱动_Linux异常处理结构
- 第12课第4.2节 字符设备驱动程序之中断方式的按键驱动_Linux中断处理结构
- 第16章 驱动开发之字符设备驱动程序框架
- 第17章 驱动开发之字符设备驱动程序
- 字符设备驱动----LED驱动程序
- WaitHandle.WaitOne 方法 ()
- 10条PHP高级技巧
- 对3D实体化的另一种构思
- 快速傅里叶介绍-matlab
- 布局中singleLine属性导致viewpager滑动失效
- 第12课第2.1节 字符设备驱动程序之LED驱动程序_编写编译
- opnfv的B版本特色
- 海量数据处理之数据库索引
- 手机访问智能硬件的三种模式
- gson用户指南(上)
- iOS音频技术的研究-音频格式
- 导出war包时报“SECURITY ALERT: INTEGERITY CHECK ERROR”进行了破解
- Mybatis 优缺点,配置,使用,结合Spring使用,常见错误,代码自动生成(草稿)
- C++强大背后