打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
使用 /dev/mem
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)

int main(int argc, char **argv) {
    int fd;
    void *map_base, *virt_addr;
    off_t target = 0xB7A01084;

    if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {
        printf("/dev/mem could not be opened.\n");
        exit(1);
    } else {
        printf("/dev/mem opened.\n");
    }
    fflush(stdout);

    /* Map one page */
    map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target + & ~MAP_MASK);
    if(map_base == (void *) -1) {
        printf("Memory map failed.\n");
    } else {
        printf("Memory mapped at address %p.\n", map_base);
    }
    fflush(stdout);

    virt_addr = map_base + (target & MAP_MASK);

    /* acess remapped region here */

    if(munmap(map_base, MAP_SIZE) == -1) {
        printf("Memory unmap failed.\n");    
    }

    close(fd);
}

Once a mapping has been achieved the registers may be accessed as required, to ensure the correct type of access is performed by the CPU the void * pointer must be cast to the correct type, for instance in the above the following could be used

    /* for a byte wide access */
    read_result = *((volatile unsigned char *) virt_addr);
    /* for a sixteen bit access */
    read_result = *((volatile unsigned short *) virt_addr);
    /* for a thirty two bit access */
    read_result = *((volatile unsigned long *) virt_addr);

使用 /dev/port 设备文件控制 LED 闪动。
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
linux内存映射相关知识点
mmap把文件映射到内存
Linux内核DMA机制 - ShangShuWu
Access kernel space from user space by using mmap
linux下使用mmap控制GPIO
devmem读写物理内存和devkmem读取内核虚拟内存
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服