linux4.10.8 内核移植(四)---字符设备驱动_led驱动程序
一、字符设备驱动程序介绍
app里面用 open、read、write等等函数出来操作底层硬件。驱动程序中也有对应的xxx_open等函数。怎么找到驱动程序中的函数依赖于驱动程序框架。
二、搭建驱动程序框架
2.1 初步框架
2.1.1 Makefile
2.1.2 jz2440_led.c
1 #include <linux/module.h>
2 #include <linux/of.h>
3 #include <linux/of_device.h>
4 #include <linux/regmap.h>
5 #include <linux/fs.h>
6
7 #define JZ2440_LEDS_MAJOR 111 /* 主设备号 */
8
9
10 static int jz2440_drv_open(struct inode *inode, struct file *file)
11 {
12 printk("jz2440_drv_open");
13 return 0;
14 }
15
16 static int jz2440_drv_close(struct inode *inode, struct file *file)
17 {
18 printk("jz2440_drv_close");
19 return 0;
20 }
21
22 static ssize_t jz2440__drv_read(struct file *file, char __user * buf, size_t count, loff_t * ppos)
23 {
24 printk("jz2440__drv_read");
25 return 0;
26 }
27
28 static ssize_t jz2440_drv_write(struct file *file, const char __user * buf, size_t count, loff_t * ppos)
29 {
30 printk("jz2440_drv_write");
31 return 0;
32 }
33
34 static struct file_operations jz2440_leds_ops = {
35 .owner = THIS_MODULE,
36 .read = jz2440__drv_read,
37 .open = jz2440_drv_open,
38 .write = jz2440_drv_write,
39 .release = jz2440_drv_close,
40 };
41
42 static int __init jz2440_drv_leds_init(void)
43 {
44 register_chrdev(JZ2440_LEDS_MAJOR,"jz2440_drv_leds",&jz2440_leds_ops);//注册驱动程序,即告诉内核
45 return 0;
46 }
47
48 static void __exit jz2440_drv_leds_exit(void)
49 {
50 unregister_chrdev(JZ2440_LEDS_MAJOR,"jz2440_drv_leds");
51 }
52
53 //指向入口函数 jz2440_drv_leds_init
54 module_init(jz2440_drv_leds_init);
55 //出口函数 卸载驱动
56 module_exit(jz2440_drv_leds_exit);
57
58 MODULE_LICENSE("GPL");
59 MODULE_DESCRIPTION("jz2440 leds driver");
编译:
烧写运行:
进入开发板u-boot界面:
设置命令行启动参数
set bootargs noinitrd root=/dev/nfs nfsroot=192.168.0.192:/home/ubuntu/work/nfs_root/fs_mini ip=192.168.0.191:192.168.0.192:192.168.0.1:255.255.255.0::eth0:off init=/linuxrc console=ttySAC0,115200
启动内核:boot
nfs目录挂载成功。
查看proc目录:
设备号可用。
加载模块:
查看是否加载进去:
测试程序
1 #include <sys/types.h>
2 #include <sys/stat.h>
3 #include <fcntl.h>
4 #include <stdio.h>
5 #include <unistd.h>
6
7 int main(int argc, char **argv)
8 {
9 int fd;
10 int val = 1;
11
12 fd = open("/dev/xxx", O_RDWR);
13 if(fd < 0)
14 {
15 printf("can't open !\n");
16 }
17
18 write(fd, &val, 4);
19 return 0;
20 }
编译烧写进板子,执行
创建设备节点;
执行 a.out
卸载驱动 rmmod jz2440_led
删除节点: rm -rf /dev/xxx