Linux下控制GPIO的方法有N种,详细请参考《RPi GPIO Code Samples》,文中用十多种语言演示了如何控制GPIO,非常全面详尽。因此,这里不再多做赘述,仅把调试过程中整理的脚本贴上并做简单的分析。特别说明,Linux必须有root权限!!!否则会出现/system/bin/sh: can't create /sys/class/gpio/export: Permission denied等错误。
@echo off
echo adb root
adb root >%temp%\_tmp
@findstr "already" %temp%\_tmp
@if NOT errorlevel 1 goto config
timeout /T 10
:config
@echo on
adb shell chmod 0666 /sys/class/gpio/export
adb shell chmod 0666 /sys/class/gpio/unexport
adb shell echo 62 ">" /sys/class/gpio/export
adb shell echo 63 ">" /sys/class/gpio/export
adb shell chmod 0666 /sys/class/gpio/gpio62/direction
adb shell chmod 0666 /sys/class/gpio/gpio63/direction
adb shell chmod 0666 /sys/class/gpio/gpio62/value
adb shell chmod 0666 /sys/class/gpio/gpio63/value
adb shell echo out ">" /sys/class/gpio/gpio62/direction
adb shell echo out ">" /sys/class/gpio/gpio63/direction
adb shell echo 1 ">" /sys/class/gpio/gpio62/value
adb shell echo 1 ">" /sys/class/gpio/gpio63/value
@echo off
:top
adb shell echo 1 ">" /sys/class/gpio/gpio62/value
adb shell echo 1 ">" /sys/class/gpio/gpio63/value
rem @timeout /T 1
for /l %%i in (1,1,768) do echo %%i>nul
rem ping 0.0.0.1 -n 1 -w 500>nul
adb shell echo 0 ">" /sys/class/gpio/gpio62/value
adb shell echo 0 ">" /sys/class/gpio/gpio63/value
rem @timeout /T 1
for /l %%i in (1,1,768) do echo %%i>nul
rem ping 0.0.0.1 -n 1 -w 500>nul
goto top
pause.
该批处理文件运行在PC上,通过adb发送shell命令操作/sys/class/gpio以控制GPIO。
在MSM8996的开发板上测试,运行结果如下图所示。
从图中可以看出,两个GPIO的控制时间间隔相差约40ms,说明GPIO控制延时较为严重。
#!/system/bin/sh
chmod 0666 /sys/class/gpio/export
chmod 0666 /sys/class/gpio/unexport
echo 62 > /sys/class/gpio/export
echo 63 > /sys/class/gpio/export
chmod 0666 /sys/class/gpio/gpio62/direction
chmod 0666 /sys/class/gpio/gpio63/direction
chmod 0666 /sys/class/gpio/gpio62/value
chmod 0666 /sys/class/gpio/gpio63/value
echo out > /sys/class/gpio/gpio62/direction
echo out > /sys/class/gpio/gpio63/direction
echo 1 > /sys/class/gpio/gpio62/value
echo 1 > /sys/class/gpio/gpio63/value
while :
do
echo 1 > /sys/class/gpio/gpio62/value
echo 1 > /sys/class/gpio/gpio63/value
usleep 100000
echo 0 > /sys/class/gpio/gpio62/value
echo 0 > /sys/class/gpio/gpio63/value
usleep 100000
done
该脚本运行在MSM8996开发板上,通过操作/sys/class/gpio以控制GPIO,执行结果如下图所示。
从图中可以看出,两个GPIO的控制时间间隔相差约0.6ms,说明GPIO控制延时较小。
Android脚本文件可通过如下命令发送到开发板上,并修改其属性。
pushd %~dp0 adb push .\gpio.sh /data/local/ adb shell chmod 777 /data/local/gpio.sh
参考:
How to use GPIO signals(Linux应用程序控制GPIO) PNG
https://developer.ridgerun.com/wiki/index.php/How_to_use_GPIO_signals
树莓派上使用多种语言(C、Python、shell)控制GPIO
http://elinux.org/RPi_GPIO_Code_Samples
Device Tree Usage
http://elinux.org/Device_Tree_Usage
BeagleBone Black (BBB) Pin Muxes
Beaglebone Pin Muxes
Using Device Trees To Configure PRU IO Pins
http://www.ofitselfso.com/BeagleNotes/UsingDeviceTreesToConfigurePRUIOPins.php
BeagleBone Black GPIOs
gpio-header
https://github.com/nomel/beaglebone/tree/master/gpio-header
beaglebone-universal-io