查詢的方式獲取按鍵值驅動的步驟大致如下:
一、框架
1.file_operations的填充
2.入口函式註冊register_chrdev
3.修飾入口函式/出口函式
4.自動建立裝置節點
二、硬體操作
一、框架:
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct class *buttons_class;
struct device *buttons_dev;
static int buttons_drv_open(struct inode *inode, struct file *file)
static ssize_t buttons_drv_read( struct file *file,
char __user *buffer,
size_t len,
loff_t *offset )
static struct file_operations buttons_fops = ;
int major;
static int __init buttons_drv_init(void)
static void __exit buttons_drv_exit(void)
module_init(buttons_drv_init);
module_exit(buttons_drv_exit);
module_license("gpl");
二、硬體操作:
檢視sch按鍵引腳對應的gpio如下:
keyint1----》gpn0
keyint2----》gpn1
keyint3----》gpn2
keyint4----》gpn3
keyint5----》gpn4
keyint6----》gpn5
open中配置為輸入引腳:gpncon &= ~0xfff
沒有按下時為高電平,按下後是低電平
read中讀取引腳的狀態: state = (gpndat | 1 << x) ? 1 : 0;
驅動**如下:
#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include struct class *buttons_class;
struct device *buttons_dev;
volatile unsigned long *gpncon = null;
volatile unsigned long *gpndat = null;
static int buttons_drv_open(struct inode *inode, struct file *file)
static ssize_t buttons_drv_read( struct file *file,
char __user *buffer,
size_t len,
loff_t *offset )
int i;
for(i = 0 ; i < 6 ; i++)
copy_to_user(buffer,reg_val,sizeof(reg_val));
return sizeof(reg_val);
}static struct file_operations buttons_fops = ;
int major;
static int __init buttons_drv_init(void)
static void __exit buttons_drv_exit(void)
module_init(buttons_drv_init);
module_exit(buttons_drv_exit);
module_license("gpl");
測試程式:
#include #include int main(int argc ,char **argv)
while(1) }
return 0;
}
這個是使用while迴圈的查詢方式進行讀取按鍵的值,
在驅動中的讀操作也一直處於一直讀的狀態,這樣會導致系統占用資源很多。
如下所示:
S3C6410按鍵驅動 中斷方式
本文 cpp view plain copy include include include include include include include include include include include include include 新版核心 include include st...
按鍵驅動程式之查詢方式的總結
按鍵驅動程式和led驅動程式相似,框架都是一樣的。file operations中定義 static struct file operations second drv fops 而這個程式使用的是查詢方式,所以要把引腳配置為輸入模式。看了原理圖可以知道,三個按鍵分別對應gpiog5 6 7pin,...
基於mini6410的linux按鍵驅動例項分析
1 button.c源 include include include include include include include include include include include include include include include include include in...