上一节在一个while(1){ }循环中不断查询按键的值,这样显示是很浪费资源的,这节用中断的方式来实现按键驱动。
答:
1. 单片机中断流程
① 按下按键
② CPU发生中断,跳到异常向量入口执行
③ b 函数
3.1 保存被中断的现场
3.2 执行中断处理函数
3.3 恢复现场
2. Linux 中断流程
① 异常向量入口由 trap_int构造提供
b ....
b vector_irq + offset
② vector_irq:用宏实现
保存现场
asm_do_irq
恢复现场
问2:单片机和Linux 下中断处理有何不同?
答:
1.单片机
① 分辨是哪一个中断; ②调用相应的处理函数; ③清除中断标志;
2. Linux系统
① 按下按键,自动进入异常模式
b vector_irq + offset
__urq_ure
b asm_do_IRQ
② 调用 irq_desc_irq[irq ]-->handle_irq
③ handle_edge_irq
3.1 desc-->chip-->ack(irq) :清不断
3.2 handle_IRQ_event: 处理中断
取出action 链表中断的成员; 执行:action-->handler.
④ 注册中断程序:
request_irq(irq,hanler,flags,name,dev_id)
4.1 分配一个irq_action结构
4.2 把这个结构放入 irq_des[irq] action链表
4.3 设置引脚,使能中断
⑤ 释放中断
free_irq(irq,dev_id) 完成出链;禁止中断。
答:1. 用命令打开设备:#exec 5 </dev/keys 这样就会运行到驱动中的open()方法。
2. 怎么关闭呢,用:# exec 5<&-
下面贴上驱动程序 keys_drv.c 代码:
执行:#top 可以看到占CPU降低到2%了。
1. 中断方式的按键驱动_Linux异常处理结构
问1:linux中断与单片机中断有何不同?答:
1. 单片机中断流程
① 按下按键
② CPU发生中断,跳到异常向量入口执行
③ b 函数
3.1 保存被中断的现场
3.2 执行中断处理函数
3.3 恢复现场
2. Linux 中断流程
① 异常向量入口由 trap_int构造提供
b ....
b vector_irq + offset
② vector_irq:用宏实现
保存现场
asm_do_irq
恢复现场
2. 中断方式的按键驱动_Linux中断处理结构
这节来分析一下 Linux中断处理如何实现,即进入asm_do_irq后 做什么事情。问2:单片机和Linux 下中断处理有何不同?
答:
1.单片机
① 分辨是哪一个中断; ②调用相应的处理函数; ③清除中断标志;
2. Linux系统
① 按下按键,自动进入异常模式
b vector_irq + offset
__urq_ure
b asm_do_IRQ
② 调用 irq_desc_irq[irq ]-->handle_irq
③ handle_edge_irq
3.1 desc-->chip-->ack(irq) :清不断
3.2 handle_IRQ_event: 处理中断
取出action 链表中断的成员; 执行:action-->handler.
④ 注册中断程序:
request_irq(irq,hanler,flags,name,dev_id)
4.1 分配一个irq_action结构
4.2 把这个结构放入 irq_des[irq] action链表
4.3 设置引脚,使能中断
⑤ 释放中断
free_irq(irq,dev_id) 完成出链;禁止中断。
3. 中断方式的按键驱动_编写代码
问1:在linux中,当只有设备节点,还没写测试程序时,怎么测试?答:1. 用命令打开设备:#exec 5 </dev/keys 这样就会运行到驱动中的open()方法。
2. 怎么关闭呢,用:# exec 5<&-
下面贴上驱动程序 keys_drv.c 代码:
#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>
static struct class *thirddrv_class;
static struct class_device *thirddrv_class_dev;
//volatile unsigned long *gpfcon;
//volatile unsigned long *gpfdat;
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,third_drv_read将它清0 */
static volatile int ev_press = 0;
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04 */
/* 键值: 松开时, 0x81, 0x82, 0x83, 0x84 */
static unsigned char key_val;
/*
* K1,K2,K3,K4对应GPG0,GPG3,GPG5,GPG6
*/
struct pin_desc pins_desc[4] = {
{S3C2410_GPG0, 0x01},
{S3C2410_GPG3, 0x02},
{S3C2410_GPG5, 0x03},
{S3C2410_GPG6, 0x04},
};
/*
* 确定按键值
*/
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc * pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
//系统的一个读取某个gpio引脚电平的函数
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_RETVAL(IRQ_HANDLED);
}
static int third_drv_open(struct inode *inode, struct file *file)
{
/* GPG0,GPG3,GPG5,GPG6为中断引脚: EINT8,EINT11,EINT13,EINT14
注册中断,4个参数分别为:中断号,中断处理函数,中断类型标志,name,dev_id
*/
request_irq(IRQ_EINT8, buttons_irq, IRQT_BOTHEDGE, "K1", &pins_desc[0]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "K2", &pins_desc[1]);
request_irq(IRQ_EINT13, buttons_irq, IRQT_BOTHEDGE, "K3", &pins_desc[2]);
request_irq(IRQ_EINT14, buttons_irq, IRQT_BOTHEDGE, "K4", &pins_desc[3]);
return 0;
}
ssize_t third_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if (size != 1)
return -EINVAL;
/* 如果没有按键动作, 休眠
ev_press是定义的中断休眠标志,为0做休眠,为1则往下继续执行
button_waitq 这是声明的进程在等待队列中的名字
*/
wait_event_interruptible(button_waitq, ev_press);
/* 如果有按键动作, 返回键值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return 1;
}
int third_drv_close(struct inode *inode, struct file *file)
{
free_irq(IRQ_EINT8, &pins_desc[0]);
free_irq(IRQ_EINT11, &pins_desc[1]);
free_irq(IRQ_EINT13, &pins_desc[2]);
free_irq(IRQ_EINT14, &pins_desc[3]);
return 0;
}
static struct file_operations sencod_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = third_drv_open,
.read = third_drv_read,
.release = third_drv_close,
};
int major;
static int third_drv_init(void)
{
major = register_chrdev(0, "third_drv", &sencod_drv_fops);
thirddrv_class = class_create(THIS_MODULE, "third_drv");
thirddrv_class_dev = class_device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
// gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
// gpfdat = gpfcon + 1;
return 0;
}
static void third_drv_exit(void)
{
unregister_chrdev(major, "third_drv");
class_device_unregister(thirddrv_class_dev);
class_destroy(thirddrv_class);
// iounmap(gpfcon);
return 0;
}
module_init(third_drv_init);
module_exit(third_drv_exit);
MODULE_LICENSE("GPL");
下面看一下测试程序 keys_test.c:
#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;
}
我们执行:#ps 可以看到它平时处理sleep状态执行:#top 可以看到占CPU降低到2%了。