mmap程序备份
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
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<
使用
./b.out w8 0x20A4002 0x09 #off
./b.out w8 0x20A4002 0x89 #on