Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_commands/ADF4156/Command.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Commanad.c
3 * @brief Implementation of the commands given by user through UART for ADF4156.
4 * @author Lucian Sin ([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 "Command.h"
46 #include "Console.h"
47 #include "adf4156.h"
48 #include "adf4156_cfg.h"
49
50 /******************************************************************************/
51 /************************ Constants Definitions *******************************/
52 /******************************************************************************/
53
54 const struct cmd_info cmdList[] = {
55 [0] = {
56 .name = "help?",
57 .description = "Displays all available commands.",
58 .acceptedValue = "",
59 .example = "",
60 },
61 [1] = {
62 .name = "frequency=",
63 .description = "Sets the desired output frequency in MHz.",
64 .acceptedValue = "Accepted values: \r\n\
65 \t500 ... 6200 - desired output frequency in MHz.",
66 .example = "To set the output frequency to 1000MHz, type: \
67 frequency=1000",
68 },
69 [2] = {
70 .name = "frequency?",
71 .description = "Displays the output frequency in MHz.",
72 .acceptedValue = "",
73 .example = "",
74 },
75 [3] = {
76 .name = "register=",
77 .description = "Sets the value of the desired register on 29 bits.",
78 .acceptedValue = "Accepted values: \r\n\
79 \tregister: \r\n\
80 \t0 .. 4 - the selected register.\r\n\
81 \tvalue: \r\n\
82 \t0 .. 0x1FFFFFFF - the new value of the register.",
83 .example = "To set the value 0x11111111 to register 2, type: \
84 register= 2 0x11111111",
85 },
86 [4] = {
87 .name = "register?",
88 .description = "Displays the value of the desired register.",
89 .acceptedValue = "Accepted values: \r\n\
90 \t0 .. 4 - the selected register.",
91 .example = "To display the value of register 0, type register?0",
92 },
93 [5] = {
94 .name = "PFDfreq?",
95 .description = "Displays the current PFD frequency in MHz.",
96 .acceptedValue = "",
97 .example = "",
98 },
99 };
100
101
102 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
103
104 /******************************************************************************/
105 /************************ Variables Definitions *******************************/
106 /******************************************************************************/
107 cmdFunction cmdFunctions[] = {GetHelp, SetFrequency, GetFrequency, SetRegister,
108 GetRegister,GetPFDFrequency};
109
110 adf4156_state adf4156_st =
111 {
112 &adf4156_pdata_lpc,
113 0,
114 0,
115 0,
116 0,
117 0
118 };
119
120 /* Variables holding information about the device */
121 double freqSet = 0;
122
123 /***************************************************************************//**
124 * @brief Displays all available commands.
125 *
126 * @return None.
127 *******************************************************************************/
128 void GetHelp(double* param, char paramNo) // "help?" command
129 {
130 unsigned char displayCmd;
131
132 CONSOLE_Print("Available commands:\r\n");
133 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
134 {
135 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
136 (char*)cmdList[displayCmd].description,
137 (char*)cmdList[displayCmd].acceptedValue);
138 }
139 }
140
141 /***************************************************************************//**
142 * @brief Internal function for displaying all the command with its description.
143 *
144 * @return None.
145 *******************************************************************************/
146 void DisplayCmdList()
147 {
148 unsigned char displayCmd;
149
150 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
151 {
152 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
153 (char*)cmdList[displayCmd].description);
154 }
155 }
156
157 /***************************************************************************//**
158 * @brief Displays error message.
159 *
160 * @return None.
161 *******************************************************************************/
162 void DisplayError(unsigned char funcNo)
163 {
164 /* Display error messages */
165 CONSOLE_Print("Invalid parameter!\r\n");
166 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
167 (char*)cmdList[funcNo].description, \
168 (char*)cmdList[funcNo].acceptedValue);
169 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
170 }
171
172 /***************************************************************************//**
173 * @brief Initializes the device.
174 *
175 * @return - The result of the initialization.
176 * Example: ERROR - the device was not initialized or the device
177 * is not present.
178 * SUCCES - the device was initialized and the device
179 * is present.
180 *******************************************************************************/
181 char DoDeviceInit(void)
182 {
183 if(ADF4156_Init() == 0)
184 {
185 CONSOLE_Print("Device OK\r\n");
186 DisplayCmdList();
187 return SUCCESS;
188 }
189 else
190 {
191 CONSOLE_Print("Device Error\r\n");
192 return ERROR;
193 }
194 }
195
196 /***************************************************************************//**
197 * @brief Sets the desired output frequency in MHz.
198 *
199 * @param param[0] = output frequency to be set in MHz.
200 *
201 * @return None.
202 *******************************************************************************/
203 void SetFrequency(double* param, char paramNo) // "frequency=" command
204 {
205 //unsigned long long freq;
206
207 /* Check if the parameter is valid */
208 if(paramNo >= 1)
209 {
210 if(param[0] < ADF4156_MIN_OUT_FREQ)
211 {
212 param[0] = ADF4156_MIN_OUT_FREQ;
213 }
214 if(param[0] > ADF4156_MAX_OUT_FREQ)
215 {
216 param[0] = ADF4156_MAX_OUT_FREQ;
217 }
218
219 // freq = (unsigned long long)param[0];
220 /* Sets the desired frequency */
221 freqSet = adf4156_set_freq(&adf4156_st, param[0]);
222 /* Send feedback to user */
223 CONSOLE_Print("%s%.3f[MHz]\r\n",(char*)cmdList[1].name,freqSet);
224 }
225 else
226 {
227 /* Display error messages */
228 DisplayError(1);
229 }
230 }
231
232 /***************************************************************************//**
233 * @brief Displays the output frequency in MHz.
234 *
235 * @return None.
236 *******************************************************************************/
237 void GetFrequency(double* param, char paramNo) // "frequency?" command
238 {
239 /* Send the requested value to user */
240 CONSOLE_Print("%s%.3f[MHz]\r\n",(char*)cmdList[1].name,freqSet);
241 }
242
243 /***************************************************************************//**
244 * @brief Sets the value of the desired register on 29 bits.
245 *
246 * @param param[0] = selected register.
247 * @param param[1] = new value of the register.
248 *
249 * @return None.
250 *******************************************************************************/
251 void SetRegister(double* param, char paramNo) // "register=" command
252 {
253 unsigned long regVal;
254 unsigned char regNo;
255
256 /* Check if the parameters are valid */
257 if(paramNo >= 2)
258 {
259 if(param[0] < 0)
260 {
261 param[0] = 0;
262 }
263 if(param[0] > 4)
264 {
265 param[0] = 4;
266 }
267 if(param[1] < 0)
268 {
269 param[1] = 0;
270 }
271 if(param[1] > ADF4156_MAX_REG_VAL)
272 {
273 param[1] = ADF4156_MAX_REG_VAL;
274 }
275
276 regNo = (unsigned char)param[0];
277 regVal = (unsigned long)param[1];
278 /* Calculate the register value with control bits */
279 regVal = (regVal << 3) | regNo;
280 ADF4156_Set(regVal);
281 adf4156_st.reg_val[regNo] = regVal;
282
283 /* Send feedback to user */
284 CONSOLE_Print("register=%d; value=0x%x\r\n",regNo,regVal);
285 }
286 else
287 {
288 /* Display error messages */
289 DisplayError(3);
290 }
291 }
292
293 /***************************************************************************//**
294 * @brief Displays the value of the desired register.
295 *
296 * @return None.
297 *******************************************************************************/
298 void GetRegister(double* param, char paramNo) // "register?" command
299 {
300 unsigned char regNo;
301 unsigned long regVal;
302 unsigned char muxCtrl,noiseMode,csrEn,curSet,prescaler,refDiv2,refDoubler;
303 unsigned char rCounter,sdRst,ldp,pdPol,pd,cpZ,counterRst,clkDivMode;
304 unsigned short intVal,fracVal,phaseVal,modulus,clkDivVal;
305
306 /* Check if the parameter is valid */
307 if(paramNo >= 1)
308 {
309 if(param[0] < 0)
310 {
311 param[0] = 0;
312 }
313 if(param[0] > 4)
314 {
315 param[0] = 4;
316 }
317
318 regNo = (unsigned char)param[0];
319 /* Send feedback to user */
320 CONSOLE_Print("register=%d; value=0x%x\r\n",regNo,
321 adf4156_st.reg_val[regNo]);
322
323 regVal = adf4156_st.reg_val[regNo];
324 if (regNo == 0)
325 {
326 muxCtrl = (0x78000000 & regVal) >> 27;
327 CONSOLE_Print("\tMuxout Control=%d\r\n", muxCtrl);
328 intVal = (0x07FF8000 & regVal) >> 15;
329 CONSOLE_Print("\tInteger Value=%d\r\n", intVal);
330 fracVal = (0x00007FF8 & regVal) >> 3;
331 CONSOLE_Print("\tFractional Value=%d\r\n", fracVal);
332 }
333 if (regNo == 1)
334 {
335 phaseVal = (0x00007FF8 & regVal) >> 3;
336 CONSOLE_Print("\tPhase Value=%d\r\n", phaseVal);
337 }
338 if (regNo == 2)
339 {
340 noiseMode = (0x60000000 & regVal) >> 29;
341 CONSOLE_Print("\tNoise Mode=%d\r\n", noiseMode);
342 csrEn = (0x10000000 & regVal) >> 28;
343 CONSOLE_Print("\tCSR Enable Bit=%d\r\n", csrEn);
344 curSet = (0x0F000000 & regVal) >> 24;
345 CONSOLE_Print("\tCurrent Setting=%d\r\n", curSet);
346 prescaler = (0x00400000 & regVal) >> 22;
347 CONSOLE_Print("\tPrescaler Bit=%d\r\n", prescaler);
348 refDiv2 = (0x00200000 & regVal) >> 21;
349 CONSOLE_Print("\tReference/2 Bit=%d\r\n", refDiv2);
350 refDoubler = (0x00100000 & regVal) >> 20;
351 CONSOLE_Print("\tReference*2 Bit=%d\r\n", refDoubler);
352 rCounter = (0x000F8000 & regVal) >> 15;
353 CONSOLE_Print("\tR Counter=%d\r\n", rCounter);
354 modulus = (0x00007FF8 & regVal) >> 3;
355 CONSOLE_Print("\tModulus Value=%d\r\n", modulus);
356 }
357 if (regNo == 3)
358 {
359 sdRst = (0x00004000 & regVal) >> 14;
360 CONSOLE_Print("\tSigma-Delta Reset Bit=%d\r\n", sdRst);
361 ldp = (0x00000080 & regVal) >> 7;
362 CONSOLE_Print("\tLock Detect Precision Bit=%d\r\n", ldp);
363 pdPol = (0x00000040 & regVal) >> 6;
364 CONSOLE_Print("\tPhase Detect Polarity Bit=%d\r\n", pdPol);
365 pd = (0x00000020 & regVal) >> 5;
366 CONSOLE_Print("\tRF Power Down Bit=%d\r\n", pd);
367 cpZ = (0x00000010 & regVal) >> 4;
368 CONSOLE_Print("\tCharge Pump Three-State Bit=%d\r\n", cpZ);
369 counterRst = (0x00000008 & regVal) >> 3;
370 CONSOLE_Print("\tCounter Reset Bit=%d\r\n", counterRst);
371 }
372 if (regNo == 4)
373 {
374 clkDivMode = (0x00180000 & regVal) >> 19;
375 CONSOLE_Print("\tClock Divider Mode=%d\r\n", clkDivMode);
376 clkDivVal = (0x0007FF80 & regVal) >> 7;
377 CONSOLE_Print("\tClock Divider Value=%d\r\n", clkDivVal);
378 }
379 }
380 else
381 {
382 /* Display error messages */
383 DisplayError(4);
384 }
385 }
386 /***************************************************************************//**
387 * @brief Displays the current PFD frequency in MHz.
388 *
389 * @return None.
390 *******************************************************************************/
391 void GetPFDFrequency(double* param, char paramNo) // "PFDfreq?" command
392 {
393 /* Send the requested value to user */
394 CONSOLE_Print("PFD%s%d[MHz]\r\n",(char*)cmdList[1].name, adf4156_st.fpfd/1000000);
395 }