編寫按鍵中斷驅動——異步機制
- 硬件平臺:韋東山嵌入式Linxu開發板(S3C2440.v3)
- 軟件平臺:運行於VMware Workstation 12 Player下UbuntuLTS16.04_x64 系統
- 參考資料:《嵌入式Linux應用開發手冊》、《嵌入式Linux應用開發手冊第2版》
- 開發環境:Linux 2.6.22.6 內核、arm-linux-gcc-3.4.5-glibc-2.3.6工具鏈
目錄
一、前言
在之前按鍵中斷中的查詢方式、休眠——喚醒方式、poll機制都是屬於同步機制,就是應用程序一直在調用驅動程序。
提問:是否可以實現由驅動程序去提醒應用程序已經準備,可以調用。
回答:可以,在Linux中,可以使用異步機制,實現驅動程序通知應用程序。
場景:下面我藉助一下韋東山老師的場景來理解一下:
媽媽怎麼知道臥室裏小孩醒了?
- 時不時進房間看一下: 查詢方式——簡單,但是累
- 進去房間陪小孩一起睡覺,小孩醒了會吵醒她: 休眠-喚醒 ——不累,但是媽媽幹不了活了
- 媽媽要幹很多活,但是可以陪小孩睡一會,定個鬧鐘: poll 方式——要浪費點時間,但是可以繼續幹活。媽媽要麼是被小孩吵醒,要麼是被鬧鐘吵醒。
- 媽媽在客廳幹活,小孩醒了他會自己走出房門告訴媽媽: 異步通知——媽媽、小孩互不耽誤
二、要點
在編程之前,我們需要對下面的問題有個清晰的答案。
1、誰發送:驅動程序發
採用異步機制的時候,驅動程序可以去提醒應用程序,可以調用。
2、 發什麼:信號
這時候,驅動程序通過信號去提醒應用程序。
3、 發什麼信號: SIGIO
/linux-2.6.22.6/include/asm-parisc/signal.h
這個文件下定義了多種信號 ,而對於我們IO口的,採用SIGIO
這個信號值。
#define SIGHUP 1
#define SIGINT 2
#define SIGQUIT 3
#define SIGILL 4
#define SIGTRAP 5
#define SIGABRT 6
#define SIGIOT 6
#define SIGEMT 7
#define SIGFPE 8
#define SIGKILL 9
#define SIGBUS 10
#define SIGSEGV 11
#define SIGSYS 12 /* Linux doesn't use this */
#define SIGPIPE 13
#define SIGALRM 14
#define SIGTERM 15
#define SIGUSR1 16
#define SIGUSR2 17
#define SIGCHLD 18
#define SIGPWR 19
#define SIGVTALRM 20
#define SIGPROF 21
#define SIGIO 22
#define SIGPOLL SIGIO
#define SIGWINCH 23
#define SIGSTOP 24
#define SIGTSTP 25
#define SIGCONT 26
#define SIGTTIN 27
#define SIGTTOU 28
#define SIGURG 29
#define SIGLOST 30 /* Linux doesn't use this either */
#define SIGUNUSED 31
4、怎麼發:內核裏提供有函數
在內核中了這個函數
void kill_fasync(struct fasync_struct **fp, int sig, int band);
struct fasync_struct **fp
:定義一個結構體,用來區分是誰發送的,button_async->fa_file 非空時, 可以從中得到 PID,表示發給哪一個 APPint sig
:上面所提到的信號值int band
:標誌位,當 FASYNC 位發生變化時,會導致驅動程序的 fasync 被調用
5、發給誰:應用程序(APP 要把自己告訴驅動)
fcntl(fd, F_SETOWN, getpid());
通過getid()
把進程的PID傳給驅動
6、應用程序收到後做什麼:執行信號處理函數
定義一個執行信號處理函數
void my_signal_fun(int signum)
{
unsigned char key_val;
read(fd, &key_val, 1);
printf("key_val: 0x%x\n", key_val);
}
7、信號處理函數和信號,之間怎麼掛鉤: 應用程序註冊信號處理函數
signal(SIGIO, my_signal_fun);
void(* signal(int sig,void(* func)(int)))(int);
int sig
:設置處理功能的信號值。void(* func)(int)
:定義一個信號處理函數
三、代碼編寫
1、驅動程序
#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>
#include <linux/poll.h>
int major;
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;
static struct fasync_struct *button_async;
/* 鍵值: 按下時, 0x01, 0x02, 0x03, 0x04
* 鍵值: 鬆開時, 0x81, 0x82, 0x83, 0x84
*/
static unsigned char key_val;
/* 按鍵信息 */
struct pin_desc{
unsigned int pin;
unsigned int key_val;
};
/* 存儲4個按鍵的信息 */
struct pin_desc pins_desc[4] = {
{S3C2410_GPF0, 0x01},
{S3C2410_GPF2, 0x02},
{S3C2410_GPG3, 0x03},
{S3C2410_GPG11, 0x04},
};
/* 中斷註冊信息 */
struct buttonirq_decs{
unsigned int irq;
unsigned long flags;
const char *devname;
void *dev_id;
};
struct buttonirq_decs buttonirqs_decs[4] = {
{IRQ_EINT0, IRQT_BOTHEDGE, "S2", &pins_desc[0]},
{IRQ_EINT2, IRQT_BOTHEDGE, "S3", &pins_desc[1]},
{IRQ_EINT11, IRQT_BOTHEDGE, "S4", &pins_desc[2]},
{IRQ_EINT19, IRQT_BOTHEDGE, "S5", &pins_desc[3]},
};
/* 生成一個等待隊列的隊頭,名字爲button_waitq */
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
/* 中斷事件標誌, 中斷服務程序將它置1,button_drv_read將它清0 */
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;
pinval = s3c2410_gpio_getpin(pindesc->pin); //讀取IO口電平
if(pinval){
/* 鬆開 */
key_val = 0x80 | pindesc->key_val;
}else{
/* 按下 */
key_val = pindesc->key_val;
}
ev_press = 1; //發生中斷
wake_up_interruptible(&button_waitq); // 喚醒休眠的進程
kill_fasync(&button_async, SIGIO, POLL_IN); //發SIGIO信號
return IRQ_RETVAL(IRQ_HANDLED);
}
/* open驅動函數 */
static int button_drv_open(struct inode *inode, struct file *file)
{
unsigned int i;
int err;
/* 註冊中斷 */
for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++){
err = request_irq(buttonirqs_decs[i].irq, button_irq, buttonirqs_decs[i].flags,
buttonirqs_decs[i].devname, buttonirqs_decs[i].dev_id);
if(err){
printk("func button_drv_open err: request_irq num:%d\n", i);
break;
}
}
/* 出現錯誤,釋放已經註冊的中斷 */
if(err){
i--;
for (; i >= 0; i--)
free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);
return -EBUSY;
}
return 0;
}
/* read驅動函數 */
static ssize_t button_drv_read(struct file *file, char __user *buf, size_t nbytes, loff_t *ppos)
{
unsigned long ret = 0;
int err;
/* 如果沒有按鍵動作, 休眠 */
err = wait_event_interruptible(button_waitq, ev_press);
if (err){
printk("func button_drv_read() err: wait_event_interruptible\n");
return err;
}
/* 如果有按鍵動作, 返回鍵值 */
ret = copy_to_user(buf, &key_val, 1);
ev_press = 0; //清中斷標誌
if(ret < 0){
printk("func button_drv_read() err: copy_to_user\n");
return -EFAULT;
}
return sizeof(key_val);
}
/* close驅動函數 */
int button_drv_close (struct inode *inode, struct file *file)
{
int i;
/* 取消中斷 */
for(i = 0; i < (sizeof(buttonirqs_decs)/sizeof(buttonirqs_decs[0])); i++)
free_irq(buttonirqs_decs[i].irq, buttonirqs_decs[i].dev_id);
return 0;
}
unsigned int buton_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; //返回給do_poll函數中的mask
}
/* 調用fasync_helper根據on值是否button_async->fa_file=驅動文件file,裏面有PID */
static int button_drv_fasync(int fd, struct file *file, int on)
{
printk("driver: fifth_drv_fasync\n");
return fasync_helper(fd, file, on, &button_async);
}
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 = buton_drv_poll,
.fasync = button_drv_fasync,
};
/* 入口函數
* 執行”insmod button_drv.ko”命令時就會調用這個函數
*/
static int button_drv_init(void)
{
//註冊一個字符設備,名字爲button_drv
major = register_chrdev(0, "button_drv", &button_drv_fops);
//創建一個類,名字爲buttond_rv(/class/button_drv)
buttondrv_class = class_create(THIS_MODULE, "button_drv");
//創建一個設備節點,名爲button(/dev/button)
buttondrv_class_dev = class_device_create(buttondrv_class, NULL, MKDEV(major, 0), NULL, "button");
/* 映射物理地址 */
gpfcon = (volatile unsigned long *)ioremap(0x56000050, 16);
gpfdat = gpfcon + 1;
gpgcon = (volatile unsigned long *)ioremap(0x56000060, 16);
gpgdat = gpgcon + 1;
return 0;
}
/* 出口函數
* 執行”rmmod button_drv.ko”命令時就會調用這個函數
*/
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);
}
module_init(button_drv_init);
module_exit(button_drv_exit);
MODULE_LICENSE("GPL");
2、測試(應用)程序
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <stdio.h>
#include <poll.h>
#include <signal.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
int fd;
/* 信號處理函數 */
void my_signal_fun(int signum)
{
int ret;
unsigned char key_val;
ret = read(fd, &key_val, 1);
if(ret < 0)
printf(" func read() err\n");
else
printf("key_val = 0x%x\n", key_val);
}
int main(int argc, char **argv)
{
int Oflags;
signal(SIGIO, my_signal_fun); //給SIGIO 信號註冊信號處理函數my_signal_fun
fd = open("/dev/button", O_RDWR);
if (fd < 0)
{
printf("can't open!\n");
}
fcntl(fd, F_SETOWN, getpid()); //把進程 ID 告訴驅動
Oflags = fcntl(fd, F_GETFL); //取得文件描述符filedes的文件狀態標誌
fcntl(fd, F_SETFL, Oflags | FASYNC); //FASYNC位發生變化時,會導致驅動程序的fasync函數被調用
while (1)
{
sleep(1000);
}
return 0;
}
四、軟件執行流程
重點從②開始:
- ② APP 給 SIGIO 這個信號註冊信號處理函數 func, 以後 APP 收到 SIGIO 信號時,這個函數會被自動調用;
- ③ 把 APP 的 PID(進程 ID)告訴驅動程序,這個調用不涉及驅動程序,在內核的文件系統層次記錄 PID;
- ④ 讀取驅動程序文件 Flag;
- ⑤ 設置 Flag 裏面的 FASYNC 位爲 1: 當 FASYNC 位發生變化時,會導致驅動程序的 fasync 被調用;
- ⑥⑦ 調用 faync_helper,它會根據 FAYSNC 的值決定是否設置 button_async->fa_file=驅動文件
filp: 驅動文件 filp 結構體裏面含有之前設置的 PID。 - ⑧ APP 可以做其他事;
- ⑨⑩ 按下按鍵,發生中斷,驅動程序的中斷服務程序被調用,裏面調用 kill_fasync 發信號;
- ⑪⑫⑬ APP 收到信號後,它的信號處理函數被自動調用,可以在裏面調用 read 函數讀取按鍵。