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

Posted Jason_linux

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了linux-2.6内核驱动学习——jz2440之按键相关的知识,希望对你有一定的参考价值。

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


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!
");
17     }
18 
19     while (1)
20     {
21         read(fd, &key_val, 1);
22         printf("key_val = 0x%x
", key_val);
23     }
24     
25     return 0;
26 }

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

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




以上是关于linux-2.6内核驱动学习——jz2440之按键的主要内容,如果未能解决你的问题,请参考以下文章

内核搭建完整的mini2440开发板驱动开发环境(仿照JZ2440驱动开发环境搭建)

一,Linux-3.19内核移植DM9000驱动(JZ2440)

JZ2440_V3_内核驱动程序_点亮一个LED灯

mini2440 官方linux-2.6内核文件zImage编译

基于JZ2440开发板编写bootloader总结

jz2440上内核和文件系统移植