龙芯mips平台打印7a桥片上pcie设备的配置空间的代码

#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);
	//}	
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值