Chinaunix

标题: 用户空间驱动的mmap映射失败 [打印本页]

作者: zhutousame    时间: 2011-10-29 12:17
标题: 用户空间驱动的mmap映射失败
用户空间驱动的mmap映射失败, 本想在mini2440中使用用户空间驱动, 直接操作led灯, 但是mmap失败了,代码如下
  1. #include <stdio.h>
  2. #include <sys/types.h>
  3. #include <sys/stat.h>
  4. #include <fcntl.h>
  5. #include <unistd.h>
  6. #include <sys/mman.h>
  7. volatile unsigned int *map_base=NULL;  
  8. volatile unsigned int *GpbCon =NULL;
  9. volatile unsigned int *GpbDat =NULL;
  10. int main()
  11. {
  12.         int fd,count=0;
  13.         fd = open("/dev/mem",0,O_RDWR|O_SYNC);
  14.         if(fd == -1 ){
  15.                 perror("open failed ");
  16.                 return -1;
  17.         }
  18.         map_base = (volatile unsigned int *)mmap(NULL,0x9,PROT_READ|PORT_WRITE,MAP_SHARED,fd,0x56000010);
  19.         //  0x56000010 这个是2440的 GPBCON的物理地址
  20.         if(map_base==NULL || map_base ==MAP_FAILED){
  21.                 perror("sorry , can't map\n");
  22.                 if(map_base ==MAP_FAILED)
  23.                         printf("sorry ,MAP_FAILED\n");
  24.                 close(fd);
  25.                 return -1;
  26.         }else printf("map secessfully \n");
  27.         GpbCon = (volatile unsigned int *)(map_base);
  28.         GpbDat = (volatile unsigned int *)(map_base + 0x4);  // GPB
  29.         *GpbCon &= ~(0xff<<10)|0x2<<5*2|0x2<<6*2|0x2<<7*2|0x2<<8*2;  
  30.         *GpbDat = 0x0;
  31.         while(1){
  32.                 *GpbDat = ~*GpbDat;
  33.                 sleep(1000);
  34.                 if(count++>5){
  35.                         break;
  36.                 }
  37.         }
  38.         munmap((void*)map_base,0x9);
  39.         close(fd);
  40.    
  41.         return 0;
  42. }
复制代码
放到mini2440中报错:
sorry , can't map
Invalid argument
sorry ,MAP_FAILED

什么原因呢?
作者: adiosET    时间: 2011-10-30 14:27
应该是不能直接映射物理地址吧
具体不记得了,你可以看看mem.c中mmap实现源码,需要先分配页表映射这段地址的吧




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2