Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file ADF4157.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 "ADF4157.h"
45 #include "ADF4157_cfg.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Variables Definitions *******************************/
50 /******************************************************************************/
51 ADF4157_state adf4157_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 ADF4157_Init()
64 {
65 unsigned long cfgValue = 0;
66 char status = -1;
67
68 /* Setup Control GPIO Pins */
69 ADF4157_LE_OUT;
70 ADF4157_LE_LOW;
71 ADF4157_CE_OUT;
72 ADF4157_CE_HIGH;
73 //ADF4157_LE2_OUT;
74 //ADF4157_CE2_OUT;
75
76 /* Setup SPI Interface */
77 status = SPI_Init(0, 1000000, 0, 1);
78
79 /* R4 */
80 cfgValue = adf4157_pdata_lpc.r4_user_settings |
81 ADF4157_R4_CTRL;
82 ADF4157_Set(cfgValue);
83 adf4157_st.reg_val[ADF4157_REG4] = cfgValue;
84
85 /* R3 */
86 cfgValue = adf4157_pdata_lpc.r3_user_settings |
87 ADF4157_R3_CTRL;
88 ADF4157_Set(cfgValue);
89 adf4157_st.reg_val[ADF4157_REG3] = cfgValue;
90
91 /* R2 */
92 cfgValue = adf4157_pdata_lpc.r2_user_settings |
93 ADF4157_PRESCALER(0) |
94 (adf4157_pdata_lpc.ref_div2_en ? ADF4157_RDIV2(1) : ADF4157_RDIV2(0)) |
95 (adf4157_pdata_lpc.ref_doubler_en ? ADF4157_REF_DBL(1) : ADF4157_REF_DBL(0)) |
96 ADF4157_R_CNT(1) |
97 ADF4157_R2_CTRL;
98 ADF4157_Set(cfgValue);
99 adf4157_st.reg_val[ADF4157_REG2] = cfgValue;
100
101 /* R1 */
102 cfgValue = ADF4157_FRAC_VAL_LSB(1) |
103 ADF4157_R1_CTRL;
104 ADF4157_Set(cfgValue);
105 adf4157_st.reg_val[ADF4157_REG1] = cfgValue;
106
107 /* R0 */
108 cfgValue = adf4157_pdata_lpc.r0_user_settings |
109 ADF4157_INT_VAL(1) |
110 ADF4157_FRAC_VAL_MSB(1) |
111 ADF4157_R0_CTRL;
112 ADF4157_Set(cfgValue);
113 adf4157_st.reg_val[ADF4157_REG0] = cfgValue;
114
115 return status;
116 }
117
118 /**************************************************************************//**
119 * @brief Transmits 32 bits on SPI.
120 *
121 * @param value - Data which will be transmitted.
122 *
123 * @return none.
124 ******************************************************************************/
125 char ADF4157_Set(unsigned long value)
126 {
127 char validation = 0;
128 char status = 0;
129 unsigned char txBuffer[4] = {0, 0, 0, 0}; // TX SPI Buffer
130
131 txBuffer[0] = (unsigned char)((value & 0xFF000000) >> 24);
132 txBuffer[1] = (unsigned char)((value & 0x00FF0000) >> 16);
133 txBuffer[2] = (unsigned char)((value & 0x0000FF00) >> 8);
134 txBuffer[3] = (unsigned char)((value & 0x000000FF) >> 0);
135
136 ADF4157_LE_LOW;
137 validation = SPI_Write(ADF4157_SLAVE_ID, txBuffer, 4);
138 if (validation != 4)
139 {
140 status = -1;
141 }
142 ADF4157_LE_HIGH;
143
144 return status;
145 }
146
147 /***************************************************************************//**
148 * @brief Increases the R counter value until the PFD frequency is
149 * smaller than ADF4351_MAX_FREQ_PFD.
150 *
151 * @param st - The selected structure.
152 * @param r_cnt - Initial r_cnt value.
153 *
154 * @return Final r_cnt value.
155 *******************************************************************************/
156 long ADF4157_tune_r_cnt(ADF4157_state *st, long r_cnt)
157 {
158 ADF4157_platform_data *pdata = st->pdata;
159
160 do {
161 r_cnt++;
162 st->fpfd = (pdata->clkin * (pdata->ref_doubler_en ? 2 : 1)) /
163 (r_cnt * (pdata->ref_div2_en ? 2 : 1));
164 } while (st->fpfd > ADF4157_MAX_FREQ_PFD);
165
166 return r_cnt;
167 }
168
169 /***************************************************************************//**
170 * @brief Computes the greatest common divider of two numbers.
171 *
172 * @return Returns the gcd.
173 *******************************************************************************/
174 unsigned long gcd(unsigned long x, unsigned long y)
175 {
176 long tmp;
177
178 tmp = y > x ? x : y;
179
180
181 while((x % tmp) || (y % tmp))
182 {
183 tmp--;
184 }
185
186 return tmp;
187 }
188
189 /***************************************************************************//**
190 * @brief Sets the ADF4157 output frequency.
191 *
192 * @param st - The selected structure.
193 * @param freq - The desired frequency value.
194 *
195 * @return calculatedFrequency - The actual frequency value that was set.
196 *******************************************************************************/
197 double adf4157_set_freq(ADF4157_state *st, double freq)
198 {
199 unsigned long long tmp;
200 unsigned long div_gcd, prescaler;
201 float chspc;
202 unsigned short mdiv;
203 unsigned short r_cnt = 0;
204 double result;
205
206 if ((freq > ADF4157_MAX_OUT_FREQ) || (freq < ADF4157_MIN_OUT_FREQ))
207 return -1;
208
209 if (freq > ADF4157_MAX_FREQ_45_PRESC) {
210 prescaler = ADF4157_PRESCALER(1);
211 mdiv = 75;
212 } else {
213 prescaler = ADF4157_PRESCALER(0);
214 mdiv = 23;
215 }
216
217 freq *= 1000000;
218 st->r2_mod = ADF4157_FIXED_MODULUS;
219 if ((st->pdata->clkin > ADF4157_MAX_FREQ_REFIN) ||
220 (st->pdata->clkin < ADF4157_MIN_FREQ_REFIN))
221 {
222 return -1;
223 }
224 do{
225 do{
226 r_cnt = ADF4157_tune_r_cnt(st, r_cnt);
227 chspc = (float)(st->fpfd) / st->r2_mod;
228 }while (r_cnt == 0);
229 st->r_cnt = r_cnt;
230 st->channel_spacing = chspc;
231
232 tmp = freq * (unsigned long long)st->r2_mod + (st->fpfd >> 1);
233 tmp = (unsigned long long)(tmp / st->fpfd);
234 st->r0_fract = tmp % st->r2_mod;
235 tmp = tmp / st->r2_mod;
236 st->r0_int = (unsigned long)tmp;
237 }while (mdiv > st->r0_int);
238
239 /* register R3 */
240 st->reg_val[ADF4157_REG3] &= ~(ADF4157_CNT_RST(-1));
241 st->reg_val[ADF4157_REG3] |= (ADF4157_CNT_RST(1));
242 ADF4157_Set(st->reg_val[ADF4157_REG3]);
243
244 /* register R2 */
245 st->reg_val[ADF4157_REG2] &= ~(ADF4157_R_CNT(-1) |
246 ADF4157_PRESCALER(-1));
247 st->reg_val[ADF4157_REG2] |= (ADF4157_R_CNT(st->r_cnt) |
248 prescaler);
249 ADF4157_Set(st->reg_val[ADF4157_REG2]);
250 /* register R1 */
251 st->reg_val[ADF4157_REG1] &= ~ADF4157_FRAC_VAL_LSB(-1);
252 st->reg_val[ADF4157_REG1] |= ADF4157_FRAC_VAL_LSB(st->r0_fract);
253 ADF4157_Set(st->reg_val[ADF4157_REG1]);
254 /* register R0 */
255 st->reg_val[ADF4157_REG0] &= ~(ADF4157_INT_VAL(-1) |
256 ADF4157_FRAC_VAL_MSB(-1));
257 st->reg_val[ADF4157_REG0] |= (ADF4157_INT_VAL(st->r0_int) |
258 ADF4157_FRAC_VAL_MSB(st->r0_fract));
259 ADF4157_Set(st->reg_val[ADF4157_REG0]);
260
261 /* register R3 */
262 st->reg_val[ADF4157_REG3] &= ~(ADF4157_CNT_RST(-1));
263 ADF4157_Set(st->reg_val[ADF4157_REG3]);
264
265 result = st->r0_int * (float)(st->fpfd / 1000000);
266 result = result + ((float)st->r0_fract / st->r2_mod) * (st->fpfd / 1000000);
267
268 return result;
269 }