-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathznyq_axi_cdma_tutorial.c
345 lines (285 loc) · 10.7 KB
/
znyq_axi_cdma_tutorial.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/******************************************************************************
*
* Copyright (C) 2009 - 2014 Xilinx, Inc. All rights reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* Use of the Software is limited solely to applications:
* (a) running on a Xilinx device, or
* (b) that interact with a Xilinx device through a bus or interconnect.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* XILINX BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF
* OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
* Except as contained in this notice, the name of the Xilinx shall not be used
* in advertising or otherwise to promote the sale, use or other dealings in
* this Software without prior written authorization from Xilinx.
*
******************************************************************************/
/*
* helloworld.c: simple test application
*
* This application configures UART 16550 to baud rate 9600.
* PS7 UART (Zynq) is not initialized by this application, since
* bootrom/bsp configures it to baud rate 115200
*
* ------------------------------------------------
* | UART TYPE BAUD RATE |
* ------------------------------------------------
* uartns550 9600
* uartlite Configurable only in HW design
* ps7_uart 115200 (configured by bootrom/bsp)
*/
#include <stdio.h>
#include "platform.h"
#include "xil_types.h"
#include "xaxicdma.h"
#include "xscugic.h"
#include "xstatus.h"
#include "xparameters.h"
#include "xtime_l.h"
#include "xil_printf.h"
#include "xdebug.h"
#include "xil_exception.h"
#include "xil_cache.h"
#define DMA_TRANSFER_SIZE 32768
//#define DMA_TRANSFER_SIZE 32
//#define SRC_DDR_MEMORY XPAR_PS7_DDR_0_S_AXI_BASEADDR+0x00200000 // pass all code and data sections (2 MB)
//#define DST_DDR_MEMORY SRC_DDR_MEMORY+0x00200000 // (2 MB)
//#define DST_OCM_MEMORY XPAR_PS7_RAM_1_S_AXI_BASEADDR
// INTC
#define INTC_DEVICE_ID XPAR_SCUGIC_0_DEVICE_ID
//#define INTC_BASEADDR XPAR_PS7_SCUGIC_0_BASEADDR
// DMA
#define DMA_ID XPAR_AXI_CDMA_0_DEVICE_ID
#define DMA_CTRL_IRPT_INTR XPAR_FABRIC_AXICDMA_0_VEC_ID
// driver instances
static XScuGic Intc; // The instance of the generic interrupt controller (GIC)
static XAxiCdma Dma; /* Instance of the XAxiCdma */
#define CDMA_BRAM_MEMORY_0 0xC0000000 // portA of BRAM (connection from CDMA)
#define CDMA_BRAM_MEMORY_1 0x40000000 // portB of BRAM (connection from PS)
#define CDMA_OCM_MEMORY_0 0x00000000
// Source and Destination memory segments
unsigned char Src[DMA_TRANSFER_SIZE]; // source is always DRAM
unsigned char Dst_DDR[DMA_TRANSFER_SIZE]; // for CDMA to access the DRAM
unsigned char Dst_OCM[DMA_TRANSFER_SIZE]__attribute__((section(".mba_ocm_section")));
unsigned char Dst_BRAM[DMA_TRANSFER_SIZE]__attribute__((section(".mba_bram_section")));
cdma_memory_destination_bram = (u8 *)CDMA_BRAM_MEMORY_0; // for CDMA to access the BRAM
cdma_memory_destination_ocm = (u8 *)CDMA_OCM_MEMORY_0; // for CDMA to access the OCM
// AXI CDMA related definitions
XAxiCdma_Config *DmaCfg;
int Status = 0;
int i = 0;
XTime tStart, tEnd;
u32 totalCycle = 0;
int Done = 0;
int Error = 0;
// lfsr seed
uint8_t start_state = 0xACu; /* Any nonzero start state will work. */
uint8_t lfsr = 0xACu;
// GIC Setup
int SetupInterruptSystem(XScuGic *GicInstancePtr,XAxiCdma *DmaPtr);
// DMA done interrupt handler
static void Example_CallBack(void *CallBackRef, u32 IrqMask, int *IgnorePtr);
// function decleration for simple 8-bit lfsr for pseudo-random number generator
unsigned char lfsr_rand();
int main()
{
init_platform();
/* Initialize the XAxiCdma device.
*/
DmaCfg = XAxiCdma_LookupConfig(DMA_ID);
if (!DmaCfg) {
return XST_FAILURE;
}
Status = XAxiCdma_CfgInitialize(&Dma, DmaCfg, DmaCfg->BaseAddress);
if (Status != XST_SUCCESS) {
return XST_FAILURE;
}
Status = SetupInterruptSystem(&Intc,&Dma);
if (Status != XST_SUCCESS) return XST_FAILURE;
XAxiCdma_IntrEnable(&Dma, XAXICDMA_XR_IRQ_ALL_MASK);
// Disable DCache
Xil_DCacheDisable();
/******************************************************************************/
// First transfer DDR to DDR with AXI CDMA
/******************************************************************************/
// initialize writedata array with random numbers
int i = 0;
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
Src[i] = lfsr_rand();
}
// clear destination array
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
Dst_DDR[i] = 0x00;
}
Done = 0;
Status = XAxiCdma_SimpleTransfer(&Dma, (u32)Src,
(u32)Dst_DDR, DMA_TRANSFER_SIZE, Example_CallBack,
(void *)&Dma);
XTime_GetTime(&tStart);
if (Status == XST_SUCCESS)
{
while (!Done && !Error);
}
XTime_GetTime(&tEnd);
u64 et_dma_ddr = tEnd - tStart;
float et_us_dma_ddr = ((float)((float)et_dma_ddr) / 333333333.3)*1000000;
// check if write and read arrays are equal
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
if (Src[i] != Dst_DDR[i])
return XST_FAILURE;
}
/******************************************************************************/
// Second transfer DDR to OCM with AXI CDMA
/******************************************************************************/
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
Src[i] = lfsr_rand();
}
// clear destination array
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
Xil_Out8(CDMA_OCM_MEMORY_0+i, 0x00);
}
Done = 0; Error = 0;
Status = XAxiCdma_SimpleTransfer(&Dma, (u32)Src,
(u32)cdma_memory_destination_ocm, DMA_TRANSFER_SIZE, Example_CallBack,
(void *)&Dma);
XTime_GetTime(&tStart);
if (Status == XST_SUCCESS)
{
while (!Done && !Error);
}
XTime_GetTime(&tEnd);
u64 et_dma_ocm = tEnd - tStart;
float et_us_dma_ocm = ((float)((float)et_dma_ocm) / 333333333.3)*1000000;
// check if write and read arrays are equal
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
u8 dataread = Xil_In8(CDMA_OCM_MEMORY_0+i);
if (Src[i] != dataread)
return XST_FAILURE;
}
/******************************************************************************/
// Third transfer DDR to BRAM with AXI CDMA
/******************************************************************************/
// refill source array with pseudo-random numbers
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
Src[i] = lfsr_rand();
}
// clear destination array
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
Xil_Out8(CDMA_BRAM_MEMORY_1+i, 0x00);
}
Done = 0; Error = 0;
Status = XAxiCdma_SimpleTransfer(&Dma, (u32)Src,
(u32)cdma_memory_destination_bram, DMA_TRANSFER_SIZE, Example_CallBack,
(void *)&Dma);
XTime_GetTime(&tStart);
if (Status == XST_SUCCESS)
{
while (!Done && !Error);
}
XTime_GetTime(&tEnd);
u64 et_dma_bram = tEnd - tStart;
float et_us_dma_bram = ((float)((float)et_dma_bram) / 333333333.3)*1000000;
// check if write and read arrays are equal
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
u8 dataread = Xil_In8(CDMA_BRAM_MEMORY_1+i);
if (Src[i] != dataread)
return XST_FAILURE;
}
/******************************************************************************/
// Fourth transfer DDR to DDR with CPU load/store instructions
/******************************************************************************/
// refill source array with pseudo-random numbers
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
Src[i] = lfsr_rand();
}
// clear destination array
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
Dst_DDR[i] = 0x00;
}
// calculate time
XTime_GetTime(&tStart);
for (i=0;i<DMA_TRANSFER_SIZE;i++)
{
Dst_DDR[i] = Src[i];
}
XTime_GetTime(&tEnd);
u64 et_ls = tEnd - tStart;
float et_us_ls = ((float)((float)et_ls) / 333333333.3)*1000000;
cleanup_platform();
return 0;
}
/***************************************************************************************************************************************************/
// SetupInterruptSystem
/***************************************************************************************************************************************************/
int SetupInterruptSystem(XScuGic *GicInstancePtr,XAxiCdma *DmaPtr)
{
XScuGic_Config *IntcConfig;
Xil_ExceptionInit();
// initialize the interrupt control driver so that it is ready to use
IntcConfig = XScuGic_LookupConfig(INTC_DEVICE_ID);
Status = XScuGic_CfgInitialize(GicInstancePtr, IntcConfig, IntcConfig->CpuBaseAddress);
if (Status != XST_SUCCESS) return XST_FAILURE;
XScuGic_SetPriorityTriggerType(GicInstancePtr, DMA_CTRL_IRPT_INTR, 0xA0, 0x3);
// Connect the interrupt controller interrupt handler to the hardware interrupt handling logic in the processor
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_INT, (Xil_ExceptionHandler)XScuGic_InterruptHandler, GicInstancePtr);
// 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
// Connect the Fault ISR
Status = XScuGic_Connect(GicInstancePtr,DMA_CTRL_IRPT_INTR,(Xil_InterruptHandler)XAxiCdma_IntrHandler,(void *)DmaPtr);
if (Status != XST_SUCCESS) return XST_FAILURE;
// Enable the interrupt for the device
XScuGic_Enable(GicInstancePtr, DMA_CTRL_IRPT_INTR);
Xil_ExceptionInit();
Xil_ExceptionRegisterHandler(XIL_EXCEPTION_ID_IRQ_INT,
(Xil_ExceptionHandler)XScuGic_InterruptHandler,
GicInstancePtr);
Xil_ExceptionEnable();
// enable interrupts in the processor
//Xil_ExceptionEnableMask(XIL_EXCEPTION_IRQ);
return XST_SUCCESS;
}
static void Example_CallBack(void *CallBackRef, u32 IrqMask, int *IgnorePtr)
{
if (IrqMask & XAXICDMA_XR_IRQ_ERROR_MASK) {
Error = TRUE;
}
if (IrqMask & XAXICDMA_XR_IRQ_IOC_MASK) {
Done = TRUE;
}
}
// simple 8-bit lfsr for pseudo-random number generator
unsigned char lfsr_rand()
{
unsigned lsb = lfsr & 1; /* Get LSB (i.e., the output bit). */
lfsr >>= 1; /* Shift register */
if (lsb) { /* If the output bit is 1, apply toggle mask. */
lfsr ^= 0x71u;
}
return lfsr;
}