Toggling GPIOs via DMA in Python – Unable to Access Registers

I want to toggle my GPIOs using the DMA method in Python, but I am unable to access the base registers and their offsets for GPIO pins 2, 3, 7, and 13. Could you please provide a script for this?

Hi rakesh2011,

Are you using the devkit or custom board for Jetson Nano?
What’s the Jetpack version in use?

Do you mean these pins from 40-pins header?

You can refer to Calculating/Getting register adresses of gpio pins - #10 by KevinFFF to calculate the register address for GPIO pins.
Although they are the example from AGX Orin, you can just search the similar keywords in the TRM document of Jetson Nano.

1 Like

I’m using custom board and SOM has jetpack 4.6. My gpio pins are GPIO02 , GPIO03, GPIO07, GPIO13

below base code i am using to toggled the gpio13

import mmap
import os
import struct
import time

GPIO_BASE = 0x6000D000 # Jetson Nano GPIO Base Address

GPIO_PE_OFFSET = 0x080 # Port E Register Offsets
GPIO_OUTPUT_OFFSET = 0x290 # Data register
GPIO_DIR_OFFSET = 0x2A0 # Direction register

PE6_BIT = (1 << 6) # GPIO_PE6 corresponds to bit 6 # GPIO_PE6 Bit Mask (since it’s PE6)

fd = os.open(“/dev/mem”, os.O_RDWR | os.O_SYNC) # Open /dev/mem to access memory-mapped registers

mem = mmap.mmap(fd, mmap.PAGESIZE, mmap.MAP_SHARED,
mmap.PROT_READ | mmap.PROT_WRITE, offset=GPIO_BASE)

def read_reg(offset): # Function to read register
mem.seek(offset)
return struct.unpack(“I”, mem.read(4))[0]

def write_reg(offset, value): # Function to write register
mem.seek(offset)
mem.write(struct.pack(“I”, value))

reg_value = read_reg(GPIO_DIR_OFFSET) # 1. Set GPIO_PE6 as output
reg_value |= PE6_BIT # Set bit 6 to 1 (output mode)
write_reg(GPIO_DIR_OFFSET, reg_value)

while True: # 2. Toggle GPIO_PE6
# Set PE6 HIGH
reg_value = read_reg(GPIO_OUTPUT_OFFSET)
reg_value |= PE6_BIT
write_reg(GPIO_OUTPUT_OFFSET, reg_value)
print(“GPIO_PE6 HIGH”)

time.sleep(1)

# Set PE6 LOW
reg_value = read_reg(GPIO_OUTPUT_OFFSET)
reg_value &= ~PE6_BIT
write_reg(GPIO_OUTPUT_OFFSET, reg_value)
print("GPIO_PE6 LOW")

time.sleep(1)

mem.close() # Cleanup
os.close(fd)

Have you managed to control GPIO through DRA (Direct Register Access) with your code successfully?

No, we are not able to find proper register for toggling GPIO13,GPIO7 ,GPIO2 and GPIO3

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