---恢復內容開始---
1.1 電路原理圖
S1-S5共5個按鍵,其中,S2-S4為中斷按鍵,S1為復位按鍵。S1直接為硬件復位電路,並不需要我們寫進驅動。
單片機接口如下圖:
由圖中可以看出,EINT0、EINT2和EINT11作為輸出引腳,EINT19是作為輸入引腳。
EINT0和EINT2對應的GPIO引腳為GPF0和GPF2;EINT11和EINT19對應的GPIO引腳為GPF3和GPF11.
作為查詢方式使用,就不使用中斷來用,則將引腳定義為輸入狀態。
1.2 對應的寄存器配置
1.2.1 GPF引腳:
GPF引腳主要對應三個寄存器,GPFCON,GPFDAT和GPFUP。
- GPFCON:配置F引腳的寄存器
- GPFDAT:F引腳的數據寄存器
- GPFUP:F引腳的上拉使能寄存器
GPF0和GPF2引腳可配置的屬性如下:
GPF0和GPF2作為輸入在使能,則應將其配置為00。
1.2.2 GPG引腳
GPG引腳類似GPF引腳。
二、代碼
2.1 驅動代碼
1 /*
2 * =====================================================================================
3 * Filename: key.c
4 * Description:
5 * Version: 1.0
6 * Created: 2017年05月24日 15時39分34秒
7 * Author: YOUR NAME (),
8 * Organization:
9 * =====================================================================================
10 */
11
12 #include <linux/module.h>
13 #include <linux/kernel.h>
14 #include <linux/fs.h>
15 #include <linux/init.h>
16 #include <linux/delay.h>
17 #include <asm/irq.h>
18 #include <linux/interrupt.h>
19 #include <asm/uaccess.h>
20 #include <asm/arch/regs-gpio.h>
21 #include <asm/hardware.h>
22
23 #define DEVICE_NAME "keys"
24 #define KEY_MAJOR 232
25
26 static struct class *keys_class;
27 static struct class_device *keys_class_dev[4];
28 static unsigned long gpio_va; //gpio物理地址映射為虛擬地址變量
29 #define GPIO_OFT(x) ((x) - 0x56000000)
30 /* GPF引腳物理地址映射 */
31 #define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))
32 #define GPFDAT (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))
33 /* GPF引腳物理地址映射 */
34 #define GPGCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000060)))
35 #define GPGDAT (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000064)))
36
37
38 static int keys_open(struct inode *inode, struct file *filp)
39 {
40
41 /* 配置GPF0,2為輸入引腳 */
42 GPFCON &= ~(0x3 << (0 * 2) | 0x3 << (2 * 2));
43 /* 配置GPG3,11為輸入引腳 */
44 GPGCON &= ~(0x3 << (3 * 2) | 0x3 << (11 * 2));
45 return 0;
46 }
47
48 static int keys_close(struct inode *inode, struct file *filp)
49 {
50
51
52 return 0;
53 }
54
55 static ssize_t keys_read(struct file *filp, char __user *buff, size_t count, loff_t *oops)
56 {
57 /* 返回4個引腳的電平 */
58 unsigned char key_vals[4];
59 int regval;
60
61 if (count != sizeof(key_vals))
62 return -EIAVAL;
63
64 /* 讀GPF0,2 */
65 regval = GPFDAT;
66 key_vals[0] = (regval & (1 << 0)) ? 1 : 0;
67 key_vals[1] = (regval & (1 << 2)) ? 1 : 0;
68
69 /* 讀GPG3,11 */
70 regval = GPGDAT;
71 key_vals[2] = (regval & (1 << 3)) ? 1 : 0;
72 key_vals[3] = (regval & (1 << 12)) ? 1 : 0;
73
74 copy_to_user(buf, key_val, sizeof(key_vals));
75
76 return sizeof(key_vals);
77 return 0;
78 }
79
80 static struct file_operations keys_fops = {
81 .owner = THIS_MODULE, /* 這是一個宏,指向編譯模塊時自動創建的__this_module變量 */
82 .open = keys_open,
83 .release = keys_close,
84 .read = keys_read,
85 };
86
87 static int __init keys_init(void)
88 {
89 int ret;
90 int minor;
91
92 gpio_va = ioremap(0x56000000, 0x100000);//物理地址映射為虛擬地址,分配1M空間
93 if (!gpio_va)
94 return -EIO;
95
96 ret = register_chrdev(KEY_MAJOR, DEVICE_NAME, &key_fops);
97 if(ret < 0)
98 {
99 printk(DEVICE_NAME " can't register major number\n");
100 return ret;
101 }
102
103 /* 設備類的創建 */
104 keys_class = class_create(THIS_MODULE, DEVICE_NAME);
105 if (IS_ERR(keys_class))
106 {
107 return PTR_ERR(keys_class);
108 }
109
110 for (minor = 0; minor < 4; minor++)
111 {
112 keys_class_dev[minor] = class_device_create(leds_class, NULL, MKDEV(KEY_MAJOR, minor), NULL, "key%d", minor);
113 if (unlikely(IS_ERR(keys_class_dev[minor])))
114 return PTR_ERR(keys_class_dev[minor]);
115 }
116
117 printk(DEVICE_NAME " initialized\n");
118 return 0;
119 }
120
121 static void __exit keys_exit(void)
122 {
123 int minor;
124 for (minor = 0; minor < 4; minor++)
125 {
126 class_device_unregister(keys_class_dev[minor]);
127 }
128 class_destroy(keys_class);
129 unregister_chrdev(KEY_MAJOR, DEVICE_NAME);
130 iounmap(gpio_va);
131 }
132
133 module_init(keys_init);
134 module_exit(keys_exit);
135 MODULE_LICENSE("Dual BSD/GPL");
View Code
2.2 測試代碼
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <unistd.h>
4 #include <sys/ioctl.h>
5
6 int main(void)
7 {
8 int fd;
9 unsigned char key_vals[4];
10 int cnt = 0;
11
12 fd = open("/dev/keys", O_RDWR);
13 if (fd < 0)
14 {
15 printf("can't open!!!\n");
16
17 while (1)
18 {
19 read(fd, key_vals, sizeof(key_vals));
20 if (!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3])
21 {
22 printf("%04d key pressed: %d %d %d %d\n", cnt++, key_vals[0], key_vals[1], key_vals[2], key_vals[3]);
23 }
24 }
25 }
26
27 return 0;
28 }
View Code
---恢復內容結束---