XILINX的PCIE实现分析:LINUX下PCIE对应文件的介绍和使用

66 篇文章 23 订阅
28 篇文章 11 订阅

前几天实验了一下XILINX官方的提供的在UBUNTU平台下的PCIE驱动,以下文字做个简单记录和总结。时间有限简单写写,可能以后有补充。

1,在LINUX下安装驱动后在/dev/目录里面多了十多个xdma0_开头的设备。

设备一:xdma0_c2h_0设备:xdma0_c2h_0 是一个只读设备,用open函数打开后直接调用read函数进行读,如果FPGA段接的不是AXI STREAM,而是带有地址的AXI 接口,可以调用seek函数进行起始地址的指定。操作完毕后关闭文件用close函数。以下代码是我做的测试速度的代码,展示了xdma0_c2h_0这个设备的使用。

/*
 * This file is part of the Xilinx DMA IP Core driver tool for Linux
 *
 * Copyright (c) 2016-present,  Xilinx, Inc.
 * All rights reserved.
 *
 * This source code is licensed under BSD-style license (found in the
 * LICENSE file in the root directory of this source tree)
 */
//gcc h2c.c -lrt -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -D_LARGE_FILE_SOURCE 

#define _BSD_SOURCE
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
 
 
/* Subtract timespec t2 from t1
 *
 * Both t1 and t2 must already be normalized
 * i.e. 0 <= nsec < 1000000000
 */
static int timespec_check(struct timespec *t)
{
	if ((t->tv_nsec < 0) || (t->tv_nsec >= 1000000000))
		return -1;
	return 0;

}

void timespec_sub(struct timespec *t1, struct timespec *t2)
{
	if (timespec_check(t1) < 0) {
		fprintf(stderr, "invalid time #1: %lld.%.9ld.\n",
			(long long)t1->tv_sec, t1->tv_nsec);
		return;
	}
	if (timespec_check(t2) < 0) {
		fprintf(stderr, "invalid time #2: %lld.%.9ld.\n",
			(long long)t2->tv_sec, t2->tv_nsec);
		return;
	}
	t1->tv_sec -= t2->tv_sec;
	t1->tv_nsec -= t2->tv_nsec;
	if (t1->tv_nsec >= 1000000000) {
		t1->tv_sec++;
		t1->tv_nsec -= 1000000000;
	} else if (t1->tv_nsec < 0) {
		t1->tv_sec--;
		t1->tv_nsec += 1000000000;
	}
}
 
int fpga_fd_c2h ;

int test_h2c(){

struct timespec ts_start, ts_end;
long long  i;
char *buffer = NULL;
char *allocated = NULL;

long long  size =  1024 * 1024 * 1  ;
long long  loop =  1024 * 40 ;

//size *=loop ;loop=1;

double  ns,s,rate;
double total_time  = 0 ;  
///fpga_fd_c2h= open( "/dev/xdma0_h2c_0", O_RDWR );
fpga_fd_c2h = open( "/dev/xdma0_c2h_0", O_RDWR | O_NONBLOCK);
if ( fpga_fd_c2h < 0 ) {printf("can not open device %s\n");return (1);}
posix_memalign((void **)&allocated, 4096 , size + 4096);
if (allocated==NULL ){ printf ( "can not alloc memor %d Bytes \n" , size ) ; return (1); }
printf("start read \n");
clock_gettime(CLOCK_MONOTONIC, &ts_start);
//lseek(fpga_fd_c2h ,0,SEEK_SET);
for(i=0;i<loop;++i) read(fpga_fd_c2h,allocated,size );
clock_gettime(CLOCK_MONOTONIC, &ts_end);
timespec_sub(&ts_end, &ts_start);
ns = ts_end.tv_nsec;
s = ts_end.tv_sec;
total_time += (double)(1000*1000*1000*s) + (double) ns ;
printf("%f GBytes, %f s \n",(double)((size * loop )/(1024*1024*1024)), (double)(total_time/(1000*1000*1000)) );

rate = (double)((size * loop )/(1024*1024*1024))  / (double)(total_time/(1000*1000*1000)) ;
printf("Rate is %2.3f G Byte/second \n",rate);
close (fpga_fd_c2h) ;
}

main (){

test_h2c();

}

设备二:xdma0_h2c_0设备:xdma0_h2c_0 是一个只写设备,用open函数打开后直接调用write函数进行写,如果FPGA段接的不是AXI STREAM,而是带有地址的AXI 接口,可以调用seek函数进行起始地址的指定。操作完毕后关闭文件用close函数。以下代码是我做的测试速度的代码,展示了xdma0_h2c_0这个设备的使用。以下代码是我做的测试速度的代码,展示了xdma0_c2h_0这个设备的使用。

/*
 * This file is part of the Xilinx DMA IP Core driver tool for Linux
 *
 * Copyright (c) 2016-present,  Xilinx, Inc.
 * All rights reserved.
 *
 * This source code is licensed under BSD-style license (found in the
 * LICENSE file in the root directory of this source tree)
 */
//gcc h2c.c -lrt -D_FILE_OFFSET_BITS=64 -D_GNU_SOURCE -D_LARGE_FILE_SOURCE 

#define _BSD_SOURCE
#define _XOPEN_SOURCE 500
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
 
 
/* Subtract timespec t2 from t1
 *
 * Both t1 and t2 must already be normalized
 * i.e. 0 <= nsec < 1000000000
 */
static int timespec_check(struct timespec *t)
{
	if ((t->tv_nsec < 0) || (t->tv_nsec >= 1000000000))
		return -1;
	return 0;

}

void timespec_sub(struct timespec *t1, struct timespec *t2)
{
	if (timespec_check(t1) < 0) {
		fprintf(stderr, "invalid time #1: %lld.%.9ld.\n",
			(long long)t1->tv_sec, t1->tv_nsec);
		return;
	}
	if (timespec_check(t2) < 0) {
		fprintf(stderr, "invalid time #2: %lld.%.9ld.\n",
			(long long)t2->tv_sec, t2->tv_nsec);
		return;
	}
	t1->tv_sec -= t2->tv_sec;
	t1->tv_nsec -= t2->tv_nsec;
	if (t1->tv_nsec >= 1000000000) {
		t1->tv_sec++;
		t1->tv_nsec -= 1000000000;
	} else if (t1->tv_nsec < 0) {
		t1->tv_sec--;
		t1->tv_nsec += 1000000000;
	}
}
 
int fpga_fd_h2c ;

int test_h2c(){

struct timespec ts_start, ts_end;
long long  i;
char *buffer = NULL;
char *allocated = NULL;

long long  size =  1024 * 1024 * 1  ;
long long  loop =  1024 * 40 ;

//size *=loop ;loop=1;

double  ns,s,rate;
double total_time  = 0 ;  
fpga_fd_h2c = open( "/dev/xdma0_h2c_0", O_RDWR );
//fpga_fd_h2c = open( "/dev/xdma0_c2h_0", O_RDWR | O_NONBLOCK);
if ( fpga_fd_h2c < 0 ) {printf("can not open device %s\n");return (1);}
posix_memalign((void **)&allocated, 4096 , size + 4096);
if (allocated==NULL ){ printf ( "can not alloc memor %d Bytes \n" , size ) ; return (1); }
printf("start write \n");
clock_gettime(CLOCK_MONOTONIC, &ts_start);
//lseek(fpga_fd_c2h ,0,SEEK_SET);
for(i=0;i<loop;++i) write(fpga_fd_h2c,allocated,size );
clock_gettime(CLOCK_MONOTONIC, &ts_end);
timespec_sub(&ts_end, &ts_start);
ns = ts_end.tv_nsec;
s = ts_end.tv_sec;
total_time += (double)(1000*1000*1000*s) + (double) ns ;
printf("%f GBytes, %f s \n",(double)((size * loop )/(1024*1024*1024)), (double)(total_time/(1000*1000*1000)) );

rate = (double)((size * loop )/(1024*1024*1024))  / (double)(total_time/(1000*1000*1000)) ;
printf("Rate is %2.3f G Byte/second \n",rate);
close (fpga_fd_h2c) ;
}

main (){

test_h2c();

}

设备三:xdma0_user设备:这个是可以读写挂在AXI SLAVE 的IP上的寄存器。上两个设备对应的是数据流。这个是对应用户外设寄存器。

#ifdef __cplusplus
 extern "C" {
 #endif
#include <assert.h>
#include <fcntl.h>
#include <getopt.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <time.h>
#include <pthread.h>
#include <semaphore.h>
#include <stdarg.h>
#include <syslog.h>
#include <sys/mman.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/sysinfo.h>
#include <unistd.h>
#include <dirent.h>
#include <string.h>
/* ltoh: little to host */
/* htol: little to host */
#if __BYTE_ORDER == __LITTLE_ENDIAN
#  define ltohl(x)       (x)
#  define ltohs(x)       (x)
#  define htoll(x)       (x)
#  define htols(x)       (x)
#elif __BYTE_ORDER == __BIG_ENDIAN
#  define ltohl(x)     __bswap_32(x)
#  define ltohs(x)     __bswap_16(x)
#  define htoll(x)     __bswap_32(x)
#  define htols(x)     __bswap_16(x)
#endif

#define MAP_SIZE (1024*1024UL)
#define MAP_MASK (MAP_SIZE - 1)

#define LED_BASE 0x30000
#define BTN_BASE 0x40000

void *control_base;

int control_fd;

static int open_control(char *filename)
{
    int fd;
    fd = open(filename, O_RDWR | O_SYNC);
    if(fd == -1)
    {
        printf("open control error\n");
        return -1;
    }
    return fd;
}
static void *mmap_control(int fd,long mapsize)
{
    void *vir_addr;
    vir_addr = mmap(0, mapsize, PROT_READ | PROT_WRITE, MAP_SHARED, fd, 0);
    return vir_addr;
}
void write_control(int offset,uint32_t val)
{
    uint32_t writeval = htoll(val);
    *((uint32_t *)(control_base+offset)) = writeval;
}
uint32_t read_control(int offset)
{
    uint32_t read_result = *((uint32_t *)(control_base+offset));
    read_result = ltohl(read_result);
    return read_result;
}


void write_io(uint32_t val)
{
   write_control(LED_BASE,val);

}

uint32_t read_io(void)
{
   return read_control(BTN_BASE);
}


int pcie_init()
{
    control_fd = open_control("/dev/xdma0_user");
    if(control_fd < 0)
        return -5;
    control_base = mmap_control(control_fd,MAP_SIZE);
    return 1;
}
void pcie_deinit()
{
    close(control_fd);
}


#ifdef __cplusplus
}
#endif

其实就是先用open函数打开(关闭对用使用close)获得设备fd,之后使用mmap函数,将fd映射成虚拟基地址,之后对寄存器的操作就是基于这个虚拟的起始地址加上偏移量后用指针进行操作读写。这其实和我们在zynq上跑的linux使用mmap函数映射寄存器有点相似,只是那里传给mmap的的是设备的物理地址,这里传给mmap的是文件的句柄(暂且使用句柄一词)。其中htoll是将数据调整成小字端,在X86,AMD64,以及ARM平台都是小字端,因此这个宏可以省掉。

设备四:xdma0_cntrol设备:这是可以读写Xilinx的pcie那个IP的寄存器的函数。和xdma0_user类似的操作。这里不放代码。

另外还有16个event设备,这里我们暂时不做研究。

我用下图来来直观展示这四个文件对应的操作内容:

 

{{aAxvOXMOIvVUoXMxvoxiowMwWV8xxWTxoxOIOVIUUOvwVOUiIoUvvTMMVMwovWHWX8vOUOVU8wMTWWoOXwTMVTwHmHo8XMmMOXIXMvIwmixUIiUxiOMoiHIoVU8VvmvIWXTvvOvv8xvMovOWMOTxUvMT8UmooOZz}}
  • 4
    点赞
  • 45
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
### 回答1: Linux系统是一个开源的操作系统,在嵌入式系统开发中使用非常广泛。而Xilinx PCIe开发则是一种基于Xilinx FPGA芯片的高速I/O接口开发。在Linux上开发Xilinx PCIe程序需要掌握一定的开发技巧和知识。 首先,需要熟悉Linux驱动程序的开发,并且具备一定的硬件设计知识,这样才能够理解Xilinx PCIe接口的使用和应用。其次,需要学习相关的Xilinx开发工具,比如Vivado设计套件和PCIe IP的使用。在此基础上,可以依据需求设计合适的硬件接口并在Vivado中创建工程。其次,编写Linux驱动程序,让它能够识别设备并且建立PCIe连接。接着,需要使用Linux的系统调用和文件操作接口等API对设备进行读写操作。 最后,在开发过程中需要注意以下几点 1. 理解Xilinx PCIe接口的工作原理,了解Linux的驱动程序工作机制 2. 确保硬件设计符合PCIe接口规范,设计良好,充分测试 3. 编写高效的Linux设备驱动程序 4. 在开发过程中遇到问题及时调试,遵循良好的代码规范和工程管理方法 总之,在Linux上开发Xilinx PCIe程序需要深入了解硬件设计、PCIe接口规范、Linux驱动程序开发和调试等方面的知识。只有掌握了这些知识,并且具有丰富的开发实践经验,才能够高效、快速地完成Xilinx PCIe接口应用开发。 ### 回答2: 随着计算机技术的快速发展,许多企业和研究机构开始将Linux操作系统用于高性能计算和数据处理。因此,对于开发Linux下的Xilinx PCIe程序,需了解PCIe总线的基本知识和应用,并掌握相应的开发工具和技术。 PCIe总线是一种高速、可靠、高扩展性的总线系统,可用于连接外设,如磁盘、视频卡、声卡等。Xilinx是一家专业从事FPGA、SoC以及器件级系统解决方案的公司,其PCIe IP核可用于集成到FPGA芯片中。在开发Linux下的Xilinx PCIe程序时,首先需要根据硬件平台、系统架构等进行选择和配置。其次,需要熟悉PCIe协议和FPGA的编程语言,包括Verilog、VHDL等。同时,还需要使用开发工具,如Xilinx ISE、Vivado等,进行开发、编译、仿真和调试。 除此之外,还需要掌握Linux操作系统的基本原理和命令。在Linux系统下,可以通过访问/dev目录、控制文件、驱动程序等方式,实现对设备的管理和控制。因此,在开发Xilinx PCIe程序时,可以结合Linux系统提供的API,如PCI驱动程序接口、DMA缓存等,实现数据传输和处理等操作。 总之,开发Linux下的Xilinx PCIe程序需要深入理解PCIe协议和FPGA编程语言,掌握相应的开发工具和技术,同时结合Linux操作系统提供的API实现功能。这将有助于提高开发效率和程序的稳定性和可靠性。 ### 回答3: Linux开发Xilinx PCIe程序,在FPGA开发中是非常重要的一个环节。PCIe总线作为高速数据传输的标准,在工业自动化、医疗设备、通信设备等领域都得到了广泛应用。而在Linux系统下,通过Xilinx PCIe接口实现对FPGA进行数据传输,可以实现高速、可靠的数据传输。 对于Linux开发Xilinx PCIe程序,需要掌握Linux系统和Xilinx PCIe接口的相关知识。首先,需要了解PCIe总线的工作原理和协议规范,以及相关的硬件电路设计。其次,需要熟悉Linux系统的驱动编程和IO操作,以及Linux内核对PCIe设备的支持。 在实际开发过程中,需要进行以下步骤: 1. 设计硬件电路,并对FPGA进行配置; 2. 设计Xilinx PCIe接口驱动程序,并进行编写、调试和测试; 3. 在Linux系统中加载驱动程序; 4. 编写应用程序,实现对FPGA进行数据传输和控制。 需要注意的是,开发Xilinx PCIe程序的过程中,需要对硬件和软件进行不断的优化和调试,以达到最佳的性能和稳定性。 总而言之,Linux开发Xilinx PCIe程序是一项复杂而重要的任务。通过掌握相关知识和技能,不仅可以提高开发效率,还可以为各行业的应用带来更加高效可靠的数据传输方案。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值