Memory Maped I/O

Memory Maped I/O

Post by John Davie » Tue, 16 May 1995 04:00:00



I'm trying to get a A/D board working on a 486 using the mmap()
        to map the board's control registers (0x700-0x70f) into my address
        space.

        Has anyone got a working example of some memory-mapped IO code
        that they could email/post?

Much appreciated.
--
        -John M. Davies

 
 
 

Memory Maped I/O

Post by rob.. » Fri, 19 May 1995 04:00:00


: I'm trying to get a A/D board working on a 486 using the mmap()
:       to map the board's control registers (0x700-0x70f) into my address
:       space.

:       Has anyone got a working example of some memory-mapped IO code
:       that they could email/post?

: Much appreciated.
: --
:       -John M. Davies

Me too. I am looking for the same information John.

Help!

Paul Lamar

 
 
 

Memory Maped I/O

Post by rob.. » Sat, 20 May 1995 04:00:00


John, I am thinking of running Linux but I have to be sure I can access
memory directly. Since I don't have it running yet would you upload the
man pages on mmap here?

Paul Lamar

 
 
 

Memory Maped I/O

Post by S. Joel Ka » Sat, 20 May 1995 04:00:00




>: I'm trying to get a A/D board working on a 486 using the mmap()
>:   to map the board's control registers (0x700-0x70f) into my address
>:   space.
>:   Has anyone got a working example of some memory-mapped IO code
>:   that they could email/post?
>: Much appreciated.
>: --
>:   -John M. Davies
>Me too. I am looking for the same information John.

        Any special reason not to use inb and outb or inw and outw. Using
memory mapped IO can be dangerous (many boards care about word versus
byte IO and you have no control if you mmap it).

--
Would you like to make money reselling heavily discounted long-distance
service? Earn years of commissions on the sales you make today. Finger

 
 
 

Memory Maped I/O

Post by rob.. » Wed, 24 May 1995 04:00:00





: >: I'm trying to get a A/D board working on a 486 using the mmap()
: >:         to map the board's control registers (0x700-0x70f) into my address
: >:         space.

: >:         Has anyone got a working example of some memory-mapped IO code
: >:         that they could email/post?

: >: Much appreciated.
: >: --
: >:         -John M. Davies

: >Me too. I am looking for the same information John.

:       Any special reason not to use inb and outb or inw and outw. Using
: memory mapped IO can be dangerous (many boards care about word versus
: byte IO and you have no control if you mmap it).

Ok the following program will mmap /dev/mem and read the bios from
0xf0000 to 0xfffff and write it to a file.  It is REAL slow due to
writing one byte at a time using unbuffered IO.

/************************************************************************/
/*Example Program for mmap                                              */
/*                                                                      */
/*Mmaps /dev/mem and read BIOS from memory and writes to a file         */
/************************************************************************/

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

int main() {
        caddr_t ptr;            /*Generic memory pointer*/
        caddr_t base;           /*Base Address in memory of mmaped file*/
        size_t length, rtn;             /*Length of file to be mapped*/
        off_t offset;           /*Offset of file*/
        int file, output;       /*File descriptor*/
        char *filename = "/dev/mem";
        char *savefile = "bios.bin";

        base = 0;               /*Let the system determine base address*/
        offset = 0xf0000;       /*Bios begins at 0xf0000*/
        length = 0xffff;        /*BIOS is 64k long*/

        /*Must open /dev/mem before we can mmap it*/
        if( (file = open(filename, O_RDONLY)) < 0 ) {
                printf("Can't open %s\n", filename);
                exit(-1);
        }
        if( (base = mmap(base, length, PROT_READ, MAP_SHARED, file,
                        offset)) == (caddr_t) -1 ) {
                printf("Can't mmap file\n");
                exit(-2);
        }

        /*Ok we now have a hunk of memory starting a 'base' that is
'length' */
        /*bytes long.  This equates to a section of physical memory beginning*/
        /*at 'offset' and is 'length' bytes
long                             */

        /*Open bios save file*/
        if( (output = open(savefile, O_RDWR | O_CREAT | O_TRUNC)) < 0) {
                printf("Can't open %s\n", savefile);
                exit(-1);
        }

        /*Loop through memory region and write to disk*/

        for(ptr=base; ptr< (base+length); ptr ++) {
                if( (rtn = write( output, ptr, 1)) != 1)
                        printf("Write Err on %s\n", savefile);
        }
        return 0;

Quote:}

Pretty straight forward.  If the A/D board has 8 bytes mapped to a location
starting at 0xd0000 change length to 0x8 and offset to 0xd0000.
If the 8 bytes have structure as this

    bytes           What they are
----------------------------------------
     0-1            16 bit value
       2             8 bit value
       3             8 bit value
     4-7            32 bit value

you can do this:

struct adboard {
        short value1;
        char value2;
        char value3;
        logn value4;

Quote:} *read;

read = mmap(parameters, type, stuff);

Now whenever you:
        printf("Value 2 is: %c\n", read->value2);

you'll read whatever is current at the physical memory location 0xd0002.
Neat eh?

If you have any further questions just ask.

--
 /------------------------------+--------------------------------------\
| Carlo J. Calica               | Linux:  Choice of the GNU Generation  |

 \ University of Wisconsin      | Republic of Madison                  /

 
 
 

1. Shared Memory,Memory mapped files and Threads

1. If thread 1 in a multithreaded application attaches to a shared memory
segment is it possible to guarantee that thread2 will not overwrite this shared
memoey segment?

2. Same question as it relates to memory-mapped file.

3. Can device drivers be used to achieve thread level insulation from shared
memory or memory mapped files?

4. What are the advantages of using memory-mapped files vs. shared memory?
(Issues such as system limits, process limits, size, performance, etc.)

5. Can you direct me to relevant material on the above mentioned topics (books,
web-sites, etc.)

Thank you.

Sincerely,

Sanjay Shrivastava.

2. linux printing; margins

3. PCI memory mapped card : memory gets overwritten

4. Is Soundblaster-SCSI *really* supported?

5. Memory mapping SVGA video memory on Solaris 2.1 x86??

6. ARRGH! Need help getting linux installed!!!

7. Shared Memory v. Mapped Memory

8. ATI Graphics

9. Q: How to control usage of memory for memory-mapped I/O?

10. Shared memory, memory mapped files and threads

11. Conflict with shared memory and memory map

12. Shared Memory vs. Memory Mapped