LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Embedded & Single-board computer (https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/)
-   -   How to access physical memory directly? (https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/how-to-access-physical-memory-directly-762044/)

atif_shabir 10-15-2009 05:05 AM

How to access physical memory directly?
 
Hello

I am working on a board which has two processors; an ARM9, and a TI's DaVinci processor. ARM9 has Linux running on it and DaVinci is running without operating system. There is a shared memory space between them and I want to use a small portion of this shared memory as communication channel between the two processors. In order to do so I have to access the physical memory directly from Linux so that I can write to it from Linux (ARM9) and then read it from DaVinci.

Can I do it using mmap()?
or will I have to write a driver for it?

How can I make sure that this small portion of memory is not allocated to some other process by the kernel on ARM9?

sunr2007 10-15-2009 05:52 AM

Quote:

Originally Posted by atif_shabir (Post 3720055)
Hello

I am working on a board which has two processors; an ARM9, and a TI's DaVinci processor. ARM9 has Linux running on it and DaVinci is running without operating system. There is a shared memory space between them and I want to use a small portion of this shared memory as communication channel between the two processors. In order to do so I have to access the physical memory directly from Linux so that I can write to it from Linux (ARM9) and then read it from DaVinci.

Can I do it using mmap()?
or will I have to write a driver for it?

How can I make sure that this small portion of memory is not allocated to some other process by the kernel on ARM9?

yes u can use tat memory by using mmap () system call by using the /dev/mem/ interface . but u will not get good performance . u should use mmap() only when u dont av any other choice. to use mmap u should declare something like this
#define MAP_SIZE 4096
#define MAP_MASK (MAP_SIZE-1)
off_t target=0xFFFFF43C; //the physical address which u should know.


and use mmap() similar to below.
map_base=mmap(0,MAP_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,fd1,target & ~ MAP_MASK);

if(map_base == (void *) -1)
{
perror("map_base"); exit(-1);
}


the above i have used for ARM 9 based AT91SAM9261
or best is u can convert this to kernel space by using a mofule. tat way ur performance will be better.
warm regards,
Ravi Kulkarni.

atif_shabir 10-16-2009 03:51 AM

Cannot write to the mapped memory location
 
Quote:

Originally Posted by sunr2007 (Post 3720103)
yes u can use tat memory by using mmap () system call by using the /dev/mem/ interface . but u will not get good performance . u should use mmap() only when u dont av any other choice. to use mmap u should declare something like this
#define MAP_SIZE 4096
#define MAP_MASK (MAP_SIZE-1)
off_t target=0xFFFFF43C; //the physical address which u should know.


and use mmap() similar to below.
map_base=mmap(0,MAP_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,fd1,target & ~ MAP_MASK);

if(map_base == (void *) -1)
{
perror("map_base"); exit(-1);
}


the above i have used for ARM 9 based AT91SAM9261
or best is u can convert this to kernel space by using a mofule. tat way ur performance will be better.
warm regards,
Ravi Kulkarni.


Thanks a lot Ravi for the reply. I tried mmap, the way you told me, on my desktop computer first but I could not write to the mapped memory. mmap() returns a valid pointer and I can read the data at that pointer. It always shows FF. When I write at it by assigning a new value, the data at that location doesn't change and it remains FF. What could be wrong? the code that I ran is

///////////////////////////////////////////
#include <stdio.h>
#include <sys/mman.h>
#include <fcntl.h>

#define MAP_SIZE 4096
#define MAP_MASK (MAP_SIZE-1)
off_t target = 0xD000F000;

int main(void)
{
int fd1;
char *map_base;

if((fd1=open("/dev/mem",O_RDWR))==0) {
perror("Error openning file /dev/mem");
return -1;
}

map_base = (char *)mmap(0,MAP_SIZE,PROT_READ|PROT_WRITE,MAP_SHARED,fd1,target&~MAP_MASK);

if(map_base == (void *)-1) {
perror("Error mapping");
return -2;
}

printf("Value at map_base before writing is %2X\n",map_base[0]);

map_base[0] = 1; //***This should alter the value at this memory location***

printf("Value at map_base after writing is %2X\n",map_base[0]);
//***This should print 1 but it prints FF***

return 0;
}
////////////////////////////////////

Regards,
Atif Shabir
.

sunr2007 10-20-2009 01:31 AM

If u tried this on your desktop make sure u check ur PAGE size correctly ? and also check whether this physical address is valid! what i gave example for ARM based board and what u are trying is on x86 unless ur desktop is other ARCH . check arch dependency first.
warm regards,
Ravi Kulkarni.


All times are GMT -5. The time now is 10:05 AM.