Linux GPIO中斷非同步通知

2021-07-25 00:05:44 字數 2700 閱讀 8133

fasync機制是非同步通知機制,當驅動程式向應用程式傳送訊號量,觸發應用程式的訊號處理函式,以達到類似中斷的效果

驅動程式中:

1、在檔案專屬的fasync函式中,呼叫了fasync_helper,將屬主資訊通知給核心

2、當發生按鍵中斷,進入按鍵中斷服務程式,讀取鍵值,呼叫kill_fasync,傳送訊號量給相應程序

應用程式中:

1、使用signal,設定訊號處理函式

2、使用fcntl(fd,f_setown,getpid());,指定乙個程序作為檔案的屬主,屬主的程序id號就儲存在filp->f_owner中

3、fcntl獲取、設定標誌,設定標誌的時候,最終會呼叫到檔案專屬的fasync函式

4、當有按鍵中斷發生,驅動程式傳送訊號量,觸發應用程式的訊號處理函式,讀取鍵值

/* raspberry pi gpio interrupt processing example

* light on led with button press

*/#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#include

#define success (0)

#define error (-1)

static

struct fasync_struct *fasync_queue;

//非同步通知佇列

#define button_pin 25

/* gpio 25 */

int flag =0;

struct cdev *button_dev;

//使用cdev結構體來描述字元裝置

dev_t devnum;

//通過其成員dev_t來定義裝置號(分為主、次裝置號)以確定字元裝置的唯一性

unsigned

int subdevnum =1;

//請求的連續裝置個數

int reg_major =

501;

//主裝置號

int reg_minor =0;

//次裝置號

static irqreturn_t irq_handler

(int irq,

void

*dev)

return irq_handled;

}static

intbutton_fasync

(int fd,

struct file * filp,

int on)

static

struct file_operations dev_fops =

;static

intbutton_init

(void

)else

printk

(kern_info " button driver init \n");

button_dev =

kzalloc

(sizeof

(struct cdev)

, gfp_kernel)

;cdev_init

(button_dev,

&dev_fops)

;cdev_add

(button_dev, devnum,1)

;printk

(kern_info "led init\n");

err =

gpio_request_one

(button_pin, gpiof_in,

"light button");

if(err)

return err;

enable_irq

(gpio_to_irq

(button_pin));

err =

request_irq

(gpio_to_irq

(button_pin)

, irq_handler,

irqf_trigger_falling,

"led test"

,null);

if(err <0)

flag =1;

return0;

}static

void

button_exit

(void

)module_init

(button_init)

;module_exit

(button_exit)

;module_license

("dual bsd/gpl"

);

#include

#include

#include

#include

#include

#include

static

int fd;

/* 核心產生非同步通知,呼叫該應用層函式處理 */

void

sigterm_handler

(int signo)

intmain

(void

)close

(fd)

;return0;

}

Linux中斷非同步通知筆記

非同步通知筆記 應用程式需要完成如下三個步驟 1 signal sigio,sig handler 呼叫signal函式,讓指定的訊號sigio與處理函式sig handler對應。2 fcntl fd,f set owner,getpid 指定乙個程序作為檔案的 屬主 filp owner 這樣核...

Linux gpio 按鍵中斷學習

先說明一下按鍵與s3c2440晶元的連線方式 key1 eint8 gpg0 key2 eint11 gpg3 key3 eint13 gpg5 key4 eint14 gpg6 key5 eint15 gpg7 key6 eint19 gpg11 驅動程式原始碼如下 drivers char mi...

linux GPIO讀取以及中斷註冊

home uart ring gpx1 1 back sim det gpx1 2 switch3 ap sleep gpc0 3 switch4 xeint6 gpx0 6 步驟 申請gpio gpio request exynos4 gpx1 1 home 設定模式,輸入模式 s3c gpio ...