| 1 | /***************************************************************************//** |
| 2 | * @file cf_ad6673.c |
| 3 | * @brief Implementation of cf_ad6673 Driver. |
| 4 | ******************************************************************************** |
| 5 | * Copyright 2013(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 "xil_io.h" |
| 46 | #include "cf_ad6673.h" |
| 47 | #include "AD6673.h" |
| 48 | #include "xaxidma.h" |
| 49 | |
| 50 | /******************************************************************************/ |
| 51 | /************************ Variables Definitions *******************************/ |
| 52 | /******************************************************************************/ |
| 53 | static XAxiDma *axiDma; |
| 54 | |
| 55 | /******************************************************************************/ |
| 56 | /************************ Private Functions Prototypes ************************/ |
| 57 | /******************************************************************************/ |
| 58 | void DisplayTestMode(uint32_t mode, uint32_t format); |
| 59 | |
| 60 | /******************************************************************************/ |
| 61 | /************************ Functions Definitions *******************************/ |
| 62 | /******************************************************************************/ |
| 63 | |
| 64 | /***************************************************************************//** |
| 65 | * @brief Creates a delay of milliseconds. |
| 66 | * |
| 67 | * @param ms_count - Delay in milliseconds. |
| 68 | * |
| 69 | * @return None. |
| 70 | *******************************************************************************/ |
| 71 | void delay_ms(uint32_t ms_count) |
| 72 | { |
| 73 | volatile uint32_t count; |
| 74 | |
| 75 | for (count = 0; count < ((ms_count * CYCLES_FOR_1_MS) + 1); count++) |
| 76 | { |
| 77 | asm("nop"); |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /***************************************************************************//** |
| 82 | * @brief Initializes JESD FPGA core. |
| 83 | * |
| 84 | * @return None. |
| 85 | *******************************************************************************/ |
| 86 | void jesd_core_setup(void) |
| 87 | { |
| 88 | Xil_Out32((JESD_BASEADDR + 0x14), 0x30); // reset |
| 89 | delay_ms(100); |
| 90 | Xil_Out32((JESD_BASEADDR + JESD_REG_LANE_CTRL), |
| 91 | JESD_LANE_CTRL_SYSREF_SEL(0) | // hardware sysref generation |
| 92 | JESD_LANE_CTRL_LANE_SYNC_EN(1) | // enable lane synchronization |
| 93 | JESD_LANE_CTRL_SCR_EN(1) | // enable scrambling |
| 94 | JESD_LANE_CTRL_SYSREF_EN(0) | // disable re-alignment at every sysref pulses |
| 95 | JESD_LANE_CTRL_ERR_DISB(0)); // enable error reporting via sync |
| 96 | Xil_Out32((JESD_BASEADDR + JESD_REG_FRAMES), |
| 97 | JESD_FRAMES_FRM_CNT(32) | // 32 frames per multi-frame |
| 98 | JESD_FRAMES_BYTE_CNT(2)); // 2 bytes (octets) per frame |
| 99 | } |
| 100 | |
| 101 | /***************************************************************************//** |
| 102 | * @brief Initializes the DMA core. |
| 103 | * |
| 104 | * @return None. |
| 105 | *******************************************************************************/ |
| 106 | void dma_core_setup(void) |
| 107 | { |
| 108 | XAxiDma_Config *axiDmaCfg; |
| 109 | |
| 110 | /* DMA initialization */ |
| 111 | axiDmaCfg = XAxiDma_LookupConfig(XPAR_AXI_DMA_0_DEVICE_ID); |
| 112 | axiDmaCfg->HasMm2S = 0; |
| 113 | axiDmaCfg->HasS2Mm = 1; |
| 114 | axiDmaCfg->HasSg = 0; |
| 115 | XAxiDma_CfgInitialize(axiDma, axiDmaCfg); |
| 116 | } |
| 117 | |
| 118 | /***************************************************************************//** |
| 119 | * @brief Captures a specified number of samples from the ADC. |
| 120 | * |
| 121 | * @param size - number of bytes to read from the device |
| 122 | * @param address - capture start address |
| 123 | * |
| 124 | * @return None. |
| 125 | *******************************************************************************/ |
| 126 | void adc_capture(uint32_t size, uint32_t address) |
| 127 | { |
| 128 | XAxiDma_SimpleTransfer(axiDma, address, (size * 8), XAXIDMA_DEVICE_TO_DMA); |
| 129 | |
| 130 | Xil_Out32((CF_BASEADDR + CF_REG_CAPTURE_CTRL), |
| 131 | CF_CAPTURE_CTRL_CAPTURE_START(0)); // capture disable |
| 132 | Xil_Out32((CF_BASEADDR + CF_REG_ADC_STATUS), |
| 133 | CF_ADC_STATUS_UNDERFLOW | |
| 134 | CF_ADC_STATUS_OVERFLOW | |
| 135 | CF_ADC_STATUS_BUSY); // clear status |
| 136 | Xil_Out32((CF_BASEADDR + CF_REG_DATA_MONITOR), |
| 137 | CF_DATA_MONITOR_PN_ERR | |
| 138 | CF_DATA_MONITOR_PN_OUT_OF_SYNC | |
| 139 | CF_DATA_MONITOR_OVER_RNG); // clear status |
| 140 | Xil_Out32((CF_BASEADDR + CF_REG_CAPTURE_CTRL), |
| 141 | CF_CAPTURE_CTRL_CAPTURE_START(1) | |
| 142 | CF_CAPTURE_CTRL_CAPTURE_COUNT(size)); // capture enable |
| 143 | do |
| 144 | { |
| 145 | delay_ms(1); |
| 146 | } |
| 147 | while ((Xil_In32(CF_BASEADDR + CF_REG_ADC_STATUS) & CF_ADC_STATUS_BUSY)); |
| 148 | if ((Xil_In32(CF_BASEADDR + CF_REG_ADC_STATUS) & CF_ADC_STATUS_OVERFLOW)) |
| 149 | { |
| 150 | xil_printf("overflow occurred, capture may be corrupted\n\r"); |
| 151 | } |
| 152 | #ifdef _XPARAMETERS_PS_H_ |
| 153 | Xil_DCacheFlush(); |
| 154 | #else |
| 155 | microblaze_flush_dcache(); |
| 156 | microblaze_invalidate_dcache(); |
| 157 | #endif |
| 158 | } |
| 159 | |
| 160 | /***************************************************************************//** |
| 161 | * @brief Configures the AD6673 device to run in the selected test mode |
| 162 | * and checks if the read data pattern corresponds to the expected |
| 163 | * values for the set test mode. |
| 164 | * |
| 165 | * @param mode - The test mode. Range 0 ..7 |
| 166 | * 0 - test mode off |
| 167 | * 1 - midscale short |
| 168 | * 2 - +FS short |
| 169 | * 3 - -FS short |
| 170 | * 4 - checkerboard output |
| 171 | * 5 - PN 23 sequence |
| 172 | * 6 - PN 9 sequence |
| 173 | * 7 - one/zero word toggle |
| 174 | * @param format - Sets the data format: |
| 175 | * 0 - offset binary |
| 176 | * 1 - twos complement |
| 177 | * |
| 178 | * @return None. |
| 179 | *******************************************************************************/ |
| 180 | void adc_test(uint32_t mode, uint32_t format) |
| 181 | { |
| 182 | uint32_t i; |
| 183 | uint32_t n; |
| 184 | uint32_t error = 0; |
| 185 | uint32_t rdata_2; |
| 186 | uint32_t rdata[2]; |
| 187 | uint32_t edata[2]; |
| 188 | |
| 189 | edata[0] = 0; |
| 190 | edata[1] = 0; |
| 191 | ad6673_output_format(format); |
| 192 | ad6673_test_mode(mode); |
| 193 | ad6673_transfer(); |
| 194 | adc_capture(16, DDR_BASEADDR); |
| 195 | DisplayTestMode(mode, format); |
| 196 | |
| 197 | for (n = 0; n < 32; n++) |
| 198 | { |
| 199 | rdata_2 = Xil_In32(DDR_BASEADDR + (n * 4)); |
| 200 | rdata[0] = (rdata_2 >> 3) & 0xffff; |
| 201 | rdata[1] = (rdata_2 >> 19) & 0xffff; |
| 202 | for (i = 0; i < 2; i++) |
| 203 | { |
| 204 | if ((mode == MIDSCALE) && (format == OFFSET_BINARY)) |
| 205 | { |
| 206 | edata[i] = 0x0400; |
| 207 | } |
| 208 | if ((mode == POS_FULLSCALE) && (format == OFFSET_BINARY)) |
| 209 | { |
| 210 | edata[i] = 0x07ff; |
| 211 | } |
| 212 | if ((mode == NEG_FULLSCALE) && (format == OFFSET_BINARY)) |
| 213 | { |
| 214 | edata[i] = 0x0000; |
| 215 | } |
| 216 | if ((mode == MIDSCALE) && (format == TWOS_COMPLEMENT)) |
| 217 | { |
| 218 | edata[i] = 0x0000; |
| 219 | } |
| 220 | if ((mode == POS_FULLSCALE) && (format == TWOS_COMPLEMENT)) |
| 221 | { |
| 222 | edata[i] = 0x03ff; |
| 223 | } |
| 224 | if ((mode == NEG_FULLSCALE) && (format == TWOS_COMPLEMENT)) |
| 225 | { |
| 226 | edata[i] = 0x0400; |
| 227 | } |
| 228 | if (((mode == CHECKERBOARD) || (mode == ONE_ZERO_TOGGLE)) && (n == 0)) |
| 229 | { |
| 230 | edata[i] = (rdata[i] & 0xffff); |
| 231 | } |
| 232 | if ((mode == CHECKERBOARD) && (n > 0)) |
| 233 | { |
| 234 | edata[i] = (edata[i] == 0x02aa) ? 0x0555 : 0x02aa; |
| 235 | } |
| 236 | if ((mode == ONE_ZERO_TOGGLE) && (n > 0)) |
| 237 | { |
| 238 | edata[i] = (edata[i] == 0x07ff) ? 0x0000 : 0x07ff; |
| 239 | } |
| 240 | if (rdata[i] != edata[i]) |
| 241 | { |
| 242 | xil_printf(" ERROR[%2d]: ch(%d), rcv(%08x), exp(%08x)\n\r", n, i, rdata[i], edata[i]); |
| 243 | error = 1; |
| 244 | } |
| 245 | } |
| 246 | } |
| 247 | if (error == 0) |
| 248 | { |
| 249 | xil_printf(" Test passed.\n\r"); |
| 250 | } |
| 251 | } |
| 252 | |
| 253 | void DisplayTestMode(uint32_t mode, uint32_t format) |
| 254 | { |
| 255 | char *sMode; |
| 256 | char *sFormat; |
| 257 | |
| 258 | switch(format) |
| 259 | { |
| 260 | case OFFSET_BINARY: |
| 261 | sFormat = "OFFSET BINARY"; |
| 262 | break; |
| 263 | case TWOS_COMPLEMENT: |
| 264 | sFormat = "TWOS_COMPLEMENT"; |
| 265 | break; |
| 266 | default: |
| 267 | sFormat = ""; |
| 268 | break; |
| 269 | } |
| 270 | switch(mode) |
| 271 | { |
| 272 | case TEST_DISABLE: |
| 273 | sMode = "TEST_DISABLE BINARY"; |
| 274 | break; |
| 275 | case MIDSCALE: |
| 276 | sMode = "MIDSCALE"; |
| 277 | break; |
| 278 | case POS_FULLSCALE: |
| 279 | sMode = "POS_FULLSCALE"; |
| 280 | break; |
| 281 | case NEG_FULLSCALE: |
| 282 | sMode = "NEG_FULLSCALE BINARY"; |
| 283 | break; |
| 284 | case CHECKERBOARD: |
| 285 | sMode = "CHECKERBOARD"; |
| 286 | break; |
| 287 | case PN_23_SEQUENCE: |
| 288 | sMode = "PN_23_SEQUENCE"; |
| 289 | break; |
| 290 | case PN_9_SEQUENCE: |
| 291 | sMode = "PN_9_SEQUENCE"; |
| 292 | break; |
| 293 | case ONE_ZERO_TOGGLE: |
| 294 | sMode = "ONE_ZERO_TOGGLE"; |
| 295 | break; |
| 296 | default: |
| 297 | sMode = ""; |
| 298 | break; |
| 299 | } |
| 300 | xil_printf("ADC Test: mode - %s\r\n format - %s\n\r", sMode, sFormat); |
| 301 | } |