Channel is invalid error when using Jetson.GPIO

Hi, i’m unable to use 8 and 10 pin of 40 pin header as GPIO outputs.

What have i done so far?

  • Used original pinmux excel spreadsheet, generated .dtsi files, set UART1_TXD and UART1_RXD as output, Direction 1
  • Flashed my jetson via SDK, applied my .dts by modifying .conf
  • Jetpack version is 6.2.2

On gpio info i see my pins as output configured

	line 110:      "PR.02"       unused  output  active-high 
	line 111:      "PR.03"       unused  output  active-high 

When using /opt/nvidia/jetson-io/jetson-io.py tool i see that these pins are gpio configured

 =================== Jetson Expansion Header Tool ===================
 |                                                                    |
 |                                                                    |
 |                      3.3V (  1) .. (  2) 5V                        |
 |                      i2c8 (  3) .. (  4) 5V                        |
 |                      i2c8 (  5) .. (  6) GND                       |
 |                    unused (  7) .. (  8) gpio                      |
 |                       GND (  9) .. ( 10) gpio                      |
 |                    unused ( 11) .. ( 12) unused                    |
 |                    unused ( 13) .. ( 14) GND                       |
 |                    unused ( 15) .. ( 16) unused                    |
 |                      3.3V ( 17) .. ( 18) unused                    |
 |                    unused ( 19) .. ( 20) GND                       |
 |                    unused ( 21) .. ( 22) unused                    |
 |                    unused ( 23) .. ( 24) unused                    |
 |                       GND ( 25) .. ( 26) unused                    |
 |                      i2c2 ( 27) .. ( 28) i2c2                      |
 |                    unused ( 29) .. ( 30) GND                       |
 |                    unused ( 31) .. ( 32) unused                    |
 |                    unused ( 33) .. ( 34) GND                       |
 |                    unused ( 35) .. ( 36) unused                    |
 |                    unused ( 37) .. ( 38) unused                    |
 |                       GND ( 39) .. ( 40) unused                    |
 |                                                                    |
 |                                                                    |
 |                        Jetson 40pin Header:                        |
 |                                                                    |
 |                 Configure for compatible hardware                  |
 |                   Configure header pins manually                   |
 |                                Back                                |

 =================== Jetson Expansion Header Tool ===================
 |                                                                    |
 |                                                                    |
 |                Select desired functions (for pins):                |
 |                                                                    |
 |      [*] unused [ ] gpio [ ] aud            (7)                    |
 |      [*] unused [ ] gpio [ ] extperiph3_clk (29)                   |
 |      [*] unused [ ] gpio [ ] extperiph4_clk (31)                   |
 |      [ ] unused [ ] gpio [*] i2c2           (27,28)                |
 |      [ ] unused [ ] gpio [*] i2c8           (3,5)                  |
 |      [*] unused [ ] gpio [ ] i2s2           (12,35,38,40)          |
 |      [*] unused [ ] gpio [ ] pwm1           (15)                   |
 |      [*] unused [ ] gpio [ ] pwm5           (33)                   |
 |      [ ] unused [ ] gpio [*] pwm7           (32)                   |
 |      [*] unused [ ] gpio [ ] spi1           (19,21,23,24,26)       |
 |      [*] unused [ ] gpio [ ] spi3           (13,16,18,22,37)       |
 |      [ ] unused [*] gpio [ ] uarta          (8,10)                 |
 |      [*] unused [ ] gpio [ ] uarta-cts/rts  (11,36)                |
 |                                                                    |
 |                                Back                                |
 |                                                                    |

When i try to run simple python script i’m unable to control 8 and 10 pins

import time

import Jetson.GPIO as GPIO
from Jetson.GPIO.gpio import _channels_to_infos  # noqa
from Jetson.GPIO.gpio_cdev import _GPIO_IN_OUT_MASK, _MAP_MASK, PadCtlRegister  # noqa


class RfSwitch:
    def __init__(self, pin_1: int, pin_2: int):
        self.pin_1 = pin_1
        self.pin_2 = pin_2
        GPIO.setmode(GPIO.BOARD)

        GPIO.setup(self.pin_1, GPIO.OUT, initial=GPIO.HIGH)
        GPIO.setup(self.pin_2, GPIO.OUT, initial=GPIO.HIGH)

    def __del__(self):
        try:
            GPIO.cleanup(self.pin_1)
            GPIO.cleanup(self.pin_2)

        except Exception:
            pass


if __name__ == "__main__":
    switch = RfSwitch(pin_1=8, pin_2=10)
    while True:
        print("Setting to HIGH")
        GPIO.output(switch.pin_1, GPIO.HIGH)
        GPIO.output(switch.pin_2, GPIO.HIGH)
        time.sleep(1)
        print("Setting to LOW")
        GPIO.output(switch.pin_1, GPIO.LOW)
        GPIO.output(switch.pin_2, GPIO.LOW)
        time.sleep(1)

i get ValueError:

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/Jetson/GPIO/gpio.py", line 333, in setup
    ch_infos = _channels_to_infos(channels, need_gpio=True)
  File "/usr/lib/python3/dist-packages/Jetson/GPIO/gpio.py", line 124, in _channels_to_infos
    return [_channel_to_info_lookup(c, need_gpio, need_pwm)
  File "/usr/lib/python3/dist-packages/Jetson/GPIO/gpio.py", line 124, in <listcomp>
    return [_channel_to_info_lookup(c, need_gpio, need_pwm)
  File "/usr/lib/python3/dist-packages/Jetson/GPIO/gpio.py", line 110, in _channel_to_info_lookup
    raise ValueError("Channel %s is invalid" % str(channel))
ValueError: Channel 8 is invalid

I use Jetson.GPIO==2.1.9 (installed with pip install, version picked automatically with pip) it has hardcoded values in */site-packages/Jetson/GPIO/gpio_pin_data.py

JETSON_ORIN_NX_PIN_DEFS = [
    (144, 'PAC.06', "tegra234-gpio", 7, 4, 'GPIO09', 'GP167', None, None),
    (112, 'PR.04', "tegra234-gpio", 11, 17, 'UART1_RTS', 'GP72_UART1_RTS_N', None, None),
    (50, 'PH.07', "tegra234-gpio", 12, 18, 'I2S0_SCLK', 'GP122', None, None),
    (122, 'PY.00', "tegra234-gpio", 13, 27, 'SPI1_SCK', 'GP36_SPI3_CLK', None, None),
    (85, 'PN.01', "tegra234-gpio", 15, 22, 'GPIO12', 'GP88_PWM1', '3280000.pwm', 0),
    (126, 'PY.04', "tegra234-gpio", 16, 23, 'SPI1_CS1', 'GP40_SPI3_CS1_N', None, None),
    (125, 'PY.03', "tegra234-gpio", 18, 24, 'SPI1_CS0', 'GP39_SPI3_CS0_N', None, None),
    (135, 'PZ.05', "tegra234-gpio", 19, 10, 'SPI0_MOSI', 'GP49_SPI1_MOSI', None, None),
    (134, 'PZ.04', "tegra234-gpio", 21, 9, 'SPI0_MISO', 'GP48_SPI1_MISO', None, None),
    (123, 'PY.01', "tegra234-gpio", 22, 25, 'SPI1_MISO', 'GP37_SPI3_MISO', None, None),
    (133, 'PZ.03', "tegra234-gpio", 23, 11, 'SPI0_SCK', 'GP47_SPI1_CLK', None, None),
    (136, 'PZ.06', "tegra234-gpio", 24, 8, 'SPI0_CS0', 'GP50_SPI1_CS0_N', None, None),
    (137, 'PZ.07', "tegra234-gpio", 26, 7, 'SPI0_CS1', 'GP51_SPI1_CS1_N', None, None),
    (105, 'PQ.05', "tegra234-gpio", 29, 5, 'GPIO01', 'GP65', None, None),
    (106, 'PQ.06', "tegra234-gpio", 31, 6, 'GPIO11', 'GP66', None, None),
    (41, 'PG.06', "tegra234-gpio", 32, 12, 'GPIO07', 'GP113_PWM7', '32e0000.pwm', 0),
    (43, 'PH.00', "tegra234-gpio", 33, 13, 'GPIO13', 'GP115', '32c0000.pwm', 0),
    (53, 'PI.02', "tegra234-gpio", 35, 19, 'I2S0_FS', 'GP125', None, None),
    (113, 'PR.05', "tegra234-gpio", 36, 16, 'UART1_CTS', 'GP73_UART1_CTS_N', None, None),
    (124, 'PY.02', "tegra234-gpio", 37, 26, 'SPI1_MOSI', 'GP38_SPI3_MOSI', None, None),
    (52, 'PI.01', "tegra234-gpio", 38, 20, 'I2S0_SDIN', 'GP124', None, None),
    (51, 'PI.00', "tegra234-gpio", 40, 21, 'I2S0_SDOUT', 'GP123', None, None),
]

Which are used to check if pin i selected is valid. After i add here my pins info i’m able to control output and script works great (i can see it with my tester)

(110, 'PR.02', "tegra234-gpio", 8, 14, 'UART1_TXD', 'GP70', None, None),
(111, 'PR.03', "tegra234-gpio", 10, 15, 'UART1_RXD', 'GP71', None, None),

So my question is - how can i use official gpio python lib to use these pins "out of the box’ without manually patching the library?

*** Please note that this reply is generated by LLM automatically ***

The issue you’re facing is due to the fact that the JetsonGPIO library doesn’t have the pin information for the 8 and 10 pins of the 40-pin header by default. This is because these pins are not part of the standard GPIO pins on the Jetson Nano.

To use these pins as GPIO outputs without manually patching the library, you can try the following approaches:

  1. Use the Jetson.GPIO library with a custom pin definition: You can define a custom pin definition in your Python script using the Jetson.GPIO library. This will allow you to use the 8 and 10 pins as GPIO outputs without modifying the library.

Here’s an example of how you can define a custom pin definition:

import Jetson.GPIO as GPIO

# Define custom pin definition
GPIO.setmode(GPIO.BOARD)
GPIO.setup(8, GPIO.OUT)
GPIO.setup(10, GPIO.OUT)

# Use the pins as GPIO outputs
GPIO.output(8, GPIO.HIGH)
GPIO.output(10, GPIO.HIGH)
  1. Use the python-gpio library: The python-gpio library is a more flexible and customizable library for working with GPIO pins on the Jetson Nano. It allows you to define custom pin definitions and use them as GPIO outputs.

Here’s an example of how you can use the python-gpio library:

import gpio

# Define custom pin definition
gpio.setmode(gpio.BOARD)
gpio.setup(8, gpio.OUT)
gpio.setup(10, gpio.OUT)

# Use the pins as GPIO outputs
gpio.output(8, gpio.HIGH)
gpio.output(10, gpio.HIGH)
  1. Modify the jetson-gpio library: If you want to use the jetson-gpio library without modifying your Python script, you can modify the library itself to include the pin information for the 8 and 10 pins.

You can modify the site-packages/Jetson.GPIO/gpio/pindata.py file to include the pin information for the 8 and 10 pins. Here’s an example of how you can modify the file:

# Add the following lines to the file
PIN8 = 8
PIN10 = 10

# Update the pin definition dictionary
pin_definition = {
    # ... existing pin definitions ...
    PIN8: {'name': 'GPIO8', 'type': 'GPIO'},
    PIN10: {'name': 'GPIO10', 'type': 'GPIO'}
}

After modifying the library, you should be able to use the 8 and 10 pins as GPIO outputs without manually patching the library.

Comparison of the approaches:

  • Custom pin definition: This approach requires modifying your Python script to define a custom pin definition. It’s a simple and flexible approach, but it may not be suitable if you want to use the jetson-gpio library without modifying your script.
  • python-gpio library: This approach requires using a different library, which may have a different API and functionality compared to the jetson-gpio library. However, it’s a more flexible and customizable library that allows you to define custom pin definitions.
  • Modifying the jetson-gpio library: This approach requires modifying the library itself, which may not be suitable if you’re using a pre-built library or don’t want to modify the library code. However, it’s a more permanent solution that allows you to use the 8 and 10 pins as GPIO outputs without modifying your Python script.

*** Please give the thumbs up if you get answers you like. Or provide feedback to help us improve the answer. ***