Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for AD5791.
4 * @author Lucian Sin ([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 "Console.h"
46 #include "AD5791.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51 const struct cmd_info cmdList[] = {
52 [0] = {
53 .name = "help?",
54 .description = "Displays all available commands.",
55 .acceptedValue = "",
56 .example = "",
57 },
58 [1] = {
59 .name = "reset!",
60 .description = "Resets the device.",
61 .acceptedValue = "",
62 .example = "",
63 },
64 [2] = {
65 .name = "coding=",
66 .description = "Selects the coding style.",
67 .acceptedValue = "Accepted values:\r\n\
68 \t0 - Two's complement coding.(default)\r\n\
69 \t1 - Offset binary coding.",
70 .example = "To select two's complement coding, type: coding=0",
71 },
72 [3] = {
73 .name = "coding?",
74 .description = " Display the current coding style.",
75 .acceptedValue = "",
76 .example = "",
77 },
78 [4] = {
79 .name = "register=",
80 .description = "Writes to the DAC register",
81 .acceptedValue = "Accepted values:\r\n\
82 \t0 .. (2^DacResolution)-1 - the value written to the DAC.",
83 .example = "To set the register value to 10000, type: \
84 register=10000",
85 },
86 [5] ={
87 .name = "register?",
88 .description = " Displays last written value to the DAC \
89 register.",
90 .acceptedValue = "",
91 .example = "",
92 },
93 [6] = {
94 .name = "voltage=",
95 .description = "Sets the DAC output voltage.",
96 .acceptedValue = "Accepted values:\r\n\
97 \t-10 .. +10 - desired output voltage in volts.",
98 .example = "To set the output voltage to 5V, type: voltage=5",
99 },
100 [7] = {
101 .name = "voltage?",
102 .description = "Displays the output voltage.",
103 .acceptedValue = "",
104 .example = "",
105 },
106 [8] = {
107 .name = "output=",
108 .description = "Selects the DAC output state.",
109 .acceptedValue = "Accepted values:\r\n\
110 \t0 - Normal state.\r\n\
111 \t1 - Clamped via 6KOhm to AGND.(default)\r\n\
112 \t2 - Tristate.",
113 .example = "To set the DAC output state to normal, type: output=0",
114 },
115 [9] = {
116 .name = "output?",
117 .description = "Displays the DAC output state.",
118 .acceptedValue = "",
119 .example = "",
120 },
121 [10] = {
122 .name = "rbuf=",
123 .description = "Sets/resets the RBUF bit from control register.",
124 .acceptedValue = "Accepted values:\r\n\
125 \t0 - RBUF is reset.\r\n\
126 \t1 - RBUF is set.(default)",
127 .example = "To set the RBUF bit to 0, type: rbuf=0",
128 },
129 [11] = {
130 .name = "rbuf?",
131 .description = "Displays the value of RBUF bit from control \
132 register.",
133 .acceptedValue = "",
134 .example = "",
135 }
136 };
137
138 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
139 const char VREFN = 0;
140 const char VREFP = 10;
141
142 /******************************************************************************/
143 /************************ Variables Definitions *******************************/
144 /******************************************************************************/
145 cmdFunction cmdFunctions[12] = {GetHelp, DoReset, SetCoding, GetCoding,
146 SetRegister, GetRegister, SetVoltage, GetVoltage,
147 SetOutput, GetOutput, SetRbuf, GetRbuf};
148
149 /* Variables holding information about the device */
150 AD5791_type deviceType;
151 unsigned long max_value = 0; // Maximum DAC value
152 unsigned long sign = 0; // Sign bit
153 unsigned char offset = 0; // Offset depending on device resolution
154 unsigned long registerValue = 0; // Last register value written to the device
155 long codingStyle = 0; // Selected coding style
156 unsigned char output = 1; // Selected output state
157 unsigned char rbuf = 1; // RBUF bit
158
159 /***************************************************************************//**
160 * @brief Internal function for displaying error messages.
161 *
162 * @return None.
163 *******************************************************************************/
164 void DisplayError(unsigned char funcNo)
165 {
166 /* Display error messages */
167 CONSOLE_Print("Invalid parameter!\r\n");
168 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
169 (char*)cmdList[funcNo].description,
170 (char*)cmdList[funcNo].acceptedValue);
171 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
172 }
173
174 /***************************************************************************//**
175 * @brief Internal function for displaying all the commands with their description.
176 *
177 * @return None.
178 *******************************************************************************/
179 void DisplayCmdList()
180 {
181 unsigned char displayCmd;
182
183 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
184 {
185 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
186 (char*)cmdList[displayCmd].description);
187 }
188 }
189
190 /***************************************************************************//**
191 * @brief Displays all available commands.
192 *
193 * @return None.
194 *******************************************************************************/
195 void GetHelp(double* param, char paramNo) // "help?" command
196 {
197 unsigned char displayCmd;
198
199 CONSOLE_Print("Available commands:\r\n");
200 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
201 {
202 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
203 (char*)cmdList[displayCmd].description,
204 (char*)cmdList[displayCmd].acceptedValue);
205 }
206 }
207
208 /***************************************************************************//**
209 * @brief Set the current device type.
210 *
211 * @return None.
212 *******************************************************************************/
213 void DoDeviceLock(void)
214 {
215 char device = 0;
216 char deviceLocked = -1;
217
218 while(deviceLocked < 0)
219 {
220 CONSOLE_Print("Please specify your device.\r\n\
221 For AD5760 type: 0\r\n\
222 For AD5780 type: 1\r\n\
223 For AD5781 type: 2\r\n\
224 For AD5790 type: 3\r\n\
225 For AD5791 type: 4\r\n\
226 ");
227 CONSOLE_GetCommand(&device);
228 if((device >= 0x30) && (device <= 0x34))
229 {
230 deviceLocked = 1;
231 switch(device)
232 {
233 case 0x30 :
234 {
235 deviceType = ID_AD5760;
236 max_value = 0xFFFF;
237 sign = 0x8000;
238 offset = 4;
239 break;
240 }
241 case 0x31 :
242 {
243 deviceType = ID_AD5780;
244 max_value = 0x3FFFF;
245 sign = 0x20000;
246 offset = 2;
247 break;
248 }
249 case 0x32 :
250 {
251 deviceType = ID_AD5781;
252 max_value = 0x3FFFF;
253 sign = 0x20000;
254 offset = 2;
255 break;
256 }
257 case 0x33 :
258 {
259 deviceType = ID_AD5790;
260 max_value = 0xFFFFF;
261 sign = 0x80000;
262 offset = 0;
263 break;
264 }
265 case 0x34 :
266 {
267 deviceType = ID_AD5791;
268 max_value = 0xFFFFF;
269 sign = 0x80000;
270 offset = 0;
271 break;
272 }
273 }
274 }
275 else
276 {
277 CONSOLE_Print("Please type the number corresponding for \
278 your device!\r\n");
279 }
280 }
281 }
282
283 /***************************************************************************//**
284 * @brief Initializes the device.
285 *
286 * @return - The result of the initialization.
287 * Example: ERROR - the device was not initialized or the device
288 * is not present.
289 * SUCCES - the device was initialized and the device
290 * is present.
291 *******************************************************************************/
292 char DoDeviceInit(void)
293 {
294 DoDeviceLock();
295
296 if(AD5791_Init(deviceType) == 0)
297 {
298 CONSOLE_Print("Device OK\r\n");
299 DisplayCmdList();
300 registerValue = (AD5791_GetRegisterValue(AD5791_REG_DAC) &
301 ~(AD5791_ADDR_REG(-1))) >> offset;
302 return SUCCESS;
303 }
304 else
305 {
306 CONSOLE_Print("Device Error\r\n");
307 return ERROR;
308 }
309 }
310
311 /***************************************************************************//**
312 * @brief Resets the device.
313 *
314 * @return None.
315 *******************************************************************************/
316 void DoReset(double* param, char paramNo) // "reset!" command
317 {
318 /* Resets the device to its power-on state. */
319 AD5791_SoftInstruction(AD5791_SOFT_CTRL_RESET);
320 codingStyle = 0;
321 output = 1;
322 rbuf = 1;
323 /* Send feedback to user */
324 CONSOLE_Print("Device was reset.\r\n");
325 registerValue = (AD5791_GetRegisterValue(AD5791_REG_DAC) &
326 ~(AD5791_ADDR_REG(-1))) >> offset;
327 }
328
329 /***************************************************************************//**
330 * @brief Selects the coding style.
331 *
332 * @return None.
333 *******************************************************************************/
334 void SetCoding(double* param, char paramNo) // "coding=" command
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] > 1)
344 {
345 param[0] = 1;
346 }
347 codingStyle = (char)param[0];
348
349 if(codingStyle == 0)
350 {
351 AD5791_Setup(rbuf * AD5791_CTRL_RBUF);
352 /* Write to DAC register */
353 AD5791_SetDacValue(registerValue);
354 CONSOLE_Print("%s%d(two's complement)\r\n",cmdList[2].name,
355 codingStyle);
356 }
357 else
358 {
359 AD5791_Setup(AD5791_CTRL_BIN2SC | rbuf * AD5791_CTRL_RBUF);
360 /* Write to DAC register */
361 AD5791_SetDacValue(registerValue);
362 /* Send feedback to user for coding change */
363 CONSOLE_Print("%s%d(offset binary)\r\n",cmdList[2].name,
364 codingStyle);
365 }
366
367 GetVoltage(0, 0);
368 }
369 else
370 {
371 /* Display error messages */
372 DisplayError(2);
373 }
374 }
375
376 /***************************************************************************//**
377 * @brief Display the current coding style.
378 *
379 * @return None.
380 *******************************************************************************/
381 void GetCoding(double* param, char paramNo) // "coding?" command
382 {
383 /* Send the requested value to user */
384 if (codingStyle == 1)
385 {
386 CONSOLE_Print("coding=%d(offset binary)\r\n", codingStyle);
387 }
388 else
389 {
390 CONSOLE_Print("coding=%d(two's complement)\r\n", codingStyle);
391 }
392 }
393
394 /***************************************************************************//**
395 * @brief Writes to the DAC register.
396 *
397 * @return None.
398 *******************************************************************************/
399 void SetRegister(double* param, char paramNo) // "register=" command
400 {
401 /* Check if the parameter is valid */
402 if(paramNo >= 1)
403 {
404 if(param[0] < 0)
405 {
406 param[0] = 0;
407 }
408 if(param[0] > max_value)
409 {
410 param[0] = max_value;
411 }
412 registerValue = (long)param[0];
413 /* Write to DAC register */
414 AD5791_SetDacValue(registerValue);
415 /* Send feedback to user */
416 CONSOLE_Print("%s%d\r\n",cmdList[4].name, registerValue);
417 }
418 else
419 {
420 /* Display error messages */
421 DisplayError(4);
422 }
423 }
424
425 /***************************************************************************//**
426 * @brief Displays last written value to the DAC register.
427 *
428 * @return None.
429 *******************************************************************************/
430 void GetRegister(double* param, char paramNo) // "register?" command
431 {
432 /* Send the requested value to user */
433 CONSOLE_Print("%s%d\r\n",cmdList[4].name,registerValue);
434 }
435
436 /***************************************************************************//**
437 * @brief Sets the DAC output voltage.
438 *
439 * @return None.
440 *******************************************************************************/
441 void SetVoltage(double* param, char paramNo) // "voltage=" command
442 {
443 double volt = 0;
444
445 /* Check if the parameter is valid */
446 if(paramNo >= 1)
447 {
448 /* Check which formula to use according to rbuf value */
449 if (rbuf == 1)
450 {
451 /* Check if the limits of the parameter */
452 if(param[0] < VREFN)
453 {
454 param[0] = VREFN;
455 }
456 if(param[0] > VREFP)
457 {
458 param[0] = VREFP;
459 }
460 volt = (float)param[0];
461 /* Calculate the register value */
462 registerValue = (unsigned long)((volt - VREFN) * max_value
463 / (VREFP - VREFN));
464
465 volt = ((float)((VREFP - VREFN) * registerValue) / max_value) +
466 VREFN ;
467
468 /* Make a correction to register value */
469 if (codingStyle == 0)
470 {
471 if (volt <= 0)
472 {
473 registerValue += sign;
474 }
475 else
476 {
477 registerValue -= sign;
478 }
479 }
480 /* Check the limits of the value to be written in register */
481 if (registerValue > max_value)
482 {
483 registerValue = max_value;
484 }
485 else
486 {
487 if (registerValue < 0)
488 {
489 registerValue = 0;
490 }
491 }
492 /* Write to DAC register */
493 AD5791_SetDacValue(registerValue);
494 /* Send feedback to user with the set voltage */
495 GetVoltage(0, 0);
496
497 }
498 else
499 {
500 /* Check which formula to use according to rbuf value */
501 if (rbuf == 0)
502 {
503 /* Check if the limits of the parameter */
504 if(param[0] < ((2 * VREFN) - VREFP))
505 {
506 param[0] = (2 * VREFN) - VREFP;
507 }
508 if(param[0] > VREFP)
509 {
510 param[0] = VREFP;
511 }
512 volt = (float)param[0];
513 /* Calculate the register value */
514 registerValue = (unsigned long)((volt - 2 * VREFN + VREFP)
515 * max_value / (2 * (VREFP - VREFN)));
516 volt = ((float)(2 * (VREFP - VREFN) * registerValue) /
517 max_value) + 2 * VREFN - VREFP;
518 /* Make a correction to register value */
519 if (codingStyle == 0)
520 {
521 if (volt <= 0)
522 {
523 registerValue += sign;
524 }
525 else
526 {
527 registerValue -= sign;
528 }
529 }
530 /* Check the limits of the value to be written in register */
531 if (registerValue > max_value)
532 {
533 registerValue = max_value;
534 }
535 else
536 {
537 if (registerValue < 0)
538 {
539 registerValue = 0;
540 }
541 }
542 /* Write to DAC register */
543 AD5791_SetDacValue(registerValue);
544 /* Send feedback to user with the set voltage */
545 GetVoltage(0, 0);
546 }
547 }
548 }
549 else
550 {
551 /* Display error messages */
552 DisplayError(6);
553 }
554 }
555
556 /***************************************************************************//**
557 * @brief Displays the output voltage.
558 *
559 * @return None.
560 *******************************************************************************/
561 void GetVoltage(double* param, char paramNo) // "voltage?" command
562 {
563 double volt = 0;
564 unsigned long regValue = 0;
565
566 /* Get the output voltage using data from regValue */
567 regValue = registerValue;
568 if (codingStyle == 0)
569 {
570 regValue = (regValue & sign) ?
571 (regValue & ~sign) :
572 (regValue | sign);
573 }
574 if (rbuf == 1)
575 {
576 volt = ((float)((VREFP - VREFN) * regValue) / max_value) + VREFN ;
577 }
578 else
579 {
580 if (rbuf == 0)
581 {
582 volt = ((float)(2 * (VREFP - VREFN) * regValue) / max_value) + 2 *
583 VREFN - VREFP;
584 }
585 }
586 /* Send the requested value to user */
587 CONSOLE_Print("voltage=%.3f [V]\r\n",volt);
588 }
589
590 /***************************************************************************//**
591 * @brief Selects the DAC output state.
592 *
593 * @return None.
594 *******************************************************************************/
595 void SetOutput(double* param, char paramNo) // "output=" command
596 {
597 /* Check if the parameter is valid */
598 if(paramNo >= 1)
599 {
600 if(param[0] < 0)
601 {
602 param[0] = 0;
603 }
604 if(param[0] > 2)
605 {
606 param[0] = 2;
607 }
608 output = (unsigned char)param[0];
609 /* Set the DAC output to the selected operating mode. */
610 AD5791_DacOuputState(output);
611 /* Send feedback to user */
612 CONSOLE_Print("%s%d\r\n",cmdList[8].name,output);
613 }
614 else
615 {
616 /* Display error messages */
617 DisplayError(8);
618 }
619 }
620
621 /***************************************************************************//**
622 * @brief Displays the DAC output state.
623 *
624 * @return None.
625 *******************************************************************************/
626 void GetOutput(double* param, char paramNo) // "output?" command
627 {
628 /* Send the requested value to user */
629 CONSOLE_Print("%s%d\r\n",cmdList[8].name,output);
630
631 }
632
633 /***************************************************************************//**
634 * @brief Sets/resets the RBUF bit from control register.
635 *
636 * @return None.
637 *******************************************************************************/
638 void SetRbuf(double* param, char paramNo) // "rbuf=" command
639 {
640 unsigned long regValue = 0;
641
642 /* Check if the parameter is valid */
643 if(paramNo >= 1)
644 {
645 if(param[0] < 0)
646 {
647 param[0] = 0;
648 }
649 if(param[0] > 1)
650 {
651 param[0] = 1;
652 }
653 rbuf = (unsigned char)param[0];
654
655 regValue = AD5791_GetRegisterValue(AD5791_REG_CTRL);
656 regValue &= ~AD5791_CTRL_RBUF;
657 regValue |= rbuf * AD5791_CTRL_RBUF;
658 AD5791_SetRegisterValue(AD5791_REG_CTRL,regValue);
659
660 /* Send feedback to user */
661 CONSOLE_Print("%s%d\r\n",cmdList[10].name,rbuf);
662 }
663 else
664 {
665 /* Display error messages */
666 DisplayError(10);
667 }
668 }
669
670 /***************************************************************************//**
671 * @brief Displays the value of RBUF bit from control register.
672 *
673 * @return None.
674 *******************************************************************************/
675 void GetRbuf(double* param, char paramNo) // "rbuf?" command
676 {
677 /* Send the requested value to user */
678 CONSOLE_Print("%s%d\r\n",cmdList[10].name,rbuf);
679
680 }