2013年11月24日 星期日

讓網樂通的LED像心跳一樣閃動

之前 github中branch leds 中的程式碼完全正確,
問題的確是在 kernel config中。
只要在 menuconfig 時選擇以下項目
Device Drivers  --->
  LED Support  --->
    LED Class Support
    LED Support for GPIO connected LEDs
      Platform device bindings for GPIO LEDs (NEW)
及需要的 Trigger (預設用到的有 Heartbeat 和 ide-disk),

或手動把
# CONFIG_LEDS_CLASS is not set
改成
CONFIG_LEDS_CLASS=y

再加入以下兩個就不會消失了
CONFIG_LEDS_GPIO=y
CONFIG_LEDS_GPIO_PLATFORM=y

下面是說明發現過程。
一開始是先找出使用到 CONFIG_LEDS_GPIO 的部份
$ find `pwd` -type f |xargs grep CONFIG_LEDS_GPIO

找到有關的是

drivers/leds/Makefile:obj-$(CONFIG_LEDS_GPIO)                      += leds-gpio.o
drivers/leds/leds-gpio.c:#ifdef CONFIG_LEDS_GPIO_PLATFORM
drivers/leds/leds-gpio.c:#endif /* CONFIG_LEDS_GPIO_PLATFORM */
drivers/leds/leds-gpio.c:#ifdef CONFIG_LEDS_GPIO_OF
drivers/leds/leds-gpio.c:#ifdef CONFIG_LEDS_GPIO_PLATFORM 
drivers/leds/leds-gpio.c:#ifdef CONFIG_LEDS_GPIO_OF
drivers/leds/leds-gpio.c:#ifdef CONFIG_LEDS_GPIO_PLATFORM 
drivers/leds/leds-gpio.c:#ifdef CONFIG_LEDS_GPIO_PLATFORM
drivers/leds/leds-gpio.c:#ifdef CONFIG_LEDS_GPIO_OF


其中 CONFIG_LEDS_GPIO_OF 是用在有 Firmware 的LED裝置上,
和我需要的功能無關,
剩下的先改成 CONFIG_KEYBOARD_GPIO 測試程式碼正確性 ,

把 drivers/leds/Makefile 檔案中的
obj-$(CONFIG_LEDS_GPIO)                      += leds-gpio.o
換成
obj-$(CONFIG_KEYBOARD_GPIO)                      += leds-gpio.o

執行下面指令修改 leds-gpio.c
$ sed -i "s/CONFIG_LEDS_GPIO_PLATFORM/CONFIG_KEYBOARD_GPIO/g"  drivers/leds/leds-gpio.c

可以看到編譯時有編到 leds-gpio.o 。

只是最後在產生 .tmp_vmlinux1 時出現Error

drivers/built-in.o: In function `delete_gpio_led':
/home/sh4twbox/ST_dev/kernel-pdk7105/drivers/leds/leds-gpio.c:134: undefined reference to `led_classdev_unregister'
drivers/built-in.o: In function `gpio_get_value':
/home/sh4twbox/ST_dev/kernel-pdk7105/drivers/leds/leds-gpio.c:105: undefined reference to `led_classdev_register'
make: *** [.tmp_vmlinux1] Error 1

接下來才是正確方式。

搜尋到 LKML: Bryan Wu: Re: [PATCH] leds: kill CONFIG_LEDS_CLASS option 寫到和 CONFIG_LEDS_CLASS 有關,
就到 .config 中找到
# CONFIG_LEDS_CLASS is not set

手動改成
CONFIG_LEDS_CLASS=y

並在 # LED drivers 下加入
CONFIG_LEDS_GPIO=y
CONFIG_LEDS_GPIO_PLATFORM=y

在 menuconfig 時就可以看到
Device Drivers
  LED support

<*>   LED Class Support                                                                    │ │
  │ │                          *** LED drivers ***                                     │ │
  │ │                    < >   LED driver for PCA9532 dimmer                │ │
  │ │                    <*>   LED Support for GPIO connected LEDs     │ │
  │ │                    [*]     Platform device bindings for GPIO LEDs 
  │ │

載入編譯完成的 vmlinux.ub ,
開機到一半時就可以看到藍色LED像心臟一樣跳動。
suspend to ram 時會停止閃動,
變成恆亮,
喚醒後繼續閃動。

只是和想要的隨CPU使用閃動不一樣,
紅色LED trigger 設定成 ide-disk 也不會顯示讀取狀態。

變成platform device 後,
原本的 gpio user space 控制方式會無效。
以下是新的控制和查看方式:

查看可用的 trigger
藍色
$ cat /sys/devices/platform/leds-gpio.0/leds/LD5/trigger
紅色
$ cat /sys/devices/platform/leds-gpio.0/leds/LD6/trigger

藍色預設trigger是 heartbeat
none nand-disk timer ide-disk [heartbeat]
紅色預設trigger是ide-disk,

改成 nand-disk 會恆亮,
timer 目前無效。

如果要手動設定LED亮暗,
須改用以下指令:
藍色亮
$ echo 0 > /sys/devices/platform/leds-gpio.0/leds/LD5/brightness
藍色暗
$ echo 1 > /sys/devices/platform/leds-gpio.0/leds/LD5/brightness

紅色亮
$ echo 0 > /sys/devices/platform/leds-gpio.0/leds/LD6/brightness
紅色暗
$ echo 1 > /sys/devices/platform/leds-gpio.0/leds/LD6/brightness

沒有留言:

張貼留言