一、原理图
二、中断表
三、设备树
三、驱动程序
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#define DeviceName "devicetree-key0-interrupt"
#define DeviceNodes 1
#define KEY_NUM 0X01
#define KEY0VALUE 0X01
#define INVAKEY 0XFF
struct irq_keydesc{
int gpio_num;
int irq_num;
unsigned char value;
char name[25];
irqreturn_t (*handler) (int, void *);
};
struct mod_struct {
dev_t devid;
int major;
int minor;
struct cdev cdev;
struct class *class;
struct device *device;
struct device_node *nd;
struct irq_keydesc irqkey0;
struct timer_list timer;
atomic_t keyvalue;
atomic_t releasekey;
};
struct mod_struct mod_device;
static int key0_interrupt_open(struct inode *inode, struct file *filp)
{
filp->private_data = &mod_device;
return 0;
}
static ssize_t key0_interrupt_read(struct file *filp, char __user *buf, size_t cnt, loff_t *offt)
{
int ret = 0;
unsigned char keyvalue;
unsigned char releasekey;
struct mod_struct *dev = filp->private_data;
keyvalue = atomic_read(&dev->keyvalue);
releasekey = atomic_read(&dev->releasekey);
if(releasekey) {
if(keyvalue & 0x80) {
keyvalue &= ~0x80;
ret = copy_to_user(buf, &keyvalue, sizeof(keyvalue));
} else {
goto data_error;
}
atomic_set(&dev->releasekey, 0);
} else {
goto data_error;
}
return ret;
data_error:
return -EINVAL;
}
static ssize_t key0_interrupt_write(struct file *filp, const char __user *buf,size_t cnt, loff_t *offt)
{
return 0;
}
static int key0_interrupt_release(struct inode *inode, struct file *filp)
{
return 0;
}
static struct file_operations beep_fops = {
.owner = THIS_MODULE,
.open = key0_interrupt_open,
.read = key0_interrupt_read,
.write = key0_interrupt_write,
.release = key0_interrupt_release,
};
static irqreturn_t key0_handler(int irq, void *dev_id)
{
struct mod_struct *dev = dev_id;
dev->timer.data = (volatile long)dev_id;
mod_timer(&dev->timer, jiffies + msecs_to_jiffies(20));
return IRQ_HANDLED;
}
static void timer_func(unsigned long arg) {
int value = 0;
struct mod_struct *dev = (struct mod_struct*)arg;
value = gpio_get_value(dev->irqkey0.gpio_num);
if(value == 0) {
atomic_set(&dev->keyvalue, dev->irqkey0.value);
} else if(value == 1) {
atomic_set(&dev->keyvalue, 0X80 | (dev->irqkey0.value));
atomic_set(&dev->releasekey, 1);
}
}
static int key_init(struct mod_struct *dev)
{
int ret = 0;
dev->nd = of_find_node_by_path("/devicetree-key0-interrupt");
if(dev->nd == NULL) {
printk("devicetree-key0-interrupt node not find!\r\n");
ret = -EINVAL;
goto fail_fd;
} else {
printk("devicetree-key0-interrupt node find!\r\n");
}
dev->irqkey0.gpio_num = of_get_named_gpio(dev->nd,"key-gpio", 0);
if(dev->irqkey0.gpio_num < 0) {
printk("can't get key-gpio\r\n");
ret = -EINVAL;
goto fail_gpio;
}
printk("key-gpio num = %d\r\n", dev->irqkey0.gpio_num);
sprintf(dev->irqkey0.name,"%s","key0-gpio-interrupt");
ret = gpio_request(dev->irqkey0.gpio_num,dev->irqkey0.name);
if(ret)
{
ret = -EBUSY;
printk("IO %d busy,can't request!\r\n",dev->irqkey0.gpio_num);
goto fail_ioreq;
}
ret = gpio_direction_input(dev->irqkey0.gpio_num);
if(ret < 0) {
printk("can't set gpio!\r\n");
goto fail_ioset;
}
dev->irqkey0.irq_num=gpio_to_irq(dev->irqkey0.gpio_num);
#if 0
dev->irqkey0.irq_num = irq_of_parse_and_map(dev->nd,0);
#endif
dev->irqkey0.handler = key0_handler;
dev->irqkey0.value = KEY0VALUE;
ret = request_irq(dev->irqkey0.irq_num,
dev->irqkey0.handler,
IRQF_TRIGGER_RISING|IRQF_TRIGGER_FALLING,
dev->irqkey0.name,
&mod_device);
if(ret){
printk("irq %d request failed!\r\n", dev->irqkey0.irq_num);
goto fail_irq;
}
init_timer(&mod_device.timer);
mod_device.timer.function = timer_func;
return 0;
fail_irq:
fail_ioset:
gpio_free(dev->irqkey0.gpio_num);
fail_ioreq:
fail_gpio:
fail_fd:
return ret;
}
static int __init key0_interrupt_init(void)
{
int ret = 0;
if (mod_device.major)
{
mod_device.devid = MKDEV(mod_device.major,0);
ret = register_chrdev_region(mod_device.devid,DeviceNodes,DeviceName);
}
else {
ret = alloc_chrdev_region(&mod_device.devid,0,DeviceNodes,DeviceName);
mod_device.major = MAJOR(mod_device.devid);
mod_device.minor = MINOR(mod_device.devid);
}
if(ret < 0){ printk("设备号申请失败\r\n");goto fail_chrdev; }
printk("mod_device major=%d,minor=%d\r\n",mod_device.major,mod_device.minor);
mod_device.cdev.owner = THIS_MODULE;
cdev_init(&mod_device.cdev,&beep_fops);
ret = cdev_add(&mod_device.cdev,mod_device.devid,DeviceNodes);
if(ret < 0){ printk("添加cdev失败!\r\n");goto fail_cdev; }
mod_device.class = class_create(THIS_MODULE,DeviceName);
if (IS_ERR(mod_device.class)) { ret = PTR_ERR(mod_device.class);goto fail_class; }
mod_device.device = device_create(mod_device.class,NULL,mod_device.devid,NULL,"pinctrl-key0-interrupt");
if (IS_ERR(mod_device.device)) { ret = PTR_ERR(mod_device.device);goto fail_device; }
ret = key_init(&mod_device);
if(ret < 0) {
goto fail_keyinit;
}
atomic_set(&mod_device.keyvalue, INVAKEY);
atomic_set(&mod_device.releasekey, 0);
return 0;
fail_keyinit:
device_destroy(mod_device.class,mod_device.devid);
fail_device:
class_destroy(mod_device.class);
fail_class:
cdev_del(&mod_device.cdev);
fail_cdev:
unregister_chrdev_region(mod_device.devid,DeviceNodes);
fail_chrdev:
return ret;
}
static void __exit key0_interrupt_exit(void)
{
free_irq(mod_device.irqkey0.irq_num, &mod_device);
del_timer_sync(&mod_device.timer);
gpio_free(mod_device.irqkey0.gpio_num);
device_destroy(mod_device.class,mod_device.devid);
class_destroy(mod_device.class);
cdev_del(&mod_device.cdev);
unregister_chrdev_region(mod_device.devid,DeviceNodes);
}
module_init(key0_interrupt_init);
module_exit(key0_interrupt_exit);
MODULE_LICENSE("GPL");
四 、APP测试程序
#include
#include
#include
#include
#include
#include
#include
#include
int main(int argc, char *argv[])
{
int fd, ret;
char *filename;
unsigned char data;
if(argc != 2) {
printf("Error Usage!\r\n");
return -1;
}
filename = argv[1];
fd = open(filename, O_RDWR);
if(fd < 0) {
printf("file %s open failed!\r\n", filename);
return -1;
}
while(1) {
ret = read(fd, &data, sizeof(data));
if(ret < 0) {
} else {
if(data)
printf("key value = %#x\r\n", data);
}
}
close(fd);
return 0;
}
五、实验现象
按键按下一次,APP输出一次0x01