/* AUTHOR: Pinus
* Creat on : 2018-10-11
* KERNEL : linux-4.4.145
* BOARD : JZ2440(arm9 s3c2440)
* REFS : 韦东山视频教程第二期
*/
概述
作为本系列的第三节第一部分,我们以实现一个按键中断讲述字符设备驱动中经常会涉及的种种,首先先介绍外部中断的实现;
实验
目的:实现一个按键驱动程序
一、单片机上常用的循环查询的方式
1、实现出入口函数,注册设备,file_operation,实现open初始化相应IO寄存器,read函数读取对应IO口的值,这部分比较基础,与前文LED本质上没有什么差别,直接上代码。
#include <linux/kernel.h>
#include <linux/device.h> //class_create
#include <linux/module.h>
#include <linux/delay.h>
#include <linux/init.h>
#include <linux/fs.h>
#include <asm/io.h>
#include <asm/irq.h>
#include <asm/uaccess.h>
static struct class *buttons_class;
static struct device *buttons_dev;
static unsigned long gpio_va;
#define GPIO_OFT(x) ((x) - 0x56000000)
#define GPFCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000050)))
#define GPFDAT (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000054)))
#define GPGCON (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000060)))
#define GPGDAT (*(volatile unsigned long *)(gpio_va + GPIO_OFT(0x56000064)))
static int buttons_drv_open(struct inode *inode, struct file *file)
{
/*BUTTONS GPF0,2 GPG3,11 */
GPFCON &= ~((0x3<<(0*2)) | (0x3<<(2*2))); //先将对应位清零
GPGCON &= ~((0x3<<(3*2)) | (0x3<<(11*2))); // 00 in
return 0;
}
static ssize_t buttons_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
/*返回四个引脚电平*/
unsigned char key_vals[4];
int regval;
if (sizeof(key_vals) != size)
return -EINVAL;
//读GPF0 2
regval = GPFDAT;
key_vals[0] = (regval & (1<<0)) ? 1 : 0;
key_vals[1] = (regval & (1<<2)) ? 1 : 0;
//读GPG3 11
regval = GPGDAT;
key_vals[2] = (regval & (1<<3)) ? 1 : 0;
key_vals[3] = (regval & (1<<11)) ? 1 : 0;
copy_to_user(buf, key_vals, sizeof(key_vals));
return sizeof(key_vals);
}
static const struct file_operations jz2440_buttons_fops = {
.owner = THIS_MODULE,
.open = buttons_drv_open,
.read = buttons_drv_read,
};
unsigned int major;
static int __init buttons_drv_init(void)
{
unsigned int minor = 0;
gpio_va = ioremap(0x56000000, 0x100000);
if (!gpio_va) {
return -EIO;
}
major = register_chrdev(0, "buttons_dev", &jz2440_buttons_fops); //注册 告诉内核
if (major < 0)
{
printk("buttons_dev can't register major number\n");
return major;
}
buttons_class = class_create(THIS_MODULE, "buttons_class"); //创建一个类
buttons_dev = device_create(buttons_class, NULL, MKDEV(major, 0), NULL, "buttons"); //创建设备节点"buttons"
printk("buttons_dev initialized\n");
return 0;
}
static void __exit buttons_drv_exit(void)
{
iounmap(gpio_va);
unregister_chrdev(major, "buttons_dev"); //卸载设备驱动
device_unregister(buttons_dev); //卸载类下的设备
class_destroy(buttons_class); //卸载类
}
/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(buttons_drv_init);
module_exit(buttons_drv_exit);
MODULE_AUTHOR("pinus0716@163.com");
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("JZ2440 BUTTONS Driver");
MODULE_LICENSE("GPL");
2、测试程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
char key_vals[4];
char old_key_vals[4];
char button_status=-1;
unsigned char len;
int cnt=0;
fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
printf("can't open dev nod !\n");
while(1){
read(fd, key_vals, sizeof(key_vals));
if(!key_vals[0] || !key_vals[1] || !key_vals[2] || !key_vals[3]){
printf("%04d key_vals : %d %d %d %d\n", cnt++, key_vals[0], key_vals[1], key_vals[2], key_vals[3]);
}
}
return 0;
}
很显然,测试程序就是打开的设备,然后不断循环读取,极度浪费cpu资源,没有任何实用价值。
二、使用中断
1.有过基础的都知道,要想使用中断,当然我们要先初始化/申请一个中断,编写对应的中断函数。
static const struct file_operations jz2440_buttons_fops = {
.owner = THIS_MODULE,
.read = buttons_drv_read,
.open = buttons_drv_open,
.release = buttons_drv_close,
};
我们在open时初始化中断,在release(释放)时释放中断
static int buttons_drv_open(struct inode *inode, struct file *file)
{
/*BUTTONS GPF0,2 GPG3,11 */
/*配置为irq mode*/
request_irq(IRQ_EINT0, button_irq_handle, IRQF_TRIGGER_FALLING, "S2", &pins_desc[0]); //注册中断
request_irq(IRQ_EINT2, button_irq_handle, IRQF_TRIGGER_FALLING, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, button_irq_handle, IRQF_TRIGGER_FALLING, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, button_irq_handle, IRQF_TRIGGER_FALLING, "S5", &pins_desc[3]);
return 0;
}
关键的函数就是
request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,const char *name, void *dev)
/*irq: 中断号;
*handler: 中断处理函数
*flags: 中断方式标志
*name: 给申请的中断取个名字
*dev: 自定义,可以用来传递一些参数
*/
自定义传递的参数,用来告知是哪个引脚触发中断,和定义的按键状态
struct intpin_desc{
unsigned int pin;
unsigned int key_val;
};
/*按下时键值是 0x01 0x02 0x03 0x04*/
/*松开时 0x81 0x82 0x83 0x84*/
struct intpin_desc pins_desc[4] = {
{S3C2410_GPF(0), 0x01},
{S3C2410_GPF(2), 0x02},
{S3C2410_GPG(3), 0x03},
{S3C2410_GPG(11), 0x04},
};
中断处理函数
static irqreturn_t button_irq_handle(int irq, void *dev_id)
{
struct intpin_desc * pindesc = (struct intpin_desc *)dev_id;
unsigned int pinval;
pinval = gpio_get_value(pindesc->pin);
if (pinval){
/*松开*/
key_val = 0x80 | pindesc->key_val;
}else{
/*按下*/
key_val = pindesc->key_val;
}
ev_press = 1;
wake_up_interruptible(&button_waitq); /*唤醒程序*/
return IRQ_HANDLED;
}
上面的中断处理函数很短,关键就两步,一是根据触发中断的引脚(由申请中断时传递的参数结构体得到,不同的触发中断参数自然不同)的到当前引脚状态;二是使用了
ev_press = 1;
wake_up_interruptible(&button_waitq); /*唤醒程序*/
首先先看一下read函数
static ssize_t buttons_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if (size != 1)
return -EINVAL;
/*如果没有按键发生, 休眠
* 如果有,返回
*/
wait_event_interruptible(button_waitq, ev_press);
ev_press = 0;
copy_to_user(buf, &key_val, 1);
return 1;
}
可以看到,其主要内容也是两部分,一是将按键状态key_val传递给用户,二是调用了
wait_event_interruptible(button_waitq, ev_press);
ev_press = 0;
它与中断处理函数中的一个wait一个wakeup,显然是一对共同起作用,它的作用就是使当前程序睡眠,等待唤醒。
首先在程序最前面初始化一个等待队列
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); /* 注册一个等待队列 */
通过调用下面函数,当condition(条件)ev_press为0时,沉睡,ev_press为1时不沉睡
wait_event_interruptible(button_waitq, ev_press);
通过调用下函数可以将队列中的程序唤醒继续运行
wake_up_interruptible(&button_waitq); /*唤醒*/
当然不要忘记在release中释放申请的中断
int buttons_drv_close(struct inode *inode, struct file *file)
{
//释放中断
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[1]);
free_irq(IRQ_EINT11, &pins_desc[2]);
free_irq(IRQ_EINT19, &pins_desc[3]);
return 0;
}
2.测试程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
int main(int argc, char **argv)
{
int fd;
char key_val;
fd = open("/dev/buttons", O_RDWR);
if (fd < 0){
printf("can't open dev nod !\n");
return -1;
}
while(1){
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
return 0;
}
此时程序就很清晰了
1.初始化,设备节点,初始化中断,在while中想要读取按键状态
2.当没有按键按下触发中断时,程序会沉睡,不占用cpu资源
3.当按键按下,程序会暂时唤醒,向用户发送一次按键状态,随后沉睡等待下一次中断