Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Commanad.c
3 * @brief Implementation of the commands given by user through UART for CN0209.
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 "cn0209.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 = "ad7193Temp?",
63 .description = "Displays the temperature from AD7193 device.",
64 .acceptedValue = "",
65 .example = "",
66 },
67 [2] = {
68 .name = "ad7193id?",
69 .description = "Displays the AD7193 device ID.",
70 .acceptedValue = "",
71 .example = "",
72 },
73 [3] = {
74 .name = "ad7193Reset!",
75 .description = "Reset the serial interface with the AD7193.",
76 .acceptedValue = "",
77 .example = "",
78 },
79 [4] = {
80 .name = "adt7310Temp?",
81 .description = "Displays the temperature from ADT7310 device.",
82 .acceptedValue = "",
83 .example = "",
84 },
85 [5] = {
86 .name = "adt7310id?",
87 .description = "Displays the ADT7310 device ID.",
88 .acceptedValue = "",
89 .example = "",
90 },
91 [6] = {
92 .name = "adt7310Reset!",
93 .description = "Resets the serial interface with the ADT7310.",
94 .acceptedValue = "",
95 .example = "",
96 },
97 [7] = {
98 .name = "voltage?",
99 .description = "Displays the input voltage on selected channel.",
100 .acceptedValue = "Accepted values:\r\n\
101 \t1 - channel 1.\r\n\
102 \t2 - channel 2.",
103 .example = "",
104 },
105 [8] = {
106 .name = "current?",
107 .description = "Displays the input current on selected channel.",
108 .acceptedValue = "Accepted values:\r\n\
109 \t1 - channel 1.\r\n\
110 \t2 - channel 2.",
111 .example = "",
112 },
113 [9] = {
114 .name = "tempTC?",
115 .description = "Displays the temperature detected by the thermocouple \
116 connected on selected channel.",
117 .acceptedValue = "Accepted values:\r\n\
118 \t1 - channel 1.\r\n\
119 \t2 - channel 2.",
120 .example = "",
121 },
122 };
123
124 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
125 float VREF = 4.5;
126
127 /******************************************************************************/
128 /************************ Variables Definitions *******************************/
129 /******************************************************************************/
130 cmdFunction cmdFunctions[] = {GetHelp,GetAd7193Temp,GetAd7193id,DoAd7193Reset,
131 GetAdt7310Temp,GetAdt7310id,DoAdt7310Reset,
132 GetVoltage,GetCurrent,GetTempTC};
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(CN0209_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 Displays the temperature from AD7193 device.
209 *
210 * @param None.
211 *
212 * @return None.
213 *******************************************************************************/
214 void GetAd7193Temp(double* param, char paramNo) // "ad7193Temp?" command
215 {
216 float temp = 0;
217
218 temp = AD7193_TemperatureRead();
219 /* Send feedback to user */
220 CONSOLE_Print("AD7193 temperature=%.2f[C].\r\n",temp);
221 }
222
223 /***************************************************************************//**
224 * @brief Displays the AD7193 device ID.
225 *
226 * @param None.
227 *
228 * @return ad7193Id - requested device id.
229 *******************************************************************************/
230 void GetAd7193id(double* param, char paramNo) // "ad7193id?" command
231 {
232 unsigned long ad7193id;
233
234 ad7193id = AD7193_Read(AD7193_REG_ID, 1);
235 CONSOLE_Print("AD7193 ID = %d.\r\n",ad7193id);
236
237 }
238
239 /***************************************************************************//**
240 * @brief Reset the serial interface with the AD7193.
241 *
242 * @param None.
243 *
244 * @return None.
245 *******************************************************************************/
246 void DoAd7193Reset(double* param, char paramNo) // "ad7193Reset!" command
247 {
248 AD7193_Reset();
249 /* Send feedback to user */
250 CONSOLE_Print("AD7193 was reset.\r\n");
251 }
252
253 /***************************************************************************//**
254 * @brief Displays the temperature from ADT7310 device.
255 *
256 * @param None.
257 *
258 * @return None.
259 *******************************************************************************/
260 void GetAdt7310Temp(double* param, char paramNo) // "adt7310Temp?" command
261 {
262 float temp = 0;
263
264 temp = ADT7310_TemperatureRead();
265 /* Send feedback to user */
266 CONSOLE_Print("ADT7310 temperature=%.2f[C].\r\n",temp);
267 }
268
269 /***************************************************************************//**
270 * @brief Displays the ADT7310 device ID.
271 *
272 * @param None.
273 *
274 * @return ad7193Id - requested device id.
275 *******************************************************************************/
276 void GetAdt7310id(double* param, char paramNo) // "adt7310id?" command
277 {
278 unsigned long adt7310id;
279
280 adt7310id = ADT7310_Read(ADT7310_REG_ID, 1);
281
282 CONSOLE_Print("ADT7310 ID = %d.\r\n",adt7310id);
283 }
284
285 /***************************************************************************//**
286 * @brief Resets the serial interface with the ADT7310.
287 *
288 * @param None.
289 *
290 * @return None.
291 *******************************************************************************/
292 void DoAdt7310Reset(double* param, char paramNo) // "adt7310Reset!" command
293 {
294 ADT7310_Reset();
295 /* Send feedback to user */
296 CONSOLE_Print("ADT7310 was reset.\r\n");
297 }
298
299
300 /***************************************************************************//**
301 * @brief Displays the input voltage on selected channel.
302 *
303 * @param param[0] - selected channel.
304 *
305 * @return None.
306 *******************************************************************************/
307 void GetVoltage(double* param, char paramNo) // "voltage?" command
308 {
309 unsigned char channel;
310 unsigned char switchSel;
311 unsigned long value = 0;
312 float voltageValue = 0;
313
314 /* Check if the parameter is valid */
315 if(paramNo >= 1)
316 {
317 if(param[0] < 1)
318 {
319 param[0] = 1;
320 }
321 if(param[0] > 2)
322 {
323 param[0] = 2;
324 }
325 channel = (unsigned char)param[0];
326
327 if (channel == 1)
328 {
329 switchSel = 0x50;
330 }
331 else
332 {
333 switchSel = 0xA0;
334 }
335 ADG1414_State(switchSel);
336 AD7193_Write(AD7193_REG_CONF, 3, AD7193_CONF_PSEUDO +
337 AD7193_CONF_CHAN(0x04) +
338 AD7193_CONF_UNIPOLAR +
339 AD7193_CONF_GAIN(AD7193_CONF_GAIN_1));
340 value = AD7193_SingleConversion();
341 ADG1414_State(0x0);
342 voltageValue = (AD7193_ConvertToVolts(value,VREF,1,1)) * 1000; // uV
343 voltageValue = (2250000 - voltageValue);
344 voltageValue *= 5;
345
346 /* Send feedback to user */
347 CONSOLE_Print("Input voltage=%d[uV] for channel %d.\r\n",
348 (unsigned long)voltageValue, channel);
349 }
350 else
351 {
352 /* Display error messages */
353 DisplayError(7);
354 }
355 }
356
357 /***************************************************************************//**
358 * @brief Displays the input current on selected channel.
359 *
360 * @param param[0] - selected channel.
361 *
362 * @return None.
363 *******************************************************************************/
364 void GetCurrent(double* param, char paramNo) // "current?" command
365 {
366 unsigned char channel;
367 unsigned char channelSel;
368 unsigned long value = 0;
369 float currentValue = 0;
370
371 /* Check if the parameter is valid */
372 if(paramNo >= 1)
373 {
374 if(param[0] < 1)
375 {
376 param[0] = 1;
377 }
378 if(param[0] > 2)
379 {
380 param[0] = 2;
381 }
382 channel = (unsigned char)param[0];
383
384 if (channel == 2)
385 {
386 channelSel = 0x08;
387 }
388 else
389 {
390 channelSel = 0x80;
391 }
392
393 ADG1414_State(0x0);
394 AD7193_Write(AD7193_REG_CONF, 3,
395 AD7193_CONF_PSEUDO +
396 AD7193_CONF_CHAN(channelSel) +
397 AD7193_CONF_UNIPOLAR +
398 AD7193_CONF_GAIN(AD7193_CONF_GAIN_1));
399
400 value = AD7193_SingleConversion();
401 currentValue = AD7193_ConvertToVolts(value,VREF,1,1) * 1000; // uV
402 currentValue /= 200; // uA
403
404 /* Send feedback to user */
405 CONSOLE_Print("Current=%.2f[uA] for channel %d.\r\n",currentValue,
406 channel);
407 }
408 else
409 {
410 /* Display error messages */
411 DisplayError(8);
412 }
413 }
414
415 /***************************************************************************//**
416 * @brief Displays the temperature detected by the thermocouple connected on
417 * selected channel.
418 *
419 * @param param[0] - selected channel.
420 *
421 * @return None.
422 *******************************************************************************/
423 void GetTempTC(double* param, char paramNo) // "tempTC?" command
424 {
425 unsigned char channel;
426 unsigned char switchSel = 0;
427 unsigned char channelSel = 0;
428 unsigned long value = 0;
429 float tcValue = 0;
430 float tempRef = 0;
431
432 /* Check if the parameter is valid */
433 if(paramNo >= 1)
434 {
435 if(param[0] < 1)
436 {
437 param[0] = 1;
438 }
439 if(param[0] > 2)
440 {
441 param[0] = 2;
442 }
443 channel = (unsigned char)param[0];
444
445 if (channel == 1)
446 {
447 switchSel = 0x03;
448 channelSel = 0x01;
449 ADG1414_State(switchSel);
450 AD7193_Write(AD7193_REG_GPOCON, 1, AD7193_GPOCON_GP32EN +
451 AD7193_GPOCON_P2DAT); // Close S1, S2.
452 TIME_DelayMs(100);
453 AD7193_Write(AD7193_REG_CONF, 3, AD7193_CONF_CHAN(channelSel) +
454 AD7193_CONF_GAIN(AD7193_CONF_GAIN_128));
455 TIME_DelayMs(100);
456 }
457 else
458 {
459 switchSel = 0x0C;
460 channelSel = 0x04;
461 ADG1414_State(switchSel);
462 AD7193_Write(AD7193_REG_GPOCON, 1, AD7193_GPOCON_GP32EN +
463 AD7193_GPOCON_P3DAT); // Close S3, S4.*/
464 TIME_DelayMs(100);
465 AD7193_Write(AD7193_REG_CONF, 3, AD7193_CONF_CHAN(channelSel) +
466 AD7193_CONF_GAIN(AD7193_CONF_GAIN_128));
467 TIME_DelayMs(100);
468 }
469
470 value = AD7193_SingleConversion();
471 tcValue = AD7193_ConvertToVolts(value,VREF,128,0) * 1000; // uV
472 tcValue /= 40; // Type K - Degrees C.
473
474 tempRef = ADT7310_TemperatureRead();
475 tcValue += tempRef;
476
477 AD7193_Write(AD7193_REG_GPOCON, 1, 0x0);
478 ADG1414_State(0x0);
479 /* Send feedback to user */
480 CONSOLE_Print("TempTC=%.2f[C] for channel %d.\r\n",tcValue,channel);
481 }
482 else
483 {
484 /* Display error messages */
485 DisplayError(9);
486 }
487 }