openwrt中LED的控制

Openwrt中LED的控制

由于公司没有使用openwrt的平台,而是使用LSDK的平台,所以好多东西都需要移植。只是内核版本升级到3.3.8,故需要把openwrt中应用层的接口移植到LSDK的平台上,所以简单研究了一下LED的控制流程。部分总结如下,后续继续补充:

主要涉及到的几个模块:

linux/kernels/linux-3.3.8/drivers/leds/ledtrig-gpio.ko
linux/kernels/linux-3.3.8/drivers/leds/ledtrig-timer.ko
linux/kernels/linux-3.3.8/drivers/leds/leds-gpio.ko
linux/kernels/linux-3.3.8/drivers/leds/ledtrig-default-on.ko
linux/kernels/linux-3.3.8/drivers/leds/ledtrig-netdev.ko

GPIO初始化文件:

linux/kernels/linux-3.3.8/arch/mips/ath79/mach-ap147.c

配置文件:

/etc/config/system
/etc/init.d/led
root@openwrt:# cat /etc/init.d/led 
#!/bin/sh /etc/rc.common
# Copyright (C) 2008 OpenWrt.org

START=96

load_led() {
    local name
    local sysfs
    local trigger
    local dev
    local mode
    local default
    local delayon
    local delayoff
    local interval

    config_get sysfs $1 sysfs
    config_get name $1 name "$sysfs"
    config_get trigger $1 trigger "none"
    config_get dev $1 dev
    config_get mode $1 mode "link"
    config_get_bool default $1 default "nil"
    config_get delayon $1 delayon
    config_get delayoff $1 delayoff
    config_get interval $1 interval "50"
    config_get port_state $1 port_state
    config_get delay $1 delay "150"
    config_get message $1 message ""

    if [ "$trigger" = "rssi" ]; then
        # handled by rssileds userspace process
        return
    fi

    [ -e /sys/class/leds/${sysfs}/brightness ] && {
        echo "setting up led ${name}"
        [ "$default" != nil ] && {
            [ $default -eq 1 ] &&
                echo 1 >/sys/class/leds/${sysfs}/brightness
            [ $default -eq 1 ] ||
                echo 0 >/sys/class/leds/${sysfs}/brightness
        }
        echo $trigger > /sys/class/leds/${sysfs}/trigger
        case "$trigger" in
        "netdev")
            [ -n "$dev" ] && {
                echo $dev > /sys/class/leds/${sysfs}/device_name
                echo $mode > /sys/class/leds/${sysfs}/mode
            }
            ;;

        "timer")
            [ -n "$delayon" ] && \
                echo $delayon > /sys/class/leds/${sysfs}/delay_on
            [ -n "$delayoff" ] && \
                echo $delayoff > /sys/class/leds/${sysfs}/delay_off
            ;;

        "usbdev")
            [ -n "$dev" ] && {
                echo $dev > /sys/class/leds/${sysfs}/device_name
                echo $interval > /sys/class/leds/${sysfs}/activity_interval
            }
            ;;

        "port_state")
            [ -n "$port_state" ] && \
                echo $port_state > /sys/class/leds/${sysfs}/port_state
            ;;

        "morse")
            echo $message > /sys/class/leds/${sysfs}/message
            echo $delay > /sys/class/leds/${sysfs}/delay
            ;;

        switch[0-9]*)
            local port_mask

            config_get port_mask $1 port_mask
            [ -n "$port_mask" ] && \
                echo $port_mask > /sys/class/leds/${sysfs}/port_mask
            ;;
        esac
    }
}

start() {
    [ -e /sys/class/leds/ ] && {
        config_load system
        config_foreach load_led led
    }
}
root@openwrt:# cat /etc/config/system
# Heartbeat led
config 'led'
    option 'sysfs'      'wrt160nl:amber:wps'
    option 'trigger'    'heartbeat'

#WLAN led
config 'led' 'wlan_led'
        option 'name'           'WLAN'
        option 'sysfs'          'tl-wr1043nd:green:wlan'
        option 'trigger'        'netdev'
        option 'dev'            'wlan0'
        option 'mode'           'link tx rx'

#3G led
#The 3G LED lits up in blue when an USB-dongle properly registers with the #3G/EDGE/GPRS network. Tested on Asus's WL-330N3G router.

config 'led'
        option 'name'           '3G'
        option 'sysfs'          'asus:blue:3g'
        option 'trigger'        'netdev'
        option 'dev'            '3g-wan'
        option 'mode'           'link'

#Timer led - 500ms ON, 2000ms OFF
config 'led'
    option 'sysfs'      'wrt160nl:blue:wps'
    option 'trigger'    'timer'
    option 'delayon'    '500'
    option 'delayoff'   '2000'

1. GPIO口注册

修改对应的参考板mach文件即可,如linux/kernels/linux-3.3.8/arch/mips/ath79/mach-ap147.c。
1)、定义对应的GPIO口:

#define AP147_GPIO_LED_WLAN     12
#define AP147_GPIO_LED_WLAN_V2      4
#define AP147_GPIO_LED_SYS      0
#define AP147_GPIO_LED_SYS_V2       1
#define AP147_GPIO_LED_SIG1     1
#define AP147_GPIO_LED_SIG2     2
#define AP147_GPIO_LED_SIG3     3
#define AP147_GPIO_LED_NET      4

#define AP147_GPIO_LED_WAN      16
#define AP147_GPIO_LED_WAN_V2       2
#define AP147_GPIO_LED_LAN1     16
#define AP147_GPIO_LED_LAN1_V2      15
#define AP147_GPIO_LED_LAN2     15
#define AP147_GPIO_LED_LAN2_V2      16
#define AP147_GPIO_LED_LAN3     14
#define AP147_GPIO_LED_LAN3_V2      0
#define AP147_GPIO_LED_LAN4     11
#define AP147_GPIO_LED_LAN4_V2      3

#define AP147_GPIO_BTN_WPS      17
#define AP147_GPIO_BTN_WPS_V2       14

#define AP147_KEYS_POLL_INTERVAL    20  /* msecs */
#define AP147_KEYS_DEBOUNCE_INTERVAL    (3 * AP147_KEYS_POLL_INTERVAL)

#define AP147_MAC0_OFFSET       0
#define AP147_MAC1_OFFSET       6
#define AP147_WMAC_CALDATA_OFFSET   0x1000

#define AP147_MAX_LED_WPS_GPIOS         6
#define AP147_MAX_BOARD_VERSION         2

#define AP147_V2_ID         17
#define AP147_WMAC1_CALDATA_OFFSET  0x5000
#define BOARDID_OFFSET          0x20

#define BOARD_V1        0
#define BOARD_V2        1

2)、注册:

static struct gpio_led ap147_leds_gpio[] __initdata = {
    {
        .name       = "ap147:green:status",
        .gpio       = AP147_GPIO_LED_SYS,
        .active_low = 1,
    },
    {
        .name       = "ap147:green:sig1",
        .gpio       = AP147_GPIO_LED_SIG1,
        .active_low = 1,
    },
    {
        .name       = "ap147:green:sig2",
        .gpio       = AP147_GPIO_LED_SIG2,
        .active_low = 1,
    },
    {
        .name       = "ap147:green:sig3",                                                                                                                                                                    
        .gpio       = AP147_GPIO_LED_SIG3,
        .active_low = 1,
    },
    {
        .name       = "ap147:green:net",
        .gpio       = AP147_GPIO_LED_NET,
        .active_low = 1,
    },
    {
        .name       = "ap147:green:wlan",
        .gpio       = AP147_GPIO_LED_WLAN,
        .active_low = 1,
    }
};

static struct gpio_keys_button ap147_gpio_keys[] __initdata = {
    {
        .desc       = "WPS button",
        .type       = EV_KEY,
        .code       = BTN_0,
        .debounce_interval = AP147_KEYS_DEBOUNCE_INTERVAL,
        .gpio       = AP147_GPIO_BTN_WPS,
        .active_low = 1,
    },
};

static void __init ap147_gpio_led_setup(int board_version)
{
    ath79_gpio_direction_select(ap147_gpios[board_version][WAN], true);
    ath79_gpio_direction_select(ap147_gpios[board_version][LAN1], true);
    ath79_gpio_direction_select(ap147_gpios[board_version][LAN2], true);
    ath79_gpio_direction_select(ap147_gpios[board_version][LAN3], true);
    ath79_gpio_direction_select(ap147_gpios[board_version][LAN4], true);

    ath79_gpio_output_select(ap147_gpios[board_version][WAN],
            QCA953X_GPIO_OUT_MUX_LED_LINK5);
    ath79_gpio_output_select(ap147_gpios[board_version][LAN1],
            QCA953X_GPIO_OUT_MUX_LED_LINK1);
    ath79_gpio_output_select(ap147_gpios[board_version][LAN2],
            QCA953X_GPIO_OUT_MUX_LED_LINK2);
    ath79_gpio_output_select(ap147_gpios[board_version][LAN3],
            QCA953X_GPIO_OUT_MUX_LED_LINK3);
    ath79_gpio_output_select(ap147_gpios[board_version][LAN4],
            QCA953X_GPIO_OUT_MUX_LED_LINK4);

    if (board_version == BOARD_V2) {
        ap147_leds_gpio[0].active_low = 0;
        ap147_leds_gpio[0].gpio = AP147_GPIO_LED_SYS_V2;
        ap147_leds_gpio[1].gpio = AP147_GPIO_LED_WLAN_V2;
        ap147_gpio_keys[0].gpio = AP147_GPIO_BTN_WPS_V2;
    }
    ath79_register_leds_gpio(-1, ARRAY_SIZE(ap147_leds_gpio),
            ap147_leds_gpio);
    ath79_register_gpio_keys_polled(-1, AP147_KEYS_POLL_INTERVAL,
            ARRAY_SIZE(ap147_gpio_keys),
            ap147_gpio_keys);
}

static void __init ap147_setup(void)
{
    u8 *art = (u8 *) KSEG1ADDR(0x1fff0000);

    u8 board_id = *(u8 *) (art + AP147_WMAC1_CALDATA_OFFSET + BOARDID_OFFSET);
    pr_info("AP147 Reference Board Id is %d\n",(u8)board_id);
    ath79_register_m25p80(NULL);

    if (board_id == AP147_V2_ID) {
        /* Disabling the JTAG due to conflicting GPIO's.
         * Can be re-enabled dynamically by writing appropriate
         * value to GPIO_FUNCTION_ADDRESS register
         */
        ath79_gpio_function_enable(AR934X_GPIO_FUNC_JTAG_DISABLE);
        ap147_gpio_led_setup(BOARD_V2);
    } else {
        ath79_gpio_function_enable(AR934X_GPIO_FUNC_JTAG_DISABLE);
        ap147_gpio_led_setup(BOARD_V1);
    }

    ath79_register_usb();
    ath79_register_pci();

    ath79_register_wmac(art + AP147_WMAC_CALDATA_OFFSET, NULL);

    ath79_register_mdio(0, 0x0);
    ath79_register_mdio(1, 0x0);

    ath79_init_mac(ath79_eth0_data.mac_addr, art + AP147_MAC0_OFFSET, 0);
    ath79_init_mac(ath79_eth1_data.mac_addr, art + AP147_MAC1_OFFSET, 0);

    /* WAN port */
    ath79_eth0_data.phy_if_mode = PHY_INTERFACE_MODE_MII;
    ath79_eth0_data.speed = SPEED_100;
    ath79_eth0_data.duplex = DUPLEX_FULL;
    ath79_eth0_data.phy_mask = BIT(4);
    ath79_register_eth(0);

    /* LAN ports */
    ath79_eth1_data.phy_if_mode = PHY_INTERFACE_MODE_GMII;
    ath79_eth1_data.speed = SPEED_1000;
    ath79_eth1_data.duplex = DUPLEX_FULL;
    ath79_switch_data.phy_poll_mask |= BIT(4);
    ath79_switch_data.phy4_mii_en = 1;
    ath79_register_eth(1);
}

MIPS_MACHINE(ATH79_MACH_AP147, "AP147", "Qualcomm Atheros AP147 reference board",
         ap147_setup);

2. 自定义控制LED打开/关闭

# 自定义4G信号灯的信号强度
# sysfs=ap147:green:sig1
root@OpenWrt:/# echo 1 >/sys/class/leds/${sysfs}/brightness # 打开
root@OpenWrt:/# echo 0 >/sys/class/leds/${sysfs}/brightness # 关闭

3. 自动事件触发LED打开/关闭

# wifi 2.4G的灯由系统自己控制
# sysfs=ap147:green:wlan
root@OpenWrt:/# echo 255 > /sys/class/leds/${sysfs}/brightness
root@OpenWrt:/# echo netdev > /sys/class/leds/${sysfs}/trigger
root@OpenWrt:/# echo ath0 > /sys/class/leds/${sysfs}/device_name
root@OpenWrt:/# echo 'tx rx' > /sys/class/leds/${sysfs}/mode

4. 自定义事件触发LED打开/关闭/闪烁

# 系统灯周期闪烁
# sysfs=ap147:green:status
root@OpenWrt:/# echo 255 > /sys/class/leds/${sysfs}/brightness
root@OpenWrt:/# echo timer > /sys/class/leds/${sysfs}/trigger
root@OpenWrt:/# echo 1000 > /sys/class/leds/${sysfs}/delay_on
root@OpenWrt:/# echo 1000 > /sys/class/leds/${sysfs}/delay_off

5. 网口灯自动闪烁


网口灯也可以实现手动配置和自动闪烁,不过这个配置已经在驱动完成了,详看mach-ap147.c文件。如果想移植为通用的,即可删除mach文件中的配置,只进行注册即可,配置由应用层完成。
    ath79_gpio_direction_select(ap147_gpios[board_version][WAN], true);
    ath79_gpio_direction_select(ap147_gpios[board_version][LAN1], true);
    ath79_gpio_direction_select(ap147_gpios[board_version][LAN2], true);
    ath79_gpio_direction_select(ap147_gpios[board_version][LAN3], true);
    ath79_gpio_direction_select(ap147_gpios[board_version][LAN4], true);

    ath79_gpio_output_select(ap147_gpios[board_version][WAN],
            QCA953X_GPIO_OUT_MUX_LED_LINK5);
    ath79_gpio_output_select(ap147_gpios[board_version][LAN1],
            QCA953X_GPIO_OUT_MUX_LED_LINK1);
    ath79_gpio_output_select(ap147_gpios[board_version][LAN2],
            QCA953X_GPIO_OUT_MUX_LED_LINK2);
    ath79_gpio_output_select(ap147_gpios[board_version][LAN3],
            QCA953X_GPIO_OUT_MUX_LED_LINK3);
    ath79_gpio_output_select(ap147_gpios[board_version][LAN4],
            QCA953X_GPIO_OUT_MUX_LED_LINK4);

再次更新,添加RGA100支持,添加LED触发器类型, 删除了按钮脚本,要自定义功能自己写脚本放在/etc/hotplud.d/button/ 下面 openwrt-RG100A_DB120-squashfs-cfe.bin http://115.com/file/c2bjz3px# 我的DB120-WG,双UBS,看着那么多的LED无法使用,于是泡论坛,看教程, 经过无数次的make, make V=99,终于修正了DB120的所有LED驱动,共9个LED, power和internet为双色LED,触发用time, 调整红色和绿色分量,可以显示绿色,橙色,红色等, 美不足的是红太亮了,绿太弱,有条件的换下LED限流电阻 2012-2-1 增加了3个按钮 BTN_0 RESET 按住8秒后放开,系统复位 BTN_1 WLAN 无线开关 BTN_2 WPS umount 以后不要手贱,随便捅菊花了,结果你懂的 基本完美了,发挥你的想象吧 集成的软件都是我自己要用的,如motion做监控,图片保存在移动硬盘上, 当画面有变化时 mutt和ssmtp 发送邮件到139邮箱,有实时短信提醒. 通过N2N,配合视频监控软件可以随时随地查看家里状况 看到有个帖子里面用用Mplayer做网络收音机,又塞了个mplayer进去 集成USB声卡驱动,基本影音全能了 基于官方 OpenWrt Backfire 10.03.1 编译而成 软件包名称 版本 alsa-lib 1.0.24.1-1 alsa-utils 1.0.24.2-1 base-files 43.32-r29685 block-mount 0.1.0-2.2 bridge 1.4-1 busybox 1.15.3-3.4 bzip2 1.0.6-1 crda 1.1.1-1 dnsmasq 2.55-6.1 dropbear 0.53.1-5 firewall 2-34.8 gpioctl 1.0-1 hd-idle 1.03-1 hotplug2 1.0-beta-3 iptables 1.4.6-3.1 iptables-mod-conntrack 1.4.6-3.1 iptables-mod-conntrack-extra 1.4.6-3.1 iptables-mod-filter 1.4.6-3.1 iptables-mod-imq 1.4.6-3.1 iptables-mod-ipopt 1.4.6-3.1 iptables-mod-nat 1.4.6-3.1 iw 0.9.22-2 kernel 2.6.32.27-1 kmod-b43 2.6.32.27+2011-12-01-1 kmod-button-hotplug 2.6.32.27-1 kmod-cfg80211 2.6.32.27+2011-12-01-1 kmod-crc-ccitt 2.6.32.27-1 kmod-crypto-aes 2.6.32.27-1 kmod-crypto-arc4 2.6.32.27-1 kmod-crypto-core 2.6.32.27-1 kmod-fs-ext2 2.6.32.27-1 kmod-fs-ext3 2.6.32.27-1 kmod-fuse 2.6.32.27-1 kmod-i2c-core 2.6.32.27-1 kmod-input-core 2.6.32.27-1 kmod-input-gpio-buttons 2.6.32.27-1 kmod-input-polldev 2.6.32.27-1 kmod-ipt-conntrack 2.6.32.27-1 kmod-ipt-conntrack-extra 2.6.32.27-1 kmod-ipt-core 2.6.32.27-1 kmod-ipt-filter 2.6.32.27-1 kmod-ipt-imq 2.6.32.27-1 kmod-ipt-ipopt 2.6.32.27-1 kmod-ipt-nat 2.6.32.27-1 kmod-mac80211 2.6.32.27+2011-12-01-1 kmod-nls-cp437 2.6.32.27-1 kmod-nls-iso8859-1 2.6.32.27-1 kmod-nls-utf8 2.6.32.27-1 kmod-ppp 2.6.32.27-1 kmod-sched 2.6.32.27-1 kmod-scsi-core 2.6.32.27-1 kmod-sound-core 2.6.32.27-1 kmod-switch 2.6.32.27-4 kmod-textsearch 2.6.32.27-1 kmod-tun 2.6.32.27-1 kmod-usb-audio 2.6.32.27-1 kmod-usb-core 2.6.32.27-1 kmod-usb-ohci 2.6.32.27-1 kmod-usb-printer 2.6.32.27-1 kmod-usb-storage 2.6.32.27-1 kmod-usb-uhci 2.6.32.27-1 kmod-usb2 2.6.32.27-1 kmod-video-core 2.6.32.27-1 kmod-video-uvc 2.6.32.27-1 kmod-zd1211rw 2.6.32.27+2011-12-01-1 lame-lib 398-2-3 libao 1.1.0-1 libc 0.9.30.1-43.32 libevent 1.4.14b-1 libfaad2 2.7-1 libffmpeg 0.5.4-2 libfuse 2.8.3-1 libgcc 4.3.3+cs-43.32 libgsm 1.0.13-1 libiconv 5 libiconv-full 1.11.1-1 libid3tag 0.15.1b-3 libip4tc 1.4.6-3.1 libiwinfo 18 libiwinfo-lua 18 libjpeg 6b-1 libltdl 2.4-1 liblua 5.1.4-7 liblzo 2.04-1 libmad 0.15.1b-3 libncurses 5.7-2 libnl-tiny 0.1-1 libogg 1.1.4-2 libopenssl 0.9.8s-1 libpthread 0.9.30.1-43.32 librrd1 1.0.50-1 librt 0.9.30.1-43.32 libsamplerate 0.1.7-1 libsndfile 1.0.21-1 libuci 12012009.7-4 libuci-lua 12012009.7-4 libusb-1.0 1.0.8-1 libv4l 0.6.1-1 libvorbis 1.2.3-1 libvorbisidec 1.0.2+svn14261-1 libxtables 1.4.6-3.1 lua 5.1.4-7 luci 0.10.0-1 luci-app-firewall 0.10.0-1 luci-app-hd-idle 0.10.0-1 luci-app-ntpc 0.10.0-1 luci-app-qos 0.10.0-1 luci-app-samba 0.10.0-1 luci-app-voice-core 0.10.0-1 luci-i18n-chinese 0.10.0-1 luci-i18n-english 0.10.0-1 luci-lib-core 0.10.0-1 luci-lib-ipkg 0.10.0-1 luci-lib-lmo 0.10.0-1 luci-lib-lucid 0.10.0-1 luci-lib-lucid-http 0.10.0-1 luci-lib-nixio 0.10.0-1 luci-lib-px5g 0.10.0-1 luci-lib-sys 0.10.0-1 luci-lib-web 0.10.0-1 luci-mod-admin-core 0.10.0-1 luci-mod-admin-full 0.10.0-1 luci-proto-core 0.10.0-1 luci-proto-ppp 0.10.0-1 luci-sgi-cgi 0.10.0-1 luci-theme-base 0.10.0-1 luci-theme-openwrt 0.10.0-1 mjpg-streamer r136-1 motion 3.2.11.1-1 mtd 13 mutt 1.5.21-1 n2n 3875-1 ntfs-3g 2011.4.12-1-fuseext ntpclient 2007_365-4 openssl-util 0.9.8s-1 opkg 576-2 qos-scripts 1.2.1-3.2 resolveip 1 samba3 3.0.24-8 screen 4.0.3-2 sox 14.0.1-3 ssmtp 2.64-3 tc 2.6.29-1-2 uci 12012009.7-4 udevtrigger 106-1 uhttpd 28 usbutils 003-1 wireless-tools 29-4 wpad-mini 20111103-2 zlib 1.2.3-5
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值