Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for
4 * ADF4106 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 "adf4106.h"
49 #include "adf4106_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 - Reference latch\r\n\
69 \t1 - N Counter latch\r\n\
70 \t2 - Function latch\r\n\
71 \t3 - Initialization latch\r\n\
72 \tdesired value of the register",
73 .example = "To load 0xD8082 to the Function Latch, \
74 type: register=2 0xD8082",
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 - Reference latch\r\n\
83 \t1 - N Counter latch\r\n\
84 \t2 - Function latch\r\n\
85 \t3 - Initialization latch",
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 \t5/500 .. 200/400/6000 Mhz in function of the device type",
94 .example = "To set the VCO frequency to 120 Mhz, \
95 type frequency=120",
96 },
97 [4] = {
98 .name = "frequency?",
99 .description = "Print the actual VCO frequency.",
100 .acceptedValue = "None",
101 .example = "",
102 }
103 };
104
105 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
106
107 /* Possible initialization methods : * INIT_LATCH
108 * * INIT_CEPIN
109 * * INIT_COUNTER_RESET
110 * Change the constant bellow to setup a different initialization method
111 * For further information about each method see the data sheet.
112 */
113 const ADF4106_init_t initMethod = INIT_LATCH;
114
115 /* Define each latch's name for better user experience */
116 const char* latchName[4] = {"reference latch",
117 "n latch",
118 "function latch",
119 "initialization latch"};
120
121 /******************************************************************************/
122 /************************ Variables Definitions *******************************/
123 /******************************************************************************/
124 cmdFunction cmdFunctions[5] = {GetHelp, SetRegister, GetRegister, \
125 SetVcoFrequency, GetVcoFrequency };
126
127 ADF4106_type_t deviceType;
128
129 /* Variables which stores the current register settings */
130 unsigned long rLatchBuff = 0; // reference latch
131 unsigned long nLatchBuff = 0; // N Counter latch
132 unsigned long fiLatchBuff = 0; // Function/Initialization latch
133
134 /* The minimum and maximum VCO frequency */
135 unsigned long vcoMinFrequency = 0; // unit in Mhz
136 unsigned long vcoMaxFrequency = 0; // unit in Mhz
137
138 /* Actual VCO frequency */
139 unsigned long actualVcoFreq = 0;
140
141 /******************************************************************************/
142 /************************ Functions Definitions *******************************/
143 /******************************************************************************/
144
145 /***************************************************************************//**
146 * @brief Internal function for displaying error messages.
147 *
148 * @param funcNo - the index of the function in 'cmdFunctions' constant array
149 *
150 * @return None.
151 *******************************************************************************/
152 void DisplayError(unsigned char funcNo)
153 {
154 /* Display error messages */
155 CONSOLE_Print("Invalid parameter!\r\n");
156 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
157 (char*)cmdList[funcNo].description,
158 (char*)cmdList[funcNo].acceptedValue);
159 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
160 }
161
162 /***************************************************************************//**
163 * @brief Internal function for displaying all the command with its description.
164 *
165 * @return None.
166 *******************************************************************************/
167 void DisplayCmdList()
168 {
169 unsigned char displayCmd;
170
171 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
172 {
173 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
174 (char*)cmdList[displayCmd].description);
175 }
176 }
177
178 /***************************************************************************//**
179 * @brief Internal function for verify if the given parameter is between his
180 * valid limits.
181 *
182 * @param None.
183 *
184 * @return None.
185 *******************************************************************************/
186 void paramLimit(double* param,
187 unsigned short lowerLimit,
188 unsigned short upperLimit)
189 {
190 if(*param < lowerLimit)
191 {
192 *param = lowerLimit;
193 }
194 else
195 {
196 if(*param > upperLimit)
197 {
198 *param = upperLimit;
199 }
200 }
201 }
202
203 /***************************************************************************//**
204 * @brief Displays all available commands.
205 *
206 * @return None.
207 *******************************************************************************/
208 void GetHelp(double* param, char paramNo) // "help?" command
209 {
210 unsigned char displayCmd;
211
212 CONSOLE_Print("Available commands:\r\n");
213 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
214 {
215 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
216 (char*)cmdList[displayCmd].description,
217 (char*)cmdList[displayCmd].acceptedValue);
218 }
219 }
220
221 /***************************************************************************//**
222 * @brief Set the current device type.
223 *
224 * @return None.
225 *******************************************************************************/
226 void DoDeviceLock(void)
227 {
228 char device = 0;
229 char deviceLocked = -1;
230
231 while(deviceLocked < 0)
232 {
233 CONSOLE_Print("Please specify your device.\r\n\
234 For ADF4001 type: 0\r\n\
235 For ADF4002 type: 1\r\n\
236 For ADF4106 type: 2\r\n");
237
238 CONSOLE_GetCommand(&device);
239 if((device >= 0x30) && (device <= 0x34))
240 {
241 deviceLocked = 1;
242 switch(device)
243 {
244 case 0x30 :
245 deviceType = ID_ADF4001;
246 vcoMinFrequency = 5;
247 vcoMaxFrequency = 200;
248 break;
249 case 0x31 :
250 deviceType = ID_ADF4002;
251 vcoMinFrequency = 5;
252 vcoMaxFrequency = 400;
253 break;
254 case 0x32 :
255 deviceType = ID_ADF4106;
256 vcoMinFrequency = 500;
257 vcoMaxFrequency = 6000;
258 break;
259 }
260 }
261 else
262 {
263 CONSOLE_Print("Please type the number corresponding for \
264 your device!\r\n");
265 }
266 }
267 }
268
269 /***************************************************************************//**
270 * @brief Initializes the device.
271 *
272 * @return - The result of the initialization.
273 * Example: ERROR - the device was not initialized or the device
274 * is not present.
275 * SUCCES - the device was initialized and the device
276 * is present.
277 *******************************************************************************/
278 char DoDeviceInit(void)
279 {
280 char initStatus = 0;
281
282 DoDeviceLock();
283
284 initStatus = ADF4106_Init(deviceType, initMethod, ADF4106_st);
285
286 if(initStatus == 0)
287 {
288 /* Update the latch buffers */
289 rLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_R_COUNTER);
290 nLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_N_COUNTER);
291 fiLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_FUNCTION_LATCH);
292
293 CONSOLE_Print("Device OK\r\n");
294 DisplayCmdList();
295 return SUCCESS;
296 }
297 else
298 {
299 CONSOLE_Print("Device Error\r\n");
300 return ERROR;
301 }
302 }
303
304 /**************************************************************************//**
305 * @brief Update the specified latch with the current settings
306 *
307 * @param param[0] - type of the latch, which will be updated
308 * 'ADF4106_CTRL_R_COUNTER' - Reference Counter Latch
309 * 'ADF4106_CTRL_N_COUNTER' - N Counter Latch
310 * 'ADF4106_CTRL_FUNCTION_LATCH' - Function Latch
311 * 'ADF4106_CTRL_INIT_LATCH' - Initialization latch
312 *
313 * param[1] - the desired value of the specified latch
314 *
315 * Note : The function and initialization latch essentially is the same,
316 * however, when the initialization latch is programmed, there is
317 * an additional internal reset pulse applied to the R and N
318 * counters.
319 *
320 * @return
321 ******************************************************************************/
322 void SetRegister(double* param, char paramNo)
323 {
324 unsigned char latch = 0;
325 unsigned long value = 0;
326
327 if(paramNo >= 2)
328 {
329 paramLimit(&param[0], 0, 3);
330 if(param[1] < 0)
331 {
332 param[1] = 0;
333 }
334 else
335 {
336 if(param[1] > 0xFFFFFF)
337 {
338 param[1] = 0xFFFFFF;
339 }
340 }
341
342 latch = (unsigned char)param[0];
343 value = (unsigned long)param[1];
344
345 switch(latch)
346 {
347 case ADF4106_CTRL_R_COUNTER :
348 // Enable the counter reset
349 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH |
350 fiLatchBuff |
351 ADF4106_CR_RESET);
352 ADF4106_UpdateLatch(ADF4106_CTRL_R_COUNTER |
353 (value & ~ADF4106_CTRL_MASK));
354 ADF4106_UpdateLatch(ADF4106_CTRL_N_COUNTER |
355 nLatchBuff);
356 // Disable counter reset
357 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH |
358 fiLatchBuff |
359 ADF4106_CR_NORMAL);
360 rLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_R_COUNTER);
361 break;
362 case ADF4106_CTRL_N_COUNTER :
363 // Enable the counter reset
364 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH |
365 fiLatchBuff |
366 ADF4106_CR_RESET);
367 ADF4106_UpdateLatch(ADF4106_CTRL_R_COUNTER |
368 rLatchBuff);
369 ADF4106_UpdateLatch(ADF4106_CTRL_N_COUNTER |
370 (value & ~ADF4106_CTRL_MASK));
371 // Disable counter reset
372 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH |
373 fiLatchBuff |
374 ADF4106_CR_NORMAL);
375 nLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_N_COUNTER);
376 break;
377 case ADF4106_CTRL_FUNCTION_LATCH :
378 ADF4106_UpdateLatch(ADF4106_CTRL_FUNCTION_LATCH |
379 (value & ~ADF4106_CTRL_MASK));
380 fiLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_FUNCTION_LATCH);
381 break;
382 case ADF4106_CTRL_INIT_LATCH :
383 ADF4106_UpdateLatch(ADF4106_CTRL_INIT_LATCH |
384 (value & ~ADF4106_CTRL_MASK));
385 fiLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_FUNCTION_LATCH);
386 break;
387 }
388
389 /* Send user feedback */
390 CONSOLE_Print("The %s was updated with 0x%.4x\r\n", \
391 latchName[latch], value);
392 }
393 else
394 {
395 /* Display error messages */
396 DisplayError(1);
397 }
398 }
399
400 /**************************************************************************//**
401 * @brief Print the specified latch current value in a human readable manner
402 *
403 * @param - param[0] - type of the latch, which will be updated
404 * 'ADF4106_CTRL_R_COUNTER' - Reference Counter Latch
405 * 'ADF4106_CTRL_N_COUNTER' - N Counter Latch
406 * 'ADF4106_CTRL_FUNCTION_LATCH' - Function Latch
407 *
408 * @return - None
409 ******************************************************************************/
410 void GetRegister(double* param, char paramNo)
411 {
412 unsigned char latch = 0;
413
414 if(paramNo >= 1)
415 {
416 paramLimit(&param[0], 0, 3);
417 latch = (unsigned char)param[0];
418 switch(latch)
419 {
420 case ADF4106_CTRL_R_COUNTER :
421 CONSOLE_Print("Bellow are the %s content : 0x%4x\r\n", \
422 latchName[latch], rLatchBuff);
423 CONSOLE_Print("\t+ Reference Counter Register : %4x\r\n", \
424 (rLatchBuff & ADF4106_R_COUNTER(ADF4106_R_COUNTER_MASK)) \
425 >> ADF4106_R_COUNTER_OFFSET);
426 CONSOLE_Print("\t+ Anti-backlash Width : 0x%1x\r\n", \
427 (rLatchBuff & ADF4106_R_ABP(ADF4106_R_ABP_MASK)) \
428 >> ADF4106_R_ABP_OFFSET);
429 CONSOLE_Print("\t+ Test Modes Bits : 0x%1x\r\n", \
430 (rLatchBuff & ADF4106_R_TMB(ADF4106_R_TMB_MASK)) \
431 >> ADF4106_R_TMB_OFFSET);
432 CONSOLE_Print("\t+ Lock Detect Precision : 0x%1x\r\n", \
433 (rLatchBuff & ADF4106_R_LDP(ADF4106_R_LDP_MASK)) \
434 >> ADF4106_R_LDP_OFFSET);
435 break;
436 case ADF4106_CTRL_N_COUNTER :
437 CONSOLE_Print("Bellow are the %s content : 0x%4x\r\n", \
438 latchName[latch], nLatchBuff);
439 if(deviceType == ID_ADF4106)
440 {
441 CONSOLE_Print("\t+ 6-bit A Counter : %4x\r\n", \
442 (nLatchBuff & ADF4106_N_COUNTER_A(ADF4106_N_COUNTER_A_MASK)) \
443 >> ADF4106_N_COUNTER_A_OFFSET);
444 }
445 CONSOLE_Print("\t+ 13-bit B Counter : %4x\r\n", \
446 (nLatchBuff & ADF4106_N_COUNTER_B(ADF4106_N_COUNTER_B_MASK)) \
447 >> ADF4106_N_COUNTER_B_OFFSET);
448 CONSOLE_Print("\t+ Charge Pump Gain : 0x%1x\r\n", \
449 (nLatchBuff & ADF4106_N_CP(ADF4106_N_CP_MASK)) \
450 >> ADF4106_N_CP_OFFSET);
451 break;
452 case ADF4106_CTRL_FUNCTION_LATCH :
453 case ADF4106_CTRL_INIT_LATCH :
454 CONSOLE_Print("Bellow are the %s content : 0x%4x\r\n", \
455 latchName[latch], fiLatchBuff);
456 CONSOLE_Print("\t+ Counter Reset : 0x%1x\r\n", \
457 (fiLatchBuff & ADF4106_CR(ADF4106_CR_MASK)) \
458 >> ADF4106_CR_OFFSET);
459 CONSOLE_Print("\t+ Power Down 1 : 0x%1x\r\n", \
460 (fiLatchBuff & ADF4106_PD1(ADF4106_PD1_MASK)) \
461 >> ADF4106_PD1_OFFSET);
462 CONSOLE_Print("\t+ Muxout Control : 0x%1x\r\n", \
463 (fiLatchBuff & ADF4106_MUXOUT(ADF4106_MUXOUT_MASK)) \
464 >> ADF4106_MUXOUT_OFFSET);
465 CONSOLE_Print("\t+ PD Polarity : 0x%1x\r\n", \
466 (fiLatchBuff & ADF4106_PDPOL(ADF4106_PDPOL_MASK)) \
467 >> ADF4106_PDPOL_OFFSET);
468 CONSOLE_Print("\t+ CP Three-State : 0x%1x\r\n",\
469 (fiLatchBuff & ADF4106_CP(ADF4106_CP_MASK)) \
470 >> ADF4106_CP_OFFSET);
471 CONSOLE_Print("\t+ FastLock Mode : 0x%1x\r\n", \
472 (fiLatchBuff & ADF4106_FASTLOCK(ADF4106_FASTLOCK_MASK)) \
473 >> ADF4106_FASTLOCK_OFFSET);
474 CONSOLE_Print("\t+ Timer Counter Control : 0x%1x\r\n", \
475 (fiLatchBuff & ADF4106_TCC(ADF4106_TCC_MASK)) \
476 >> ADF4106_TCC_OFFSET);
477 CONSOLE_Print("\t+ Current Settings 1 : 0x%1x\r\n", \
478 (fiLatchBuff & ADF4106_CS1(ADF4106_CS1_MASK)) \
479 >> ADF4106_CS1_OFFSET);
480 CONSOLE_Print("\t+ Current Settings 2 : 0x%1x\r\n", \
481 (fiLatchBuff & ADF4106_CS2(ADF4106_CS2_MASK)) \
482 >> ADF4106_CS2_OFFSET);
483 CONSOLE_Print("\t+ Power Down 2 : 0x%1x\r\n", \
484 (fiLatchBuff & ADF4106_PD2(ADF4106_PD2_MASK)) \
485 >> ADF4106_PD2_OFFSET);
486 CONSOLE_Print("\t+ Prescale Value : 0x%1x\r\n", \
487 (fiLatchBuff & ADF4106_PS(ADF4106_PS_MASK)) \
488 >> ADF4106_PS_OFFSET);
489 break;
490 }
491 }
492 else
493 {
494 /* Display error messages */
495 DisplayError(2);
496 }
497 }
498
499 /**************************************************************************//**
500 * @brief Set a desire frequency (the minimum and maximum values vary at each
501 * device), the unit of the frequency is Mhz
502 *
503 * @param - param[0] - the value of the desired frequency in Mhz
504 *
505 * @return - None
506 ******************************************************************************/
507 void SetVcoFrequency(double* param, char paramNo)
508 {
509 unsigned long long vcoFrequency = 0;
510
511 if(paramNo >= 1)
512 {
513 paramLimit(&param[0], vcoMinFrequency, vcoMaxFrequency);
514 /* convert the unit to Hz */
515 vcoFrequency = (unsigned long long)param[0] * 1000000;
516 vcoFrequency = ADF4106_SetFrequency(vcoFrequency);
517 /* Send feedback to user */
518 CONSOLE_Print("The VCO frequency was configured to %dMhz\r\n",
519 vcoFrequency/1000000);
520 actualVcoFreq = vcoFrequency;
521 /* Update the latch buffers */
522 rLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_R_COUNTER);
523 nLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_N_COUNTER);
524 fiLatchBuff = ADF4106_ReadLatch(ADF4106_CTRL_FUNCTION_LATCH);
525 }
526 else
527 {
528 /* Display error messages */
529 DisplayError(3);
530 }
531 }
532
533 /**************************************************************************//**
534 * @brief Get the current VCO frequency (the minimum and maximum values vary at
535 * each device), the unit of the frequency is Mhz
536 *
537 * @param - None
538 *
539 * @return - None
540 ******************************************************************************/
541 void GetVcoFrequency(double* param, char paramNo)
542 {
543 CONSOLE_Print("The actual VCO frequency is %dMHz\r\n", \
544 actualVcoFreq / 1000000);
545 }