Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for AD5446.
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
12 * modification, 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
30 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
31 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
33 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
34 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
36 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
37 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
38 * OF THIS 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 "AD5446.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 = "register=",
63 .description = "Writes to the DAC register.",
64 .acceptedValue = "Accepted values:\r\n\
65 \t0 .. 2^DAC_resolution - the value written to the DAC.",
66 .example = "To set the DAC register to 20000, type: register=20000",
67 },
68 [2] = {
69 .name = "register?",
70 .description = "Displays last written value in the DAC register.",
71 .acceptedValue = "",
72 .example = "",
73 },
74 [3] = {
75 .name = "voltage=",
76 .description = "Sets the DAC output voltage.",
77 .acceptedValue = "Accepted values:\r\n\
78 \t-Vref .. +Vref - desired output voltage in milivolts.",
79 .example = "To set the output voltage to 2350.25 [mV], type: voltage = \
80 2350.25",
81 },
82 [4] = {
83 .name = "voltage?",
84 .description = "Displays last written voltage value to the DAC.",
85 .acceptedValue = "",
86 .example = "",
87 },
88 [5] = {
89 .name = "ldacPin=",
90 .description = "Sets the output value of LDAC pin.",
91 .acceptedValue = "Accepted values:\r\n\
92 \t0 - sets LDAC pin low.(default)\r\n\
93 \t1 - sets LDAC pin high.",
94 .example = "To set the LDAC pin high, type: ldacPin=1",
95 },
96 [6] = {
97 .name = "ldacPin?",
98 .description = "Displays the value of LDAC pin.",
99 .acceptedValue = "",
100 .example = "",
101 },
102 [7] = {
103 .name = "clrPin=",
104 .description = "Sets the output value of CLR pin.",
105 .acceptedValue = "Accepted values:\r\n\
106 \t0 - sets CLR pin low.\r\n\
107 \t1 - sets CLR pin high.(default)",
108 .example = "To set the CLR pin low, type: clrPin=0",
109 },
110 [8] = {
111 .name = "clrPin?",
112 .description = "Displays the value of CLR pin.",
113 .acceptedValue = "",
114 .example = "",
115 }
116 };
117
118 /* Define the output type of each evaluation board */
119 const vout_type_t boardOutput[] = {
120 unipolar_inv, // ID_AD5553
121 unipolar_inv, // ID_AD5543
122 bipolar, // ID_AD5542A
123 unipolar, // ID_AD5541A
124 unipolar, // ID_AD5512A
125 unipolar_inv, // ID_AD5453
126 unipolar, // ID_AD5452
127 unipolar, // ID_AD5451
128 unipolar, // ID_AD5450
129 unipolar_inv, // ID_AD5446
130 unipolar_inv // ID_AD5444
131 };
132
133 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
134
135 /******************************************************************************/
136 /************************ Variables Definitions *******************************/
137 /******************************************************************************/
138 cmdFunction cmdFunctions[9] = {GetHelp, SetRegister, GetRegister,
139 SetVoltage, GetVoltage, SetLdacPin, GetLdacPin,
140 SetClrPin, GetClrPin};
141
142 /* Variables holding information about the device */
143 AD5446_type_t deviceType;
144 unsigned char ldac = 0;
145 unsigned char clr = 1;
146 unsigned short registerValue = 0;
147 /* Device specific constants */
148 unsigned short max_value = 0;
149 float vref = 0;
150
151 /***************************************************************************//**
152 * @brief Internal function for displaying error messages.
153 *
154 * @return None.
155 *******************************************************************************/
156 void DisplayError(unsigned char funcNo)
157 {
158 /* Display error messages */
159 CONSOLE_Print("Invalid parameter!\r\n");
160 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
161 (char*)cmdList[funcNo].description, \
162 (char*)cmdList[funcNo].acceptedValue);
163 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
164 }
165
166 /***************************************************************************//**
167 * @brief Internal function for displaying all the command with its description.
168 *
169 * @return None.
170 *******************************************************************************/
171 void DisplayCmdList()
172 {
173 unsigned char displayCmd;
174
175 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
176 {
177 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
178 (char*)cmdList[displayCmd].description);
179 }
180 }
181
182 /***************************************************************************//**
183 * @brief Verify if the given parameter is between his valid limits.
184 *
185 * @param None.
186 *
187 * @return None.
188 *******************************************************************************/
189 void paramLimit(double* param,
190 unsigned short lowerLimit,
191 unsigned short upperLimit)
192 {
193 if(*param < lowerLimit)
194 {
195 *param = lowerLimit;
196 }
197 else
198 {
199 if(*param > upperLimit)
200 {
201 *param = upperLimit;
202 }
203 }
204 }
205
206 /***************************************************************************//**
207 * @brief Displays all available commands.
208 *
209 * @return None.
210 *******************************************************************************/
211 void GetHelp(double* param, char paramNo) // "help?" command
212 {
213 unsigned char displayCmd;
214
215 CONSOLE_Print("Available commands:\r\n");
216 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
217 {
218 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
219 (char*)cmdList[displayCmd].description,
220 (char*)cmdList[displayCmd].acceptedValue);
221 }
222 }
223
224 /***************************************************************************//**
225 * @brief Set the current device type.
226 *
227 * @return None.
228 *******************************************************************************/
229 void DoDeviceLock(void)
230 {
231 char device = 0;
232 char deviceLocked = -1;
233
234 while(deviceLocked < 0)
235 {
236 CONSOLE_Print("Please specify your device.\r\n\
237 For AD5553 type 0\r\n\
238 For AD5543 type 1\r\n\
239 For AD5542A type 2\r\n\
240 For AD5541A type 3\r\n\
241 For AD5453 type 4\r\n\
242 For AD5446 type 5\r\n");
243 CONSOLE_GetCommand(&device);
244 if((device >= 0x30) && (device <= 0x35))
245 {
246 deviceLocked = 1;
247 switch(device)
248 {
249 case 0x30 :
250 {
251 deviceType = ID_AD5553;
252 max_value = 0x3FFF;
253 vref = 5;
254 break;
255 }
256 case 0x31 :
257 {
258 deviceType = ID_AD5543;
259 max_value = 0xFFFF;
260 vref = 5;
261 break;
262 }
263 case 0x32 :
264 {
265 deviceType = ID_AD5542A;
266 max_value = 0xFFFF;
267 vref = 2.5;
268 break;
269 }
270 case 0x33 :
271 {
272 deviceType = ID_AD5541A;
273 max_value = 0xFFFF;
274 vref = 2.5;
275 break;
276 }
277 case 0x34 :
278 {
279 deviceType = ID_AD5453;
280 max_value = 0x3FFF;
281 vref = 10;
282 break;
283 }
284 case 0x35 :
285 {
286 deviceType = ID_AD5446;
287 max_value = 0x3FFF;
288 vref = 10;
289 break;
290 }
291 }
292 }
293 else
294 {
295 CONSOLE_Print("Please type the number corresponding for \
296 your device!\r\n");
297 }
298 }
299 }
300
301 /***************************************************************************//**
302 * @brief Initializes the device.
303 *
304 * @return - The result of the initialization.
305 * Example: ERROR - the device was not initialized or the device
306 * is not present.
307 * SUCCES - the device was initialized and the device
308 * is present.
309 *******************************************************************************/
310 char DoDeviceInit(void)
311 {
312
313 DoDeviceLock();
314
315 if(AD5446_Init(deviceType) == 0)
316 {
317 CONSOLE_Print("Device OK\r\n");
318 CONSOLE_Print("Slave device ID: %s\r\n", AD5446_SLAVE_ID);
319 DisplayCmdList();
320 return SUCCESS;
321 }
322 else
323 {
324 CONSOLE_Print("Device Error\r\n");
325 return ERROR;
326 }
327 }
328
329 /***************************************************************************//**
330 * @brief Writes to the DAC register.
331 *
332 * @return None.
333 *******************************************************************************/
334 void SetRegister(double* param, char paramNo) /*!< "register=" command */
335 {
336
337 /*!< Check if the parameter is valid */
338 if(paramNo >= 1)
339 {
340 if(param[0] < 0)
341 {
342 param[0] = 0;
343 }
344 if(param[0] > max_value)
345 {
346 param[0] = max_value;
347 }
348 CONSOLE_Print("1.Debug: %f\r\n", param[0]);
349 registerValue = (unsigned short)param[0];
350 CONSOLE_Print("2.Debug: %d\r\n", registerValue);
351 /*!< Write to DAC register */
352 AD5446_SetRegister(0, registerValue);
353 /*!< Send the requested value to user */
354 CONSOLE_Print("%s%d\r\n",(char*)cmdList[1].name, registerValue);
355 }
356 else
357 {
358 /*!< Display error messages */
359 DisplayError(1);
360 }
361 }
362
363 /***************************************************************************//**
364 * @brief Displays last written value in the DAC register.
365 *
366 * @return None.
367 *******************************************************************************/
368 void GetRegister(double* param, char paramNo) /*!< "register?" command */
369 {
370 /*!< Send the requested value to user */
371 CONSOLE_Print("%s%d\r\n",(char*)cmdList[1].name, registerValue);
372 }
373
374 /***************************************************************************//**
375 * @brief Sets the DAC output voltage.
376 *
377 * @param param[0] value of the desired voltage. The value can be between:
378 * - (0 .. Vref), if the output is unipolar
379 * - (-Vref .. 0) if the output is unipolar inverted
380 * - (-Vref .. Vref) if the output is bipolar
381 *
382 * @return None.
383 *******************************************************************************/
384 void SetVoltage(double* param, char paramNo) /*!< "voltage=" command */
385 {
386 double outVoltage = 0;
387 short min_voltage = 0;
388 short max_voltage = (vref * 1000) - 0.001;
389
390 /* Define the lower and upper limits in function of the output polarity */
391 if(boardOutput[deviceType] == bipolar)
392 {
393 min_voltage = (-1) * max_voltage;
394 }
395 if(boardOutput[deviceType] == unipolar_inv)
396 {
397 min_voltage = (-1) * max_voltage;
398 max_voltage = 0;
399 }
400
401 /*!< Check if the parameter is valid */
402 if(paramNo >= 1)
403 {
404 if(param[0] < min_voltage)
405 {
406 param[0] = min_voltage;
407 }
408 if(param[0] > max_voltage)
409 {
410 param[0] = max_voltage;
411 }
412
413 outVoltage = param[0] / 1000;
414 outVoltage = AD5446_SetVoltage(outVoltage, vref, boardOutput[deviceType]);
415 /*!< Get the registerValue using value of the outVoltage */
416 switch(boardOutput[deviceType])
417 {
418 case unipolar :
419 {
420 registerValue = (outVoltage * max_value) / vref;
421 break;
422 }
423 case unipolar_inv :
424 {
425 registerValue = (-1) * (outVoltage * max_value) / vref;
426 break;
427 }
428 case bipolar :
429 {
430 registerValue = ((outVoltage + vref) * (max_value/2)) / vref;
431 break;
432 }
433 }
434 /*!< Send feedback to user */
435 CONSOLE_Print("%s%.3f [mV]\r\n",(char*)cmdList[3].name, outVoltage);
436 }
437 else
438 {
439 /*!< Display error messages */
440 DisplayError(3);
441 }
442 }
443
444 /***************************************************************************//**
445 * @brief Displays last written voltage value to the DAC.
446 *
447 * @return None.
448 *******************************************************************************/
449 void GetVoltage(double* param, char paramNo) /*!< "voltage?" command */
450 {
451 double outVoltage = 0;
452
453 /*!< Get the output voltage using data from registerValue */
454 CONSOLE_Print("Vout type: %d\r\n", boardOutput[deviceType]);
455 switch(boardOutput[deviceType])
456 {
457 case unipolar :
458 {
459 outVoltage = ((float)registerValue / max_value) * vref;
460 break;
461 }
462 case unipolar_inv :
463 {
464 outVoltage = (-1) * ((float)registerValue / max_value) * vref;
465 break;
466 }
467 case bipolar :
468 {
469 outVoltage = (vref * (float)registerValue / (max_value/2)) - vref;
470 break;
471 }
472 }
473
474 /*!< Send feedback to user */
475 CONSOLE_Print("%s%.3f [mV]\r\n",(char*)cmdList[3].name, outVoltage);
476 }
477
478 /***************************************************************************//**
479 * @brief Sets the output value of LDAC pin.
480 *
481 * @param param[0] - value to be set for LDAC pin.
482 *
483 * @return None.
484 *******************************************************************************/
485 void SetLdacPin(double* param, char paramNo) // "ldacPin=" command
486 {
487 unsigned char status = 0;
488
489 if((deviceType == ID_AD5542A) || (deviceType == ID_AD5541A))
490 {
491 /* Check if the parameter is valid */
492 if(paramNo >= 1)
493 {
494 paramLimit(&param[0], 0, 1);
495
496 status = (unsigned char) param[0];
497
498 if (status == 0)
499 {
500 AD5446_LDAC_LOW;
501 ldac = 0;
502 }
503 else
504 {
505 if (status == 1)
506 {
507 AD5446_LDAC_HIGH;
508 ldac = 1;
509 }
510 }
511 /* Send feedback to user */
512 CONSOLE_Print("%s%d\r\n",(char*)cmdList[5].name, status);
513 }
514 else
515 {
516 /* Display error messages */
517 DisplayError(5);
518 }
519 }
520 else
521 {
522 CONSOLE_Print("This function is not supported \
523 by the current device!\n\r");
524 }
525 }
526
527 /***************************************************************************//**
528 * @brief Sets the output value of CLR pin.
529 *
530 * @param param[0] - value to be set for CLR pin.
531 *
532 * @return None.
533 *******************************************************************************/
534 void SetClrPin(double* param, char paramNo) // "clrPin=" command
535 {
536 unsigned char status = 0;
537
538 if((deviceType == ID_AD5542A))
539 {
540 /* Check if the parameter is valid */
541 if(paramNo >= 1)
542 {
543 paramLimit(&param[0], 0, 1);
544
545 status = (unsigned char)param[0];
546
547 if (status == 0)
548 {
549 AD5446_CLR_LOW;
550 clr = 0;
551 }
552 else
553 {
554 if (status == 1)
555 {
556 AD5446_CLR_HIGH;
557 clr = 1;
558 }
559 }
560
561 /* Send feedback to user */
562 CONSOLE_Print("%s%d\r\n",(char*)cmdList[7].name, status);
563 }
564 else
565 {
566 /* Display error messages */
567 DisplayError(7);
568 }
569 }
570 else
571 {
572 CONSOLE_Print("This function is not supported \
573 by the current device!\n\r");
574 }
575 }
576
577 /***************************************************************************//**
578 * @brief Displays the value of LDAC pin.
579 *
580 * @param None.
581 *
582 * @return None.
583 *******************************************************************************/
584 void GetLdacPin(double* param, char paramNo) // "ldacPin?" command
585 {
586
587 if((deviceType == ID_AD5542A) || (deviceType == ID_AD5541A))
588 {
589 /* Send requested value to user */
590 CONSOLE_Print("LDAC=%d\r\n",ldac);
591 }
592 else
593 {
594 CONSOLE_Print("This function is not supported \
595 by the current device!\n\r");
596 }
597 }
598
599 /***************************************************************************//**
600 * @brief Displays the value of CLR pin.
601 *
602 * @param None.
603 *
604 * @return None.
605 *******************************************************************************/
606 void GetClrPin(double* param, char paramNo) // "clrPin?" command
607 {
608 if(deviceType == ID_AD5542A)
609 {
610 /* Send requested value to user */
611 CONSOLE_Print("CLR=%d\r\n",clr);
612 }
613 else
614 {
615 CONSOLE_Print("This function is not supported \
616 by the current device!\n\r");
617 }
618 }