Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Commanad.c
3 * @brief Implementation of the commands given by user through UART for CN0178.
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 "ad7466.h"
48 #include "TIME.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 = "calibration=",
63 .description = "Makes a four points calibration.",
64 .acceptedValue = "Accepted values:\r\n\
65 \tpower input1(lowest value): \r\n\
66 \t-60 .. 0 - first point input power in [dBm].\r\n\
67 \tpower input2: \r\n\
68 \t-60 .. 0 - second point input power in [dBm].\r\n\
69 \tpower input3: \r\n\
70 \t-60 .. 0 - third point input power in [dBm].\r\n\
71 \tpower input4(highest value): \r\n\
72 \t-60 .. 0 - fourth point input power in [dBm].",
73 .example = "To choose -30[dBm], -20[dBm], -10[dBm] and 0[dBm as the \
74 two calibration points, type: calibration=-30 -20 -10 0",
75 },
76 [2] = {
77 .name = "pinCalc?",
78 .description = "Displays the calculated input power.",
79 .acceptedValue = "",
80 .example = "",
81 },
82 [3] = {
83 .name = "error?",
84 .description = "Displays the error associated with the last input \
85 power calculation.",
86 .acceptedValue = "Accepted values: \r\n\
87 \t-60 .. 0 - true input power in [dBm].",
88 .example = "To display the error between the last calculated input \
89 power and the last true \r\n input power=-20[dBm], type: error?-20",
90 },
91 };
92
93 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
94
95 /******************************************************************************/
96 /************************ Variables Definitions *******************************/
97 /******************************************************************************/
98 cmdFunction cmdFunctions[] = {GetHelp, DoCalibration, GetPinCalc, GetError};
99
100 /* Variables holding information about the device */
101 float slope[3] = { 60, 60, 60}; // nominal values
102 float intercept[3] = {-50,-50,-50}; // nominal values
103 char pin[4] = {0, 0, 0, 0};
104 unsigned short code[4] = {0, 0, 0, 0};
105 float pinCalc = 0;
106
107 /***************************************************************************//**
108 * @brief Displays all available commands.
109 *
110 * @return None.
111 *******************************************************************************/
112 void GetHelp(double* param, char paramNo) // "help?" command
113 {
114 unsigned char displayCmd;
115
116 CONSOLE_Print("Available commands:\r\n");
117 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
118 {
119 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
120 (char*)cmdList[displayCmd].description,
121 (char*)cmdList[displayCmd].acceptedValue);
122 }
123 }
124
125 /***************************************************************************//**
126 * @brief Internal function for displaying all the command with its description.
127 *
128 * @return None.
129 *******************************************************************************/
130 void DisplayCmdList()
131 {
132 unsigned char displayCmd;
133
134 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
135 {
136 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
137 (char*)cmdList[displayCmd].description);
138 }
139 }
140
141 /***************************************************************************//**
142 * @brief Displays error message.
143 *
144 * @return None.
145 *******************************************************************************/
146 void DisplayError(unsigned char funcNo)
147 {
148 /* Display error messages */
149 CONSOLE_Print("Invalid parameter!\r\n");
150 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
151 (char*)cmdList[funcNo].description, \
152 (char*)cmdList[funcNo].acceptedValue);
153 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
154 }
155
156 /***************************************************************************//**
157 * @brief Initializes the device.
158 *
159 * @return - The result of the initialization.
160 * Example: ERROR - the device was not initialized or the device
161 * is not present.
162 * SUCCES - the device was initialized and the device
163 * is present.
164 *******************************************************************************/
165 char DoDeviceInit(void)
166 {
167 if(AD7466_Init() == 0)
168 {
169 CONSOLE_Print("Device OK\r\n");
170 DisplayCmdList();
171 return SUCCESS;
172 }
173 else
174 {
175 CONSOLE_Print("Device Error\r\n");
176 return ERROR;
177 }
178 }
179
180 /***************************************************************************//**
181 * @brief Makes a four points calibration.
182 *
183 * @param param[0] - first point input power in [dBm].
184 * param[1] - second point input power in [dBm].
185 * param[2] - third point input power in [dBm].
186 * param[3] - fourth point input power in [dBm].
187 *
188 * @return None.
189 *******************************************************************************/
190 void DoCalibration(double* param, char paramNo) // "calibration=" command
191 {
192 unsigned char count = 0;
193 unsigned long average[4] = {0,0,0,0};
194
195 /* Check if the parameters are valid. */
196 if(paramNo >= 4)
197 {
198 if (param[0] < -60)
199 {
200 param[0] = -60;
201 }
202 if (param[0] > 0)
203 {
204 param[0] = 0;
205 }
206 pin[0] = (char)param[0];
207 if (param[1] < -60)
208 {
209 param[1] = -60;
210 }
211 if (param[1] > 0)
212 {
213 param[1] = 0;
214 }
215 pin[1] = (char)param[1];
216 if (param[2] < -60)
217 {
218 param[2] = -60;
219 }
220 if (param[2] > 0)
221 {
222 param[2] = 0;
223 }
224 pin[2] = (char)param[2];
225 if (param[3] < -60)
226 {
227 param[3] = -60;
228 }
229 if (param[3] > 0)
230 {
231 param[3] = 0;
232 }
233 pin[3] = (char)param[3];
234
235 CONSOLE_Print("First set the input power to %d[dBm].\r\n", pin[0]);
236 CONSOLE_Print("You have 10 seconds for that.\r\n");
237 /* Make a 10 seconds delay. */
238 for (count = 0; count < 10; count++)
239 {
240 TIME_DelayMs(1000);
241 }
242 /* Calculate the average ADC code from 50 samples. */
243 for (count = 0; count < 50; count++)
244 {
245 code[0] = (AD7466_Read16Bits()) & 0x0FFF;
246 average[0] += code[0];
247 }
248 average[0] /= 50;
249 code[0] = (unsigned short)average[0];
250
251 CONSOLE_Print("Now set the input power to %d[dBm].\r\n", pin[1]);
252 CONSOLE_Print("You have 10 seconds for that.\r\n");
253 /* Make a 10 seconds delay. */
254 for (count = 0; count < 10; count++)
255 {
256 TIME_DelayMs(1000);
257 }
258 /* Calculate the average ADC code from 50 samples. */
259 for (count = 0; count < 50; count++)
260 {
261 code[1] = (AD7466_Read16Bits()) & 0x0FFF;
262 average[1] += code[1];
263 }
264 average[1] /= 50;
265 code[1] = (unsigned short)average[1];
266 /* Calculate the first slope and intercept according to datasheet formulas. */
267 slope[0] = (float)(code[1] - code[0]) / (pin[1] - pin[0]);
268 intercept[0] = (float)pin[1] - (float)(code[1] / slope[0]);
269 /* Send feedback to user */
270 CONSOLE_Print("The first calculated slope is %.3f.\r\n", slope[0]);
271 CONSOLE_Print("The first calculated intercept is %.3f.\r\n", intercept[0]);
272 CONSOLE_Print("\r\n");
273
274 CONSOLE_Print("Now set the input power to %d[dBm].\r\n", pin[2]);
275 CONSOLE_Print("You have 10 seconds for that.\r\n");
276 /* Make a 10 seconds delay. */
277 for (count = 0; count < 10; count++)
278 {
279 TIME_DelayMs(1000);
280 }
281 /* Calculate the average ADC code from 50 samples. */
282 for (count = 0; count < 50; count++)
283 {
284 code[2] = (AD7466_Read16Bits()) & 0x0FFF;
285 average[2] += code[2];
286 }
287 average[2] /= 50;
288 code[2] = (unsigned short)average[2];
289 /* Calculate the second slope and intercept according to datasheet formulas. */
290 slope[1] = (float)(code[2] - code[1]) / (pin[2] - pin[1]);
291 intercept[1] = (float)pin[2] - (float)(code[2] / slope[1]);
292 /* Send feedback to user */
293 CONSOLE_Print("The second calculated slope is %.3f.\r\n", slope[1]);
294 CONSOLE_Print("The second calculated intercept is %.3f.\r\n", intercept[1]);
295 CONSOLE_Print("\r\n");
296
297 CONSOLE_Print("Now set the input power to %d[dBm].\r\n", pin[3]);
298 CONSOLE_Print("You have 10 seconds for that.\r\n");
299 /* Make a 10 seconds delay. */
300 for (count = 0; count < 10; count++)
301 {
302 TIME_DelayMs(1000);
303 }
304 /* Calculate the average ADC code from 50 samples. */
305 for (count = 0; count < 50; count++)
306 {
307 code[3] = (AD7466_Read16Bits()) & 0x0FFF;
308 average[3] += code[3];
309 }
310 average[3] /= 50;
311 code[3] = (unsigned short)average[3];
312 /* Calculate the third slope and intercept according to datasheet formulas. */
313 slope[2] = (float)(code[3] - code[2]) / (pin[3] - pin[2]);
314 intercept[2] = (float)pin[3] - (float)(code[3] / slope[2]);
315 /* Send feedback to user */
316 CONSOLE_Print("The last calculated slope is %.3f.\r\n", slope[2]);
317 CONSOLE_Print("The last calculated intercept is %.3f.\r\n", intercept[2]);
318 }
319 else
320 {
321 /* Display error messages */
322 DisplayError(1);
323 }
324 }
325
326 /***************************************************************************//**
327 * @brief Displays the calculated input power.
328 *
329 * @return None.
330 *******************************************************************************/
331 void GetPinCalc(double* param, char paramNo) // "pinCalc?" command
332 {
333 unsigned char count = 0;
334 unsigned short codeRead = 0;
335 unsigned long average = 0;
336 float slopeChosen = 0;
337 float interceptChosen = 0;
338
339 /* Calculate the average ADC code from 50 samples. */
340 for (count = 0; count < 50; count++)
341 {
342 codeRead = (AD7466_Read16Bits()) & 0x0FFF;
343 average += codeRead;
344 }
345 average /= 50;
346 codeRead = (unsigned short)average;
347 /* Verify if the code read is inside the calibration range. */
348 if ((codeRead < (code[0] - 5)) || (codeRead > (code[3] + 5)))
349 {
350 CONSOLE_Print("ADC Code=%d. \r\n", codeRead);
351 CONSOLE_Print("Input power is outside calibration range! \r\n");
352 }
353 else
354 {
355 /* Choose which slope and intercept to use. */
356 if ((codeRead >= (code[0] - 5)) && (codeRead < code[1]))
357 {
358 slopeChosen = slope[0];
359 interceptChosen = intercept[0];
360 }
361 if ((codeRead >= code[1]) && (codeRead < code[2]))
362 {
363 slopeChosen = slope[1];
364 interceptChosen = intercept[1];
365 }
366 if ((codeRead >= code[2]) && (codeRead <= (code[3] + 5)))
367 {
368 slopeChosen = slope[2];
369 interceptChosen = intercept[2];
370 }
371 /* Calculate the input power according to datasheet formula. */
372 pinCalc = (float)(codeRead / slopeChosen) + interceptChosen;
373 /* Send feedback to user */
374 CONSOLE_Print("ADC Code=%d. \r\n", codeRead);
375 CONSOLE_Print("The calculated input power is %.3f[dBm]. \r\n", pinCalc);
376 }
377 }
378
379 /***************************************************************************//**
380 * @brief Displays the error associated with the last input power calculation.
381 *
382 * @param param[0] - last true input power.
383 *
384 * @return None.
385 *******************************************************************************/
386 void GetError(double* param, char paramNo) // "error?" command
387 {
388 float error = 0;
389
390 if(paramNo >= 1)
391 {
392 if (param[0] < -60)
393 {
394 param[0] = -60;
395 }
396 if (param[0] > 0)
397 {
398 param[0] = 0;
399 }
400 /* Calculate the error according to datasheet formula. */
401 error = pinCalc - param[0];
402 /* Send feedback to user */
403 CONSOLE_Print("The error between the last calculated input power and \
404 the last true input power is %.3f. \r\n", error);
405 }
406 else
407 {
408 /* Display error messages */
409 DisplayError(3);
410 }
411 }