Chinaunix首页 | 论坛 | 博客
  • 博客访问: 4471759
  • 博文数量: 356
  • 博客积分: 10458
  • 博客等级: 上将
  • 技术积分: 4734
  • 用 户 组: 普通用户
  • 注册时间: 2008-03-24 14:59
文章分类

全部博文(356)

文章存档

2020年(17)

2019年(9)

2018年(26)

2017年(5)

2016年(11)

2015年(20)

2014年(2)

2013年(17)

2012年(15)

2011年(4)

2010年(7)

2009年(14)

2008年(209)

分类: LINUX

2015-11-24 20:34:29

从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
阅读(7041) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~