Debian Codegraph @

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

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