標籤:
Linux下控制GPIO的方法有N種,詳細請參考《RPi GPIO Code Samples》,文中用十多種語言示範了如何控制GPIO,非常全面詳盡。因此,這裡不再多做贅述,僅把調試過程中整理的指令碼貼上並做簡單的分析。特別說明,Linux必須有root許可權!!!否則會出現/system/bin/sh: can‘t create /sys/class/gpio/export: Permission denied等錯誤。
@echo offecho adb rootadb root >%temp%\_tmp@findstr "already" %temp%\_tmp@if NOT errorlevel 1 goto configtimeout /T 10:config@echo onadb shell chmod 0666 /sys/class/gpio/exportadb shell chmod 0666 /sys/class/gpio/unexportadb shell echo 62 ">" /sys/class/gpio/exportadb shell echo 63 ">" /sys/class/gpio/exportadb shell chmod 0666 /sys/class/gpio/gpio62/directionadb shell chmod 0666 /sys/class/gpio/gpio63/directionadb shell chmod 0666 /sys/class/gpio/gpio62/valueadb shell chmod 0666 /sys/class/gpio/gpio63/valueadb shell echo out ">" /sys/class/gpio/gpio62/directionadb shell echo out ">" /sys/class/gpio/gpio63/directionadb shell echo 1 ">" /sys/class/gpio/gpio62/valueadb shell echo 1 ">" /sys/class/gpio/gpio63/value@echo off:topadb shell echo 1 ">" /sys/class/gpio/gpio62/valueadb shell echo 1 ">" /sys/class/gpio/gpio63/valuerem @timeout /T 1for /l %%i in (1,1,768) do echo %%i>nulrem ping 0.0.0.1 -n 1 -w 500>nuladb shell echo 0 ">" /sys/class/gpio/gpio62/valueadb shell echo 0 ">" /sys/class/gpio/gpio63/valuerem @timeout /T 1for /l %%i in (1,1,768) do echo %%i>nulrem ping 0.0.0.1 -n 1 -w 500>nulgoto toppause.
該批次檔運行在PC上,通過adb發送shell命令操作/sys/class/gpio以控制GPIO。
在MSM8996的開發板上測試,運行結果如所示。
可以看出,兩個GPIO的控制時間間隔相差約40ms,說明GPIO控制延時較為嚴重。
#!/system/bin/shchmod 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 :doecho 1 > /sys/class/gpio/gpio62/valueecho 1 > /sys/class/gpio/gpio63/valueusleep 100000echo 0 > /sys/class/gpio/gpio62/valueecho 0 > /sys/class/gpio/gpio63/valueusleep 100000done
該指令碼運行在MSM8996開發板上,通過操作/sys/class/gpio以控制GPIO,執行結果如所示。
可以看出,兩個GPIO的控制時間間隔相差約0.6ms,說明GPIO控制延時較小。
Android指令檔可通過如下命令發送到開發板上,並修改其屬性。
pushd %~dp0adb 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
安卓應用程式通過JNI控制GPIO
http://android.serverbox.ch/?p=972
Android(Linux)控制GPIO的方法及即時性分析