Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Microchip/PIC32MX320F128H/PmodIA/Command.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodTMP2.
4 * @author DNechita ([email protected])
5 ********************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 *******************************************************************************/
40
41 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include "Command.h"
45 #include "AD5933.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51 /*!< List of available commands */
52 const char* cmdList[] = { "help?",
53 "temperature?",
54 "startFreq=",
55 "incFreq=",
56 "incNum=",
57 "sweepParam?",
58 "calibImpedance=",
59 "impedance?",
60 "currentFreq?"};
61 const char* cmdDescription[] = {
62 " - Displays all available commands.",
63 " - Displays the ambient device temperature.",
64 " - Start frequency parameter. Accepted values:\r\n\
65 \t1 000 .. 100 000 [Hz].",
66 " - Frequency increment parameter. Accepted values:\r\n\
67 \t0 .. 100 000 [Hz].",
68 " - Number of increments parameter. Accepted values:\r\n\
69 \t0 .. 511.",
70 " - Displays all three sweep parameters.",
71 " - Calibration impedance value. Accepted values:\r\n\
72 \t1 000 .. 10 000 000 [Ohm].",
73 " - Displays the measured impedance.",
74 " - Displays the current frequency."};
75 const char* cmdExample[] = {
76 "",
77 "",
78 "To set the start frequency to 1000 [Hz], type: startFreq=1000",
79 "To set the frequency increment to 100 [Hz], type: incFreq=100",
80 "To set the number of increments to 10, type: incNum=10",
81 "",
82 "To set the calibration impedance to 10000[Ohm], type calibImpedance=10000",
83 "",
84 ""};
85 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
86 const long MHZ_4 = 4000000; /*!< 16MHz / 4 */
87 const long P_2_27 = 134217728; /*!< 2 to the power of 27 */
88 const long MAX_START_FREQ = 100000; /*!< 100 KHz maximum frequency */
89 const short MIN_START_FREQ = 1000; /*!< 1KHz minimum frequency */
90 const short MAX_INC_NUM = 511; /*!< Value is represented by a 9-bit word */
91 const short MIN_IMPEDANCE = 1000; /*!< 1KOhm minimum impedance */
92 const long MAX_IMPEDANCE = 10000000; /*!< 10MOhms maximum impedance */
93
94 /******************************************************************************/
95 /************************ Variables Definitions *******************************/
96 /******************************************************************************/
97 cmdFunction cmdFunctions[9] = {GetHelp, GetTemperature, SetStartFreq, SetIncFreq,
98 SetIncNum, GetSweepParam, SetCalibImpedance,
99 GetImpedance, GetCurrentFreq};
100 /*!< Variables holding information about the device */
101 float temperature = 0.0; /*!< Last temperature read from the device */
102 unsigned long startFreq = 1000; /*!< Start frequency sweep */
103 unsigned long incFreq = 0; /*!< Increment frequency */
104 unsigned short incNum = 0; /*!< Number of increments */
105 unsigned long calibImped = 0; /*!< Calibration impedance */
106 double gainFactor = 0; /*!< Stores the value of the gain factor */
107 double impedance = 0; /*!< Measured impedance */
108 unsigned long currentFreq = 0; /*!< Signal frequency used during a measurement */
109
110 /***************************************************************************//**
111 * @brief Displays all available commands.
112 *
113 * @return None.
114 *******************************************************************************/
115 void GetHelp(double* param, char paramNo) /*!< "help?" command */
116 {
117 char displayCmd;
118
119 CONSOLE_Print("Available commands:\r\n");
120 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
121 {
122 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
123 (char*)cmdDescription[displayCmd]);
124 }
125 }
126
127 /***************************************************************************//**
128 * @brief Initializes the device.
129 *
130 * @return - The result of the initialization.
131 * Example: ERROR - the device was not initialized or the device
132 * is not present.
133 * SUCCES - the device was initialized and the device
134 * is present.
135 *******************************************************************************/
136 char DoDeviceInit(void)
137 {
138 if(AD5933_Init() == 0)
139 {
140 CONSOLE_Print("AD5933 OK\r\n");
141 GetHelp(NULL, 0);
142
143 return SUCCESS;
144 }
145 else
146 {
147 CONSOLE_Print("AD5933 Error\r\n");
148
149 return ERROR;
150 }
151 }
152
153 /***************************************************************************//**
154 * @brief Displays the temperature.
155 *
156 * @return None.
157 *******************************************************************************/
158 void GetTemperature(double* param, char paramNo) /*!< "temperature?" command */
159 {
160 /*!< Read the temperature from the device */
161 temperature = AD5933_GetTemperature();
162 /*!< Send the requested value to user */
163 CONSOLE_Print("temperature=%.1f [C]\r\n",temperature);
164 }
165
166 /***************************************************************************//**
167 * @brief Sets the start frequency parameter for sweep operation.
168 *
169 * @return None.
170 *******************************************************************************/
171 void SetStartFreq(double* param, char paramNo) /*!< "startFreq=" command */
172 {
173 /*!< Check if the parameter is valid */
174 if(paramNo >= 1)
175 {
176 if(param[0] < MIN_START_FREQ)
177 {
178 param[0] = MIN_START_FREQ;
179 }
180 if(param[0] > MAX_START_FREQ)
181 {
182 param[0] = MAX_START_FREQ;
183 }
184
185 startFreq = (unsigned long)(param[0] + 0.5);
186
187 /*!< Configure the sweep parameters */
188 AD5933_ConfigSweep(startFreq,
189 incFreq,
190 incNum);
191 /*!< Start the sweep operation */
192 AD5933_StartSweep();
193 /*!< Update the currentFrequrncy */
194 currentFreq = startFreq;
195 /*!< Send feedback to user */
196 CONSOLE_Print("%s%d Hz\r\n",(char*)cmdList[2], startFreq);
197 }
198 else
199 {
200 /*!< Display error messages */
201 CONSOLE_Print("Invalid parameter!\r\n");
202 CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]);
203 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]);
204 }
205 }
206
207 /***************************************************************************//**
208 * @brief Sets the frequency increment parameter for sweep operation.
209 *
210 * @return None.
211 *******************************************************************************/
212 void SetIncFreq(double* param, char paramNo) /*!< "incFreq=" command */
213 {
214
215 /*!< Check if the parameter is valid */
216 if(paramNo >= 1)
217 {
218 if(param[0] < 0)
219 {
220 param[0] = 0;
221 }
222 if(param[0] > MAX_START_FREQ)
223 {
224 param[0] = MAX_START_FREQ;
225 }
226
227 incFreq = (unsigned long)(param[0] + 0.5);
228 /*!< Configure the sweep parameters */
229 AD5933_ConfigSweep(startFreq,
230 incFreq,
231 incNum);
232 /*!< Start the sweep operation */
233 AD5933_StartSweep();
234 /*!< Send feedback to user */
235 CONSOLE_Print("%s%d Hz\r\n",(char*)cmdList[3], incFreq);
236 }
237 else
238 {
239 /*!< Display error messages */
240 CONSOLE_Print("Invalid parameter!\r\n");
241 CONSOLE_Print("%s%s\r\n", (char*)cmdList[3], (char*)cmdDescription[3]);
242 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[3]);
243 }
244 }
245
246 /***************************************************************************//**
247 * @brief Sets the number of increments parameter for sweep operation.
248 *
249 * @return None.
250 *******************************************************************************/
251 void SetIncNum(double* param, char paramNo) /*!< "incNum=" command */
252 {
253 /*!< Check if the parameter is valid */
254 if(paramNo >= 1)
255 {
256 if(param[0] < 0)
257 {
258 param[0] = 0;
259 }
260 if(param[0] > MAX_INC_NUM)
261 {
262 param[0] = MAX_INC_NUM;
263 }
264 incNum = (unsigned short)param[0];
265 /*!< Configure the sweep parameters */
266 AD5933_ConfigSweep(startFreq,
267 incFreq,
268 incNum);
269 /*!< Start the sweep operation */
270 AD5933_StartSweep();
271 /*!< Send feedback to user */
272 CONSOLE_Print("%s%d \r\n",(char*)cmdList[4], incNum);
273 }
274 else
275 {
276 /*!< Display error messages */
277 CONSOLE_Print("Invalid parameter!\r\n");
278 CONSOLE_Print("%s%s\r\n", (char*)cmdList[4], (char*)cmdDescription[4]);
279 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[4]);
280 }
281 }
282
283 /***************************************************************************//**
284 * @brief Displays all three sweep parameters.
285 *
286 * @return None.
287 *******************************************************************************/
288 void GetSweepParam(double* param, char paramNo) /*!< "sweepParam?" command */
289 {
290 /*!< Send the requested values to user */
291 CONSOLE_Print("%s%d Hz\r\n",cmdList[2], startFreq);
292 CONSOLE_Print("%s%d Hz\r\n",cmdList[3], incFreq);
293 CONSOLE_Print("%s%d\r\n",cmdList[4], incNum);
294 }
295
296 /***************************************************************************//**
297 * @brief Sets the calibration impedance value.
298 *
299 * @return None.
300 *******************************************************************************/
301 void SetCalibImpedance(double* param, char paramNo) /*!< "calibImpedance=" command */
302 {
303 /*!< Check if the parameter is valid */
304 if(paramNo >= 1)
305 {
306 if(param[0] < MIN_IMPEDANCE)
307 {
308 param[0] = MIN_IMPEDANCE;
309 }
310 if(param[0] > MAX_IMPEDANCE)
311 {
312 param[0] = MAX_IMPEDANCE;
313 }
314 calibImped = (unsigned long)param[0];
315
316 /*!< Calculate the gain factor for the selected impedance */
317 gainFactor = AD5933_CalculateGainFactor(calibImped,
318 AD5933_FUNCTION_REPEAT_FREQ);
319 /*!< Send feedback to user */
320 CONSOLE_Print("Gain was calculated for Z=%d [Ohm]\r\n",calibImped);
321 }
322 else
323 {
324 /*!< Display error messages */
325 CONSOLE_Print("Invalid parameter!\r\n");
326 CONSOLE_Print("%s%s\r\n", (char*)cmdList[6], (char*)cmdDescription[6]);
327 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[6]);
328 }
329 }
330
331 /***************************************************************************//**
332 * @brief Displays the measured impedance.
333 *
334 * @return None.
335 *******************************************************************************/
336 void GetImpedance(double* param, char paramNo) /*!< "impedance?" command */
337 {
338 double tempValue = 0;
339
340 /*!< Calculates the impedance between the VOUT and VIN pins. */
341 impedance = AD5933_CalculateImpedance(gainFactor,
342 AD5933_FUNCTION_INC_FREQ);
343 /*!< Send the requested value to user */
344 if(impedance >= 1000000)
345 {
346 tempValue = (double)impedance / 1000000;
347 CONSOLE_Print("impedance=%.3f [MOhm]\r\n",tempValue);
348 }
349 else if (impedance >= 1000)
350 {
351 tempValue = (double)impedance / 1000;
352 CONSOLE_Print("impedance=%.3f [KOhm]\r\n",tempValue);
353 }
354 else
355 {
356 tempValue = (double)impedance;
357 CONSOLE_Print("impedance=%.3f [Ohm]\r\n",tempValue);
358 }
359 /*!< Update the currentFrequrncy */
360 currentFreq = currentFreq + incFreq;
361 }
362
363 /***************************************************************************//**
364 * @brief Displays the current frequency.
365 *
366 * @return None.
367 *******************************************************************************/
368 void GetCurrentFreq(double* param, char paramNo) /*!< "currentFreq?" command */
369 {
370
371 CONSOLE_Print("%d [Hz]\r\n",currentFreq);
372
373 }