以【tiny210 按鍵實驗爲例】
準備:
內核:Linux-3.0.8 (開發板的運行內核)
平臺:Fedora14
例子:
建立空文件夾(ko文件),在裏面添加需要製成的文件:
內核源碼:my_button.c
Makefile文件:Makefile
測試文件:buttons_test.c
編輯內核源碼:my_button.c 【採用按鍵驅動(異步通知機制)】 ———部分代碼
my_button.c:
#define DEVICE_NAME "buttons_test"
static struct fasync_struct *button_async;
/*
* 按鍵中斷出發確定按鍵值
*在中斷處理程序中調用kill_fasync函數
*/
static irqreturn_t button_interrupt(int irq, void *dev_id)
{
struct button_desc *bdata = (struct button_desc *)dev_id;
mod_timer(&bdata->timer, jiffies + msecs_to_jiffies(40));
//發送信號SIGIO信號給fasync_struct 結構體所描述的PID,觸發應用程序的SIGIO信號處理函數
kill_fasync(&button_async, SIGIO, POLL_IN);
return IRQ_HANDLED;
}
/*
*驅動fasync接口實現
*/
static int mini210_buttons_fasync (int fd, struct file *filp, int on)
{
printk("driver: fifth_drv_fasync\n");
//初始化/釋放 fasync_struct 結構體 (fasync_struct->fa_file->f_owner->pid)
return fasync_helper(fd, filp, on, &button_async);
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.open = mini210_buttons_open,
.release = mini210_buttons_close,
.read = mini210_buttons_read,
.poll = mini210_buttons_poll,
.fasync = mini210_buttons_fasync,
};
異步通知機制:
在Linux下,異步通知類似於信號機制,內核和應用程序之間採用通知方法來告知是否發生對應的事件,並進一步採取相應的動作,當產生按鍵動作時,發生中斷,由驅動程序使用kill_fasync函數告知應用程序,而應用程序需要向內核提供PID,然後就可以工作了。
Makefile文件:Makefile
內容如下:
obj-m := my_button.o #要生成的模塊名
my_button-objs:= module #生成這個模塊名所需要的目標文件
#KDIR := /lib/modules/`uname -r`/build #Fedora14 默認的內核目錄
KDIR := /opt/FriendlyARM/tiny210/android/linux-3.0.8 #自定義內核目錄(tiny210運行內核)
PWD := $(shell pwd)
MAKE:=make
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
說明:
obj-m = *.o
obj-y = *.o
上面兩者的區別在於,前者纔會生成ko文件,後者只是代碼編譯進內核,並不生成ko文件。
生成KO文件,分兩種情況:單個.c文件和多個.c文件
單個.c文件:
kernel配置文件中定義
CONFIG_RUNYEE_CAMVIB=m
注意上面的m,表示作爲一個模塊進行編譯,最後在MAKEFILE中需要用到的編譯開關。
然後再相應的源碼目錄中的MAKEFILE中添加如下語句:
obj-$(CONFIG_RUNYEE_CAMVIB) := camvib.o
上面的一行的作用就是編譯camvib.c的源文件,同時會生成相應的camvib.ko文件,和編譯生成的camvib.o在同一目錄
最後就是insmod動作了:
insmod /system/lib/modules/camvib.ko
2.多個.c文件生成ko文件
kernel配置文件中定義
CONFIG_TOUCHSCREEN_FOCALTECH=m
注意上面的m,表示作爲一個模塊進行編譯,最後在MAKEFILE中需要用到的編譯開關。
然後再相應的源碼目錄中的MAKEFILE中添加如下語句:
obj-$(CONFIG_TOUCHSCREEN_FOCALTECH) += focaltech_ts.o
focaltech_ts-objs := focaltech.o
focaltech_ts-objs += focaltech_ctl.o
focaltech_ts-objs += focaltech_ex_fun.o
上面的意思就是編譯生成ko文件需要三個.c文件【focaltech.c focaltech_ctl.c focaltech_ex_fun.c】,最後
生成名爲focaltech_ts的ko文件,注意ko文件名一定不能爲focaltech。那麼在obj-m和lpc-objs中都含有focaltech.o,
對make來講會產生循環和混淆,因此也不能這樣書寫
最後就是insmod動作了:
insmod /system/lib/modules/focaltech_ts.ko
注意事項:
1、內核目錄
2、Makefile中obj-m:=my_button.o 這個和源文件my_button.c要對應
3、my_button-objs:=module 這個my_button也是和my_button.c對應的
如果源文件爲your.c
這兩句話就應該改爲obj-m:=your.o
your-objs:=module
4、查看輸出的時候 用dmesg輸出信息太多,可以用grep過濾一下
dmesg | grep “buttons_test”
測試文件:buttons_test.c
核心代碼:
//在應用程序中捕捉SIGIO信號(由驅動程序發送)
signal(SIGIO, my_signal_fun);
//將當前進程PID設置爲fd文件所對應驅動程序將要發送SIGIO,SIGUSR信號進程PID
fcntl(buttons_fd, F_SETOWN, getpid());
//獲取fd的打開方式
Oflags = fcntl(buttons_fd, F_GETFL);
//將fd的打開方式設置爲FASYNC — 即 支持異步通知
//該行代碼執行會觸發 驅動程序中 file_operations->fasync 函數 ——fasync函數調用fasync_helper初始化一個fasync_struct結構體,該結構體描述了將要發送信號的進程PID (fasync_struct->fa_file->f_owner->pid)
fcntl(buttons_fd, F_SETFL, Oflags | FASYNC);
代碼如下:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <signal.h>
int buttons_fd;
int break_flg;
char buttons[8] = {'0', '0', '0', '0', '0', '0', '0', '0'};
//信號處理函數
void my_signal_fun(int signum)
{
char current_buttons[8];
int count_of_changed_key;
int i;
if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
perror("read buttons:");
exit(1);
}
for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {
if (buttons[i] != current_buttons[i]) {
buttons[i] = current_buttons[i];
printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "0" : "1");
count_of_changed_key++;
}
}
if (count_of_changed_key) {
printf("\n");
}
if((buttons[0] == '1')&&(buttons[7]=='1'))
{
printf("key test off\n");
break_flg = 1;
}
}
int main(void)
{
int Oflags;
break_flg = 0;
//在應用程序中捕捉SIGIO信號(由驅動程序發送)
signal(SIGIO, my_signal_fun);
buttons_fd = open("/dev/buttons_test", 0);
if (buttons_fd < 0) {
perror("open device buttons");
exit(1);
}
//將當前進程PID設置爲fd文件所對應驅動程序將要發送SIGIO,SIGUSR信號進程PID
fcntl(buttons_fd, F_SETOWN, getpid());
//獲取fd的打開方式
Oflags = fcntl(buttons_fd, F_GETFL);
//將fd的打開方式設置爲FASYNC --- 即 支持異步通知
//該行代碼執行會觸發 驅動程序中 file_operations->fasync 函數 ------fasync函數調用fasync_helper初始化一個fasync_struct結構體,該結構體描述了將要發送信號的進程PID (fasync_struct->fa_file->f_owner->pid)
fcntl(buttons_fd, F_SETFL, Oflags | FASYNC);
while (1)
{
sleep(1000);
if(break_flg == 1)
break;
}
close(buttons_fd);
return 0;
}
完整代碼:
//內核源碼:
/*
* linux/drivers/char/mini210_buttons.c
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*/
#include <linux/module.h>
#include <linux/kernel.h>
#include <linux/fs.h>
#include <linux/init.h>
#include <linux/delay.h>
#include <linux/sched.h>
#include <linux/poll.h>
#include <linux/irq.h>
#include <asm/irq.h>
#include <asm/io.h>
#include <linux/interrupt.h>
#include <asm/uaccess.h>
#include <mach/hardware.h>
#include <linux/platform_device.h>
#include <linux/cdev.h>
#include <linux/miscdevice.h>
#include <mach/map.h>
#include <mach/gpio.h>
#include <mach/regs-clock.h>
#include <mach/regs-gpio.h>
#define DEVICE_NAME "buttons_test"
struct button_desc {
int gpio;
int number;
char *name;
struct timer_list timer;
};
static struct button_desc buttons[] = {
{ S5PV210_GPH2(0), 0, "KEY0" },
{ S5PV210_GPH2(1), 1, "KEY1" },
{ S5PV210_GPH2(2), 2, "KEY2" },
{ S5PV210_GPH2(3), 3, "KEY3" },
{ S5PV210_GPH3(0), 4, "KEY4" },
{ S5PV210_GPH3(1), 5, "KEY5" },
{ S5PV210_GPH3(2), 6, "KEY6" },
{ S5PV210_GPH3(3), 7, "KEY7" },
};
static volatile char key_values[] = {
'0', '0', '0', '0', '0', '0', '0', '0'
};
static DECLARE_WAIT_QUEUE_HEAD(button_waitq);
static volatile int ev_press = 0;
static struct fasync_struct *button_async;
static void mini210_buttons_timer(unsigned long _data)
{
struct button_desc *bdata = (struct button_desc *)_data;
int down;
int number;
unsigned tmp;
tmp = gpio_get_value(bdata->gpio);
/* active low */
down = !tmp;
printk("KEY %d: %08x\n", bdata->number, down);
number = bdata->number;
if (down != (key_values[number] & 1)) {
key_values[number] = '0' + down;
ev_press = 1;
wake_up_interruptible(&button_waitq);
}
}
static irqreturn_t button_interrupt(int irq, void *dev_id)
{
struct button_desc *bdata = (struct button_desc *)dev_id;
mod_timer(&bdata->timer, jiffies + msecs_to_jiffies(40));
//發送信號SIGIO信號給fasync_struct 結構體所描述的PID,觸發應用程序的SIGIO信號處理函數
kill_fasync(&button_async, SIGIO, POLL_IN);
return IRQ_HANDLED;
}
static int mini210_buttons_open(struct inode *inode, struct file *file)
{
int irq;
int i;
int err = 0;
for (i = 0; i < ARRAY_SIZE(buttons); i++) {
if (!buttons[i].gpio)
continue;
setup_timer(&buttons[i].timer, mini210_buttons_timer,
(unsigned long)&buttons[i]);
irq = gpio_to_irq(buttons[i].gpio);
err = request_irq(irq, button_interrupt, IRQ_TYPE_EDGE_BOTH,
buttons[i].name, (void *)&buttons[i]);
if (err)
break;
}
if (err) {
i--;
for (; i >= 0; i--) {
if (!buttons[i].gpio)
continue;
irq = gpio_to_irq(buttons[i].gpio);
disable_irq(irq);
free_irq(irq, (void *)&buttons[i]);
del_timer_sync(&buttons[i].timer);
}
return -EBUSY;
}
ev_press = 1;
return 0;
}
static int mini210_buttons_close(struct inode *inode, struct file *file)
{
int irq, i;
for (i = 0; i < ARRAY_SIZE(buttons); i++) {
if (!buttons[i].gpio)
continue;
irq = gpio_to_irq(buttons[i].gpio);
free_irq(irq, (void *)&buttons[i]);
del_timer_sync(&buttons[i].timer);
}
return 0;
}
static int mini210_buttons_read(struct file *filp, char __user *buff,
size_t count, loff_t *offp)
{
unsigned long err;
if (!ev_press) {
if (filp->f_flags & O_NONBLOCK)
return -EAGAIN;
else
wait_event_interruptible(button_waitq, ev_press);
}
ev_press = 0;
err = copy_to_user((void *)buff, (const void *)(&key_values),
min(sizeof(key_values), count));
return err ? -EFAULT : min(sizeof(key_values), count);
}
static unsigned int mini210_buttons_poll( struct file *file,
struct poll_table_struct *wait)
{
unsigned int mask = 0;
poll_wait(file, &button_waitq, wait);
if (ev_press)
mask |= POLLIN | POLLRDNORM;
return mask;
}
static int mini210_buttons_fasync (int fd, struct file *filp, int on)
{
printk("driver: fifth_drv_fasync\n");
//初始化/釋放 fasync_struct 結構體 (fasync_struct->fa_file->f_owner->pid)
return fasync_helper(fd, filp, on, &button_async);
}
static struct file_operations dev_fops = {
.owner = THIS_MODULE,
.open = mini210_buttons_open,
.release = mini210_buttons_close,
.read = mini210_buttons_read,
.poll = mini210_buttons_poll,
.fasync = mini210_buttons_fasync,
};
static struct miscdevice misc = {
.minor = MISC_DYNAMIC_MINOR,
.name = DEVICE_NAME,
.fops = &dev_fops,
};
static int __init button_dev_init(void)
{
int ret;
ret = misc_register(&misc);
printk(DEVICE_NAME"\tinitialized\n");
return ret;
}
static void __exit button_dev_exit(void)
{
misc_deregister(&misc);
}
module_init(button_dev_init);
module_exit(button_dev_exit);
MODULE_LICENSE("GPL");
MODULE_AUTHOR("FriendlyARM Inc.");
Makefile:
obj-m := my_button.o #要生成的模塊名
my_buttonmodule-objs:= module #生成這個模塊名所需要的目標文件
#KDIR := /lib/modules/`uname -r`/build
KDIR := /opt/FriendlyARM/tiny210/android/linux-3.0.8
PWD := $(shell pwd)
MAKE:=make
default:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) modules
clean:
$(MAKE) -C $(KDIR) SUBDIRS=$(PWD) clean
#default:
# make -C $(KDIR) M=$(PWD) modules
#clean:
# rm -rf *.o *.cmd *.ko *.mod.c .tmp_versions
測試代碼:buttons_test.c
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/select.h>
#include <sys/time.h>
#include <errno.h>
#include <signal.h>
int buttons_fd;
int break_flg;
char buttons[8] = {'0', '0', '0', '0', '0', '0', '0', '0'};
//信號處理函數
void my_signal_fun(int signum)
{
char current_buttons[8];
int count_of_changed_key;
int i;
if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
perror("read buttons:");
exit(1);
}
for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {
if (buttons[i] != current_buttons[i]) {
buttons[i] = current_buttons[i];
printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "0" : "1");
count_of_changed_key++;
}
}
if (count_of_changed_key) {
printf("\n");
}
if((buttons[0] == '1')&&(buttons[7]=='1'))
{
printf("key test off\n");
break_flg = 1;
//close(buttons_fd);
}
}
int main(void)
{
int Oflags;
break_flg = 0;
//在應用程序中捕捉SIGIO信號(由驅動程序發送)
signal(SIGIO, my_signal_fun);
buttons_fd = open("/dev/buttons_test", 0);
if (buttons_fd < 0) {
perror("open device buttons");
exit(1);
}
//將當前進程PID設置爲fd文件所對應驅動程序將要發送SIGIO,SIGUSR信號進程PID
fcntl(buttons_fd, F_SETOWN, getpid());
//獲取fd的打開方式
Oflags = fcntl(buttons_fd, F_GETFL);
//將fd的打開方式設置爲FASYNC --- 即 支持異步通知
//該行代碼執行會觸發 驅動程序中 file_operations->fasync 函數 ------fasync函數調用fasync_helper初始化一個fasync_struct結構體,該結構體描述了將要發送信號的進程PID (fasync_struct->fa_file->f_owner->pid)
fcntl(buttons_fd, F_SETFL, Oflags | FASYNC);
while (1)
{
sleep(1000);
if(break_flg == 1)
break;
}
for (;;) {
char current_buttons[8];
int count_of_changed_key;
int i;
if (read(buttons_fd, current_buttons, sizeof current_buttons) != sizeof current_buttons) {
perror("read buttons:");
exit(1);
}
for (i = 0, count_of_changed_key = 0; i < sizeof buttons / sizeof buttons[0]; i++) {
if (buttons[i] != current_buttons[i]) {
buttons[i] = current_buttons[i];
printf("%skey %d is %s", count_of_changed_key? ", ": "", i+1, buttons[i] == '0' ? "0" : "1");
count_of_changed_key++;
}
}
if (count_of_changed_key) {
printf("\n");
}
if((buttons[0] == '1')&&(buttons[7]=='1'))
{
printf("key test off\n");
break;
}
}
close(buttons_fd);
return 0;
}