TIA,
Mark
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...
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
/*
* 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...
"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)
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.
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,
Michael Schnell <msch...@lumino.de> wrote in message news:<3D97F273...@lumino.de>...
>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
Not the only one but the best one, regarding compatibility and
clearness.
~ Mark
"Larry Doolittle" <ldoo...@recycle.lbl.gov> wrote in message
news:slrnapgr70....@recycle.lbl.gov...
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