Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/AD9467-FMC-EBZ/cf_ad9467.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file cf_ad9467.c
3 * @brief Implementation of cf_ad9467 Driver.
4 ********************************************************************************
5 * Copyright 2012(c) Analog Devices, Inc.
6 *
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
11 * - Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * - Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
16 * distribution.
17 * - Neither the name of Analog Devices, Inc. nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
20 * - The use of this software may or may not infringe the patent rights
21 * of one or more patent holders. This license does not release you
22 * from the requirement that you obtain separate licenses from these
23 * patent holders to use this software.
24 * - Use of the software either in source or binary form, must be run
25 * on or directly connected to an Analog Devices Inc. component.
26 *
27 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
28 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
29 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
30 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
31 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
32 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
33 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
34 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
35 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
36 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 *
38 ********************************************************************************
39 * SVN Revision: $WCREV$
40 *******************************************************************************/
41
42 /******************************************************************************/
43 /***************************** Include Files **********************************/
44 /******************************************************************************/
45 #include "cf_ad9467.h"
46 #include "xil_io.h"
47 #include "AD9467.h"
48
49 extern char inbyte(void);
50 void Xil_DCacheFlush();
51 void xil_printf(const char *ctrl1, ...);
52
53 /*****************************************************************************/
54 /************************ Private Functions Prototypes ***********************/
55 /*****************************************************************************/
56 void DisplayTestMode(uint32_t mode, uint32_t format);
57
58 /******************************************************************************/
59 /************************ Functions Definitions *******************************/
60 /******************************************************************************/
61
62 /***************************************************************************//**
63 * @brief Creates a delay of milliseconds.
64 *
65 * @param ms_count - Delay in milliseconds.
66 *
67 * @return None.
68 *******************************************************************************/
69 void delay_ms(uint32_t ms_count)
70 {
71 uint32_t count;
72 #ifdef _XPARAMETERS_PS_H_
73 for (count = 0; count < ((ms_count * 100000) + 1); count++)
74 {
75 asm("nop");
76 }
77 #else
78 for(count = 0; count < ms_count * 10000; count++);
79 #endif
80 }
81
82 /***************************************************************************//**
83 * @brief adc_dma_read
84 *******************************************************************************/
85 void adc_dma_read(uint32_t regAddr, uint32_t *data)
86 {
87 *data = Xil_In32(XPAR_AXI_AD9467_DMA_BASEADDR + regAddr);
88 }
89
90 /***************************************************************************//**
91 * @brief adc_dma_write
92 *******************************************************************************/
93 void adc_dma_write(uint32_t regAddr, uint32_t data)
94 {
95 Xil_Out32(XPAR_AXI_AD9467_DMA_BASEADDR + regAddr, data);
96 }
97
98 /***************************************************************************//**
99 * @brief Captures a specified number of samples from the ADC.
100 *
101 * @param size - number of bytes to read from the device
102 * @param address - capture start address
103 *
104 * @return None.
105 *******************************************************************************/
106 void adc_capture(uint32_t size, uint32_t address)
107 {
108 uint32_t reg_val;
109 uint32_t transfer_id;
110 uint32_t length;
111
112 length = (size * 2);
113
114 adc_dma_write(AXI_DMAC_REG_CTRL, 0x0);
115 adc_dma_write(AXI_DMAC_REG_CTRL, AXI_DMAC_CTRL_ENABLE);
116
117 adc_dma_write(AXI_DMAC_REG_IRQ_MASK, 0x0);
118
119 adc_dma_read(AXI_DMAC_REG_TRANSFER_ID, &transfer_id);
120 adc_dma_read(AXI_DMAC_REG_IRQ_PENDING, &reg_val);
121 adc_dma_write(AXI_DMAC_REG_IRQ_PENDING, reg_val);
122
123 adc_dma_write(AXI_DMAC_REG_DEST_ADDRESS, address);
124 adc_dma_write(AXI_DMAC_REG_DEST_STRIDE, 0x0);
125 adc_dma_write(AXI_DMAC_REG_X_LENGTH, length - 1);
126 adc_dma_write(AXI_DMAC_REG_Y_LENGTH, 0x0);
127
128 adc_dma_write(AXI_DMAC_REG_START_TRANSFER, 0x1);
129 /* Wait until the new transfer is queued. */
130 do {
131 adc_dma_read(AXI_DMAC_REG_START_TRANSFER, &reg_val);
132 }
133 while(reg_val == 1);
134
135 /* Wait until the current transfer is completed. */
136 do {
137 adc_dma_read(AXI_DMAC_REG_IRQ_PENDING, &reg_val);
138 }
139 while(reg_val != (AXI_DMAC_IRQ_SOT | AXI_DMAC_IRQ_EOT));
140 adc_dma_write(AXI_DMAC_REG_IRQ_PENDING, reg_val);
141
142 /* Wait until the transfer with the ID transfer_id is completed. */
143 do {
144 adc_dma_read(AXI_DMAC_REG_TRANSFER_DONE, &reg_val);
145 }
146 while((reg_val & (1 << transfer_id)) != (1 << transfer_id));
147
148 #ifdef _XPARAMETERS_PS_H_
149 Xil_DCacheFlush();
150 #else
151 microblaze_flush_dcache();
152 microblaze_invalidate_dcache();
153 #endif
154 }
155
156 /***************************************************************************//**
157 * @brief ADC delay.
158 *
159 * @param delay - Value of the delay.
160 *
161 * @return 0.
162 *******************************************************************************/
163 uint32_t adc_delay_1(uint32_t no_of_lanes, uint32_t delay)
164 {
165 uint32_t i;
166 uint32_t rdata;
167 uint32_t pcore_version;
168
169 pcore_version = Xil_In32(CF_BASEADDR + 0x0);
170 pcore_version >>= 16;
171 xil_printf("pcore_version: %d\n\r", pcore_version);
172 if (pcore_version < 9) {
173 for (i = 0; i < no_of_lanes; i++) {
174 Xil_Out32((CF_BASEADDR + 0x060), 0x0); // delay write
175 Xil_Out32((CF_BASEADDR + 0x060), (0x20000 | (i<<8) | delay)); // delay write
176 while (Xil_In32((CF_BASEADDR + 0x064) & 0x100) == 0x100) {}
177 }
178
179 for (i = 0; i < no_of_lanes; i++) {
180 Xil_Out32((CF_BASEADDR + 0x060), 0x0); // delay write
181 Xil_Out32((CF_BASEADDR + 0x060), (0x30000 | (i<<8) | delay)); // delay write
182 while (Xil_In32((CF_BASEADDR + 0x064) & 0x100) == 0x100) {}
183 rdata = Xil_In32(CF_BASEADDR + 0x064) & 0xff;
184 if (rdata != delay) {
185 xil_printf("adc_delay_1: sel(%2d), data(%04x)\n\r", i, rdata);
186 }
187 }
188 } else {
189 for (i = 0; i < no_of_lanes; i++) {
190 Xil_Out32((CF_BASEADDR + ((0x200 + i)*4)), delay);
191 rdata = Xil_In32(CF_BASEADDR + ((0x200 + i)*4));
192 if (rdata != delay) {
193 xil_printf("adc_delay_1: sel(%2d), rcv(%04x), exp(%04x)\n\r", i, rdata, delay);
194 }
195 }
196 }
197
198 return(0);
199 }
200
201 /***************************************************************************//**
202 * @brief adc_pn_mon
203 *
204 * @param pn23_pn9_n - PN type.
205 *
206 * @return 0 or error.
207 *******************************************************************************/
208 u32 adc_pn_mon(u32 pn23_pn9_n)
209 {
210 uint32_t rdata;
211 uint32_t error;
212
213 Xil_Out32((CF_BASEADDR + 0x400), 0x1); // signextn, fmt & ch enable
214 Xil_Out32((CF_BASEADDR + 0x418), ((pn23_pn9_n == 0x1) ? 0x10000 : 0x00000));
215 delay_ms(10);
216
217 Xil_Out32((CF_BASEADDR + 0x404), 0xff);
218 delay_ms(100);
219
220 error = 0;
221 rdata = Xil_In32(CF_BASEADDR + 0x404);
222 if (rdata != 0) {
223 error = 1;
224 }
225
226 return(error);
227 }
228
229 /***************************************************************************//**
230 * @brief ADC delay.
231 *
232 * @return 0.
233 *******************************************************************************/
234 uint32_t adc_delay(u32 no_of_lanes, uint32_t pn23_pn9_n)
235 {
236 uint32_t delay;
237 uint32_t lasterr;
238 uint32_t startdelay;
239 uint32_t stopdelay;
240
241 lasterr = 1;
242 startdelay = 32;
243 stopdelay = 31;
244
245 for (delay = 0; delay < 32; delay++) {
246 adc_delay_1(no_of_lanes, delay);
247 delay_ms(10);
248 if (adc_pn_mon(pn23_pn9_n) == 0) {
249 if (lasterr == 1) {
250 startdelay = delay;
251 }
252 lasterr = 0;
253 } else {
254 if (lasterr == 0) {
255 stopdelay = delay;
256 break;
257 }
258 lasterr = 1;
259 }
260 }
261 if (startdelay > 31) {
262 adc_delay_1(no_of_lanes, 0);
263 return(1);
264 }
265 delay = startdelay + ((stopdelay - startdelay)/2);
266 xil_printf("adc_delay: setting zero error delay (%d)\n\r", delay);
267 adc_delay_1(no_of_lanes, delay);
268
269 return(0);
270 }
271
272 /***************************************************************************//**
273 * @brief adc_set_pnsel
274 *******************************************************************************/
275 int32_t adc_set_pnsel(uint32_t ch, enum adc_pnsel sel)
276 {
277 uint32_t reg_data;
278
279 reg_data = Xil_In32(CF_BASEADDR + ADC_REG_CHAN_CNTRL_3(ch));
280 reg_data &= ~ADC_ADC_PN_SEL(~0);
281 reg_data |= ADC_ADC_PN_SEL(sel);
282 Xil_Out32(CF_BASEADDR + ADC_REG_CHAN_CNTRL_3(ch), reg_data);
283
284 return 0;
285 }
286
287 /***************************************************************************//**
288 * @brief Configures the AD9467 device to run in the selected test mode
289 * and checks if the read data pattern corresponds to the expected
290 * values for the set test mode.
291 *
292 * @param mode - The test mode. Range 0 ..7
293 * 0 - test mode off
294 * 1 - midscale short
295 * 2 - +FS short
296 * 3 - -FS short
297 * 4 - checkerboard output
298 * 5 - PN 23 sequence
299 * 6 - PN 9 sequence
300 * 7 - one/zero word toggle
301 * @param format - Sets the data format:
302 * 0 - offset binary
303 * 1 - twos complement
304 * 2 - gray code
305 *
306 * @return None.
307 *******************************************************************************/
308 void adc_test(uint32_t mode, uint32_t format)
309 {
310 uint32_t n;
311 uint32_t rdata;
312 uint32_t edata = 0;
313 uint32_t error = 0;
314
315 ad9467_output_format(format);
316 ad9467_test_mode(mode);
317 ad9467_transfer();
318 adc_capture(64, DDR_BASEADDR);
319 DisplayTestMode(mode, format);
320 if ((mode == PN_23_SEQUENCE) || (mode == PN_9_SEQUENCE))
321 {
322 if (format == TWOS_COMPLEMENT)
323 {
324 xil_printf(" Test skipped\r\n");
325 return;
326 }
327
328 adc_set_pnsel(0, ((mode == PN_23_SEQUENCE) ? ADC_PN23A : ADC_PN9));
329 delay_ms(10);
330 Xil_Out32((CF_BASEADDR + CF_REG_DATA_MONITOR),
331 CF_DATA_MONITOR_PN_ERR |
332 CF_DATA_MONITOR_PN_SYNC |
333 CF_DATA_MONITOR_PN_OVER_RNG); // write ones to clear bits
334 delay_ms(100);
335 if ((Xil_In32(CF_BASEADDR + CF_REG_DATA_MONITOR) &
336 (CF_DATA_MONITOR_PN_ERR |
337 CF_DATA_MONITOR_PN_SYNC |
338 CF_DATA_MONITOR_PN_OVER_RNG)) != 0)
339 {
340 xil_printf(" ERROR: PN status(%04x).\n\r",
341 Xil_In32(CF_BASEADDR + CF_REG_DATA_MONITOR));
342 }
343 else
344 {
345 xil_printf(" Test passed\r\n");
346 }
347 return;
348 }
349 for (n = 0; n < 32; n++)
350 {
351 rdata = Xil_In32(DDR_BASEADDR+(n*4));
352 if ((mode == MIDSCALE) && (format == OFFSET_BINARY))
353 edata = 0x80008000;
354 if ((mode == POS_FULLSCALE) && (format == OFFSET_BINARY))
355 edata = 0xffffffff;
356 if ((mode == NEG_FULLSCALE) && (format == OFFSET_BINARY))
357 edata = 0x00000000;
358 if ((mode == MIDSCALE) && (format == TWOS_COMPLEMENT))
359 edata = 0x00000000;
360 if ((mode == POS_FULLSCALE) && (format == TWOS_COMPLEMENT))
361 edata = 0x7fff7fff;
362 if ((mode == NEG_FULLSCALE) && (format == TWOS_COMPLEMENT))
363 edata = 0x80008000;
364 if (((mode == CHECKERBOARD) || (mode == ONE_ZERO_TOGGLE)) && (n == 0))
365 {
366 edata = (rdata & 0xffff);
367 }
368 if ((mode == CHECKERBOARD) && (n == 0))
369 edata = (edata == 0xaaaa) ? 0x5555aaaa : 0xaaaa5555;
370 if ((mode == ONE_ZERO_TOGGLE) && (n == 0))
371 edata = (edata == 0xffff) ? 0x0000ffff : 0xffff0000;
372 if (rdata != edata)
373 {
374 xil_printf(" ERROR[%2d]: rcv(%08x), exp(%08x)\n\r", n, rdata,
375 edata);
376 error = 1;
377 }
378 }
379 if(error == 0)
380 {
381 xil_printf(" Test passed\r\n");
382 }
383 }
384
385 /***************************************************************************//**
386 * @brief Initializes the AD9467 FPGA core.
387 *
388 * @param dco_delay - ADC delay.
389 *
390 * @return None.
391 *******************************************************************************/
392 void adc_setup(uint32_t dco_delay)
393 {
394 Xil_Out32((CF_BASEADDR + 0x040), 0x3);
395 delay_ms(10);
396
397 // setup device
398 ad9467_write(AD9467_REG_TEST_IO, 0x05); // pn23
399 ad9467_write(AD9467_REG_DEVICE_UPDATE, 0x01); // update
400 ad9467_write(AD9467_REG_DEVICE_UPDATE, 0x00);
401
402 xil_printf("AD9467[0x016]: %02x\n\r", ad9467_read(AD9467_REG_OUT_PHASE));
403 // setup adc core
404 Xil_Out32((CF_BASEADDR+0x44), 0x2); // DDR_EDGESEL active
405 Xil_Out32((CF_BASEADDR+0x400), 0x3); // pn23
406 Xil_Out32((CF_BASEADDR+0x60), 0x0); // clear Delay Control
407 Xil_Out32((CF_BASEADDR+0x60), 0x20F1F); // Setup Delay Control
408
409 delay_ms(10);
410 if (adc_delay(8, 1)) {
411 xil_printf("AD9467[0x016]: %02x\n\r", ad9467_read(0x16));
412 ad9467_write(AD9467_REG_OUT_PHASE, 0x80);
413 ad9467_write(AD9467_REG_DEVICE_UPDATE, 0x01);
414 ad9467_write(AD9467_REG_DEVICE_UPDATE, 0x00);
415 xil_printf("AD9467[0x016]: %02x\n\r", ad9467_read(0x16));
416 delay_ms(10);
417 if (adc_delay(16, 1)) {
418 xil_printf("adc_setup: can not set a zero error delay!\n\r");
419 }
420 }
421 }
422
423 /***************************************************************************//**
424 * @brief Display test mode.
425 *
426 * @param mode - Test mode.
427 * @param format - Data format.
428 *
429 * @return None.
430 *******************************************************************************/
431 void DisplayTestMode(uint32_t mode, uint32_t format)
432 {
433 char *sMode;
434 char *sFormat;
435
436 switch(format)
437 {
438 case OFFSET_BINARY:
439 sFormat = "OFFSET BINARY";
440 break;
441 case TWOS_COMPLEMENT:
442 sFormat = "TWOS_COMPLEMENT";
443 break;
444 case GRAY_CODE:
445 sFormat = "GRAY_CODE";
446 break;
447 default:
448 sFormat = "";
449 break;
450 }
451 switch(mode)
452 {
453 case TEST_DISABLE:
454 sMode = "TEST_DISABLE BINARY";
455 break;
456 case MIDSCALE:
457 sMode = "MIDSCALE";
458 break;
459 case POS_FULLSCALE:
460 sMode = "POS_FULLSCALE";
461 break;
462 case NEG_FULLSCALE:
463 sMode = "NEG_FULLSCALE BINARY";
464 break;
465 case CHECKERBOARD:
466 sMode = "CHECKERBOARD";
467 break;
468 case PN_23_SEQUENCE:
469 sMode = "PN_23_SEQUENCE";
470 break;
471 case PN_9_SEQUENCE:
472 sMode = "PN_9_SEQUENCE";
473 break;
474 case ONE_ZERO_TOGGLE:
475 sMode = "ONE_ZERO_TOGGLE";
476 break;
477 default:
478 sMode = "";
479 break;
480 }
481 xil_printf("ADC Test: mode - %s\r\n format - %s\n\r", sMode, sFormat);
482 }