博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
OpenWRT中的按键和灯的GPIO控制实现_转
阅读量:4457 次
发布时间:2019-06-08

本文共 2717 字,大约阅读时间需要 9 分钟。

本文转自:

基于BarrierBreaker版本,基于AR9331 AP121 Demo单板 来进行描述

1.灯

A.在mach-ap121.c中,定义了灯所对应的GPIO定义:

#define AP121_GPIO_LED_WLAN 0

#define AP121_GPIO_LED_USB 1

 并定义了灯的GPIO结构对象:

static struct gpio_led ap121_leds_gpio[] __initdata = {{.name    = "ap121:green:usb",.gpio    = AP121_GPIO_LED_USB,.active_low    = 0,},{.name    = "ap121:green:wlan",.gpio    = AP121_GPIO_LED_WLAN,.active_low    = 0,}, }

在初始化函数:ap121_setup 中,利用ath79_register_leds_gpio(-1, ARRAY_SIZE(ap121_leds_gpio), ap121_leds_gpio);实现了LED device的注册。此函数调用后,会创建platform类型的设备,并和leds-gpio驱动(leds-gpio.c)实现了绑定。这样,就会在/sys/devices/platform/leds-gpio/目录中,产生对应的led灯的控制目录:

drwxr-xr-x    2 root     root             0 Jan  1  1970 ap121:green:usb
drwxr-xr-x    2 root     root             0 Jan  1  1970 ap121:green:wlan
B.进入上述任意一个目录,如:ap121:green:wlan,会有如下文件:
-rw-r--r--    1 root     root          4096 Jan 15 06:19 brightness 
lrwxrwxrwx    1 root     root             0 Jan 15 06:04 device -> ../../../leds-gpio
-r--r--r--    1 root     root          4096 Jan 15 06:04 max_brightness
lrwxrwxrwx    1 root     root             0 Jan 15 06:04 subsystem -> ../../../../../class/leds
-rw-r--r--    1 root     root          4096 Jan 15 06:04 trigger
-rw-r--r--    1 root     root          4096 Jan 15 06:04 uevent

则通过 echo 1 > brightness,就可以控制灯亮; echo 0 > brightness,就可以控制灯灭 。

 2.按键

A.在mach-ap121.c中,定义了按键对应的GPIO以及数据结构对象:

#define AP121_GPIO_BTN_JUMPSTART 11

#define AP121_GPIO_BTN_RESET 12

 以及

static struct gpio_keys_button ap121_gpio_keys[] __initdata = {{.desc    = "jumpstart button",.type    = EV_KEY,.code    = KEY_WPS_BUTTON, //定义在gpio-button-hotplug.c.debounce_interval = AP121_KEYS_DEBOUNCE_INTERVAL,.gpio    = AP121_GPIO_BTN_JUMPSTART,.active_low    = 1,},{.desc    = "reset button",.type    = EV_KEY,.code    = KEY_RESTART,    //定义在gpio-button-hotplug.c.debounce_interval = AP121_KEYS_DEBOUNCE_INTERVAL,.gpio    = AP121_GPIO_BTN_RESET,.active_low    = 1,}, }

 在初始化函数:ap121_setup 中,利用

ath79_register_gpio_keys_polled(-1, AP121_KEYS_POLL_INTERVAL,ARRAY_SIZE(ap121_gpio_keys),ap121_gpio_keys);

实现了KEY device的注册。此函数调用后,会创建platform类型的设备,并和gpio-keys-polled驱动(gpio-button-hotplug.c

)实现了绑定。

B. 当按键时,则触发button_hotplug_event函数(gpio-button-hotplug.c):调用button_hotplug_create_event产生uevent事件,调用button_hotplug_fill_even填充事件(JSON格式),并最终调用button_hotplug_work发出uevent广播

上述广播,被procd进程中的hotplug_handler (procd/plug/hotplug.c) 收到,并根据etc/hotplug.json中预先定义的JSON内容匹配条件,定位到对应的执行函数,具体为:
[ "if",[ "and",[ "has", "BUTTON" ],[ "eq", "SUBSYSTEM", "button" ],],[ "exec", "/etc/rc.button/%BUTTON%" ]],
[ "if",[ "eq", "SUBSYSTEM",[ "net", "input", "usb", "ieee1394", "block", "atm", "zaptel", "tty", "button" ]],[ "exec", "/sbin/hotplug-call", "%SUBSYSTEM%" ]],

在openwrt的GPIO系统上,主要使用了如下技术点:

driver-device-platform, uevent,procd,JSON,UCI;当然还需要读懂对应芯片的Datasheet 

 

注:博主有很多openwrt应用文档,推荐学习。

转载于:https://www.cnblogs.com/embedded-linux/p/10200779.html

你可能感兴趣的文章
HGOI 20181028 题解
查看>>
python with as的用法
查看>>
Spring MVC 整合Activiti5.22配置详解
查看>>
『Python基础-14』匿名函数 `lambda`
查看>>
java: Set类及子类:TreeSet有序子类,HashSet无序子类:重复元素
查看>>
jquery: 一些常见的获取
查看>>
phalcon: acl权限控制
查看>>
[转] Oracle数据库System Global Area(SGA)的理解
查看>>
基于JavaScript实现表单密码的隐藏和显示出来
查看>>
Spring对注解(Annotation)处理【转】
查看>>
文件以二进制流保存到DB及Download(转)
查看>>
博客上看别人的正则替换
查看>>
计应152班第3小组之软件初步开发(小组项目)
查看>>
Mysql多实例集群搭建
查看>>
简单的 jQuery 浮动层随窗口滚动滑动插件实例
查看>>
Android 之 Run Android Lint
查看>>
[典型漏洞分享]功能逻辑缺陷,不需要旧手机号码即可绑定新手机号码【高】...
查看>>
样条之埃特金(Aitken)逐步插值函数
查看>>
C#基础第二天
查看>>
两个栈实现队列
查看>>