内核不是开源的,只能选择.ko文件加载驱动
主板:么么厂家的rk3288,非瑞芯微官方板子
瑞芯微官网下载SDK,编译后的内核写进板子,板子无法启动,显然厂家在瑞芯微的基础上改硬件了
最简单的方法是修改按键驱动,修改成模块,然后insmod加载,提示
log: version magic '4.4.192 mod_unload ARMv5 ' should be '4.4.154 mod_unload ARMv5 '
内核版本不一致,网上有各种方法,直接修改版本编号为4.4.154,只要重新编译内核,肯定恢复成192
最好直接修改Makefile文件
Kernel/Makefile文件里的:
#KERNELRELEASE = $(shell cat include/config/kernel.release 2> /dev/null)
KERNELRELEASE = 4.4.154
这样版本问题解决,然后insmo gpio-keys.ko
Error: Driver 'gpio-keys' is already registered, aborting...
内核中有及加载了gpio按键驱动
所以必须自己写一个input输入驱动
不想用设备树,直接在代码里写死
找到IO口的pin编号:
我的是
GPIO0_C1 17
GPIO0_C2 18
GPIO8_A0 248
GPIO8_A1 249
具体如下图:
定义按键结构,并初始化
struct pin_desc{ int irq; char *name; unsigned int pin; unsigned int key_val; }; struct pin_desc pins_desc[4] = { {65, "S4", ESC_GPIO, KEY_ESC}, {66, "S3", UP_GPIO, KEY_UP}, {67, "S2", DOWN_GPIO, KEY_DOWN}, {68, "S1", ENTER_GPIO, KEY_ENTER}, };
一开始我在这里初始化中断为65,66,67,68
结果没反应,中断没产生
原来3288的GPIO_pin指定了特定中断,直接使用就好
用gpio_to_irq获取gpio_pin编号的指定中断
我这里是:
for (i = 0; i < 4; i++) { pins_desc[i].irq=gpio_to_irq(pins_desc[i].pin); ret = request_irq(pins_desc[i].irq, buttons_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, pins_desc[i].name, &pins_desc[i]); }
上报按键事件
pinval = gpio_get_value(pindesc->pin); if (pinval) { /* 松开 : 最后一个参数: 0-松开, 1-按下 */ input_event(buttons_dev, EV_KEY, pindesc->key_val, 0); input_sync(buttons_dev); //printk("buttons_dev EV_KEY 0\r\n"); } else { /* 按下 */ input_event(buttons_dev, EV_KEY, pindesc->key_val, 1); input_sync(buttons_dev); // printk("buttons_dev EV_KEY 1\r\n"); }
让QT支持键盘消息:
export QT_QPA_GENERIC_PLUGINS=evdevkeyboard
insmod my_key.ko
全部代码如下:
#include <linux/module.h> #include <linux/version.h> #include <linux/init.h> #include <linux/fs.h> #include <linux/interrupt.h> #include <linux/irq.h> #include <linux/sched.h> #include <linux/pm.h> #include <linux/sysctl.h> #include <linux/proc_fs.h> #include <linux/delay.h> #include <linux/platform_device.h> #include <linux/input.h> #include <linux/irq.h> #include <linux/gpio.h> #include <linux/of.h> #include <linux/of_platform.h> #include <linux/of_gpio.h> #include <linux/of_irq.h> #include <linux/spinlock.h> #define ESC_GPIO 248 #define UP_GPIO 249 #define DOWN_GPIO 17 #define ENTER_GPIO 18 struct pin_desc{ int irq; char *name; unsigned int pin; unsigned int key_val; }; struct pin_desc pins_desc[4] = { {65, "S4", ESC_GPIO, KEY_ESC}, {66, "S3", UP_GPIO, KEY_UP}, {67, "S2", DOWN_GPIO, KEY_DOWN}, {68, "S1", ENTER_GPIO, KEY_ENTER}, }; static struct input_dev *buttons_dev; static struct pin_desc *irq_pd; static struct timer_list buttons_timer; irqreturn_t buttons_irq(int irq, void *dev_id) //irqreturn_t buttons_irq(int irq,void *dev_id,struct myint_dev * intd) { /* 10ms后启动定时器 */ irq_pd = (struct pin_desc *)dev_id; mod_timer(&buttons_timer, jiffies+HZ/100); return IRQ_HANDLED; } static void buttons_timer_function(unsigned long data) { struct pin_desc * pindesc = irq_pd; unsigned int pinval; if (!pindesc) return; pinval = gpio_get_value(pindesc->pin); if (pinval) { /* 松开 : 最后一个参数: 0-松开, 1-按下 */ input_event(buttons_dev, EV_KEY, pindesc->key_val, 0); input_sync(buttons_dev); //printk("buttons_dev EV_KEY 0\r\n"); } else { /* 按下 */ input_event(buttons_dev, EV_KEY, pindesc->key_val, 1); input_sync(buttons_dev); // printk("buttons_dev EV_KEY 1\r\n"); } } static int buttons_init(void) { int i,ret; gpio_direction_input(pins_desc[0].pin); gpio_direction_input(pins_desc[1].pin); gpio_direction_input(pins_desc[2].pin); gpio_direction_input(pins_desc[3].pin); /* 1. 分配一个input_dev结构体 */ buttons_dev = input_allocate_device();; /* 2. 设置 */ /* 2.1 能产生哪类事件 */ set_bit(EV_KEY, buttons_dev->evbit); set_bit(EV_REP, buttons_dev->evbit); /* 2.2 能产生这类操作里的哪些事件: L,S,ENTER,LEFTSHIT */ set_bit(KEY_ESC, buttons_dev->keybit); set_bit(KEY_UP, buttons_dev->keybit); set_bit(KEY_DOWN, buttons_dev->keybit); set_bit(KEY_ENTER, buttons_dev->keybit); /* 3. 注册 */ ret = input_register_device(buttons_dev); /* 4. 硬件相关的操作 */ init_timer(&buttons_timer); buttons_timer.function = buttons_timer_function; add_timer(&buttons_timer); for (i = 0; i < 4; i++) { pins_desc[i].irq=gpio_to_irq(pins_desc[i].pin); ret = request_irq(pins_desc[i].irq, buttons_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, pins_desc[i].name, &pins_desc[i]); } return 0; } static void buttons_exit(void) { int i; for (i = 0; i < 4; i++) { free_irq(pins_desc[i].irq, &pins_desc[i]); } del_timer(&buttons_timer); input_unregister_device(buttons_dev); input_free_device(buttons_dev); } module_init(buttons_init); module_exit(buttons_exit); MODULE_LICENSE("GPL");
文件放在:driver/input/keyboard下
修改Makefile:
加入:
obj-m += my-keys.o
然后编译
生成:my-keys.ko