mmap程序备份

#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 
",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
",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
");
        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
",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 
",regVal); 
        for(int i=0;i<32;i++)
        {
            printf("|%2d ",i);
        }
        printf("|
");
        for(int i=0;i<32;i++)
        {
            printf("|%2d ",((regVal&(1<<i))==0?0:1)); 
        }
        printf("|
");
    }
    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("|
");
        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("|

");
    }
    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("|
");
        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("|

");

    }



    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
");
        }
        else
        {
           *p32=num;
        }
    }  

 





    munmap(basePtr, MMAP_LEN);

    return 0;
}



使用

./b.out  w8 0x20A4002 0x09  #off
./b.out  w8 0x20A4002 0x89  #on
原文地址:https://www.cnblogs.com/zongzi10010/p/13858119.html