Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/drivers/ad9739a/ad9739a.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD9739A.c
3 * @brief Implementation of AD9739A Driver.
4 * @author Bancisor Mihai
5 ********************************************************************************
6 * Copyright 2012-2015(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 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include <malloc.h>
45 #include <stdio.h>
46 #include "platform_drivers.h"
47 #include "ad9739a.h"
48
49 /******************************************************************************/
50 /********************** Macros and Constants Definitions **********************/
51 /******************************************************************************/
52 #define FDATA 2500 // for 2.5 GSPS
53
54 /***************************************************************************//**
55 * @brief Writes a value to the selected register.
56 *
57 * @param registerAddress - The address of the register to write to.
58 * @param registerValue - The value to write to the register.
59 *
60 * @return Returns 0 in case of success or negative error code.
61 *******************************************************************************/
62 int32_t ad9739a_write(ad9739a_dev *dev,
63 uint8_t registerAddress,
64 uint8_t registerValue)
65 {
66 uint8_t data[2] = {0, 0};
67
68 data[0] = AD9739A_WRITE | (0x7F & registerAddress);
69 data[1] = registerValue;
70
71 return spi_write_and_read(&dev->spi_dev, data, 2);
72 }
73
74 /***************************************************************************//**
75 * @brief Reads the value of the selected register.
76 *
77 * @param regAddress - The address of the register to read.
78 *
79 * @return registerValue - The register's value or negative error code.
80 *******************************************************************************/
81 int32_t ad9739a_read(ad9739a_dev *dev,
82 uint8_t registerAddress)
83 {
84 uint8_t txData[2] = {0, 0};
85 int32_t ret;
86
87 txData[0] = AD9739A_READ | (0x7F & registerAddress);
88
89 ret = spi_write_and_read(&dev->spi_dev, txData, 2);
90
91 return (ret < 0 ? ret : (int32_t)txData[1]);
92 }
93
94 /***************************************************************************//**
95 * @brief Resets the device.
96 *
97 * @return Returns negative error code or 0 in case of success.
98 *******************************************************************************/
99 int32_t ad9739a_reset(ad9739a_dev *dev)
100 {
101 int32_t ret;
102
103 /* Software reset to default SPI values. */
104 ret = ad9739a_write(dev, AD9739A_REG_MODE,
105 AD9739A_MODE_RESET);
106 if(ret < 0)
107 {
108 return ret;
109 }
110 ret = ad9739a_write(dev, AD9739A_REG_MODE, 0x00); // Clear the reset bit.
111
112 return ret;
113 }
114
115 /***************************************************************************//**
116 * @brief Powers down LVDS interface and TxDAC.
117 *
118 * @param pwrConfig - Selects the modules to be powered-down.
119 * Example:
120 * AD9739A_POWER_DOWN_LVDS_DRVR_PD | AD9739A_POWER_DOWN_LVDS_RCVR_PD
121 *
122 * @return Returns negative error code or 0 in case of success.
123 *******************************************************************************/
124 int32_t ad9739a_power_down(ad9739a_dev *dev,
125 uint8_t pwrConfig)
126 {
127 int32_t ret;
128 int32_t regData;
129
130 /* Write to register if pwrConfig contains valid parameters. Else read and
131 return the register value. */
132 if((pwrConfig & ((1 << 7) | (1 << 6) | (1 << 3) | (1 << 2))) == 0)
133 {
134 ret = ad9739a_write(dev, AD9739A_REG_POWER_DOWN, pwrConfig);
135 }
136 else
137 {
138 regData = ad9739a_read(dev, AD9739A_REG_POWER_DOWN);
139 return regData;
140 }
141
142 return ret;
143 }
144
145 /***************************************************************************//**
146 * @brief Sets the normal baseband mode or mix-mode.
147 *
148 * @param mode - mode of operation.
149 * Example: AD9739A_DAC_DEC_NORMAL_BASEBAND - normal baseband mode;
150 * AD9739A_DAC_DEC_MIX_MODE - mix-mode.
151 *
152 * @return Returns negative error code or 0 in case of success.
153 *******************************************************************************/
154 int32_t ad9739a_operation_mode(ad9739a_dev *dev,
155 uint8_t mode)
156 {
157 int32_t ret;
158 int32_t regData;
159
160 if((mode == AD9739A_DAC_DEC_NORMAL_BASEBAND) |
161 (mode == AD9739A_DAC_DEC_MIX_MODE))
162 {
163 ret = ad9739a_write(dev, AD9739A_REG_DEC_CNT, AD9739A_DEC_CNT_DAC_DEC(mode));
164 }
165 else
166 {
167 regData = ad9739a_read(dev, AD9739A_REG_DEC_CNT);
168 return regData;
169 }
170
171 return ret;
172 }
173
174 /***************************************************************************//**
175 * @brief Sets the full-scale output current for the DAC.
176 *
177 * @param fs_val - The desired full-scale output current. Accepted values:
178 * 8.7 to 32.7 (mA) and 0. When fs_val is set to 0 the DAC
179 * output is disabled(sleep).
180 *
181 * @return Returns the actual set full-scale current or negative error code.
182 *******************************************************************************/
183 float ad9739a_DAC_fs_current(ad9739a_dev *dev,
184 float fs_val)
185 {
186 float actual_fs = 0;
187 int16_t regData = 0;
188 int32_t ret = 0;
189
190 if((fs_val >= 8.7) && (fs_val <= 31.7))
191 {
192 regData = (int16_t)((fs_val - 8.58) / 0.0226);
193 ret = ad9739a_write(dev, AD9739A_REG_FSC_1, (regData & 0xFF));
194 if(ret < 0)
195 {
196 return (float)ret;
197 }
198 ret = ad9739a_write(dev, AD9739A_REG_FSC_2, ((regData & 0x300) >> 8));
199 if(ret < 0)
200 {
201 return (float)ret;
202 }
203 }
204 else if(fs_val == 0)
205 {
206 ret = ad9739a_write(dev, AD9739A_REG_FSC_2, AD9739A_FSC_2_Sleep);
207 if(ret < 0)
208 {
209 return (float)ret;
210 }
211 }
212 else
213 {
214 regData = ad9739a_read(dev, AD9739A_REG_FSC_1) & 0xFF;
215 if(regData < 0)
216 {
217 return -1;
218 }
219 ret = ad9739a_read(dev, AD9739A_REG_FSC_2);
220 if(ret < 0)
221 {
222 return (float)ret;
223 }
224 regData |= (ret & 0x3) << 8;
225 actual_fs = (float)(regData * 0.0226 + 8.58);
226 return actual_fs;
227 }
228
229 return (float)ret;
230 }
231
232 /***************************************************************************//**
233 * @brief Delay for a number of fdata clock cycles.
234 *
235 * @param cycles - Number of cycles to wait for.
236 *
237 * @return Returns negative error code or 0 in case of success.
238 *******************************************************************************/
239 int32_t delay_fdata_cycles(uint32_t cycles)
240 {
241 volatile uint32_t count;
242 uint32_t us;
243 uint8_t cpu_clk_period = 10; // 10 ns
244 uint32_t cpu_clks_in_a_us = 1000 / cpu_clk_period; // 100 cpu clocks in a microsecond
245 uint32_t delay;
246
247 us = (cycles/FDATA); // convert DAC cycles to microseconds delay
248 delay = ((us + 9) * cpu_clks_in_a_us); // add 9-10 microseconds to the delay as a precaution
249 for (count = 0; count < delay; count++)
250 {
251 asm("nop");
252 }
253
254 return 0;
255 }
256
257 /***************************************************************************//**
258 * @brief Initializes the AD9739A.
259 *
260 * @param spiBaseAddr - SPI peripheral AXI base address.
261 * @param ssNo - Slave select line on which the slave is connected.
262 *
263 * @return Returns negative error code or 0 in case of success.
264 *******************************************************************************/
265 int32_t ad9739a_setup(ad9739a_dev **device,
266 ad9739a_init_param init_param)
267 {
268 ad9739a_dev *dev;
269 int32_t ret = 0;
270 float fret = 0;
271 uint8_t dll_loop_lock_counter = 0;
272 uint8_t dll_loop_locked = 0;
273
274 dev = (ad9739a_dev *)malloc(sizeof(*dev));
275 if (!dev) {
276 return -1;
277 }
278
279 dev->spi_dev.chip_select = init_param.spi_chip_select;
280 dev->spi_dev.mode = init_param.spi_mode;
281 dev->spi_dev.device_id = init_param.spi_device_id;
282 dev->spi_dev.type = init_param.spi_type;
283 ret = spi_init(&dev->spi_dev);
284
285 dev->pdata = (struct ad9739a_platform_data *)malloc(sizeof(*dev->pdata));
286 if (!dev->pdata)
287 return -1;
288
289 dev->pdata->common_mode_voltage_dacclk_p =
290 init_param.common_mode_voltage_dacclk_p;
291 dev->pdata->common_mode_voltage_dacclk_n =
292 init_param.common_mode_voltage_dacclk_n;
293 dev->pdata->full_scale_current = init_param.full_scale_current;
294
295 /* Set 4-wire SPI, MSB first. */
296 ret |= ad9739a_write(dev, AD9739A_REG_MODE, 0x00);
297 if(ret < 0)
298 {
299 return ret;
300 }
301 ret = ad9739a_reset(dev);
302 if(ret < 0)
303 {
304 return ret;
305 }
306 /* Set the common-mode voltage of DACCLK_P and DACCLK_N inputs. */
307 ret = ad9739a_write(dev, AD9739A_REG_CROSS_CNT1,
308 AD9739A_CROSS_CNT1_CLKP_OFFSET(dev->pdata->common_mode_voltage_dacclk_p));
309 if(ret < 0)
310 {
311 return ret;
312 }
313 ret = ad9739a_write(dev, AD9739A_REG_CROSS_CNT2,
314 AD9739A_CROSS_CNT2_CLKN_OFFSET(dev->pdata->common_mode_voltage_dacclk_n));
315 if(ret < 0)
316 {
317 return ret;
318 }
319
320 /* MU CONTROLLER Setup*/
321 /* Phase detector enable and boost bias bits. */
322 ret = ad9739a_write(dev, AD9739A_REG_PHS_DET,
323 AD9739A_PHS_DET_CMP_BST | AD9739A_PHS_DET_PHS_DET_AUTO_EN);
324 if(ret < 0)
325 {
326 return ret;
327 }
328 ret = ad9739a_write(dev, AD9739A_REG_MU_DUTY, AD9739A_MU_DUTY_MU_DUTY_AUTO_EN);
329 if(ret < 0)
330 {
331 return ret;
332 }
333 /* Set the MU Controller search direction to UP and the target phase to 4.*/
334 ret = ad9739a_write(dev, AD9739A_REG_MU_CNT2,
335 AD9739A_MU_CNT2_SRCH_MODE(1) | AD9739A_MU_CNT2_SET_PHS(4));
336 if(ret < 0)
337 {
338 return ret;
339 }
340 /* Set the MU delay value at witch the controller begins its search to 216. */
341 ret = ad9739a_write(dev, AD9739A_REG_MU_CNT3, AD9739A_MU_CNT3_MUDEL(0x6C));
342 if(ret < 0)
343 {
344 return ret;
345 }
346 do
347 {
348 /* Set: find the exact targeted phase, retry the search until correct value is found,
349 continue if desired phase is not found, guard band set to optimal value: 0xB. */
350 ret = ad9739a_write(dev, AD9739A_REG_MU_CNT4, AD9739A_MU_CNT4_SEARCH_TOL |
351 AD9739A_MU_CNT4_RETRY | AD9739A_MU_CNT4_GUARD(0xB));
352 if(ret < 0)
353 {
354 return ret;
355 }
356 /* Set the MU controller tracking gain to the recommended value of 0x1. */
357 ret = ad9739a_write(dev, AD9739A_REG_MU_CNT1, AD9739A_MU_CNT1_GAIN(0x1));
358 if(ret < 0)
359 {
360 return ret;
361 }
362 /* Enable the MU controller. */
363 ret = ad9739a_write(dev, AD9739A_REG_MU_CNT1,
364 AD9739A_MU_CNT1_GAIN(0x1) | AD9739A_MU_CNT1_ENABLE);
365 delay_fdata_cycles(180000);
366 dll_loop_lock_counter++;
367 if(ad9739a_read(dev, AD9739A_REG_MU_STAT1) == AD9739A_MU_STAT1_MU_LKD)
368 {
369 dll_loop_locked = 1;
370 }
371 }
372 while((dll_loop_lock_counter <= 3) && (dll_loop_locked == 0));
373 if(dll_loop_locked == 0)
374 {
375 return -1;
376 }
377 /* Set FINE_DEL_SKEW to 2 for a larger DCI sampling window. */
378 ret = ad9739a_write(dev, AD9739A_REG_LVDS_REC_CNT4,
379 AD9739A_LVDS_REC_CNT4_FINE_DEL_SKEW(2) |
380 AD9739A_LVDS_REC_CNT4_DCI_DEL(0x7));
381 if(ret < 0)
382 {
383 return ret;
384 }
385 dll_loop_lock_counter = 0;
386 dll_loop_locked = 0;
387 do
388 {
389 /* Disable the data Rx controller before enabling it. */
390 ret = ad9739a_write(dev, AD9739A_REG_LVDS_REC_CNT1, 0x00);
391 if(ret < 0)
392 {
393 return ret;
394 }
395 /* Enable the data Rx controller for loop and IRQ. */
396 ret = ad9739a_write(dev, AD9739A_REG_LVDS_REC_CNT1,
397 AD9739A_LVDS_REC_CNT1_RCVR_LOOP_ON);
398 if(ret < 0)
399 {
400 return ret;
401 }
402 /* Enable the data Rx controller for search and track mode. */
403 ret = ad9739a_write(dev, AD9739A_REG_LVDS_REC_CNT1,
404 AD9739A_LVDS_REC_CNT1_RCVR_LOOP_ON | AD9739A_LVDS_REC_CNT1_RCVR_CNT_ENA);
405 if(ret < 0)
406 {
407 return ret;
408 }
409 delay_fdata_cycles(135000);
410 dll_loop_lock_counter++;
411 if(ad9739a_read(dev, AD9739A_REG_LVDS_REC_STAT9) ==
412 (AD9739A_LVDS_REC_STAT9_RCVR_LCK | AD9739A_LVDS_REC_STAT9_RCVR_TRK_ON))
413 {
414 dll_loop_locked = 1;
415 }
416 }
417 while((dll_loop_lock_counter <= 3) && (dll_loop_locked == 0));
418 if(dll_loop_locked == 0)
419 {
420 printf("AD9739A error: DLL unlocked.\n");
421 return -1;
422 }
423 fret = ad9739a_DAC_fs_current(dev, dev->pdata->full_scale_current);
424 if(fret < 0)
425 {
426 return (int32_t)fret;
427 }
428
429 *device = dev;
430
431 printf("AD9739A successfully initialized.\n");
432
433 return 0;
434 }