Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/fmcomms1/AD9122/AD9122.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file AD9122.c
3 * @brief Implementation of AD9122 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2012(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 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 #include "spi_interface.h"
47 #include "dac_core.h"
48 #include "cf_axi_dds.h"
49 #include "mmcm.h"
50 #include "AD9122.h"
51
52 struct cf_axi_converter dds_conv;
53
54 extern void delay_us(uint32_t us_count);
55
56 int64_t (*pfnSetDataClk)(int64_t Hz);
57 int64_t (*pfnSetDacClk)(int64_t Hz);
58 int64_t (*pfnSetRefClk)(int64_t Hz);
59 uint32_t (*pfnRoundRateDataClk)(int32_t rate);
60 uint32_t (*pfnRoundRateDacClk)(int32_t rate);
61 uint32_t (*pfnRoundRateRefClk)(int32_t rate);
62
63 /******************************************************************************/
64 /***************************** Local Types and Variables***********************/
65 /******************************************************************************/
66 static const uint32_t ad9122_reg_defaults[][2] =
67 {
68 {AD9122_REG_COMM, 0x00},
69 {AD9122_REG_COMM, AD9122_COMM_RESET},
70 {AD9122_REG_COMM, 0x00},
71 {AD9122_REG_POWER_CTRL, 0},
72 {AD9122_REG_DATA_FORMAT, AD9122_DATA_FORMAT_BINARY},
73 {AD9122_REG_INTERRUPT_EN_1, 0x00},
74 {AD9122_REG_INTERRUPT_EN_2, 0x00},
75 {AD9122_REG_CLK_REC_CTRL, AD9122_CLK_REC_CTRL_DACCLK_CROSS_CORRECTION |
76 AD9122_CLK_REC_CTRL_REFCLK_CROSS_CORRECTION |
77 0xF},
78 {AD9122_REG_PLL_CTRL_1, AD9122_PLL_CTRL_1_PLL_MANUAL_EN},
79 {AD9122_REG_PLL_CTRL_2, AD9122_PLL_CTRL_2_PLL_LOOP_BANDWIDTH(3) |
80 AD9122_PLL_CTRL_2_PLL_CHARGE_PUMP_CURRENT(0x11)},
81 {AD9122_REG_PLL_CTRL_3, AD9122_PLL_CTRL_3_N2(3) |
82 AD9122_PLL_CTRL_3_PLL_CROSS_CTRL_EN |
83 AD9122_PLL_CTRL_3_N0(1) |
84 AD9122_PLL_CTRL_3_N1(2)},
85 {AD9122_REG_SYNC_CTRL_1, AD9122_SYNC_CTRL_1_DATA_FIFO_RATE_TOGGLE |
86 AD9122_SYNC_CTRL_1_RISING_EDGE_SYNC},
87 {AD9122_REG_SYNC_CTRL_2, 0x00},
88 {AD9122_REG_DCI_DELAY, 0x00},
89 {AD9122_REG_FIFO_CTRL, AD9122_FIFO_CTRL_FIFO_PHA_OFFSET(4)},
90 {AD9122_REG_FIFO_STATUS_1, 0x00},
91 {AD9122_REG_DATAPATH_CTRL, AD9122_DATAPATH_CTRL_BYPASS_PREMOD |
92 AD9122_DATAPATH_CTRL_BYPASS_INV_SINC |
93 AD9122_DATAPATH_CTRL_BYPASS_NCO},
94 {AD9122_REG_HB1_CTRL, AD9122_HB1_CTRL_BYPASS_HB1},
95 {AD9122_REG_HB2_CTRL, AD9122_HB2_CTRL_BYPASS_HB2},
96 {AD9122_REG_HB3_CTRL, AD9122_HB3_CTRL_BYPASS_HB3},
97 {AD9122_REG_FTW_7_0, 0x00},
98 {AD9122_REG_FTW_15_8, 0x00},
99 {AD9122_REG_FTW_23_16, 0x00},
100 {AD9122_REG_FTW_31_24, 0x00},
101 {AD9122_REG_NCO_PHA_OFFSET_LSB, 0x00},
102 {AD9122_REG_NCO_PHA_OFFSET_MSB, 0x00},
103 {AD9122_REG_NCO_FTW_UPDATE, 0x00},
104 {AD9122_REG_I_PHA_ADJ_LSB, 0x00},
105 {AD9122_REG_I_PHA_ADJ_MSB, 0x00},
106 {AD9122_REG_Q_PHA_ADJ_LSB, 0x00},
107 {AD9122_REG_Q_PHA_ADJ_MSB, 0x00},
108 {AD9122_REG_I_DAC_OFFSET_LSB, 0x00},
109 {AD9122_REG_I_DAC_OFFSET_MSB, 0x00},
110 {AD9122_REG_Q_DAC_OFFSET_LSB, 0x00},
111 {AD9122_REG_Q_DAC_OFFSET_MSB, 0x00},
112 {AD9122_REG_I_DAC_FS_ADJ, 0xF9},
113 {AD9122_REG_I_DAC_CTRL, 0x01},
114 {AD9122_REG_I_AUX_DAC_DATA, 0x00},
115 {AD9122_REG_I_AUX_DAC_CTRL, 0x00},
116 {AD9122_REG_Q_DAC_FS_ADJ, 0xF9},
117 {AD9122_REG_Q_DAC_CTRL, 0x01},
118 {AD9122_REG_Q_AUX_DAC_DATA, 0x00},
119 {AD9122_REG_Q_AUX_DAC_CTRL, 0x00},
120 {AD9122_REG_DIE_TEMP_RANGE_CTRL, AD9122_DIE_TEMP_RANGE_CTRL_REF_CURRENT(1)},
121 {AD9122_REG_FIFO_STATUS_1, AD9122_FIFO_STATUS_1_FIFO_SOFT_ALIGN_REQ},
122 };
123
124 struct ad9122_sed {
125 unsigned short i0;
126 unsigned short q0;
127 unsigned short i1;
128 unsigned short q1;
129 };
130
131 static struct ad9122_sed dac_sed_pattern[5] = {
132 {
133 0x5555,
134 0xAAAA,
135 0x5555,
136 0xAAAA
137 },
138 {
139 0,
140 0xFFFF,
141 0,
142 0xFFFF
143 },
144 {
145 0,
146 0,
147 0,
148 0
149 },
150 {
151 0xFFFF,
152 0xFFFF,
153 0xFFFF,
154 0xFFFF
155 },
156 {
157 0x1248,
158 0xEDC7,
159 0xEDC7,
160 0x1248
161 }
162 };
163
164 /******************************************************************************/
165 /***************************** Delay Functions ********************************/
166 /******************************************************************************/
167 extern void delay_us(uint32_t us_count);
168 #define mdelay(x) delay_us(x*1000)
169 #define msleep(x) delay_us(x*1000)
170
171 /******************************************************************************/
172 /***************************** Macros *****************************************/
173 /******************************************************************************/
174 #define test_bit(bit, val) \
175 (((*val) & (1 << bit)) != 0)
176 #define set_bit(bit, val) \
177 (*val |= (1 << bit))
178 #define sign_extend32(value, index) \
179 ((int32_t)(value << (31-index)) >> (31-index))
180 #define ARRAY_SIZE(x) \
181 sizeof(x) / sizeof(x[0])
182
183 /***************************************************************************//**
184 * @brief Writes a value to the selected register.
185 *
186 * @param registerAddress - The address of the register to write to.
187 * @param registerValue - The value to write to the register.
188 *
189 * @return None.
190 *******************************************************************************/
191 int32_t ad9122_write(uint8_t registerAddress,
192 uint8_t registerValue)
193 {
194 uint8_t regAddr = 0;
195
196 regAddr = (0x7F & registerAddress);
197 return SPI_Write(SPI_SEL_AD9122, regAddr, registerValue);
198 }
199
200 /***************************************************************************//**
201 * @brief Reads the value of the selected register.
202 *
203 * @param regAddress - The address of the register to read.
204 *
205 * @return registerValue - The register's value or negative error code.
206 *******************************************************************************/
207 int32_t ad9122_read(uint8_t registerAddress)
208 {
209 uint8_t regAddr = 0;
210 uint32_t registerValue = 0;
211 int32_t ret;
212
213 regAddr = 0x80 + (0x7F & registerAddress);
214 ret = SPI_Read(SPI_SEL_AD9122, regAddr, &registerValue);
215
216 return (ret < 0 ? ret : (int32_t)registerValue);
217 }
218
219 /***************************************************************************//**
220 * @brief Finds the DCI.
221 *
222 * @return Returns the DCI value
223 *******************************************************************************/
224 static int32_t ad9122_find_dci(uint32_t *err_field, uint32_t entries)
225 {
226 int32_t dci, cnt, start, max_start, max_cnt;
227 int32_t ret;
228 int32_t valid_dci = 0;
229 #ifdef DEBUG_DCI
230 int8_t str[33];
231 #endif
232
233 for(dci = 0, cnt = 0, max_cnt = 0, start = -1, max_start = 0;
234 dci < (int32_t)entries; dci++) {
235 if (test_bit(dci, err_field) == 0) {
236 valid_dci = 1;
237 if (start == -1)
238 start = dci;
239 cnt++;
240 #ifdef DEBUG_DCI
241 str[dci] = 'o';
242 #endif
243 } else {
244 if (cnt > max_cnt) {
245 max_cnt = cnt;
246 max_start = start;
247 }
248 start = -1;
249 cnt = 0;
250 #ifdef DEBUG_DCI
251 str[dci] = '-';
252 #endif
253 }
254 }
255 #ifdef DEBUG_DCI
256 str[dci] = 0;
257 #endif
258 if (cnt > max_cnt) {
259 max_cnt = cnt;
260 max_start = start;
261 }
262
263
264 ret = max_start + ((max_cnt - 1) / 2);
265
266 #ifdef DEBUG_DCI
267 str[ret] = '|';
268 xil_printf("%s DCI %d\n",str, ret);
269 #endif
270 if(valid_dci == 1)
271 {
272 return ret;
273 }
274 else
275 {
276 return -1;
277 }
278 }
279
280 /***************************************************************************//**
281 * @brief Calibrates the AD9122 DCI.
282 *
283 * @return Returns negative error code or 0 in case of success.
284 *******************************************************************************/
285 int32_t ad9122_tune_dci(struct cf_axi_converter *conv)
286 {
287 uint32_t reg;
288 int32_t i = 0, dci;
289 uint32_t err_bfield = 0;
290
291 for (dci = 0; dci < 4; dci++) {
292 ad9122_write(AD9122_REG_DCI_DELAY, dci);
293 for (i = 0; i < ARRAY_SIZE(dac_sed_pattern); i++) {
294
295 ad9122_write(AD9122_REG_SED_CTRL, 0);
296
297 if(conv->pcore_set_sed_pattern)
298 {
299 conv->pcore_set_sed_pattern(0,
300 dac_sed_pattern[i].i0, dac_sed_pattern[i].i1);
301 conv->pcore_set_sed_pattern(1,
302 dac_sed_pattern[i].q0, dac_sed_pattern[i].q1);
303 }
304
305 ad9122_write(AD9122_REG_COMPARE_I0_LSBS,
306 dac_sed_pattern[i].i0 & 0xFF);
307 ad9122_write(AD9122_REG_COMPARE_I0_MSBS,
308 dac_sed_pattern[i].i0 >> 8);
309
310 ad9122_write(AD9122_REG_COMPARE_Q0_LSBS,
311 dac_sed_pattern[i].q0 & 0xFF);
312 ad9122_write(AD9122_REG_COMPARE_Q0_MSBS,
313 dac_sed_pattern[i].q0 >> 8);
314
315 ad9122_write(AD9122_REG_COMPARE_I1_LSBS,
316 dac_sed_pattern[i].i1 & 0xFF);
317 ad9122_write(AD9122_REG_COMPARE_I1_MSBS,
318 dac_sed_pattern[i].i1 >> 8);
319
320 ad9122_write(AD9122_REG_COMPARE_Q1_LSBS,
321 dac_sed_pattern[i].q1 & 0xFF);
322 ad9122_write(AD9122_REG_COMPARE_Q1_MSBS,
323 dac_sed_pattern[i].q1 >> 8);
324
325 ad9122_write(AD9122_REG_SED_CTRL,
326 AD9122_SED_CTRL_SED_COMPARE_EN);
327
328 ad9122_write(AD9122_REG_EVENT_FLAG_2,
329 AD9122_EVENT_FLAG_2_AED_COMPARE_PASS |
330 AD9122_EVENT_FLAG_2_AED_COMPARE_FAIL |
331 AD9122_EVENT_FLAG_2_SED_COMPARE_FAIL);
332
333 ad9122_write(AD9122_REG_SED_CTRL,
334 AD9122_SED_CTRL_SED_COMPARE_EN |
335 AD9122_SED_CTRL_AUTOCLEAR_EN);
336
337 msleep(100);
338 reg = ad9122_read(AD9122_REG_SED_CTRL);
339 if(!(reg & (AD9122_SED_CTRL_SAMPLE_ERR_DETECTED | AD9122_SED_CTRL_COMPARE_PASS)))
340 {
341 return -1;
342 }
343 if (reg & AD9122_SED_CTRL_SAMPLE_ERR_DETECTED)
344 set_bit(dci, &err_bfield);
345 }
346 }
347 dci = ad9122_find_dci(&err_bfield, 4);
348 if(dci < 0)
349 {
350 return -1;
351 }
352 ad9122_write(AD9122_REG_DCI_DELAY, dci);
353 ad9122_write(AD9122_REG_SED_CTRL, 0);
354
355 return 0;
356 }
357
358 /***************************************************************************//**
359 * @brief Gets the AD9122 FIFO status.
360 *
361 * @return Returns negative error code in case of FIFO warning
362 * or 0 in case of success.
363 *******************************************************************************/
364 int32_t ad9122_get_fifo_status(struct cf_axi_converter *conv)
365 {
366 uint32_t stat;
367
368 stat = ad9122_read(AD9122_REG_SYNC_STATUS_1);
369 if (!(stat & AD9122_SYNC_STATUS_1_SYNC_LOCKED))
370 return -1;
371
372 stat = ad9122_read(AD9122_REG_FIFO_STATUS_1);
373 if (stat & (AD9122_FIFO_STATUS_1_FIFO_WARNING_1 |
374 AD9122_FIFO_STATUS_1_FIFO_WARNING_2))
375 return -1;
376
377 return 0;
378 }
379
380 /***************************************************************************//**
381 * @brief Waits for the AD9122 to sync.
382 *
383 * @return Returns negative error code or 0 in case of success.
384 *******************************************************************************/
385 int32_t ad9122_sync()
386 {
387 int32_t ret, timeout;
388
389 timeout = 255;
390 do
391 {
392 delay_us(1000);
393 ret = ad9122_read(AD9122_REG_FIFO_STATUS_1);
394 if (ret < 0)
395 return ret;
396
397 } while (timeout-- && !(ret & AD9122_FIFO_STATUS_1_FIFO_SOFT_ALIGN_ACK));
398
399 ad9122_write(AD9122_REG_FIFO_STATUS_1, 0x0);
400 ad9122_write(AD9122_REG_SYNC_CTRL_1,
401 AD9122_SYNC_CTRL_1_SYNC_EN |
402 AD9122_SYNC_CTRL_1_RISING_EDGE_SYNC);
403
404 timeout = 255;
405 do
406 {
407 delay_us(1000);
408 ret = ad9122_read(AD9122_REG_SYNC_STATUS_1);
409 if (ret < 0)
410 return ret;
411
412 } while (timeout-- && !(ret & AD9122_SYNC_STATUS_1_SYNC_LOCKED));
413
414 return 0;
415 }
416
417 /***************************************************************************//**
418 * @brief Returns the value of the data clock.
419 *
420 * @param conv - Pointer to a cf_axi_converter struct which contains the
421 * data clock value
422 *
423 * @return eturns the value of the data clock.
424 *******************************************************************************/
425 static uint32_t ad9122_get_data_clk(struct cf_axi_converter *conv)
426 {
427 return conv->clk[CLK_DATA];
428 }
429
430 /***************************************************************************//**
431 * @brief Updates the available interpolation frequencies list.
432 *
433 * @param conv - Pointer to a cf_axi_converter struct
434 * @param dat_freq - Data clock frequency in Hz
435 *
436 * @return None.
437 *******************************************************************************/
438 static void ad9122_update_avail_intp_modes(struct cf_axi_converter *conv,
439 uint32_t dat_freq)
440 {
441 uint32_t dac_freq;
442 int32_t r_dac_freq;
443 int32_t intp, i;
444
445 for (i = 0, intp = 1; intp <= 8; intp *= 2) {
446 dac_freq = dat_freq * intp;
447 if (dac_freq > AD9122_MAX_DAC_RATE) {
448 break;
449 }
450 r_dac_freq = pfnRoundRateDacClk(dac_freq);
451 if (r_dac_freq != dac_freq)
452 continue;
453 else
454 conv->intp_modes[i++] = dac_freq;
455 }
456
457 conv->intp_modes[i] = 0;
458 }
459
460 /***************************************************************************//**
461 * @brief Updates the available center frequencies list.
462 *
463 * @param conv - Pointer to a cf_axi_converter struct
464 * @param freq - Data clock value
465 *
466 * @return Returns negative error code or 0 in case of success.
467 *******************************************************************************/
468 static void ad9122_update_avail_fcent_modes(struct cf_axi_converter *conv,
469 uint32_t dat_freq)
470 {
471 uint32_t i;
472
473 if (conv->interp_factor == 1) {
474 conv->cs_modes[0] = 0;
475 conv->cs_modes[1] = -1;
476 return;
477 }
478
479 for (i = 0; i < (conv->interp_factor * 2); i++) {
480 conv->cs_modes[i] = (dat_freq * i) / 2;
481 }
482
483 conv->cs_modes[i] = -1;
484 }
485
486 /***************************************************************************//**
487 * @brief Sets the value of the data clock.
488 *
489 * @param conv - Pointer to a cf_axi_converter struct
490 * @param freq - Data clock value
491 *
492 * @return Returns negative error code or 0 in case of success.
493 *******************************************************************************/
494 static int32_t ad9122_set_data_clk(struct cf_axi_converter *conv, uint32_t freq)
495 {
496 uint32_t dac_freq;
497 int32_t dat_freq, r_dac_freq, r_ref_freq;
498 int64_t ret;
499
500 dat_freq = pfnRoundRateDataClk(freq);
501 if (dat_freq < 0 || dat_freq > AD9122_MAX_DAC_RATE) {
502 #ifdef CLK_DEBUG
503 xil_printf("CLK_DATA: Error or requested rate exceeds maximum %ld (%lu)",
504 dat_freq, AD9122_MAX_DAC_RATE);
505 #endif
506 return -1;
507 }
508
509 dac_freq = dat_freq * conv->interp_factor;
510 if (dac_freq > AD9122_MAX_DAC_RATE) {
511 #ifdef CLK_DEBUG
512 xil_printf("CLK_DAC: Requested Rate exceeds maximum %lu (%lu)",
513 dac_freq, AD9122_MAX_DAC_RATE);
514 #endif
515 return -1;
516 }
517
518 r_dac_freq = pfnRoundRateDacClk(dac_freq);
519 if (r_dac_freq != dac_freq) {
520 #ifdef CLK_DEBUG
521 xil_printf("CLK_DAC: Requested Rate mismatch %ld (%lu)",
522 r_dac_freq, dac_freq);
523 #endif
524 return -1;
525 }
526
527 r_ref_freq = pfnRoundRateRefClk(dat_freq / 8);
528 if (r_ref_freq != (dat_freq / 8)) {
529 #ifdef CLK_DEBUG
530 xil_printf("CLK_REF: Requested Rate exceeds mismatch %ld (%lu)",
531 r_ref_freq, (dat_freq / 8));
532 #endif
533 return -1;
534 }
535
536 ret = pfnSetDataClk(dat_freq);
537 if(ret < 0)
538 return (int32_t)ret;
539 conv->clk[CLK_DATA] = (uint32_t)ret;
540 mmcm_set_rate(conv->clk[CLK_DATA], conv->clk[CLK_DATA]);
541
542 ret = pfnSetRefClk(r_ref_freq);
543 if(ret < 0)
544 return (int32_t)ret;
545 conv->clk[CLK_REF] = (uint32_t)ret;
546
547 ret = pfnSetDacClk(dac_freq);
548 if(ret < 0)
549 return (int32_t)ret;
550 conv->clk[CLK_DAC] = (uint32_t)ret;
551
552 ad9122_update_avail_fcent_modes(conv, dat_freq);
553 ad9122_update_avail_intp_modes(conv, dat_freq);
554
555 return 0;
556 }
557
558 /***************************************************************************//**
559 * @brief Validates the interpolation factor
560 *
561 * @param fact - Interpolation factor
562 *
563 * @return Returns a valid interpolation factor.
564 *******************************************************************************/
565 static uint32_t ad9122_validate_interp_factor(unsigned fact)
566 {
567 switch (fact)
568 {
569 case 1:
570 case 2:
571 case 4:
572 case 8:
573 return fact;
574 default:
575 return 1;
576 }
577 }
578
579 /***************************************************************************//**
580 * @brief Sets the interpolation factor and the center shift frequency.
581 *
582 * @param conv - Pointer to a cf_axi_converter struct.
583 * @param interp - Interpolation factor
584 * @param fcent_shift - Center frequency shift as a multiplier of fData / 2.
585 * The shift values should be in the range [0, 15]
586 * @param data_rate - Data rate in Hz
587 *
588 * @return Returns negative error code or 0 in case of success.
589 *******************************************************************************/
590 static int32_t ad9122_set_interpol(struct cf_axi_converter *conv,
591 uint32_t interp,
592 uint32_t fcent_shift,
593 uint32_t data_rate)
594 {
595 uint32_t hb1, hb2, hb3, tmp;
596 int32_t ret, cached;
597
598 hb1 = AD9122_HB1_CTRL_BYPASS_HB1;
599 hb2 = AD9122_HB2_CTRL_BYPASS_HB2;
600 hb3 = AD9122_HB3_CTRL_BYPASS_HB3;
601
602 switch (interp) {
603 case 1:
604 break;
605 case 2:
606 if (fcent_shift > 3)
607 return -1;
608 hb1 = AD9122_HB1_INTERP(fcent_shift);
609 break;
610 case 4:
611 if (fcent_shift > 7)
612 return -1;
613 hb1 = AD9122_HB1_INTERP(fcent_shift % 4);
614 hb2 = AD9122_HB23_INTERP(fcent_shift);
615 break;
616 case 8:
617 if (fcent_shift > 15)
618 return -1;
619 hb1 = AD9122_HB1_INTERP(fcent_shift % 4);
620 hb2 = AD9122_HB23_INTERP(fcent_shift % 8);
621 hb3 = AD9122_HB23_INTERP(fcent_shift / 2);
622 break;
623 default:
624 return -1;
625 }
626
627 cached = conv->interp_factor;
628 conv->interp_factor = interp;
629 ret = ad9122_set_data_clk(conv, data_rate ?
630 data_rate : ad9122_get_data_clk(conv));
631
632 if (ret < 0) {
633 conv->interp_factor = cached;
634
635 return ret;
636 }
637
638 tmp = ad9122_read(AD9122_REG_DATAPATH_CTRL);
639 switch (hb1) {
640 case AD9122_HB1_INTERP(1):
641 case AD9122_HB1_INTERP(3):
642 tmp &= ~AD9122_DATAPATH_CTRL_BYPASS_PREMOD;
643 break;
644 default:
645 tmp |= AD9122_DATAPATH_CTRL_BYPASS_PREMOD;
646 }
647
648 ad9122_write(AD9122_REG_DATAPATH_CTRL, tmp);
649 ad9122_write(AD9122_REG_HB1_CTRL, hb1);
650 ad9122_write(AD9122_REG_HB2_CTRL, hb2);
651 ad9122_write(AD9122_REG_HB3_CTRL, hb3);
652 conv->fcenter_shift = fcent_shift;
653
654 return 0;
655 }
656 /***************************************************************************//**
657 * @brief Sets the interpolation frequency.
658 *
659 * @param conv - Pointer to a cf_axi_converter struct.
660 * @param freq - Interpolation frequency in Hz
661 *
662 * @return Returns negative error code or 0 in case of success.
663 *******************************************************************************/
664 static int32_t ad9122_set_interpol_freq(struct cf_axi_converter *conv,
665 uint32_t freq)
666 {
667 uint32_t dat_freq;
668 int32_t ret;
669
670 dat_freq = ad9122_get_data_clk(conv);
671 ret = ad9122_set_interpol(conv, freq / dat_freq,
672 conv->fcenter_shift, 0);
673 ad9122_update_avail_fcent_modes(conv, dat_freq);
674
675 return ret;
676 }
677
678 /***************************************************************************//**
679 * @brief Sets the interpolation center frequency shift.
680 *
681 * @param conv - Pointer to a cf_axi_converter struct.
682 * @param freq - Center frequency shift in Hz
683 *
684 * @return Returns negative error code or 0 in case of success.
685 *******************************************************************************/
686 static int32_t ad9122_set_interpol_fcent_freq(struct cf_axi_converter *conv,
687 uint32_t freq)
688 {
689 return ad9122_set_interpol(conv, conv->interp_factor,
690 (freq * 2) / ad9122_get_data_clk(conv), 0);
691 }
692
693 /***************************************************************************//**
694 * @brief Returns the interpolation frequency.
695 *
696 * @param conv - Pointer to a cf_axi_converter struct.
697 *
698 * @return Returns negative error code or the interpolation frequency.
699 *******************************************************************************/
700 static uint32_t ad9122_get_interpol_freq(struct cf_axi_converter *conv)
701 {
702 return ad9122_get_data_clk(conv) * conv->interp_factor;
703 }
704
705 /***************************************************************************//**
706 * @brief Returns the center frequency shift.
707 *
708 * @param conv - Pointer to a cf_axi_converter struct.
709 *
710 * @return Returns negative error code or the center frequency shift.
711 *******************************************************************************/
712 static uint32_t ad9122_get_interpol_fcent_freq(struct cf_axi_converter *conv)
713 {
714 return (ad9122_get_data_clk(conv) * conv->fcenter_shift) / 2;
715 }
716
717 /***************************************************************************//**
718 * @brief Writes data to the AD9122.
719 *
720 * @return Returns negative error code or the written value in case of success.
721 *******************************************************************************/
722 uint32_t ad9122_store(uint32_t address, int32_t readin)
723 {
724 int32_t ret = 0;
725
726 switch (address) {
727 case AD9122_REG_I_DAC_OFFSET_MSB:
728 case AD9122_REG_Q_DAC_OFFSET_MSB:
729 if (readin < 0 || readin > 0xFFFF) {
730 ret = -1;
731 goto out;
732 }
733 break;
734 case AD9122_REG_I_PHA_ADJ_MSB:
735 case AD9122_REG_Q_PHA_ADJ_MSB:
736 if (readin < -512 || readin > 511) {
737 ret = -1;
738 goto out;
739 }
740 break;
741 default:
742 if (readin < 0 || readin > 0x3FF) {
743 ret = -1;
744 goto out;
745 }
746 break;
747 }
748
749 ret = ad9122_write((uint32_t)address, readin >> 8);
750 if (ret < 0)
751 goto out;
752
753 ret = ad9122_write((uint32_t)address - 1, readin & 0xFF);
754 if (ret < 0)
755 goto out;
756
757 out:
758
759 return ret ? ret : readin;
760 }
761
762 /***************************************************************************//**
763 * @brief Reads data from the AD9122.
764 *
765 * @return Returns negative error code or 0 in case of success.
766 *******************************************************************************/
767 uint32_t ad9122_show(uint32_t address, int32_t* val)
768 {
769 int32_t ret = 0;
770
771 ret = ad9122_read((uint32_t)address);
772 if (ret < 0)
773 goto out;
774 *val = ret << 8;
775
776 ret = ad9122_read((uint32_t)address - 1);
777 if (ret < 0)
778 goto out;
779 *val |= ret & 0xFF;
780
781 switch ((uint32_t)address) {
782 case AD9122_REG_I_PHA_ADJ_MSB:
783 case AD9122_REG_Q_PHA_ADJ_MSB:
784 *val = sign_extend32(*val, 9);
785 break;
786 }
787
788 out:
789 return ret;
790 }
791
792 /***************************************************************************//**
793 * @brief Writes interpolation data to the AD9122.
794 *
795 * @return Returns negative error code or the written data in case of success.
796 *******************************************************************************/
797 uint32_t ad9122_interpolation_store(uint32_t address, int32_t readin)
798 {
799 struct cf_axi_converter *conv = &dds_conv;
800 int32_t ret;
801
802 switch (address) {
803 case 0:
804 ret = ad9122_set_interpol_freq(conv, readin);
805 break;
806 case 1:
807 ret = ad9122_set_interpol_fcent_freq(conv, readin);
808 break;
809 default:
810 ret = -1;
811 }
812
813 if (conv->pcore_sync)
814 conv->pcore_sync();
815
816 return ret ? ret : readin;
817 }
818
819 /***************************************************************************//**
820 * @brief Reads interpolation data from the AD9122.
821 *
822 * @return Returns negative error code or 0 in case of success.
823 *******************************************************************************/
824 uint32_t ad9122_interpolation_show(uint32_t address, int32_t* val)
825 {
826 struct cf_axi_converter *conv = &dds_conv;
827 int32_t ret = 0;
828 int32_t i;
829
830 switch (address) {
831 case 0:
832 *val = ad9122_get_interpol_freq(conv);
833 break;
834 case 1:
835 *val = ad9122_get_interpol_fcent_freq(conv);
836 break;
837 case 2:
838 for (i = 0; conv->intp_modes[i] != 0; i++)
839 val[i] = (int32_t)conv->intp_modes[i];
840 val[i] = 0;
841 break;
842 case 3:
843 for (i = 0; conv->cs_modes[i] != -1; i++)
844 val[i] = (int32_t)conv->cs_modes[i];
845 val[i] = -1;
846 default:
847 ret = -1;
848 }
849
850 return ret;
851 }
852
853 /***************************************************************************//**
854 * @brief Resets the device.
855 *
856 * @return Returns negative error code or 0 in case of success.
857 *******************************************************************************/
858 int32_t ad9122_reset(void)
859 {
860 int32_t ret;
861
862 ret = ad9122_write(AD9122_REG_COMM, AD9122_COMM_RESET);
863 if(ret < 0)
864 return ret;
865
866 ret = ad9122_write(AD9122_REG_COMM, 0x00);
867
868 return ret;
869 }
870
871 /***************************************************************************//**
872 * @brief Reads parameters from the AD9122.
873 *
874 * @return Returns negative error code or 0 in case of success.
875 *******************************************************************************/
876 int32_t ad9122_read_raw(uint32_t channel,
877 int32_t *val,
878 int32_t *val2,
879 int32_t m)
880 {
881 struct cf_axi_converter *conv = &dds_conv;
882
883 switch (m) {
884 case IIO_CHAN_INFO_SAMP_FREQ:
885
886 *val = ad9122_get_data_clk(conv);
887 return 0;
888
889 }
890 return -1;
891 }
892
893 /***************************************************************************//**
894 * @brief Writes parameters to the AD9122.
895 *
896 * @return Returns negative error code or 0 in case of success.
897 *******************************************************************************/
898 int32_t ad9122_write_raw(uint32_t channel,
899 int32_t val,
900 int32_t val2,
901 int32_t mask)
902 {
903 struct cf_axi_converter *conv = &dds_conv;
904 uint32_t rate;
905 int32_t ret;
906 uint32_t ctrl_reg_1;
907 uint32_t ctrl_reg_2;
908
909 DAC_Core_Read(ADI_REG_CNTRL_1, &ctrl_reg_1);
910 DAC_Core_Read(ADI_REG_CNTRL_2, &ctrl_reg_2);
911 switch (mask) {
912 case IIO_CHAN_INFO_SAMP_FREQ:
913 rate = ad9122_get_data_clk(conv);
914 ret = ad9122_set_data_clk(conv, val);
915 if (ret < 0) {
916 return ret;
917 }
918
919 if (val != rate) {
920 ret = ad9122_tune_dci(conv);
921 }
922 break;
923 default:
924 return -1;
925 }
926 DAC_Core_Write(ADI_REG_CNTRL_1, ctrl_reg_1);
927 DAC_Core_Write(ADI_REG_CNTRL_2, ctrl_reg_2);
928
929 return ret;
930 }
931
932 /***************************************************************************//**
933 * @brief Initializes the AD9122.
934 *
935 * @param pfnSetDataClock - Pointer to a function which sets the data clock
936 * @param pfnSetDacClock - Pointer to a function which sets the DAC clock
937 * @param pfnRoundRateDataClock - Pointer to a function which computes the
938 * actual data clock for a desired clock value
939 * @param pfnRoundRateDacClock - Pointer to a function which computes the
940 * actual DAC clock for a desired clock value
941 *
942 * @return Returns negative error code or 0 in case of success.
943 *******************************************************************************/
944 int32_t ad9122_setup(void* pfnSetDataClock,
945 void* pfnSetDacClock,
946 void* pfnSetRefClock,
947 void* pfnRoundRateDataClock,
948 void* pfnRoundRateDacClock,
949 void* pfnRoundRateRefClock)
950 {
951 int32_t ret;
952 int32_t i;
953 uint32_t datapath_ctrl, rate;
954 struct cf_axi_converter *conv = &dds_conv;
955
956 if(ad9122_reset() < 0)
957 return -1;
958
959 pfnSetDataClk = pfnSetDataClock;
960 pfnSetDacClk = pfnSetDacClock;
961 pfnSetRefClk = pfnSetRefClock;
962 pfnRoundRateDataClk = pfnRoundRateDataClock;
963 pfnRoundRateDacClk = pfnRoundRateDacClock;
964 pfnRoundRateRefClk = pfnRoundRateRefClock;
965
966 conv->write = ad9122_write;
967 conv->read = ad9122_read;
968 conv->setup = ad9122_tune_dci;
969 conv->get_fifo_status = ad9122_get_fifo_status;
970 conv->get_data_clk = ad9122_get_data_clk;
971 conv->write_raw = ad9122_write_raw;
972 conv->read_raw = ad9122_read_raw;
973
974 for (i = 0; i < ARRAY_SIZE(ad9122_reg_defaults); i++)
975 {
976 ad9122_write(ad9122_reg_defaults[i][0],
977 ad9122_reg_defaults[i][1]);
978 }
979
980 if(ad9122_sync() < 0)
981 return -1;
982
983 conv->interp_factor = 1;
984 conv->interp_factor = ad9122_validate_interp_factor(conv->interp_factor);
985
986 conv->fcenter_shift = 0;
987
988 datapath_ctrl = AD9122_DATAPATH_CTRL_BYPASS_PREMOD |
989 AD9122_DATAPATH_CTRL_BYPASS_NCO |
990 AD9122_DATAPATH_CTRL_BYPASS_INV_SINC;
991 ad9122_write(AD9122_REG_DATAPATH_CTRL, datapath_ctrl);
992
993 rate = 245760000;
994 ret = ad9122_set_interpol(conv, conv->interp_factor,
995 conv->fcenter_shift, rate);
996
997 cf_axi_dds_of_probe();
998
999 return ret;
1000 }
1001
1002 /***************************************************************************//**
1003 * @brief Calibrates the AD9122 DCI.
1004 *
1005 * @return Returns negative error code or 0 in case of success.
1006 *******************************************************************************/
1007 int32_t ad9122_dci_calibrate()
1008 {
1009 return ad9122_tune_dci(&dds_conv);
1010 }
1011
1012 /***************************************************************************//**
1013 * @brief Sets the AD9122 data rate.
1014 *
1015 * @param rate - Desired data rate in Hz
1016 *
1017 * @return Returns the set data rate.
1018 *******************************************************************************/
1019 int32_t ad9122_set_data_rate(uint32_t rate)
1020 {
1021 struct cf_axi_converter* conv = &dds_conv;
1022 int32_t ret = 0;
1023
1024 #ifdef CF_AXI_DDS
1025 ret = cf_axi_dds_write_raw(0, 0,
1026 (int32_t)rate, 0,
1027 IIO_CHAN_INFO_SAMP_FREQ);
1028 #else
1029 ret = ad9122_write_raw(0,
1030 (int32_t)rate, 0,
1031 IIO_CHAN_INFO_SAMP_FREQ);
1032 #endif
1033
1034 if(ret < 0)
1035 return ret;
1036
1037 return ad9122_get_data_clk(conv);
1038 }
1039
1040 /***************************************************************************//**
1041 * @brief Sets the phase adjustment of the I DAC.
1042 *
1043 * @param val - Phase adjustment value. If the value equals INT32_MAX then
1044 * the function returns the current set value.
1045 *
1046 * @return Returns the set value or negative error code.
1047 *******************************************************************************/
1048 int32_t ad9122_out_voltage0_phase(int32_t val)
1049 {
1050 int32_t ret;
1051
1052 if(val != INT32_MAX)
1053 {
1054 return ad9122_store(AD9122_REG_I_PHA_ADJ_MSB, val);
1055 }
1056
1057 ret = ad9122_show(AD9122_REG_I_PHA_ADJ_MSB, &val);
1058
1059 return ret > 0 ? val : ret;
1060 }
1061
1062 /***************************************************************************//**
1063 * @brief Sets the phase adjustment of the Q DAC.
1064 *
1065 * @param val - Phase adjustment value. If the value equals INT32_MAX then
1066 * the function returns the current set value.
1067 *
1068 * @return Returns the set value or negative error code.
1069 *******************************************************************************/
1070 int32_t ad9122_out_voltage1_phase(int32_t val)
1071 {
1072 int32_t ret;
1073
1074 if(val != INT32_MAX)
1075 {
1076 return ad9122_store(AD9122_REG_Q_PHA_ADJ_MSB, val);
1077 }
1078
1079 ret = ad9122_show(AD9122_REG_Q_PHA_ADJ_MSB, &val);
1080
1081 return ret > 0 ? val : ret;
1082 }
1083
1084 /***************************************************************************//**
1085 * @brief Sets the calibration bias of the I DAC.
1086 *
1087 * @param val - Calibration bias value. If the value equals INT32_MAX then
1088 * the function returns the current set value.
1089 *
1090 * @return Returns the set value or negative error code.
1091 *******************************************************************************/
1092 int32_t ad9122_out_voltage0_calibbias(int32_t val)
1093 {
1094 int32_t ret;
1095
1096 if(val != INT32_MAX)
1097 {
1098 return ad9122_store(AD9122_REG_I_DAC_OFFSET_MSB, val);
1099 }
1100
1101 ret = ad9122_show(AD9122_REG_I_DAC_OFFSET_MSB, &val);
1102
1103 return ret > 0 ? val : ret;
1104 }
1105
1106 /***************************************************************************//**
1107 * @brief Sets the calibration bias of the Q DAC.
1108 *
1109 * @param val - Calibration bias value. If the value equals INT32_MAX then
1110 * the function returns the current set value.
1111 *
1112 * @return Returns the set value or negative error code.
1113 *******************************************************************************/
1114 int32_t ad9122_out_voltage1_calibbias(int32_t val)
1115 {
1116 int32_t ret;
1117
1118 if(val != INT32_MAX)
1119 {
1120 return ad9122_store(AD9122_REG_Q_DAC_OFFSET_MSB, val);
1121 }
1122
1123 ret = ad9122_show(AD9122_REG_Q_DAC_OFFSET_MSB, &val);
1124
1125 return ret > 0 ? val : ret;
1126 }
1127
1128 /***************************************************************************//**
1129 * @brief Sets the calibration scale of the I DAC.
1130 *
1131 * @param val - Calibration scale value. If the value equals INT32_MAX then
1132 * the function returns the current set value.
1133 *
1134 * @return Returns the set value or negative error code.
1135 *******************************************************************************/
1136 int32_t ad9122_out_voltage0_calibscale(int32_t val)
1137 {
1138 int32_t ret;
1139
1140 if(val != INT32_MAX)
1141 {
1142 return ad9122_store(AD9122_REG_I_DAC_CTRL, val);
1143 }
1144
1145 ret = ad9122_show(AD9122_REG_I_DAC_CTRL, &val);
1146
1147 return ret > 0 ? val : ret;
1148 }
1149
1150 /***************************************************************************//**
1151 * @brief Sets the calibration scale of the Q DAC.
1152 *
1153 * @param val - Calibration scale value. If the value equals INT32_MAX then
1154 * the function returns the current set value.
1155 *
1156 * @return Returns the set value or negative error code.
1157 *******************************************************************************/
1158 int32_t ad9122_out_voltage1_calibscale(int32_t val)
1159 {
1160 int32_t ret;
1161
1162 if(val != INT32_MAX)
1163 {
1164 return ad9122_store(AD9122_REG_Q_DAC_CTRL, val);
1165 }
1166
1167 ret = ad9122_show(AD9122_REG_Q_DAC_CTRL, &val);
1168
1169 return ret > 0 ? val : ret;
1170 }
1171
1172 /***************************************************************************//**
1173 * @brief Sets the interpolation frequency.
1174 *
1175 * @param val - Interpolation frequency in Hz. If the value equals INT32_MAX then
1176 * the function returns the current set value.
1177 *
1178 * @return Returns the set value or negative error code.
1179 *******************************************************************************/
1180 int32_t ad9122_out_altvoltage_interpolation_frequency(int32_t val)
1181 {
1182 int32_t ret;
1183
1184 if(val != INT32_MAX)
1185 {
1186 return ad9122_interpolation_store(0, val);
1187 }
1188
1189 ret = ad9122_interpolation_show(0, &val);
1190
1191 return ret > 0 ? val : ret;
1192 }
1193
1194 /***************************************************************************//**
1195 * @brief Stores the available interpolation frequencies in the array
1196 * supplied as parameter.
1197 *
1198 * @param val_array - Array to store the return values.
1199 * The size of the array must be 5, the last valid value
1200 * in the array is followed by a 0.
1201 *
1202 * @return Returns 0 for success or negative error code.
1203 *******************************************************************************/
1204 int32_t out_altvoltage_interpolation_frequency_available(int32_t* val_array)
1205 {
1206 return ad9122_interpolation_show(2, val_array);
1207 }
1208
1209 /***************************************************************************//**
1210 * @brief Sets the center frequency shift value.
1211 *
1212 * @param val - Center frequency shift value in Hz. If the value equals INT32_MAX then
1213 * the function returns the current set value.
1214 *
1215 * @return Returns the set value or negative error code.
1216 *******************************************************************************/
1217 int32_t ad9122_out_altvoltage_interpolation_center_shift_frequency(int32_t val)
1218 {
1219 int32_t ret;
1220
1221 if(val != INT32_MAX)
1222 {
1223 return ad9122_interpolation_store(1, val);
1224 }
1225
1226 ret = ad9122_interpolation_show(1, &val);
1227
1228 return ret > 0 ? val : ret;
1229 }
1230
1231 /***************************************************************************//**
1232 * @brief Stores the available center shift frequencies in the array
1233 * supplied as parameter.
1234 *
1235 * @param val_array - Array to store the return values.
1236 * The size of the array must be 17, the last valid value
1237 * in the array is followed by a -1.
1238 *
1239 * @return Returns 0 for success or negative error code.
1240 *******************************************************************************/
1241 int32_t out_altvoltage_interpolation_center_shift_frequency_available(int32_t* val_array)
1242 {
1243 return ad9122_interpolation_show(3, val_array);
1244 }
1245
1246 /***************************************************************************//**
1247 * @brief Sets the full-scale current for I DAC.
1248 *
1249 * @param fs_adj - Full scale current value. If the value equals INT32_MAX then
1250 * the function returns the current set value.
1251 *
1252 * @return Returns the set full-scale current.
1253 *******************************************************************************/
1254 int32_t ad9122_fs_adj_I_DAC(int32_t fs_adj)
1255 {
1256 uint8_t sleepBit = 0;
1257 uint8_t regData1 = 0;
1258 uint8_t regData2 = 0;
1259
1260 if(fs_adj != INT32_MAX)
1261 {
1262 /* Read the current state of the sleep bit */
1263 sleepBit = (ad9122_read(AD9122_REG_I_DAC_CTRL) &
1264 AD9122_I_DAC_CTRL_I_DAC_SLEEP);
1265
1266 /* Set the full-scale value and keep the state of the sleep bit. */
1267 regData1 = AD9122_I_DAC_CTRL_I_DAC_FS_ADJ_9_8((fs_adj & 0x0300) >> 8);
1268 ad9122_write(AD9122_REG_I_DAC_CTRL, (sleepBit | regData1));
1269 regData2 = AD9122_I_DAC_FS_ADJ_I_DAC_FS_ADJ_7_0((fs_adj & 0x00FF) >> 0);
1270 ad9122_write(AD9122_REG_I_DAC_FS_ADJ, regData2);
1271
1272 /* Compute the set full scale current */
1273 fs_adj = ((regData1 & 0x3) << 8) + (regData2 << 0);
1274 }
1275 else
1276 {
1277 fs_adj = ((ad9122_read(AD9122_REG_I_DAC_CTRL) & 0x3) << 8) +
1278 (ad9122_read(AD9122_REG_I_DAC_FS_ADJ) << 0);
1279 }
1280
1281 return fs_adj;
1282 }
1283
1284 /***************************************************************************//**
1285 * @brief Sets the full-scale current for Q DAC.
1286 *
1287 * @param fs_adj - Full scale current value. If the value equals INT32_MAX then
1288 * the function returns the current set value.
1289 *
1290 * @return Returns the set full-scale current.
1291 *******************************************************************************/
1292 int32_t ad9122_fs_adj_Q_DAC(int32_t fs_adj)
1293 {
1294 uint8_t sleepBit = 0;
1295 uint8_t regData1 = 0;
1296 uint8_t regData2 = 0;
1297
1298 if(fs_adj != INT32_MAX)
1299 {
1300 /* Read the current state of the sleep bit */
1301 sleepBit = (ad9122_read(AD9122_REG_Q_DAC_CTRL) &
1302 AD9122_Q_DAC_CTRL_Q_DAC_SLEEP);
1303
1304 /* Set the full-scale value and keep the state of the sleep bit. */
1305 regData1 = AD9122_Q_DAC_CTRL_Q_DAC_FS_ADJ_9_8((fs_adj & 0x0300) >> 8);
1306 ad9122_write(AD9122_REG_Q_DAC_CTRL, (sleepBit | regData1));
1307 regData2 = AD9122_Q_DAC_FS_ADJ_Q_DAC_FS_ADJ_7_0((fs_adj & 0x00FF) >> 0);
1308 ad9122_write(AD9122_REG_Q_DAC_FS_ADJ, regData2);
1309
1310 /* Compute the set full scale current */
1311 fs_adj = ((regData1 & 0x3) << 8) + (regData2 << 0);
1312 }
1313 else
1314 {
1315 fs_adj = ((ad9122_read(AD9122_REG_Q_DAC_CTRL) & 0x3) << 8) +
1316 (ad9122_read(AD9122_REG_Q_DAC_FS_ADJ) << 0);
1317 }
1318
1319 return fs_adj;
1320 }
1321
1322 /***************************************************************************//**
1323 * @brief Sets the offset of the I DAC.
1324 *
1325 * @param offset - The value that is added directly to the samples written
1326 * to the I DAC. If the value equals INT32_MAX then
1327 * the function returns the current set value.
1328 *
1329 * @return Returns the set offset.
1330 *******************************************************************************/
1331 int32_t ad9122_offset_I_DAC(int32_t offset)
1332 {
1333 uint8_t regData1 = 0;
1334 uint8_t regData2 = 0;
1335
1336 if(offset != INT32_MAX)
1337 {
1338 regData1 = (offset & 0x00FF) >> 0;
1339 ad9122_write(AD9122_REG_I_DAC_OFFSET_LSB, regData1);
1340 regData2 = (offset & 0xFF00) >> 8;
1341 ad9122_write(AD9122_REG_I_DAC_OFFSET_MSB, regData2);
1342 }
1343 else
1344 {
1345 offset = (ad9122_read(AD9122_REG_I_DAC_OFFSET_MSB) << 8) +
1346 (ad9122_read(AD9122_REG_I_DAC_OFFSET_LSB) << 0);
1347 }
1348
1349 return offset;
1350 }
1351
1352 /***************************************************************************//**
1353 * @brief Sets the offset of the Q DAC.
1354 *
1355 * @param offset - The value that is added directly to the samples written
1356 * to the Q DAC. If the value equals INT32_MAX then
1357 * the function returns the current set value.
1358 *
1359 * @return Returns the set offset.
1360 *******************************************************************************/
1361 int32_t ad9122_offset_Q_DAC(int32_t offset)
1362 {
1363 uint8_t regData1 = 0;
1364 uint8_t regData2 = 0;
1365
1366 if(offset != INT32_MAX)
1367 {
1368 regData1 = (offset & 0x00FF) >> 0;
1369 ad9122_write(AD9122_REG_Q_DAC_OFFSET_LSB, regData1);
1370 regData2 = (offset & 0xFF00) >> 8;
1371 ad9122_write(AD9122_REG_Q_DAC_OFFSET_MSB, regData2);
1372 }
1373 else
1374 {
1375 offset = (ad9122_read(AD9122_REG_Q_DAC_OFFSET_MSB) << 8) +
1376 (ad9122_read(AD9122_REG_Q_DAC_OFFSET_LSB) << 0);
1377 }
1378
1379 return offset;
1380 }
1381
1382 /***************************************************************************//**
1383 * @brief Sets the phase adjustment of the I DAC.
1384 *
1385 * @param offset - The value that is added directly to the samples written
1386 * to the I DAC. If the value equals INT32_MAX then
1387 * the function returns the current set value.
1388 *
1389 * @return Returns the set phase adjustment.
1390 *******************************************************************************/
1391 int32_t ad9122_phaseAdj_I_DAC(int32_t phaseAdj)
1392 {
1393 uint8_t regData1 = 0;
1394 uint8_t regData2 = 0;
1395
1396 if(phaseAdj != INT32_MAX)
1397 {
1398 regData1 = (phaseAdj & 0x00FF) >> 0;
1399 ad9122_write(AD9122_REG_I_PHA_ADJ_LSB, regData1);
1400 regData2 = (phaseAdj & 0x0300) >> 8;
1401 ad9122_write(AD9122_REG_I_PHA_ADJ_MSB, regData2);
1402 phaseAdj &= 0x03FF;
1403 }
1404 else
1405 {
1406 phaseAdj = ((ad9122_read(AD9122_REG_I_PHA_ADJ_MSB) & 0x03) << 8) +
1407 (ad9122_read(AD9122_REG_I_PHA_ADJ_LSB) << 0);
1408 }
1409
1410 return phaseAdj;
1411 }
1412
1413 /***************************************************************************//**
1414 * @brief Sets the phase adjustment of the Q DAC.
1415 *
1416 * @param offset - The value that is added directly to the samples written
1417 * to the Q DAC. If the value equals INT32_MAX then
1418 * the function returns the current set value.
1419 *
1420 * @return Returns the set phase adjustment.
1421 *******************************************************************************/
1422 int32_t ad9122_phaseAdj_Q_DAC(int32_t phaseAdj)
1423 {
1424 uint8_t regData1 = 0;
1425 uint8_t regData2 = 0;
1426
1427 if(phaseAdj != INT32_MAX)
1428 {
1429 regData1 = (phaseAdj & 0x00FF) >> 0;
1430 ad9122_write(AD9122_REG_Q_PHA_ADJ_LSB, regData1);
1431 regData2 = (phaseAdj & 0x0300) >> 8;
1432 ad9122_write(AD9122_REG_Q_PHA_ADJ_MSB, regData2);
1433 phaseAdj &= 0x03FF;
1434 }
1435 else
1436 {
1437 phaseAdj = ((ad9122_read(AD9122_REG_Q_PHA_ADJ_MSB) & 0x03) << 8) +
1438 (ad9122_read(AD9122_REG_Q_PHA_ADJ_LSB) << 0);
1439 }
1440
1441 return phaseAdj;
1442 }
1443
1444 /***************************************************************************//**
1445 * @brief Gets the AD9122 FIFO status registers.
1446 *
1447 * @param status1 - AD9122_REG_FIFO_STATUS_1 value.
1448 * @param status2 - AD9122_REG_FIFO_STATUS_2 value.
1449 *
1450 * @return Returns negative error code in case of error
1451 * or 0 in case of success.
1452 *******************************************************************************/
1453 int32_t ad9122_get_fifo_status_regs(uint8_t *status1,
1454 uint8_t *status2)
1455 {
1456 int32_t ret = 0;
1457
1458 ret = ad9122_read(AD9122_REG_FIFO_STATUS_1);
1459 if (ret < 0)
1460 {
1461 return ret;
1462 }
1463 else
1464 {
1465 *status1 = (uint8_t)ret;
1466 }
1467 ret = ad9122_read(AD9122_REG_FIFO_STATUS_2);
1468 if (ret < 0)
1469 {
1470 return ret;
1471 }
1472 else
1473 {
1474 *status2 = (uint8_t)ret;
1475 }
1476
1477 return 0;
1478 }
1479
1480 /***************************************************************************//**
1481 * @brief Sets the input data format.
1482 *
1483 * @param format - The input data format.
1484 * Example: 0 - input data is in twos complement format.
1485 * 1 - input data is in binary format.
1486 *
1487 * @return Returns negative error code in case of error or the set
1488 * input data format.
1489 *******************************************************************************/
1490 int32_t ad9122_set_data_format(uint8_t format)
1491 {
1492 int32_t ret = 0;
1493 uint8_t reg_value = 0;
1494
1495 /* Read the current state of the AD9122_REG_DATA_FORMAT*/
1496 ret = ad9122_read(AD9122_REG_DATA_FORMAT);
1497 if(ret < 0)
1498 {
1499 return -1;
1500 }
1501 reg_value = (uint8_t)ret;
1502
1503 if((format == 0) || (format == 1))
1504 {
1505 reg_value &= ~AD9122_DATA_FORMAT_BINARY;
1506 reg_value |= AD9122_DATA_FORMAT_BINARY * format;
1507 ret = ad9122_write(AD9122_REG_DATA_FORMAT, reg_value);
1508 if(ret < 0)
1509 {
1510 return -1;
1511 }
1512 }
1513 else
1514 {
1515 format = (reg_value & AD9122_DATA_FORMAT_BINARY) >> 7;
1516 }
1517
1518 return format;
1519 }
1520
1521 /***************************************************************************//**
1522 * @brief Gets the AD9122 die temperature code.
1523 *
1524 * @return Returns negative error code in case of error
1525 * or the die temperature code.
1526 *******************************************************************************/
1527 int32_t ad9122_get_temperature_code(void)
1528 {
1529 int32_t ret;
1530 int32_t tmp_code;
1531
1532 ret = ad9122_read(AD9122_REG_DIE_TEMP_LSB);
1533 if (ret < 0)
1534 return ret;
1535 tmp_code = ret;
1536 ret = ad9122_read(AD9122_REG_DIE_TEMP_MSB);
1537 if (ret < 0)
1538 return ret;
1539 tmp_code |= (ret << 8);
1540
1541 return tmp_code;
1542 }