码迷,mamicode.com
首页 > 其他好文 > 详细

mmap直接控制底层【转】

时间:2016-07-21 14:50:16      阅读:405      评论:0      收藏:0      [点我收藏+]

标签:

这是在mini6410上测试成功的,在没有驱动的情况下用程序直接控制了led灯test_mmap.c:

 

[cpp] view plain copy
 
  1. /* Example how to access the value of the on-board DIP switches on 
  2.  * HiCO.SH7760. You can compile the program with command: 
  3.  * 
  4.  *   sh4-linux-gcc -Wall dip_switch.c -o dip_switch 
  5.  */  
  6. #include <stdio.h>  
  7. #include <stdlib.h>  
  8. #include <assert.h>  
  9. #include <unistd.h>  
  10. #include <errno.h>  
  11. #include <fcntl.h>  
  12. #include <sys/mman.h>  
  13.    
  14. #define MAP_SIZE 4096UL  
  15. #define MAP_MASK (MAP_SIZE - 1)  
  16.    
  17.    
  18. int main(void) {  
  19.     int fd;  
  20.     void *map_base, *virt_addr;  
  21.    
  22.     /* Physical address of the DIP switch GPIO register on HiCO.SH7760 (See 
  23.      * the HiCO.SH7760 hardware manual  
  24.      * 
  25.      * _NOTE_: the dipswitches also define how the system boots (i.e. the value 
  26.      * is used by the bootloader at startup), so oafter testting, leave the 
  27.      * switches in the same position as they were */  
  28.     off_t target = 0x7F008800;  //GPKCON0 0x7F008800   
  29.    
  30.     if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) {  
  31.         printf("/dev/mem could not be opened.\n");  
  32.     perror("open");  
  33.         exit(1);  
  34.     } else {  
  35.         printf("/dev/mem opened.\n");  
  36.     }  
  37.    
  38.     /* Map one page */  
  39.     map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);  
  40.     if(map_base == (void *) -1) {  
  41.         printf("Memory map failed.\n");  
  42.     perror("mmap");  
  43.     } else {  
  44.         printf("Memory mapped at address %p.\n", map_base);  
  45.     }  
  46.    
  47.     virt_addr = map_base + (target & MAP_MASK);  
  48.    
  49.     /* acess remapped region here */  
  50.    
  51.     printf("Now try to change the value of the on-board DIP switche\n");  
  52.     *(unsigned int *)virt_addr=0x11110000;//0x1111<<16;//设定为输出状态  
  53.     int ab=*(unsigned int *)virt_addr;  
  54.     //*(unsigned int *)(virt_addr+8)=0xffffffff;  
  55.     printf("ab=%x\n",ab);  
  56.       
  57. #if 1  
  58.     while(1){  
  59.     *(volatile unsigned int *)(virt_addr+8)=0x00;   //一定要是virt_addr不能是map_base//置0亮  
  60.     printf("1value is 0x%x\n",*(unsigned int *)(virt_addr+8));  
  61.     printf("2value is 0x%x\n",*(unsigned int *)virt_addr);  
  62.     sleep(1);   //一定要有要不很快,人眼发现不了  
  63.     *(volatile unsigned int *)(virt_addr+8)=0xf0;                  //置1灭  
  64.     printf("3value is 0x%x\n",*(unsigned int *)(virt_addr+8));  
  65.     printf("4value is 0x%x\n",*(unsigned int *)virt_addr);  
  66.     sleep(1);   //一定要有要不很快,人眼发现不了  
  67.     }  
  68.  #endif  
  69.     /* we‘ll never get here in this example, but here‘s how the region is  
  70.      * unmapped. */  
  71.     if(munmap(map_base, MAP_SIZE) == -1) {  
  72.         printf("Memory unmap failed.\n");     
  73.     }  
  74.    
  75.     close(fd);  
  76. }  

 

 

以上程序证明了在linux下用mmap函数可以在没有驱动的情况下也可以直接控制底层。

网上说只能控制gpio设备,我还可以直接控制其它设备,有待验证。

mmap直接控制底层【转】

标签:

原文地址:http://www.cnblogs.com/sky-heaven/p/5691572.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!