Use mmap access sysram is not working

Hello everyone:
I’m trying use “mmap” to access sysram,right now, i can read from sysram(actually,i don’t know the data read from sysram is right or wrong), but, sysram data not be changed after write data to it,does any one knows what wrong with it?

my code is bellow:

Blockquote

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <stdbool.h>

//*****************************************************************************
#define FATAL do { printf( “Error at line %d, file %s (%d) [%s]\n”,
LINE, FILE, errno, strerror(errno)); exit(1); } while(0)

#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
#define STEP 8
//*****************************************************************************

static int Devmem_Write(unsigned long writeAddr, unsigned long* buf, unsigned long len)
{
int i = 0;
int ret = 0;
int fd;
void *map_base, *virt_addr;
unsigned long addr = writeAddr;
int offset_len = 0;

if(len == 0)
{
    printf("%s %s %d, len = 0\n", __FILE__, __func__, __LINE__);
    return -1;
}

if ((fd = open("/dev/mem", O_RDWR)) == -1)
{
	printf( "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
	return -1;
}

/* Map one page */ 
map_base = mmap64(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr & ~MAP_MASK);
//map_base=(void*)((long int )map_base & 0x007fffffffff);
if(map_base == (void *) -1)
{
	printf("Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
	close(fd);
	return -1;
}

	
for (i = 0; i < len; i++)
{
	
    if(offset_len >= MAP_MASK)
    { 
        offset_len = 0;
        if(munmap(map_base, MAP_SIZE) == -1)
    	{
    		printf("Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
    		close(fd);
    		return -1;
    	}
        map_base = mmap64(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr & ~MAP_MASK);
		//map_base=(void*)((long int )map_base & 0x007fffffffff);
        if(map_base == (void *) -1)
    	{
    		printf("Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
    		close(fd);
    		return -1;
        }
        printf("map_base over 4k = [%p].\n", map_base);		
    }
	virt_addr = map_base + (addr & MAP_MASK);	
	printf("map_base = %08X , virt_addr=%08X , addr=%08X ,buf[%d]=%d \n",map_base,virt_addr,addr,i,buf[i]);
	*((unsigned long *) virt_addr) = (unsigned long)buf[i]; 	
	printf("virt_data is %08X \n",*((unsigned long *) virt_addr));
	addr += STEP;
    offset_len += STEP;
}

    
if(munmap(map_base, MAP_SIZE) == -1)
{
	printf("Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
	return -1;
}
else{
	printf("close map\n");
}

close(fd);
printf("close mem\n");
return 0;

}

static int Devmem_Read(unsigned long readAddr, unsigned long* buf, unsigned long len)
{
int i = 0;
int fd,ret;
int offset_len = 0;
void *map_base, *virt_addr;
off_t addr = readAddr;
unsigned long littleEndianLength = 0;

if ((fd = open("/dev/mem", O_RDWR)) == -1)
{
	printf( "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
	return 0;
}

/* Map one page */
map_base = mmap64(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr & ~MAP_MASK);
//map_base=(void*)((long int )map_base & 0x007fffffffff);
if(map_base == (void *) -1)
{
	printf( "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
	close(fd);
	return 0;
}

for (i = 0; i < len; i++)
{
	
    if(offset_len >= MAP_MASK)
    {
        offset_len = 0;
        if(munmap(map_base, MAP_SIZE) == -1)
    	{
    		printf( "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
    		close(fd);
    		return 0;
    	}
        map_base = mmap64(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, addr & ~MAP_MASK);
		//map_base=(void*)((long int )map_base & 0x007fffffffff);
        if(map_base == (void *) -1)
    	{
    		printf( "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
    		close(fd);
    		return 0;
        }
    }
	printf("map_base = %08X , addr=%08X ,",map_base,addr);
	virt_addr = map_base + (addr & MAP_MASK);	
	printf("virt_addr = %08X , ",virt_addr);
	buf[i] = *((unsigned long *) virt_addr);	
	printf("buf[%d] = %x \n ",i,buf[i]);
	addr += STEP;
    offset_len += STEP;
}

if(munmap(map_base, MAP_SIZE) == -1)
{
	printf( "Error at line %d, file %s (%d) [%s]\n", __LINE__, __FILE__, errno, strerror(errno));
	close(fd);
	return 0;
}
else{
	printf("close map\n");
}
close(fd);
printf("close mem\n");
return i;

}

static void Devmem_usage(void)
{
printf(“Usage ./devmem_tool read/write 0xfa0000 20\n”);
printf(“The read/write is the command type, read or write data to memory\n”);
printf(“The 0xfa2000 is the memory physical address\n”);
printf(“The 20 is read/write data length, uint is 4 byte, so it is total 20 * 4 = 80 bytes\n”);
printf(“Usage: ./devmem_tool read 0xfa0000 20\n”);
printf(“Usage: ./devmem_tool write 0xfa0000 20\n”);
}

int main(int argc, char** argv)
{
unsigned long len = 0;
unsigned long writeData[8192];
unsigned long readData[8192];
unsigned long addr = 0;
unsigned long i = 0;

addr = 0x40000000;
len = 20;

	printf("read data\n");
	memset(readData, 0, len);				
	Devmem_Read(addr, readData, len);		
	// for (i = 0; i < len; i++)
	// {
	// 	printf("address = 0x%08x, data = 0x%08x\n", (addr + i * 4), readData[i]);
	// }

	printf("write data--------------------------------------------------\n");
	memset(writeData, 0, len);
	for (i = 0; i < len; i++)
	{
		writeData[i] = i+10;
	}
	Devmem_Write(addr, writeData, len);		



	printf("read data----------------------------------------------------\n");
	memset(readData, 0, len);				
	Devmem_Read(addr, readData, len);		
	// for (i = 0; i < len; i++)
	// {
	// 	printf("address = 0x%08x, data = 0x%08x\n", (addr + i * 4), readData[i]);
	// }

}

Blockquote

and the print is :
read data
map_base = 8E92A000 , addr=40000000 ,virt_addr = 8E92A000 , buf[0] = dead2009
map_base = 8E92A000 , addr=40000008 ,virt_addr = 8E92A008 , buf[1] = dead2009
map_base = 8E92A000 , addr=40000010 ,virt_addr = 8E92A010 , buf[2] = dead2009
map_base = 8E92A000 , addr=40000018 ,virt_addr = 8E92A018 , buf[3] = dead2009
map_base = 8E92A000 , addr=40000020 ,virt_addr = 8E92A020 , buf[4] = dead2009
map_base = 8E92A000 , addr=40000028 ,virt_addr = 8E92A028 , buf[5] = dead2009
map_base = 8E92A000 , addr=40000030 ,virt_addr = 8E92A030 , buf[6] = dead2009
map_base = 8E92A000 , addr=40000038 ,virt_addr = 8E92A038 , buf[7] = dead2009
map_base = 8E92A000 , addr=40000040 ,virt_addr = 8E92A040 , buf[8] = dead2009
map_base = 8E92A000 , addr=40000048 ,virt_addr = 8E92A048 , buf[9] = dead2009
map_base = 8E92A000 , addr=40000050 ,virt_addr = 8E92A050 , buf[10] = dead2009
map_base = 8E92A000 , addr=40000058 ,virt_addr = 8E92A058 , buf[11] = dead2009
map_base = 8E92A000 , addr=40000060 ,virt_addr = 8E92A060 , buf[12] = dead2009
map_base = 8E92A000 , addr=40000068 ,virt_addr = 8E92A068 , buf[13] = dead2009
map_base = 8E92A000 , addr=40000070 ,virt_addr = 8E92A070 , buf[14] = dead2009
map_base = 8E92A000 , addr=40000078 ,virt_addr = 8E92A078 , buf[15] = dead2009
map_base = 8E92A000 , addr=40000080 ,virt_addr = 8E92A080 , buf[16] = dead2009
map_base = 8E92A000 , addr=40000088 ,virt_addr = 8E92A088 , buf[17] = dead2009
map_base = 8E92A000 , addr=40000090 ,virt_addr = 8E92A090 , buf[18] = dead2009
map_base = 8E92A000 , addr=40000098 ,virt_addr = 8E92A098 , buf[19] = dead2009
close map
close mem
write data--------------------------------------------------
map_base = 8E92A000 , virt_addr=8E92A000 , addr=40000000 ,buf[0]=10
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A008 , addr=40000008 ,buf[1]=11
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A010 , addr=40000010 ,buf[2]=12
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A018 , addr=40000018 ,buf[3]=13
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A020 , addr=40000020 ,buf[4]=14
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A028 , addr=40000028 ,buf[5]=15
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A030 , addr=40000030 ,buf[6]=16
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A038 , addr=40000038 ,buf[7]=17
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A040 , addr=40000040 ,buf[8]=18
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A048 , addr=40000048 ,buf[9]=19
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A050 , addr=40000050 ,buf[10]=20
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A058 , addr=40000058 ,buf[11]=21
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A060 , addr=40000060 ,buf[12]=22
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A068 , addr=40000068 ,buf[13]=23
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A070 , addr=40000070 ,buf[14]=24
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A078 , addr=40000078 ,buf[15]=25
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A080 , addr=40000080 ,buf[16]=26
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A088 , addr=40000088 ,buf[17]=27
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A090 , addr=40000090 ,buf[18]=28
virt_data is DEAD2009
map_base = 8E92A000 , virt_addr=8E92A098 , addr=40000098 ,buf[19]=29
virt_data is DEAD2009
close map
close mem
read data----------------------------------------------------
map_base = 8E92A000 , addr=40000000 ,virt_addr = 8E92A000 , buf[0] = dead2009
map_base = 8E92A000 , addr=40000008 ,virt_addr = 8E92A008 , buf[1] = dead2009
map_base = 8E92A000 , addr=40000010 ,virt_addr = 8E92A010 , buf[2] = dead2009
map_base = 8E92A000 , addr=40000018 ,virt_addr = 8E92A018 , buf[3] = dead2009
map_base = 8E92A000 , addr=40000020 ,virt_addr = 8E92A020 , buf[4] = dead2009
map_base = 8E92A000 , addr=40000028 ,virt_addr = 8E92A028 , buf[5] = dead2009
map_base = 8E92A000 , addr=40000030 ,virt_addr = 8E92A030 , buf[6] = dead2009
map_base = 8E92A000 , addr=40000038 ,virt_addr = 8E92A038 , buf[7] = dead2009
map_base = 8E92A000 , addr=40000040 ,virt_addr = 8E92A040 , buf[8] = dead2009
map_base = 8E92A000 , addr=40000048 ,virt_addr = 8E92A048 , buf[9] = dead2009
map_base = 8E92A000 , addr=40000050 ,virt_addr = 8E92A050 , buf[10] = dead2009
map_base = 8E92A000 , addr=40000058 ,virt_addr = 8E92A058 , buf[11] = dead2009
map_base = 8E92A000 , addr=40000060 ,virt_addr = 8E92A060 , buf[12] = dead2009
map_base = 8E92A000 , addr=40000068 ,virt_addr = 8E92A068 , buf[13] = dead2009
map_base = 8E92A000 , addr=40000070 ,virt_addr = 8E92A070 , buf[14] = dead2009
map_base = 8E92A000 , addr=40000078 ,virt_addr = 8E92A078 , buf[15] = dead2009
map_base = 8E92A000 , addr=40000080 ,virt_addr = 8E92A080 , buf[16] = dead2009
map_base = 8E92A000 , addr=40000088 ,virt_addr = 8E92A088 , buf[17] = dead2009
map_base = 8E92A000 , addr=40000090 ,virt_addr = 8E92A090 , buf[18] = dead2009
map_base = 8E92A000 , addr=40000098 ,virt_addr = 8E92A098 , buf[19] = dead2009
close map
close mem

Maybe the TRM have information about it. Have a check the Chapter 2 “Address Map” to check if can help.