zoukankan      html  css  js  c++  java
  • Linux在应用层读写寄存器的方法

    可以通过操作/dev/mem设备文件,以及mmap函数,将寄存器的地址映射到用户空间,直接在应用层对寄存器进行操作,示例如下:

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. #include <stdio.h>  
    2. #include <stdlib.h>  
    3. #include <time.h>  
    4. #include <unistd.h>  
    5. #include <fcntl.h>  
    6. #include <unistd.h>   
    7. #include <sys/mman.h>  
    8. #define AUDIO_REG_BASE   (0xB800E000)  
    9. #define MAP_SIZE        0xFF  
    10.   
    11. static int dev_fd;  
    12. int main(int argc, char **argv)  
    13. {  
    14.   
    15.     dev_fd = open("/dev/mem", O_RDWR | O_NDELAY);        
    16.   
    17.     if (dev_fd < 0)    
    18.     {  
    19.         printf("open(/dev/mem) failed.");      
    20.         return 0;  
    21.     }    
    22.   
    23.     unsigned char *map_base=(unsigned char * )mmap(NULL, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, dev_fd, AUDIO_REG_BASE );  
    24.   
    25.     printf("%x  ", *(volatile unsigned int *)(map_base+0x38)); //打印该寄存器地址的value  
    26.   
    27. #if 1 // LINE IN  
    28.     printf("%x  ", *(volatile unsigned int *)(map_base+0x30));  
    29.   
    30.     *(volatile unsigned int *)(map_base + 0x30) = 0x208121bc; //修改该寄存器地址的value  
    31.     usleep(1000000);  
    32.     *(volatile unsigned int *)(map_base + 0x30) &= ~(0x1<<16); //修改该寄存器地址的value  
    33.     usleep(1000000);  
    34.   
    35.     printf("%x  ", *(volatile unsigned int *)(map_base+0x30));  
    36. #endif  
    37.   
    38.     if(dev_fd)  
    39.         close(dev_fd);  
    40.   
    41.     munmap(map_base,MAP_SIZE);//解除映射关系  
    42.   
    43.     return 0;  
    44. }  
  • 相关阅读:
    navigator
    windows事件
    js 数组
    类,屏蔽鼠标右键
    document.links[i].onclick;展示表单的输入
    手机端取消文字选中、取消图片长按下载
    ios显示一个下载banner
    js时间Date对象介绍及解决getTime转换为8点的问题
    iphone的click导致div变黑
    如何给外部引用的js文件传递参数
  • 原文地址:https://www.cnblogs.com/lidabo/p/6404183.html
Copyright © 2011-2022 走看看