先给自己打个广告,本人的微信公众号正式上线了,搜索:张笑生的地盘,主要关注嵌入式软件开发,股票基金定投,足球等等,希望大家多多关注,有问题可以直接留言给我,一定尽心尽力回答大家的问题,二维码如下:
同时本篇是为记录在学习韦东山老师的嵌入式linux课程的笔记,有想学习的,请用微信扫描下方二维码,学习过程中遇到问题,欢迎在我的公众号上留言,或者加我个人微信
一 前言
在谈linux中断框架时,我们先来简单回顾一下单片机中断系统的设计方式,一般地,比如gpio中断,cpu检测到发生中断,先保存被中断的现场,然后跳转到异常向量入口函数,执行中断处理函数,执行完之后,恢复之前被中断的现场,继续执行。
单片机程序中,首先分辨是哪个中断,然后调用处理函数,在中断函数中清中断(一般地,有些中断需要软件清除,有些中断在响应中断服务程序的时候会自动清除)。
本篇文章中,主要是以使用目的为主,没有仔细分析很多linux内核封装的函数,前期先学会使用这些内核函数即可。
二 what
知道单片机中断处理流程之后,我们来看看linux中断框架,linux系统只是将中断处理的流程封装成一套处理流程api,对于用户程序来说,只需要做注册的动作即可。
linux内核中断流程:
- 异常中断向量设置,中断向量入口地址为:0x0和0xffff0000,通过trap_init函数实现构造异常向量
- vector_irq实现保存,并执行asm_do_irq
- desc->handle_irq()
- irq_desc[irq]
- set_irq_chip
- s3c24xx_init_irq
linux中断框架如下:
a. 进入异常模式
b vector_irq + offset -----------> entry-armv.s
b. __irq_usr -----------> entry-armv.s
c. asm_do_irq -----------> Irq.c
d. irq_desc[irq] ----> handle_irq -----------> Irq.h
e. handle_edge_irq
三 why
字符设备驱动程序为什么需要中断的方式呢?
比如我们应用程序需要查询某个电平的状态,如果没有中断方式,用户程序通过read一直读的话,会导致应用程序一直占据cpu,其他程序无法执行(当然我们可以在用户程序中通过别的方式来避免出现这种情况);如果我们有中断方式,那么我们用户程序在read的时候,就不用关心通过其他方式来避免出现用户程序一直占据cpu的情况了。
那么同时这种做法有什么缺点呢?在应用程序中使用read函数读取按键时,应用程序会在read这里一直等待,而不会退出。那有没有解决办法?答案是肯定的,就是我们在《linux字符设备驱动之中断篇(三)》种的poll机制方法。
四 how
介绍完了基础知识之后,我们来看看字符设备驱动的中断实现方式,与前面字符设备驱动相同的是,open,read的实现是相同的,但是在中断实现方式中多了一个release的操作,即释放中断,或者说不使能中断
A. open函数
我们在open函数中注册中断,调用linux内核提供的request_irq函数来实现注册中断,将中断服务程序添加到链表中,并且使能中断
static int third_drv_open(struct inode *inode, struct file *file)
{
/* 配置GPF0,2、GPG3,11为输出引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
request_irq函数原型如下,它接受五个参数
int request_irq(unsigned int irq, irq_handler_t handler,
unsigned long irqflags, const char *devname, void *dev_id)
第一个参数 irq为中断号
第二个参数 handle为中断回调函数
第三个参数 irqflags为中断类型,比如对于gpio中断来说,可以是上升沿中断、下降沿中断、双边沿中断、低电平中断、高电平中断
第四个参数 devname为名称,可以任意指定
第五个参数 dev_id为注册时传递给中断服务程序的参数
pins_desc定义如下
struct pin_desc
{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] =
{
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04}
};
注册的中断回调函数为buttons_irq,也就是中断发生时,cpu正在会跳转进入执行的驱动入口函数,这个里面的内容可以由中断驱动实现,我们先通过系统实现的s3c2410_gpio_getpin获取引脚电平状态的函数获取对应引脚的电平,并在中断中通过wake_up_interruptible唤醒cpu,通知等待中断的地方继续执行
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
//printk("irq=%d\n", irq);
pinval = s3c2410_gpio_getpin(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;
}
B. read函数
我们使用wait_event_interruptible等待中断,如果一直没有中断,那么read函数中这条语句的后面部分就不会被执行,会引发cpu线程调度;如果发生了中断,read函数就会紧接着执行后面的内容,因为key_val是属于kernel驱动的,用户空间无法访问,所以要使用copy_to_usr拷贝到用户态
static int third_drv_read(struct file *filp, char __user *buf,
size_t size, loff_t *ppos)
{
/* 返回4个引脚电平状态 */
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return (sizeof(key_val));
}
C. release函数
我们调用free_irq禁止对应中断,大致的实现过程就是将前面使能的中断函数从链表中删除,并且禁止中断,可参考源码分析free_irq的过程
int third_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;
}
完整代码如下
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/irq.h>
#include <asm/uaccess.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#include <linux/poll.h>
static struct class *thirddrv_class;
static struct class_device *thirddrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpgcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgdat;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
static volatile int ev_press = 0;
static unsigned char key_val;
struct pin_desc
{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
struct pin_desc pins_desc[4] =
{
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04}
};
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
//printk("irq=%d\n", irq);
pinval = s3c2410_gpio_getpin(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;
}
static int third_drv_open(struct inode *inode, struct file *file)
{
/* 配置GPF0,2、GPG3,11为输出引脚 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
static int third_drv_read(struct file *filp, char __user *buf,
size_t size, loff_t *ppos)
{
/* 返回4个引脚电平状态 */
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠 */
wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return (sizeof(key_val));
}
int third_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;
}
static struct file_operations third_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = third_drv_open,
.read = third_drv_read,
.release = third_drv_close,
};
static int major;
static int third_drv_init(void)
{
major = register_chrdev(0, "my_third_drv", &third_drv_fops);
thirddrv_class = class_create(THIS_MODULE, "mythirddrv");
thirddrv_class_dev = class_device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/xyz */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
static void third_drv_exit(void)
{
unregister_chrdev(major, "my_third_drv");
class_device_unregister(thirddrv_class_dev);
class_destroy(thirddrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
return 0;
}
module_init(third_drv_init);
module_exit(third_drv_exit);
MODULE_LICENSE("GPL");
五 test
- 写drvtest用户程序,比如针对按键按下的可执行程序,代码如下
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
/* thirddrvtest
*/
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
while (1)
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
return 0;
}
- 编译动态库: make
- 编译用户程序: arm-linux-gcc -0 xxx xxx.c
- 上传动态库以及可执行程序到nfs服务器 cp xxx /work/nfs/first_fs
- 加载动态库: insmod xxx.ko
- 修改可执行文件权限: chmod +777 yyy
- 执行可执行程序
- 之所以采用中断的实现方式,是为了降低cpu的占用率,我们使用top命令查看此时cpu占用率,发现比一直读取的方法明显降低了许多