#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);
Linux按键驱动编写
最新推荐文章于 2023-10-12 20:12:57 发布