• mmap直接控制底层【转】


    这是在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. ");  
    32.     perror("open");  
    33.         exit(1);  
    34.     } else {  
    35.         printf("/dev/mem opened. ");  
    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. ");  
    42.     perror("mmap");  
    43.     } else {  
    44.         printf("Memory mapped at address %p. ", 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 ");  
    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 ",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 ",*(unsigned int *)(virt_addr+8));  
    61.     printf("2value is 0x%x ",*(unsigned int *)virt_addr);  
    62.     sleep(1);   //一定要有要不很快,人眼发现不了  
    63.     *(volatile unsigned int *)(virt_addr+8)=0xf0;                  //置1灭  
    64.     printf("3value is 0x%x ",*(unsigned int *)(virt_addr+8));  
    65.     printf("4value is 0x%x ",*(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. ");     
    73.     }  
    74.    
    75.     close(fd);  
    76. }  

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

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

  • 相关阅读:
    hibernate4 无法保存 不报错
    win10开启mongodb 开启服务
    nodejs学习笔记
    mariadb Too many connections
    新老ECS数据库速度对比
    数据库自动备份并打成tar.gz包
    字符串和数组----string
    I/O复用(select)——回声服务器端/客户端
    回声UDP服务器端/客户端
    回声TCP服务器端/客户端
  • 原文地址:https://www.cnblogs.com/sky-heaven/p/5691572.html
Copyright © 2020-2023  润新知