通过中断读取按键值
使用循环读取按键值,需要应用程序一直查询设备节点,cpu占用率高。
可以通过应用程序在read函数上休眠,当有按键中断时,唤醒应用程序读取按键值,读取之后继续在read函数上休眠,这样可以极大地减少CPU占用率。
如何将GPIO配置成中断功能
高通平台可以用gpio_to_irq()函数将GPIO配置成中断功能,该函数同时返回中断号
使用返回的中断号,通过request_threaded_irq函数来注册中断处理函数
如何休眠
使用wait_event_interruptible()函数休眠
如何唤醒
使用wake_up_interruptible()函数唤醒
参考代码
#include <linux/module.h>
#include <linux/init.h>
#include <linux/major.h>
#include <linux/cdev.h>
#include <linux/device.h>
#include <linux/types.h>
#include <linux/fs.h>
#include <asm/uaccess.h>
#include <linux/io.h>
#include <linux/sched.h>
//#include <asm/irq.h>
#include <linux/gpio.h>
#include <linux/interrupt.h>
#define TAG "keyvol"
#define INT_GPIO 91
static int key_major;
static struct cdev key_cdev;
static struct class *key_class;
volatile unsigned long *tlmm_gpio_cfg;
volatile unsigned long *tlmm_in_out;
static int key_press = 0;
static int irq;
static wait_queue_head_t kwait;
static irqreturn_t key_irq_thread(int irq, void *data)
{
int value;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
value = *tlmm_in_out;
value &= 0x1;
if (!value ) {
key_press = 1;
wake_up_interruptible(&kwait);
}
return IRQ_HANDLED;
}
static ssize_t key_read(struct file *file, char __user *buffer,
size_t count, loff_t *ppos)
{
if(count !=1 )
return -EINVAL;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
if (!key_press) {
wait_event_interruptible(kwait, key_press);
}
if(key_press) {
if(!copy_to_user(buffer, &key_press, 1)) {
printk(TAG"%s key is press\n", __func__);
key_press = 0;
} else {
printk(TAG"%s copy to user error\n", __func__);
return -EFAULT;
}
}
return count;
}
ssize_t key_write(struct file *file, const char __user *buf, size_t count, loff_t *ppos)
{
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
return count;
}
static int key_open(struct inode *inode, struct file *file)
{
int ret;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
irq = gpio_to_irq(INT_GPIO);
printk(TAG"%s irq is %d\n", __func__, irq);
ret = request_threaded_irq(irq, NULL, key_irq_thread, IRQF_TRIGGER_FALLING | IRQF_ONESHOT, "vol_key", NULL);
printk(TAG"%s ret is %d\n", __func__, ret);
return ret;
}
static int key_release(struct inode *inode, struct file *file)
{
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
free_irq(irq, NULL);
return 0;
}
static const struct file_operations key_ops = {
.owner = THIS_MODULE,
.read = key_read,
.write = key_write,
.open = key_open,
.release = key_release,
};
static int my_key_init(void)
{
int retval;
dev_t dev_id;
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
retval = alloc_chrdev_region(&dev_id, 0, 1, "key"); //0,1
key_major = MAJOR(dev_id);
printk(TAG"major is %d\n", key_major);
if (retval < 0) {
printk(TAG"can't get major number\n");
goto error;
}
cdev_init(&key_cdev, &key_ops);
retval = cdev_add(&key_cdev, dev_id, 1); //1
if (retval < 0) {
printk(TAG"cannot add cdev\n");
goto cleanup_alloc_chrdev_region;
}
key_class = class_create(THIS_MODULE, "key");
if (IS_ERR(key_class)) {
printk(TAG "Error creating key class.\n");
cdev_del(&key_cdev);
retval = PTR_ERR(key_class);
goto cleanup_alloc_chrdev_region;
}
device_create(key_class, NULL, MKDEV(key_major, 0), NULL, "keyvol");
tlmm_gpio_cfg = (volatile unsigned long *)ioremap(0x105B000, 8);
tlmm_in_out = tlmm_gpio_cfg + 1;
*tlmm_gpio_cfg |= 0x3;
init_waitqueue_head(&kwait);
return 0;
cleanup_alloc_chrdev_region:
unregister_chrdev_region(dev_id, 0);
error:
return retval;
}
static void key_exit(void)
{
dev_t dev_id = MKDEV(key_major, 0);
iounmap(tlmm_gpio_cfg);
device_destroy(key_class, MKDEV(key_major, 0));
class_destroy(key_class);
cdev_del(&key_cdev);
unregister_chrdev_region(dev_id, 0);
printk(TAG" func:%s line:%d\n", __func__, __LINE__);
}
module_init(my_key_init);
module_exit(key_exit);
MODULE_LICENSE("GPL");
编写测试程序
打开设备节点后,通过read系统调用读取按键的状态
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <stdlib.h>
int main(int argc, char **argv)
{
int fd;
char buf;
int count = 0;
if(argc != 2) {
printf("Usage: %s [node]\n", argv[0]);
exit(-1);
}
fd = open(argv[1], O_RDWR);
if (fd < 0) {
printf("open %s failed\n", argv[1]);
exit(-1);
}
while (1) {
read(fd, &buf, 1);
//printf("buf is %d\n", buf);
if (buf == 0) {
printf("key is release, count is %d\n", ++count);
} else if(buf == 1) {
printf("key is press, count is %d\n", ++count);
} else {
printf("read WRONG value\n");
}
//usleep(500000);
}
close(fd);
return 0;
}
假如编译出的测试程序为testkey,运行测试程序
./testkey /dev/keyvol
未按按键,即松开按键时会打印一次key is release,按下后,会打印一次key is press,说明正确读到了按键状态的值
如果使用top观察,可以看出测试程序的CPU占用率明显降低