OpenWRT中的按键和灯的GPIO控制实现_转

本文深入探讨了在OpenWRT环境下,如何通过GPIO接口控制AR9331AP121Demo单板上的LED灯和按键。详细解析了灯和按键的GPIO定义、数据结构及初始化过程,介绍了如何通过/sys/devices平台控制LED亮度,以及按键触发的事件处理流程。
AI助手已提取文章相关产品:

本文转自:OpenWRT中的按键和灯的GPIO控制实现

基于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

您可能感兴趣的与本文相关内容

给我分析一下这个GPIO的代码,我着重关注reset的代码 /* * GPIO Button Hotplug driver * * Copyright (C) 2012 Felix Fietkau <nbd@openwrt.org> * Copyright (C) 2008-2010 Gabor Juhos <juhosg@openwrt.org> * * Based on the diag.c - GPIO interface driver for Broadcom boards * Copyright (C) 2006 Mike Baker <mbm@openwrt.org>, * Copyright (C) 2006-2007 Felix Fietkau <nbd@openwrt.org> * Copyright (C) 2008 Andy Boyett <agb@openwrt.org> * * 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/version.h> #include <linux/kmod.h> #include <linux/workqueue.h> #include <linux/skbuff.h> #include <linux/netlink.h> #include <linux/kobject.h> #include <linux/input.h> #include <linux/interrupt.h> #include <linux/platform_device.h> #include <linux/of_gpio.h> #include <linux/gpio_keys.h> #define DRV_NAME "gpio-keys" #define BH_SKB_SIZE 2048 #define PFX DRV_NAME ": " #undef BH_DEBUG /*调试的宏控制调试日志输出*/ #ifdef BH_DEBUG #define BH_DBG(fmt, args...) printk(KERN_DEBUG "%s: " fmt, DRV_NAME, ##args ) #else #define BH_DBG(fmt, args...) do {} while (0) #endif #define BH_ERR(fmt, args...) printk(KERN_ERR "%s: " fmt, DRV_NAME, ##args ) struct bh_priv { unsigned long seen; }; /*按键事件结构体(用于暂存上报的事件)*/ struct bh_event { const char *name; unsigned int type; char *action; unsigned long seen; struct sk_buff *skb; struct work_struct work; }; /*按键名称映射表*/ struct bh_map { unsigned int code; const char *name; }; /*按键状态数据(每一个按键对应一个实例)*/ struct gpio_keys_button_data { struct delayed_work work; struct bh_priv bh; int last_state; int count; int threshold; int can_sleep; struct gpio_keys_button *b; }; extern u64 uevent_next_seqnum(void); #define BH_MAP(_code, _name) \ { \ .code = (_code), \ .name = (_name), \ } /*按键码映射表*/ static struct bh_map button_map[] = { BH_MAP(BTN_0, "BTN_0"), BH_MAP(BTN_1, "BTN_1"), BH_MAP(BTN_2, "BTN_2"), BH_MAP(BTN_3, "BTN_3"), BH_MAP(BTN_4, "BTN_4"), BH_MAP(BTN_5, "BTN_5"), BH_MAP(BTN_6, "BTN_6"), BH_MAP(BTN_7, "BTN_7"), BH_MAP(BTN_8, "BTN_8"), BH_MAP(BTN_9, "BTN_9"), BH_MAP(KEY_POWER, "power"), BH_MAP(KEY_RESTART, "reset"), BH_MAP(KEY_RFKILL, "rfkill"), BH_MAP(KEY_WPS_BUTTON, "wps"), BH_MAP(KEY_WIMAX, "wwan"), }; /* -------------------------------------------------------------------------*/ /*事件构建函数(将按键事件的元数据(如键码、名称、时间戳)格式化并存储到 skb 缓冲区,为后续上报到用户空间做准备)*/ static int bh_event_add_var(struct bh_event *event, int argv, const char *format, ...) { static char buf[128]; char *s; va_list args; int len; if (argv) return 0; va_start(args, format); len = vsnprintf(buf, sizeof(buf), format, args); va_end(args); if (len >= sizeof(buf)) { BH_ERR("buffer size too small\n"); WARN_ON(1); return -ENOMEM; } s = skb_put(event->skb, len + 1); strcpy(s, buf); BH_DBG("added variable '%s'\n", s); return 0; } /*填充事件元数据(向事件缓冲区(skb)添加标准化的环境变量,用户空间通过解析这些变量识别事件详情。)*/ static int button_hotplug_fill_event(struct bh_event *event) { int ret; ret = bh_event_add_var(event, 0, "HOME=%s", "/"); if (ret) return ret; ret = bh_event_add_var(event, 0, "PATH=%s", "/sbin:/bin:/usr/sbin:/usr/bin"); if (ret) return ret; ret = bh_event_add_var(event, 0, "SUBSYSTEM=%s", "button"); if (ret) return ret; ret = bh_event_add_var(event, 0, "ACTION=%s", event->action); if (ret) return ret; ret = bh_event_add_var(event, 0, "BUTTON=%s", event->name); if (ret) return ret; if (event->type == EV_SW) { ret = bh_event_add_var(event, 0, "TYPE=%s", "switch"); if (ret) return ret; } ret = bh_event_add_var(event, 0, "SEEN=%ld", event->seen); if (ret) return ret; ret = bh_event_add_var(event, 0, "SEQNUM=%llu", uevent_next_seqnum()); return ret; } /*工作队列处理函数(异步执行)(在进程上下文中异步处理事件(避免中断上下文阻塞),完成缓冲区分配、事件填充广播。)*/ static void button_hotplug_work(struct work_struct *work) { struct bh_event *event = container_of(work, struct bh_event, work); int ret = 0; event->skb = alloc_skb(BH_SKB_SIZE, GFP_KERNEL); if (!event->skb) goto out_free_event; ret = bh_event_add_var(event, 0, "%s@", event->action); if (ret) goto out_free_skb; ret = button_hotplug_fill_event(event); if (ret) goto out_free_skb; NETLINK_CB(event->skb).dst_group = 1; broadcast_uevent(event->skb, 0, 1, GFP_KERNEL); out_free_skb: if (ret) { BH_ERR("work error %d\n", ret); kfree_skb(event->skb); } out_free_event: kfree(event); } /*事件创建入口(当按键状态稳定后(经过消抖处理,确认是有效按下/释放),由驱动核心逻辑调用。)*/ static int button_hotplug_create_event(const char *name, unsigned int type, unsigned long seen, int pressed) { struct bh_event *event; BH_DBG("create event, name=%s, seen=%lu, pressed=%d\n", name, seen, pressed); event = kzalloc(sizeof(*event), GFP_KERNEL); if (!event) return -ENOMEM; event->name = name; event->type = type; event->seen = seen; event->action = pressed ? "pressed" : "released"; INIT_WORK(&event->work, (void *)(void *)button_hotplug_work); schedule_work(&event->work); return 0; } /* -------------------------------------------------------------------------*/ /*按键索引事件映射(通过按键的内核标准码(code)查找其在 button_map 中的索引。例如,当 code=KEY_RESTART 时,返回对应 "reset" 按键的索引。)*/ static int button_get_index(unsigned int code) { int i; for (i = 0; i < ARRAY_SIZE(button_map); i++) if (button_map[i].code == code) return i; return -1; } /*按键事件上报*/ static void button_hotplug_event(struct gpio_keys_button_data *data, unsigned int type, int value) { struct bh_priv *priv = &data->bh; unsigned long seen = jiffies; int btn; BH_DBG("event type=%u, code=%u, value=%d\n", type, data->b->code, value); if ((type != EV_KEY) && (type != EV_SW)) return; btn = button_get_index(data->b->code); if (btn < 0) return; button_hotplug_create_event(button_map[btn].name, type, (seen - priv->seen) / HZ, value); priv->seen = seen; } /*设备数据结构*/ struct gpio_keys_button_dev { int polled; struct delayed_work work; struct device *dev; struct gpio_keys_platform_data *pdata; struct gpio_keys_button_data data[0]; }; /*GPIO按键值读取*/ static int gpio_button_get_value(struct gpio_keys_button_data *bdata) { int val; if (bdata->can_sleep) val = !!gpio_get_value_cansleep(bdata->b->gpio); else val = !!gpio_get_value(bdata->b->gpio); return val ^ bdata->b->active_low; } /*轮询消抖逻辑*/ static void gpio_keys_polled_check_state(struct gpio_keys_button_data *bdata) { int state = gpio_button_get_value(bdata); if (state != bdata->last_state) { unsigned int type = bdata->b->type ?: EV_KEY; if (bdata->count < bdata->threshold) { bdata->count++; return; } if ((bdata->last_state != -1) || (type == EV_SW)) button_hotplug_event(bdata, type, state); bdata->last_state = state; } bdata->count = 0; } /*轮询模式相关函数(非中断方式检测按键)*/ static void gpio_keys_polled_queue_work(struct gpio_keys_button_dev *bdev) { struct gpio_keys_platform_data *pdata = bdev->pdata; unsigned long delay = msecs_to_jiffies(pdata->poll_interval); if (delay >= HZ) delay = round_jiffies_relative(delay); schedule_delayed_work(&bdev->work, delay); } /*轮询核心逻辑,遍历所有按键并检查状态变化。*/ static void gpio_keys_polled_poll(struct work_struct *work) { struct gpio_keys_button_dev *bdev = container_of(work, struct gpio_keys_button_dev, work.work); int i; for (i = 0; i < bdev->pdata->nbuttons; i++) { struct gpio_keys_button_data *bdata = &bdev->data[i]; gpio_keys_polled_check_state(bdata); } gpio_keys_polled_queue_work(bdev); } static void gpio_keys_polled_close(struct gpio_keys_button_dev *bdev) { struct gpio_keys_platform_data *pdata = bdev->pdata; cancel_delayed_work_sync(&bdev->work); if (pdata->disable) pdata->disable(bdev->dev); } /*中断处理函数(GPIO 中断服务程序(ISR),按键按下/释放时触发。)*/ static irqreturn_t button_handle_irq(int irq, void *_bdata) { struct gpio_keys_button_data *bdata = (struct gpio_keys_button_data *) _bdata; button_hotplug_event(bdata, bdata->b->type ?: EV_KEY, gpio_button_get_value(bdata)); return IRQ_HANDLED; } /*设备树解析函数(从设备树(Device Tree)中解析按键硬件配置,生成驱动所需的 platform_data 结构体。)*/ #ifdef CONFIG_OF static struct gpio_keys_platform_data * gpio_keys_get_devtree_pdata(struct device *dev) { struct device_node *node, *pp; struct gpio_keys_platform_data *pdata; struct gpio_keys_button *button; int error; int nbuttons; int i = 0; node = dev->of_node; if (!node) return NULL; nbuttons = of_get_child_count(node); if (nbuttons == 0) return NULL; pdata = devm_kzalloc(dev, sizeof(*pdata) + nbuttons * (sizeof *button), GFP_KERNEL); if (!pdata) { error = -ENOMEM; goto err_out; } pdata->buttons = (struct gpio_keys_button *)(pdata + 1); pdata->nbuttons = nbuttons; pdata->rep = !!of_get_property(node, "autorepeat", NULL); of_property_read_u32(node, "poll-interval", &pdata->poll_interval); for_each_child_of_node(node, pp) { enum of_gpio_flags flags; if (!of_find_property(pp, "gpios", NULL)) { pdata->nbuttons--; dev_warn(dev, "Found button without gpios\n"); continue; } button = &pdata->buttons[i++]; button->gpio = of_get_gpio_flags(pp, 0, &flags); button->active_low = flags & OF_GPIO_ACTIVE_LOW; if (of_property_read_u32(pp, "linux,code", &button->code)) { dev_err(dev, "Button without keycode: 0x%x\n", button->gpio); error = -EINVAL; goto err_out; } button->desc = of_get_property(pp, "label", NULL); if (of_property_read_u32(pp, "linux,input-type", &button->type)) button->type = EV_KEY; button->wakeup = !!of_get_property(pp, "gpio-key,wakeup", NULL); if (of_property_read_u32(pp, "debounce-interval", &button->debounce_interval)) button->debounce_interval = 5; } if (pdata->nbuttons == 0) { error = -EINVAL; goto err_out; } return pdata; err_out: return ERR_PTR(error); } /*驱动匹配初始化(内核启动时,通过 compatible 字段匹配设备树节点驱动,自动调用 probe 函数完成初始化(如申请 GPIO、注册中断)。)*/ static struct of_device_id gpio_keys_of_match[] = { { .compatible = "gpio-keys", }, { }, }; MODULE_DEVICE_TABLE(of, gpio_keys_of_match); static struct of_device_id gpio_keys_polled_of_match[] = { { .compatible = "gpio-keys-polled", }, { }, }; MODULE_DEVICE_TABLE(of, gpio_keys_polled_of_match); #else static inline struct gpio_keys_platform_data * gpio_keys_get_devtree_pdata(struct device *dev) { return NULL; } #endif /*通用初始化函数(驱动加载时的基础初始化,为中断触发轮询两种模式提供公共配置)*/ static int gpio_keys_button_probe(struct platform_device *pdev, struct gpio_keys_button_dev **_bdev, int polled) { struct gpio_keys_platform_data *pdata = pdev->dev.platform_data; struct device *dev = &pdev->dev; struct gpio_keys_button_dev *bdev; struct gpio_keys_button *buttons; int error; int i; if (!pdata) { pdata = gpio_keys_get_devtree_pdata(dev); if (IS_ERR(pdata)) return PTR_ERR(pdata); if (!pdata) { dev_err(dev, "missing platform data\n"); return -EINVAL; } pdev->dev.platform_data = pdata; } if (polled && !pdata->poll_interval) { dev_err(dev, "missing poll_interval value\n"); return -EINVAL; } buttons = devm_kzalloc(dev, pdata->nbuttons * sizeof(struct gpio_keys_button), GFP_KERNEL); if (!buttons) { dev_err(dev, "no memory for button data\n"); return -ENOMEM; } memcpy(buttons, pdata->buttons, pdata->nbuttons * sizeof(struct gpio_keys_button)); bdev = devm_kzalloc(dev, sizeof(struct gpio_keys_button_dev) + pdata->nbuttons * sizeof(struct gpio_keys_button_data), GFP_KERNEL); if (!bdev) { dev_err(dev, "no memory for private data\n"); return -ENOMEM; } bdev->polled = polled; for (i = 0; i < pdata->nbuttons; i++) { struct gpio_keys_button *button = &buttons[i]; struct gpio_keys_button_data *bdata = &bdev->data[i]; unsigned int gpio = button->gpio; if (button->wakeup) { dev_err(dev, DRV_NAME "does not support wakeup\n"); return -EINVAL; } error = devm_gpio_request(dev, gpio, button->desc ? button->desc : DRV_NAME); if (error) { dev_err(dev, "unable to claim gpio %u, err=%d\n", gpio, error); return error; } error = gpio_direction_input(gpio); if (error) { dev_err(dev, "unable to set direction on gpio %u, err=%d\n", gpio, error); return error; } bdata->can_sleep = gpio_cansleep(gpio); bdata->last_state = -1; if (bdev->polled) bdata->threshold = DIV_ROUND_UP(button->debounce_interval, pdata->poll_interval); else bdata->threshold = 1; bdata->b = &pdata->buttons[i]; } bdev->dev = &pdev->dev; bdev->pdata = pdata; platform_set_drvdata(pdev, bdev); *_bdev = bdev; return 0; } /*中断触发模式初始化(为支持中断的 GPIO 按键注册中断处理函数,实现高效事件响应)*/ static int gpio_keys_probe(struct platform_device *pdev) { struct gpio_keys_platform_data *pdata; struct gpio_keys_button_dev *bdev; int ret, i; ret = gpio_keys_button_probe(pdev, &bdev, 0); if (ret) return ret; pdata = pdev->dev.platform_data; for (i = 0; i < pdata->nbuttons; i++) { struct gpio_keys_button *button = &pdata->buttons[i]; struct gpio_keys_button_data *bdata = &bdev->data[i]; if (bdata->can_sleep) { dev_err(&pdev->dev, "skipping gpio:%d, it can sleep\n", button->gpio); continue; } if (!button->irq) button->irq = gpio_to_irq(button->gpio);//将GPIO引脚换为中断号 if (button->irq < 0) { dev_err(&pdev->dev, "failed to get irq for gpio:%d\n", button->gpio); continue; } ret = devm_request_irq(&pdev->dev, button->irq, button_handle_irq, IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING, dev_name(&pdev->dev), bdata); if (ret) dev_err(&pdev->dev, "failed to request irq:%d for gpio:%d\n", button->irq, button->gpio); else dev_dbg(&pdev->dev, "gpio:%d has irq:%d\n", button->gpio, button->irq); if (bdata->b->type == EV_SW) button_hotplug_event(bdata, EV_SW, gpio_button_get_value(bdata)); } return 0; } /*轮询模式初始化*/ static int gpio_keys_polled_probe(struct platform_device *pdev) { struct gpio_keys_platform_data *pdata; struct gpio_keys_button_dev *bdev; int ret; int i; ret = gpio_keys_button_probe(pdev, &bdev, 1); if (ret) return ret; INIT_DELAYED_WORK(&bdev->work, gpio_keys_polled_poll); pdata = bdev->pdata; if (pdata->enable) pdata->enable(bdev->dev); for (i = 0; i < pdata->nbuttons; i++) gpio_keys_polled_check_state(&bdev->data[i]); gpio_keys_polled_queue_work(bdev); return ret; } /*设备卸载函数*/ static int gpio_keys_remove(struct platform_device *pdev) { struct gpio_keys_button_dev *bdev = platform_get_drvdata(pdev); platform_set_drvdata(pdev, NULL); if (bdev->polled) gpio_keys_polled_close(bdev); return 0; } /*中断模式驱动*/ static struct platform_driver gpio_keys_driver = { .probe = gpio_keys_probe, .remove = gpio_keys_remove, .driver = { .name = "gpio-keys", .owner = THIS_MODULE, .of_match_table = of_match_ptr(gpio_keys_of_match), }, }; /*轮询模式驱动*/ static struct platform_driver gpio_keys_polled_driver = { .probe = gpio_keys_polled_probe, .remove = gpio_keys_remove, .driver = { .name = "gpio-keys-polled", .owner = THIS_MODULE, .of_match_table = of_match_ptr(gpio_keys_polled_of_match), }, }; /*驱动注册卸载*/ static int __init gpio_button_init(void) { int ret; ret = platform_driver_register(&gpio_keys_driver); if (ret) return ret; ret = platform_driver_register(&gpio_keys_polled_driver); if (ret) platform_driver_unregister(&gpio_keys_driver); return ret; } /*模块卸载*/ static void __exit gpio_button_exit(void) { platform_driver_unregister(&gpio_keys_driver); platform_driver_unregister(&gpio_keys_polled_driver); } module_init(gpio_button_init); module_exit(gpio_button_exit); MODULE_AUTHOR("Gabor Juhos <juhosg@openwrt.org>"); MODULE_AUTHOR("Felix Fietkau <nbd@openwrt.org>"); MODULE_DESCRIPTION("Polled GPIO Buttons hotplug driver"); MODULE_LICENSE("GPL v2"); MODULE_ALIAS("platform:" DRV_NAME);
最新发布
10-31
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值