中斷是指在CPU正常運行期間,由於內外部事件或由程序預先安排的事件引起的CPU暫時停止正在運行的程序,轉而為該內部或外部事件或預先安排的事件服務 的程序中去,服務完畢后再返回去繼續運行被暫時中斷的程序。Linux中通常分為外部中斷(又叫硬件中斷)和內部中斷(又叫異常)。
單片機中斷處理:
①分辨中斷類型
②調用處理函數
③清中斷
Linux系統 : asm_do_IRQ
1.申請中斷:request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags,const char *name, void *dev);
①分配irqaction結構。
②setup_irq(irq, action); a.加入中斷鏈表:irq_desc[irq]->action. b.設置中斷引腳和中斷類型:desc->chip->settype() c.使能中斷:desc->chip->startup/enable
2.釋放中斷:free_irq(irq, dev_id);
①從鏈表取出中斷
②禁止中斷,free
中斷線共享的數據結構:
struct irqaction {
irq_handler_t handler; /* 具體的中斷處理程序 */
unsigned long flags;/*中斷處理屬性*/
const char *name; /* 名稱,會顯示在/proc/interreupts 中 */
void *dev_id; /* 設備ID,用於區分共享一條中斷線的多個處理程序 */
struct irqaction *next; /* 指向下一個irq_action 結構 */
int irq; /* 中斷通道號 */
struct proc_dir_entry *dir; /* 指向proc/irq/NN/name 的入口*/
irq_handler_t thread_fn;/*線程中斷處理函數*/
struct task_struct *thread;/*線程中斷指針*/
unsigned long thread_flags;/*與線程有關的中斷標記屬性*/
};
thread_flags參見枚舉型:
enum {
IRQTF_RUNTHREAD,/*線程中斷處理*/
IRQTF_DIED,/*線程中斷死亡*/
IRQTF_WARNED,/*警告信息*/
IRQTF_AFFINITY,/*調整線程中斷的關系*/
};
多個中斷處理程序可以共享同一條 中斷線(引腳),irqaction 結構中的 next 成員用來把共享同一條中斷線的所有中斷處理程序組成一個單向鏈表,dev_id 成員用於區分各個中斷處理程序。
3.poll機制:
app: poll
kernel: sys_poll -> do_sys_poll(..., timeout_jiffies) -> poll_initwait(&table); -> init_poll_funcptr(&pwq->pt, __pollwait); -> pt->qproc = qproc -> do_poll(nfds, head, &table, timeout) :
for(;;)
{
if (do_pollfd(pfd, pt)){ -> mask = file->f_op->poll(file, pwait); return mask;
//驅動poll:
__pollwait(flip, &button_waitq, p); //把當前進程掛到button_waitq隊列里去
count++;
pt = NULL;
}
//break的條件:count非0, 超時,有信號在等待處理
if (count || !*timeout || signal_pending(current))
break;
//休眠__timeout 5s
__timeout = schedule_timeout(__timeout);
}
等待隊列:
在 Linux 驅動程序設計中,可以使用等待隊列可以看作保存進程的容器,在阻塞進程時,將進程放入等待隊列;
當喚醒進程時,從等待隊列中取出進程.
等待隊列的 定義 和 初始化 wait_queue_head_t DECLARE_WAIT_QUEUE_HEAD :
Linux 2.6 內核提供了如下關於等待隊列的操作:
1,定義等待隊列.
wait_queue_head_t my_queue
2,初始化等待隊列.
init_waitqueue_head ( &my_queue )
3,定義並初始化等待隊列.
DECLARE_WAIT_QUEUE_HEAD ( my_queue )
等待隊列的 睡眠 wait_event_interruptible :
有條件睡眠:
1, wait_event ( queue , condition )
當 condition ( 一個布爾表達式 ) 為真,立即返回;否則讓進程進入 TASK_UNINTERRUPTIBLE 模式
睡眠,並掛在 queue 參數所指定的等待隊列上.
2, wait_event_interruptible ( queue , condition )
當 condition ( 一個布爾表達式 ) 為真,立即返回;否則讓進程進入 TASK_INTERRUPTIBLE 模式
睡眠,並掛在 queue 參數所指定的等待隊列上.
3, int wait_event_killable ( wait_queue_t queue , condition )
當 condition ( 一個布爾表達式 ) 為真,立即返回;否則讓進程進入 TASK_KILLABLE 模式
睡眠,並掛在 queue 參數所指定的等待隊列上.
無條件睡眠:
( 老版本,不建議使用 )
sleep_on ( wait_queue_head_t *q )
讓進程進入 不可中斷 的睡眠,並把它放入等待隊列 q.
interruptible_sleep_on ( wait_queue_head_t *q )
讓進程進入 可中斷 的睡眠,並把它放入等待隊列 q.
等待隊列中喚醒進程 wake_up :
wake_up ( wait_queue_t *q )
從等待隊列 q 中喚醒狀態為 TASKUNINTERRUPTIBLE ,TASK_INTERRUPTIBLE ,TASK_KILLABLE
的所有進程.
wake_up_interruptible ( wait_queue_t *q )
從等待隊列 q 中喚醒狀態為 TASK_INTERRUPTIBLE 的進程.
按鍵中斷驅動示例代碼:
1.驅動代碼:button_drv.c
#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>
#include <linux/poll.h>
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;
/*該宏初始化一個button_waitq隊列*/
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中斷事件標志, 中斷服務程序將它置1,button_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;
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
/*
* 確定按鍵值
*/
static irqreturn_t buttons_irq(int irq, void *dev_id)
{
struct pin_desc * pindesc = (struct pin_desc *)dev_id;
unsigned int pinval;
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 button_drv_open(struct inode *inode, struct file *file)
{
/* 配置GPF0,2為輸入引腳 */
/* 配置GPG3,11為輸入引腳 */
request_irq(IRQ_EINT0, buttons_irq, IRQT_BOTHEDGE, "S2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQT_BOTHEDGE, "S3", &pins_desc[1]);
request_irq(IRQ_EINT11, buttons_irq, IRQT_BOTHEDGE, "S4", &pins_desc[2]);
request_irq(IRQ_EINT19, buttons_irq, IRQT_BOTHEDGE, "S5", &pins_desc[3]);
return 0;
}
ssize_t button_drv_read(struct file *file, char __user *buf, size_t size, loff_t *ppos)
{
if (size != 1)
return -EINVAL;
/* 如果沒有按鍵動作, 休眠 */
wait_event_interruptible(button_waitq, ev_press); //ev_press : 0-休眠,1-返回
/* 如果有按鍵動作, 返回鍵值 */
copy_to_user(buf, &key_val, 1);
ev_press = 0;
return 1;
}
int button_drv_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]);
free_irq(IRQ_EINT19, &pins_desc[3]);
return 0;
}
static unsigned button_drv_poll(struct file *file, poll_table *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait); // 不會立即休眠
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static struct file_operations button_drv_fops = {
.owner = THIS_MODULE, /* 這是一個宏,推向編譯模塊時自動創建的__this_module變量 */
.open = button_drv_open,
.read = button_drv_read,
.release = button_drv_close,
.poll = button_drv_poll,
};
int major;
static int button_drv_init(void)
{
major = register_chrdev(0, "button_drv", &button_drv_fops);
buttondrv_class = class_create(THIS_MODULE, "button_drv");
buttondrv_class_dev = class_device_create(buttondrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
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);
return 0;
}
module_init(button_drv_init);
module_exit(button_drv_exit);
MODULE_LICENSE("GPL");
2.測試代碼:buttondrvtest.c
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
/* buttondrvtest */
int main(int argc, char **argv)
{
int fd;
unsigned char key_val;
int ret;
struct pollfd fds[1]; //同時可以查詢多個,當前只定義一個
fd = open("/dev/buttons", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
fds[0].fd = fd; //要查詢的文件
fds[0].events = POLLIN; //期待獲得的值,POLLIN:有數據等待讀取
while (1)
{
//read(fd, &key_val, 1); //wait_event_interruptible(button_waitq, ev_press); 會休眠阻塞到中斷發生返回,使用poll機制可以避免此問題
//printf("key_val = 0x%x\n", key_val);
//sleep(5);
//int poll(struct pollfd *fds, nfds_t nfds, int timeout);
ret = poll(fds, 1, 5000);
if (ret == 0)
{
printf("time out\n");
}
else
{
read(fd, &key_val, 1);
printf("key_val = 0x%x\n", key_val);
}
}
return 0;
}
3.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