Memmory mapping in jetson

I developed a code to memmory map using mmap function in ubuntu worked well in main()
int fd = open(“/dev/mem”, O_RDWR | O_SYNC);
if (fd < 0)
void *map = mmap(NULL, MEM_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
if (map == MAP_FAILED)
int *req1 = (int *)map;
req1[0] = 1;
std::cout << "Request one: " << req1[0] << std::endl;
std::cout << "Request one: " << req1 << std::endl;
// Open serial port
int usb_fd;
struct termios serial;
std::string port = “/dev/ttyACM0”; // Replace with the correct port
usb_fd = open(port.c_str(), O_RDWR | O_NOCTTY | O_NDELAY);
if (usb_fd == -1)
std::cerr << “Error: Unable to open serial port.” << std::endl;
return 1;
} i got output as 1 in ubuntu.but when i tried it in jetson xavier nx module i got some random values.i tried in python also with the code below

import mmap

MEM_SIZE = 2048 # 2k

def memorymap():
# Open /dev/mem with read/write and synchronous I/O flags
with open(“/dev/mem”, “r+b”) as fd:
# Map memory file into virtual address space with read/write permissions and shared access
mem_map = mmap.mmap(fd.fileno(), MEM_SIZE, mmap.MAP_SHARED, mmap.PROT_READ | mmap.PROT_WRITE)
return mem_map

Obtain the memory map

mem_map = memorymap()
register_map = {
‘ACT1_CUR_DMD’: slice(268, 272),
‘ACT2_CUR_DMD’: slice(272, 276),
‘ACT3_CUR_DMD’: slice(276, 280),
‘ACT4_CUR_DMD’: slice(280, 284),
‘ACT5_CUR_DMD’: slice(284, 288)

Read values from the text file and write them into corresponding registers

register_values = {}
with open(‘/home/amma/reg_input.txt’, ‘r’) as file:
for line in file:
# Split each line into register name and value
register_name, value = line.strip().split(‘=’)
register_name = register_name.strip()
value = int(value.strip())

    # Assign the value to the corresponding register
    if register_name in register_map:
        register_values[register_name] = value

Iterate over the register map and assign values to the corresponding registers

for register_name, register_slice in register_map.items():
if register_name in register_values:
value = register_values[register_name]
mem_map[register_slice] = value.to_bytes(4, byteorder=‘little’)

Create a dictionary to store register names and values

registers = {}
for register_name, register_slice in register_map.items():
if register_name in register_values:
registers[register_name] = int.from_bytes(mem_map[register_slice], byteorder=‘little’)

Write register values to the output file

with open(‘/home/amma/reg_output.txt’, ‘w’) as file:
for register_name, register_value in registers.items():
file.write(f’{register_name}: {register_value}\n’)

Remember to clean up the memory map when you’re done

mem_map.close() .

then also i didnot get expected output .Can you help me as soon as possible.

mmap() is supposed to work since we have called it in the jetson_multimedia_api samples. The samples are in


Not sure why it does not work in this use-case. This would need other users to share experience.

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