點燈
上篇文章我們使用 buildroot 編譯出了 BananaPi M1 的鏡像,燒錄到 SD 卡,系統跑起來后,我便開始了內核設備樹的探索,
先從點燈開始,進入到板子的 /sys/class/leds/ 目錄看到
# cd /sys/class/leds/
# ls
bananapi:green:usr
其中 bananapi:green:usr 正是對應板子上的綠色 LED 燈,原理圖如下

進入到 bananapi:green:usr,我們向 brightness 檔案寫入 1,即可點亮綠色 LED 燈
# cd bananapi:green:usr/
# ls
brightness max_brightness subsystem uevent
device power trigger
# echo 1 > brightness

向 trigger 寫入 heartbeat 就可以讓綠色 LED 燈像心跳一樣閃動
# echo heartbeat > trigger

設備樹
我們使用 buildroot 編譯的 Banana Pi M1 鏡像,內核使用的是 linux-4.18.12 原始代碼,既然研究設備樹,那就先動手改一改設備樹,跑一跑感受一下,
修改內核
于是我就把設備樹中 LED 的名字改了一下
buildroot-2021.05/output/build/linux-4.18.12/arch/arm/boot/dts/sun7i-a20-bananapi.dts
leds {
compatible = "gpio-leds";
pinctrl-names = "default";
pinctrl-0 = <&led_pins_bananapi>;
green {
/* label = "bananapi:green:usr"; */
label = "green";
gpios = <&pio 7 24 GPIO_ACTIVE_HIGH>;
};
};
重新編譯
$ make linux-rebuild
make[1]: 進入目錄“/home/liyongjun/project/board/buildroot-2021.05”
rm -f /home/liyongjun/project/board/buildroot-2021.05/output/build/linux-4.18.12/.stamp_installed
rm -f /home/liyongjun/project/board/buildroot-2021.05/output/build/linux-4.18.12/.stamp_staging_installed
rm -f /home/liyongjun/project/board/buildroot-2021.05/output/build/linux-4.18.12/.stamp_target_installed
rm -f /home/liyongjun/project/board/buildroot-2021.05/output/build/linux-4.18.12/.stamp_images_installed
rm -f /home/liyongjun/project/board/buildroot-2021.05/output/build/linux-4.18.12/.stamp_host_installed
rm -f /home/liyongjun/project/board/buildroot-2021.05/output/build/linux-4.18.12/.stamp_built
>>> linux 4.18.12 Building
...
comm: /home/liyongjun/project/board/buildroot-2021.05/output/build/linux-4.18.12/.files-list.before: No such file or directory
comm: /home/liyongjun/project/board/buildroot-2021.05/output/build/linux-4.18.12/.files-list-staging.before: No such file or directory
comm: /home/liyongjun/project/board/buildroot-2021.05/output/build/linux-4.18.12/.files-list-host.before: No such file or directory
make[1]: 離開目錄“/home/liyongjun/project/board/buildroot-2021.05”
報錯了,提示沒有 .files-list.before、.files-list-staging.before、.files-list-host.before 這三個檔案,
看了下 Makefile,貌似是用來輔助打包鏡像用的,
靈性操作
我在這里也是卡了大半天,buildroot 的 Makefile 還是稍顯復雜的,找了半天都沒找到出錯的地方是 Makefile 的哪條陳述句,后來索性想,你不是需要這三個檔案嗎,那我就手動給你創建這三個檔案試試,結果,還真就編譯通過了,,
正規操作
后來才知道原來 buildroot 有個功能叫 OVERRIDE_SRCDIR,專門處理修改代碼并同步的問題,
我們可以在 output 以外的位置修改原始碼,這也是一個好習慣,不然哪天不小心 make clean 了,辛辛苦苦修改的代碼就沒了,,
我們在 local.mk 里指定 linux-kernel 對應的原始碼目錄
buildroot-2021.05/local.mk
LINUX_OVERRIDE_SRCDIR = /home/liyongjun/project/board/buildroot-2021.05/override/linux-4.18.12
這樣,當我們進行 make linux-rebuild 的時候,buildroot 就會使用 rsync 將指定路徑的代碼同步到 output/build 中進行重新編譯,
完善
不過,編譯的時候還是出現了和上面一樣缺少那三個檔案的錯誤,為了方便日后編譯,索性將創建這三個檔案的動作寫到 Makefile 中
buildroot-2021.05/package/pkg-generic.mk
# Rsync the source directory if the <pkg>_OVERRIDE_SRCDIR feature is
# used.
$(BUILD_DIR)/%/.stamp_rsynced:
@$(call step_start,rsync)
@$(call MESSAGE,"Syncing from source dir $(SRCDIR)")
@mkdir -p $(@D)
$(foreach hook,$($(PKG)_PRE_RSYNC_HOOKS),$(call $(hook))$(sep))
@test -d $(SRCDIR) || (echo "ERROR: $(SRCDIR) does not exist" ; exit 1)
rsync -au --chmod=u=rwX,go=rX $($(PKG)_OVERRIDE_SRCDIR_RSYNC_EXCLUSIONS) $(RSYNC_VCS_EXCLUSIONS) $(call qstrip,$(SRCDIR))/ $(@D)
touch $(@D)/.files-list.before ; touch $(@D)/.files-list-staging.before ; touch $(@D)/.files-list-host.before ;
$(foreach hook,$($(PKG)_POST_RSYNC_HOOKS),$(call $(hook))$(sep))
@$(call step_end,rsync)
$(Q)touch $@
再次 make linux-rebuild && make,不會報錯了,
驗證
燒錄新編譯的鏡像,開機運行,進入到 /sys/class/leds/ 目錄
# cd /sys/class/leds/
# ls
green
果然,名稱變成了 green 說明我們修改 dts 成功,重新編譯成功,
搞定!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/291600.html
標籤:其他
