您的位置:首页 > 运维架构 > Linux

第一个Linux字符设备驱动程序和应用测试

2017-03-19 22:19 232 查看
1 首先写一个字符设备源文件 xxx.c   字符设备驱动程序的框架结构/*文件打开函数*/
int scull_open(struct inode *inode,struct file *filp);
 /*文件释放函数*/
 int scull_release(struct inode *inode, struct file *filp);
/*读函数*/static ssize_t scull_read(struct file *filp, char __user *buf, size_t size, loff_t *ppos);
/*写函数*/static ssize_t scull_write(struct file *filp, const char __user *buf, size_t size, loff_t *ppos);
/* seek文件定位函数 */static loff_t scull_llseek(struct file *filp, loff_t offset, int whence);
/*文件操作结构体*/static const struct file_operations scull_fops ={};
/*设备驱动模块加载函数*/static int sculldev_init(void);
/*模块卸载函数*/static void sculldev_exit(void);
2   编写头文件xxx.h3 编写Makefile完成以上3个步骤后编译make生成xxx.ko安装到系统内sudo insmod xxx.ko 查看驱动设备cat  /proc/devices前面的数字为主设备号,如果与已有设备号冲突,则需要修改主设备号创建设备节点sudo mknod xxxx c AA BB上边xxxx  代表设备驱动的名称   c 代表字符设备  AA 主设备号  BB次设备号可以用 ls -al xxxx 查看驱动设备的相关信息4 编写应用程序main.c 测试驱动程序编译并执行执行时需要加sudo
                                            
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息