码迷,mamicode.com
首页 > 系统相关 > 详细

linux-2.6内核驱动学习——jz2440之按键

时间:2019-01-17 10:52:53      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:and   pos   没有   自动   clu   pg1   volatil   写法   void   

//以下是学习完韦东山老师视屏教程后所做学习记录


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/irq.h> 7 #include <asm/uaccess.h> 8 #include <asm/irq.h> 9 #include <asm/io.h> 10 #include <asm/arch/regs-gpio.h> 11 #include <asm/hardware.h> 12 13 14 static struct class *thirddrv_class; 15 static struct class_device *thirddrv_class_dev; 16 17 volatile unsigned long *gpfcon; 18 volatile unsigned long *gpfdat; 19 20 volatile unsigned long *gpgcon; 21 volatile unsigned long *gpgdat; 22 23 24 static DECLARE_WAIT_QUEUE_HEAD(button_waitq); 25 26 /* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */ 27 static volatile int ev_press = 0; 28 29 30 struct pin_desc{ 31 unsigned int pin; 32 unsigned int key_val; 33 }; 34 35 36 /* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */ 37 /* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */ 38 static unsigned char key_val; 39 40 struct pin_desc pins_desc[4] = { 41 {S3C2410_GPF0, 0x01}, 42 {S3C2410_GPF2, 0x02}, 43 {S3C2410_GPG3, 0x03}, 44 {S3C2410_GPG11, 0x04}, 45 }; 46 47 48 /* 49 * 确定按键值 50 */ 51 static irqreturn_t buttons_irq(int irq, void *dev_id) 52 { 53 struct pin_desc * pindesc = (struct pin_desc *)dev_id; 54 unsigned int pinval; 55 56 pinval = s3c2410_gpio_getpin(pindesc->pin); 57 58 if (pinval) 59 { 60 /* 松开 */ 61 key_val = 0x80 | pindesc->key_val; 62 } 63 else 64 { 65 /* 按下 */ 66 key_val = pindesc->key_val; 67 } 68 69 ev_press = 1; /* 表示中断发生了 */ 70 wake_up_interruptible(&button_waitq); /* 唤醒休眠的进程 */ 71 72 73 return IRQ_RETVAL(IRQ_HANDLED); 74 } 75 76 static int third_drv_open(struct inode *inode, struct file *file) 77 { 78 /* 配置GPF0,2为输入引脚 */ 79 /* 配置GPG3,11为输入引脚 */ 80 request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]); 81 request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]); 82 request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]); 83 request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]); 84 85 return 0; 86 } 87 88 ssize_t third_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos) 89 { 90 if (size != 1) 91 return -EINVAL; 92 93 /* 如果没有按键动作, 休眠 */ 94 wait_event_interruptible(button_waitq, ev_press); 95 96 /* 如果有按键动作, 返回键值 */ 97 copy_to_user(buf, &key_val, 1); 98 ev_press = 0; 99 100 return 1; 101 } 102 103 104 int third_drv_close(struct inode *inode, struct file *file) 105 { 106 free_irq(IRQ_EINT0, &pins_desc[0]); 107 free_irq(IRQ_EINT2, &pins_desc[1]); 108 free_irq(IRQ_EINT11, &pins_desc[2]); 109 free_irq(IRQ_EINT19, &pins_desc[3]); 110 return 0; 111 } 112 113 114 static struct file_operations sencod_drv_fops = { 115 .owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */ 116 .open = third_drv_open, 117 .read = third_drv_read, 118 .release = third_drv_close, 119 }; 120 121 122 int major; 123 static int third_drv_init(void) 124 { 125 major = register_chrdev(0, "third_drv", &sencod_drv_fops); 126 127 thirddrv_class = class_create(THIS_MODULE, "third_drv"); 128 129 thirddrv_class_dev = class_device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */ 130 131 gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16); 132 gpfdat = gpfcon + 1; 133 134 gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16); 135 gpgdat = gpgcon + 1; 136 137 return 0; 138 } 139 140 static void third_drv_exit(void) 141 { 142 unregister_chrdev(major, "third_drv"); 143 class_device_unregister(thirddrv_class_dev); 144 class_destroy(thirddrv_class); 145 iounmap(gpfcon); 146 iounmap(gpgcon); 147 return 0; 148 } 149 150 151 module_init(third_drv_init); 152 153 module_exit(third_drv_exit); 154 155 MODULE_LICENSE("GPL");

测试用例

 1 #include <sys/types.h>
 2 #include <sys/stat.h>
 3 #include <fcntl.h>
 4 #include <stdio.h>
 5 
 6 /* thirddrvtest 
 7   */
 8 int main(int argc, char **argv)
 9 {
10     int fd;
11     unsigned char key_val;
12     
13     fd = open("/dev/buttons", O_RDWR);
14     if (fd < 0)
15     {
16         printf("can‘t open!\n");
17     }
18 
19     while (1)
20     {
21         read(fd, &key_val, 1);
22         printf("key_val = 0x%x\n", key_val);
23     }
24     
25     return 0;
26 }

以上是采用中断方式的写法。

下面学习了poll机制后,做了改进:

linux-2.6内核驱动学习——jz2440之按键

标签:and   pos   没有   自动   clu   pg1   volatil   写法   void   

原文地址:https://www.cnblogs.com/jason-linux/p/10280844.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!