Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/ADF4106/adf4106.c

raw symbols SCIP
1 /******************************************************************************
2 * @file ADF4106.c
3 * @brief Implementation of ADF4106 Driver for Microblaze processor.
4 * @author Istvan Csomortani ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2013(c) Analog Devices, Inc.
8 *
9 * All rights reserved.
10 *
11 * Redistribution and use in source and binary forms, with or without modification,
12 * are permitted provided that the following conditions are met:
13 * - Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * - Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 * - Neither the name of Analog Devices, Inc. nor the names of its
20 * contributors may be used to endorse or promote products derived
21 * from this software without specific prior written permission.
22 * - The use of this software may or may not infringe the patent rights
23 * of one or more patent holders. This license does not release you
24 * from the requirement that you obtain separate licenses from these
25 * patent holders to use this software.
26 * - Use of the software either in source or binary form, must be run
27 * on or directly connected to an Analog Devices Inc. component.
28 *
29 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
39 *
40 ******************************************************************************/
41
42 /*****************************************************************************/
43 /****************************** Include Files ********************************/
44 /*****************************************************************************/
45 #include "adf4106.h"
46 #include "Communication.h"
47 #include "TIME.h"
48
49 #define DATA_MASK_MSB8 0xFF0000
50 #define DATA_OFFSET_MSB8 16
51 #define DATA_MASK_MID8 0xFF00
52 #define DATA_OFFSET_MID8 8
53 #define DATA_MASK_LSB8 0xFF
54 #define DATA_OFFSET_LSB8 0
55 #define ADDRESS_MASK 3
56
57 #define FREQ_2_4_GHZ 2400000000
58 #define FREQ_5_2_GHZ 5200000000
59
60 /*****************************************************************************/
61 /**************************** Private variables ******************************/
62 /*****************************************************************************/
63 struct adf4106_chip_info {
64 unsigned long long vcoMaxFrequency;
65 unsigned long pfdMaxFrequency;
66 unsigned long vcoMinFrequency;
67 unsigned long pfdMinFrequency;
68 };
69
70 struct adf4106_chip_info ADF4106_CHIP_INFO[] = {
71 [ID_ADF4001] = {
72 .vcoMaxFrequency = 200000000, // 200 Mhz
73 .pfdMaxFrequency = 55000000, // 55 Mhz
74 .vcoMinFrequency = 5000000, // 5 Mhz
75 .pfdMinFrequency = 5000000, // 5 Mhz
76 },
77 [ID_ADF4002] = {
78 .vcoMaxFrequency = 400000000, // 400 Mhz
79 .pfdMaxFrequency = 104000000, // 104 Mhz
80 .vcoMinFrequency = 5000000, // 5 Mhz
81 .pfdMinFrequency = 20000000, // 20 Mhz
82 },
83 [ID_ADF4106] = {
84 .vcoMaxFrequency = 6000000000, // 6 Ghz
85 .pfdMaxFrequency = 104000000, // 104 Mhz
86 .vcoMinFrequency = 500000000, // 500 Mhz
87 .pfdMinFrequency = 20000000, // 20 Mhz
88 }
89 };
90
91 /* Actual device type */
92 ADF4106_type_t this_device;
93
94 /* Reference input frequency */
95 unsigned long refIn = 0;
96
97 /* Internal buffers for each latch */
98 unsigned long rLatch = 0;
99 unsigned long nLatch = 0;
100 unsigned long fLatch = 0;
101 unsigned long iLatch = 0;
102
103 /*****************************************************************************/
104 /*************************** Functions definitions ***************************/
105 /*****************************************************************************/
106
107 /**************************************************************************//**
108 * @brief Initialize SPI and Initial Values for ADF4106 Board.
109 *
110 * @param device - The current device type.
111 * initMethod - Initialization method
112 * 0 - 'INIT_LATCH',
113 * 1 - 'INIT_CEPIN',
114 * 2 - 'INIT_COUNTER_RESET'
115 * ADF4106_st - the structure with the initial set up values of the
116 * registers
117 *
118 * @return success
119 ******************************************************************************/
120 char ADF4106_Init(ADF4106_type_t device,
121 ADF4106_init_t initMethod,
122 ADF4106_settings_t ADF4106_st)
123 {
124 char status = -1;
125
126 /* lock the current device */
127 this_device = device;
128
129 /* CPHA = 1; CPOL = 0; */
130 status = SPI_Init(0, 100000, 0, 1);
131
132 /* Bring CE high to put device to power up */
133 ADF4106_CE_OUT;
134 ADF4106_CE_HIGH;
135 /* Initialize the load enable pin */
136 ADF4106_LE_OUT;
137 ADF4106_LE_LOW;
138
139 /* Set up the reference input frequency */
140 refIn = ADF4106_st.refIn;
141
142 /* Import the PFD max limit, from user set up */
143 if(ADF4106_CHIP_INFO[this_device].pfdMaxFrequency > ADF4106_st.pfdMax)
144 {
145 ADF4106_CHIP_INFO[this_device].pfdMaxFrequency = ADF4106_st.pfdMax;
146 }
147
148 switch(initMethod)
149 {
150 case INIT_LATCH :
151 ADF4106_InitLatchMethod(ADF4106_st);
152 break;
153 case INIT_CEPIN :
154 ADF4106_InitCEPinMethod(ADF4106_st);
155 break;
156 case INIT_COUNTER_RESET :
157 ADF4106_InitCounteResetMethod(ADF4106_st);
158 break;
159 }
160
161 return status;
162 }
163
164 /**************************************************************************//**
165 * @brief Update one of the latch via the SPI interface
166 *
167 * @param latchData - the data which will be written to the latch
168 *
169 * @return
170 ******************************************************************************/
171 void ADF4106_UpdateLatch(unsigned long latchData)
172 {
173 unsigned char dataBuffer[3] = {0,};
174 unsigned char latchType = latchData & 0x3;
175
176 /* Update the internal buffers */
177 switch(latchType)
178 {
179 case ADF4106_CTRL_R_COUNTER :
180 rLatch = latchData;
181 break;
182 case ADF4106_CTRL_N_COUNTER :
183 nLatch = latchData;
184 break;
185 case ADF4106_CTRL_FUNCTION_LATCH :
186 fLatch = latchData;
187 break;
188 case ADF4106_CTRL_INIT_LATCH :
189 iLatch = latchData;
190 fLatch = ADF4106_CTRL_FUNCTION_LATCH |
191 (latchData & ~ADF4106_CTRL_MASK);
192 break;
193 }
194
195 dataBuffer[0] = (latchData & DATA_MASK_MSB8) >> DATA_OFFSET_MSB8;
196 dataBuffer[1] = (latchData & DATA_MASK_MID8) >> DATA_OFFSET_MID8;
197 dataBuffer[2] = (latchData & DATA_MASK_LSB8) >> DATA_OFFSET_LSB8;
198
199 SPI_Write(ADF4106_SLAVE_ID,
200 dataBuffer,
201 3);
202
203 /* Generate a load pulse */
204 ADF4106_LE_HIGH;
205 ADF4106_LE_LOW;
206 }
207
208 /**************************************************************************//**
209 * @brief Initialization latch method
210 *
211 * @param None
212 *
213 * @return
214 ******************************************************************************/
215 void ADF4106_InitLatchMethod(ADF4106_settings_t ADF4106_st)
216 {
217 /* Program initialization latch (COUNTER RESET bit is zero) */
218 ADF4106_UpdateLatch(ADF4106_CTRL_INIT_LATCH | \
219 ADF4106_CR(ADF4106_st.counterReset) | \
220 ADF4106_PD1(ADF4106_st.powerDown1) | \
221 ADF4106_MUXOUT(ADF4106_st.muxoutControl) | \
222 ADF4106_PDPOL(ADF4106_st.phaseDetectorPol) | \
223 ADF4106_CP(ADF4106_st.cpType) | \
224 ADF4106_FASTLOCK(ADF4106_st.fastLockMode) | \
225 ADF4106_TCC(ADF4106_st.timerCounterControl) |\
226 ADF4106_CS1(ADF4106_st.currentSetting1) | \
227 ADF4106_CS2(ADF4106_st.currentSetting2) | \
228 ADF4106_PD2(ADF4106_st.powerDown2));
229
230 /* Do an R load */
231 ADF4106_UpdateLatch(ADF4106_CTRL_R_COUNTER | \
232 ADF4106_R_COUNTER(ADF4106_st.refCounter) | \
233 ADF4106_R_ABP(ADF4106_st.antiBacklashWidth)| \
234 ADF4106_R_TMB(ADF4106_st.testModeBits) | \
235 ADF4106_R_LDP(ADF4106_st.lockDetectPrecision));
236
237 /* Do an N load */
238 ADF4106_UpdateLatch(ADF4106_CTRL_N_COUNTER | \
239 ADF4106_N_COUNTER_B(ADF4106_st.bNCounter) | \
240 ADF4106_N_CP(ADF4106_st.cpGain));
241
242 /* Update the local variable for Function Latch */
243 fLatch = ADF4106_CTRL_FUNCTION_LATCH | (iLatch & 0xFFFFFFFC);
244 }
245
246 /**************************************************************************//**
247 * @brief CE Pin method
248 *
249 * @param None
250 *
251 * @return
252 ******************************************************************************/
253 void ADF4106_InitCEPinMethod(ADF4106_settings_t ADF4106_st)
254 {
255 /* Bring CE low to put the device into power down. */
256 ADF4106_CE_LOW;
257 /* Program the function latch */
258 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH | \
259 ADF4106_CR(ADF4106_st.counterReset) | \
260 ADF4106_PD1(ADF4106_st.powerDown1) | \
261 ADF4106_MUXOUT(ADF4106_st.muxoutControl) | \
262 ADF4106_PDPOL(ADF4106_st.phaseDetectorPol) | \
263 ADF4106_CP(ADF4106_st.cpType) | \
264 ADF4106_FASTLOCK(ADF4106_st.fastLockMode) | \
265 ADF4106_TCC(ADF4106_st.timerCounterControl) |\
266 ADF4106_CS1(ADF4106_st.currentSetting1) | \
267 ADF4106_CS2(ADF4106_st.currentSetting2) | \
268 ADF4106_PD2(ADF4106_st.powerDown2));
269 /* Program the R counter latch */
270 ADF4106_UpdateLatch(ADF4106_CTRL_R_COUNTER | \
271 ADF4106_R_COUNTER(ADF4106_st.refCounter) | \
272 ADF4106_R_ABP(ADF4106_st.antiBacklashWidth)| \
273 ADF4106_R_TMB(ADF4106_st.testModeBits) | \
274 ADF4106_R_LDP(ADF4106_st.lockDetectPrecision));
275 /* Program the N counter latch */
276 ADF4106_UpdateLatch(ADF4106_CTRL_N_COUNTER | \
277 ADF4106_N_COUNTER_B(ADF4106_st.bNCounter) | \
278 ADF4106_N_CP(ADF4106_st.cpGain));
279 /* Bring CE high to take the device out of power-down */
280 ADF4106_CE_HIGH;
281 /* Wait for the input buffer bias to reach steady state */
282 TIME_DelayUs(1);
283 }
284
285 /**************************************************************************//**
286 * @brief Counter reset method
287 *
288 * @param None
289 *
290 * @return
291 ******************************************************************************/
292 void ADF4106_InitCounteResetMethod(ADF4106_settings_t ADF4106_st)
293 {
294 /* Program the function latch, enable the counter reset */
295 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH | \
296 ADF4106_CR_RESET | \
297 ADF4106_PD1(ADF4106_st.powerDown1) | \
298 ADF4106_MUXOUT(ADF4106_st.muxoutControl) | \
299 ADF4106_PDPOL(ADF4106_st.phaseDetectorPol) | \
300 ADF4106_CP(ADF4106_st.cpType) | \
301 ADF4106_FASTLOCK(ADF4106_st.fastLockMode) | \
302 ADF4106_TCC(ADF4106_st.timerCounterControl) |\
303 ADF4106_CS1(ADF4106_st.currentSetting1) | \
304 ADF4106_CS2(ADF4106_st.currentSetting2) | \
305 ADF4106_PD2(ADF4106_st.powerDown2));
306 /* Program the R counter latch */
307 ADF4106_UpdateLatch(ADF4106_CTRL_R_COUNTER | \
308 ADF4106_R_COUNTER(ADF4106_st.refCounter) | \
309 ADF4106_R_ABP(ADF4106_st.antiBacklashWidth)| \
310 ADF4106_R_TMB(ADF4106_st.testModeBits) | \
311 ADF4106_R_LDP(ADF4106_st.lockDetectPrecision));
312 /* Program the N counter latch */
313 ADF4106_UpdateLatch(ADF4106_CTRL_N_COUNTER | \
314 ADF4106_N_COUNTER_B(ADF4106_st.bNCounter) | \
315 ADF4106_N_CP(ADF4106_st.cpGain));
316 /* Do a Function Load, this time disable the counter reset */
317 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH | \
318 ADF4106_CR_NORMAL | \
319 ADF4106_PD1(ADF4106_st.powerDown1) | \
320 ADF4106_MUXOUT(ADF4106_st.muxoutControl) | \
321 ADF4106_PDPOL(ADF4106_st.phaseDetectorPol) | \
322 ADF4106_CP(ADF4106_st.cpType) | \
323 ADF4106_FASTLOCK(ADF4106_st.fastLockMode) | \
324 ADF4106_TCC(ADF4106_st.timerCounterControl) |\
325 ADF4106_CS1(ADF4106_st.currentSetting1) | \
326 ADF4106_CS2(ADF4106_st.currentSetting2) | \
327 ADF4106_PD2(ADF4106_st.powerDown2));
328 }
329
330 /***************************************************************************//**
331 * @brief Return the value of a desired latch
332 *
333 * @param param[0] - the type of the latch:
334 * 0 - 'ADF4106_CTRL_R_COUNTER'
335 * 1 - 'ADF4106_CTRL_N_COUNTER'
336 * 2 - 'ADF4106_CTRL_FUNCTION_LATCH'
337 * 3 - 'ADF4106_CTRL_INIT_LATCH'
338 *
339 * @return latchValue - the value of the desired latch
340 *******************************************************************************/
341 unsigned long ADF4106_ReadLatch(unsigned char latchType)
342 {
343 switch(latchType)
344 {
345 case ADF4106_CTRL_R_COUNTER :
346 return rLatch;
347
348 case ADF4106_CTRL_N_COUNTER :
349 return nLatch;
350
351 case ADF4106_CTRL_FUNCTION_LATCH :
352 return fLatch;
353
354 case ADF4106_CTRL_INIT_LATCH :
355 return iLatch;
356
357 default :
358 return -1;
359 }
360 }
361
362 /***************************************************************************//**
363 * @brief Increases the R counter value until the ADF4106_PDF_MAX_FREQ is
364 * greater than PFD frequency.
365 *
366 * @param rCounter - R counter value.
367 *
368 * @return rCounter - modified R counter value.
369 *******************************************************************************/
370 unsigned short ADF4106_TuneRcounter(unsigned short rCounter)
371 {
372 unsigned long frequencyPfd = 0; // PFD frequency
373
374 do
375 {
376 rCounter++;
377 frequencyPfd = refIn / rCounter;
378 }
379 while(frequencyPfd > ADF4106_CHIP_INFO[this_device].pfdMaxFrequency);
380
381 return rCounter;
382 }
383
384 /***************************************************************************//**
385 * @brief Sets the output frequency.
386 *
387 * @param frequency - The desired frequency value.
388 *
389 * @return calculatedFrequency - The actual frequency value that was set.
390 *******************************************************************************/
391 unsigned long long ADF4106_SetFrequency(unsigned long long frequency)
392 {
393 unsigned long long vcoFrequency = 0; // VCO frequency
394 unsigned long frequencyPfd = 0; // PFD frequency
395 unsigned long freqRatio = 0; // VCOfreq / PFDfreq
396 unsigned long long calculatedFrequency = 0; // Actual VCO frequency
397 unsigned short rCounterValue = 0; // Value for R counter
398 unsigned short a = 0; // Value for A counter
399 unsigned short b = 0; // Value for B counter
400 unsigned char devicePrescaler = 0;
401 unsigned char userPrescaler = 0; // prescaler defined by user
402
403 /* Force "frequency" parameter to the [minMHz...maxGHz] interval. */
404 if(frequency <= ADF4106_CHIP_INFO[this_device].vcoMaxFrequency)
405 {
406 if(frequency >= ADF4106_CHIP_INFO[this_device].vcoMinFrequency)
407 {
408 vcoFrequency = frequency;
409 }
410 else
411 {
412 vcoFrequency = ADF4106_CHIP_INFO[this_device].vcoMinFrequency;
413 }
414 }
415 else
416 {
417 vcoFrequency = ADF4106_CHIP_INFO[this_device].vcoMaxFrequency;
418 }
419 do
420 {
421 /* Get the actual PFD frequency. */
422 rCounterValue = ADF4106_TuneRcounter(rCounterValue);
423 frequencyPfd = refIn / rCounterValue;
424 /* Define the value of the prescaler
425 * By default the prescaler is constant for ADF4001 and ADF4002
426 * In case of ADF4106, the minimum admitted prescaler will be selected,
427 * or the user defined prescaler if it is valid (big enough)
428 * Valid min prescaler : VCO < 2.4Ghz -> 8/9
429 * VCO > 2.4Ghz and VCO < 5.4Ghz -> 16/17
430 * VCO > 5.4Ghz -> 32/33
431 */
432 userPrescaler = ADF4106_PRESCALE(fLatch >> ADF4106_PS_OFFSET);
433 devicePrescaler = (this_device != ID_ADF4106) ? \
434 ADF4106_PRESCALE(ADF4106_PS_8_9) : \
435 (vcoFrequency <= FREQ_2_4_GHZ) ? \
436 ADF4106_PRESCALE(ADF4106_PS_8_9) : \
437 ((vcoFrequency > FREQ_2_4_GHZ) & \
438 (vcoFrequency < FREQ_5_2_GHZ)) ? \
439 ADF4106_PRESCALE(ADF4106_PS_16_17) : \
440 ADF4106_PRESCALE(ADF4106_PS_32_33);
441 if(devicePrescaler < userPrescaler)
442 {
443 devicePrescaler = userPrescaler;
444 }
445 /* Find the values for Counter A and Counter B using VCO frequency and
446 * PFD frequency.
447 */
448 freqRatio = (unsigned short)((float)vcoFrequency / frequencyPfd);
449 b = freqRatio / devicePrescaler;
450 a = freqRatio % devicePrescaler;
451 }while(a > b); // B must be greater or equal to A
452 /* Find the actual VCO frequency. */
453 calculatedFrequency = (unsigned long long)((b * devicePrescaler) + a) * frequencyPfd;
454 /* Load the saved values into the registers using Counter Reset Method. */
455 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH |
456 fLatch |
457 ADF4106_CR(ADF4106_CR_RESET));
458
459 rLatch &= ~ADF4106_R_COUNTER(ADF4106_R_COUNTER_MASK);
460 ADF4106_UpdateLatch(ADF4106_CTRL_R_COUNTER |
461 rLatch |
462 ADF4106_R_COUNTER(rCounterValue));
463
464 nLatch &= ~(ADF4106_N_COUNTER_A(ADF4106_N_COUNTER_A_MASK) |
465 ADF4106_N_COUNTER_B(ADF4106_N_COUNTER_B_MASK));
466 if(this_device == ID_ADF4106)
467 {
468 ADF4106_UpdateLatch(ADF4106_CTRL_N_COUNTER |
469 nLatch |
470 ADF4106_N_COUNTER_A(a) |
471 ADF4106_N_COUNTER_B(b));
472 }
473 else
474 {
475 ADF4106_UpdateLatch(ADF4106_CTRL_N_COUNTER |
476 nLatch |
477 ADF4106_N_COUNTER_B((b * devicePrescaler) + a));
478 }
479
480 fLatch &= ~ADF4106_CR(ADF4106_CR_MASK);
481 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH |
482 fLatch |
483 ADF4106_CR(ADF4106_CR_NORMAL));
484
485 return calculatedFrequency;
486 }