软件环境:vivado 2017.4 硬件平台:XC7Z020
这篇依旧是在前一篇AXI_DMA_PL_PS的基础上修改得到的,一方面去掉了PS-PL的读通道,另外一方面将PS端收到的PL发送的0~1023的数,通过裸LWIP经网口发送出去。
除了AXI_DMA少了个读通道,s2mm_introut直连IRQ_F2P[0:0]外,模块的连接关系以及顶层文件与上一篇一模一样。
Generate Bitstream,Export Hardware(注意勾选include biestream),最后launch SDK进行测试。
SDK这边一样,在上一篇程序的基础上,去掉读通道的相应配置,然后加上lwip的相关文件,并建立两个接收buffer。
首先添加板级LWIP包的支持。bsp包上右键,然后选Board Support Package Settings。
选Overview在lwip141前面打勾。
然后左侧继续选择lwip141进行具体参数设置。
可以看到修改以后的value都比defaul默认值大的多,目的是为了提升tcp的效率。
红框处参数修改完毕以后,有改动及新添加的文件如下,main.c。
#include "dma_intr.h"
#include "timer_intr.h"
#include "sys_intr.h"
#include "lwip/err.h"
#include "lwip/tcp.h"
#include "lwipopts.h"
#include "netif/xadapter.h"
#include "lwipopts.h"
#include "tcp_transmission.h"
static XScuGic Intc; //GIC
static XScuTimer Timer;//timer
XAxiDma AxiDma;
u32 *RxBufferPtr[2]; /* ping pong buffers*/
volatile u32 RX_success;
volatile u32 TX_success;
volatile u32 RX_ready=1;
volatile u32 TX_ready=1;
#define TIMER_LOAD_VALUE XPAR_CPU_CORTEXA9_0_CPU_CLK_FREQ_HZ / 8 //0.25S
int init_intr_sys(void)
{
DMA_Intr_Init(&AxiDma,0);//initial interrupt system
Timer_init(&Timer,TIMER_LOAD_VALUE,0);
Init_Intr_System(&Intc); // initial DMA interrupt system
Setup_Intr_Exception(&Intc);
DMA_Setup_Intr_System(&Intc,&AxiDma,0,RX_INTR_ID);//setup dma interrpt system
Timer_Setup_Intr_System(&Intc,&Timer,TIMER_IRPT_INTR);
DMA_Intr_Enable(&Intc,&AxiDma);
}
int main(void)
{
int Status;
struct netif *netif, server_netif;
struct ip_addr ipaddr, netmask, gw;
err_t err;
/* the mac address of the board. this should be unique per board */
unsigned char mac_ethernet_address[] = { 0x00, 0x0a, 0x35, 0x00, 0x01, 0x02 };
/* Initialize the ping pong buffers for the data received from axidma */
RxBufferPtr[0] = (u32 *)RX_BUFFER0_BASE;
RxBufferPtr[1] = (u32 *)RX_BUFFER1_BASE;
init_intr_sys();
TcpTmrFlag = 0;
netif = &server_netif;
IP4_ADDR(&ipaddr, 192, 168, 31, 10);
IP4_ADDR(&netmask, 255, 255, 255, 0);
IP4_ADDR(&gw, 192, 168, 31, 1);
/*lwip library init*/
lwip_init();
/* Add network interface to the netif_list, and set it as default */
if (!xemac_add(netif, &ipaddr, &netmask, &gw, mac_ethernet_address, XPAR_XEMACPS_0_BASEADDR)) {
xil_printf("Error adding N/W interface\r\n");
return -1;
}
netif_set_default(netif);
/* specify that the network if is up */
netif_set_up(netif);
/* initialize tcp pcb */
tcp_send_init();
Timer_start(&Timer);
while (1)
{
/* call tcp timer every 250ms */
if(TcpTmrFlag)
{
if(request_pcb->state == CLOSED || (request_pcb->state == SYN_SENT && request_pcb->nrtx == TCP_SYNMAXRTX))
{
request_pcb = tcp_new();
if (!request_pcb) {
xil_printf("txperf: Error creating PCB. Out of Memory\r\n");
return -1;
}
//ip_set_option(request_pcb, SOF_REUSEADDR);
err = tcp_connect(request_pcb, &ipaddress, port, tcp_connected_callback);
if (err != ERR_OK) {
xil_printf("txperf: tcp_connect returned error: %d\r\n", err);
return err;
}
}
tcp_tmr();
TcpTmrFlag = 0;
}
/*receive input packet from emac*/
xemacif_input(netif);
/* if connected to the server, start receive data from PL through axidma, then transmit the data to the PC software by TCP*/
if(tcp_client_connected)
send_received_data();
}
return 0;
}
tcp_transmission.c
#include <stdio.h>
#include <string.h>
#include "lwip/err.h"
#include "lwip/tcp.h"
#include "lwip/init.h"
#include "lwipopts.h"
#include "netif/xadapter.h"
#include "lwipopts.h"
#include "xaxidma.h"
#include "xil_cache.h"
#include "xil_printf.h"
#include "sleep.h"
#include "tcp_transmission.h"
#include "dma_intr.h"
void send_received_data()
{
#if __arm__
int copy = 3;
#else
int copy = 0;
#endif
err_t err;
int Status;
struct tcp_pcb *tpcb = connected_pcb;
/*initial the first axdma transmission, only excuse once*/
if(!first_trans_start)
{
Status = XAxiDma_SimpleTransfer(&AxiDma, (u32)RxBufferPtr[0],
(u32)(PAKET_LENGTH*sizeof(u32)), XAXIDMA_DEVICE_TO_DMA);
if (Status != XST_SUCCESS)
{
xil_printf("axi dma failed! 0 %d\r\n", Status);
return;
}
/*set the flag, so this part of code will not excuse again*/
first_trans_start = 1;
}
/*if the last axidma transmission is done, the interrupt triggered, then start TCP transmission*/
if(packet_trans_done)
{
if (!connected_pcb)
return;
/* if tcp send buffer has enough space to hold the data we want to transmit from PL, then start tcp transmission*/
if (tcp_sndbuf(tpcb) > SEND_SIZE)
{
/*transmit received data through TCP*/
Xil_DCacheInvalidateRange((u32)RxBufferPtr[packet_index & 1], SEND_SIZE);
err = tcp_write(tpcb, RxBufferPtr[packet_index & 1], SEND_SIZE, copy);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_write: %d\r\n", err);
connected_pcb = NULL;
return;
}
err = tcp_output(tpcb);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_output: %d\r\n",err);
return;
}
packet_index++;
/*clear the axidma done flag*/
packet_trans_done = 0;
/*initial the other axidma transmission when the current transmission is done*/
Status = XAxiDma_SimpleTransfer(&AxiDma, (u32)RxBufferPtr[(packet_index + 1)&1],
(u32)(PAKET_LENGTH*sizeof(u32)), XAXIDMA_DEVICE_TO_DMA);
if (Status != XST_SUCCESS)
{
xil_printf("axi dma %d failed! %d \r\n", (packet_index + 1), Status);
return;
}
}
}
}
/*this fuction just used to count the tcp transmission times*/
static err_t
tcp_sent_callback(void *arg, struct tcp_pcb *tpcb, u16_t len)
{
err_t err;
tcp_trans_done = 1;
err = tcp_output(tpcb);
if (err != ERR_OK) {
xil_printf("txperf: Error on tcp_output: %d\r\n",err);
return -1;
}
return ERR_OK;
}
err_t
tcp_connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err)
{
xil_printf("txperf: Connected to iperf server\r\n");
/* store state */
connected_pcb = tpcb;
/* set callback values & functions */
tcp_arg(tpcb, NULL);
tcp_sent(tpcb, tcp_sent_callback);
/* disable nagle algorithm to ensure
* the last small segment of a ADC packet will be sent out immediately
* with no delay
* */
tcp_nagle_disable(tpcb);
if(!tcp_nagle_disabled(tpcb))
xil_printf("tcp nagle disable failed!\r\n");
tcp_client_connected = 1;
/* initiate data transfer */
return ERR_OK;
}
int tcp_send_init()
{
err_t err;
/* create new TCP PCB structure */
request_pcb = tcp_new();
if (!request_pcb) {
xil_printf("txperf: Error creating PCB. Out of Memory\r\n");
return -1;
}
/* connect to iperf tcp server */
IP4_ADDR(&ipaddress, 192, 168, 31, 178); /* iperf server address */
port = 9527; /* iperf default port */
tcp_client_connected = 0;
first_trans_start = 0;
packet_trans_done = 0;
packet_index = 0;
tcp_trans_done = 1;
connected_pcb = NULL;
err = tcp_connect(request_pcb, &ipaddress, port, tcp_connected_callback);
if (err != ERR_OK) {
xil_printf("txperf: tcp_connect returned error: %d\r\n", err);
return err;
}
return 0;
}
tcp_transmission.h
#ifndef TCP_TRANSMISSION_H_
#define TCP_TRANSMISSION_H_
#include <stdio.h>
#include "xadcps.h"
#include "xil_types.h"
#include "Xscugic.h"
#include "Xil_exception.h"
#define SEND_SIZE (1024)
#define PAKET_LENGTH (1024)
struct tcp_pcb *connected_pcb;
struct tcp_pcb *request_pcb;
volatile unsigned tcp_client_connected;
volatile int tcp_trans_done;
unsigned first_trans_start;
volatile u32 packet_index;
extern XAxiDma AxiDma;
extern u32 *RxBufferPtr[2];
u16_t port;
struct ip_addr ipaddress;
int tcp_send_init();
void send_received_data();
err_t tcp_connected_callback(void *arg, struct tcp_pcb *tpcb, err_t err);
#endif /* TCP_TRANSMISSION_H_ */
dma_intr.c
#include "dma_intr.h"
int DMA_CheckData(int Length, u8 StartValue)
{
u8 *RxPacket;
int Index = 0;
u8 Value;
RxPacket = (u8 *) RX_BUFFER_BASE;
Value = StartValue;
/* Invalidate the DestBuffer before receiving the data, in case the
* Data Cache is enabled
*/
#ifndef __aarch64__
Xil_DCacheInvalidateRange((u32)RxPacket, Length);
#endif
for(Index = 0; Index < Length; Index++) {
if (RxPacket[Index] != Value) {
xil_printf("Data error %d: %x/%x\r\n",
Index, RxPacket[Index], Value);
return XST_FAILURE;
}
Value = (Value + 1) & 0xFF;
}
return XST_SUCCESS;
}
void DMA_DisableIntrSystem(XScuGic * IntcInstancePtr,
u16 TxIntrId, u16 RxIntrId)
{
#ifdef XPAR_INTC_0_DEVICE_ID
/* Disconnect the interrupts for the DMA TX and RX channels */
//XIntc_Disconnect(IntcInstancePtr, TxIntrId);
XIntc_Disconnect(IntcInstancePtr, RxIntrId);
#else
//XScuGic_Disconnect(IntcInstancePtr, TxIntrId);
XScuGic_Disconnect(IntcInstancePtr, RxIntrId);
#endif
}
static void DMA_TxIntrHandler(void *Callback)
{
u32 IrqStatus;
int TimeOut;
XAxiDma *AxiDmaInst = (XAxiDma *)Callback;
/* Read pending interrupts */
IrqStatus = XAxiDma_IntrGetIrq(AxiDmaInst, XAXIDMA_DMA_TO_DEVICE);
/* Acknowledge pending interrupts */
XAxiDma_IntrAckIrq(AxiDmaInst, IrqStatus, XAXIDMA_DMA_TO_DEVICE);
/*
* If no interrupt is asserted, we do not do anything
*/
if (!(IrqStatus & XAXIDMA_IRQ_ALL_MASK)) {
return;
}
/*
* If error interrupt is asserted, raise error flag, reset the
* hardware to recover from the error, and return with no further
* processing.
*/
if ((IrqStatus & XAXIDMA_IRQ_ERROR_MASK)) {
xil_printf("rx error! \r\n");
/*
* Reset should never fail for transmit channel
*/
XAxiDma_Reset(AxiDmaInst);
TimeOut = RESET_TIMEOUT_COUNTER;
while (TimeOut) {
if (XAxiDma_ResetIsDone(AxiDmaInst)) {
break;
}
TimeOut -= 1;
}
return;
}
/*
* If Completion interrupt is asserted, then set the TxDone flag
*/
if ((IrqStatus & XAXIDMA_IRQ_IOC_MASK)) {
TxDone = 1;
}
}
static void DMA_RxIntrHandler(void *Callback)
{
u32 IrqStatus;
int TimeOut;
XAxiDma *AxiDmaInst = (XAxiDma *)Callback;
/* Read pending interrupts */
IrqStatus = XAxiDma_IntrGetIrq(AxiDmaInst, XAXIDMA_DEVICE_TO_DMA);
/* Acknowledge pending interrupts */
XAxiDma_IntrAckIrq(AxiDmaInst, IrqStatus, XAXIDMA_DEVICE_TO_DMA);
/*
* If no interrupt is asserted, we do not do anything
*/
if (!(IrqStatus & XAXIDMA_IRQ_ALL_MASK)) {
return;
}
/*
* If error interrupt is asserted, raise error flag, reset the
* hardware to recover from the error, and return with no further
* processing.
*/
if ((IrqStatus & XAXIDMA_IRQ_ERROR_MASK)) {
xil_printf("rx error! \r\n");
return;
}
/*
* If completion interrupt is asserted, then set RxDone flag
*/
if ((IrqStatus & XAXIDMA_IRQ_IOC_MASK)) {
if(packet_trans_done)
xil_printf("last transmission has not finished!\r\n");
else
/*set the axidma done flag*/
packet_trans_done = 1;
}
}
int DMA_Setup_Intr_System(XScuGic * IntcInstancePtr,XAxiDma * AxiDmaPtr, u16 TxIntrId, u16 RxIntrId)
{
int Status;
//XScuGic_SetPriorityTriggerType(IntcInstancePtr, TxIntrId, 0xA0, 0x3);
XScuGic_SetPriorityTriggerType(IntcInstancePtr, RxIntrId, 0xA0, 0x3);
/*
* Connect the device driver handler that will be called when an
* interrupt for the device occurs, the handler defined above performs
* the specific interrupt processing for the device.
*/
/*
Status = XScuGic_Connect(IntcInstancePtr, TxIntrId,
(Xil_InterruptHandler)DMA_TxIntrHandler,
AxiDmaPtr);
if (Status != XST_SUCCESS) {
return Status;
}
*/
Status = XScuGic_Connect(IntcInstancePtr, RxIntrId,
(Xil_InterruptHandler)DMA_RxIntrHandler,
AxiDmaPtr);
if (Status != XST_SUCCESS) {
return Status;
}
//XScuGic_Enable(IntcInstancePtr, TxIntrId);
XScuGic_Enable(IntcInstancePtr, RxIntrId);
return XST_SUCCESS;
}
int DMA_Intr_Enable(XScuGic * IntcInstancePtr,XAxiDma *DMAPtr)
{
/* Disable all interrupts before setup */
/*
XAxiDma_IntrDisable(DMAPtr, XAXIDMA_IRQ_ALL_MASK,
XAXIDMA_DMA_TO_DEVICE);
*/
XAxiDma_IntrDisable(DMAPtr, XAXIDMA_IRQ_ALL_MASK,
XAXIDMA_DEVICE_TO_DMA);
/* Enable all interrupts */
/*
XAxiDma_IntrEnable(DMAPtr, XAXIDMA_IRQ_ALL_MASK,
XAXIDMA_DMA_TO_DEVICE);
*/
XAxiDma_IntrEnable(DMAPtr, XAXIDMA_IRQ_ALL_MASK,
XAXIDMA_DEVICE_TO_DMA);
return XST_SUCCESS;
}
int DMA_Intr_Init(XAxiDma *DMAPtr,u32 DeviceId)
{
int Status;
XAxiDma_Config *Config=NULL;
Config = XAxiDma_LookupConfig(DeviceId);
if (!Config) {
xil_printf("No config found for %d\r\n", DeviceId);
return XST_FAILURE;
}
/* Initialize DMA engine */
Status = XAxiDma_CfgInitialize(DMAPtr, Config);
if (Status != XST_SUCCESS) {
xil_printf("Initialization failed %d\r\n", Status);
return XST_FAILURE;
}
if(XAxiDma_HasSg(DMAPtr)){
xil_printf("Device configured as SG mode \r\n");
return XST_FAILURE;
}
return XST_SUCCESS;
}
dma_intr.h
#ifndef DMA_INTR_H
#define DMA_INTR_H
#include "xaxidma.h"
#include "xparameters.h"
#include "xil_exception.h"
#include "xdebug.h"
#include "xscugic.h"
#define DMA_DEV_ID XPAR_AXIDMA_0_DEVICE_ID
#define MEM_BASE_ADDR 0x10000000
#define RX_INTR_ID XPAR_FABRIC_AXI_DMA_0_S2MM_INTROUT_INTR
#define MEM_BASE_ADDR 0x10000000
#define RX_BUFFER_BASE (MEM_BASE_ADDR + 0x00300000)
#define RX_BUFFER_HIGH (MEM_BASE_ADDR + 0x004FFFFF)
#define RX_BUFFER0_BASE RX_BUFFER_BASE
#define RX_BUFFER1_BASE (RX_BUFFER_BASE + 0x00020000)
#define RESET_TIMEOUT_COUNTER 10000
#define TEST_START_VALUE 0xC
#define MAX_PKT_LEN 2048
#define NUMBER_OF_TRANSFERS 100000
volatile int TxDone;
volatile int packet_trans_done;
volatile int Error;
int DMA_CheckData(int Length, u8 StartValue);
int DMA_Setup_Intr_System(XScuGic * IntcInstancePtr,XAxiDma * AxiDmaPtr, u16 TxIntrId, u16 RxIntrId);
int DMA_Intr_Enable(XScuGic * IntcInstancePtr,XAxiDma *DMAPtr);
int DMA_Intr_Init(XAxiDma *DMAPtr,u32 DeviceId);
#endif
sys_intr.c
#include "sys_intr.h"
void Setup_Intr_Exception(XScuGic * IntcInstancePtr)
{
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
(void *)IntcInstancePtr);
Xil_ExceptionEnable();
}
int Init_Intr_System(XScuGic * IntcInstancePtr)
{
int Status;
XScuGic_Config *IntcConfig;
IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
if (NULL == IntcConfig) {
return XST_FAILURE;
}
Status = XScuGic_CfgInitialize(IntcInstancePtr, IntcConfig,
IntcConfig->CpuBaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
return XST_SUCCESS;
}
sys_intr.h
#ifndef SYS_INTR_H_
#define SYS_INTR_H_
#include "xparameters.h"
#include "xil_exception.h"
#include "xdebug.h"
#include "xscugic.h"
#define INTC_DEVICE_ID XPAR_SCUGIC_SINGLE_DEVICE_ID
int Init_Intr_System(XScuGic * IntcInstancePtr);
void setup_Intr_Exception(XScuGic * IntcInstancePtr);
#endif /* SYS_INTR_H_ */
timer_intr.c
#include "timer_intr.h"
volatile int TcpTmrFlag;
static void TimerIntrHandler(void *CallBackRef)
{
XScuTimer *TimerInstancePtr = (XScuTimer *) CallBackRef;
XScuTimer_ClearInterruptStatus(TimerInstancePtr);
TcpTmrFlag = 1;
//isrDone++;
}
void Timer_start(XScuTimer *TimerPtr)
{
XScuTimer_Start(TimerPtr);
}
void Timer_Setup_Intr_System(XScuGic *GicInstancePtr,XScuTimer *TimerInstancePtr, u16 TimerIntrId)
{
XScuGic_Connect(GicInstancePtr, TimerIntrId,
(Xil_ExceptionHandler)TimerIntrHandler,//set up the timer interrupt
(void *)TimerInstancePtr);
XScuGic_Enable(GicInstancePtr, TimerIntrId);//enable the interrupt for the Timer at GIC
XScuTimer_EnableInterrupt(TimerInstancePtr);//enable interrupt on the timer
}
int Timer_init(XScuTimer *TimerPtr,u32 Load_Value,u32 DeviceId)
{
XScuTimer_Config *TMRConfigPtr;
TMRConfigPtr = XScuTimer_LookupConfig(DeviceId);
XScuTimer_CfgInitialize(TimerPtr, TMRConfigPtr,TMRConfigPtr->BaseAddr);
XScuTimer_LoadTimer(TimerPtr, Load_Value);
XScuTimer_EnableAutoReload(TimerPtr);
return 1;
}
timer_intr.h
#ifndef TIMER_INTR_H_
#define TIMER_INTR_H_
#include <stdio.h>
#include "xadcps.h"
#include "xil_types.h"
#include "xscugic.h"
#include "xil_exception.h"
#include "xscutimer.h"
extern volatile int TcpTmrFlag;
//timer info
#define TIMER_DEVICE_ID XPAR_XSCUTIMER_0_DEVICE_ID
#define TIMER_IRPT_INTR XPAR_SCUTIMER_INTR
void Timer_start(XScuTimer *TimerPtr);
void Timer_Setup_Intr_System(XScuGic *GicInstancePtr,XScuTimer *TimerInstancePtr, u16 TimerIntrId);
int Timer_init(XScuTimer *TimerPtr,u32 Load_Value,u32 DeviceId);
#endif /* TIMER_INTR_H_ */
进debug,先把网口打开,记得端口号该的和程序里面服务器的配置一致,然后运行程序。SDK这边可以看到,百兆网,右侧可以看到接收到的数据。
网口助手打开,设置如下,友情提示一下,最好把接收显示关掉,不然数据量太大,网口助手容易卡死,右侧可以看到,数据32bits依次递增。
VIVADO这边。
最后说一下程序这边需要注意的。
main.c:
(1)u32 *RxBufferPtr[2];根据你自己的传输数据位宽,来设置数组类型,最好与AXI_DMA通道宽度保持一致。
(2)RxBufferPtr[0] = (u32 *)RX_BUFFER0_BASE;基址前的类型转换别忘了也要一起改。
(3)IP4_ADDR(&ipaddr, 192, 168, 31, 10);根据自己实际网络地址修改,这个是板子的IP,与电脑IP要在同一网段。
(4)send_received_data();是主要的AXI_DMA数据接收及LWIP数据发送的处理函数。
tcp_transmission.c
(1)所有涉及PAKET_LENGTH的地方,记得*sizeof(u32),与传输数据宽度保持一致。
(2)tcp_send_init()函数中,记得修改tcp_server的IP地址,IP4_ADDR(&ipaddress, 192, 168, 31, 178);就是主机IP。
(3)port = 9527;主机端口
(4)send_received_data();函数大概原理是通过XAxiDma_SimpleTransfer(&AxiDma, (u32)RxBufferPtr[0],(u32)(PAKET_LENGTH*sizeof(u32)), XAXIDMA_DEVICE_TO_DMA);启动AXI_DMA传输,AXI_DMA传输完成后,会将packet_trans_done置1(看dma_intr.c的DMA_RxIntrHandler(void *Callback)),利用Xil_DCacheInvalidateRange((u32)RxBufferPtr[packet_index & 1], SEND_SIZE);在其中一个buffer收数时,将另一个buffer数据刷新,完成乒乓的操作,然后利用tcp_write(tpcb, RxBufferPtr[packet_index & 1], SEND_SIZE, copy);通过tcp将数据发出,最后再一次 XAxiDma_SimpleTransfer(&AxiDma, (u32)RxBufferPtr[(packet_index + 1)&1],(u32)(PAKET_LENGTH*sizeof(u32)), XAXIDMA_DEVICE_TO_DMA);启动下一次AXI_DMA数据传输。