Linux按键驱动编写

#include <linux/module.h>
#include <linux/init.h>
#include <linux/miscdevice.h>
#include <linux/interrupt.h>
#include <linux/io.h>
#include <linux/fs.h>
#include <asm/uaccess.h>


#define GPFCON 0x56000050
#define GPFDAT 0x56000054

struct work_struct *work1;

struct timer_list buttons_timer;

unsigned int *gpio_data;

unsigned int key_num;

void work1_func(struct work_struct *work)
{
    mod_timer(&buttons_timer, jiffies + (HZ /10));  
}

void buttons_timer_function(unsigned long data)  
{
    unsigned int key_val;

    key_val = readw(gpio_data)&0x1; 
    if (key_val == 0)
       key_num = 4;


    key_val = readw(gpio_data)&0x4;
    if (key_val == 0)
        key_num = 3;

} 


irqreturn_t key_int(int irq, void *dev_id)
{
    //1. 检测是否发生了按键中断

    //2. 清除已经发生的按键中断

    //3. 提交下半部
    schedule_work(work1);

    //return 0;
    return IRQ_HANDLED;

}

void key_hw_init()
{ 
    unsigned int *gpio_config;
    unsigned short data;

    gpio_config = ioremap(GPFCON,4);
    data = readw(gpio_config);
    data &= ~0b110011;
    data |= 0b100010;

    writew(data,gpio_config);

    gpio_data = ioremap(GPFDAT,4);

}


int key_open(struct inode *node,struct file *filp)
{
    return 0;   
}

ssize_t key_read(struct file *filp, char __user *buf, size_t size, loff_t *pos)
{
    printk("in kernel :key num is %d\n",key_num);   
    copy_to_user(buf, &key_num, 4);

    return 4;
}

struct file_operations key_fops = 
{
    .open = key_open,
    .read = key_read,   
};

struct miscdevice key_miscdev = {
    .minor = 200,
    .name = "key",
    .fops = &key_fops,  
};

static int button_init()
{
    int ret;
    ret = misc_register(&key_miscdev);

    if (ret !=0)
        printk("register fail!\n");

    //注册中断处理程序
    request_irq(IRQ_EINT0,key_int,IRQF_TRIGGER_FALLING,"key",(void *)4);
    request_irq(IRQ_EINT2,key_int,IRQF_TRIGGER_FALLING,"key",(void *)3);

    //按键初始化
    key_hw_init();

    //. 创建工作
    work1 = kmalloc(sizeof(struct work_struct),GFP_KERNEL);
    INIT_WORK(work1, work1_func);

    /* 初始化定时器 */  
    init_timer(&buttons_timer);   
    buttons_timer.function  = buttons_timer_function;  

    /* 向内核注册一个定时器 */  
    add_timer(&buttons_timer);  

    return 0;

}


static void button_exit()
{
    misc_deregister(&key_miscdev);  
}


module_init(button_init);
module_exit(button_exit);


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值