Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/ad-fmcadc5-ebz/ad_fmcadc5_ebz.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ad_fmcadc2_ebz.c
3 * @brief Implementation of Main Function.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2014(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *******************************************************************************/
39
40 /******************************************************************************/
41 /***************************** Include Files **********************************/
42 /******************************************************************************/
43 #include <xparameters.h>
44 #include <xil_io.h>
45 #include "platform_drivers.h"
46 #include "ad9625.h"
47 #include "adc_core.h"
48 #include "jesd204b_gt.h"
49 #include "jesd204b_v51.h"
50
51 /******************************************************************************/
52 /********************** Macros and Constants Definitions **********************/
53 /******************************************************************************/
54 #define AD9625_CORE_0_BASEADDR XPAR_AXI_AD9625_0_CORE_BASEADDR
55 #define AD9625_CORE_1_BASEADDR XPAR_AXI_AD9625_1_CORE_BASEADDR
56 #define AD9625_DMA_BASEADDR XPAR_AXI_AD9625_DMA_BASEADDR
57 #define AD9625_JESD_0_BASEADDR XPAR_AXI_AD9625_0_JESD_BASEADDR
58 #define AD9625_JESD_1_BASEADDR XPAR_AXI_AD9625_1_JESD_BASEADDR
59 #define FMCADC5_GT_0_BASEADDR XPAR_AXI_FMCADC5_0_GT_BASEADDR
60 #define FMCADC5_GT_1_BASEADDR XPAR_AXI_FMCADC5_1_GT_BASEADDR
61 #define SPI_DEVICE_ID XPAR_SPI_0_DEVICE_ID
62 #define SPI_AD9625_0_SS 0
63 #define SPI_AD9625_1_SS 1
64 #define GPIO_DEVICE_ID XPAR_AXI_GPIO_DEVICE_ID
65 #define GPIO_OFFSET 32
66 #define GPIO_RST_0 GPIO_OFFSET + 2
67 #define GPIO_PWDN_0 GPIO_OFFSET + 3
68 #define GPIO_RST_1 GPIO_OFFSET + 6
69 #define GPIO_PWDN_1 GPIO_OFFSET + 7
70 #define GPIO_IRQ_0 GPIO_OFFSET + 8
71 #define GPIO_FD_0 GPIO_OFFSET + 9
72 #define GPIO_IRQ_1 GPIO_OFFSET + 10
73 #define GPIO_FD_1 GPIO_OFFSET + 11
74 #define GPIO_PWR_GOOD GPIO_OFFSET + 12
75
76 #define ADC_DDR_BASEADDR XPAR_AXI_DDR_CNTRL_BASEADDR + 0x800000
77
78 jesd204b_state jesd204b_st = {
79 1, // lanesync_enable
80 1, // scramble_enable
81 0, // sysref_always_enable
82 32, // frames_per_multiframe
83 1, // bytes_per_frame
84 1, // subclass
85 };
86
87 jesd204b_gt_link gt_0_link = {
88 FMCADC5_GT_0_BASEADDR, // gt_core_addr
89 JESD204B_GT_RX, // tx_or_rx
90 0, // first_lane
91 7, // last_lane
92 JESD204B_GT_QPLL, // qpll_or_cpll
93 JESD204B_GT_DFE, // lpm_or_dfe
94 625, // ref_clk
95 6250, // lane_rate
96 JESD204B_GT_SYSREF_INT, // sysref_int_or_ext
97 0, // sys_clk_sel
98 2, // out_clk_sel
99 0, // gth_or_gtx
100 };
101
102 jesd204b_gt_link gt_1_link = {
103 FMCADC5_GT_1_BASEADDR, // gt_core_addr
104 JESD204B_GT_RX, // tx_or_rx
105 0, // first_lane
106 7, // last_lane
107 JESD204B_GT_QPLL, // qpll_or_cpll
108 JESD204B_GT_DFE, // lpm_or_dfe
109 625, // ref_clk
110 6250, // lane_rate
111 JESD204B_GT_SYSREF_INT, // sysref_int_or_ext
112 0, // sys_clk_sel
113 2, // out_clk_sel
114 0, // gth_or_gtx
115 };
116
117 ad9625_init_param default_ad9625_init_param = {
118 0, // spi_chip_select
119 SPI_MODE_3, // spi_mode
120 #ifdef _XPARAMETERS_PS_H_
121 PS7_SPI, // spi_type
122 #else
123 AXI_SPI, // spi_type
124 #endif
125 SPI_DEVICE_ID, // spi_device_id;
126 };
127
128 /***************************************************************************//**
129 * @brief adc5_gpio_ctl
130 *******************************************************************************/
131 int32_t adc5_gpio_ctl(void)
132 {
133 gpio_device dev;
134 uint8_t pwr_good;
135
136 #ifdef _XPARAMETERS_PS_H_
137 dev.type = PS7_GPIO;
138 #else
139 dev.type = AXI_GPIO;
140 #endif
141 dev.device_id = GPIO_DEVICE_ID;
142
143 gpio_init(&dev);
144
145 gpio_set_direction(&dev, GPIO_RST_0, GPIO_OUT);
146 gpio_set_direction(&dev, GPIO_PWDN_0, GPIO_OUT);
147 gpio_set_direction(&dev, GPIO_RST_1, GPIO_OUT);
148 gpio_set_direction(&dev, GPIO_PWDN_1, GPIO_OUT);
149 gpio_set_direction(&dev, GPIO_IRQ_0, GPIO_IN);
150 gpio_set_direction(&dev, GPIO_FD_0, GPIO_IN);
151 gpio_set_direction(&dev, GPIO_IRQ_1, GPIO_IN);
152 gpio_set_direction(&dev, GPIO_FD_1, GPIO_IN);
153 gpio_set_direction(&dev, GPIO_PWR_GOOD, GPIO_IN);
154 gpio_set_value(&dev, GPIO_RST_0, 0);
155 gpio_set_value(&dev, GPIO_PWDN_0, 0);
156 gpio_set_value(&dev, GPIO_RST_1, 0);
157 gpio_set_value(&dev, GPIO_PWDN_1, 0);
158 mdelay(10);
159
160 gpio_get_value(&dev, GPIO_PWR_GOOD, &pwr_good);
161 if (!pwr_good) {
162 xil_printf("Error: GPIO Power Good NOT set.\n\r");
163 return -1;
164 }
165
166 gpio_set_value(&dev, GPIO_RST_0, 1);
167 gpio_set_value(&dev, GPIO_RST_1, 1);
168 mdelay(100);
169
170 return 0;
171 }
172
173 /***************************************************************************//**
174 * @brief adc5_gtlink_control
175 *******************************************************************************/
176 int32_t adc5_gtlink_control(uint32_t data)
177 {
178 int32_t n;
179
180 for (n = 7; n >= 0; n--) {
181 jesd204b_gt_txrx_write(gt_0_link, JESD204B_GT_REG_RSTN(n), data);
182 jesd204b_gt_txrx_write(gt_0_link, JESD204B_GT_REG_SYNC_CTL(n), data);
183 jesd204b_gt_txrx_write(gt_1_link, JESD204B_GT_REG_RSTN(n), data);
184 jesd204b_gt_txrx_write(gt_1_link, JESD204B_GT_REG_SYNC_CTL(n), data);
185 }
186 return(0);
187 }
188
189 /***************************************************************************//**
190 * @brief adc5_gtlink_sysref
191 *******************************************************************************/
192 int32_t adc5_gtlink_sysref(uint32_t data,
193 uint32_t status)
194 {
195 uint32_t n;
196 uint32_t rdata;
197 int32_t rstatus = 0;
198
199 jesd204b_gt_txrx_write(gt_0_link, JESD204B_GT_REG_SYSREF_CTL(0), 0x0);
200 jesd204b_gt_txrx_write(gt_0_link, JESD204B_GT_REG_SYSREF_CTL(0), data);
201 jesd204b_gt_txrx_write(gt_0_link, JESD204B_GT_REG_SYSREF_CTL(0), 0x0);
202
203 // check the status again-
204
205 for (n = 0; n < 8; n++) {
206 jesd204b_gt_read(gt_0_link, JESD204B_GT_REG_STATUS(n), &rdata);
207 if (rdata != status) {
208 rstatus = -1;
209 xil_printf("JESD204B-GT[%d,%d]: Invalid status, received(0x%05x), "
210 "expected(0x%05x)!\n", 0, n, rdata, status);
211 }
212 jesd204b_gt_read(gt_1_link, JESD204B_GT_REG_STATUS(n), &rdata);
213 if (rdata != status) {
214 rstatus = -1;
215 xil_printf("JESD204B-GT[%d,%d]: Invalid status, received(0x%05x), "
216 "expected(0x%05x)!\n", 1, n, rdata, status);
217 }
218 }
219
220 return rstatus;
221 }
222
223 /***************************************************************************//**
224 * @brief adc5_sysref_sw_calibrate
225 *******************************************************************************/
226 int32_t adc5_sysref_sw_calibrate(ad9625_dev *dev_0, ad9625_dev *dev_1)
227 {
228 uint32_t n;
229 uint32_t rgpio;
230 uint32_t rdata;
231 uint32_t rstatus = 0;
232 uint8_t rdata8_0;
233 uint8_t rdata8_1;
234
235 // reconfigure the devices so that sysref is used for synchronization
236
237 ad9625_spi_write(dev_0, 0x072, 0x8b); // CS - overrange + sysref time-stamp. (default is 0x0b)
238 ad9625_spi_write(dev_0, 0x03a, 0x02); // Sysref enabled (default is 0x00)
239 ad9625_spi_write(dev_0, 0x0ff, 0x01); // Register update
240 ad9625_spi_write(dev_1, 0x072, 0x8b); // CS - overrange + sysref time-stamp. (default is 0x0b)
241 ad9625_spi_write(dev_1, 0x03a, 0x02); // Sysref enabled (default is 0x00)
242 ad9625_spi_write(dev_1, 0x0ff, 0x01); // Register update
243
244 rgpio = Xil_In32(XPAR_GPIO_0_BASEADDR + 0x8) & 0x00ffffff;
245 Xil_Out32((XPAR_GPIO_0_BASEADDR + 0x8), (rgpio | (31<<24)));
246
247 ad9625_spi_write(dev_0, 0x13c, 0x60);
248 ad9625_spi_write(dev_0, 0x0ff, 0x01);
249
250 for (n = 0; n < 32; n++) {
251 adc5_gtlink_control(0);
252 if (adc5_gtlink_sysref(0, 0xffff) != 0) {
253 xil_printf("[%05d]: Interleaving Synchronization Failed, "
254 "Exiting!!\n", __LINE__);
255 return -1;
256 }
257
258 ad9625_spi_write(dev_0, 0x03a, 0x42);
259 ad9625_spi_write(dev_0, 0x0ff, 0x01);
260 ad9625_spi_write(dev_0, 0x03a, 0x02);
261 ad9625_spi_write(dev_0, 0x0ff, 0x01);
262
263 ad9625_spi_write(dev_1, 0x03a, 0x42);
264 ad9625_spi_write(dev_1, 0x0ff, 0x01);
265 ad9625_spi_write(dev_1, 0x03a, 0x02);
266 ad9625_spi_write(dev_1, 0x0ff, 0x01);
267
268 Xil_Out32((XPAR_GPIO_0_BASEADDR + 0x8), (rgpio | (n<<24)));
269 rdata = Xil_In32(XPAR_GPIO_0_BASEADDR + 0x8);
270 if ((rdata & 0xff000000) != ((n<<24) | 0x20000000)) {
271 xil_printf("[%05d]: SYSREF Delay Control Failed(%d, 0x%08x), Exiting!!\n", __LINE__, n, rdata);
272 return(-1);
273 }
274
275 ad9625_spi_read(dev_0, 0x100, &rdata8_0);
276 ad9625_spi_read(dev_1, 0x100, &rdata8_1);
277 if (((rdata8_0 & 0x04) != 0x00) || ((rdata8_1 & 0x04) != 0x00)) {
278 xil_printf("[%05d]: SYSREF Clear Status Failed(%d, 0x%02x, 0x%02x), Exiting!!\n", __LINE__, n, rdata8_0, rdata8_1);
279 return(-1);
280 }
281
282 adc5_gtlink_control(1);
283 if (adc5_gtlink_sysref(1, 0x1ffff) != 0) {
284 xil_printf("[%05d]: Interleaving Synchronization Failed, Exiting!!\n", __LINE__);
285 return(-1);
286 }
287
288 ad9625_spi_read(dev_0, 0x100, &rdata8_0);
289 ad9625_spi_read(dev_1, 0x100, &rdata8_1);
290
291 if ((rstatus == 1) && ((rdata8_0 & 0x04) == 0x04) && ((rdata8_1 & 0x04) == 0x00)) {
292 rstatus = 2;
293 xil_printf("SYSREF Calibration Successful[%d]\n", n);
294 break;
295 }
296
297 rstatus = 0;
298 if (((rdata8_0 & 0x04) == 0x00) && ((rdata8_1 & 0x04) == 0x00)) {
299 rstatus = 1;
300 }
301 }
302
303 if (rstatus != 2) {
304 xil_printf("SYSREF Calibration Failed!!\n");
305 return -1;
306 }
307
308 return 0;
309 }
310
311 /***************************************************************************//**
312 * @brief main
313 *******************************************************************************/
314 int main(void)
315 {
316 ad9625_dev *ad9625_0_device;
317 ad9625_dev *ad9625_1_device;
318 adc_core ad9625_0_core;
319 adc_core ad9625_1_core;
320
321 if (adc5_gpio_ctl())
322 return -1;
323
324 default_ad9625_init_param.spi_chip_select = SPI_AD9625_0_SS;
325 ad9625_setup(&ad9625_0_device, default_ad9625_init_param);
326
327 default_ad9625_init_param.spi_chip_select = SPI_AD9625_1_SS;
328 ad9625_setup(&ad9625_1_device, default_ad9625_init_param);
329
330 jesd204b_gt_initialize(gt_0_link);
331 jesd204b_setup(XPAR_AXI_AD9625_0_JESD_BASEADDR, jesd204b_st);
332 jesd204b_gt_setup(gt_0_link);
333
334 jesd204b_gt_initialize(gt_1_link);
335 jesd204b_setup(XPAR_AXI_AD9625_1_JESD_BASEADDR, jesd204b_st);
336 jesd204b_gt_setup(gt_1_link);
337
338 ad9625_0_core.adc_baseaddr = AD9625_CORE_0_BASEADDR;
339 ad9625_0_core.dmac_baseaddr = AD9625_DMA_BASEADDR;
340 ad9625_0_core.no_of_channels = 1;
341 ad9625_0_core.resolution = 12;
342
343 ad9625_1_core.adc_baseaddr = AD9625_CORE_1_BASEADDR;
344 ad9625_1_core.dmac_baseaddr = 0;
345 ad9625_1_core.no_of_channels = 1;
346 ad9625_0_core.resolution = 12;
347
348 // setup above works independently, here we need to start again- so that
349 // both cores receive sysref at the same time.
350 // reset the data path from master
351
352 adc5_sysref_sw_calibrate(ad9625_0_device, ad9625_1_device);
353
354 // data path must be active now, bring cores out of reset
355
356 adc_setup(ad9625_0_core);
357 adc_setup(ad9625_1_core);
358
359 // check prbs on both channels-
360
361 ad9625_spi_write(ad9625_0_device, AD9625_REG_TEST_CNTRL, 0x5);
362 ad9625_spi_write(ad9625_0_device, AD9625_REG_OUTPUT_MODE, 0x0);
363 ad9625_spi_write(ad9625_0_device, AD9625_REG_TRANSFER, 0x1);
364
365 ad9625_spi_write(ad9625_1_device, AD9625_REG_TEST_CNTRL, 0x5);
366 ad9625_spi_write(ad9625_1_device, AD9625_REG_OUTPUT_MODE, 0x0);
367 ad9625_spi_write(ad9625_1_device, AD9625_REG_TRANSFER, 0x1);
368
369 if (adc_pn_mon(ad9625_0_core, ADC_PN23A) != 0)
370 return -1;
371 if (adc_pn_mon(ad9625_1_core, ADC_PN23A) != 0)
372 return -1;
373
374 // no prbs errors, now sync the 2 devices
375
376 ad9625_spi_write(ad9625_0_device, AD9625_REG_TEST_CNTRL, 0x0);
377 ad9625_spi_write(ad9625_0_device, AD9625_REG_OUTPUT_MODE, 0x1);
378 ad9625_spi_write(ad9625_0_device, AD9625_REG_TRANSFER, 0x1);
379 adc_write(ad9625_0_core, ADC_REG_CHAN_CNTRL(0), 0x51);
380
381 ad9625_spi_write(ad9625_1_device, AD9625_REG_TEST_CNTRL, 0x0);
382 ad9625_spi_write(ad9625_1_device, AD9625_REG_OUTPUT_MODE, 0x1);
383 ad9625_spi_write(ad9625_1_device, AD9625_REG_TRANSFER, 0x1);
384 adc_write(ad9625_1_core, ADC_REG_CHAN_CNTRL(0), 0x51);
385
386 xil_printf("Initialization done.\n");
387
388 adc_capture(ad9625_0_core, 32768, ADC_DDR_BASEADDR);
389
390 xil_printf("Capture done.\n");
391
392 return 0;
393 }