编写按键中断驱动
- 硬件平台:韦东山嵌入式Linxu开发板(S3C2440.v3)
- 软件平台:运行于VMware Workstation 12 Player下UbuntuLTS16.04_x64 系统
- 参考资料:《嵌入式Linux应用开发手册》、《嵌入式Linux应用开发手册第2版》
- 开发环境:Linux 2.6.22.6 内核、arm-linux-gcc-3.4.5-glibc-2.3.6工具链
目录
编写按键中断驱动 - 一、程序框架图
- 1、`request_irq()`注册中断服务函数解析
- 2、`free_irq()`函数解析
- 3、`wait_event_interruptible()`宏解析
- 4、`wake_up_interruptible()`宏解析
- 二、驱动文件编写
- 1、定义的变量
- 2、告诉内核有这个驱动程序
- 2.1 构建` file_operations()`结构体
- 2.2 定义驱动的入口函数`button_drv_init()`、注册驱动`register_chrdev()`
- 2.3 定义驱动的出口函数`button_drv_exit()`、卸载驱动`unregister_chrdev()`
- 2.4 修饰出、入口函数,让内核怎么知道哪个设备对应哪个的出、入口
- 3、编写中断服务函数`button_irq()`
- 4、编写驱动函数
- 三、Makefile文件编写
- 四、测试程序的编写
- 五、实际运行
一、程序框架图
1、request_irq()
注册中断服务函数解析
1.1 函数原型与作用
- 源码处于:
\arch\arm26\kernel\irq.c
, - 作用:向内核注册中断,不需要像裸板驱动配置各种寄存器
int request_irq(unsigned int irq, irqreturn_t (*handler)(int, void *, struct pt_regs *),
unsigned long irq_flags, const char * devname, void *dev_id)
{
unsigned long retval;
struct irqaction *action;
if (irq >= NR_IRQS || !irq_desc[irq].valid || !handler ||
(irq_flags & IRQF_SHARED && !dev_id))
return -EINVAL;
action = kmalloc(sizeof(struct irqaction), GFP_KERNEL);
if (!action)
return -ENOMEM;
action->handler = handler;
action->flags = irq_flags;
cpus_clear(action->mask);
action->name = devname;
action->next = NULL;
action->dev_id = dev_id;
retval = setup_irq(irq, action);
if (retval)
kfree(action);
return retval;
}
1.2 参数讲解
unsigned int irq
:申请的硬件中断号,在Linux内核中,已有开发板的驱动中有对应的宏定义irqreturn_t (*handler)(int, void *, struct pt_regs *),
:系统注册的中断处理函数,自己编写unsigned long irq_flags,
:中断处理的属性,如按键中断中的上升沿触发、下升沿触发等,在Linux内核中,已有开发板的驱动中有对应的宏定义const char * devname,
:设置中断名称,通常是设备驱动程序的名称,自己定义void *dev_id
:在中断共享时会用到,传入中断处理程序的参数,注册共享中断时不能为NULL,因为卸载时需要这个做参数,避免卸载其它中断服务函数,自己定义
1.3 返回值
函数运行正常时返回 0 ,否则返回对应错误的负值。
2、free_irq()
函数解析
2.1 函数原型与作用
- 源码处于:
\arch\arm26\kernel\irq.c
, - 作用:向内核取消中断,不需要像裸板驱动配置各种寄存器
void free_irq(unsigned int irq, void *dev_id)
{
struct irqaction * action, **p;
unsigned long flags;
if (irq >= NR_IRQS || !irq_desc[irq].valid) {
printk(KERN_ERR "Trying to free IRQ%d\n",irq);
#ifdef CONFIG_DEBUG_ERRORS
__backtrace();
#endif
return;
}
spin_lock_irqsave(&irq_controller_lock, flags);
for (p = &irq_desc[irq].action; (action = *p) != NULL; p = &action->next) {
if (action->dev_id != dev_id)
continue;
/* Found it - now free it */
*p = action->next;
kfree(action);
goto out;
}
printk(KERN_ERR "Trying to free free IRQ%d\n",irq);
#ifdef CONFIG_DEBUG_ERRORS
__backtrace();
#endif
out:
spin_unlock_irqrestore(&irq_controller_lock, flags);
}
2.2 参数讲解
unsigned int irq
:申请的硬件中断号,在Linux内核中,已有开发板的驱动中有对应的宏定义void *dev_id
:卸载的中断action下的哪个服务函数*
3、wait_event_interruptible()
宏解析
3.1 宏原型
- 源码处于:
\include\linux\wait.h
, - 作用:将本进程置为可中断的挂起状态,反复检查condition是否成立,如果成立则退出,如果不成立则继续休眠;条件满足后,即把本进程运行状态置为运行态
#define wait_event_interruptible(wq, condition)
({
int __ret = 0;
if (!(condition))
__wait_event_interruptible(wq, condition, __ret);
__ret;
})
3.2 宏参数讲解
- wq: 是一个等待队列对头的名字,名字也是通过下面这宏定义
#define DECLARE_WAIT_QUEUE_HEAD(name)
wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
- condition:是一个标志位,为0时休眠,为1时唤醒
4、wake_up_interruptible()
宏解析
4.1 宏原型
- 源码处于:
\include\linux\wait.h
, - 作用: // 唤醒休眠的进程
#define wake_up_interruptible(x) __wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)
void fastcall __wake_up(wait_queue_head_t *q, unsigned int mode,
int nr_exclusive, void *key)
{
unsigned long flags;
spin_lock_irqsave(&q->lock, flags);
__wake_up_common(q, mode, nr_exclusive, 0, key);
spin_unlock_irqrestore(&q->lock, flags);
}
4.2 宏参数讲解
- x:为队列名字,根据名字找到等待队列,唤醒队列所在的进程。
二、驱动文件编写
1、定义的变量
int major;
static struct class *buttondrv_class;
static struct class_device *buttondrv_class_dev;
volatile unsigned long *gpfcon;
volatile unsigned long *gpfdat;
volatile unsigned long *gpgcon;
volatile unsigned long *gpgdat;
/* 键值: 按下时, 0x01, 0x02, 0x03, 0x04
* 键值: 松开时, 0x81, 0x82, 0x83, 0x84
*/
static unsigned char key_val;
/* 按键信息 */
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 存储4个按键的信息 */
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
/* 中断注册信息 */
struct buttonirq_decs{
unsigned int irq;
unsigned long flags;
const char *devname;
void *dev_id;
};
struct buttonirq_decs buttonirqs_decs[4] = {
{IRQ_EINT0, IRQT_BOTHEDGE, "S2", &pins_desc[0]},
{IRQ_EINT2, IRQT_BOTHEDGE, "S3", &pins_desc[1]},
{IRQ_EINT11, IRQT_BOTHEDGE, "S4", &pins_desc[2]},
{IRQ_EINT19, IRQT_BOTHEDGE, "S5", &pins_desc[3]},
};
/* 生成一个等待队列的队头,名字为button_waitq */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中断事件标志, 中断服务程序将它置1,button_drv_read将它清0 */
static volatile int ev_press = 0;
2、告诉内核有这个驱动程序
2.1 构建file_operations()
结构体
static struct file_operations button_drv_fops = {
.owner = THIS_MODULE, /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
.open = button_drv_open,
.read = button_drv_read,
.release = button_drv_close,
};
2.2 定义驱动的入口函数button_drv_init()
、注册驱动register_chrdev()
/* 入口函数
* 执行”insmod button_drv.ko”命令时就会调用这个函数
*/
static int button_drv_init(void)
{
//注册一个字符设备,名字为button_drv
major = register_chrdev(0, "button_drv", &button_drv_fops);
//创建一个类,名字为buttond_rv(/class/button_drv)
buttondrv_class = class_create(THIS_MODULE, "button_drv");
//创建一个设备节点,名为button(/dev/button)
buttondrv_class_dev = class_device_create(buttondrv_class, NULL, MKDEV(major, 0), NULL, "button");
/* 映射物理地址 */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
2.3 定义驱动的出口函数button_drv_exit()
、卸载驱动unregister_chrdev()
/* 出口函数
* 执行”rmmod button_drv.ko”命令时就会调用这个函数
*/
static void button_drv_exit(void)
{
unregister_chrdev(major, "button_drv"); // 卸载字符
class_device_unregister(buttondrv_class_dev); //删除设备节点
class_destroy(buttondrv_class); //销毁类
/* 取消映射 */
iounmap(gpfcon);
iounmap(gpgcon);
}
2.4 修饰出、入口函数,让内核怎么知道哪个设备对应哪个的出、入口
module_init(button_drv_init);
module_exit(button_drv_exit);
3、编写中断服务函数button_irq()
/* 中断服务函数 */
static irqreturn_t button_irq(int irq, void *dev_id)
{
struct pin_desc *pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
pinval = s3c2410_gpio_getpin(pindesc->pin); //读取IO口电平
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);
}
4、编写驱动函数
4.1 button_drv_open()
函数
* open驱动函数 */
static int button_drv_open(struct inode *inode, struct file *file)
{
unsigned int i;
int err;
/* 注册中断 */
for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++){
err = request_irq(buttonirqs_decs[i].irq, button_irq, buttonirqs_decs[i].flags,
buttonirqs_decs[i].devname, buttonirqs_decs[i].dev_id);
if(err){
printk("func button_drv_open err: request_irq num:%d\n", i);
break;
}
}
/* 出现错误,释放已经注册的中断 */
if(err){
i--;
for (; i >= 0; i--)
free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);
return -EBUSY;
}
return 0;
}
4.2 button_drv_read()
函数
/* read驱动函数 */
static ssize_t button_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
unsigned long ret = 0;
int err;
/* 如果没有按键动作, 休眠 */
err = wait_event_interruptible(button_waitq, ev_press);
if (err){
printk("func button_drv_read() err: wait_event_interruptible\n");
return err;
}
/* 如果有按键动作, 返回键值 */
ret = copy_to_user(buf, &key_val, 1);
ev_press = 0; //清中断标志
if(ret < 0){
printk("func button_drv_read() err: copy_to_user\n");
return -EFAULT;
}
return sizeof(key_val);
}
4.3 button_drv_close()
函数
/* close驱动函数 */
int button_drv_close (struct inode *inode, struct file *file)
{
int i;
/* 取消中断 */
for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++)
free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);
return 0;
}
三、Makefile文件编写
KERN_DIR = /work/system/linux-2.6.22.6
all:
make -C $(KERN_DIR) M=`pwd` modules
clean:
make -C $(KERN_DIR) M=`pwd` modules clean
rm -rf modules.order
obj-m += button_drv.o
四、测试程序的编写
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <unistd.h>
int main(int argc, char **argv)
{
int fd;
int ret = 0;
unsigned char key_val;
int count = 0;
fd = open("/dev/button", O_RDWR);
if (fd < 0){
printf("can't open!\n");
return -1;
}
while(1){
ret = read(fd, &key_val, 1);
if(ret < 0){
printf(" func read() err\n");
}else{
printf("key_val = 0x%x\n", key_val);
}
}
return ret;
}
五、实际运行
可以看到,驱动以及测试程序可以正常运行,此时CPU使用率不会像查询方式一样,高达99%。