Writing to HW registers via /dev/mem needs extra write
I want to write to some HW registers using /dev/mem on Linux. The target board is ZYBO (Zynq, ARM Cortex-A9), and the HW is AXI4 Lite Slave with 4 registers which is automatically generated by Xilinx Vivado.
Here is the C code to write to the HW registers.
#include <sys/mman.h>
#include <fcntl.h>
#include <stdio.h>
#define MAP_BASE (0x43c30000)
#define MAP_RANGE (0x10000)
int main(int argc, char *argv[])
{
volatile unsigned int *p;
void *iomap_ptr;
int fd, i, v;
fd = open("/dev/mem", O_RDWR | O_SYNC);
iomap_ptr = mmap(0, MAP_RANGE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, MAP_BASE);
p = (volatile unsigned int *)iomap_ptr;
for (i = 0; i < 4; i++)
p[i] = i;
for (i = 0; i < 4; i++)
printf("%04X: %08X\n", i, p[i]);
munmap(iomap_ptr, MAP_RANGE);
close(fd);
return 0;
}
When I run the C code first time, the result is:
0000: 00000000
0001: 00000001
0002: 00000002
0003: 00000000
It seems that the final write is not applied.
Then, when I run the C code once again, the result is:
0000: 00000000
0001: 00000001
0002: 00000002
0003: 00000003
After investigation, I realized that I need an extra write to the HW in order to apply the last write.
How can I write to the HW registers without an extra write?