《linux驱动:字符设备驱动之二 中断与休眠唤醒》

目录

 

前言

常用函数

使用中断以及休眠唤醒的按键驱动

编译

测试

小结


 

前言

        前一篇文章使用查询方式获取按键的状态,极其耗费cpu资源。对此,这篇文章进一步改进,使用中断以及休眠唤醒的方式来获取按键状态。

常用函数

/**
 *	request_irq - 申请一个中断
 *	@irq: 中断号,一般系统会提供一系列的中断号。比如上s3c2440 按键脚位对应有IRQ_EINT0、IRQ_EINT2、IRQ_EINT11等。
 *	@handler: 中断处理函数
 *	@irqflags: 中断触发方式。比如上升沿触发、下降沿触发、双边沿触发等。
 *	@devname: 自定义中断名
 *	@dev_id: 自定义中断ID
 *
 *	This call allocates interrupt resources and enables the
 *	interrupt line and IRQ handling. From the point this
 *	call is made your handler function may be invoked. Since
 *	your handler function must clear any interrupt the board
 *	raises, you must take care both to initialise your hardware
 *	and to set up the interrupt handler in the right order.
 *
 *	Dev_id must be globally unique. Normally the address of the
 *	device data structure is used as the cookie. Since the handler
 *	receives this value it makes sense to use it.
 *
 *	If your interrupt is shared you must pass a non NULL dev_id
 *	as this is required when freeing the interrupt.
 *
 *	Flags:
 *
 *	IRQF_SHARED		Interrupt is shared
 *	IRQF_DISABLED	Disable local interrupts while processing
 *	IRQF_SAMPLE_RANDOM	The interrupt can be used for entropy
 *
 */
int request_irq(unsigned int irq, irq_handler_t handler,
		unsigned long irqflags, const char *devname, void *dev_id)
/**
 * wait_event_interruptible - sleep until a condition gets true
 * @wq: the waitqueue to wait on
 * @condition: a C expression for the event to wait for
 *
 * The process is put to sleep (TASK_INTERRUPTIBLE) until the
 * @condition evaluates to true or a signal is received.
 * The @condition is checked each time the waitqueue @wq is woken up.
 *
 * wake_up() has to be called after changing any variable that could
 * change the result of the wait condition.
 *
 * The function will return -ERESTARTSYS if it was interrupted by a
 * signal and 0 if @condition evaluated to true.
 */
#define wait_event_interruptible(wq, condition)

#define wake_up_interruptible(x)	__wake_up(x, TASK_INTERRUPTIBLE, 1, NULL)


#define DECLARE_WAIT_QUEUE_HEAD(name) \
	wait_queue_head_t name = __WAIT_QUEUE_HEAD_INITIALIZER(name)
  • 使用 DECLARE_WAIT_QUEUE_HEAD 初始化一个waitqueue。
  • 使用 wait_event_interruptible 休眠并等待条件condition为真(非0)。
  • 使用 wake_up_interruptible 唤醒休眠,同时需要设置条件变量condition为真(非0),wait_event_interruptible处才会去执行。

使用中断以及休眠唤醒的按键驱动

#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>

#define DEVICE_NAME     "button"  /* 加载模块后,执行”cat /proc/devices”命令看到的主设备名称 */
#define BUTTON_MAJOR     232     /* 主设备号 */

static struct class *buttons_class;    //设备类  使用cd /sys/class 查看当前系统上的设备类
static struct class_device	*buttons_class_dev; //类设备,一个设备类下可以有多个类设备   ls /dev/* 查看当前系统上的设备。或者 cd /sys/class/类名/ 查看当前设备类下的设备。


struct pin_desc{
    unsigned int pin;
    unsigned int pin_val;
};

struct pin_desc pins_desc[3] = {
    {S3C2410_GPF0,0x1},
    {S3C2410_GPF2,0x2},
    {S3C2410_GPG3,0x3},
};

// 按下 0x1 0x2 0x3
// 松开 0x81 0x82 0x83
static unsigned char key_val;

static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;


static irqreturn_t button_irq(int irq, void *dev_id)
{
    struct pin_desc *pindesc = (struct pin_desc *)dev_id;
    unsigned int pinval;

    printk(" enter %d \n",irq);

    pinval = s3c2410_gpio_getpin(pindesc->pin);

    if(pinval)
    {
        // 松开
        key_val = 0x80 | pindesc->pin_val;
    }
    else
    {
        // 按下
        key_val = pindesc->pin_val;
    }

    ev_press = 1;                  
    wake_up_interruptible(&button_waitq);  


    return IRQ_RETVAL(IRQ_HANDLED);
}

/* 应用程序对设备文件/dev/button 执行open(...)时,
 * 就会调用s3c24xx_buttons_open函数
 */
static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
	// s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPF0_INP);
    // s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_INP);
    // s3c2410_gpio_cfgpin(S3C2410_GPG3, S3C2410_GPG3_INP);

    request_irq(IRQ_EINT0,button_irq,IRQT_BOTHEDGE,"S0",&pins_desc[0]);
    request_irq(IRQ_EINT2,button_irq,IRQT_BOTHEDGE,"S1",&pins_desc[1]);
    request_irq(IRQ_EINT11,button_irq,IRQT_BOTHEDGE,"S2",&pins_desc[2]);

    return 0;
}



static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
    if (count != 1)
		return -EINVAL;

    wait_event_interruptible(button_waitq, ev_press);
    copy_to_user(buff, &key_val, sizeof(key_val));

    ev_press = 0;

    return 0;
}




static ssize_t s3c24xx_buttons_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]);

    return 0;
}



/* 这个结构是字符设备驱动程序的核心
 * 当应用程序操作设备文件时所调用的open、read、write等函数,
 * 最终会调用这个结构中指定的对应函数
 */
static struct file_operations s3c24xx_buttons_fops = {
    .owner  =   THIS_MODULE,    /* 这是一个宏,推向编译模块时自动创建的__this_module变量 */
    .open   =   s3c24xx_buttons_open,     
	.read	=	s3c24xx_buttons_read,	   
	.release	=	s3c24xx_buttons_close,	   
};

/*
 * 执行insmod命令时就会调用这个函数 
 */
static int __init s3c24xx_button_init(void)
{
    int ret;

    /* 注册字符设备
     * 参数为主设备号、设备名字、file_operations结构;
     * 这样,主设备号就和具体的file_operations结构联系起来了,
     * 操作主设备为BUTTON_MAJOR的设备文件时,就会调用s3c24xx_buttons_fops中的相关成员函数
     * BUTTON_MAJOR可以设为0,表示由内核自动分配主设备号,返回值为主设备号
     */
    ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &s3c24xx_buttons_fops);
    if (ret < 0) {
      printk(DEVICE_NAME " can't register major number\n");
      return ret;
    }

    // 创建设备类
	buttons_class = class_create(THIS_MODULE, "buttons");
	if (IS_ERR(buttons_class))
		return PTR_ERR(buttons_class);
	
	// 创建类设备节点
    buttons_class_dev = class_device_create(buttons_class, NULL, MKDEV(BUTTON_MAJOR,0), NULL, "button");
    if (unlikely(IS_ERR(buttons_class_dev)))
        return PTR_ERR(buttons_class_dev);
	
    printk(DEVICE_NAME " initialized\n");
    return 0;
}

/*
 * 执行rmmod命令时就会调用这个函数 
 */
static void __exit s3c24xx_button_exit(void)
{
    printk("to unregister_chrdev \n");
	unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
    printk("to class_device_unregister \n");
    class_device_unregister(buttons_class_dev);
    printk("to class_destroy \n");
    class_destroy(buttons_class);
}

/* 这两行指定驱动程序的初始化函数和卸载函数 */
module_init(s3c24xx_button_init);
module_exit(s3c24xx_button_exit);

/* 描述驱动程序的一些信息,不是必须的 */
MODULE_VERSION("0.1.0");
MODULE_DESCRIPTION("S3C2410/S3C2440 BUTTON Driver");
MODULE_LICENSE("GPL");

编译

KERN_DIR = /home/book/linuxSrc/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	+= buttons.o

测试

使用  cat /proc/interrupts 可以查看到当前系统的中断处理

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>

int main(int argc, char **argv)
{
    int i;
    int ret;
    int fd;
    unsigned char press;
    
    fd = open("/dev/button", 0);  // 打开设备
    if (fd < 0) {
        printf("Can't open /dev/button\n");
        return -1;
    }

    while (1) {
        ret = read(fd, &press, 1);
        if (ret < 0) {
            printf("read err!\n");
            continue;
        }

        printf(" read buttons key : 0x%x \n",press);
    }
    
    close(fd);
    return 0;    
}

小结

        休眠唤醒流程

  • 使用 DECLARE_WAIT_QUEUE_HEAD 初始化一个waitqueue。定义一个条件变量。
  • 使用 wait_event_interruptible 休眠并等待条件变量condition为真(非0)。
  • 使用 wake_up_interruptible 唤醒休眠,同时需要设置条件变量condition为真(非0),wait_event_interruptible处才会去执行。

        程序流程

  • 应用程序读取按键状态,无中断(无按键变化),进入休眠等待,等待中断服务程序的唤醒。
  • 按键按下,触发中断,进入中断服务程序处理。 
  • 中断服务程序中,根据中断号,读取对应脚位的状态判定按键的状态,唤醒休眠,将按键状态数据给到应用程序。

 

 

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

编程界的小学生、

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值