Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Memory Mapping for an FPGA

446 views
Skip to first unread message

Mark

unread,
Sep 28, 2002, 10:35:55 PM9/28/02
to
I am using Bluecat Linux running on an ARM processor to interface with an
FPGA via memory mapping. This is my first experience using Linux as an
embedded operating system. I have no idea how to approach a solution.
Wasted a lot of time browsing websites with no luck. There must be an easy
way to read and write to physical memory. Any suggestions.

TIA,
Mark


Steve Watt

unread,
Sep 29, 2002, 4:29:13 PM9/29/02
to
In article <fGtl9.288028$216.11...@bin4.nnrp.aus1.giganews.com>,

mmap().

Open /dev/mem, and call mmap() on the file descriptor you get back with
the appropriate offset.
--
Steve Watt KD6GGD PP-ASEL-IA ICBM: 121W 56' 57.8" / 37N 20' 14.9"
Internet: steve @ Watt.COM Whois: SW32
Free time? There's no such thing. It just comes in varying prices...

Larry Doolittle

unread,
Sep 29, 2002, 5:21:23 PM9/29/02
to
On Sun, 29 Sep 2002 20:29:13 GMT, Steve Watt <st...@nospam.Watt.COM> wrote:
>In article <fGtl9.288028$216.11...@bin4.nnrp.aus1.giganews.com>,
>Mark <msor...@nospam.comcast.net> wrote:
>>I am using Bluecat Linux running on an ARM processor to interface with an
>>FPGA via memory mapping. This is my first experience using Linux as an
>>embedded operating system. I have no idea how to approach a solution.
>>Wasted a lot of time browsing websites with no luck. There must be an easy
>>way to read and write to physical memory. Any suggestions.
>
>mmap().
>
>Open /dev/mem, and call mmap() on the file descriptor you get back with
>the appropriate offset.

And don't forget to set the O_SYNC flag on the open() call, or
else the memory will get cached. Not exactly what you want
when you are register banging.

- Larry

Mark

unread,
Sep 29, 2002, 9:07:35 PM9/29/02
to
So this is what I have:

/*
* mdump.c
*/
#include <sys/types.h>
#include <sys/mman.h>
#include <fcntl.h>
#include <stdlib.h>
#include <stdio.h>

int main (int argc, char **argv) {
int fd;
char* ptr;

fd = open ("/dev/mem", O_RDWR | O_CREAT | O_SYNC, 0);
if (fd < 0) {
printf ("open failed\n");
exit(-1);
}

/* I would like to read addresss 0x80000000 */
(void*)ptr = mmap ((void*)0x80000000, 4, PROT_READ | PROT_WRITE
,MAP_PRIVATE|MAP_FILE, fd, 0);

if (ptr == NULL) {
perror ("mmap failed");
exit(-1);
}

printf ("%c-%c-%c-%c\n", ptr[0], ptr[1], ptr[2], ptr[3]);

return 0;
}

I'm obviously doing something wrong. All I am trying to do is read 4 bytes
from address 0x80000000.
Any ideas?

~ Mark

"Larry Doolittle" <ldoo...@recycle.lbl.gov> wrote in message
news:slrnaperlo....@recycle.lbl.gov...

Bevan Weiss

unread,
Sep 29, 2002, 9:48:59 PM9/29/02
to
Comments inline

"Mark" <msor...@nospam.comcast.net> wrote in message
news:rtNl9.30469$Ii4.1...@bin2.nnrp.aus1.giganews.com...


> So this is what I have:
>
> /*
> * mdump.c
> */
> #include <sys/types.h>
> #include <sys/mman.h>
> #include <fcntl.h>
> #include <stdlib.h>
> #include <stdio.h>
>
> int main (int argc, char **argv) {
> int fd;
> char* ptr;
>
> fd = open ("/dev/mem", O_RDWR | O_CREAT | O_SYNC, 0);
> if (fd < 0) {
> printf ("open failed\n");
> exit(-1);
> }
>
> /* I would like to read addresss 0x80000000 */
> (void*)ptr = mmap ((void*)0x80000000, 4, PROT_READ | PROT_WRITE
> ,MAP_PRIVATE|MAP_FILE, fd, 0);

what's with the void * cast??
Shouldn't you cast the return of mmap to char*?, that would make ptr[0],
ptr[1] valid.
How can you have ptr[0], ptr[1] of a void type? (if doing so even worked)

Steve Watt

unread,
Sep 29, 2002, 10:45:36 PM9/29/02
to
In article <rtNl9.30469$Ii4.1...@bin2.nnrp.aus1.giganews.com>,
Mark <msor...@nospam.comcast.net> wrote:
[ Top post repaired ]

>"Larry Doolittle" <ldoo...@recycle.lbl.gov> wrote in message
>news:slrnaperlo....@recycle.lbl.gov...
>> On Sun, 29 Sep 2002 20:29:13 GMT, Steve Watt <st...@nospam.Watt.COM> wrote:
>> >In article <fGtl9.288028$216.11...@bin4.nnrp.aus1.giganews.com>,
>> >Mark <msor...@nospam.comcast.net> wrote:
>> >>I am using Bluecat Linux running on an ARM processor to interface with an
>> >>FPGA via memory mapping. [ ... ]

>> >> There must be an easy
>> >>way to read and write to physical memory. Any suggestions.
>> >
>> >mmap().
>> >
>> >Open /dev/mem, and call mmap() on the file descriptor you get back with
>> >the appropriate offset.
>>
>> And don't forget to set the O_SYNC flag on the open() call, or
>> else the memory will get cached. Not exactly what you want
>> when you are register banging.

That's a new one to me, but I haven't looked deeply into the Linux memory
management system.

>So this is what I have:

[ ... ]


> /* I would like to read addresss 0x80000000 */
> (void*)ptr = mmap ((void*)0x80000000, 4, PROT_READ | PROT_WRITE
>,MAP_PRIVATE|MAP_FILE, fd, 0);

You want the offset (last argument) to be 0x80000000. The address
argument is for where in your process' virtual address space you want
the memory mapped. Yes, it's more than a little counterintuitive to
have the offset at the end of the parameter list and the length
second, even before the file descriptor. But it's an amazingly
powerful little function.

Michael Schnell

unread,
Sep 30, 2002, 2:42:59 AM9/30/02
to
> I am using Bluecat Linux running on an ARM processor to interface with an
> FPGA via memory mapping.

This should be done in a device driver, not in a user program. You
should read the great standard "Linux device drivers" to find out how to
do that (it's much easier than writing a Windows device driver) and how
to access your driver from your user program. The book is available in
the internet if you don't want to buy it at first.

hope that helps,

Mark

unread,
Sep 30, 2002, 9:58:05 AM9/30/02
to
So the only way to access physical memory space is to write a driver?


Michael Schnell <msch...@lumino.de> wrote in message news:<3D97F273...@lumino.de>...

Larry Doolittle

unread,
Sep 30, 2002, 11:25:48 AM9/30/02
to
On 30 Sep 2002 06:58:05 -0700, Mark <sore...@csciences.com> wrote:
>> > I am using Bluecat Linux running on an ARM processor to interface with an
>> > FPGA via memory mapping.
>>
>> This should be done in a device driver, not in a user program. You
>> should read the great standard "Linux device drivers" to find out how to

>So the only way to access physical memory space is to write a driver?

No. Michael is right, in most cases a driver is the "best" final
result. That doesn't mean it can't be done in user space. And,
a good user space pseudo-driver can easily out-perform a badly
thought out kernel driver.

The one thing user-space code can't do directly is hook interrupts.
I wrote a super-simple kernel driver that works around that issue,
connecting the interrupt from a StrongARM GPIO pin to activity on
a Linux file descriptor, that can be used with select(2).

- Larry

Michael Schnell

unread,
Oct 1, 2002, 4:55:41 AM10/1/02
to
Mark schrieb:

>
> So the only way to access physical memory space is to write a driver?
>

Not the only one but the best one, regarding compatibility and
clearness.

Mark

unread,
Oct 5, 2002, 11:29:27 AM10/5/02
to
I'm obviously new and not very good with this yet. Are there any sources
for code or documentation for reading memory?

~ Mark

"Larry Doolittle" <ldoo...@recycle.lbl.gov> wrote in message

news:slrnapgr70....@recycle.lbl.gov...

Tauno Voipio

unread,
Oct 6, 2002, 1:42:44 PM10/6/02
to

"Mark" <msor...@nospam.comcast.net> wrote in message
news:soucnePOF8o...@News.GigaNews.Com...

> I'm obviously new and not very good with this yet. Are there any sources
> for code or documentation for reading memory?
>
> ~ Mark

Once again: If you're going to read / write things addressable at certain
physical memory locations (usually called 'memory mapped I/O), get the
'Linux Device Drivers' book.

The addresses a program sees are linear addresses, the memory management
hardware in the CPU converts them to physical (hardware) addresses according
to tables set up by the kernel. The book tells how to set up the scenario.

HTH

Tauno Voipio
tauno voipio @ iki fi


0 new messages