linux设备驱动——中断、定时器的用法

中断


申请中断

int request_irq(unsigned int irq, irq_handler_t handler,unsigned long irqflags, const char *devname, void *dev_id)

irq为中断号,在与board相关的头文件中定义,2440在Irqs.h include\asm-arm\arch-s3c2410\Irqs.h 中

handler为中断处理函数

irqflags为触发方式,在Irq.h里面定义

devname为名字

dev_id卸载时才用到,只要与free_irq中的dev_id相同即可,写为NULL也行



看一下irqflags
/*
 * These correspond to the IORESOURCE_IRQ_* defines in
 * linux/ioport.h to select the interrupt line behaviour.  When
 * requesting an interrupt without specifying a IRQF_TRIGGER, the
 * setting should be assumed to be "as already configured", which
 * may be as per machine or firmware initialisation.
 */
#define IRQF_TRIGGER_NONE	0x00000000	/*无触发中断*/
#define IRQF_TRIGGER_RISING	0x00000001	/*指定中断触发类型:上升沿有效*/
#define IRQF_TRIGGER_FALLING	0x00000002	/*指定中断触发类型:下降沿有效*/
#define IRQF_TRIGGER_HIGH	0x00000004	/*指定中断触发类型:高电平有效*/
#define IRQF_TRIGGER_LOW	0x00000008	/*指定中断触发类型:低电平有效*/
#define IRQF_TRIGGER_MASK	(IRQF_TRIGGER_HIGH | IRQF_TRIGGER_LOW | \
				 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING)
#define IRQF_TRIGGER_PROBE	0x00000010	/*触发式检测中断*/

/*
 * These flags used only by the kernel as part of the
 * irq handling routines.
 *
 * IRQF_DISABLED - keep irqs disabled when calling the action handler
 * IRQF_SAMPLE_RANDOM - irq is used to feed the random generator
 * IRQF_SHARED - allow sharing the irq among several devices
 * IRQF_PROBE_SHARED - set by callers when they expect sharing mismatches to occur
 * IRQF_TIMER - Flag to mark this interrupt as timer interrupt
 * IRQF_PERCPU - Interrupt is per cpu
 * IRQF_NOBALANCING - Flag to exclude this interrupt from irq balancing
 * IRQF_IRQPOLL - Interrupt is used for polling (only the interrupt that is
 *                registered first in an shared interrupt is considered for
 *                performance reasons)
 */
#define IRQF_DISABLED		0x00000020	/*中断禁止*/
#define IRQF_SAMPLE_RANDOM	0x00000040	/*供系统产生随机数使用*/
#define IRQF_SHARED		0x00000080	/*在设备之间可共享*/
#define IRQF_PROBE_SHARED	0x00000100	/*探测共享中断*/
#define IRQF_TIMER		0x00000200	/*专用于时钟中断*/
#define IRQF_PERCPU		0x00000400	/*每CPU周期执行中断*/
#define IRQF_NOBALANCING	0x00000800	/*复位中断*/
#define IRQF_IRQPOLL		0x00001000	/*共享中断中根据注册时间判断*/




卸载中断

free_irq(unsigned int irq,void *dev_id)


定时器


设置定时器

分配一个定时器结构
static struct timer_list buttons_timer;

初始化定时器
init_timer(&buttons_timer);

设置处理函数
buttons_timer.function = buttons_timer_function;

将结构体加入链表
add_timer(&buttons_timer);

启动定时器
mod_timer(&buttons_timer, jiffies+HZ/100);

删除定时器

del_timer(&buttons_timer);






你可能感兴趣的:(linux设备驱动——中断、定时器的用法)