查詢方式的按鍵驅動程式框架詳解

2021-10-10 20:35:41 字數 3196 閱讀 8360

static

struct class *button_class;

static

int major =0;

//先定義主裝置號1(用來尋找驅動程式)

static

struct button_operations *p_button_opr;

//在.**件後定義b_opr結構體後,在.c檔案中定義結構體的指標全域性變數,由底層**提供的

static

int button_open (

struct inode *node,

struct file *file)

//開啟函式

;static ssize_t button_read (

struct file *file,

char __user *buf, size_t size, loff_t *off)

//這裡的user是空的巨集,表示使用者態的buf,在核心,在驅動程式時不可以使用它

;static

struct file_operations button_fops =

;//定義file_operations 已實現open read write等函式

void

register_button_operations

(struct button_operations *opr)

//有底層**提供struct button_operations這個結構體

}void

unregister_button_operations

(void

)//有底層**提供struct button_operations這個結構體,撤銷裝置節點

}export_symbol

(register_button_operations)

;export_symbol

(unregister_button_operations)

;//定義的函式給另外驅動程式使用,需要用export將其符號匯出來

intbutton_init

(void

)//3初始化button函式,用來在核心註冊file_operations結構體,入口函式

void

button_exit

(void

)//4出口函式,用來登出file_operations

module_init

(button_init)

;module_exit

(button_exit)

;module_license

("gpl"

);

static

void board_***_button_init_gpio (

int which)

static

int board_***_button_read_gpio (

int which)

static

struct button_operations my_buttons_ops =

;int

board_***_button_init

(void

)//看驅動程式先看入口函式

void

board_***_button_exit

(void

)//出口函式登出這個結構體

module_init

(board_***_button_init)

;module_exit

(board_***_button_exit)

;module_license

("gpl"

);

#ifndef _button_drv_h

//防止標頭檔案被重複包含

#define _button_dev_h

struct button_operations

;//這兩個函式是給驅動使用的,需要在.**件中定義

void

register_button_operations

(struct button_operations *opr)

;//有底層**提供struct button_operations這個結構體

void

unregister_button_operations

(void);

//有底層**提供struct button_operations這個結構體,撤銷裝置節點

#endif

```c

intmain

(int argc,

char

**ar**)

fd = open (ar**[1]

,o_rdwr);if

(fd ==-1

)read

(fd,

&val,1)

;printf

("get button : %d\n"

,val)

;close

(fd)

;return0;

}

kern_dir =

/home/john/

100ask_imx6ull-sdk/linux-

4.9.88

//makefile檔案的kern_dir一定要為核心所在的目錄,因為驅動程式所用到的標頭檔案是不同的,不同的單板對應的標頭檔案是不同地

all:

make -c $(kern_dir) m=`pwd` modules

$(cross_compile)gcc -o button_test button_test.c

clean:

make -c $(kern_dir) m=`pwd` modules clean

rm -rf modules.order`在這裡插入**片`

rm -f ledtest

obj-m +

= button_drv.o //將button_drv.o編譯成button_drv.ko

obj-m +

= board_***.o //將board_***.o編譯成button_drv.ko

以上是今天按鍵驅動框架的學習,良好的習慣,收益終身,從今天開始也記錄下嵌入式linux的學習記錄吧,嘖嘖嘖。麼麼,哈哈哈!

按鍵對映 4 查詢方式來寫按鍵驅動程式 詳解

本節目標 寫second程式,內容 通過查詢方式驅動按鍵 1.寫出框架 1.1寫file oprations結構體,second drv open函式,second drv read函式 1.2寫入口函式,並自動建立裝置節點,修飾入口函式 1.3寫出口函式,並自動登出裝置節點,修飾出口函式 1.4 ...

arm 驅動基礎 查詢方式的按鍵驅動程式

原理圖 驅動程式 include include include include include include include include include include static struct class seconddrv class static struct class devic...

按鍵驅動程式之查詢方式的總結

按鍵驅動程式和led驅動程式相似,框架都是一樣的。file operations中定義 static struct file operations second drv fops 而這個程式使用的是查詢方式,所以要把引腳配置為輸入模式。看了原理圖可以知道,三個按鍵分別對應gpiog5 6 7pin,...