Write to a physical address in Linux on ARM

1

I would like to write an integer(just one value, it can be also other type) to a specific register (for example: 0x60006666) on a Linux machine using the ARM platform.

There are many examples using the mmap(2), but it is not clear how to write just one value to a specific address using the mmap(). Having a look at the mmap() manual, it does not specify what value to write to the specific register: http://man7.org/linux/man-pages/man2/mmap.2.html

Here is the function:

void *mmap(void *addr, size_t length, int prot, int flags,
              int fd, off_t offset);

It is clear that *addr is the address, but where do we insert the value is written to this address?

In my case I would like to write an int to a specific address, how would mmap look like?

#define _WRITE_ADDR       0x60006666 //address where to write 
unsigned int value_addr = 0x00000080 //value to be written to the address

I would like to write the above mentioned value to the specified address. It should be trivial, but not very clear since it has been some time since I worked with this type of questions, hopefully somebody has some hints. Thanks!

Similar question:
WRITE and READ registers in Linux on ARM

c
linux
memory
arm
memory-address
asked on Stack Overflow Dec 21, 2018 by SW Dev

1 Answer

2

It is rather simple. Example based on RPI

first you need to:

mem_fd = open("/dev/mem", O_RDWR | O_SYNC);

then allocate memory for the map. For example for one BCM RPi peritheral it will me 4K + 4K

periph_mem = malloc( 8*1024 - 1);

then make sure that it is 4k aligned and mmap it:

gpio_map = (unsigned char *)mmap((caddr_t)poriph_mem, BLOCK_SIZE,PROT_READ | PROT_WRITE, MAP_SHARED | MAP_FIXED, mem_fd, PERIPH_BASE    );

where PERIPH_BASE is the base address of the peripheral (for example GPIO 0x20000000 (BCM peripherals base) + 0x200000)

then you can access them as normal pointers (but remember they have to volatile)

*(volatile uint32_t *)(periph_mem + OFFSET) = VALUE;
answered on Stack Overflow Dec 21, 2018 by 0___________

User contributions licensed under CC BY-SA 3.0