注:代码有完整的注释,方便阅读
开发环境: ubuntu18.04
平台: JZ2440
kernel: Linux-3.4.2
交叉编译工具: arm-linux-gcc-4.4.3
驱动程序如下:
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/of.h>
#include <linux/of_device.h>
#include <linux/of_platform.h>
#include <linux/cdev.h>
#include <mach/gpio.h>
#include <linux/interrupt.h>
#include <linux/device.h>
#include <linux/sched.h>
#include <linux/poll.h>
#define DEVICE_NAME "fifth_drv" //设备的名称需要和文件系统中设备节点名称区分开
/* 1.1、确定主设备号,如果没有确定则由内核自动分配 */
static int major;
static struct cdev fifth_cdev;
static dev_t devid;
static struct class * fifthdrv_class;
/* 定义按键GPIO寄存器指针 */
volatile unsigned long *gpfcon = NULL;
volatile unsigned long *gpfdat = NULL;
volatile unsigned long *gpgcon = NULL;
volatile unsigned long *gpgdat = NULL;
/* 按键描述结构体 */
static struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 键值 按下时;0x01 0x02 0x03 0x04 */
/* 键值 松开后;0x81 0x82 0x83 0x84 */
static unsigned char key_val;
static struct pin_desc key_pin_desc[4] = {
{S3C2410_GPF(0), 0x01},
{S3C2410_GPF(2), 0x02},
{S3C2410_GPG(3), 0x03},
{S3C2410_GPG(11), 0x04},
};
/* 等待序列 */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 唤醒进程标志,在中断函数中将它置1,在read将它置0, */
static volatile int evpress = 0;
/* 定义fasync_struct结构体 */
static struct fasync_struct *button_async;
/* 中断处理函数 */
static irqreturn_t buttons_irq(int irq, void* dev_id)
{
/* 先获得资源描述 */
struct pin_desc* pindesc = (struct pin_desc *)dev_id;
unsigned int pinval = 0;
/* 根据得到的资源描述结构体,获得引脚电平 */
pinval = s3c2410_gpio_getpin(pindesc->pin);
if(pinval)
key_val = 0x80 | pindesc->key_val;
else
key_val = pindesc->key_val;
/* 唤醒进程 */
evpress = 1;
wake_up_interruptible(&button_waitq);
/* 在设备资源可获得时,调用 kill_fasync()函数发相应的信号 */
kill_fasync(&button_async, SIGIO, POLL_IN);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int fifth_drv_open(struct inode *inode, struct file *file)
{
/* 申请中断 配置GPF0,2 GPG3,11为输入引脚 */
request_irq(IRQ_EINT0, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S2", &key_pin_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S3", &key_pin_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S4", &key_pin_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, (IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING), "S5", &key_pin_desc[3]);
return 0;
}
static ssize_t fifth_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if(size != 1)
return -EINVAL;
/* 在读函数中,如果按键没有操作,就在此处休眠,当evpress=1时,说明发生按键中断,则继续向下执行 */
wait_event_interruptible(button_waitq, evpress);
/* evpress在中断函数中置1,在此处置0 */
evpress = 0;
copy_to_user(buf, &key_val, sizeof(key_val));
return sizeof(key_val);
}
static int fifth_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT0, &key_pin_desc[0]);
free_irq(IRQ_EINT2, &key_pin_desc[1]);
free_irq(IRQ_EINT11, &key_pin_desc[2]);
free_irq(IRQ_EINT19, &key_pin_desc[3]);
return 0;
}
static unsigned int fifth_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if(evpress)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int fifth_drv_fasync (int fd, struct file *filp, int on)
{
printk("driver: fifth_drv_fasync\n");
/* 发给谁肯定是这个fasync_struct结构中定义的。这个结构“button_async”要做些初始化 */
return fasync_helper(fd, filp, on, &button_async);
}
/* 1.2、构造file_operations结构体,里面是操作设备的函数 */
static struct file_operations fifth_drv_fops = {
.owner = THIS_MODULE,
.open = fifth_drv_open,
.read = fifth_drv_read,
.release = fifth_drv_close,
.poll = fifth_drv_poll,
.fasync = fifth_drv_fasync,
};
/* 2、定义入口函数 */
static int fifth_drv_init(void)
{
/* 2.1 分配设备号(动态和静态两种) */
if(major)
{
//如果确定了主设备号,就是用静态分配函数
devid = MKDEV(major, 0);
register_chrdev_region(devid, 2, DEVICE_NAME);
}
else
{
alloc_chrdev_region(&devid, 0, 2, DEVICE_NAME); //cat /proc/device
major = MAJOR(devid);
devid = MKDEV(major, 0);
}
/* 2.2 用fifth_drv_fops结构体初始化设备描述结构体cdev,建立关联 */
cdev_init(&fifth_cdev, &fifth_drv_fops);
/* 2.3 注册字符设备,将字符设备结构体添加到内核 */
cdev_add(&fifth_cdev, devid, 2);
/* 3、使用udev机制自动创建的设备节点 */
/* 3.1 注册一个类,使得mdev在/dev目录下自动创建设备节点,不用再使用mknod命令 */
fifthdrv_class = class_create(THIS_MODULE, DEVICE_NAME);//创建一个fifth_drv类 /sys/class/DEVICE_NAME
/* 在文件系统中创建/dev/irqbtn 这个设备文件,应用程序调用 */
device_create(fifthdrv_class, NULL, MKDEV(major, 0), NULL, "irqbtn"); //dev/irqbtn
/* 4、硬件操作,内存映射用于操作IO口 */
gpfcon = (volatile unsigned long*)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long*)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
/* 3、定义出口函数 */
static void fifth_drv_exit(void)
{
iounmap(gpfcon); //取消内存映射
iounmap(gpgcon);
device_destroy(fifthdrv_class, MKDEV(major, 0)); //取消自动创建设备节点
class_destroy(fifthdrv_class); //释放fifthdrv_class
cdev_del(&fifth_cdev); //从内核中卸载字符设备描述结构体
unregister_chrdev_region(devid, 2); //取消设备号的申请
}
/* 4、修饰入口函数和出口函数 */
module_init(fifth_drv_init);
module_exit(fifth_drv_exit);
MODULE_LICENSE("GPL");
应用程序如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <fcntl.h>
#include <signal.h>
#include <unistd.h>
int fd = 0;
/* 信号处理函数 */
void my_signal_fun(int signum)
{
unsigned char key_val = 0;
read(fd, &key_val, 1);
printf("key_val: 0x%x\n", key_val);
}
int main(int argc, char **argv)
{
unsigned char key_val;
int Oflags = 0;
int ret = 1;
/* 监听信号 */
signal(SIGIO, my_signal_fun);
fd = open("/dev/irqbtn", O_RDWR);
if(fd < 0)
{
printf("cannot open!!\n");
return -1;
}
/* 应用程序告诉内核,信号发给谁 */
fcntl(fd, F_SETOWN, getpid());
/* 通过“F_GETFL”读出“flags”,在 flags 上置上“FASYNC”位 */
Oflags = fcntl(fd, F_GETFL);
fcntl(fd, F_SETFL, Oflags|FASYNC);
while(1)
{
sleep(1000);
}
return 0;
}