字符设备驱动程序的传统写法
以led驱动程序为例,介绍字符设备驱动程序的传统写法。
驱动程序:
程序代码来源于韦老大视频代码
1 #include <linux/module.h> 2 #include <linux/kernel.h> 3 #include <linux/fs.h> 4 #include <linux/init.h> 5 #include <linux/delay.h> 6 #include <linux/uaccess.h> 7 #include <asm/irq.h> 8 #include <asm/io.h> 9 #include <linux/of.h> 10 #include <linux/of_device.h> 11 #include <linux/of_platform.h> 12 13 #define S3C2440_GPA(n) (0<<16 | n) 14 #define S3C2440_GPB(n) (1<<16 | n) 15 #define S3C2440_GPC(n) (2<<16 | n) 16 #define S3C2440_GPD(n) (3<<16 | n) 17 #define S3C2440_GPE(n) (4<<16 | n) 18 #define S3C2440_GPF(n) (5<<16 | n) 19 #define S3C2440_GPG(n) (6<<16 | n) 20 #define S3C2440_GPH(n) (7<<16 | n) 21 #define S3C2440_GPI(n) (8<<16 | n) 22 #define S3C2440_GPJ(n) (9<<16 | n) 23 24 static int led_pin = S3C2440_GPF(5); 25 static volatile unsigned int *gpio_con; 26 static volatile unsigned int *gpio_dat; 27 28 /* 123. 分配/设置/注册file_operations 29 * 4. 入口 30 * 5. 出口 31 */ 32 33 static int major; 34 static struct class *led_class; 35 36 static unsigned int gpio_base[] = { 37 0x56000000, /* GPACON */ 38 0x56000010, /* GPBCON */ 39 0x56000020, /* GPCCON */ 40 0x56000030, /* GPDCON */ 41 0x56000040, /* GPECON */ 42 0x56000050, /* GPFCON */ 43 0x56000060, /* GPGCON */ 44 0x56000070, /* GPHCON */ 45 0, /* GPICON */ 46 0x560000D0, /* GPJCON */ 47 }; 48 49 static int led_open (struct inode *node, struct file *filp) 50 { 51 /* 把LED引脚配置为输出引脚 */ 52 /* GPF5 - 0x56000050 */ 53 int bank = led_pin >> 16; 54 int base = gpio_base[bank]; 55 56 int pin = led_pin & 0xffff; 57 gpio_con = ioremap(base, 8); 58 if (gpio_con) { 59 printk("ioremap(0x%x) = 0x%x\n", base, gpio_con); 60 } 61 else { 62 return -EINVAL; 63 } 64 65 gpio_dat = gpio_con + 1; 66 67 *gpio_con &= ~(3<<(pin * 2)); 68 *gpio_con |= (1<<(pin * 2)); 69 70 return 0; 71 } 72 73 static ssize_t led_write (struct file *filp, const char __user *buf, size_t size, loff_t *off) 74 { 75 /* 根据APP传入的值来设置LED引脚 */ 76 unsigned char val; 77 int pin = led_pin & 0xffff; 78 79 copy_from_user(&val, buf, 1); 80 81 if (val) 82 { 83 /* 点灯 */ 84 *gpio_dat &= ~(1<<pin); 85 } 86 else 87 { 88 /* 灭灯 */ 89 *gpio_dat |= (1<<pin); 90 } 91 92 return 1; /* 已写入1个数据 */ 93 } 94 95 static int led_release (struct inode *node, struct file *filp) 96 { 97 printk("iounmap(0x%x)\n", gpio_con); 98 iounmap(gpio_con); 99 return 0; 100 } 101 102 103 static struct file_operations myled_oprs = { 104 .owner = THIS_MODULE, 105 .open = led_open, 106 .write = led_write, 107 .release = led_release, 108 }; 109 110 111 static int myled_init(void) 112 { 113 major = register_chrdev(0, "myled", &myled_oprs); 114 115 led_class = class_create(THIS_MODULE, "myled"); 116 device_create(led_class, NULL, MKDEV(major, 0), NULL, "led"); /* /dev/led */ 117 118 return 0; 119 } 120 121 static void myled_exit(void) 122 { 123 unregister_chrdev(major, "myled"); 124 device_destroy(led_class, MKDEV(major, 0)); 125 class_destroy(led_class); 126 } 127 128 module_init(myled_init); 129 module_exit(myled_exit); 130 131 132 MODULE_LICENSE("GPL");
2、测试程序
1 #include <sys/types.h> 2 #include <sys/stat.h> 3 #include <fcntl.h> 4 #include <stdio.h> 5 6 /* ledtest on 7 * ledtest off 8 */ 9 int main(int argc, char **argv) 10 { 11 int fd; 12 unsigned char val = 1; 13 fd = open("/dev/led", O_RDWR); 14 if (fd < 0) 15 { 16 printf("can't open!\n"); 17 } 18 if (argc != 2) 19 { 20 printf("Usage :\n"); 21 printf("%s <on|off>\n", argv[0]); 22 return 0; 23 } 24 25 if (strcmp(argv[1], "on") == 0) 26 { 27 val = 1; 28 } 29 else 30 { 31 val = 0; 32 } 33 34 write(fd, &val, 1); 35 return 0; 36 }
转载于:https://www.cnblogs.com/-glb/p/11210461.html
字符设备驱动程序的传统写法相关推荐
- Linux驱动实践:你知道【字符设备驱动程序】的两种写法吗?
作 者:道哥,10+年嵌入式开发老兵,专注于:C/C++.嵌入式.Linux. 关注下方公众号,回复[书籍],获取 Linux.嵌入式领域经典书籍:回复[PDF],获取所有原创文章( PDF 格式). ...
- 字符设备驱动程序框架
1, 设备号的内部表示形式 类型:dev_t 32=12(主设备号) + 20(次设备号) 相关宏:<linux/kdev_t.h> MAJOR(dev_t dev) MINOR(dev_ ...
- i.MX6ULL学习笔记--字符设备驱动程序
i.MX6ULL学习笔记--字符设备驱动程序 简介 1.驱动的配置过程 1.1设备号 1.2哈希表-chrdevs 1.3哈希表-obj_map->probes 1.4文件操作接口 1.5简单了 ...
- 第12课第3节 字符设备驱动程序之查询方式的按键驱动程序
第12课第3节 字符设备驱动程序之查询方式的按键驱动程序 cat /proc/devices //查询主设备号 insmod ./second_drv.ko ls /dev/button -l pos ...
- 字符设备驱动0:一个简单但完整的字符设备驱动程序
参考: linux设备驱动程序之简单字符设备驱动 [很详细,必看]http://www.cnblogs.com/geneil/archive/2011/12/03/2272869.html //在驱动 ...
- 第12课第2.2节 字符设备驱动程序之LED驱动程序_测试改进
第12课第2.2节 字符设备驱动程序之LED驱动程序_测试改进 //仅用flash上的根文件系统启动后,手工MOUNT NFS mount -t nfs -o nolock,vers=2 192.16 ...
- 字符设备驱动程序——点亮、熄灭LED操作
2019独角兽企业重金招聘Python工程师标准>>> 字符设备:是指只能一个字节一个字节读写的设备,不能随机读取设备内存中的某一数据,读取数据需要按照先后数据.字符设备是面向流的设 ...
- 字符设备驱动程序的使用
1.编译.安装驱动 linux系统中,驱动程序通常采用内核模块的程序结构来进行编码,因此,编译.安装一个驱动程序,其实质就是编译.安装一个内核模块. 将文件memdev.c makefile 放入虚拟 ...
- Linux字符设备驱动程序开发(1)-使用字符设备驱动
1.编译/安装驱动 在Linux系统中,驱动程序通常采用内核模块的程序结构来进行编码.因此,编译/安装一个驱动程序,其实质就是编译/安装一个内核模块.把下面的范例代码拷贝到Linux系统中: memd ...
最新文章
- 阿里某员工面试华为后吐槽:面试官太水,反问几句都答不上来
- String、String Buffer、StringBuilder区别与比较
- Mac 系统引导过程概述 BootCamp 的秘密
- python对英语的要求_学python需要英语基础吗
- 49 FI配置-财务会计-固定资产-与总账集成-分配总帐科目
- 对'\0'的敬畏——由阶乘想到的
- vSphere 7 With Kubernetes系列01:随想
- CyclicBarrier底层实现和原理
- 22年前,100万买入谷歌原始股,奥尼尔的股份如今市值多少?
- Android viewpager 嵌套 viewpager滑动 点击事件冲突解决方案
- table表格字母无法换行
- 问题查询-批文页面显示别人操作的结果
- 【LuoguP4770】[NOI2018] 你的名字
- 【一路走来】北大数字媒体所保研面经
- 12306网站火车票抢票详细攻略(gohome抢票程序)
- 抗变态或亲变态是更好的解决方案
- 如何为你的网站植入广告,赚取收益呢GoogleAdsense首选
- Android 收银机Wifi 连接厨房厨单打印机
- java中的命名规则(超详细~~),变量、类名、方法名
- 第一课:数据库的基本操作(表的创建、删除、修改、重命名、主键、截断表)