linux驅動學習筆記6

2021-06-29 08:18:04 字數 3843 閱讀 9653

在open函式中寫申請中斷函式 request_irq

int request_irq(unsigned

int irq,

void (*handler)(int irq, void *dev_id, struct pt_regs *regs ),

unsigned

long irqflags,

const

char * devname,

void *dev_id);

irq是要申請的硬體中斷號,在include\asm-arm\arch-s3c2410\irqs.h中定義。

#define irq_eint0      s3c2410_irq(0)       /* 16 */

#define irq_eint1 s3c2410_irq(1)

#define irq_eint2 s3c2410_irq(2)

#define irq_eint3 s3c2410_irq(3)

#define irq_eint4t7 s3c2410_irq(4) /* 20 */

#define irq_eint8t23 s3c2410_irq(5)

#define irq_reserved6 s3c2410_irq(6) /* for s3c2410 */

#define irq_cam s3c2410_irq(6) /* for s3c2440,s3c2443 */

#define irq_batt_flt s3c2410_irq(7)

#define irq_tick s3c2410_irq(8) /* 24 */

#define irq_wdt s3c2410_irq(9) /* wdt/ac97 for s3c2443 */

#define irq_timer0 s3c2410_irq(10)

#define irq_timer1 s3c2410_irq(11)

#define irq_timer2 s3c2410_irq(12)

#define irq_timer3 s3c2410_irq(13)

#define irq_timer4 s3c2410_irq(14)

#define irq_uart2 s3c2410_irq(15)

#define irq_lcd s3c2410_irq(16) /* 32 */

#define irq_dma0 s3c2410_irq(17) /* irq_dma for s3c2443 */

#define irq_dma1 s3c2410_irq(18)

#define irq_dma2 s3c2410_irq(19)

#define irq_dma3 s3c2410_irq(20)

#define irq_sdi s3c2410_irq(21)

#define irq_spi0 s3c2410_irq(22)

#define irq_uart1 s3c2410_irq(23)

#define irq_reserved24 s3c2410_irq(24) /* 40 */

#define irq_nfcon s3c2410_irq(24) /* for s3c2440 */

#define irq_usbd s3c2410_irq(25)

#define irq_usbh s3c2410_irq(26)

#define irq_iic s3c2410_irq(27)

#define irq_uart0 s3c2410_irq(28) /* 44 */

#define irq_spi1 s3c2410_irq(29)

#define irq_rtc s3c2410_irq(30)

#define irq_adcparent s3c2410_irq(31)

handler是向系統登記的中斷處理函式。

irqflags是中斷觸發方式,如高電平觸發,下降沿觸發…。include\linux\irqs.h

#define irq_type_none       0x00000000  /* default, unspecified type */

#define irq_type_edge_rising 0x00000001 /* edge rising type */

#define irq_type_edge_falling 0x00000002 /* edge falling type */

#define irq_type_edge_both (irq_type_edge_falling | irq_type_edge_rising)

#define irq_type_level_high 0x00000004 /* level high type */

#define irq_type_level_low 0x00000008 /* level low type */

#define irq_type_sense_mask 0x0000000f /* mask of the above */

#define irq_type_probe 0x00000010 /* probing in progress */

devname是裝置名字。

devid是裝置id。

這個函式做了以下幾件事情:

a.分配乙個irqaction。

b.把這個結構放入irq_desc中。

c.設定引腳。

d.使能中斷。

函式執行正常時返回 0 ,否則返回對應錯誤的負值。

2.在close函式寫中斷釋放函式 free_irq

void free_irq(  unsigned int irq,

void * dev_id);

這個函式做了以下幾件事情:

a.出鏈。

b.禁止中斷。

c.設定引腳。

函式執行正常時返回 0 ,否則返回對應錯誤的負值。

3.寫中斷處理函式

中斷處理程式宣告:

static irqreturn_t intr_handler(int irq, void *dev_id, struct pt_regs *regs)
說明:

該型別與request_irq()引數中的handler所要求的引數型別相匹配。

int irq :中斷號;

void *dev_id :與request_irq()的引數dev_id一致,可以根據這個裝置id號得到相應裝置的資料結構,進而的到相應裝置的資訊和相關資料;

struct pt_regs *regs :它指向乙個資料結構,此結構儲存的是中斷之預處理器的暫存器和狀態。主要用在程式除錯,一般忽略。

返回值:中斷程式的返回值是乙個特殊型別——irqreturn_t。但是中斷程式的返回值卻只有兩個值irq_none和irq_handled。

irq_none:中斷程式接收到中斷訊號後發現這並不是註冊時指定的中斷原發出的中斷訊號;

irq_handled:接收到了準確的中斷訊號,並且作了相應正確的處理。

亦可以使用巨集irq_retval(x),若x為非0值,該巨集返回irq_handled,否則返回irq_none。

linux驅動學習筆記

1.先從最簡單的例子開始 include include module license dual bsd gpl static int hello init void printk kern alert hello,world n return 0 static void hello exit vo...

linux驅動學習筆記(linux驅動標頭檔案說明)

include 是在linux 2.6.29 include linux下面尋找原始檔。include 是在linux 2.6.29 arch arm include asm下面尋找原始檔。include 是在linux 2.6.29 arch arm mach s3c2410 include ma...

linux驅動學習筆記(linux驅動標頭檔案說明)

include 是在linux 2.6.29 include linux下面尋找原始檔。include 是在linux 2.6.29 arch arm include asm下面尋找原始檔。include 是在linux 2.6.29 arch arm mach s3c2410 include ma...