A 中断初始化
init_IRQ()----->init_arch_irq(){对于2440开发板对应s3c24xx_init_irq}--------->一些初始化{例如对外部中断来说:设置外部中断触发方式,使能中断,禁止中断,设置这些中断的处理函数入口为irq_desc[irqno].handle=handle_edge_irq()等}
B 中断处理过程
发生中断-----》执行vector_irq处的代码----》调用中断处理总入口函数asm_do_IRQ---------》asm_do_IRQ根据中断号调用irq_desc数组中的handle_irq(外部中断对应handle_edge_irq())------》handle_irq首先使用chip成员中的函数设置硬件,清除中断,禁止中断,然后调用irq_desc[].action链表中用户注册的中断处理函数,最后重新开启中断。
C 用户程序可以通过swi指令触发swi异常,操作系统在swi异常处理中实现各种系统调用
1 driver
#include <linux/module.h>
#include <linux/kernel.h>#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <asm/irq.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <asm/arch/regs-gpio.h>
#include <asm/hardware.h>
#define DEVICE_NAME "buttons"
#define BUTTON_MAJOR 232
struct button_irq_desc {
int irq;
unsigned long flags;
char *name;
};
static struct button_irq_desc button_irqs [] = {
{IRQ_EINT2, IRQF_TRIGGER_FALLING, "KEY1"}, /* K1 */
{IRQ_EINT3, IRQF_TRIGGER_FALLING, "KEY2"}, /* K2 */
{IRQ_EINT4, IRQF_TRIGGER_FALLING, "KEY3"}, /* K3 */
{IRQ_EINT5, IRQF_TRIGGER_FALLING, "KEY4"}, /* K4 */
};
static volatile int press_cnt [] = {0, 0, 0, 0};
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
static irqreturn_t buttons_interrupt(int irq, void *dev_id)
{
volatile int *press_cnt = (volatile int *)dev_id;
*press_cnt = *press_cnt + 1;
ev_press = 1;
wake_up_interruptible(&button_waitq);
return IRQ_RETVAL(IRQ_HANDLED);
}
static int s3c24xx_buttons_open(struct inode *inode, struct file *file)
{
int i;
int err;
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
err = request_irq(button_irqs[i].irq, buttons_interrupt, button_irqs[i].flags,
button_irqs[i].name, (void *)&press_cnt[i]);
if (err)
break;
}
if (err) {
i--;
for (; i >= 0; i--)
free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
return -EBUSY;
}
return 0;
}
static int s3c24xx_buttons_close(struct inode *inode, struct file *file)
{
int i;
for (i = 0; i < sizeof(button_irqs)/sizeof(button_irqs[0]); i++) {
free_irq(button_irqs[i].irq, (void *)&press_cnt[i]);
}
return 0;
}
static int s3c24xx_buttons_read(struct file *filp, char __user *buff, size_t count, loff_t *offp)
{
unsigned long err;
wait_event_interruptible(button_waitq, ev_press);
ev_press = 0;
err = copy_to_user(buff, (const void *)press_cnt, min(sizeof(press_cnt), count));
memset((void *)press_cnt, 0, sizeof(press_cnt));
return err ? -EFAULT : 0;
}
static struct file_operations s3c24xx_buttons_fops = {
.owner = THIS_MODULE,
.open = s3c24xx_buttons_open,
.release = s3c24xx_buttons_close,
.read = s3c24xx_buttons_read,
};
static int __init s3c24xx_buttons_init(void)
{
int ret;
ret = register_chrdev(BUTTON_MAJOR, DEVICE_NAME, &s3c24xx_buttons_fops);
if (ret < 0) {
printk(DEVICE_NAME " can't register major number\n");
return ret;
}
printk(DEVICE_NAME " initialized\n");
return 0;
}
static void __exit s3c24xx_buttons_exit(void)
{
unregister_chrdev(BUTTON_MAJOR, DEVICE_NAME);
}
module_init(s3c24xx_buttons_init);
module_exit(s3c24xx_buttons_exit);
MODULE_AUTHOR("litingting");
MODULE_DESCRIPTION("gec2440 BUTTON Driver");
MODULE_LICENSE("GPL");
2 makefile
ifneq ($(KERNELRELEASE),)
obj-m :=s3c24xx_buttons.o
else
KERNELDIR :=/home/litingting/gec2440/linux-2.6.30.4
all:
make -C $(KERNELDIR) M=$(PWD) modules ARCH=arm CROSS_COMPILE=arm-linux-
clean:
rm -f *.o *.ko *.mod.o *.mod.c *.symvers modul* *.*~
endif
3 test
#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;
int press_cnt[4];
fd = open("/dev/buttons", 0);
if (fd < 0) {
printf("Can't open /dev/buttons\n");
return -1;
}
while (1) {
ret = read(fd, press_cnt, sizeof(press_cnt));
if (ret < 0) {
printf("read err!\n");
continue;
}
for (i = 0; i < sizeof(press_cnt)/sizeof(press_cnt[0]); i++) {
if (press_cnt[i])
printf("K%d has been pressed %d times!\n", i+1, press_cnt[i]);
}
}
close(fd);
return 0;
}
all:button_test
arm-linux-gcc -static -o button_test button_test.c
clean:
rm button_test