【韦东山驱动代码移植高通平台之五】读取按键值

硬件电路分析

查看原理图可知该按键使用的引脚是GPIO91,配置时将GPIO91上拉,因为按键的另一个触点是接地的,所以按下时GPIO会接地,电平为0,松开时,电平为1

 

datasheet关于gpio的说明

 

当将GPIO配置成输入模式并且上拉

也就是将0x01000000+0x1000*91=0x105B000寄存器设置成0x03

然后读取0x01000004+0x1000*91=0x105B004寄存器的值

 

使用 /system/bin/r观察寄存器的值

/system/bin/r 0x105B000

0105b000: 00000003

按下的值

/system/bin/r 0x105B004

0105b004: 00000000

松开的值

/system/bin/r 0x105B004

0105b004: 00000001

 

读取按键值驱动说明

将寄存器通过ioremap进行映射,然后就可以在驱动里操作(比如读取或者写入)该寄存器

参考代码

#include <linux/init.h>
#include <linux/module.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>


#define TAG "keyvol "

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 ssize_t key_read(struct file *file, char __user *buffer,
		size_t count, loff_t *ppos)
{
	int value;

	printk(TAG" func:%s line:%d\n", __func__, __LINE__);

	value = *tlmm_in_out;
	value &= 0x1;

    if(!copy_to_user(buffer, &value, 1)) {
        if (value ) {
            printk(TAG"%s key is release\n", __func__);
        } else {
            printk(TAG"%s key is press\n", __func__);
        }
    } 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)
{
	printk(TAG" func:%s line:%d\n", __func__, __LINE__);

	return 0;
}

static int key_release(struct inode *inode, struct file *file)
{

	printk(TAG" func:%s line:%d\n", __func__, __LINE__);

	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 mykey_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;

	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(mykey_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 == 1) {
    		printf("key is release, count is %d\n", ++count);
    	} else if(buf == 0) {
    		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,说明正确读到了按键状态的值

 

问题

通过死循环读取, 太浪费cpu了,可以通过top指令观察测试程序占用cpu状况

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值