LinuxQuestions.org
Visit the LQ Articles and Editorials section
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Hardware > Linux - Embedded & Single-board computer
User Name
Password
Linux - Embedded & Single-board computer This forum is for the discussion of Linux on both embedded devices and single-board computers (such as the Raspberry Pi, BeagleBoard and PandaBoard). Discussions involving Arduino, plug computers and other micro-controller like devices are also welcome.

Notices

Reply
 
Search this Thread
Old 10-15-2009, 05:05 AM   #1
atif_shabir
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Rep: Reputation: 0
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?
 
Old 10-15-2009, 05:52 AM   #2
sunr2007
Member
 
Registered: Jan 2009
Location: Bangalore , India
Distribution: Fedora 12
Posts: 65

Rep: Reputation: 17
Quote:
Originally Posted by atif_shabir View Post
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.
 
Old 10-16-2009, 03:51 AM   #3
atif_shabir
LQ Newbie
 
Registered: Oct 2009
Posts: 7

Original Poster
Rep: Reputation: 0
Cannot write to the mapped memory location

Quote:
Originally Posted by sunr2007 View Post
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
.

Last edited by atif_shabir; 10-16-2009 at 03:53 AM.
 
Old 10-20-2009, 01:31 AM   #4
sunr2007
Member
 
Registered: Jan 2009
Location: Bangalore , India
Distribution: Fedora 12
Posts: 65

Rep: Reputation: 17
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.
 
  


Reply

Tags
mmap


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Question on using mmap to access physical memory Gnu2Linux2 Linux - Embedded & Single-board computer 1 10-05-2009 03:54 AM
Help needed: mmap and physical memory access manaav Linux - Newbie 3 03-25-2009 03:38 AM
How to access Physical memory not mapped in kernel sbc Programming 0 02-09-2009 09:13 AM
How to access physical (as opposed to virtual) memory in C language for a Linux app. ss729 Programming 2 12-23-2008 03:02 PM
How to access physical memory in C? cyu021 Programming 6 03-27-2005 04:16 AM


All times are GMT -5. The time now is 04:01 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration