Hi3519v101gpio字符驱动-按键查询

10 篇文章 0 订阅
5 篇文章 0 订阅

1、key_drv.c

#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/device.h>
#include <asm/io.h>
#include <linux/init.h>
#include <linux/platform_device.h>
#include <linux/miscdevice.h>
#include <linux/ioport.h>
#include <linux/of.h>
#include <linux/uaccess.h>

//主设备号
int major;

//udev自动创建节点
static struct class *keydrv_class;
static struct class_device *keydrv_class_dev;

// 操作管脚为gpio14_2
// 该管脚采用boot_sel[1:0]硬件配置复用功能,现单板已经配置为gpio,默认高电平,按键按下为低电平
static void __iomem *key_base; //寄存器基地址
static void __iomem *key_dir;  //方向

#define key_phy_base 0x1214E000 //key管脚物理地址
#define key_phy_data 0x010      //key管脚数据偏移地址,0b00_0001_0000

#define gpio_phy_dir 0x400 //gpio管脚方向寄存器偏移地址
#define gpio_in 0x00       //配置为输入,1输出 0输入def
#define gpio_out_h 0xFF
#define gpio_out_l 0x00

static int key_drv_open(struct inode *inode, struct file *filp)
{
    iowrite8(gpio_in, key_dir); //gpio14_2 配置为输入
    printk("set key in mode\n");
    return 0;
}

static int key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
    int val;

    val = ioread8(key_base);
    __copy_to_user(buf, &val, 1);
    return 0;
}

static struct file_operations key_drv_fops =
    {
        .owner = THIS_MODULE,
        .open = key_drv_open,
        .read = key_drv_read,
};

int key_drv_int(void)
{
    major = register_chrdev(0, "key_drv", &key_drv_fops);
    keydrv_class = class_create(THIS_MODULE, "key_drv");
    keydrv_class_dev = device_create(keydrv_class, NULL, MKDEV(major, 0), NULL, "key"); //dev/key

    key_base = ioremap(key_phy_base, 1);
    key_dir = key_base + gpio_phy_dir;
    key_base = key_base + key_phy_data;

    printk("key_base = %x\n", (unsigned int)key_base);
    printk("key_dir = %x\n", (unsigned int)key_dir);
    return 0;
}

void key_drv_exit(void)
{
    unregister_chrdev(major, "key_drv");
    device_unregister(keydrv_class_dev);
    class_destroy(keydrv_class);
}

module_init(key_drv_int);
module_exit(key_drv_exit);

MODULE_LICENSE("GPL");

2、key_drv_test.c

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>

int main(int argc, char **argv)
{
    int fd;
    unsigned int val = 0;

    fd = open("/dev/key", O_RDWR);
    if(fd < 0)
        printf("can`t open!\n");
    while (1)//死循环,查询按键值,未做按键消抖处理
    {
       read(fd, &val, 1);
        if(val == 0)
            printf("key down!get val = %d\n", val);
    }    

    return 0;
}

3、Makefile

#内核路径
KERN_DIR = /home/osrc/Hi3519V101_SDK_V1.0.4.0/osdrv/opensource/kernel/linux-3.18.y
 
all:
	make -C $(KERN_DIR) M=`pwd` modules 
 
clean:
	make -C $(KERN_DIR) M=`pwd` modules clean
	rm -rf modules.order
 
obj-m += key_drv.o

4、测试

/ # insmod key_drv.ko
key_base = fea4e010
key_dir = fea4e400
/ # ./key_drv_test
set key in mode
get key val = 0
get key val = 0
get key val = 0
get key val = 0
get key val = 0
get key val = 0
get key val = 0
get key val = 0
get key val = 0
get key val = 0

后台运行测试程序,查看cpu占用率,可以看到测试程序占用cpu一半的资源,查询方式还是很占用cpu资源的!

/ # ./key_drv_test &
/ # set key in mode

/ #
/ # top
Mem: 12788K used, 61976K free, 0K shrd, 0K buff, 1972K cached
CPU:  4.7% usr 47.6% sys  0.0% nic 47.6% idle  0.0% io  0.0% irq  0.0% sirq
Load average: 0.86 0.52 0.23 2/43 156
  PID  PPID USER     STAT   VSZ %VSZ CPU %CPU COMMAND
  155   125 root     R      868  1.1   1 47.5 ./key_drv_test
  156   125 root     R     1828  2.4   0  4.7 top
  125     1 root     S     1848  2.4   0  0.0 -sh
    1     0 root     S     1824  2.4   0  0.0 {linuxrc} init
   71     1 root     S <   1252  1.6   0  0.0 udevd --daemon
  148    71 root     S <   1252  1.6   1  0.0 udevd --daemon
  116    71 root     S <   1236  1.6   0  0.0 udevd --daemon
   10     2 root     SW       0  0.0   1  0.0 [migration/1]
   36     2 root     SW       0  0.0   1  0.0 [kworker/1:1]
    7     2 root     SW       0  0.0   0  0.0 [rcu_sched]
   56     2 root     SW       0  0.0   0  0.0 [kworker/0:1]
   21     2 root     SW       0  0.0   0  0.0 [kworker/u4:1]
    3     2 root     SW       0  0.0   0  0.0 [ksoftirqd/0]
   48     2 root     SW       0  0.0   1  0.0 [cfinteractive]
    5     2 root     SW<      0  0.0   0  0.0 [kworker/0:0H]
    8     2 root     SW       0  0.0   0  0.0 [rcu_bh]
    4     2 root     SW       0  0.0   0  0.0 [kworker/0:0]
   20     2 root     SW       0  0.0   1  0.0 [spi0]
   17     2 root     SW<      0  0.0   1  0.0 [writeback]
    9     2 root     SW       0  0.0   0  0.0 [migration/0]
Mem: 12816K used, 61948K free, 0K shrd, 0K buff, 1976K cached
CPU:  7.9% usr 42.0% sys  0.0% nic 49.9% idle  0.0% io  0.0% irq  0.0% sirq

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值