以下是一份加上詳細註解的 FL2440 按鍵中斷驅動,已實測通過。
平台:FL2440 核心版本:Linux 2.6.28
效果:沒有按鍵時程式進入休眠,每 5 秒印出超時資訊;按下按鍵時觸發中斷,並輸出是按下還是放開。
#include <linux/module.h> /* 模組相關 */ #include <linux/kernel.h> /* 核心相關 */ #include <linux/fs.h> /* 檔案系統相關 */ #include <linux/init.h> #include <linux/device.h> #include <linux/miscdevice.h> #include <linux/delay.h> #include <linux/irq.h> #include <linux/interrupt.h> /* Linux 中斷 */ #include <linux/poll.h> #include <asm/io.h> #include <asm/irq.h> #include <asm/uaccess.h> // copy_to_user #include <mach/regs-gpio.h> /* 暫存器設定,S3C2410_GPF0 等定義 */ #include <mach/hardware.h> // s3c2410_gpio_getpin 等定義 #include <mach/irqs.h> // IRQ_EINT0 等定義 #include <asm/system.h>
static struct class *thirddrv_class; // 載入模組時自動建立裝置檔
static struct class_device *thirddrv_class_dev;
volatile unsigned long *gpfcon; // IO 埠配置暫存器,FL2440 為 F 埠
volatile unsigned long *gpfdat; // IO 埠資料暫存器
static unsigned char keyval; // 鍵值,回傳使用者空間;1~4 為按下,0x81~0x84 為放開
static DECLARE_WAIT_QUEUE_HEAD(button_waitq); // 初始化等待佇列,供 read 休眠使用
static volatile int ev_press = 0; // 喚醒進程的標誌
struct pin_desc {
unsigned int pin;
unsigned int key_val;
}; // 引腳相關結構體
struct pin_desc pins_desc[4] = { // FL2440 按鍵 IO 實例化
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPF3, 0x03},
{S3C2410_GPF4, 0x04},
};
static irqreturn_t buttons_irq(int irq, void *dev_id) {
struct pin_desc *pindesc = (struct pin_desc *) dev_id; // 由 request_irq 傳入
unsigned int pinval; // 按鍵引腳電平
pinval = s3c2410_gpio_getpin(pindesc->pin); // 取得引腳電平
if (pinval) { // 依高低電平回傳不同鍵值
keyval = 0x80 | pindesc->key_val;
} else {
keyval = pindesc->key_val;
}
ev_press = 1; // 進程喚醒參數
wake_up_interruptible(&button_waitq); // 喚醒睡眠中的 read 進程
return IRQ_HANDLED;
}
static int third_drv_open(struct inode *inode, struct file *file) {
*gpfcon &= ~((0x2 << (0 * 2)) | (0x2 << (2 * 2)) | (0x2 << (3 * 2)) | (0x2 << (4 * 2))); // 設為中斷狀態
request_irq(IRQ_EINT0, buttons_irq, IRQF_TRIGGER_RISING, "s2", &pins_desc[0]);
request_irq(IRQ_EINT2, buttons_irq, IRQF_TRIGGER_RISING, "s3", &pins_desc[1]);
request_irq(IRQ_EINT3, buttons_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s4", &pins_desc[2]);
request_irq(IRQ_EINT4, buttons_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, "s5", &pins_desc[3]);
return 0;
}
request_irq 是 Linux 驅動中申請中斷的核心函數,需指定中斷號、引腳、服務程式、觸發方式與名稱。觸發方式要對照按鍵原理圖決定:EINT0、EINT2 設為上升沿觸發,EINT3、EINT4 則上升沿與下降沿都觸發。
ssize_t third_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 則進程休眠
copy_to_user(buf, &keyval, 1); // 中斷發生後回傳鍵值
ev_press = 0; // 參數歸零
return sizeof(keyval);
}
static unsigned int third_drv_poll(struct file *file, poll_table *wait) {
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
// 將進程掛到等待佇列,此處不休眠;是否休眠取決於 mask 回傳值
if (ev_press)
mask |= POLLIN | POLLRDNORM; // 中斷發生時 mask 非零,喚醒休眠
return mask;
}
int third_drv_close(struct inode *inode, struct file *file) {
free_irq(IRQ_EINT0, &pins_desc[0]);
free_irq(IRQ_EINT2, &pins_desc[0]);
free_irq(IRQ_EINT3, &pins_desc[0]);
free_irq(IRQ_EINT4, &pins_desc[0]);
return 0;
}
static struct file_operations sencod_drv_fops = {
.owner = THIS_MODULE,
.open = third_drv_open,
.read = third_drv_read,
.poll = third_drv_poll,
.release = third_drv_close,
};
int major;
static int third_drv_init(void) { // 向核心註冊模組
major = register_chrdev(0, "third_drv", &sencod_drv_fops);
thirddrv_class = class_create(THIS_MODULE, "third_drv");
thirddrv_class_dev = device_create(thirddrv_class, NULL, MKDEV(major, 0), NULL, "buttons"); /* /dev/buttons */
gpfcon = (volatile unsigned long *) ioremap(0x56000050, 16); // 物理位址轉虛擬位址
gpfdat = gpfcon + 1;
return 0;
}
static void third_drv_exit(void) { // 從核心卸載模組
unregister_chrdev(major, "third_drv");
device_unregister(thirddrv_class_dev);
class_destroy(thirddrv_class);
iounmap(gpfcon);
iounmap(gpgcon);
}
module_init(third_drv_init);
module_exit(third_drv_exit);
MODULE_LICENSE("GPL");
這份驅動示範了三件事如何協同運作:中斷負責即時反應、等待佇列負責讓 read 休眠喚醒、poll 機制負責讓上層程式以 select/epoll 監聽。三者缺一,按鍵輸入體驗就會大打折扣。