Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/device_commands/AD5755/command.c

raw symbols SCIP
1 /**************************************************************************//**
2 * @file command.c
3 * @brief Implementation of command functions
4 * @author Istvan Csomortani ([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 "AD5755.h"
47 #include "Console.h"
48
49 /***************************************************************************/
50 /************************ Constants Definitions ****************************/
51 /***************************************************************************/
52
53 #define MAXVALUE 65535
54
55 const struct cmd_info cmdList[] = {
56 [0] = {
57 .name = "help?",
58 .description = "Displays all available commands.",
59 .acceptedValue = "",
60 .example = "",
61 },
62 [1] = {
63 .name = "register=",
64 .description = "Writes to the a data register.",
65 .acceptedValue = "Accepted values:\r\n\
66 \tRegister address : \t0 - DAC Data Reg,\r\n\
67 \t\t\t\t2 -> Gain Register,\r\n\
68 \t\t\t\t3 -> Gain Register All DACs,\r\n\
69 \t\t\t\t4 -> Offset Register,\r\n\
70 \t\t\t\t5 -> Offset Register All DACs,\r\n\
71 \t\t\t\t6 -> Clear Code Register,\r\n\
72 \t\t\t\t7 -> Control Register.\r\n\
73 \tChannel : 0 .. 3 - channel A .. D.\r\n\
74 \tValue : 0 .. 65535 - the value written to the DAC.",
75 .example = "To write 3200 to DAC data register of channel A, type: \
76 dataReg=0 0 3200",
77 },
78 [2] = {
79 .name = "control=",
80 .description = "Writes to the a control register.",
81 .acceptedValue = "Accepted values:\r\n\
82 \tRegister address : \t0 - Slew Rate Register,\r\n\
83 \t\t\t\t1 -> Main Control Register,\r\n\
84 \t\t\t\t2 -> DAC Control Register,\r\n\
85 \t\t\t\t3 -> Dc-to-dc Control Register,\r\n\
86 \t\t\t\t4 -> Software Register.\r\n\
87 \tChannel : 0 .. 3 - channel A .. D.\r\n\
88 \tValue : 0 .. 65535 - the value written to the DAC.",
89 .example = "To write 0x1001 to DAC control register of channel A, \
90 type : controlReg=2 0 0x1001",
91 },
92 [3] = {
93 .name = "register?",
94 .description = "Read back the value of a specified register.",
95 .acceptedValue = "Accepted values:\r\n\
96 \tRegister address : 0x00 .. 0x1A.",
97 .example = "",
98 },
99 [4] = {
100 .name = "power=",
101 .description = "Set the power state of the dc-to-dc converters, \
102 DAC and internal amplifiers for the selected channel.",
103 .acceptedValue = "Accepted values:\r\n\
104 \tChannel : 0 .. 3 - channel A .. D.\r\n\
105 \tvalue : 0 - turn off; 1 - turn on.",
106 .example = "To turn ON the power of channel C, type: power=2 1",
107 },
108 [5] = {
109 .name = "power?",
110 .description = "Displays the power state of the dc-to-dc converters,\
111 DAC and internal amplifiers for the selected channel.",
112 .acceptedValue = "Accepted values:\r\n\
113 \tChannel : 0 .. 3 - channel A .. D.",
114 .example = "To display the power status of channel C, type: power?2",
115 },
116 [6] = {
117 .name = "range=",
118 .description = "Set the range of the selected channel.",
119 .acceptedValue = "Accepted values:\r\n\
120 \tChannel : 0 .. 3 - channel A .. D.\r\n\
121 \tRange : \t0 -> 0 V to 5 V voltage\r\n\
122 \t\t\t1 -> 0 V to 10 V voltage\r\n\
123 \t\t\t2 -> -5 V to +5 V voltage\r\n\
124 \t\t\t3 -> -10 V to +10 V voltage\r\n\
125 \t\t\t4 -> 4 mA to 20 mA current\r\n\
126 \t\t\t5 -> 0 mA to 20 mA current\r\n\
127 \t\t\t6 -> 0 mA to 24 mA current",
128 .example = "To set the range of channel A to 4 mA to 20 mA, \
129 type: range=0 4",
130 },
131 [7] = {
132 .name = "range?",
133 .description = "Displays the range of the selected channel.",
134 .acceptedValue = "Accepted values:\r\n\
135 \tChannel : 0 .. 3 - channel A .. D.",
136 .example = "To display the range of channel A, type: range?0",
137 },
138 [8] = {
139 .name = "voltage=",
140 .description = "Sets the output voltage for a selected channel.",
141 .acceptedValue = "Accepted values:\r\n\
142 \tChannel : 0 .. 3 - channel A .. D.\r\n\
143 \tDesired voltage value : Vmin*1000 .. Vmax*1000",
144 .example = "To set 3.5V on channel B, type : voltage=1 3500",
145 },
146 [9] = {
147 .name = "voltage?",
148 .description = "Displays the output voltage for a selected channel.",
149 .acceptedValue = "Accepted values:\r\n\
150 \tChannel : 0 .. 3 - channel A .. D.",
151 .example = "To display the output voltage for channel B, type: \
152 voltage?1",
153 },
154 [10] = {
155 .name = "current=",
156 .description = "Sets the output current for a selected channel.",
157 .acceptedValue = "Accepted values:\r\n\
158 \tChannel : 0 .. 3 - channel A .. D.\r\n\
159 \tDesired current value (unit is mA) multiplied by 1000",
160 .example = "To set 15mA on channel B, type : current=1 15000",
161 },
162 [11] = {
163 .name = "current?",
164 .description = "Displays the output current for a selected channel.",
165 .acceptedValue = "Accepted values:\r\n\
166 \tChannel : 0 .. 3 - channel A .. D.",
167 .example = "To display the output current for channel D, \
168 type: current?3",
169 },
170 [12] = {
171 .name = "getStatus!",
172 .description = "Read back the Status register and print any faults or \
173 errors.",
174 .acceptedValue = "",
175 .example = "",
176 },
177 [13] = {
178 .name = "testSPI!",
179 .description = "Ensure that the SPI pins are working correctly.",
180 .acceptedValue = "",
181 .example = "",
182 }
183 };
184
185 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
186
187 /***************************************************************************/
188 /************************ Variables Definitions ****************************/
189 /***************************************************************************/
190
191 cmdFunction cmdFunctions[14] = {GetHelp, SetDataRegister, SetControlRegister,
192 GetRegister, SetPower, GetPower, SetRange,
193 GetRange, SetVoltage, GetVoltage, SetCurrent,
194 GetCurrent, EvaluateStatus, TestSpi};
195
196 /* Variables holding information about the device */
197 AD5755_type_t deviceType;
198 short minLimit[4] = {0, 0, 0, 0};
199 short maxLimit[4] = {0, 0, 0, 0};
200 /*****************************************************************************/
201 /************************ Functions Definitions ******************************/
202 /*****************************************************************************/
203
204 /***************************************************************************//**
205 * @brief Internal function for displaying error messages.
206 *
207 * @return None.
208 *******************************************************************************/
209 void DisplayError(unsigned char funcNo)
210 {
211 /* Display error messages */
212 CONSOLE_Print("Invalid parameter!\r\n");
213 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
214 (char*)cmdList[funcNo].description, \
215 (char*)cmdList[funcNo].acceptedValue);
216 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
217 }
218
219 /***************************************************************************//**
220 * @brief Internal function for displaying all the command with its description.
221 *
222 * @return None.
223 *******************************************************************************/
224 void DisplayCmdList()
225 {
226 unsigned char displayCmd;
227
228 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
229 {
230 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
231 (char*)cmdList[displayCmd].description);
232 }
233 }
234
235 /***************************************************************************//**
236 * @brief Verify if the given parameter is between his valid limits.
237 *
238 * @param None.
239 *
240 * @return None.
241 *******************************************************************************/
242 void paramLimit(double* param,
243 unsigned short lowerLimit,
244 unsigned short upperLimit)
245 {
246 if(*param < lowerLimit)
247 {
248 *param = lowerLimit;
249 }
250 else
251 {
252 if(*param > upperLimit)
253 {
254 *param = upperLimit;
255 }
256 }
257 }
258
259 /***************************************************************************//**
260 * @brief Set the current device type.
261 *
262 * @return None.
263 *******************************************************************************/
264 void DoDeviceLock(void)
265 {
266 char device = 0;
267 char deviceLocked = -1;
268
269 while(deviceLocked < 0)
270 {
271 CONSOLE_Print("Please specify your device.\r\n\
272 For AD5755 type: 0\r\n\
273 For AD5755-1 type: 1\r\n\
274 For AD5757 type: 2\r\n\
275 ");
276 CONSOLE_GetCommand(&device);
277 if((device >= 0x30) && (device <= 0x32))
278 {
279 deviceLocked = 1;
280 switch(device)
281 {
282 case 0x30 :
283 {
284 deviceType = ID_AD5755;
285 break;
286 }
287 case 0x31 :
288 {
289 deviceType = ID_AD5755_1;
290 break;
291 }
292 case 0x32 :
293 {
294 deviceType = ID_AD5757;
295 break;
296 }
297 }
298 }
299 else
300 {
301 CONSOLE_Print("Please type the number corresponding for \
302 your device!\r\n");
303 }
304 }
305 }
306
307 /***************************************************************************//**
308 * @brief Initializes the device.
309 *
310 * @return - The result of the initialization.
311 * Example: ERROR - the device was not initialized or the device
312 * is not present.
313 * SUCCES - the device was initialized and the device
314 * is present.
315 *******************************************************************************/
316 char DoDeviceInit(void)
317 {
318
319 DoDeviceLock();
320
321 if(AD5755_Init(deviceType) == 0)
322 {
323 CONSOLE_Print("Device OK\r\n");
324 DisplayCmdList();
325 return SUCCESS;
326 }
327 else
328 {
329 CONSOLE_Print("Device Error\r\n");
330 return ERROR;
331 }
332
333 /* Set the Gain for channel A. */
334 AD5755_SetRegisterValue(AD5755_DREG_WR_GAIN,
335 AD5755_DAC_A,
336 0xFFFF);
337
338 /* Set the Offset for channel A. */
339 AD5755_SetRegisterValue(AD5755_DREG_WR_OFFSET,
340 AD5755_DAC_A,
341 0x8000);
342
343 /* Set the Clear Code for channel A. */
344 AD5755_SetRegisterValue(AD5755_DREG_WR_CLR_CODE,
345 AD5755_DAC_A,
346 0x0000);
347 /* Select the output range */
348 if( (deviceType == ID_AD5755) ||
349 (deviceType == ID_AD5755_1) )
350 { /* 0V to 5V. */
351 AD5755_SetChannelRange(AD5755_DAC_A, AD5755_R_0_5_V);
352 AD5755_SetChannelRange(AD5755_DAC_B, AD5755_R_0_5_V);
353 AD5755_SetChannelRange(AD5755_DAC_C, AD5755_R_0_5_V);
354 AD5755_SetChannelRange(AD5755_DAC_D, AD5755_R_0_5_V);
355 }
356 else //ID_AD5757
357 { /* 0 mA to 20 mA */
358 AD5755_SetChannelRange(AD5755_DAC_A, AD5755_R_0_20_MA);
359 AD5755_SetChannelRange(AD5755_DAC_B, AD5755_R_0_20_MA);
360 AD5755_SetChannelRange(AD5755_DAC_C, AD5755_R_0_20_MA);
361 AD5755_SetChannelRange(AD5755_DAC_D, AD5755_R_0_20_MA);
362 }
363
364 /* Enable only channel one to be cleared when CLEAR bit is asserted. */
365 AD5755_ChannelClearEnable(AD5755_DAC_A, 1);
366
367 /* Set the output voltage for channels B, C and D. */
368 TIME_DelayUs(10000);
369 if( (deviceType == ID_AD5755) ||
370 (deviceType == ID_AD5755_1) )
371 {
372 AD5755_SetVoltage(AD5755_DAC_B, 1.25);
373 AD5755_SetVoltage(AD5755_DAC_C, 2.5);
374 AD5755_SetVoltage(AD5755_DAC_D, 4.9);
375 }
376 else
377 {
378 AD5755_SetCurrent(AD5755_DAC_B, 3);
379 AD5755_SetCurrent(AD5755_DAC_C, 12);
380 AD5755_SetCurrent(AD5755_DAC_D, 19.5);
381 }
382 }
383
384 /***************************************************************************//**
385 * @brief Displays all available commands.
386 *
387 * @return None.
388 *******************************************************************************/
389 void GetHelp(double* param, char paramNo) // "help?" command
390 {
391 unsigned char displayCmd;
392
393 CONSOLE_Print("Available commands:\r\n");
394 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
395 {
396 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
397 (char*)cmdList[displayCmd].description,
398 (char*)cmdList[displayCmd].acceptedValue);
399 }
400 }
401
402 /**************************************************************************//**
403 * @brief Function of the dataReg= command. Sets a value for a data register.
404 *
405 * @param param - array of the command arguments, which should be:
406 * param[0] - The address of the data register. Accepted values:
407 * 0 - DAC Data Reg
408 * 2 - Gain Register
409 * 3 - Gain Register All DACs
410 * 4 - Offset Register
411 * 5 - Offset Register All DACs
412 * 6 - Clear Code Register
413 * 7 - Control Register
414 * param[1] - The selected channel. Accepted value:
415 * 0 - channel A
416 * 1 - channel B
417 * 2 - channel C
418 * 3 - channel D
419 * param[2] - the value of the register, should be between [0 .. 65535]
420 *
421 * @param paramNo - number of the arguments
422 *
423 * @return void
424 ******************************************************************************/
425 void SetDataRegister(double* param, char paramNo)
426 {
427 unsigned char address = 0;
428 unsigned char channel = 0;
429 unsigned short data = 0;
430
431 if(paramNo >= 3)
432 {
433 /* Ensure that the address is not 1 */
434 if(param[0] == 1)
435 {
436 param[0] = 0;
437 }
438 /* Keep the parameters between valid limits */
439 paramLimit(&param[0], 0, 7);
440 paramLimit(&param[1], 0, 3);
441 paramLimit(&param[2], 0, MAXVALUE);
442
443 address = (char)param[0];
444 channel = (char)param[1];
445 data = (unsigned short)param[2];
446
447 AD5755_SetRegisterValue(address, channel, data);
448 /* Send feedback to user */
449 CONSOLE_Print("Register[0x%02x] at channel 0x%02x is 0x%4x\r\n", \
450 address, channel, data);
451 }
452 else
453 {
454 DisplayError(1);
455 }
456 }
457
458 /**************************************************************************//**
459 * @brief Function of the controlReg= command. Sets a value for a control
460 * register.
461 * @param param - array of the command arguments, which should be:
462 * param[0] - The address of the data register. Accepted values:
463 * 0 - Slew Rate control register (one per channel)
464 * 1 - Main control register
465 * 2 - DAC control register (one per channel)
466 * 3 - DC-to-DC control register
467 * 4 - Software register
468 * param[1] - The selected channel. Accepted value:
469 * 0 - channel A
470 * 1 - channel B
471 * 2 - channel C
472 * 3 - channel D
473 * param[2] - the value of the register, should be between [0 .. 65535]
474 *
475 * @param paramNo - number of the arguments
476 *
477 * @return void
478 ******************************************************************************/
479 void SetControlRegister(double* param, char paramNo)
480 {
481 unsigned char address = 0;
482 unsigned char channel = 0;
483 unsigned short data = 0;
484
485 if(paramNo >= 3)
486 {
487 /* Keep the parameters between valid limits */
488 paramLimit(&param[0], 0, 4);
489 paramLimit(&param[1], 0, 3);
490 paramLimit(&param[2], 0, 8191);
491
492 address = (unsigned char)param[0];
493 channel = (unsigned char)param[1];
494 data = (unsigned short)param[2];
495
496 AD5755_SetControlRegisters(address, channel, data);
497 /* Send feedback to user */
498 CONSOLE_Print("Control register[0x%02x] for channel 0x%02x = 0x%4x\r\n",\
499 address, channel, data);
500 }
501 else
502 {
503 DisplayError(2);
504 }
505 }
506
507 /**************************************************************************//**
508 * @brief Function of the register? command.
509 *
510 * @param param - array of the command arguments
511 * param[0] - the address of the register, which will be read
512 * @param paramNo - number of the arguments
513 * @return void
514 ******************************************************************************/
515 void GetRegister(double* param, char paramNo)
516 {
517 unsigned short regValue = 0;
518 unsigned char registerAddr = 0;
519
520 if(paramNo >= 1)
521 { /* Validate address */
522 paramLimit(&param[0], 0, 26);
523 /* Address wrapping */
524 switch((int)param[0])
525 {
526 case 0 :
527 registerAddr = AD5755_RD_DATA_REG(0);
528 break;
529 case 1 :
530 registerAddr = AD5755_RD_DATA_REG(1);
531 break;
532 case 2 :
533 registerAddr = AD5755_RD_DATA_REG(2);
534 break;
535 case 3 :
536 registerAddr = AD5755_RD_DATA_REG(3);
537 break;
538 case 4 :
539 registerAddr = AD5755_RD_CTRL_REG(0);
540 break;
541 case 5 :
542 registerAddr = AD5755_RD_CTRL_REG(1);
543 break;
544 case 6 :
545 registerAddr = AD5755_RD_CTRL_REG(2);
546 break;
547 case 7 :
548 registerAddr = AD5755_RD_CTRL_REG(3);
549 break;
550 case 8 :
551 registerAddr = AD5755_RD_GAIN_REG(0);
552 break;
553 case 9 :
554 registerAddr = AD5755_RD_GAIN_REG(1);
555 break;
556 case 10 :
557 registerAddr = AD5755_RD_GAIN_REG(2);
558 break;
559 case 11 :
560 registerAddr = AD5755_RD_GAIN_REG(3);
561 break;
562 case 12 :
563 registerAddr = AD5755_RD_OFFSET_REG(0);
564 break;
565 case 13 :
566 registerAddr = AD5755_RD_OFFSET_REG(1);
567 break;
568 case 14 :
569 registerAddr = AD5755_RD_OFFSET_REG(2);
570 break;
571 case 15 :
572 registerAddr = AD5755_RD_OFFSET_REG(3);
573 break;
574 case 16 :
575 registerAddr = AD5755_RD_CODE_REG(0);
576 break;
577 case 17 :
578 registerAddr = AD5755_RD_CODE_REG(1);
579 break;
580 case 18 :
581 registerAddr = AD5755_RD_CODE_REG(2);
582 break;
583 case 19 :
584 registerAddr = AD5755_RD_CODE_REG(3);
585 break;
586 case 20 :
587 registerAddr = AD5755_RD_SR_CTRL_REG(0);
588 break;
589 case 21 :
590 registerAddr = AD5755_RD_SR_CTRL_REG(1);
591 break;
592 case 22 :
593 registerAddr = AD5755_RD_SR_CTRL_REG(2);
594 break;
595 case 23 :
596 registerAddr = AD5755_RD_SR_CTRL_REG(3);
597 break;
598 case 24 :
599 registerAddr = AD5755_RD_STATUS_REG;
600 break;
601 case 25 :
602 registerAddr = AD5755_RD_MAIN_CTRL_REG;
603 break;
604 case 26 :
605 registerAddr = AD5755_RD_Dc_DC_CTRL_REG;
606 break;
607 default:
608 registerAddr = 0;
609 break;
610 }
611 regValue = (short)AD5755_GetRegisterValue(registerAddr);
612 if(regValue == -1)
613 {
614 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
615 }
616 else
617 {
618 CONSOLE_Print("register[0x%02x]=0x%02x\r\n", \
619 (unsigned char)param[0], regValue);
620 }
621 }
622 else
623 {
624 DisplayError(3);
625 }
626 }
627
628 /**************************************************************************//**
629 * @brief Function of the power= command.
630 *
631 * @param param - array of the command arguments
632 * param[0] - selected channel
633 * param[1] - switch ON(1) or OFF(0)
634 * @param paramNo - number of the arguments
635 * @return void
636 ******************************************************************************/
637 void SetPower(double* param, char paramNo)
638 {
639 short regValue = 0;
640 char channel = 0;
641 char toggleSw = 0;
642 /* Validate parameters */
643 if(paramNo >= 2)
644 { /* Validate channel */
645 paramLimit(&param[0], 0, 3);
646 paramLimit(&param[1], 0, 1);
647 channel = (unsigned char)param[0];
648 toggleSw = (unsigned char)param[1];
649
650 /* Read the DAC control register */
651 regValue = (short)AD5755_GetRegisterValue(AD5755_RD_CTRL_REG((char)channel));
652 if(regValue == -1)
653 {
654 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
655 }
656 else
657 {
658 if(toggleSw == 1)
659 { /* Enable the AD5755_DAC_DC_DC and channel output */
660 AD5755_SetControlRegisters( AD5755_CREG_DAC,
661 channel,
662 (regValue | AD5755_DAC_INT_ENABLE \
663 | AD5755_DAC_DC_DC \
664 | AD5755_DAC_OUTEN) );
665 CONSOLE_Print("Channel %d is ON\r\n", \
666 (int)channel, (regValue | AD5755_DAC_INT_ENABLE \
667 | AD5755_DAC_DC_DC \
668 | AD5755_DAC_OUTEN) );
669 }
670 else
671 { /* Disable the AD5755_DAC_DC_DC and channel output */
672 AD5755_SetControlRegisters(AD5755_CREG_DAC,
673 channel,
674 (regValue & ~(AD5755_DAC_INT_ENABLE | \
675 AD5755_DAC_DC_DC | \
676 AD5755_DAC_OUTEN)) );
677 CONSOLE_Print("Channel %d is OFF\r\n", \
678 (int)channel, (regValue & ~(AD5755_DAC_INT_ENABLE \
679 | AD5755_DAC_DC_DC \
680 | AD5755_DAC_OUTEN)));
681 }
682 }
683 }
684 else
685 {
686 DisplayError(4);
687 }
688 }
689
690 /**************************************************************************//**
691 * @brief Function of the power? command, verify if the selected channel is ON
692 *
693 * @param param - array of the command arguments
694 * param[0] - selected channel
695 * @param paramNo - number of the arguments
696 * @return void
697 ******************************************************************************/
698 void GetPower(double* param, char paramNo)
699 {
700 char channel = 0;
701 short regValue = 0;
702 /* Validate parameters */
703 if(paramNo >= 1)
704 { /* Validate channel */
705 paramLimit(&param[0], 0, 3);
706 channel = param[0];
707
708 regValue = (short)AD5755_GetRegisterValue(AD5755_RD_CTRL_REG((char)channel));
709 if(regValue == -1)
710 {
711 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
712 }
713 else
714 {
715 if((regValue & AD5755_DAC_DC_DC) &&
716 (regValue & AD5755_DAC_OUTEN) &&
717 (regValue & AD5755_DAC_INT_ENABLE))
718 {
719 CONSOLE_Print("Channel %d is ON\r\n", \
720 (int)channel, regValue );
721 }
722 else
723 {
724 CONSOLE_Print("Channel %d is OFF\r\n", \
725 (int)channel, regValue );
726 }
727 }
728 }
729 else
730 {
731 DisplayError(5);
732 }
733 }
734
735 /**************************************************************************//**
736 * @brief Function of the range= command. Changing and reprogramming the output
737 * range, the value of the output will be reseted.
738 *
739 * @param param - array of the command arguments
740 * param[0] - selected channel
741 * param[1] - selected range, accepted value:
742 * 'AD5755_R_0_5_V' - 0V to 5V voltage range (default)
743 * 'AD5755_R_0_10_V' - 0V to 10V voltage range
744 * 'AD5755_R_M5_P5_V' - +/-5V voltage range
745 * 'AD5755_R_M10_P10_V' - +/-10V voltage range
746 * 'AD5755_R_4_20_MA' - 4 mA to 20 mA current range
747 * 'AD5755_R_0_20_MA' - 0 mA to 20 mA current range
748 * 'AD5755_R_0_24_MA' - 0 mA to 24 mA current range
749 * @param paramNo - number of the arguments
750 * @return void
751 ******************************************************************************/
752 void SetRange(double* param, char paramNo)
753 {
754 unsigned short regValue = 0;
755 unsigned char channel = 0;
756 unsigned short range = 0;
757
758 if(paramNo >= 2)
759 { /* Validate channel */
760 paramLimit(&param[0], 0, 3);
761 paramLimit(&param[1], 0, 6);
762 channel = (unsigned char)param[0];
763 range = (unsigned char)param[1];
764
765 regValue = (unsigned short)AD5755_GetRegisterValue(AD5755_RD_CTRL_REG((unsigned char)channel));
766 if(regValue == -1)
767 {
768 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
769 }
770 else
771 {
772 /* set the output to 0V/0mA */
773 AD5755_SetRegisterValue(AD5755_DREG_WR_DAC, channel, 0x0);
774 // Clear the DAC_R bits
775
776 /* Disable the output */
777 regValue &= ~AD5755_DAC_OUTEN;
778 AD5755_SetControlRegisters( AD5755_CREG_DAC,
779 channel,
780 regValue);
781 /* Set up the new output range */
782 regValue &= 0xFFFFFFF8;
783 regValue |= range;
784 AD5755_SetControlRegisters( AD5755_CREG_DAC,
785 channel,
786 regValue);
787 /* Write to data DAC register */
788 AD5755_SetRegisterValue(AD5755_DREG_WR_DAC, channel, 0x0);
789 TIME_DelayUs(200);
790 /* Enable the data output */
791 regValue |= AD5755_DAC_OUTEN;
792 AD5755_SetControlRegisters( AD5755_CREG_DAC,
793 channel,
794 regValue);
795 CONSOLE_Print("The range of channel %d was changed\r\n", channel);
796 }
797 }
798 else
799 {
800 DisplayError(6);
801 }
802 }
803
804 /**************************************************************************//**
805 * @brief Function of the range? command.
806 *
807 * @param param - array of the command arguments
808 * param[0] - selected channel
809 * @param paramNo - number of the arguments
810 * @return void
811 ******************************************************************************/
812 void GetRange(double* param, char paramNo)
813 {
814 short regValue = 0;
815 short range = 0;
816 char channel = 0;
817 if(paramNo >= 1)
818 { /* Validate channel */
819 paramLimit(&param[0], 0, 3);
820 channel = param[0];
821
822 regValue = (short)AD5755_GetRegisterValue(AD5755_RD_CTRL_REG((char)channel));
823 if(regValue == -1)
824 {
825 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
826 }
827 else
828 {
829 range = regValue & 0x07;
830 switch(range)
831 {
832 case AD5755_R_0_5_V :
833 {
834 CONSOLE_Print("Range of channel %d is 0V to 5V.\r\n", \
835 (int)channel);
836 break;
837 }
838 case AD5755_R_0_10_V:
839 {
840 CONSOLE_Print("Range of channel %d is 0V to 10V.\r\n", \
841 (int)channel);
842 break;
843 }
844 case AD5755_R_M5_P5_V:
845 {
846 CONSOLE_Print("Range of channel %d is +/-5V.\r\n", \
847 (int)channel);
848 break;
849 }
850 case AD5755_R_M10_P10_V:
851 {
852 CONSOLE_Print("Range of channel %d is +/-10V.\r\n", \
853 (int)channel);
854 break;
855 }
856 case AD5755_R_4_20_MA:
857 {
858 CONSOLE_Print("Range of channel %d is 4mA to 20mA.\r\n", \
859 (int)channel);
860 break;
861 }
862 case AD5755_R_0_20_MA:
863 {
864 CONSOLE_Print("Range of channel %d is 0mA to 20mA.\r\n", \
865 (int)channel);
866 break;
867 }
868 case AD5755_R_0_24_MA:
869 {
870 CONSOLE_Print("Range of channel %d is 0mA to 24mA.\r\n", \
871 (int)channel);
872 break;
873 }
874 default:
875 {
876 CONSOLE_Print("Invalid range!\r\n");
877 break;
878 }
879 }
880 }
881 }
882 else
883 {
884 DisplayError(7);
885 }
886 }
887
888 /**************************************************************************//**
889 * @brief Function of the voltage? command. Set up a desired voltage level for a
890 * selected channel. If the channel is powered off or it's configured
891 * to a current output the function throws an error message.
892 *
893 * @param param - array of the command arguments
894 * param[0] - selected channel
895 * param[1] - the value of the voltage multiplied by 1000
896 * @param paramNo - number of the arguments
897 * @return void
898 ******************************************************************************/
899 void SetVoltage(double* param, char paramNo)
900 {
901 long buffer = 0;
902 unsigned short cntrValue = 0;
903 short lowerLimit = 0;
904 short upperLimit = 0;
905 unsigned char channel = 0;
906 float voltage = 0;
907
908 if(paramNo >= 2)
909 {/* Validate channel */
910 paramLimit(&param[0], 0, 3);
911 channel = param[0];
912
913 voltage = param[1] / 1000;
914
915 /* Validate data */
916 buffer = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG((char)channel));
917 if(buffer == -1)
918 {
919 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
920 }
921 else
922 {
923 cntrValue = (unsigned short)buffer;
924 if( ((cntrValue & AD5755_DAC_INT_ENABLE) != 0) && \
925 ((cntrValue & AD5755_DAC_OUTEN) != 0) && \
926 ((cntrValue & AD5755_DAC_DC_DC) != 0) && \
927 ((cntrValue & AD5755_DAC_R(4)) == 0) )
928 { /* Define the lower and upper limit in function of the range */
929 lowerLimit = ((cntrValue & AD5755_DAC_R(2)) == 0) ? 0 :\
930 ((cntrValue & AD5755_DAC_R(1)) == 0) ? -5 :\
931 -10;
932 upperLimit = ((cntrValue & AD5755_DAC_R(1)) == 0) ? 5 : 10;
933 if(voltage < (float)lowerLimit)
934 {
935 voltage = lowerLimit;
936 }
937 if(voltage > (float)upperLimit)
938 {
939 voltage = upperLimit;
940 }
941 /* Write data to register */
942 AD5755_SetVoltage(channel, voltage);
943 CONSOLE_Print("Output channel %d was updated to %1.2fV\r\n", \
944 channel, voltage);
945 }
946 else
947 {
948 CONSOLE_Print("Can not perform the command, incompatible \
949 configuration. Check out if the \r\n channel is enabled, or the output is set \
950 up to current mode.\r\n");
951 }
952 }
953 }
954 else
955 {
956 DisplayError(8);
957 }
958 }
959
960 /**************************************************************************//**
961 * @brief Function of the voltage? command. Read back the voltage level of a
962 * selected channel. If the channel is powered off or it's configured
963 * to a current output the function throws an error message.
964 *
965 * @param param - array of the command arguments
966 * param[0] - selected channel
967 * @param paramNo - number of the arguments
968 *
969 * @return void
970 ******************************************************************************/
971 void GetVoltage(double* param, char paramNo)
972 {
973 unsigned short cntrValue = -1;
974 unsigned long regValue = 0;
975 unsigned char channel = 0;
976 unsigned short range = 0;
977 float realValue = 0;
978
979 if(paramNo >= 1)
980 { /* Validate channel */
981 paramLimit(&param[0], 0, 3);
982 channel = param[0];
983
984 cntrValue = (short)AD5755_GetRegisterValue(AD5755_RD_CTRL_REG((char)channel));
985 range = cntrValue & 0x7;
986 /* Check the output range of the channel */
987 if( ((cntrValue & AD5755_DAC_INT_ENABLE) != 0) && \
988 ((cntrValue & AD5755_DAC_OUTEN) != 0) && \
989 ((cntrValue & AD5755_DAC_DC_DC) != 0) && \
990 ((cntrValue & AD5755_DAC_R(4)) == 0) )
991 {
992 regValue = AD5755_GetRegisterValue(AD5755_RD_DATA_REG((char)channel));
993 if(regValue == -1)
994 {
995 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
996 }
997 else
998 {
999 switch(range)
1000 {
1001 case 0:
1002 {
1003 realValue = (float)(regValue * 5) / MAXVALUE;
1004 CONSOLE_Print("Channel %d = %.2f V\r\n", (int)channel,\
1005 realValue);
1006 break;
1007 }
1008 case 1:
1009 {
1010 realValue = (float)(regValue * 10) / MAXVALUE;
1011 CONSOLE_Print("Channel %d = %.2f V\r\n", (int)channel,\
1012 realValue);
1013 break;
1014 }
1015 case 2:
1016 {
1017 realValue = ((float)(regValue * 10) / MAXVALUE) - 5;
1018 CONSOLE_Print("Channel %d = %.2fV\r\n", (int)channel,\
1019 realValue);
1020 break;
1021 }
1022 case 3:
1023 {
1024 realValue = ((float)(regValue * 20) / MAXVALUE) - 10;
1025 CONSOLE_Print("Channel %d = %.2fV\r\n", (int)channel,\
1026 realValue );
1027 break;
1028 }
1029 default:
1030 {
1031 CONSOLE_Print("Invalid range!\r\n");
1032 break;
1033 }
1034 }
1035 }
1036 }
1037 else
1038 {
1039 CONSOLE_Print("Can not perform the command, incompatible \
1040 configuration. Check out if the \r\n channel is enabled, or the output is set \
1041 up to current mode.\r\n");
1042 }
1043 }
1044 else
1045 {
1046 DisplayError(9);
1047 }
1048 }
1049
1050 /**************************************************************************//**
1051 * @brief Function of the current? command. Set up a desired current for a
1052 * selected channel. If the channel is powered off or it's configured
1053 * to voltage output the function throws an error message.
1054 *
1055 * @param param - array of the command arguments
1056 * param[0] - selected channel
1057 * param[1] - the value of the current(mA) multiplied by 1000
1058 * @param paramNo - number of the arguments
1059 * @return void
1060 ******************************************************************************/
1061 void SetCurrent(double* param, char paramNo)
1062 {
1063 unsigned short cntrValue = -1;
1064 short lowerLimit = 0;
1065 short upperLimit = 0;
1066 unsigned char channel = 0;
1067 float current = 0;
1068 if(paramNo >= 2)
1069 { /* Validate channel */
1070 paramLimit(&param[0], 0, 3);
1071 channel = param[0];
1072 current = param[1] / 1000;
1073
1074 cntrValue = (short)AD5755_GetRegisterValue(AD5755_RD_CTRL_REG((char)channel));
1075 if(cntrValue == -1)
1076 {
1077 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
1078 }
1079 else
1080 {
1081 if( ((cntrValue & AD5755_DAC_INT_ENABLE) != 0) && \
1082 ((cntrValue & AD5755_DAC_OUTEN) != 0) && \
1083 ((cntrValue & AD5755_DAC_DC_DC) != 0) && \
1084 ((cntrValue & AD5755_DAC_R(4)) != 0) )
1085 { /* Define the lower and upper limit in function of the range */
1086 lowerLimit = ( ((cntrValue & AD5755_DAC_R(2)) == 0) &&
1087 ((cntrValue & AD5755_DAC_R(1)) == 0) ) ? 4 : 0;
1088 upperLimit = ((cntrValue & AD5755_DAC_R(2)) != 0) ? 24 : 20;
1089 if(current < lowerLimit)
1090 {
1091 current = lowerLimit;
1092 }
1093 if(current > upperLimit)
1094 {
1095 current = upperLimit;
1096 }
1097 AD5755_SetCurrent(channel, current);
1098 CONSOLE_Print("Output channel %d was updated to %1.2fmA\r\n", \
1099 channel, current);
1100 }
1101 else
1102 {
1103 CONSOLE_Print("Can not perform the command, incompatible \
1104 configuration. Check out if the \r\n channel is enabled, or the output is set \
1105 up to current mode.\r\n");
1106 }
1107 }
1108 }
1109 else
1110 {
1111 DisplayError(10);
1112 }
1113 }
1114
1115 /**************************************************************************//**
1116 * @brief Function of the current? command. Read back the current of a
1117 * selected channel. If the channel is powered off or it's configured
1118 * to a voltage output the function throws an error message.
1119 *
1120 * @param param - array of the command arguments
1121 * param[0] - selected channel
1122 * @param paramNo - number of the arguments
1123 *
1124 * @return void
1125 ******************************************************************************/
1126 void GetCurrent(double* param, char paramNo)
1127 {
1128 unsigned short cntrValue = -1;
1129 unsigned short regValue = 0;
1130 unsigned char channel = 0;
1131 unsigned short range = 0;
1132 float realValue = 0;
1133
1134 if(paramNo >= 1)
1135 { /* Validate channel */
1136 paramLimit(&param[0], 0, 3);
1137 channel = param[0];
1138
1139 cntrValue = (short)AD5755_GetRegisterValue(AD5755_RD_CTRL_REG((char)channel));
1140 range = cntrValue & 0x7;
1141 if(cntrValue == -1)
1142 {
1143 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
1144 }
1145 else
1146 {
1147 if( ((cntrValue & AD5755_DAC_INT_ENABLE) != 0) && \
1148 ((cntrValue & AD5755_DAC_OUTEN) != 0) && \
1149 ((cntrValue & AD5755_DAC_DC_DC) != 0) && \
1150 ((cntrValue & AD5755_DAC_R(4)) != 0) )
1151 {
1152 regValue = AD5755_GetRegisterValue(AD5755_RD_DATA_REG((char)channel));
1153 if(regValue == -1)
1154 {
1155 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
1156 }
1157 else
1158 {
1159 switch(range)
1160 {
1161 case 4:
1162 {
1163 realValue = ((float)(regValue * 16) / MAXVALUE) + 4;
1164 CONSOLE_Print("Channel %d = %0.2fmA\r\n", \
1165 (int)channel, realValue);
1166 break;
1167 }
1168 case 5:
1169 {
1170 realValue = (float)(regValue * 20) / MAXVALUE;
1171 CONSOLE_Print("Channel %d = %0.2fmA\r\n", \
1172 (int)channel, realValue);
1173 break;
1174 }
1175 case 6:
1176 {
1177 realValue = (float)(regValue * 24) / MAXVALUE;
1178 CONSOLE_Print("Channel %d = %0.2fmA\r\n", \
1179 (int)channel, realValue);
1180 break;
1181 }
1182 default:
1183 {
1184 CONSOLE_Print("Invalid range!\r\n");
1185 break;
1186 }
1187 }
1188 }
1189 }
1190 else
1191 {
1192 CONSOLE_Print("Can not perform the command, incompatible \
1193 configuration. Check out if the \r\n channel is enabled, or the output is set \
1194 up to current mode.\r\n");
1195 }
1196 }
1197 }
1198 else
1199 {
1200 DisplayError(11);
1201 }
1202 }
1203
1204 /**************************************************************************//**
1205 * @brief Function of the getStatus! command. Read back the Status register and
1206 * throws an error message is there is any faults or errors in the system.
1207 *
1208 * @param param - None
1209 * @param paramNo - number of the arguments
1210 *
1211 * @return void
1212 ******************************************************************************/
1213 void EvaluateStatus(double* param, char paramNo)
1214 {
1215 unsigned short range[4] = {0, 0, 0, 0};
1216 unsigned short status = 0;
1217 char crcError = 0;
1218 int i = 0;
1219 char fault = 0;
1220
1221 /* Read back the range of each channel */
1222 for(i=0; i<4; i++)
1223 {
1224 long rangeBuf = AD5755_GetRegisterValue(AD5755_RD_CTRL_REG(i)) & 0x7;
1225 if(rangeBuf != ERROR)
1226 {
1227 range[i] = (unsigned short)rangeBuf;
1228 }
1229 else
1230 {
1231 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
1232 crcError = ERROR;
1233 break;
1234 }
1235 }
1236 if(crcError == SUCCESS)
1237 {
1238 /* Read back the status register */
1239 status = (unsigned short)AD5755_GetRegisterValue(AD5755_RD_STATUS_REG);
1240 for(i=0; i<4; i++)
1241 { /* Check if there any dc-to-dc converter warnings */
1242 if( (status & AD5755_STATUS_DC_DC(i)) != 0 )
1243 {
1244 /* channel has voltage output */
1245 if(range[i] <= 3)
1246 {
1247 CONSOLE_Print("WARNING [ch%d]: The dc-to-dc converter is \
1248 unable to regulate to 15V as expected!\r\n", i);
1249 fault++;
1250 }
1251 else /* channel has current output */
1252 {
1253 CONSOLE_Print("WARNING [ch%d]: The dc-to-dc converter \
1254 cannot maintain compliance (it may be reaching its Vmax voltage)\r\n", i);
1255 fault++;
1256 }
1257 }
1258 /* Check if there are any fault detection at Vout pins */
1259 if( (status & AD5755_STATUS_VOUT_FAULT(i)) != 0)
1260 {
1261 CONSOLE_Print("ERROR [ch%d]: Fault detected on Vout pin!\r\n",\
1262 i);
1263 fault++;
1264 }
1265 /* Check if there are any fault detection at Iout pins */
1266 if( (status & AD5755_STATUS_IOUT_FAULT(i)) != 0)
1267 {
1268 CONSOLE_Print("ERROR [ch%d]: Fault detected on Iout pin!\r\n\
1269 You have an open-loop circuit, put a resistor(1kOhm) between Iout and AGND.\r\n",\
1270 i);
1271 fault++;
1272 }
1273 }
1274 if( (status & AD5755_STATUS_PEC_ERROR) != 0 )
1275 {
1276 CONSOLE_Print("PEC ERROR: Denotes a PEC Error on the last data-word\
1277 received over the SPI\r\n");
1278 fault++;
1279 }
1280 if( (status & AD5755_STATUS_RAMP_ACTIVE) != 0 )
1281 {
1282 CONSOLE_Print("WARNING : One of the output channel is slewing. \
1283 (slew rate control is enabled on at least one channel)\r\n");
1284 fault++;
1285 }
1286 if( (status & AD5755_STATUS_OVER_TEMP) != 0 )
1287 {
1288 CONSOLE_Print("ERROR : The device core temperature exceeds \
1289 approximately 150`C\r\n");
1290 fault++;
1291 }
1292 if(fault == 0)
1293 {
1294 CONSOLE_Print("No faults or warnings.\r\n");
1295 }
1296 }
1297 }
1298
1299 /**************************************************************************//**
1300 * @brief Function of the testSPI! command. Read back the Status register and
1301 * throws an error message is there is any faults or errors in the system.
1302 *
1303 * @param param - None
1304 * @param paramNo - number of the arguments
1305 *
1306 * @return void
1307 ******************************************************************************/
1308 void TestSpi(double* param, char paramNo)
1309 {
1310 long statusBuf = 0;
1311 unsigned short statusReg = 0;
1312 /* Set the D12 bit in the Software register */
1313 AD5755_SetControlRegisters( AD5755_CREG_SOFT,
1314 0,
1315 (1 << 12));
1316 /* Read back the status register to check the 'User Toggle' bit */
1317 statusBuf = AD5755_GetRegisterValue(AD5755_RD_STATUS_REG);
1318 if(statusBuf >= 0)
1319 {
1320 statusReg = (unsigned short)statusBuf;
1321 if(statusReg & AD5755_STATUS_USER_BIT)
1322 {
1323 CONSOLE_Print("SPI test PASSED!\r\n");
1324 }
1325 else
1326 {
1327 CONSOLE_Print("SPI test FAILED!\r\n");
1328 }
1329 }
1330 else
1331 {
1332 CONSOLE_Print("AD5755 CRC ERROR!\r\n");
1333 }
1334 }