Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/ADF4153/adf4153.c

raw symbols SCIP
1 /******************************************************************************
2 * @file ADF4153.c
3 * @brief Implementation of ADF4153 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 "adf4153.h"
46 #include "Communication.h"
47 #include "TIME.h"
48
49 /* For round up division */
50 #define CEIL(a, b) (((a) / (b)) + (((a) % (b)) > 0 ? 1 : 0))
51
52 #define DATA_MASK_MSB8 0xFF0000
53 #define DATA_OFFSET_MSB8 16
54 #define DATA_MASK_MID8 0xFF00
55 #define DATA_OFFSET_MID8 8
56 #define DATA_MASK_LSB8 0xFF
57 #define DATA_OFFSET_LSB8 0
58 #define ADDRESS_MASK 3
59
60 #define FREQ_2_GHZ 2000000000
61
62 /*****************************************************************************/
63 /**************************** Private variables ******************************/
64 /*****************************************************************************/
65
66 /* RF input frequency limits */
67 long ADF4153_RFIN_MIN_FRQ = 10000000; // 10 Mhz
68 long ADF4153_RFIN_MAX_FRQ = 250000000; // 250 Mhz
69 /* Maximum PFD frequency */
70 long ADF4153_PFD_MAX_FRQ = 32000000; // 32 Mhz
71 /* VCO out frequency limits */
72 long ADF4153_VCO_MIN_FRQ = 500000000; // 500 Mhz
73 long long ADF4153_VCO_MAX_FRQ = 4000000000; // 4 Ghz
74 /* maximum interpolator modulus value */
75 short ADF4153_MOD_MAX = 4095; // the MOD is stored in 12 bits
76
77 /* Reference input frequency */
78 unsigned long refIn = 0;
79 unsigned long channelSpacing = 0;
80
81 /* Internal buffers for each latch */
82 unsigned long r0 = 0; /* the actual value of N Divider Register */
83 unsigned long r1 = 0; /* the actual value of R Divider Register */
84 unsigned long r2 = 0; /* the actual value of Control Register */
85 unsigned long r3 = 0; /* the actual value of Noise and Spur Reg*/
86
87 /*****************************************************************************/
88 /*************************** Functions definitions ***************************/
89 /*****************************************************************************/
90
91 /**************************************************************************//**
92 * @brief Initialize SPI and Initial Values for ADF4106 Board.
93 *
94 * @param ADF4106_st - the structure with the initial set up values of the
95 * registers
96 *
97 * @return success
98 ******************************************************************************/
99 char ADF4153_Init(ADF4153_settings_t ADF4153_st)
100 {
101 char status = -1;
102
103 /* CPHA = 1; CPOL = 0; */
104 status = SPI_Init(0, 100000, 0, 1);
105
106 /* Bring CE high to put device to power up */
107 ADF4153_CE_OUT;
108 ADF4153_CE_HIGH;
109 /* Initialize the load enable pin */
110 ADF4153_LE_OUT;
111 ADF4153_LE_LOW;
112
113 /* Set up the reference input frequency */
114 refIn = ADF4153_st.refIn;
115 channelSpacing = ADF4153_st.channelSpacing;
116
117 /* Write all zeros to the noise and spur register */
118 ADF4153_UpdateLatch(ADF4153_CTRL_NOISE_SPUR |
119 0x0);
120 /* selects the lowest noise mode by default */
121 ADF4153_UpdateLatch(ADF4153_CTRL_NOISE_SPUR |
122 0x3C7);
123 /* Set up the control register and enable the counter reset */
124 ADF4153_UpdateLatch(ADF4153_CTRL_CONTROL |
125 ADF4153_R2_COUNTER_RST(ADF4153_CR_ENABLED) |
126 ADF4153_R2_CP_3STATE(ADF4153_st.cpThreeState) |
127 ADF4153_R2_POWER_DOWN(ADF4153_st.powerDown) |
128 ADF4153_R2_LDP(ADF4153_st.ldp) |
129 ADF4153_R2_PD_POL(ADF4153_st.pdPolarity) |
130 ADF4153_R2_CP_CURRENT(ADF4153_st.cpCurrent) |
131 ADF4153_R2_REF_DOUBLER(ADF4153_st.refDoubler) |
132 ADF4153_R2_RESYNC(ADF4153_st.resync)
133 );
134 /* If resync feature is enabled */
135 if(ADF4153_st.resync != 0x0)
136 {
137 /* Load the R divider register */
138 ADF4153_UpdateLatch(ADF4153_CTRL_R_DIVIDER |
139 ADF4153_R1_MOD(10) | //Resync Delay
140 ADF4153_R1_RCOUNTER(ADF4153_st.rCounter) |
141 ADF4153_R1_PRESCALE(ADF4153_st.prescaler) |
142 ADF4153_R1_MUXOUT(ADF4153_st.muxout) |
143 ADF4153_R1_LOAD(ADF4153_LOAD_RESYNC)
144 );
145 }
146 /* Load the R divider register */
147 ADF4153_UpdateLatch(ADF4153_CTRL_R_DIVIDER |
148 ADF4153_R1_MOD(ADF4153_st.modValue) |
149 ADF4153_R1_RCOUNTER(ADF4153_st.rCounter) |
150 ADF4153_R1_PRESCALE(ADF4153_st.prescaler) |
151 ADF4153_R1_MUXOUT(ADF4153_st.muxout) |
152 ADF4153_R1_LOAD(ADF4153_LOAD_NORMAL)
153 );
154 /* Load the N divider register */
155 ADF4153_UpdateLatch(ADF4153_CTRL_N_DIVIDER |
156 ADF4153_R0_FRAC(ADF4153_st.fracValue) |
157 ADF4153_R0_INT(ADF4153_st.intValue) |
158 ADF4153_R0_FASTLOCK(ADF4153_st.fastlock)
159 );
160
161 /* Disable the counter reset in the Control Register */
162 r2 &= ~ADF4153_R2_COUNTER_RST(ADF4153_R2_COUNTER_RST_MASK);
163 ADF4153_UpdateLatch(ADF4153_CTRL_CONTROL |
164 r2 |
165 ADF4153_R2_COUNTER_RST(ADF4153_CR_DISABLED)
166 );
167 return status;
168 }
169
170 /**************************************************************************//**
171 * @brief Update one of the latch via the SPI interface
172 *
173 * @param latchData - the data which will be written to the latch
174 *
175 * @return
176 ******************************************************************************/
177 void ADF4153_UpdateLatch(unsigned long latchData)
178 {
179 unsigned char dataBuffer[3] = {0,};
180 unsigned char latchType = latchData & 0x3;
181
182 /* Update the internal buffers */
183 switch(latchType)
184 {
185 case ADF4153_CTRL_N_DIVIDER :
186 r0 = latchData;
187 break;
188 case ADF4153_CTRL_R_DIVIDER :
189 r1 = latchData;
190 break;
191 case ADF4153_CTRL_CONTROL :
192 r2 = latchData;
193 break;
194 case ADF4153_CTRL_NOISE_SPUR :
195 r3 = latchData;
196 break;
197 }
198
199 dataBuffer[0] = (latchData & DATA_MASK_MSB8) >> DATA_OFFSET_MSB8;
200 dataBuffer[1] = (latchData & DATA_MASK_MID8) >> DATA_OFFSET_MID8;
201 dataBuffer[2] = (latchData & DATA_MASK_LSB8) >> DATA_OFFSET_LSB8;
202
203 SPI_Write(ADF4153_SLAVE_ID,
204 dataBuffer,
205 3);
206
207 /* Generate a load pulse */
208 ADF4153_LE_HIGH;
209 ADF4153_LE_LOW;
210 }
211
212 /***************************************************************************//**
213 * @brief Return the value of a desired latch
214 *
215 * @param param[0] - the type of the latch:
216 * 0 - 'ADF4153_CTRL_N_DIVIDER'
217 * 1 - 'ADF4153_CTRL_R_DIVIDER'
218 * 2 - 'ADF4153_CTRL_CONTROL'
219 * 3 - 'ADF4153_CTRL_NOISE_SPUR'
220 *
221 * @return latchValue - the value of the desired latch
222 *******************************************************************************/
223 unsigned long ADF4153_ReadLatch(unsigned char latchType)
224 {
225 switch(latchType)
226 {
227 case ADF4153_CTRL_N_DIVIDER :
228 return r0;
229
230 case ADF4153_CTRL_R_DIVIDER :
231 return r1;
232
233 case ADF4153_CTRL_CONTROL :
234 return r2;
235
236 case ADF4153_CTRL_NOISE_SPUR :
237 return r3;
238
239 default :
240 return -1;
241 }
242 }
243
244 /***************************************************************************//**
245 * @brief Increases the R counter value until the ADF4106_PDF_MAX_FREQ is
246 * greater than PFD frequency.
247 *
248 * @param rCounter - pointer of the R counter variable.
249 *
250 * @return pfdFrequency - The value of the PFD frequency.
251 *******************************************************************************/
252 unsigned long ADF4153_TuneRcounter(unsigned short *rCounter)
253 {
254 unsigned long pfdFrequency = 0; // PFD frequency
255 unsigned char refDoubler = 0; // Reference Doubler
256
257 refDoubler = (r2 & ADF4153_R2_REF_DOUBLER(ADF4153_R2_RESYNC_MASK)) >> \
258 ADF4153_R2_REF_DOUBLER_OFFSET; // the actual reference doubler
259 do
260 {
261 (*rCounter)++;
262 pfdFrequency = refIn * ((float)(1 + refDoubler) / (*rCounter));
263 }
264 while(pfdFrequency > ADF4153_PFD_MAX_FRQ);
265
266 return pfdFrequency;
267 }
268
269 /***************************************************************************//**
270 * @brief Sets the output frequency.
271 *
272 * @param frequency - The desired frequency value.
273 *
274 * @return calculatedFrequency - The actual frequency value that was set.
275 *******************************************************************************/
276 unsigned long long ADF4153_SetFrequency(unsigned long long frequency)
277 {
278 unsigned long long vcoFrequency = 0; // VCO frequency
279 unsigned long pfdFrequency = 0; // PFD frequency
280 unsigned long long calculatedFrequency = 0; // Actual VCO frequency
281 unsigned long intValue = 0; // INT value
282 unsigned long fracValue = 0; // FRAC value
283 unsigned long modValue = 0; // MOD value
284 unsigned short rCounter = 0; // R Counter
285 float buffer = 0;
286 unsigned char devicePrescaler = 0;
287 unsigned char intMin = 0;
288 /* validate the given frequency parameter */
289 if(frequency <= ADF4153_VCO_MAX_FRQ)
290 {
291 if(frequency >= ADF4153_VCO_MIN_FRQ)
292 {
293 vcoFrequency = frequency;
294 }
295 else
296 {
297 vcoFrequency = ADF4153_VCO_MIN_FRQ;
298 }
299 }
300 else
301 {
302 vcoFrequency = ADF4153_VCO_MAX_FRQ;
303 }
304
305 /* define the value of MOD */
306 modValue = CEIL(refIn, channelSpacing);
307 /* if the modValue is too high, increase the channel spacing */
308 if(modValue > ADF4153_MOD_MAX)
309 {
310 do{
311 channelSpacing++;
312 modValue = CEIL(refIn, channelSpacing);
313 }while(modValue <= ADF4153_MOD_MAX);
314 }
315 /* define prescaler */
316 devicePrescaler = (vcoFrequency <= FREQ_2_GHZ) ? ADF4153_PRESCALER_4_5 : \
317 ADF4153_PRESCALER_8_9;
318 intMin = (devicePrescaler == ADF4153_PRESCALER_4_5) ? 31 : 91;
319 /* define the PFD frequency, R counter ant INT value */
320 do
321 {
322 /* define the PFD frequency and R Counter, using the TuneRCounter() */
323 pfdFrequency = ADF4153_TuneRcounter(&rCounter);
324 intValue = vcoFrequency / pfdFrequency;
325 }while(intValue < intMin);
326 /*define FRAC value */
327 do
328 {
329 fracValue++;
330 buffer = intValue + ((float)fracValue/modValue);
331 calculatedFrequency = (unsigned long long)(buffer * pfdFrequency);
332 }while(calculatedFrequency <= vcoFrequency);
333 fracValue--;
334
335 /* Find the actual VCO frequency. */
336 buffer = intValue + ((float)fracValue/modValue);
337 calculatedFrequency = (unsigned long long)(buffer * pfdFrequency);
338
339 /* Enable the Counter Reset */
340 ADF4153_UpdateLatch(ADF4153_CTRL_CONTROL |
341 r2 |
342 ADF4153_R2_COUNTER_RST(ADF4153_CR_ENABLED));
343
344 /* Load the R divider with the new values */
345 r1 &= (~ADF4153_R1_MOD(ADF4153_R1_MOD_MASK) &
346 ~ADF4153_R1_RCOUNTER(ADF4153_R1_RCOUNTER_MASK) &
347 ~ADF4153_R1_PRESCALE(ADF4153_R1_PRESCALE_MASK));
348 ADF4153_UpdateLatch(ADF4153_CTRL_R_DIVIDER |
349 r1 |
350 ADF4153_R1_MOD(modValue) |
351 ADF4153_R1_RCOUNTER(rCounter) |
352 ADF4153_R1_PRESCALE(devicePrescaler));
353
354 /* Load the N divider with the new values */
355 r0 &= (~ADF4153_R0_FRAC(ADF4153_R0_FRAC_MASK) &
356 ~ADF4153_R0_INT(ADF4153_R0_INT_MASK));
357 ADF4153_UpdateLatch(ADF4153_CTRL_N_DIVIDER |
358 r0 |
359 ADF4153_R0_FRAC(fracValue) |
360 ADF4153_R0_INT(intValue));
361 /* Disable the Counter Reset */
362 r2 &= ~ADF4153_R2_COUNTER_RST(ADF4153_R2_COUNTER_RST_MASK);
363 ADF4153_UpdateLatch(ADF4153_CTRL_CONTROL |
364 r2 |
365 ADF4153_R2_COUNTER_RST(ADF4153_CR_DISABLED));
366
367 return calculatedFrequency;
368 }
369
370 /***************************************************************************//**
371 * @brief Return the value of the channel spacing
372 *
373 * @param frequency - The desired frequency value.
374 *
375 * @return calculatedFrequency - The actual frequency value that was set.
376 *******************************************************************************/
377 unsigned long ADF4153_GetChannelSpacing( void )
378 {
379 return channelSpacing;
380 }