Search code examples
pythongpionvidia-jetson

how to setup gpio pins in GPIO.TEGRA_SOC mode vs GPIO.BCM mode using Jetson nano GPIO?


while finding a number of examples on how to setup gpio using GPIO.BCM mode (in which pins numbers are equivalent to RPI (see image and nvidia/jetson-gpio), I could not find an example to setup pins in GPIO.TEGRA_SOC mode. so after digging into the gpio library I thought I would share this in an orderly fashion. see answer below.

jetson nano gpio layout


Solution

  • BCM mode, defining pins 9 (signal from nano out. e.g. trigger) and 11 (signal from sensor into the nan0) -

    import Jetson.GPIO as GPIO
    GPIO.setmode(GPIO.BCM)
    GPIO.setup(9, GPIO.OUT)
    GPIO.setup(11, GPIO.IN)
    

    after setup, getting value from input pin -

    GPIO.input(11)
    

    after setup, setting value from output pin -

    GPIO.output(9, False)
    GPIO.output(9, True)
    

    TEGRA_SOC mode, defining pins 9 and 11 as before. replace number with identifying string names {9: 'SPI1_MISO', 11: 'SPI1_SCK'}

    import Jetson.GPIO as GPIO
    GPIO.setmode(GPIO.TEGRA_SOC)
    GPIO.setup('SPI1_MISO', GPIO.OUT)
    GPIO.setup('SPI1_SCK', GPIO.IN)
    

    after setup, getting value from input pin -

    GPIO.input('SPI1_SCK')
    

    after setup, setting value from output pin -

    GPIO.output('SPI1_MISO', False)
    GPIO.output('SPI1_MISO', True)
    

    in general, to print out naming of all gpio pins in each possible mode: ['BOARD', 'BCM', 'CVM', 'TEGRA_SOC'] use -

    print(GPIO.gpio_pin_data.get_data()[-1])
    

    to create a nice dictionary converting bcm num to tegra use -

    bcm_to_tegra = {
    k: list(GPIO.gpio_pin_data.get_data()[-1]['TEGRA_SOC'].keys())[i] for i, k in enumerate(GPIO.gpio_pin_data.get_data()[-1]['BCM'])}
    
    for k, v in bcm_to_tegra.items():
        print('bcm #:', k, 'tegra:', v)
    
    // output:
    bcm #: 4 tegra: AUD_MCLK
    bcm #: 17 tegra: UART2_RTS
    bcm #: 18 tegra: DAP4_SCLK
    bcm #: 27 tegra: SPI2_SCK
    bcm #: 22 tegra: LCD_TE
    bcm #: 23 tegra: SPI2_CS1
    bcm #: 24 tegra: SPI2_CS0
    bcm #: 10 tegra: SPI1_MOSI
    bcm #: 9 tegra: SPI1_MISO
    bcm #: 25 tegra: SPI2_MISO
    bcm #: 11 tegra: SPI1_SCK
    bcm #: 8 tegra: SPI1_CS0
    bcm #: 7 tegra: SPI1_CS1
    bcm #: 5 tegra: CAM_AF_EN
    bcm #: 6 tegra: GPIO_PZ0
    bcm #: 12 tegra: LCD_BL_PW
    bcm #: 13 tegra: GPIO_PE6
    bcm #: 19 tegra: DAP4_FS
    bcm #: 16 tegra: UART2_CTS
    bcm #: 26 tegra: SPI2_MOSI
    bcm #: 20 tegra: DAP4_DIN
    bcm #: 21 tegra: DAP4_DOUT