Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_drivers/ADF4156/ADF4156.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file adf4156.c
3 * @brief Implementation of ad9833 Driver for Microblaze processor.
4 * @author Lucian Sin ([email protected])
5 *******************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without modification,
11 * 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 IMPLIED
29 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
30 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
32 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
33 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
34 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
35 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
36 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
37 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 *******************************************************************************/
40
41 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include "ADF4156.h"
45 #include "ADF4156_cfg.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Variables Definitions *******************************/
50 /******************************************************************************/
51 adf4156_state adf4156_st;
52
53
54 /***************************************************************************//**
55 * @brief Initialize the SPI communication with the device.
56 *
57 * @param None.
58 *
59 * @return status - Result of the initialization procedure.
60 * Example: 0 - if initialization was successful;
61 * -1 - if initialization was unsuccessful.
62 *******************************************************************************/
63 char ADF4156_Init(void)
64 {
65 unsigned long cfgValue = 0;
66 char status = -1;
67
68 /* Setup Control GPIO Pins */
69 ADF4156_LE_OUT;
70 ADF4156_LE_LOW;
71 ADF4156_CE_OUT;
72 ADF4156_CE_HIGH;
73 //ADF4156_LE2_OUT;
74 //ADF4156_CE2_OUT;
75
76 /* Setup SPI Interface */
77 status = SPI_Init(0, 1000000, 0, 1);
78
79 /* R4 */
80 cfgValue = adf4156_pdata_lpc.r4_user_settings |
81 ADF4156_R4_CTRL;
82 ADF4156_Set(cfgValue);
83 adf4156_st.reg_val[ADF4156_REG4] = cfgValue;
84
85 /* R3 */
86 cfgValue = adf4156_pdata_lpc.r3_user_settings |
87 ADF4156_R3_CTRL;
88 ADF4156_Set(cfgValue);
89 adf4156_st.reg_val[ADF4156_REG3] = cfgValue;
90
91 /* R2 */
92 cfgValue = adf4156_pdata_lpc.r2_user_settings |
93 ADF4156_PRESCALER(0) |
94 (adf4156_pdata_lpc.ref_div2_en ? ADF4156_RDIV2(1) : ADF4156_RDIV2(0)) |
95 (adf4156_pdata_lpc.ref_doubler_en ? ADF4156_REF_DBL(1) : ADF4156_REF_DBL(0)) |
96 ADF4156_R_CNT(1) |
97 ADF4156_MOD_WORD(0) |
98 ADF4156_R2_CTRL;
99 ADF4156_Set(cfgValue);
100 adf4156_st.reg_val[ADF4156_REG2] = cfgValue;
101
102 /* R1 */
103 cfgValue = ADF4156_PHASE_VAL(1) |
104 ADF4156_R1_CTRL;
105 ADF4156_Set(cfgValue);
106 adf4156_st.reg_val[ADF4156_REG1] = cfgValue;
107
108 /* R0 */
109 cfgValue = adf4156_pdata_lpc.r0_user_settings |
110 ADF4156_INT_VAL(1) |
111 ADF4156_FRAC_VAL(1) |
112 ADF4156_R0_CTRL;
113 ADF4156_Set(cfgValue);
114 adf4156_st.reg_val[ADF4156_REG0] = cfgValue;
115
116 return status;
117 }
118
119 /**************************************************************************//**
120 * @brief Transmits 32 bits on SPI.
121 *
122 * @param value - Data which will be transmitted.
123 *
124 * @return none.
125 ******************************************************************************/
126 char ADF4156_Set(unsigned long value)
127 {
128 char validation = 0;
129 char status = 0;
130 unsigned char txBuffer[4] = {0, 0, 0, 0}; // TX SPI Buffer
131
132 txBuffer[0] = (unsigned char)((value & 0xFF000000) >> 24);
133 txBuffer[1] = (unsigned char)((value & 0x00FF0000) >> 16);
134 txBuffer[2] = (unsigned char)((value & 0x0000FF00) >> 8);
135 txBuffer[3] = (unsigned char)((value & 0x000000FF) >> 0);
136
137 ADF4156_LE_LOW;
138 validation = SPI_Write(ADF4156_SLAVE_ID, txBuffer, 4);
139 if (validation != 4)
140 {
141 status = -1;
142 }
143 ADF4156_LE_HIGH;
144
145 return status;
146 }
147
148 /***************************************************************************//**
149 * @brief Increases the R counter value until the PFD frequency is
150 * smaller than ADF4351_MAX_FREQ_PFD.
151 *
152 * @param st - The selected structure.
153 * @param r_cnt - Initial r_cnt value.
154 *
155 * @return Final r_cnt value.
156 *******************************************************************************/
157 long adf4156_tune_r_cnt(adf4156_state *st, long r_cnt)
158 {
159 adf4156_platform_data *pdata = st->pdata;
160
161 do {
162 r_cnt++;
163 st->fpfd = (pdata->clkin * (pdata->ref_doubler_en ? 2 : 1)) /
164 (r_cnt * (pdata->ref_div2_en ? 2 : 1));
165 } while (st->fpfd > ADF4156_MAX_FREQ_PFD);
166
167 return r_cnt;
168 }
169
170 /***************************************************************************//**
171 * @brief Computes the greatest common divider of two numbers.
172 *
173 * @return Returns the gcd.
174 *******************************************************************************/
175 unsigned long gcd(unsigned long x, unsigned long y)
176 {
177 long tmp;
178
179 tmp = y > x ? x : y;
180
181
182 while((x % tmp) || (y % tmp))
183 {
184 tmp--;
185 }
186
187 return tmp;
188 }
189
190 /***************************************************************************//**
191 * @brief Sets the ADF4156 output frequency.
192 *
193 * @param st - The selected structure.
194 * @param freq - The desired frequency value.
195 *
196 * @return calculatedFrequency - The actual frequency value that was set.
197 *******************************************************************************/
198 double adf4156_set_freq(adf4156_state *st,double freq)
199 {
200 unsigned long long tmp;
201 unsigned long div_gcd, prescaler, chspc;
202 unsigned short mdiv;
203 unsigned short r_cnt = 0;
204 double result;
205
206
207 if ((freq > ADF4156_MAX_OUT_FREQ) || (freq < ADF4156_MIN_OUT_FREQ))
208 {
209 return -1;
210 }
211 if (freq > ADF4156_MAX_FREQ_45_PRESC) {
212 prescaler = ADF4156_PRESCALER(1);
213 mdiv = 75;
214 } else {
215 prescaler = ADF4156_PRESCALER(0);
216 mdiv = 23;
217 }
218
219 freq *= 1000000;
220 chspc = st->pdata->channel_spacing;
221
222 if ((st->pdata->clkin > ADF4156_MAX_FREQ_REFIN) ||
223 (st->pdata->clkin < ADF4156_MIN_FREQ_REFIN))
224 {
225 return -1;
226 }
227
228 do{
229 do{
230 r_cnt = adf4156_tune_r_cnt(st, r_cnt);
231 st->r2_mod = st->fpfd / chspc;
232 if (r_cnt > ADF4156_MAX_R_CNT)
233 {
234 CONSOLE_Print("Error! R-counter has a too high \
235 value for the selected spacing. Try with a higher spacing value.\r\n");
236 return -1;
237 }
238 }while ((st->r2_mod > ADF4156_MAX_MODULUS) && r_cnt);
239 st->r_cnt = r_cnt;
240 tmp = freq * (unsigned long long)st->r2_mod + (st->fpfd >> 1);
241 tmp = (unsigned long long)(tmp / st->fpfd);
242 st->r0_fract = tmp % st->r2_mod;
243 tmp = tmp / st->r2_mod;
244 st->r0_int = (unsigned long)tmp;
245 }while (mdiv > st->r0_int);
246
247 if (st->r0_fract && st->r2_mod)
248 {
249 div_gcd = gcd(st->r2_mod, st->r0_fract);
250 st->r2_mod /= div_gcd;
251 st->r0_fract /= div_gcd;
252 }else
253 {
254 st->r0_fract = 0;
255 st->r2_mod = 1;
256 }
257
258 /* register R3 */
259 st->reg_val[ADF4156_REG3] &= ~(ADF4156_CNT_RST(-1));
260 st->reg_val[ADF4156_REG3] |= (ADF4156_CNT_RST(1));
261
262 ADF4156_Set(st->reg_val[ADF4156_REG3]);
263
264 /* register R2 */
265 st->reg_val[ADF4156_REG2] &= ~(ADF4156_MOD_WORD(-1) |
266 ADF4156_R_CNT(-1) |
267 ADF4156_PRESCALER(-1));
268 st->reg_val[ADF4156_REG2] |= (ADF4156_MOD_WORD(st->r2_mod) |
269 ADF4156_R_CNT(st->r_cnt) |
270 prescaler);
271 ADF4156_Set(st->reg_val[ADF4156_REG2]);
272
273 /* register R0 */
274 st->reg_val[ADF4156_REG0] &= ~(ADF4156_INT_VAL(-1) |
275 ADF4156_FRAC_VAL(-1));
276 st->reg_val[ADF4156_REG0] |= (ADF4156_INT_VAL(st->r0_int) |
277 ADF4156_FRAC_VAL(st->r0_fract));
278 ADF4156_Set(st->reg_val[ADF4156_REG0]);
279
280 /* register R3 */
281 st->reg_val[ADF4156_REG3] &= ~(ADF4156_CNT_RST(-1));
282 ADF4156_Set(st->reg_val[ADF4156_REG3]);
283
284 result = st->r0_int * (float)(st->fpfd / 1000000);
285 result = result + ((float)st->r0_fract / st->r2_mod) * (st->fpfd / 1000000);
286
287 return result;
288 }