Understanding JETSON-GPIO: cleanup() setup() output() setmode()

1)

GPIO.setup(channel, GPIO.OUT, initial=GPIO.HIGH)
<==>

GPIO.setup(channel, GPIO.OUT)
GPIO.output(channel, GPIO.HIGH) #state = GPIO.HIGH

To set initial is the same as to run directly GPIO.ouput function with the same state. Do I understand it right? Or do the state: initial more?

2)
From github:

7. Clean up

At the end of the program, it is good to clean up the channels so that all pins are set in their default state. To clean up all channels used, call:
GPIO.cleanup()

Here is the talk of “default state”. This means the pinmux default state, not the initial state from GPIO.setup function. Do I understand it right?

3)
GPIO.setmode() not working? Why?

# GPIO library
import Jetson.GPIO as GPIO

# define Pins
LED4 = 7
LED8 = 12
LED3 = 13
IN3 = 15
LED7 = 18
IN2 = 19
IN1 = 21
IN7 = 22
LED2 = 23
IN6 = 24
LED1 = 29
IN5 = 32
IN4 = 33
LED5 = 35
LED6 = 38
IN8 = 40

# Pin Definition
LED_IN = [IN1,IN2,IN3,IN4,IN5,IN6,IN7,IN8]

# Warning disable
#GPIO.setwarnings(False)

# Set up the GPIO channel
GPIO.setmode(GPIO.BOARD)
GPIO.setup(LED_IN, GPIO.OUT, initial=GPIO.LOW) 
 
# ON the LEDs
for led_pin in LED_IN:
	GPIO.output(led_pin, GPIO.HIGH)
	GPIO.cleanup()
print("all LED are ON")

ERROR:

Traceback (most recent call last):
  File "/home/nvidia/Desktop/error_ON_gpiodemo_forum.py", line 34, in <module>
    GPIO.output(led_pin, GPIO.HIGH)
  File "/home/nvidia/.local/lib/python3.9/site-packages/Jetson/GPIO/gpio.py", line 444, in output
    ch_infos = _channels_to_infos(channels, need_gpio=True)
  File "/home/nvidia/.local/lib/python3.9/site-packages/Jetson/GPIO/gpio.py", line 119, in _channels_to_infos
    _validate_mode_set()
  File "/home/nvidia/.local/lib/python3.9/site-packages/Jetson/GPIO/gpio.py", line 83, in _validate_mode_set
    raise RuntimeError("Please set pin numbering mode using "
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD), GPIO.setmode(GPIO.BCM), GPIO.setmode(GPIO.TEGRA_SOC) or GPIO.setmode(GPIO.CVM)

Why does this code give the following error?

hello a.phoenix,

please refer to the pinmux spreadsheets to show the default pin configuration.
it looks you had failure by setting pin numbering mode, could you please try to use CVM or TEGRA_SOC,
for example, GPIO.setmode(GPIO.CVM) or GPIO.setmode(GPIO.TEGRA_SOC).
thanks

Yes, I know the pinmux spreadsheet, but that wasn’t the question. When I run the code, what does cleanup() do? What state am I in after cleanup()?
The github wiki say “default state” - but what is the “default state”?

  • Is the “default state” the same as initial in the GPIO.setup()?
  • Or “default state” is it define by pinmux?
  • Or it is something else?

hello a.phoenix,

it’s pinmux spreadsheets to generate the default board configuration file,
you could also check the file to confirm the default pin state. for example, tegra186-mb1-bct-pinmux-quill-p3310-1000-c03.cfg

With tegra_soc I have the same error:

Traceback (most recent call last):
  File "/home/nvidia/Desktop/error_ON_gpiodemo.py", line 58, in <module>
    GPIO.output(led_pin, GPIO.HIGH)
  File "/home/nvidia/.local/lib/python3.9/site-packages/Jetson/GPIO/gpio.py", line 444, in output
    ch_infos = _channels_to_infos(channels, need_gpio=True)
  File "/home/nvidia/.local/lib/python3.9/site-packages/Jetson/GPIO/gpio.py", line 119, in _channels_to_infos
    _validate_mode_set()
  File "/home/nvidia/.local/lib/python3.9/site-packages/Jetson/GPIO/gpio.py", line 83, in _validate_mode_set
    raise RuntimeError("Please set pin numbering mode using "
RuntimeError: Please set pin numbering mode using GPIO.setmode(GPIO.BOARD), GPIO.setmode(GPIO.BCM), GPIO.setmode(GPIO.TEGRA_SOC) or GPIO.setmode(GPIO.CVM)

Here my new code:

# GPIO library
import Jetson.GPIO as GPIO

# define Pins
pin7 = "AUD_MCLK"
pin11 = "UART1_RTS"
pin12 = "DAP1_SCLK"
pin13 = "GPIO_AUD0"
pin15 = "GPIO_EXP_P17"
pin16 = "CAN_GPIO0"
pin18 = "GPIO_MDM2"
pin19 = "GPIO_CAM6"
pin21 = "GPIO_CAM5"
pin22 = "GPIO_EXP_P16"
pin23 = "GPIO_CAM4"
pin24 = "GPIO_CAM7"
pin29 = "GPIO_AUD1"
pin31 = "CAN_GPIO2"
pin32 = "CAN_GPIO1"
pin33 = "GPIO_PQ5"
pin35 = "DAP1_FS"
pin36 = "UART1_CTS"
pin37 = "GPIO_PQ4"
pin38 = "DAP1_DIN"
pin40 = "DAP1_DOUT"

# define used Pins
LED4 = pin7
LED8 = pin12
LED3 = pin13
IN3 = pin15
LED7 = pin18
IN2 = pin19
IN1 = pin21
IN7 = pin22
LED2 = pin23
IN6 = pin24
LED1 = pin29
IN5 = pin32
IN4 = pin33
LED5 = pin35
LED6 = pin38
IN8 = pin40

# Pin Definition
#led_pin = IN5
LED_IN = [IN1,IN2,IN3,IN4,IN5,IN6,IN7,IN8]

# Warning disable
#GPIO.setwarnings(False)

# Set up the GPIO channel
GPIO.setmode(GPIO.TEGRA_SOC)
GPIO.setup(LED_IN, GPIO.OUT, initial=GPIO.LOW) 
 
# ON the LED 
for led_pin in LED_IN:
	GPIO.output(led_pin, GPIO.HIGH)
	GPIO.cleanup()
print("LED is ON")

How do I find this file or what is the path to this file?

It really just seems to be a shortened function call. Or is there a other “clean function” that resets the state to initial ?

Hi phoenix,
Please check sample codes which are using Jetson GPIO calls. jetson-gpio/simple_out.py at master · NVIDIA/jetson-gpio · GitHub
Default settings should bring the pins to pinmux default states not the onces you set from initial. Initial sets the value of GPIO during set mode/ export of the GPIO.

Would it be possible to indicate the respective point (1 to 4) in the answer, it would make it easier for me to understand. Thank you.

I’m trying to do like in the wiki/readme (GitHub - NVIDIA/jetson-gpio: A Python library that enables the use of Jetson's GPIOs) and it doesn’t work.

read me:

1. Importing the libary

To import the Jetson.GPIO module use:

import Jetson.GPIO as GPIO

This way, you can refer to the module as GPIO throughout the rest of the application. The module can also be imported using the name RPi.GPIO instead of Jetson.GPIO for existing code using the RPi library.

  1. I import Jetson.GPIO as GPIO as in the readme,
    but in the example code (https://github.com/NVIDIA/jetson-gpio/blob/master/samples/simple_out.py) was used import RPi.GPIO as GPIO
    - Or does it not work without RPi library?

Questions from the previous post that are remains (for code see also previous post):

I have tried other mode, it does not solve the error, although that is the statement of the error. How do I solve it?

if you install the JetPack release via https://developer.nvidia.com/nvidia-sdk-manager,
there’s default path as following, ~/nvidia/nvidia_sdk/JetPack_4.5.1_.../Linux_for_Tegra/
and you’ll see the configuration file under bootloader/t186ref/BCT/.
thanks

Since almost all questions have been answered here, except why the code does not work and it is slowly becoming confusing, I will open up a new forum post.

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.