本帖最后由 简单侶途 于 2015-11-16 11:14 编辑
首先,说明我的程序是查询模式的按键输入驱动程序
先看看寄存器的配置吧:
配置为输入模式input :000
pc_cfg2 &= ~( 7<<12 | 7<<16 ); /* 19--bit[14:12] 20--bit[18:16]*/
加内部上拉电阻,确保电平的稳定,按下为低电平,松开为高电平
1.驱动程序部分- #include <linux/module.h>
- #include <linux/moduleparam.h>
- #include <linux/errno.h>
- #include <linux/poll.h>
- #include <linux/sched.h>
- #include <linux/spinlock.h>
- #include <linux/slab.h>
- #include <linux/ipmi.h>
- #include <linux/mutex.h>
- #include <linux/init.h>
- #include <linux/device.h>
- #include <linux/cdev.h>
- #include <linux/compat.h>
- #include <asm/io.h>
- #include<linux/fs.h>
- #include<linux/kernel.h>
- #include<linux/uaccess.h>
- #include<linux/types.h>
- #include <asm/gpio.h>
- #include <linux/gpio.h>
- #include <plat/sys_config.h>
- #include <linux/delay.h>
- #include <asm/irq.h>
- #include <linux/moduleparam.h>
- #include <linux/spinlock.h>
- #include <linux/ipmi.h>
- #include <linux/mutex.h>
- //#define PIO_BASE 0x01c20800
- #define PC_BASE (PIO_BASE + 2*0x24)
- #define PC_CFG2 (PC_BASE + 0x08)
- #define PC_DAT (PC_BASE + 0x10)
- #define PC_PULL1 (PC_BASE + 0x20)
- static volatile unsigned int *pc_cfg2;
- static volatile unsigned int *pc_pull1;
- static volatile unsigned int *pc_dat;
- static struct class *keydrv_class;
- static struct class_device *keydrv_class_dev;
- static int major;
- /*
- * 1. Configure register PG_CFG0 enable PG0 as input pin .
- * 2. Configure register PG_PULL0 enable PG0 pull-up .
- */
- static int key_open(struct inode *inode, struct file *file)
- {
- *pc_cfg2 &= ~( 7<<12 | 7<<16 ); /* 19--bit[14:12] */
- *pc_pull1 &= ~( 3<<6 | 3<<8 ); /* 19--bit[7:6] */
- *pc_pull1 |= ( 1<<6 | 1<<8 ); /*01 is pull up*/
- return 0;
- }
- static int key_close(struct inode *inode, struct file *file)
- {
- return 0;
- }
- static ssize_t key_read(struct file *file, char __user *buf,
- size_t size,loff_t *ppos)
- {
- static char keyval[2];
- if (size != sizeof(keyval))
- return -EINVAL;
- keyval[0] = (*pc_dat & (1<<19))? 1:0;
- keyval[1] = (*pc_dat & (1<<20))? 1:0;
- copy_to_user(buf, &keyval,sizeof(keyval));
- return sizeof(keyval);
- }
- static const struct file_operations key_fops = {
- .owner = THIS_MODULE,
- .open = key_open,
- .release = key_close,
- .read = key_read,
- };
- static int keys_init(void)
- {
- major = register_chrdev(0,"key", &key_fops);
- keydrv_class = class_create(THIS_MODULE, "key");
- keydrv_class_dev = device_create(keydrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
- /* ioremap*/
- pc_cfg2 = ioremap(PC_CFG2,4);
- pc_pull1 = ioremap(PC_PULL1,4);
- pc_dat = ioremap(PC_DAT,4);
- return 0;
- }
- static void keys_exit(void)
- {
- device_destroy(keydrv_class,MKDEV(major, 0));
- class_destroy(keydrv_class);
- unregister_chrdev(major,"key");
- //注销LED设备
- iounmap(pc_cfg2);
- iounmap(pc_pull1);
- iounmap(pc_dat);
- }
- module_init(keys_init);
- module_exit(keys_exit);
- MODULE_LICENSE("GPL");
复制代码 2.应用程序部分:- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <stdio.h>
- int main(int argc, char **argv)
- {
- int fd;
- unsigned char key_val[2];
- int cnt = 0;
- fd = open("/dev/buttons", O_RDWR);
- if (fd < 0) {
- printf("can't open!\n");
- return -1;
- }
- while (1){
- read(fd,key_val, sizeof(key_val));
- if ( (key_val[0] == 0) || (key_val[1] == 0) )
- printf("%04d key pressed: %d , %d\n", cnt++, key_val[0],key_val[1]);
- }
- return 0;
- }
复制代码
|