This forum uses cookies
This forum makes use of cookies to store your login information if you are registered, and your last visit if you are not. Cookies are small text documents stored on your computer; the cookies set by this forum can only be used on this website and pose no security risk. Cookies on this forum also track the specific topics you have read and when you last read them. Please confirm whether you accept or reject these cookies being set.

A cookie will be stored in your browser regardless of choice to prevent you being asked this question again. You will be able to change your cookie settings at any time using the link in the footer.

Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using Python to control/test GPIO pins
#1
I need some SIMPLE python code that shows me how to include GPIO libraries and use them to control (any) two GPIO pins, one for input (button push) and one for output (LED off and on).

I have downloaded and installed GPIO_API_for_Python.zip, which I believe loads the libraries. But in reading through some of the sample code I was just overwhelmed with information and I'm having a difficult time sorting out what I need. Do I write the python code the same as Raspberry Pi code?  RPi example below:

#library
...
import RPi.GPIO as GPIO
...

#pin definitions
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM)
GPIO.setup (8, GPIO.OUT)
GPIO.setup(10, GPIO.IN)

GPIO.output(8, GPIO.HIGH) # set the initial output state

#programming code
...
inputvalue = GPIO.input(10) #to test the value of pin 10
GPIO.output(8, GPIO.HIGH) # to drive pin 8 output high
GPIO.output(8, GPIO.LOW) # to drive pin 8 output low
...

Can I use this same Python code with the Tinker Board?
Reply
#2
Not all same. You need to change some specific code to Tinker Board!!
(You can find the GPIO pin table from the manual or official site or type "sudo gpio readall" in terminal to get table.)


Here is the code after I recoded. (without test, but should work?)
Code:
#library
...
import ASUS.GPIO as GPIO
...

#pin definitions
GPIO.setwarnings(False)
GPIO.setmode(GPIO.ASUS)
GPIO.setup (255, GPIO.OUT)
GPIO.setup(257, GPIO.IN)

GPIO.output(255, GPIO.HIGH) # set the initial output state

#programming code
...
inputvalue = GPIO.input(257) #to test the value of pin 257
GPIO.output(255, GPIO.HIGH) # to drive pin 255 output high
GPIO.output(255, GPIO.LOW) # to drive pin 255 output low
...

or

Code:
#library
...
import ASUS.GPIO as GPIO
...

#pin definitions
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup (24, GPIO.OUT)
GPIO.setup(19, GPIO.IN)

GPIO.output(24, GPIO.HIGH) # set the initial output state

#programming code
...
inputvalue = GPIO.input(19) #to test the value of pin 19
GPIO.output(24, GPIO.HIGH) # to drive pin 24 output high
GPIO.output(24, GPIO.LOW) # to drive pin 24 output low
...
Reply
#3
More examples: https://github.com/sabrigultekin/Asus-Tinker-Board
I left this community in Aug 2017 due to ASUS bad product quality and ASUS community support that did not match my expectation.  Sad
Reply
#4
Thank you, frankwu. That is exactly the information I needed. I can't help but wonder if the distinction between the GPIO.ASUS pin numbers and the GPIO.BOARD numbers is the GPIO.ASUS number represent the Rocket chip pin number.

And to mcerveny, thank you for ASUS-Tinker-Board code samples.
Reply
#5
(05-15-2017, 10:26 AM)jatomlins Wrote: Thank you, frankwu. That is exactly the information I needed. I can't help but wonder if the distinction between the GPIO.ASUS pin numbers and the GPIO.BOARD numbers is the GPIO.ASUS number represent the Rocket chip pin number.

And to mcerveny, thank you for ASUS-Tinker-Board code samples.

Great.
If you want to get pin number and function table, you can type following command in terminal.
Code:
sudo gpio readall #need GPIO_C_Library

And you may get table like this:
[Image: oL54mkn.png]

Also can find another table info from ASUS Tinker Board Official Site for Python used.
https://www.asus.com/Single-Board-Comput.../overview/
[Image: N1RbUU4.png]
Reply
#6
Once again, I thank you, frankwu, for your assistance. I already have the second pinout chart posted on my workbench, but, still the GPIO.ASUS numbers are a mystery. For instance, GPIO.BOARD pin 7 (GPOC1_CLKOUT) is GPIO.ASUS pin 17 which, in turn, connects to the Rockchip pin M23. Where is the logic (or sense) in that?

At present, my research has turned to the sink/source current the Rockchip GPIO pins can supply for driving loads (like an LED). I have downloaded the Rockchip RK3288 preliminary data sheet but it does not list the value. Now I'm trying to locate the finalized copy of the data sheet.

Oh, I forgot to tell you I ran into a problem entering the Python code to manipulate the GPIO pins. I got an error code that said I would have to switch to "root" to enter the code. After some research, I decided to edit /usr/share/application/idle-python3.5.desktop and change the execution line to sudo /usr/bin/idle-python3.5. Not exactly elegance, but it works.
Reply
#7
No idea...maybe just need to map chip's define?

And...do you mean you need onboard GPIO-LEDs pin??

Here you are~
Mode: GPIO.ASUS (Python)
PWR_LED 3
ACT_LED 48
LED1_LED 49

I only know about GPIO.ASUS's pin, and maybe no GPIO.BOARD's pin
Reply
#8
Exclamation 
(05-19-2017, 10:13 AM)jatomlins Wrote: I already have the second pinout chart posted on my workbench, but, still the GPIO.ASUS numbers are a mystery. For instance, GPIO.BOARD pin 7 (GPOC1_CLKOUT) is GPIO.ASUS pin 17 which, in turn, connects to the Rockchip pin M23. Where is the logic (or sense) in that?

It is easy. GPIO marking consists of GPIO[0-8][A-D][0-7] eg. 9x 32 bit GPIO banks are split to 4 ("A-D") 8 bit ("0-7") pins (total 160 pins some are unused/unrouted). Example of translation to number:
GPIO0C1 = 0*32 + ("C") 2*8 + 1 = 17
GPIO5B4 = 5*32 + ("B") 1*8 + 4 - 8 = 164

The "- 8" (for >=GPIO1) is idiotic thing introduced here. The rockchip-4.4 try to repair this in commit 1d9964a but this breaks TinkerBoard GPIO_API_for_C and GPIO_API_for_Python. I logged issue.  

Beware this incompatibility in rockchip kernel (like build, yocto) !

PS: you can also control GPIO over "pinctrl" driver directly in shell:
Code:
# ## GPIO7A7 (on connector #36) = 7*32 + 0*8 + 7 - 8 = 223
# echo 223 > /sys/class/gpio/export
# echo out > /sys/class/gpio/gpio223/direction
# echo 0 > /sys/class/gpio/gpio223/value
# echo 1 > /sys/class/gpio/gpio223/value
# echo in > /sys/class/gpio/gpio223/direction
# cat /sys/class/gpio/gpio223/value
# echo 223 > /sys/class/gpio/unexport
I left this community in Aug 2017 due to ASUS bad product quality and ASUS community support that did not match my expectation.  Sad
Reply
#9
Hi all,

I making a port of Adafruit TFT LCD display on Tinker board, it has 4 buttons, connected as 17,22,23,27 pins (RPi notation).
I'm trying to read their state on Tinkerboard - the last pin is always 0.

Sample:

Code:
boardPin1 = 11
boardPin2 = 15
boardPin3 = 16
boardPin4 = 13
GPIO.setmode(GPIO.BOARD)
GPIO.setup(boardPin1, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(boardPin2, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(boardPin3, GPIO.IN, pull_up_down = GPIO.PUD_UP)
GPIO.setup(boardPin4, GPIO.IN, pull_up_down = GPIO.PUD_UP)
while True:
           time.sleep(1.0)
           print "Values", GPIO.input(boardPin1), GPIO.input(boardPin2), GPIO.input(boardPin3), GPIO.input(boardPin4)

Result: all lcd buttons are working except the last one. Looks like, other service is using this pin.

Also pins 40 and 38 cannot be read properly, the results are incorrect (probably PCM/I2S service use them).
What can I do with it?

Thanks.
Reply
#10
Bug 
(05-30-2017, 01:04 PM)DVE Wrote: ... it has 4 buttons, on connector pins 11,15,16,13 and 40,38. The 13 pin is always 0.

If I run your's test (added connector pins 40,38) it works "OK" for me when attach simple button between pins and ground (except pin 13).  
Let's translate to GPIO.BOARD (connector pin id) to GPIO.ASUS (linux kernel pin id) 11 ->  164, 15 -> 167, 16 -> 162, 13 -> 166, 40 -> 188, 38 -> 187 (for TinkerOS 1.8 kernel). And review kernel state:
Code:
# grep -e 18[78] -e 16[2467] /sys/kernel/debug/pinctrl/pinctrl/pinmux-pins
pin 162 (gpio5-10): (MUX UNCLAIMED) gpio5:162
pin 164 (gpio5-12): (MUX UNCLAIMED) gpio5:164
pin 166 (gpio5-14): (MUX UNCLAIMED) gpio5:166
pin 167 (gpio5-15): (MUX UNCLAIMED) gpio5:167
pin 187 (gpio6-3): ff890000.i2s gpio6:187 function i2s0 group i2s0-bus
pin 188 (gpio6-4): ff890000.i2s gpio6:188 function i2s0 group i2s0-bus

# grep -e 18[78] -e 16[2467] /sys/kernel/debug/pinctrl/pinctrl/pinconf-pins
pin 162 (gpio5-10): input bias pull up output drive strength (4 mA) pin output (1 level)
pin 164 (gpio5-12): input bias pull up output drive strength (4 mA) pin output (1 level)
pin 166 (gpio5-14): input bias pull down output drive strength (4 mA) pin output (0 level)
pin 167 (gpio5-15): input bias pull up output drive strength (4 mA) pin output (1 level)
pin 187 (gpio6-3): input bias disabled output drive strength (4 mA) pin output (1 level)
pin 188 (gpio6-4): input bias disabled output drive strength (4 mA) pin output (1 level)

The problem is in GPIO.setup(..., ..., pull_up_down = GPIO.PUD_UP). "pull_up_down" parameter is unimplemented (ignored) in ASUS-python library (look to ".../sources/c_gpio.c" function "setup_gpio()" and parameter "pud") !
Pin 13 (kernel pin 166) is configured (by default?) with pulldown. I successfully tried function with button between connector pin 13 and 3.3V. Connector pins 38+40 work OK for me even without pullup (yes, i2s0 is enabled but you are exporting pin to gpio later that reconfigures pin multiplexor). See previous post how to manipulate with gpio over "/sys/class/gpio".

I do not known how "Adafruit TFT LCD display" buttons are connected (directly, directly with pullup or over resistor). You should check voltage level on pins with voltmeter if it match expected levels ("0" Vil <= 0.3*3.3V, "1" Vih >= 0.7*3.3V).
I left this community in Aug 2017 due to ASUS bad product quality and ASUS community support that did not match my expectation.  Sad
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)