linux .ko的编译与测试
来源:互联网 发布:不干胶排版软件 编辑:程序博客网 时间:2024/05/18 03:21
以【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`/buildKDIR := /opt/FriendlyARM/tiny210/android/linux-3.0.8PWD := $(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;}
- linux .ko的编译与测试
- arm-linux交叉编译ko的makefile
- LINUX编译ko文件
- linux 系统 ko模块编译
- Linux内核编译 ko文件
- linux 系统 ko模块编译
- linux 系统 ko模块编译
- linux 模块层叠技术和多个ko的编译
- 编译我的hello.ko
- Linux驱动中的.o文件与.ko文件的区别
- Linux驱动中的.o文件与.ko文件的区别
- Linux 基本module结构与hello.ko的编写
- linux helloword 内核编译.ko菜鸟记录
- Linux内核驱动模块测试 hello.ko
- Linux下的.ko文件
- 在kernel中编译单独的.ko
- 编译动态modules ko的makefile
- 编译.ko的简单Makefile实例
- debian上使用dpkg -b打包
- solidity投票合约在bcos上的部署及解析(二)
- C#进阶6_部分类、密封类、多态的手段、ToString用法
- 调戏和窥探 iPhone X
- UVALive 4683(鸽巢原理 O(n))
- linux .ko的编译与测试
- Sublime配置
- 基于netty的文件上传下载组件
- PHPExcel导出类(列数超过Z,导出图片)
- [11.6] 日常模拟
- kong api可视化管理工具konga安装使用教程
- 解决未能加载文件或程序集“Office,Version=15.0.0.0, Culture=neutral, PublicKeyToken=71e9bce111e9429c”或它的某一个依赖项
- Windows下查找文件或文件夹被哪个进程占用
- AR大屏互动自己整合基础框架