在做一个项目中有一个需求:启动时GPIO6 输出高电平,锁住电源。
开始使用 rc.local方法:
在rc.local中添加启动GPIO6的Python程序,发现从上电到运行程序需要十几秒的时间,不满足需求。
后来使用 update-rc.d 方法(启动脚本放在init.d 中):
参照这个连接方法 Setting GPIO value on boot - Raspberry Pi Stack Exchange
但结果上电到运行程序时间过长超过15秒,不符合要求
那只有把这个操作放到Boot启动中了,就是要修改设备树文件
在下面这个链接中有介绍
Raspberry Pi Documentation - The config.txt file
说添加
gpio=12=op,dh
到/boot/config.txt 中。
添加后,启动后并没有效果
再仔细阅读后发现如下说明
The gpio
directive allows GPIO pins to be set to specific modes and values at boot time in a way that would previously have needed a custom dt-blob.bin
file.
那就是要编译dt-blob.bin了,编译方法如下:
nanCreating dt-blob.bin ? - Raspberry Pi Forums 中说明如何制作
首先要下载dt-blob.dts
firmware/dt-blob.dts at master · raspberrypi/firmware · GitHub
提供下载链接 树莓派设备树文件用于编译dt-blob.bin-Linux文档类资源-CSDN文库
下载后,
拷贝到树莓派上,并修改仅留CM4部分
在pin@p14添加 上一行添加
pin@p6 { function = "output"; termination = "no_pulling"; polarity = "active_high"; };
执行
sudo dtc -I dts -O dtb -o /boot/dt-blob.bin dt-blob.dts
会报如下warnning
/boot/dt-blob.bin: Warning (unit_address_vs_reg): /videocore/pins_cm4/pin_config/pin@default: node has a unit name, but no reg property
/boot/dt-blob.bin: Warning (unit_address_vs_reg): /videocore/pins_cm4/pin_config/pin@p6: node has a unit name, but no reg property
/boot/dt-blob.bin: Warning (unit_address_vs_reg): /videocore/pins_cm4/pin_config/pin@p14: node has a unit name, but no reg property
/boot/dt-blob.bin: Warning (unit_address_vs_reg): /videocore/pins_cm4/pin_config/pin@p15: node has a unit name, but no reg property
......
......
......
目前不知道原因,待查。但可以在/boot中发现有dt-blob.bin生成。
重启后,发现启动过程中GPIO6没有拉高。
登录后使用
gpio readall
发现GPIO6 脚已设置为输出了,但输出是 0,那就说明有效果,但没有在启动执行。
后来发现
pin@default {
polarity = "active_high";
termination = "pull_down";
startup_state = "inactive";
function = "input";
}; // pin
中间startup_state设置,默认值为inactive。
那就修改dts,改为
pin@p6 { function = "output"; termination = "no_pulling"; polarity = "active_high"; startup_state = "active"; };
编译,重启。按下电源键,5秒后释放,不会掉电。成功。