Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/platform_drivers/Xilinx/SDP_ADC/AD7091/main.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file main.c
3 * @brief Implementation of main program for FMC-SDP evaluation projects.
4 * @author Istvan Csomortani ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2011(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 ******************************************************************************/
41
42 /*****************************************************************************/
43 /***************************** Include Files *********************************/
44 /*****************************************************************************/
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include "xparameters.h"
48 #include "Communication.h"
49 #include "xaxidma.h"
50 #include "xdma_config.h"
51 #include "ad7091.h"
52
53 /*****************************************************************************/
54 /************************ Constants Definitions ******************************/
55 /*****************************************************************************/
56
57 #define NULL ((void *)0)
58 #define SUCCESS 0
59 #define ERROR -1
60
61 /* ADC core base address */
62 #define ADC_BASEADDR XPAR_AXI_AD7091_0_BASEADDR
63
64 /* Memory allocation macros */
65 #define DMA_DEV_ID XPAR_AXIDMA_0_DEVICE_ID
66 #define DMA_BASEADDR XPAR_AXI_DMA_0_BASEADDR
67 #define DDR_BASEADDR XPAR_DDR3_SDRAM_S_AXI_BASEADDR
68 #define MEM_BASEADDR (DDR_BASEADDR + 0x1000000)
69 // memory space for the buffer descriptors (63 descriptors)
70 #define RX_BD_SPACE_BASE (MEM_BASEADDR + 0x00001000)
71 #define RX_BD_SPACE_HIGH (MEM_BASEADDR + 0x00001FFF)
72 // memory space for the data buffer
73 #define RX_BUFFER_BASE (MEM_BASEADDR + 0x00300000)
74 #define RX_BUFFER_HIGH (MEM_BASEADDR + 0x004FFFFF)
75
76 // Number of samples
77 // Note: XDMA buffer length is 14 -> max sample is 2^14 - > 16k
78 #define SAMPLE_NO 16 * 1024
79
80 // maximum packet length per descriptor
81 #define MAX_PKT_LEN 0x400
82 // number of packets
83 #define NUMBER_OF_PACKETS 0x20
84 // Stream Data Width is 32
85 #define BUS_WIDTH 4
86
87 /*****************************************************************************/
88 /************************ Variables Definitions ******************************/
89 /*****************************************************************************/
90 /* Device instance definitions */
91 XAxiDma AxiDma;
92 /* Global variable for script interactions */
93 int startAcquisition = 0;
94 u32 * prxData = NULL;
95 u32 numberOfSample = 0;
96
97 /*****************************************************************************/
98 /************************** Function Prototypes ******************************/
99 /*****************************************************************************/
100
101 static int AxiDmaSetup(XAxiDma * AxiDmaInstPtr);
102
103 /**************************************************************************//**
104 * @brief Main function implementation.
105 *
106 * @return None
107 ******************************************************************************/
108 int main(void)
109 {
110 int Status;
111
112 // pointer to the Rx Buffer
113 prxData = (u32*)RX_BUFFER_BASE;
114 // number of samples
115 numberOfSample = SAMPLE_NO;
116
117 // initialize the UART
118 UART_Init(115200);
119
120 // setup the AXI DMA
121 Status = AxiDmaSetup(&AxiDma);
122 if(Status != SUCCESS)
123 {
124 xil_printf("AXI DMA setup failed!\r\n");
125 return ERROR;
126 }
127 else
128 {
129 xil_printf("AXI DMA setup complete.\r\n");
130 }
131
132 // initialize the ADC
133 AD7091_Init(ADC_BASEADDR);
134
135 // waiting for script
136 while(!startAcquisition);
137
138 // start data acquisition
139 Status = AD7091_GetData(numberOfSample);
140 if(Status != SUCCESS)
141 {
142 xil_printf("Overflow occurred\r\n");
143 }
144 xil_printf("Data transaction was finished!\r\n");
145
146 // waiting for script to save the received data
147 while(startAcquisition);
148
149 xil_printf("Data was saved to Acquisition.csv file!\r\n");
150
151 return SUCCESS;
152 }
153
154 /**************************************************************************//**
155 * @brief This function sets up RX channel of the DMA engine to be ready
156 * for packet reception
157 *
158 * @param AxiDmaInstPtr is the pointer to the instance of the DMA engine.
159 *
160 * @return SUCCESS if the setup is successful, ERROR otherwise.
161 *
162 ******************************************************************************/
163 static int AxiDmaSetup(XAxiDma * AxiDmaInstPtr)
164 {
165 XAxiDma_BdRing *RxRingPtr;
166 XAxiDma_Config *Config;
167 int Delay = 0;
168 int Coalesce = 1;
169 int Status;
170 XAxiDma_Bd BdTemplate;
171 XAxiDma_Bd *BdPtr;
172 XAxiDma_Bd *BdCurPtr;
173 u32 BdCount;
174 u32 FreeBdCount;
175 u32 RxBufferPtr;
176 int Index;
177
178 Config = XAxiDma_LookupConfig(DMA_DEV_ID);
179 if (!Config) {
180 xil_printf("No configuration found for %d\r\n", DMA_DEV_ID);
181 return ERROR;
182 }
183
184 /* Initialize DMA engine */
185 Status = XAxiDma_CfgInitialize(AxiDmaInstPtr, Config);
186 if (Status != SUCCESS) {
187 xil_printf("Initialization failed %d\r\n", Status);
188 return ERROR;
189 }
190
191 if(!XAxiDma_HasSg(AxiDmaInstPtr)) {
192 xil_printf("Device configured as Simple mode \r\n");
193 return ERROR;
194 }
195
196 RxRingPtr = XAxiDma_GetRxRing(AxiDmaInstPtr);
197
198 /* Disable all RX interrupts before RxBD space setup */
199
200 XAxiDma_BdRingIntDisable(RxRingPtr, XAXIDMA_IRQ_ALL_MASK);
201
202 /* Set delay and coalescing */
203 XAxiDma_BdRingSetCoalesce(RxRingPtr, Coalesce, Delay);
204
205 /* Setup Rx BD space */
206 BdCount = XAxiDma_BdRingCntCalc(XAXIDMA_BD_MINIMUM_ALIGNMENT,
207 RX_BD_SPACE_HIGH - RX_BD_SPACE_BASE + 1);
208
209 Status = XAxiDma_BdRingCreate(RxRingPtr, RX_BD_SPACE_BASE,
210 RX_BD_SPACE_BASE,
211 XAXIDMA_BD_MINIMUM_ALIGNMENT, BdCount);
212
213 if (Status != SUCCESS) {
214 xil_printf("RX create BD ring failed %d\r\n", Status);
215 return ERROR;
216 }
217
218 /*
219 * Setup an all-zero BD as the template for the Rx channel.
220 */
221 XAxiDma_BdClear(&BdTemplate);
222
223 Status = XAxiDma_BdRingClone(RxRingPtr, &BdTemplate);
224 if (Status != SUCCESS) {
225 xil_printf("RX clone BD failed %d\r\n", Status);
226 return ERROR;
227 }
228
229 /* Attach buffers to RxBD ring so we are ready to receive packets */
230
231 FreeBdCount = XAxiDma_BdRingGetFreeCnt(RxRingPtr);
232
233 Status = XAxiDma_BdRingAlloc(RxRingPtr, FreeBdCount, &BdPtr);
234 if (Status != SUCCESS) {
235 xil_printf("RX alloc BD failed %d\r\n", Status);
236 return ERROR;
237 }
238
239 BdCurPtr = BdPtr;
240 RxBufferPtr = RX_BUFFER_BASE;
241 for (Index = 0; Index < FreeBdCount; Index++) {
242 Status = XAxiDma_BdSetBufAddr(BdCurPtr, RxBufferPtr);
243
244 if (Status != SUCCESS) {
245 xil_printf("Set buffer addr %x on BD %x failed %d\r\n",
246 (unsigned int)RxBufferPtr,
247 (unsigned int)BdCurPtr, Status);
248 return ERROR;
249 }
250
251 Status = XAxiDma_BdSetLength(BdCurPtr, MAX_PKT_LEN,
252 RxRingPtr->MaxTransferLen);
253 if (Status != SUCCESS) {
254 xil_printf("Rx set length %d on BD %x failed %d\r\n",
255 MAX_PKT_LEN, (unsigned int)BdCurPtr, Status);
256 return ERROR;
257 }
258
259 /* Receive BDs do not need to set anything for the control
260 * The hardware will set the SOF/EOF bits per stream status
261 */
262 XAxiDma_BdSetCtrl(BdCurPtr, 0);
263 XAxiDma_BdSetId(BdCurPtr, RxBufferPtr);
264
265 RxBufferPtr += MAX_PKT_LEN;
266 BdCurPtr = XAxiDma_BdRingNext(RxRingPtr, BdCurPtr);
267 }
268
269 /* Clear the receive buffer, so we can verify data
270 */
271 memset((void *)RX_BUFFER_BASE, 0, (MAX_PKT_LEN * NUMBER_OF_PACKETS * 4));
272
273 Status = XAxiDma_BdRingToHw(RxRingPtr, FreeBdCount,
274 BdPtr);
275 if (Status != SUCCESS) {
276 xil_printf("RX submit hw failed %d\r\n", Status);
277 return ERROR;
278 }
279
280 /* Start RX DMA channel */
281 Status = XAxiDma_BdRingStart(RxRingPtr);
282 if (Status != SUCCESS) {
283 xil_printf("RX start hw failed %d\r\n", Status);
284 return ERROR;
285 }
286
287 return SUCCESS;
288 }