Linux下控制GPIO的方法有N种,详细请参考《RPi GPIO Code Samples》,文中用十多种语言演示了如何控制GPIO,非常全面详尽。因此,这里不再多做赘述,仅把调试过程中整理的脚本贴上并做简单的分析。特别说明,Linux必须有root权限!!!否则会出现/system/bin/sh: can't create /sys/class/gpio/export: Permission denied等错误。
@echo offechoadb root
adb root>%temp%\_tmp
@findstr"already" %temp%\_tmp
@if NOT errorlevel1goto config
timeout/T 10:config
@echo on
adb shellchmod 0666 /sys/class/gpio/export
adb shellchmod 0666 /sys/class/gpio/unexport
adb shellecho 62 ">" /sys/class/gpio/export
adb shellecho 63 ">" /sys/class/gpio/export
adb shellchmod 0666 /sys/class/gpio/gpio62/direction
adb shellchmod 0666 /sys/class/gpio/gpio63/direction
adb shellchmod 0666 /sys/class/gpio/gpio62/value
adb shellchmod 0666 /sys/class/gpio/gpio63/value
adb shellecho out ">" /sys/class/gpio/gpio62/direction
adb shellecho out ">" /sys/class/gpio/gpio63/direction
adb shellecho 1 ">" /sys/class/gpio/gpio62/value
adb shellecho 1 ">" /sys/class/gpio/gpio63/value
@echo off
:top
adb shellecho 1 ">" /sys/class/gpio/gpio62/value
adb shellecho 1 ">" /sys/class/gpio/gpio63/value
rem @timeout/T 1
for /l %%i in (1,1,768) do echo %%i>nul
remping 0.0.0.1 -n 1 -w 500>nul
adb shellecho 0 ">" /sys/class/gpio/gpio62/value
adb shellecho 0 ">" /sys/class/gpio/gpio63/value
rem @timeout/T 1
for /l %%i in (1,1,768) do echo %%i>nul
remping 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/exportchmod 0666 /sys/class/gpio/unexportecho 62 > /sys/class/gpio/exportecho 63 > /sys/class/gpio/exportchmod 0666 /sys/class/gpio/gpio62/directionchmod 0666 /sys/class/gpio/gpio63/directionchmod 0666 /sys/class/gpio/gpio62/valuechmod 0666 /sys/class/gpio/gpio63/valueecho out > /sys/class/gpio/gpio62/directionecho out > /sys/class/gpio/gpio63/directionecho 1 > /sys/class/gpio/gpio62/valueecho 1 > /sys/class/gpio/gpio63/valuewhile:do
echo 1 > /sys/class/gpio/gpio62/valueecho 1 > /sys/class/gpio/gpio63/value
usleep100000
echo 0 > /sys/class/gpio/gpio62/valueecho 0 > /sys/class/gpio/gpio63/value
usleep100000
done
该脚本运行在MSM8996开发板上,通过操作/sys/class/gpio以控制GPIO,执行结果如下图所示。
从图中可以看出,两个GPIO的控制时间间隔相差约0.6ms,说明GPIO控制延时较小。
Android脚本文件可通过如下命令发送到开发板上,并修改其属性。
pushd %~dp0
adb push.\gpio.sh /data/local/adbshell chmod 777 /data/local/gpio.sh
参考:
Device Tree Usage
BeagleBone Black (BBB) Pin Muxes
Beaglebone Pin Muxes
Using Device Trees To Configure PRU IO Pins
BeagleBone Black GPIOs
gpio-header
beaglebone-universal-io