注:代码都有完整的注释,方便阅读
开发环境: 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>
#define DEVICE_NAME "third_drv" //设备的名称需要和文件系统中设备节点名称区分开
/* 1.1、确定主设备号,如果没有确定则由内核自动分配 */
static int major;
static struct cdev third_cdev;
static dev_t devid;
static struct class * thirddrv_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;
/* 中断处理函数 */
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);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int third_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 third_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 third_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;
}
/* 1.2、构造file_operations结构体,里面是操作设备的函数 */
static struct file_operations third_drv_fops = {
.owner = THIS_MODULE,
.open = third_drv_open,
.read = third_drv_read,
.release = third_drv_close,
};
/* 2、定义入口函数 */
static int third_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 用third_drv_fops结构体初始化设备描述结构体cdev,建立关联 */
cdev_init(&third_cdev, &third_drv_fops);
/* 2.3 注册字符设备,将字符设备结构体添加到内核 */
cdev_add(&third_cdev, devid, 2);
/* 3、使用udev机制自动创建的设备节点 */
/* 3.1 注册一个类,使得mdev在/dev目录下自动创建设备节点,不用再使用mknod命令 */
thirddrv_class = class_create(THIS_MODULE, DEVICE_NAME);//创建一个third_drv类 /sys/class/DEVICE_NAME
/* 在文件系统中创建/dev/irqbtn 这个设备文件,应用程序调用 */
device_create(thirddrv_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 third_drv_exit(void)
{
iounmap(gpfcon); //取消内存映射
iounmap(gpgcon);
device_destroy(thirddrv_class, MKDEV(major, 0)); //取消自动创建设备节点
class_destroy(thirddrv_class); //释放thirddrv_class
cdev_del(&third_cdev); //从内核中卸载字符设备描述结构体
unregister_chrdev_region(devid, 2); //取消设备号的申请
}
/* 4、修饰入口函数和出口函数 */
module_init(third_drv_init);
module_exit(third_drv_exit);
MODULE_LICENSE("GPL");
应用程序如下:
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd = 0;
unsigned char key_val;
fd = open("/dev/irqbtn", O_RDWR);
if(fd < 0)
{
printf("cannot open!!\n");
return -1;
}
while(1)
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
return 0;
}