Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for
4 * ADF4153 driver.
5 * @author Istvan Csomortani ([email protected])
6 *
7 ********************************************************************************
8 * Copyright 2013(c) Analog Devices, Inc.
9 *
10 * All rights reserved.
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions are met:
14 * - Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * - Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
19 * distribution.
20 * - Neither the name of Analog Devices, Inc. nor the names of its
21 * contributors may be used to endorse or promote products derived
22 * from this software without specific prior written permission.
23 * - The use of this software may or may not infringe the patent rights
24 * of one or more patent holders. This license does not release you
25 * from the requirement that you obtain separate licenses from these
26 * patent holders to use this software.
27 * - Use of the software either in source or binary form, must be run
28 * on or directly connected to an Analog Devices Inc. component.
29 *
30 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
31 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
32 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
33 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
34 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
36 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
37 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
38 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 *******************************************************************************/
42
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 #include "Command.h"
47 #include "Console.h"
48 #include "adf4153.h"
49 #include "adf4153_cfg.h"
50 #include "TIME.h"
51
52 /******************************************************************************/
53 /************************ Constants Definitions *******************************/
54 /******************************************************************************/
55
56 const struct cmd_info cmdList[] = {
57 [0] = {
58 .name = "help?",
59 .description = "Displays all available commands.",
60 .acceptedValue = "None",
61 .example = "",
62 },
63 [1] = {
64 .name = "register=",
65 .description = "Update the selected latch with the current set ups.",
66 .acceptedValue = "Accepted value:\r\n\
67 \tlatch:\r\n\
68 \t0 - N divider register\r\n\
69 \t1 - R divider register\r\n\
70 \t2 - Control register\r\n\
71 \t3 - Noise and spur register\r\n\
72 \tdesired value of the register",
73 .example = "To load 0x1042 to the Control register, \
74 type: register=2 0x1042",
75 },
76 [2] = {
77 .name = "register?",
78 .description = "Print the specified latch values in a human \
79 readable format",
80 .acceptedValue = "Accepted value:\r\n\
81 \tlatch:\r\n\
82 \t0 - N divider register\r\n\
83 \t1 - R divider register\r\n\
84 \t2 - Control register\r\n\
85 \t3 - Noise and spur register",
86 .example = "To print the functional latch, type: register?2",
87 },
88 [3] = {
89 .name = "frequency=",
90 .description = "Set the VCO frequency.",
91 .acceptedValue = "Accepted value:\r\n\
92 \tfrequency:\r\n\
93 \t500 .. 4000 Mhz in function of the device type",
94 .example = "To set the VCO frequency to 620 Mhz, \
95 type frequency=620",
96 },
97 [4] = {
98 .name = "frequency?",
99 .description = "Print the actual VCO frequency.",
100 .acceptedValue = "None",
101 .example = "",
102 },
103 [5] = {
104 .name = "channelspacing?",
105 .description = "Print the actual value of the channel spacing.",
106 .acceptedValue = "None",
107 .example = "",
108 }
109 };
110
111 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
112
113 /* Define each latch's name for better user experience */
114 const char* latchName[4] = {"N divider register",
115 "R divider register",
116 "control register",
117 "noise and spur register"};
118
119 /******************************************************************************/
120 /************************ Variables Definitions *******************************/
121 /******************************************************************************/
122 cmdFunction cmdFunctions[6] = {GetHelp, SetRegister, GetRegister, \
123 SetVcoFrequency, GetVcoFrequency, \
124 GetChannelSpacing};
125
126 /* Variables which stores the current register settings */
127 unsigned long r0Buff = 0; // N Divider
128 unsigned long r1Buff = 0; // R Divider
129 unsigned long r2Buff = 0; // Control Register
130 unsigned long r3Buff = 0; // Noise and spur register
131
132 /* The minimum and maximum VCO frequency */
133 unsigned long vcoMinFrequency = 0; // unit in Mhz
134 unsigned long vcoMaxFrequency = 0; // unit in Mhz
135
136 /* Actual VCO frequency */
137 unsigned long long actualVcoFreq = 0;
138
139 /******************************************************************************/
140 /************************ Functions Definitions *******************************/
141 /******************************************************************************/
142
143 /***************************************************************************//**
144 * @brief Internal function for displaying error messages.
145 *
146 * @param funcNo - the index of the function in 'cmdFunctions' constant array
147 *
148 * @return None.
149 *******************************************************************************/
150 void DisplayError(unsigned char funcNo)
151 {
152 /* Display error messages */
153 CONSOLE_Print("Invalid parameter!\r\n");
154 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
155 (char*)cmdList[funcNo].description,
156 (char*)cmdList[funcNo].acceptedValue);
157 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
158 }
159
160 /***************************************************************************//**
161 * @brief Internal function for displaying all the command with its description.
162 *
163 * @return None.
164 *******************************************************************************/
165 void DisplayCmdList()
166 {
167 unsigned char displayCmd;
168
169 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
170 {
171 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
172 (char*)cmdList[displayCmd].description);
173 }
174 }
175
176 /***************************************************************************//**
177 * @brief Internal function for verify if the given parameter is between his
178 * valid limits.
179 *
180 * @param None.
181 *
182 * @return None.
183 *******************************************************************************/
184 void paramLimit(double* param,
185 unsigned short lowerLimit,
186 unsigned short upperLimit)
187 {
188 if(*param < lowerLimit)
189 {
190 *param = lowerLimit;
191 }
192 else
193 {
194 if(*param > upperLimit)
195 {
196 *param = upperLimit;
197 }
198 }
199 }
200
201 /***************************************************************************//**
202 * @brief Displays all available commands.
203 *
204 * @return None.
205 *******************************************************************************/
206 void GetHelp(double* param, char paramNo) // "help?" command
207 {
208 unsigned char displayCmd;
209
210 CONSOLE_Print("Available commands:\r\n");
211 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
212 {
213 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
214 (char*)cmdList[displayCmd].description,
215 (char*)cmdList[displayCmd].acceptedValue);
216 }
217 }
218
219 /***************************************************************************//**
220 * @brief Initializes the device.
221 *
222 * @return - The result of the initialization.
223 * Example: ERROR - the device was not initialized or the device
224 * is not present.
225 * SUCCES - the device was initialized and the device
226 * is present.
227 *******************************************************************************/
228 char DoDeviceInit(void)
229 {
230 char initStatus = 0;
231
232 initStatus = ADF4153_Init(ADF4153_st);
233
234 if(initStatus == 0)
235 {
236 /* Update the latch buffers */
237 r0Buff = ADF4153_ReadLatch(ADF4153_CTRL_N_DIVIDER);
238 r1Buff = ADF4153_ReadLatch(ADF4153_CTRL_R_DIVIDER);
239 r2Buff = ADF4153_ReadLatch(ADF4153_CTRL_CONTROL);
240 r3Buff = ADF4153_ReadLatch(ADF4153_CTRL_NOISE_SPUR);
241 /* set up the VCO frequency limits */
242 vcoMinFrequency = 500;
243 vcoMaxFrequency = 4000;
244
245 CONSOLE_Print("Device OK\r\n");
246 DisplayCmdList();
247 return SUCCESS;
248 }
249 else
250 {
251 CONSOLE_Print("Device Error\r\n");
252 return ERROR;
253 }
254 }
255
256 /**************************************************************************//**
257 * @brief Update the specified latch with the current settings
258 *
259 * @param param[0] - type of the latch, which will be updated
260 * 'ADF4153_CTRL_N_DIVIDER' - N Divider
261 * 'ADF4153_CTRL_R_DIVIDER' - R Divider
262 * 'ADF4153_CTRL_CONTROL' - Control Register
263 * 'ADF4153_CTRL_NOISE_SPUR' - Noise and spur register
264 *
265 * param[1] - the desired value of the specified latch
266 *
267 * @return
268 ******************************************************************************/
269 void SetRegister(double* param, char paramNo)
270 {
271 unsigned char latch = 0;
272 unsigned long value = 0;
273
274 if(paramNo >= 2)
275 {
276 paramLimit(&param[0], 0, 3);
277 if(param[1] < 0)
278 {
279 param[1] = 0;
280 }
281 else
282 {
283 if(param[1] > 0xFFFFFF)
284 {
285 param[1] = 0xFFFFFF;
286 }
287 }
288
289 latch = (unsigned char)param[0];
290 value = (unsigned long)param[1];
291
292 switch(latch)
293 {
294 case ADF4153_CTRL_N_DIVIDER :
295 ADF4153_UpdateLatch(ADF4153_CTRL_N_DIVIDER |
296 (value & ~ADF4153_CTRL_MASK));
297 r0Buff = ADF4153_ReadLatch(ADF4153_CTRL_N_DIVIDER);
298 break;
299 case ADF4153_CTRL_R_DIVIDER :
300 ADF4153_UpdateLatch(ADF4153_CTRL_R_DIVIDER |
301 (value & ~ADF4153_CTRL_MASK));
302 r1Buff = ADF4153_ReadLatch(ADF4153_CTRL_R_DIVIDER);
303 break;
304 case ADF4153_CTRL_CONTROL :
305 ADF4153_UpdateLatch(ADF4153_CTRL_CONTROL |
306 (value & ~ADF4153_CTRL_MASK));
307
308 r2Buff = ADF4153_ReadLatch(ADF4153_CTRL_CONTROL);
309 break;
310 case ADF4153_CTRL_NOISE_SPUR :
311 ADF4153_UpdateLatch(ADF4153_CTRL_NOISE_SPUR |
312 (value & ~ADF4153_CTRL_MASK));
313 r3Buff = ADF4153_ReadLatch(ADF4153_CTRL_NOISE_SPUR);
314 break;
315 }
316 /* Send user feedback */
317 CONSOLE_Print("The %s was updated with 0x%.4x\r\n", \
318 latchName[latch], value);
319 }
320 else
321 {
322 /* Display error messages */
323 DisplayError(1);
324 }
325 }
326
327 /**************************************************************************//**
328 * @brief Print the specified latch current value in a human readable manner
329 *
330 * @param - param[0] - type of the latch, which will be updated
331 * 'ADF4153_CTRL_N_DIVIDER' - N Divider
332 * 'ADF4153_CTRL_R_DIVIDER' - R Divider
333 * 'ADF4153_CTRL_CONTROL' - Control Register
334 * 'ADF4153_CTRL_NOISE_SPUR' - Noise and spur register
335 * @return - None
336 ******************************************************************************/
337 void GetRegister(double* param, char paramNo)
338 {
339 unsigned char latch = 0;
340
341 if(paramNo >= 1)
342 {
343 paramLimit(&param[0], 0, 3);
344 latch = (unsigned char)param[0];
345 switch(latch)
346 {
347 case ADF4153_CTRL_N_DIVIDER :
348 CONSOLE_Print("Bellow are the %s content : 0x%x\r\n", \
349 latchName[latch], r0Buff);
350 CONSOLE_Print("\t+ Fractional value : 0x%4x\r\n", \
351 ((r0Buff & ADF4153_R0_FRAC(ADF4153_R0_FRAC_MASK)) >> \
352 ADF4153_R0_FRAC_OFFSET));
353 CONSOLE_Print("\t+ Integer value : 0x%4x\r\n", \
354 ((r0Buff & ADF4153_R0_INT(ADF4153_R0_INT_MASK)) >> \
355 ADF4153_R0_INT_OFFSET));
356 CONSOLE_Print("\t+ Fast-lock : 0x%1x\r\n", \
357 ((r0Buff & ADF4153_R0_FASTLOCK(ADF4153_R0_FASTLOCK_MASK)) >> \
358 ADF4153_R0_FASTLOCK_OFFSET));
359 break;
360 case ADF4153_CTRL_R_DIVIDER :
361 CONSOLE_Print("Bellow are the %s content : 0x%x\r\n", \
362 latchName[latch], r1Buff);
363 CONSOLE_Print("\t+ Interpolator modulus value : 0x%3x\r\n", \
364 ((r1Buff & ADF4153_R1_MOD(ADF4153_R1_MOD_MASK)) >> \
365 ADF4153_R1_MOD_OFFSET));
366 CONSOLE_Print("\t+ R Counter : 0x%1x\r\n", \
367 ((r1Buff & ADF4153_R1_RCOUNTER(ADF4153_R1_RCOUNTER_MASK)) >> \
368 ADF4153_R1_RCOUNTER_OFFSET));
369 CONSOLE_Print("\t+ Prescaler : 0x%1x\r\n", \
370 ((r1Buff & ADF4153_R1_PRESCALE(ADF4153_R1_PRESCALE_MASK)) >> \
371 ADF4153_R1_PRESCALE_OFFSET));
372 CONSOLE_Print("\t+ Muxout : 0x%1x\r\n", \
373 ((r1Buff & ADF4153_R1_MUXOUT(ADF4153_R1_MUXOUT_MASK)) >> \
374 ADF4153_R1_MUXOUT_OFFSET));
375 CONSOLE_Print("\t+ Load Control : 0x%1x\r\n", \
376 ((r1Buff & ADF4153_R1_LOAD(ADF4153_R1_LOAD_MASK)) >> \
377 ADF4153_R1_LOAD_OFFSET));
378 break;
379 case ADF4153_CTRL_CONTROL :
380 CONSOLE_Print("Bellow are the %s content : 0x%x\r\n", \
381 latchName[latch], r2Buff);
382 CONSOLE_Print("\t+ Counter Reset : 0x%1x\r\n", ((r2Buff & \
383 ADF4153_R2_COUNTER_RST(ADF4153_R2_COUNTER_RST_MASK)) >> \
384 ADF4153_R2_COUNTER_RST_OFFSET));
385 CONSOLE_Print("\t+ CP Three-State : 0x%1x\r\n", ((r2Buff & \
386 ADF4153_R2_CP_3STATE(ADF4153_R2_CP_3STATE_MASK)) >> \
387 ADF4153_R2_CP_3STATE_OFFSET));
388 CONSOLE_Print("\t+ Power-Down : 0x%1x\r\n", ((r2Buff & \
389 ADF4153_R2_POWER_DOWN(ADF4153_R2_POWER_DOWN_MASK)) >> \
390 ADF4153_R2_POWER_DOWN_OFFSET));
391 CONSOLE_Print("\t+ LDP : 0x%1x\r\n", \
392 ((r2Buff & ADF4153_R2_LDP(ADF4153_R2_LDP_MASK)) >> \
393 ADF4153_R2_LDP_OFFSET));
394 CONSOLE_Print("\t+ PD Polarity : 0x%1x\r\n", \
395 ((r2Buff & ADF4153_R2_PD_POL(ADF4153_R2_PD_POL_MASK)) >> \
396 ADF4153_R2_PD_POL_OFFSET));
397 CONSOLE_Print("\t+ CP Current Settings : 0x%1x\r\n", ((r2Buff & \
398 ADF4153_R2_CP_CURRENT(ADF4153_R2_CP_CURRENT_MASK)) >> \
399 ADF4153_R2_CP_CURRENT_OFFSET));
400 CONSOLE_Print("\t+ Reference doubler : 0x%1x\r\n", ((r2Buff & \
401 ADF4153_R2_REF_DOUBLER(ADF4153_R2_REF_DOUBLER_MASK)) >> \
402 ADF4153_R2_REF_DOUBLER_OFFSET));
403 CONSOLE_Print("\t+ Resync : 0x%1x\r\n", \
404 ((r2Buff & ADF4153_R2_RESYNC(ADF4153_R2_RESYNC_MASK)) >> \
405 ADF4153_R2_RESYNC_OFFSET));
406 break;
407 case ADF4153_CTRL_NOISE_SPUR :
408 CONSOLE_Print("Bellow are the %s content : 0x%x\r\n", \
409 latchName[latch], r3Buff);
410 CONSOLE_Print("\t+ Noise and spur value: 0x%2x\r\n", \
411 (((r3Buff >> 0x6) << 0x1) | ((r3Buff >> 0x2) & 0x1)));
412 break;
413 }
414 }
415 else
416 {
417 /* Display error messages */
418 DisplayError(2);
419 }
420 }
421
422 /**************************************************************************//**
423 * @brief Set a desire frequency (the minimum and maximum values vary at each
424 * device), the unit of the frequency is Mhz
425 *
426 * @param - param[0] - the value of the desired frequency in Mhz
427 *
428 * @return - None
429 ******************************************************************************/
430 void SetVcoFrequency(double* param, char paramNo)
431 {
432 unsigned long long vcoFrequency = 0;
433
434 if(paramNo >= 1)
435 {
436 paramLimit(&param[0], vcoMinFrequency, vcoMaxFrequency);
437 /* convert to Hz*/
438 vcoFrequency = (unsigned long long)param[0] * 1000000;
439 vcoFrequency = ADF4153_SetFrequency(vcoFrequency);
440 /* Send feedback to user */
441 CONSOLE_Print("The VCO frequency was configured to %.2fMhz\r\n",
442 ((float)vcoFrequency/1000000));
443 actualVcoFreq = vcoFrequency;
444 /* Update the latch buffers */
445 r0Buff = ADF4153_ReadLatch(ADF4153_CTRL_N_DIVIDER);
446 r1Buff = ADF4153_ReadLatch(ADF4153_CTRL_R_DIVIDER);
447 r2Buff = ADF4153_ReadLatch(ADF4153_CTRL_CONTROL);
448 r3Buff = ADF4153_ReadLatch(ADF4153_CTRL_NOISE_SPUR);
449 }
450 else
451 {
452 /* Display error messages */
453 DisplayError(3);
454 }
455 }
456
457 /**************************************************************************//**
458 * @brief Get the current VCO frequency (the minimum and maximum values vary at
459 * each device), the unit of the frequency is Mhz
460 *
461 * @param - None
462 *
463 * @return - None
464 ******************************************************************************/
465 void GetVcoFrequency(double* param, char paramNo)
466 {
467 CONSOLE_Print("The actual VCO frequency is %.2fMHz\r\n", \
468 ((float)actualVcoFreq/1000000));
469 }
470
471 /**************************************************************************//**
472 * @brief Get the value of channel spacing
473 *
474 * @param - None
475 *
476 * @return - None
477 ******************************************************************************/
478 void GetChannelSpacing(double* param, char paramNo)
479 {
480 unsigned long channelSpacing = 0;
481
482 channelSpacing = ADF4153_GetChannelSpacing();
483 CONSOLE_Print("The actual value of channel spacing is %.2fKHz",\
484 ((float)channelSpacing/1000));
485 }