mmap程序备份

Copy
#include <sys/mman.h> #include <stdio.h> #include<string.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdint.h> #include <sys/types.h> #include <sys/stat.h> #include <fcntl.h> #include <stdio.h> #include <poll.h> #include <signal.h> #include <sys/types.h> #include <unistd.h> #include <fcntl.h> #include <stdlib.h> #include <string.h> void usage(int argc, char const *argv[]) { printf("%s r/w/b8 addr value/num \n",argv[0]); } typedef union { uint8_t b8[4]; uint16_t b16[2]; uint32_t b32; } commType; // 小端返回TRUE,大端返回FALSE int checkCPUIsLittle() { union w { int a; char b; }c; c.a = 1; return (c.b == 1); } #define MMAP_LEN 1024 int main(int argc, char const *argv[]) { int pin=-1; int val=0; if(argc<3) { usage(argc,argv); return -1; } uint32_t num=1; if (argc == 4) { num = strtoul(argv[3], NULL, 0); } uint32_t addr=strtoul(argv[2],NULL,0); // 地址偏移对其到4字节 uint32_t addr_4=addr&(~0x03); // 计算向前的偏移量 uint32_t addr_offset=addr-addr_4; #define GPIO4 0x20A8000 #define GPIO3 0x20A4000 if (strcmp(argv[1], "g4") == 0) { addr_4=GPIO4; pin=strtoul(argv[2],NULL,0); val=strtoul(argv[3],NULL,0); addr_offset=0; } else if (strcmp(argv[1], "g3") == 0) { addr_4=GPIO3; pin=strtoul(argv[2],NULL,0); val=strtoul(argv[3],NULL,0); addr_offset=0; } char dev_name[] = "/dev/mem"; int fd = open(dev_name,O_RDWR); if(fd<0){ printf("open %s is error\n",dev_name); return -1 ; } unsigned char * basePtr=mmap( 0, MMAP_LEN, PROT_READ | PROT_WRITE, MAP_SHARED,fd, addr_4); if(basePtr == NULL){ printf("basePtr mmap is error\n"); close(fd); return -1; } volatile unsigned char *p8; volatile unsigned short *p16; volatile unsigned int *p32; p8 = basePtr; p16 = (volatile unsigned short *)p8; p32 = (volatile unsigned int *)p8; if (pin != -1) { // 寻找字节 addr_offset=pin/8; //addr_offset 表示的就是第几个字节 commType d; d.b32=*p32; d.b32&=~(1<<pin); if(val==1)d.b32|=(1<<pin); printf("addr=%08x,offset=%08x,pin=%d,val=%08x\n",addr_4,addr_offset,pin,d.b32); *p32=d.b32; return 0; } if (strcmp(argv[1], "b8") == 0) { num=4; uint8_t buf[4]={0}; for(int i=0;i<4;i++) { buf[i]=basePtr[i]; } uint32_t regVal; // 先处理小端的cpu if(checkCPUIsLittle()) { regVal=(uint32_t)buf[0]+((uint32_t)buf[1]<<8)+((uint32_t)buf[2]<<16)+((uint32_t)buf[3]<<24); } else { regVal=(uint32_t)buf[3]+((uint32_t)buf[2]<<8)+((uint32_t)buf[1]<<16)+((uint32_t)buf[0]<<24); } printf("0x%4x \n",regVal); for(int i=0;i<32;i++) { printf("|%2d ",i); } printf("|\n"); for(int i=0;i<32;i++) { printf("|%2d ",((regVal&(1<<i))==0?0:1)); } printf("|\n"); } else if (strcmp(argv[1], "r8") == 0) { printf("AddRess "); for(int i=0;i<16;i++) { if(addr_offset==i) { printf("|*%3x ",i); } else { printf("|%4x ",i); } } printf("|\n"); for(int i=0;i<num+addr_offset;i++) { printf("%08x",addr_4+i); for(int j=0;i<16;j++) { printf("| %02x ",*p8); p8++; i++; } } printf("|\n\n"); } else if (strcmp(argv[1], "r32") == 0) { printf("AddRess "); for(int i=0;i<16;i++) { if(addr_offset==i) { printf("|*%7x ",i); } else { printf("|%8x ",i); } } printf("|\n"); for(int i=0;i<num+addr_offset;i++) { printf("%08x",addr_4+i); for(int j=0;i<16;j++) { printf("|%08x ",*p32); p32++; i++; } } printf("|\n\n"); } if (strcmp(argv[1], "w8") == 0) { //addr_offset 表示的就是第几个字节 commType d; d.b32=*p32; d.b8[addr_offset]=num; *p32=d.b32; } // else if (strcmp(argv[1], "w16") == 0) // { // } else if (strcmp(argv[1], "w32") == 0) { if(addr_offset!=0) { printf("ERROR: please Check addr\n"); } else { *p32=num; } } munmap(basePtr, MMAP_LEN); return 0; }

使用

Copy
./b.out w8 0x20A4002 0x09 #off ./b.out w8 0x20A4002 0x89 #on
posted @   zongzi10010  阅读(102)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
· 【杂谈】分布式事务——高大上的无用知识?
历史上的今天:
2017-10-22 vpn bootload学习
点击右上角即可分享
微信分享提示