Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodAD5.
4 * @author DBogdan ([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 "AD7193.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51 /*!< List of available commands */
52 const char* cmdList[] =
53 {"help?",
54 "reset!",
55 "polarity?",
56 "polarity=",
57 "range?",
58 "range=",
59 "pseudoBit?",
60 "pseudoBit=",
61 "register?",
62 "voltage?",
63 "temperature?"};
64 const char* cmdDescription[] ={
65 " - Displays all available commands.",
66 " - Resets the AD7193.",
67 " - Displays the polarity value.",
68 " - Sets the polarity value. Accepted values: \r\n\
69 \t0 - Bipolar mode. \r\n\
70 \t1 - Unipolar mode.",
71 " - Displays the range value.",
72 " - Sets the range value. Accepted values: \r\n\
73 \t0 - Gain=1. (Input voltage range must be +/-2.5V) \r\n\
74 \t3 - Gain=8. (Input voltage range must be +/-312.5mV)\r\n\
75 \t4 - Gain=16. (Input voltage range must be +/-156.2mV)\r\n\
76 \t5 - Gain=32. (Input voltage range must be +/-78.12mV) \r\n\
77 \t6 - Gain=64. (Input voltage range must be +/-39.06mV) \r\n\
78 \t7 - Gain=128. (Input voltage range must be +/-19.53mV)",
79 " - Displays the Pseudo Bit value (AD7193_REG_CONF).",
80 " - Sets the Pseudo Bit Value (AD7193_REG_CONF). Accepted values: \r\n\
81 \t0 - Disables the pseudo differential measuring. \r\n\
82 \t1 - Enables the pseudo differential measuring.",
83 " - Displays the value of the data register (AD7193_REG_DATA) for specified \
84 channel. Accepted values: \r\n\
85 \t0..7 - selected channel.",
86 " - Displays the voltage applied to specified channel. Accepted values: \r\n\
87 \t0..7 - selected channel.",
88 " - Displays the temperature."};
89 const char* cmdExample[] = {
90 "",
91 "",
92 "",
93 "To set the bipolar mode, type: polarity=0",
94 "",
95 "To set the gain=16, type range=4",
96 "",
97 "To enable the pseudo differential measuring, type: pseudoBit=1",
98 "To display the data register for channel 1, type: register?1",
99 "To display the voltage applied to channel 3, type: voltage?3",
100 ""};
101 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
102 const float V_REF = 2.5; /*!< Vref = 2.5V */
103
104 /******************************************************************************/
105 /************************ Variables Definitions *******************************/
106 /******************************************************************************/
107 cmdFunction cmdFunctions[13] = {GetHelp, DoReset, GetPolarity, SetPolarity,
108 GetRange, SetRange, GetPseudoBit, SetPseudoBit,
109 GetRegister, GetVoltage, GetTemperature};
110
111 /*!< Variables holding information about the device */
112 unsigned char channel = 0; /*!< Enabled channel */
113 unsigned char pseudoBit = 0; /*!< Pseudo Bit value */
114 unsigned char selectedPolarity = 0; /*!< Selected polarity value */
115 unsigned char selectedRange = 0; /*!< Selected range value */
116
117 /***************************************************************************//**
118 * @brief Displays all available commands.
119 *
120 * @return None.
121 *******************************************************************************/
122 void GetHelp(double* param, char paramNo) /*!< "help?" command */
123 {
124 char displayCmd;
125
126 CONSOLE_Print("Available commands:\r\n");
127 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
128 {
129 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
130 (char*)cmdDescription[displayCmd]);
131 }
132 }
133
134 /***************************************************************************//**
135 * @brief Initializes the device.
136 *
137 * @return - The result of the initialization.
138 * Example: ERROR - the device was not initialized or the device
139 * is not present.
140 * SUCCES - the device was initialized and the device
141 * is present.
142 *******************************************************************************/
143 char DoDeviceInit(void)
144 {
145 if(AD7193_Init() == 0)
146 {
147 CONSOLE_Print("AD7193 OK\r\n");
148 GetHelp(NULL, 0);
149 return SUCCESS;
150 }
151 else
152 {
153 CONSOLE_Print("AD7193 Error\r\n");
154 return ERROR;
155 }
156 }
157
158 /***************************************************************************//**
159 * @brief Resets the AD7193.
160 *
161 * @return None.
162 *******************************************************************************/
163 void DoReset(double* param, char paramNo) /*!< "reset!" command */
164 {
165 AD7193_Reset();
166 /*!< Selects bipolar operation and ADC's input range to +-2.5V. */
167 //AD7193_RangeSetup(0, AD7193_CONF_GAIN_1);
168 selectedPolarity = 0;
169 selectedRange = 7;
170 pseudoBit = 0;
171 /*!< Send feedback to user */
172 CONSOLE_Print("Device was reset.\r\n");
173 }
174
175 /***************************************************************************//**
176 * @brief Displays the polarity value.
177 *
178 * @return None.
179 *******************************************************************************/
180 void GetPolarity(double* param, char paramNo) /*!< "polarity?" command */
181 {
182 /*!< Send the requested value to user */
183 CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],selectedPolarity);
184 }
185
186 /***************************************************************************//**
187 * @brief Sets the polarity value.
188 *
189 * @return None.
190 *******************************************************************************/
191 void SetPolarity(double* param, char paramNo) /*!< "polarity=" command */
192 {
193 /*!< Check if the parameter is valid */
194 if(paramNo >= 1)
195 {
196 if(param[0] < 0)
197 {
198 param[0] = 0;
199 }
200 if(param[0] > 1)
201 {
202 param[0] = 1;
203 }
204
205 selectedPolarity = (unsigned char)param[0];
206
207 AD7193_RangeSetup(selectedPolarity, selectedRange);
208
209 /*!< Send feedback to user */
210 CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],selectedPolarity);
211 }
212 else
213 {
214 /*!< Display error messages */
215 CONSOLE_Print("Invalid parameter!\r\n");
216 CONSOLE_Print("%s%s\r\n", (char*)cmdList[3], (char*)cmdDescription[3]);
217 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[3]);
218 }
219 }
220
221 /***************************************************************************//**
222 * @brief Displays the range value.
223 *
224 * @return None.
225 *******************************************************************************/
226 void GetRange(double* param, char paramNo) /*!< "range?" command */
227 {
228 /*!< Send the requested value to user */
229 CONSOLE_Print("%s%d\r\n",(char*)cmdList[5],selectedRange);
230 }
231
232 /***************************************************************************//**
233 * @brief Sets the range value.
234 *
235 * @return None.
236 *******************************************************************************/
237 void SetRange(double* param, char paramNo) /*!< "range=" command */
238 {
239 /*!< Check if the parameter is valid */
240 if(paramNo >= 1)
241 {
242 if(param[0] < 3)
243 {
244 param[0] = 0;
245 }
246 if(param[0] > 7)
247 {
248 param[0] = 7;
249 }
250
251 selectedRange = (unsigned char)param[0];
252
253 AD7193_RangeSetup(selectedPolarity, selectedRange);
254
255 /*!< Send feedback to user */
256 CONSOLE_Print("%s%d\r\n",(char*)cmdList[5],selectedRange);
257 }
258 else
259 {
260 /*!< Display error messages */
261 CONSOLE_Print("Invalid parameter!\r\n");
262 CONSOLE_Print("%s%s\r\n", (char*)cmdList[5], (char*)cmdDescription[5]);
263 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[5]);
264 }
265 }
266
267 /***************************************************************************//**
268 * @brief Displays the Pseudo Bit value (AD7193_REG_CONF).
269 *
270 * @return None.
271 *******************************************************************************/
272 void GetPseudoBit(double* param, char paramNo) /*!< "pseudoBit?" command */
273 {
274 /*!< Send the requested value to user */
275 CONSOLE_Print("%s%d\r\n",(char*)cmdList[7],pseudoBit);
276 }
277
278 /***************************************************************************//**
279 * @brief Sets the Pseudo Bit Value (AD7193_REG_CONF).
280 *
281 * @return None.
282 *******************************************************************************/
283 void SetPseudoBit(double* param, char paramNo) /*!< "pseudoBit=" command */
284 {
285 unsigned long regValue = 0;
286
287 /*!< Check if the parameter is valid */
288 if(paramNo >= 1)
289 {
290 if(param[0] < 0)
291 {
292 param[0] = 0;
293 }
294 if(param[0] > 1)
295 {
296 param[0] = 1;
297 }
298
299 switch((unsigned char)param[0])
300 {
301 case 1:
302 pseudoBit = 1;
303 regValue = AD7193_GetRegisterValue(AD7193_REG_CONF, 3, 1);
304 regValue |= AD7193_CONF_PSEUDO;
305 AD7193_SetRegisterValue(AD7193_REG_CONF, regValue, 3, 1);
306 break;
307 default:
308 pseudoBit = 0;
309 regValue = AD7193_GetRegisterValue(AD7193_REG_CONF, 3, 1);
310 regValue &= ~ AD7193_CONF_PSEUDO;
311 AD7193_SetRegisterValue(AD7193_REG_CONF, regValue, 3, 1);
312 break;
313 }
314
315 /*!< Send feedback to user */
316 CONSOLE_Print("%s%d\r\n",(char*)cmdList[7],pseudoBit);
317 }
318 else
319 {
320 /*!< Display error messages */
321 CONSOLE_Print("Invalid parameter!\r\n");
322 CONSOLE_Print("%s%s\r\n", (char*)cmdList[7], (char*)cmdDescription[7]);
323 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[7]);
324 }
325 }
326
327 /***************************************************************************//**
328 * @brief Displays the value of the data register (AD7193_REG_DATA) for the specified channel.
329 *
330 * @return None.
331 *******************************************************************************/
332 void GetRegister(double* param, char paramNo) /*!< "register?" command */
333 {
334 unsigned long data = 0;
335
336 /*!< Check if the parameter is valid */
337 if(paramNo >= 1)
338 {
339 if(param[0] < 0)
340 {
341 param[0] = 0;
342 }
343 if(param[0] > 7)
344 {
345 param[0] = 7;
346 }
347
348 channel = (unsigned char)param[0];
349 AD7193_ChannelSelect(channel);
350 /*!< Start a conversion */
351 data = AD7193_SingleConversion();
352 /*!< Send the requested value to user */
353 CONSOLE_Print("Register=%d for channel %d.\r\n",data,channel);
354 }
355 else
356 {
357 /*!< Display error messages */
358 CONSOLE_Print("Invalid parameter!\r\n");
359 CONSOLE_Print("%s%s\r\n", (char*)cmdList[8], (char*)cmdDescription[8]);
360 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[8]);
361 }
362 }
363
364 /***************************************************************************//**
365 * @brief Displays the temperature.
366 *
367 * @return None.
368 *******************************************************************************/
369 void GetTemperature(double* param, char paramNo) /*!< "temperature?" command */
370 {
371 float temperatureValue = 0;
372
373 /*!< Start a conversion */
374 temperatureValue = AD7193_TemperatureRead();
375 /*!< Send the requested value to user */
376 CONSOLE_Print("temperature=%.2f [C]\r\n",temperatureValue);
377 /*!< Return to previous polarity and gain*/
378 AD7193_RangeSetup(selectedPolarity, selectedRange);
379 }
380
381 /***************************************************************************//**
382 * @brief Displays the voltage applied to the specified channel.
383 *
384 * @return None.
385 *******************************************************************************/
386 void GetVoltage(double* param, char paramNo) /*!< "voltage?" command */
387 {
388 unsigned long data = 0;
389 double voltage = 0;
390
391 /*!< Check if the parameter is valid */
392 if(paramNo >= 1)
393 {
394 if(param[0] < 0)
395 {
396 param[0] = 0;
397 }
398 if(param[0] > 7)
399 {
400 param[0] = 7;
401 }
402
403 channel = (unsigned char)param[0];
404 AD7193_ChannelSelect(channel);
405 /*!< Start a conversion */
406 data = AD7193_SingleConversion();
407 voltage = AD7193_ConvertToVolts(data, V_REF);
408 /*!< Send the requested value to user */
409 CONSOLE_Print("Voltage=%.3f [V] for channel %d.\r\n",voltage,channel);
410 }
411 else
412 {
413 /*!< Display error messages */
414 CONSOLE_Print("Invalid parameter!\r\n");
415 CONSOLE_Print("%s%s\r\n", (char*)cmdList[9], (char*)cmdDescription[9]);
416 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[9]);
417 }
418 }