实现驱动程序通知运用程序读取按键值
要点:
①运用程序注册信号处理函数
②确认谁发→驱动
③驱动怎么发→驱动调用kill_fasync()
④发给谁→fcntl
1、key_irq_async_drv.c
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <asm/io.h>
#include <linux/sched.h>
//主设备号
int major;
//udev自动创建节点
static struct class *keyirqdrv_class;
static struct class_device *keyirqdrv_class_dev;
// 操作管脚为gpio14_2
// 该管脚采用boot_sel[1:0]硬件配置复用功能,现单板已经配置为gpio,默认高电平,按键按下为低电平
static void __iomem *key_base; //寄存器基地址
#define gpio_phy_base 0x1214E000 //key管脚物理地址
#define gpio_data key_base + 0x010 //GPIO 数据寄存器 ,0b00_0001_0000
#define gpio_dir key_base + 0x400 //GPIO 方向控制寄存器
#define gpio_is key_base + 0x404 //GPIO 中断触发寄存器
#define gpio_ibe key_base + 0x408 //GPIO 双沿触发中断寄存器
#define gpio_iev key_base + 0x40C //GPIO 触发中断条件寄存器
#define gpio_ie key_base + 0x410 //GPIO 中断屏蔽寄存器
#define gpio_ris key_base + 0x414 //GPIO 原始中断状态寄存器
#define gpio_mis key_base + 0x418 //GPIO 屏蔽状态中断寄存器
#define gpio_ic key_base + 0x41C //GPIO 中断清除寄存器
#define gpio_in 0x00 //配置为输入,1输出 0输入def
#define gpio_out_h 0xFF
#define gpio_out_l 0x00
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序置1 不进休眠,drv_read将它清0 进休眠*/
static volatile int ev_press = 0;
unsigned int val; //中断值
static struct fasync_struct *button_async;
//引脚描述结构体
struct pin_irq_desc
{
unsigned int irq; //中断号
unsigned int pin; //中断标志寄存器,有中断产生时为1,无中断时为0
unsigned int number; //编号
char *name; //名称
};
struct pin_irq_desc pins_desc[] =
{
{76, 0x04, 0x01, "key_irq"},
};
static irqreturn_t irq_interrupt(int irq, void *dev_id)
{
static unsigned int cnt = 0;
unsigned int key;
// printk("cnt = %d\n", cnt++);
key = ioread8(gpio_ris); //读取中断标志位
if (key == 0x04)
{
val = key;
ev_press = 1; //不进去休眠
wake_up_interruptible(&button_waitq); //唤醒休眠的进程
kill_fasync(&button_async, SIGIO, POLL_IN);//async驱动发送信号
//button_async 包换进程id;SIGIO 表示发什么;POLL_IN 表示有数据等待读取
}
iowrite8(0xFF, gpio_ic); //清除中断
return IRQ_HANDLED;
}
static int key_drv_open(struct inode *inode, struct file *filp)
{
iowrite8(gpio_in, gpio_dir); //gpio14_2 配置为输入
iowrite8(0x04, gpio_is); //配置电平触发
iowrite8(0x00, gpio_iev); //配置下降沿触发或者是低电平触发
iowrite8(0x00, gpio_ibe); //配置单边沿触发
iowrite8(0xFF, gpio_ic); //清除中断
iowrite8(0x04, gpio_ie); //使能gpio14_2中断
//Hi3519v101好几组GPIO只有一个中断号,所以这里得是共享中断IRQF_SHARED,作为共享中断,dev用来区分不同的中断
request_irq(pins_desc->irq, irq_interrupt, IRQF_SHARED, pins_desc->name, (void *)&pins_desc);
printk("set key rising irq mode\n");
return 0;
}
static int key_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
//如果没有按键动作, 休眠
wait_event_interruptible(button_waitq, ev_press);
__copy_to_user(buf, &val, 1);
ev_press = 0; //数据拷贝给运用之后,一次中断完成,进去休眠
return 0;
}
int key_drv_close(struct inode *inode, struct file *file)
{
free_irq(pins_desc->irq, (void *)&pins_desc);
return 0;
}
static int key_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: key_drv_fasync\n");
return fasync_helper(fd, filp, on, &button_async);//初始化 button_async
}
static struct file_operations key_drv_fops =
{
.owner = THIS_MODULE,
.open = key_drv_open,
.read = key_drv_read,
.release = key_drv_close,
.fasync = key_drv_fasync,
};
int key_drv_int(void)
{
major = register_chrdev(0, "key_drv", &key_drv_fops);
keyirqdrv_class = class_create(THIS_MODULE, "key_drv");
keyirqdrv_class_dev = device_create(keyirqdrv_class, NULL, MKDEV(major, 0), NULL, "key"); //dev/key
key_base = ioremap(gpio_phy_base, 0x1000);
printk("key_data = 0x%x\n", (unsigned int)gpio_data);
printk("key_dir = 0x%x\n", (unsigned int)gpio_dir);
return 0;
}
void key_drv_exit(void)
{
unregister_chrdev(major, "key_drv");
device_unregister(keyirqdrv_class_dev);
class_destroy(keyirqdrv_class);
}
module_init(key_drv_int);
module_exit(key_drv_exit);
MODULE_LICENSE("GPL");
2、test_key_irq_async_drv.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <unistd.h>
#include <fcntl.h>
int fd;
//信号处理函数
void key_signal_fun(int signum)
{
unsigned int val = 0;
read(fd, &val, 1);
printf("key down!get val = %d\n", val);
}
int main(int argc, char **argv)
{
int Oflags;
//注册信号处理函数
signal(SIGIO, key_signal_fun);//SIGIO,为内核发出的通知类型,表示io口有数据可读,写
fd = open("/dev/key", O_RDWR);
if(fd < 0)
printf("can`t open!\n");
//发给谁,由运用通知驱动程序
fcntl(fd, F_SETOWN, getpid());//getpid 获取进程号
//每当FASYNC标志改变时,驱动程序的key_drv_fasync函数将得以执行
//读取原flag,下面的函数为修改
Oflags = fcntl(fd, F_GETFL);
//调用驱动的key_drv_fasync函数
fcntl(fd, F_SETFL, Oflags | FASYNC);//更改为FASYNC(异步通知的flag)
while (1)
{
sleep(1000);
}
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_irq_async_drv.o
4、测试
/ # insmod key_irq_async_drv.ko
key_data = 0xfea4e010
key_dir = 0xfea4e400
/ # ./test_key_irq_async_drv &
/ # set key rising irq mode
driver: key_drv_fasync
/ # key down!get val = 4
key down!get val = 4
key down!get val = 4