#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <signal.h>
#include <fcntl.h>
#include <ctype.h>
#include <termios.h>
#include <sys/types.h>
#include <sys/mman.h>
#include <sys/stat.h>
#define FATAL do { fprintf(stderr, "Error at line %d, file %s (%d) [%s]\n", \
__LINE__, __FILE__, errno, strerror(errno)); exit(1); } while(0)
#define MAP_SIZE 4096UL
#define MAP_MASK (MAP_SIZE - 1)
int reg_dump(unsigned long addr) {
int fd;
void *map_base, *virt_addr;
unsigned long read_result;
off_t target;
int access_type = 'w';
target = addr;
if((fd = open("/dev/mem", O_RDWR | O_SYNC)) == -1) FATAL;
//printf("\n") ;
/* Map one page */ //将内核空间映射到用户空间
map_base = mmap(0, MAP_SIZE, PROT_READ | PROT_WRITE, MAP_SHARED, fd, target & ~MAP_MASK);
if(map_base == (void *) -1) FATAL;
virt_addr = map_base + (target & MAP_MASK);
//针对不同的参数获取不同类型内存数据
read_result = *((unsigned long *) virt_addr);
if(munmap(map_base, MAP_SIZE) == -1) FATAL;
close(fd);
return read_result;
}
int main(){
int i;
//unsigned long base_addr = 0xefe10030000;
unsigned long base_addr = 0xefe00008800;
//unsigned long base_addr = 0x48C98000;
unsigned long read_val;
//create_daemon();
//while(1){
FILE* fd = fopen("/home/lwh/pcie_bar_4kspace","a");
//FILE* fd = fopen("/home/lwh/pcie_ctl_log","a");
for(i = 0; i < 4096;i = i+4){
read_val = reg_dump(base_addr+i);
/*
if(i <256)
read_val = reg_dump(base_addr+i);
else if(i>=256)
read_val = reg_dump(base_addr | ((i >> 8 )& 0xf) << 24 | i & 0xff);
*/
printf("Address %x now read reg is %08x\n",base_addr+i,read_val);
// printf(" %03x : %08x\n",i,read_val);
// fprintf(fd,"Address %x now read reg is %08x\n",base_addr+i,read_val);
}
fprintf(fd,"**************************************************************\n");
fclose(fd);
// sleep(20);
//}
}
龙芯mips平台打印7a桥片上pcie设备的配置空间的代码
于 2023-03-07 10:53:17 首次发布