FS2410開發(fā)板上的按鍵驅(qū)動程序(2-1)
作者:趙明,華清遠(yuǎn)見嵌入式學(xué)院講師。
首先按鍵設(shè)備相關(guān)的數(shù)據(jù)結(jié)構(gòu)的定義如下所示:
/* butt_drv.h */
……
typedef struct _st_key_info_matrix /* 按鍵數(shù)據(jù)結(jié)構(gòu) */
{
uNSigned char key_id; /* 按鍵ID */
unsigned int IRq_no; /* 對應(yīng)的中斷號 */
unsigned int irq_gpio_port; /* 對應(yīng)的中斷線的輸入端口地址*/
unsigned int kscan_gpio_port; /* 對應(yīng)的KSCAN端口地址 */
} st_key_info_matrix;
typedef struct _st_key_buffer /* 按鍵緩沖數(shù)據(jù)結(jié)構(gòu) */
{
unsigned long jiffy[MAX_KEY_COUNT]; /* 按鍵時間, 5秒鐘以前的銨鍵作廢*/
unsigned char buf[MAX_KEY_COUNT]; /* 按鍵緩沖區(qū) */
unsigned int head,tail; /* 按鍵緩沖區(qū)頭和尾 */
} st_key_buffer;
……
下面是矩陣按鍵數(shù)組的定義,數(shù)組元素的信息(一個按鍵信息)按照0行0列,0行1列,…,3行2列,3行3列的順序逐行排列。
static st_key_info_matrix key_info_matrix[MAX_COLUMN][MAX_ROW] =
{
{{10, IRQ_EINT0, S3C2410_GPF0, S3C2410_GPE11}, /* 0行0列 */
{11, IRQ_EINT0, S3C2410_GPF0, S3C2410_GPG6},
{12, IRQ_EINT0, S3C2410_GPF0, S3C2410_GPE13},
{16, IRQ_EINT0, S3C2410_GPF0, S3C2410_GPG2}},
{{7, IRQ_EINT2, S3C2410_GPF2, S3C2410_GPE11}, /* 1行0列 */
{8, IRQ_EINT2, S3C2410_GPF2, S3C2410_GPG6},
{9, IRQ_EINT2, S3C2410_GPF2, S3C2410_GPE13},
{15, IRQ_EINT2, S3C2410_GPF2, S3C2410_GPG2}},
{{4, IRQ_EINT11, S3C2410_GPG3, S3C2410_GPE11}, /* 2行0列 */
{5, IRQ_EINT11, S3C2410_GPG3, S3C2410_GPG6},
{6, IRQ_EINT11, S3C2410_GPG3, S3C2410_GPE13},
{14, IRQ_EINT11, S3C2410_GPG3, S3C2410_GPG2}},
{{1, IRQ_EINT19, S3C2410_GPG11, S3C2410_GPE11}, /* 3行0列 */
{2, IRQ_EINT19, S3C2410_GPG11, S3C2410_GPG6},
{3, IRQ_EINT19, S3C2410_GPG11, S3C2410_GPE13},
{13, IRQ_EINT19, S3C2410_GPG11, S3C2410_GPG2}},
};
下面是與按鍵相關(guān)的端口的初始化函數(shù)。這些函數(shù)已經(jīng)在簡單的GPIO字符設(shè)備驅(qū)動程序里被使用過。此外,set_irq_type()函數(shù)用于設(shè)定中斷線的類型,在本實(shí)例中通過該函數(shù)將4個中斷線的類型配置為下降沿觸發(fā)式。
static void init_gpio(void)
{
s3c2410_gpio_cfgpin(S3C2410_GPE11, S3C2410_GPE11_OUTP); /* GPE11 */
s3c2410_gpio_setpin(S3C2410_GPE11, 0);
s3c2410_gpio_cfgpin(S3C2410_GPE13, S3C2410_GPE13_OUTP); /* GPE13 */
s3c2410_gpio_setpin(S3C2410_GPE13, 0);
s3c2410_gpio_cfgpin(S3C2410_GPG2, S3C2410_GPG2_OUTP); /* GPG2 */
s3c2410_gpio_setpin(S3C2410_GPG2, 0);
s3c2410_gpio_cfgpin(S3C2410_GPG6, S3C2410_GPG6_OUTP); /* GPG6 */
s3c2410_gpio_setpin(S3C2410_GPG6, 0);
s3c2410_gpio_cfgpin(S3C2410_GPF0, S3C2410_GPF0_EINT0); /* GPF0 */
s3c2410_gpio_cfgpin(S3C2410_GPF2, S3C2410_GPF2_EINT2); /* GPF2 */
s3c2410_gpio_cfgpin(S3C2410_GPG3, S3C2410_GPG3_EINT11); /* GPG3 */
s3c2410_gpio_cfgpin(S3C2410_GPG11, S3C2410_GPG11_EINT19); /* GPG11 */
set_irq_type(IRQ_EINT0, IRQT_FALLING);
set_irq_type(IRQ_EINT2, IRQT_FALLING);
set_irq_type(IRQ_EINT11, IRQT_FALLING);
set_irq_type(IRQ_EINT19, IRQT_FALLING);
}
現(xiàn)在討論按鍵驅(qū)動的主要接口,以下為驅(qū)動模塊的入口和卸載函數(shù)。
/* 初始化并添加struct cdev結(jié)構(gòu)到系統(tǒng)之中 */
static void button_setup_cdev(struct cdev *dev,
int minor, struct file_operations *fops)
{
int err;
int devno = MKDEV(button_major,minor);
cdev_init(dev, fops); /* 初始化結(jié)構(gòu)體struct cdev */
dev->owner = THIS_MODULE;
dev->ops = fops; /* 關(guān)聯(lián)到設(shè)備的file_operations結(jié)構(gòu) */
err = cdev_add(dev, devno, 1); /* 將struct cdev結(jié)構(gòu)添加到系統(tǒng)之中 */
if (err)
{
printk(KERN_INFO"Error %d adding button %d\n",err, minor);
}
}
……
/* 驅(qū)動初始化 */
static int button_init(void)
{
int ret;
/* 將主設(shè)備號和次設(shè)備號定義到一個dev_t數(shù)據(jù)類型的結(jié)構(gòu)體之中 */
dev_t dev = MKDEV(button_major, 0);
if (button_major)
{/* 靜態(tài)注冊一個設(shè)備,設(shè)備號先前指定好,并設(shè)定設(shè)備名,用cat /proc/devices來查看 */
ret = register_chrdev_region(dev, 1, BUTT*_DEVICE_NAME);
}
else
{ /*由系統(tǒng)動態(tài)分配主設(shè)備號 */
ret = alloc_chrdev_region(&dev, 0, 1, BUTT*_DEVICE_NAME);
button_major = MAJOR(dev); /* 獲得主設(shè)備號 */
}
if (ret < 0)
{
printk(KERN_WARNING"Button:unable to get major %d\n",button_major);
return ret;
}
/* 初始化和添加結(jié)構(gòu)體struct cdev到系統(tǒng)之中 */
button_setup_cdev(&button_dev, 0, &button_fops);
printk("Button driver initialized.\n");
return 0;
}
/* 驅(qū)動卸載 */
static void __exit button_exit(void)
{
cdev_del(&button_dev); /* 刪除結(jié)構(gòu)體struct cdev */
/* 卸載設(shè)備驅(qū)動所占有的資源 */
unregister_chrdev_region(MKDEV(button_major, 0), 1);
printk("Button driver uninstalled\n");
}
module_init(button_init); /* 初始化設(shè)備驅(qū)動程序的入口 */
module_exit(button_exit); /* 卸載設(shè)備驅(qū)動程序的入口 */
MODULE_AUTHOR("David");
MODULE_LICENSE("Dual BSD/GPL");
“本文由華清遠(yuǎn)見http://www.embedu.org/index.htm提供”
華清遠(yuǎn)見