linux板子用户态通过/dev/mem访问物理内存(寄存器,GPIO)

从datasheet中查到
IO口寄存器基地址Base Address:0x01C20800
PE口的寄存器PE Data Register 偏移offset: 0xA0,那么PE_DAT地址就是0x01C208A0
现在要访问PE4的值,PE4就是0x01C208A4
程序如下:

点击(此处)折叠或打开

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <sys/types.h>
  5. #include <sys/stat.h>
  6. #include <fcntl.h>
  7. #include <sys/mman.h>

  8. int main ()
  9. {
  10.     int mem_fd = open( "/dev/mem", O_RDONLY|O_SYNC );
  11.     if (mem_fd == -1)
  12.     {
  13.         perror("open /dev/mem");
  14.         return -1;
  15.     }
  16.     int size = 0x800 + 0xa4;

  17.     int *addr = (int*)mmap( 0, size, PROT_READ, MAP_SHARED, mem_fd, 0x01C20000); //最后参数必须与页大小对齐,所以没有直接指定gpio地址,而是先映射后再通过偏移访问
  18.     close(mem_fd);
  19.     if (addr == MAP_FAILED)
  20.     {
  21.         perror("mmap");
  22.         return -1;
  23.     }
  24.     printf( "0x%x\n", *(addr+ (size-4)/4) );
  25.     munmap(addr, size);
  26.     return 0;
  27. }
作者:帅得不敢出门   程序员群:31843264
<script>window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdMiniList":false,"bdPic":"","bdStyle":"0","bdSize":"16"},"share":{}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];</script>
阅读(2834) | 评论(0) | 转发(0) |
0

上一篇:fatal error: linux/compiler-gcc5.h: 没有那个文件或目录

下一篇:linux下mp3批量转wav与spx

给主人留下些什么吧!~~
评论热议

猜你喜欢

转载自blog.csdn.net/zmlovelx/article/details/80263005