Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodAD3.
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 "AD7780.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51 /*!< List of available commands */
52 const char* cmdList[] = { "help?",
53 "power=",
54 "filter=",
55 "gain=",
56 "status=",
57 "register?",
58 "voltage?",
59 "samples?"};
60 const char* cmdDescription[] = {
61 " - Displays all available commands.",
62 " - Power off/on the device. Accepted values:\r\n\
63 \t0 - Power off.\r\n\
64 \t1 - Power on.",
65 " - Select the filter frequency. Accepted values: \r\n\
66 \t0 - Filter frequency is 16.7 Hz. \r\n\
67 \t1 - Filter frequency is 10 Hz.",
68 " - Select gain. Accepted values:\r\n\
69 \t0 - Gain is set to 128.(Input voltage range must be +/-25.78mV)\r\n\
70 \t1 - Gain is set to 1.(Input voltage range must be +/-3300mV)",
71 " - Displays the conversion status for 'register?' and 'voltage?' commands.\
72 Accepted values:\r\n\
73 \t0 - Disable status display.\r\n\
74 \t1 - Enable status display.",
75 " - Reads one sample from the input channel and displays the data register.",
76 " - Reads one sample from the input channel, converts it to voltage and displays the voltage.",
77 " - Reads a number of samples of the input channel and displays the voltage.\
78 Accepted values: \r\n\
79 \t1 .. 1024 - Number of samples."};
80 const char* cmdExample[] = {
81 "",
82 "To power on the device, type power=1",
83 "To set filter frequency to 10 Hz, type filter=1",
84 "To set the gain to 128, type gain=0",
85 "To enable the conversion status display, type status=1",
86 "",
87 "",
88 "To display 30 samples, type samples?30"};
89 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
90 const float V_REF = 3.3; /*!< Vref = 3.3V */
91
92 /******************************************************************************/
93 /************************ Variables Definitions *******************************/
94 /******************************************************************************/
95 cmdFunction cmdFunctions[8] = {GetHelp, SetPower, SetFilter, SetGain, SetStatus,
96 GetRegister, GetVoltage, GetSamples};
97
98 /*!< Variables holding information about the device */
99 unsigned char gain = 1;
100 unsigned char conversionStatus = 0;
101 unsigned char showStatus = 0;
102 unsigned char power = 1;
103
104 /***************************************************************************//**
105 * @brief Displays all available commands.
106 *
107 * @return None.
108 *******************************************************************************/
109 void GetHelp(double* param, char paramNo) /*!< "help?" command */
110 {
111 char displayCmd;
112
113 CONSOLE_Print("Available commands:\r\n");
114 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
115 {
116 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
117 (char*)cmdDescription[displayCmd]);
118 }
119 }
120
121 /***************************************************************************//**
122 * @brief Initializes the device.
123 *
124 * @return - The result of the initialization.
125 * Example: ERROR - the device was not initialized or the device
126 * is not present.
127 * SUCCES - the device was initialized and the device
128 * is present.
129 *******************************************************************************/
130 char DoDeviceInit(void)
131 {
132 if(AD7780_Init() == 0)
133 {
134 CONSOLE_Print("AD7780 OK\r\n");
135 GetHelp(NULL, 0);
136 return SUCCESS;
137 }
138 else
139 {
140 CONSOLE_Print("AD7780 Error\r\n");
141 return ERROR;
142 }
143 }
144
145 /***************************************************************************//**
146 * @brief Power off/on the device.
147 *
148 * @return None.
149 *******************************************************************************/
150 void SetPower(double* param, char paramNo) /*!< "power=" command */
151 {
152 /*!< Check if the parameter is valid */
153 if(paramNo >= 1)
154 {
155 if(param[0] < 0)
156 {
157 param[0] = 0;
158 }
159 if(param[0] > 1)
160 {
161 param[0] = 1;
162 }
163
164 power = (unsigned char)param[0];
165 if (power == 1)
166 {
167 /* Power-up the device. */
168 AD7780_PDRST_HIGH;
169 }
170 else
171 {
172 /* Power-down the device. */
173 AD7780_PDRST_LOW;
174 }
175 /*!< Send feedback to user */
176 CONSOLE_Print("%s%d\r\n",(char*)cmdList[1],power);
177 }
178 else
179 {
180 /*!< Display error messages */
181 CONSOLE_Print("Invalid parameter!\r\n");
182 CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]);
183 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]);
184 }
185 }
186
187 /***************************************************************************//**
188 * @brief Select the filter frequency.
189 *
190 * @return None.
191 *******************************************************************************/
192 void SetFilter(double* param, char paramNo) /*!< "fliter=" command */
193 {
194 unsigned char filter = 0;
195
196 /*!< Check if the parameter is valid */
197 if(paramNo >= 1)
198 {
199 if(param[0] < 0)
200 {
201 param[0] = 0;
202 }
203 if(param[0] > 1)
204 {
205 param[0] = 1;
206 }
207
208 filter = (unsigned char)param[0];
209 if (filter == 1)
210 {
211 AD7780_FILTER_HIGH; /*!< The update rate is set to 10 Hz. */
212 }
213 else
214 {
215 AD7780_FILTER_LOW; /*!< The update rate is set to 16.7 Hz. */
216 }
217 /*!< Send feedback to user */
218 CONSOLE_Print("%s%d\r\n",(char*)cmdList[2],filter);
219 }
220 else
221 {
222 /*!< Display error messages */
223 CONSOLE_Print("Invalid parameter!\r\n");
224 CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]);
225 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]);
226 }
227 }
228
229 /***************************************************************************//**
230 * @brief Select gain.
231 *
232 * @return None.
233 *******************************************************************************/
234 void SetGain(double* param, char paramNo) /*!< "gain=" command */
235 {
236 /*!< Check if the parameter is valid */
237 if(paramNo >= 1)
238 {
239 if(param[0] < 0)
240 {
241 param[0] = 0;
242 }
243 if(param[0] > 1)
244 {
245 param[0] = 1;
246 }
247 gain = (unsigned char)param[0];
248 if(gain == 1)
249 {
250 AD7780_GAIN_HIGH; /*!< Gain is set to 1. */
251 gain = 1;
252 }
253 else
254 {
255 AD7780_GAIN_LOW; /*!< Gain is set to 128. */
256 gain = 128;
257 }
258 /*!< Send feedback to user */
259 CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],(unsigned char)param[0]);
260 }
261 else
262 {
263 /*!< Display error messages */
264 CONSOLE_Print("Invalid parameter!\r\n");
265 CONSOLE_Print("%s%s\r\n", (char*)cmdList[3], (char*)cmdDescription[3]);
266 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[3]);
267 }
268 }
269
270
271
272 /***************************************************************************//**
273 * @brief Displays the conversion status for 'register?' and 'voltage?' commands.
274 *
275 * @return None.
276 *******************************************************************************/
277 void SetStatus(double* param, char paramNo) /*!< "status=" command */
278 {
279 /*!< Check if the parameter is valid */
280 if(paramNo >= 1)
281 {
282 if(param[0] < 0)
283 {
284 param[0] = 0;
285 }
286 if(param[0] > 1)
287 {
288 param[0] = 1;
289 }
290 showStatus = (unsigned char)param[0];
291 /*!< Send feedback to user */
292 CONSOLE_Print("%s%d\r\n",(char*)cmdList[4],gain);
293 }
294 else
295 {
296 /*!< Display error messages */
297 CONSOLE_Print("Invalid parameter!\r\n");
298 CONSOLE_Print("%s%s\r\n", (char*)cmdList[4], (char*)cmdDescription[4]);
299 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[4]);
300 }
301 }
302
303 /***************************************************************************//**
304 * @brief Reads one sample from the input channel and displays the data register.
305 *
306 * @return None.
307 *******************************************************************************/
308 void GetRegister(double* param, char paramNo) /*!< "register?" command */
309 {
310 unsigned long data = 0;
311
312 if (power == 1)
313 {
314 /*!< Read one sample form the device. */
315 data = AD7780_ReadSample(&conversionStatus);
316 /*!< Send the requested value to user */
317 CONSOLE_Print("register=%d\r\n",data);
318 if(showStatus)
319 {
320 /*!< Check gain flag. */
321 if(conversionStatus & AD7780_STAT_GAIN)
322 {
323 CONSOLE_Print("gain=1\r\n");
324 }
325 else
326 {
327 CONSOLE_Print("gain=128\r\n");
328 }
329 /*!< Check filter flag. */
330 if(conversionStatus & AD7780_STAT_FILTER)
331 {
332 CONSOLE_Print("filter=10 Hz\r\n");
333 }
334 else
335 {
336 CONSOLE_Print("filter=16.7 Hz\r\n");
337 }
338 /*!< Check for conversion error. */
339 if(conversionStatus & AD7780_STAT_ERR)
340 {
341 CONSOLE_Print("Conversion error occurred.\r\n");
342 }
343 else
344 {
345 CONSOLE_Print("No conversion error.\r\n");
346 }
347 /*!< Check for serial transfer error. */
348 if((conversionStatus &
349 (AD7780_STAT_PAT1 | AD7780_STAT_PAT0)) != 0x01)
350 {
351 CONSOLE_Print("Serial transfer error occurred.\r\n");
352 }
353 else
354 {
355 CONSOLE_Print("No serial transfer error.\r\n");
356 }
357 }
358 }
359 else
360 {
361 CONSOLE_Print("The device is powered off. Please use the 'power=1' \
362 command in order to power on the device.\r\n");
363 }
364 }
365
366 /***************************************************************************//**
367 * @brief Reads one sample from the input channel, converts it voltage and displays the voltage.
368 *
369 * @return None.
370 *******************************************************************************/
371 void GetVoltage(double* param, char paramNo) /*!< "voltage?" command */
372 {
373 unsigned long data = 0;
374 double voltage = 0;
375
376
377 if (power == 1)
378 {
379 /*!< Read one sample from the device. */
380 data = AD7780_ReadSample(&conversionStatus);
381 /*!< Convert raw data to voltage. */
382 voltage = AD7780_ConvertToVoltage(data, V_REF, gain);
383 /*!< Send the requested value to user */
384 CONSOLE_Print("voltage=%.3f [mV]\r\n",voltage);
385 if(showStatus)
386 {
387 /*!< Check gain flag. */
388 if(conversionStatus & AD7780_STAT_GAIN)
389 {
390 CONSOLE_Print("gain=1\r\n");
391 }
392 else
393 {
394 CONSOLE_Print("gain=128\r\n");
395 }
396 /*!< Check filter flag. */
397 if(conversionStatus & AD7780_STAT_FILTER)
398 {
399 CONSOLE_Print("filter=10 Hz\r\n");
400 }
401 else
402 {
403 CONSOLE_Print("filter=16.7 Hz\r\n");
404 }
405 /*!< Check for conversion error. */
406 if(conversionStatus & AD7780_STAT_ERR)
407 {
408 CONSOLE_Print("Conversion error occurred.\r\n");
409 }
410 else
411 {
412 CONSOLE_Print("No conversion error.\r\n");
413 }
414 /*!< Check for serial transfer error. */
415 if((conversionStatus &
416 (AD7780_STAT_PAT1 | AD7780_STAT_PAT0)) != 0x01)
417 {
418 CONSOLE_Print("Serial transfer error occurred.\r\n");
419 }
420 else
421 {
422 CONSOLE_Print("No serial transfer error.\r\n");
423 }
424 }
425 }
426 else
427 {
428 CONSOLE_Print("The device is powered off. Please use the 'power=1' \
429 command in order to power on the device.\r\n");
430 }
431 }
432
433 /***************************************************************************//**
434 * @brief Reads a number of samples of the input channel and displays the voltage.
435 *
436 * @return None.
437 *******************************************************************************/
438 void GetSamples(double* param, char paramNo) /*!< "samples?" command */
439 {
440 unsigned long data = 0;
441 double voltage = 0;
442 long samples = 0;
443
444 /*!< Check if the parameter is valid */
445 if(paramNo >= 1)
446 {
447 if(param[0] < 1)
448 {
449 param[0] = 1;
450 }
451 if(param[0] > 1024)
452 {
453 param[0] = 11024;
454 }
455 samples = (unsigned char)param[0];
456
457 if (power == 1)
458 {
459 while(samples--)
460 {
461 AD7780_WaitRdyGoLow();
462 /* Read one sample from the device. */
463 data = AD7780_ReadSample(&conversionStatus);
464 /* Convert raw data to voltage. */
465 voltage = AD7780_ConvertToVoltage(data, V_REF, gain);
466 /*!< Send the requested value to user */
467 CONSOLE_Print("voltage=%.3f [mV]\r\n",voltage);
468 }
469 }
470 else
471 {
472 CONSOLE_Print("The device is powered off. Please use the 'power=1' \
473 command in order to power on the device.\r\n");
474 }
475 }
476 else
477 {
478 /*!< Display error messages */
479 CONSOLE_Print("Invalid parameter!\r\n");
480 CONSOLE_Print("%s%s\r\n", (char*)cmdList[7], (char*)cmdDescription[7]);
481 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[7]);
482 }
483 }