linux 物理内存读写

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

main()
{
    int   i, j;
    int   words = 10;
    int   devmem;
    off_t PageOffset, PageAddress;

    unsigned long *hw_addr = (unsigned long *) 0x20000000;
    unsigned long  read_data;

    devmem = open("/dev/mem", O_RDWR | O_SYNC);
    PageOffset = (off_t) hw_addr % getpagesize();
    PageAddress = (off_t) (hw_addr - PageOffset);

    hw_addr = (unsigned long *) mmap(0, 4, PROT_READ|PROT_WRITE, MAP_SHARED, devmem, PageAddress);

    for (i = 0, j = 0; i < words; i++, j+=4)
    {
        printf("Writing to hardware model\n");

        *(hw_addr+j) = 0xabcd0000+j;

        read_data = *(hw_addr+j);

        printf("Read from hardware %x\n",read_data);

    }

}




你可能感兴趣的:(linux 物理内存读写)