Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodDA4.
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 "AD5628.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51 /*!< List of available commands */
52 const char* cmdList[] = {"help?",
53 "mode=",
54 "mode?",
55 "voltage=",
56 "voltage?",
57 "register=",
58 "register?"};
59 const char* cmdDescription[] = {
60 " - Displays all available commands.",
61 " - Selects a mode of operation for selected channel. Accepted values:\r\n\
62 \tchannel: \r\n\
63 \t0 .. 7 - Channel A .. H(one channel)\r\n\
64 \t15 - All channels. \r\n\
65 \tmode:\r\n\
66 \t0 - Normal operation. \r\n\
67 \t1 - 1 KOhm to GND (Power down mode). \r\n\
68 \t2 - 100 KOhm to GND (Power down mode). \r\n\
69 \t3 - Three-state (Power down mode).",
70 " - Displays the selected mode of operation for the selected channel. Accepted\
71 values: \r\n\
72 \t0 .. 7 - Channel A .. H(one channel)\r\n\
73 \t15 - All channels.",
74 " - Sets the output voltage for the selected channel. Accepted values: \r\n\
75 \tchannel: \r\n\
76 \t0 .. 7 - Channel A .. H(one channel)\r\n\
77 \t15 - All channels. \r\n\
78 \toutput voltage:\r\n\
79 \t0 .. 2500 - desired output voltage in milivolts.",
80 " - Displays the output voltage of the selected channel. Accepted values: \r\n\
81 \t0 .. 7 - Channel A .. H(one channel)\r\n\
82 \t15 - All channels.",
83 " - Writes the value into the DAC register of the selected channel.\
84 Accepted values: \r\n\
85 \tchannel: \r\n\
86 \t0 .. 7 - Channel A .. H(one channel)\r\n\
87 \t15 - All channels. \r\n\
88 \tregister value:\r\n\
89 \t0 .. 4095 - the value written to the DAC.",
90 " - Displays the value written in the DAC register of the selected channel. \
91 Accepted values: \r\n\
92 \t0 .. 7 - Channel A .. H(one channel)\r\n\
93 \t15 - All channels."};
94 const char* cmdExample[] = {
95 "",
96 "To select the power down mode 1KOhm to GND for channel C, type: mode=2 1",
97 "To display the mode of operation for channel A, type: mode?0",
98 "To set the output voltage to 1250mV for all channels, type: voltage=15 1250",
99 "To display the output voltage for channel H, type: voltage?7",
100 "To set DAC register value to 2500 for channel D, type: register=3 2500",
101 "To display the value of the DAC register for channel G, type: register?6"};
102 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
103 const float VREF = 1.25; /*!< Vref = 1.25V */
104
105 /******************************************************************************/
106 /************************ Variables Definitions *******************************/
107 /******************************************************************************/
108 cmdFunction cmdFunctions[7] = {GetHelp, SetMode, GetMode, SetVoltage,
109 GetVoltage, SetRegister, GetRegister};
110 /*!< Variables holding information about the device */
111 unsigned char powerMode = 0; /*!< Last powerMode value written to the device */
112 unsigned char currentChannel = 0; /*!< Last channel selected */
113 unsigned short registerValue = 0; /*!< Last register value written to the device */
114 unsigned short dacRegArray[8] = {0, 0, 0, 0, 0, 0, 0, 0};
115 unsigned char powerArray[8] = {0, 0, 0, 0, 0, 0, 0, 0};
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(AD5628_Init() == 0)
146 {
147 CONSOLE_Print("AD5628 OK\r\n");
148 GetHelp(NULL, 0);
149 return SUCCESS;
150 }
151 else
152 {
153 CONSOLE_Print("AD5628 Error\r\n");
154 return ERROR;
155 }
156 }
157
158 /***************************************************************************//**
159 * @brief Selects a mode of operation for selected channel.
160 *
161 * @return None.
162 *******************************************************************************/
163 void SetMode(double* param, char paramNo) /*!< "mode=" command */
164 {
165
166 /*!< Check if the parameters are valid */
167 if(paramNo >= 2)
168 {
169 if(param[0] < 0)
170 {
171 param[0] = 0;
172 }
173 if(param[0] > 7)
174 {
175 param[0] = 15;
176 }
177 currentChannel = (char)param[0];
178
179 if(param[1] < 0)
180 {
181 param[1] = 0;
182 }
183 if(param[1] > 4)
184 {
185 param[1] = 4;
186 }
187 powerMode = (unsigned char)param[1];
188
189 powerArray[currentChannel] = powerMode;
190 /*!< Set the power mode of the selected channel. */
191 AD5628_PowerMode(powerMode, currentChannel);
192 /*!< Send feedback to user */
193 CONSOLE_Print("%s%d for ",(char*)cmdList[1],powerMode);
194 switch (currentChannel)
195 { case 0:
196 CONSOLE_Print("channel A\r\n");
197 break;
198 case 1:
199 CONSOLE_Print("channel B\r\n");
200 break;
201 case 2:
202 CONSOLE_Print("channel C\r\n");
203 break;
204 case 3:
205 CONSOLE_Print("channel D\r\n");
206 break;
207 case 4:
208 CONSOLE_Print("channel E\r\n");
209 break;
210 case 5:
211 CONSOLE_Print("channel F\r\n");
212 break;
213 case 6:
214 CONSOLE_Print("channel G\r\n");
215 break;
216 case 7:
217 CONSOLE_Print("channel H\r\n");
218 break;
219 default:
220 CONSOLE_Print("all channels\r\n");
221 break;
222 }
223 }
224 else
225 {
226 /*!< Display error messages */
227 CONSOLE_Print("Invalid parameter!\r\n");
228 CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]);
229 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]);
230 }
231 }
232
233 /***************************************************************************//**
234 * @brief Displays the selected mode of operation for the selected channel.
235 *
236 * @return None.
237 *******************************************************************************/
238 void GetMode(double* param, char paramNo) /*!< "mode?" command */
239 {
240 /*!< Check if the parameter is valid */
241 if(paramNo >= 1)
242 {
243 if(param[0] < 0)
244 {
245 param[0] = 0;
246 }
247 if(param[0] > 7)
248 {
249 param[0] = 15;
250 }
251 currentChannel = (char)param[0];
252 /*!< Send feedback to user */
253 CONSOLE_Print("%s%d for ",(char*)cmdList[1],
254 powerArray[currentChannel]);
255 switch (currentChannel)
256 { case 0:
257 CONSOLE_Print("channel A\r\n");
258 break;
259 case 1:
260 CONSOLE_Print("channel B\r\n");
261 break;
262 case 2:
263 CONSOLE_Print("channel C\r\n");
264 break;
265 case 3:
266 CONSOLE_Print("channel D\r\n");
267 break;
268 case 4:
269 CONSOLE_Print("channel E\r\n");
270 break;
271 case 5:
272 CONSOLE_Print("channel F\r\n");
273 break;
274 case 6:
275 CONSOLE_Print("channel G\r\n");
276 break;
277 case 7:
278 CONSOLE_Print("channel H\r\n");
279 break;
280 default:
281 CONSOLE_Print("all channels\r\n");
282 break;
283 }
284 }
285 else
286 {
287 /*!< Display error messages */
288 CONSOLE_Print("Invalid parameter!\r\n");
289 CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]);
290 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]);
291 }
292 }
293
294 /***************************************************************************//**
295 * @brief Sets the output voltage of the selected channel.
296 *
297 * @return None.
298 *******************************************************************************/
299 void SetVoltage(double* param, char paramNo) /*!< "voltage=" command */
300 {
301 double outVoltage = 0;
302 double tempFloat = 0;
303
304 /*!< Check if the parameters are valid */
305 if(paramNo >= 2)
306 {
307 if(param[0] < 0)
308 {
309 param[0] = 0;
310 }
311 if(param[0] > 7)
312 {
313 param[0] = 15;
314 }
315 currentChannel = (char)param[0];
316
317 if(param[1] < 0)
318 {
319 param[1] = 0;
320 }
321 if(param[1] > 2499.999)
322 {
323 param[1] = 2499.999;
324 }
325 outVoltage = param[1] / 1000;
326
327 /*!< Find the binary value corresponding to the output voltage*/
328 tempFloat = (outVoltage / (double)(2 * VREF)) * 4096;
329 registerValue = (short) tempFloat;
330 dacRegArray[currentChannel] = registerValue;
331 /*!< Select a channel and write to its register */
332 AD5628_SetInputRegister(
333 AD5628_CMD(AD5628_CMD_WRITE_INPUT_N_UPDATE_N)|
334 AD5628_ADDR(currentChannel)|
335 AD5628_DATA_BITS(registerValue));
336 outVoltage = 2000 * VREF * ((double)registerValue / 4096);
337 /*!< Send feedback to user */
338 if (powerArray[currentChannel] > 0)
339 {
340 CONSOLE_Print("The selected channel is powered down. Please use \
341 'mode=%d 0' command in order to power it up.\r\n",currentChannel);
342 }
343 else
344 {
345 CONSOLE_Print("%s%.1f [mV] for ",(char*)cmdList[3], outVoltage);
346 switch (currentChannel)
347 { case 0:
348 CONSOLE_Print("channel A\r\n");
349 break;
350 case 1:
351 CONSOLE_Print("channel B\r\n");
352 break;
353 case 2:
354 CONSOLE_Print("channel C\r\n");
355 break;
356 case 3:
357 CONSOLE_Print("channel D\r\n");
358 break;
359 case 4:
360 CONSOLE_Print("channel E\r\n");
361 break;
362 case 5:
363 CONSOLE_Print("channel F\r\n");
364 break;
365 case 6:
366 CONSOLE_Print("channel G\r\n");
367 break;
368 case 7:
369 CONSOLE_Print("channel H\r\n");
370 break;
371 default:
372 CONSOLE_Print("all channels\r\n");
373 break;
374 }
375 }
376 }
377 else
378 {
379 /*!< Display error messages */
380 CONSOLE_Print("Invalid parameter!\r\n");
381 CONSOLE_Print("%s%s\r\n", (char*)cmdList[3], (char*)cmdDescription[3]);
382 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[3]);
383 }
384 }
385
386 /***************************************************************************//**
387 * @brief Displays the output voltage of the selected channel.
388 *
389 * @return None.
390 *******************************************************************************/
391 void GetVoltage(double* param, char paramNo) /*!< "voltage?" command */
392 {
393 double outVoltage = 0;
394
395 /*!< Check if the parameter is valid */
396 if(paramNo >= 1)
397 {
398 if(param[0] < 0)
399 {
400 param[0] = 0;
401 }
402 if(param[0] > 7)
403 {
404 param[0] = 15;
405 }
406
407 currentChannel = (char)param[0];
408 registerValue = dacRegArray[currentChannel];
409 /*!< Get the output voltage using data from registerValue */
410 outVoltage = 2000 * VREF * ((double)registerValue / 4096);
411 /*!< Send the requested value to user */
412 if (powerArray[currentChannel] > 0)
413 {
414 CONSOLE_Print("The selected channel is powered down. Please use \
415 'mode=%d 0' command in order to power it up.\r\n",currentChannel);
416 }
417 else
418 {
419 CONSOLE_Print("%s%.1f [mV] for ",(char*)cmdList[3], outVoltage);
420 switch (currentChannel)
421 { case 0:
422 CONSOLE_Print("channel A\r\n");
423 break;
424 case 1:
425 CONSOLE_Print("channel B\r\n");
426 break;
427 case 2:
428 CONSOLE_Print("channel C\r\n");
429 break;
430 case 3:
431 CONSOLE_Print("channel D\r\n");
432 break;
433 case 4:
434 CONSOLE_Print("channel E\r\n");
435 break;
436 case 5:
437 CONSOLE_Print("channel F\r\n");
438 break;
439 case 6:
440 CONSOLE_Print("channel G\r\n");
441 break;
442 case 7:
443 CONSOLE_Print("channel H\r\n");
444 break;
445 default:
446 CONSOLE_Print("all channels\r\n");
447 break;
448 }
449 }
450 }
451 else
452 {
453 /*!< Display error messages */
454 CONSOLE_Print("Invalid parameter!\r\n");
455 CONSOLE_Print("%s%s\r\n", (char*)cmdList[4], (char*)cmdDescription[4]);
456 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[4]);
457 }
458 }
459
460 /***************************************************************************//**
461 * @brief Writes the value into the DAC register of the selected channel.
462 *
463 * @return None.
464 *******************************************************************************/
465 void SetRegister(double* param, char paramNo) /*!< "register=" command */
466 {
467 double outVoltage = 0;
468
469 /*!< Check if the parameters are valid */
470 if(paramNo >= 2)
471 {
472 if(param[0] < 0)
473 {
474 param[0] = 0;
475 }
476 if(param[0] > 7)
477 {
478 param[0] = 15;
479 }
480 currentChannel = (char)param[0];
481 if(param[1] < 0)
482 {
483 param[1] = 0;
484 }
485 if(param[1] > 4095)
486 {
487 param[1] = 4095;
488 }
489 registerValue = (unsigned short)param[1];
490
491 dacRegArray[currentChannel] = registerValue;
492 /*!< Select a channel and write to its register */
493 AD5628_SetInputRegister(
494 AD5628_CMD(AD5628_CMD_WRITE_INPUT_N_UPDATE_N)|
495 AD5628_ADDR(currentChannel)|
496 AD5628_DATA_BITS(registerValue));
497 /*!< Send feedback to user */
498 if (powerArray[currentChannel] > 0)
499 {
500 CONSOLE_Print("The selected channel is powered down. Please use \
501 'mode=%d 0' command in order to power it up.\r\n",currentChannel);
502 }
503 else
504 {
505 CONSOLE_Print("%s%d for ",(char*)cmdList[5],registerValue);
506 switch (currentChannel)
507 { case 0:
508 CONSOLE_Print("channel A\r\n");
509 break;
510 case 1:
511 CONSOLE_Print("channel B\r\n");
512 break;
513 case 2:
514 CONSOLE_Print("channel C\r\n");
515 break;
516 case 3:
517 CONSOLE_Print("channel D\r\n");
518 break;
519 case 4:
520 CONSOLE_Print("channel E\r\n");
521 break;
522 case 5:
523 CONSOLE_Print("channel F\r\n");
524 break;
525 case 6:
526 CONSOLE_Print("channel G\r\n");
527 break;
528 case 7:
529 CONSOLE_Print("channel H\r\n");
530 break;
531 default:
532 CONSOLE_Print("all channels\r\n");
533 break;
534 }
535 }
536 }
537 else
538 {
539 /*!< Display error messages */
540 CONSOLE_Print("Invalid parameter!\r\n");
541 CONSOLE_Print("%s%s\r\n", (char*)cmdList[5], (char*)cmdDescription[5]);
542 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[5]);
543 }
544 }
545
546 /***************************************************************************//**
547 * @brief Displays the value written in the DAC register of the selected channel.
548 *
549 * @return None.
550 *******************************************************************************/
551 void GetRegister(double* param, char paramNo) /*!< "register?" command */
552 {
553
554 /*!< Check if the parameter is valid */
555 if(paramNo >= 1)
556 {
557 if(param[0] < 0)
558 {
559 param[0] = 0;
560 }
561 if(param[0] > 7)
562 {
563 param[0] = 15;
564 }
565 currentChannel = (char)param[0];
566 registerValue = dacRegArray[currentChannel];
567 /*!< Send the requested value to user */
568 if (powerArray[currentChannel] > 0)
569 {
570 CONSOLE_Print("The selected channel is powered down. Please use \
571 'mode=%d 0' command in order to power it up.\r\n",currentChannel);
572 }
573 else
574 {
575 CONSOLE_Print("%s%d for ",(char*)cmdList[5],registerValue);
576 switch (currentChannel)
577 { case 0:
578 CONSOLE_Print("channel A\r\n");
579 break;
580 case 1:
581 CONSOLE_Print("channel B\r\n");
582 break;
583 case 2:
584 CONSOLE_Print("channel C\r\n");
585 break;
586 case 3:
587 CONSOLE_Print("channel D\r\n");
588 break;
589 case 4:
590 CONSOLE_Print("channel E\r\n");
591 break;
592 case 5:
593 CONSOLE_Print("channel F\r\n");
594 break;
595 case 6:
596 CONSOLE_Print("channel G\r\n");
597 break;
598 case 7:
599 CONSOLE_Print("channel H\r\n");
600 break;
601 default:
602 CONSOLE_Print("all channels\r\n");
603 break;
604 }
605 }
606 }
607 else
608 {
609 /*!< Display error messages */
610 CONSOLE_Print("Invalid parameter!\r\n");
611 CONSOLE_Print("%s%s\r\n", (char*)cmdList[6], (char*)cmdDescription[6]);
612 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[6]);
613 }
614 }
615