Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for AD5449.
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 "AD5449.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 = "load=",
63 .description = "Loads selected DAC input register with a given value.",
64 .acceptedValue = "Accepted values:\r\n\
65 \tchannel:\r\n\
66 \t0 - select DAC A input register.\r\n\
67 \t1 - select DAC B input register.\r\n\
68 \tvalue:\r\n\
69 \t0 .. (2^DAC_Resolution)-1 - value to be written in register.",
70 .example = "To load DAC B input register with 500, type: load=1 500",
71 },
72 [2] = {
73 .name = "loadAll=",
74 .description = "Loads both DAC input registers.",
75 .acceptedValue = "Accepted values:\r\n\
76 \t0 .. (2^DAC_Resolution)-1 - value to be written in register.",
77 .example = "To load both DAC input registers with 3525, \
78 type: loadBoth=3525",
79 },
80 [3] = {
81 .name = "loadAndUpdate=",
82 .description = "Loads and updates the selected DAC with a given value.",
83 .acceptedValue = "Accepted values:\r\n\
84 \tchannel:\r\n\
85 \t0 - select DAC A.\r\n\
86 \t1 - select DAC B.\r\n\
87 \tvalue:\r\n\
88 \t0 .. (2^DAC_Resolution)-1 - value to be written in register.",
89 .example = "To load and update DAC A with 2000, \
90 type: loadAndUpdate=0 2000",
91 },
92 [4] = {
93 .name = "updateAll!",
94 .description = "Updates both DAC outputs.",
95 .acceptedValue = "",
96 .example = "",
97 },
98 [5] = {
99 .name = "readback?",
100 .description = "Reads from the selected DAC register.",
101 .acceptedValue = "Accepted values:\r\n\
102 \tchannel:\r\n\
103 \t0 - read from DAC A.\r\n\
104 \t1 - read from DAC B.",
105 .example = "To read from the DAC A register, type: readback?0",
106 },
107 [6] = {
108 .name = "clearToZero!",
109 .description = "Clears both DAC outputs to zero scale.",
110 .acceptedValue = "",
111 .example = "",
112 },
113 [7] = {
114 .name = "clearToMid!",
115 .description = "Clears both DAC outputs to midscale.",
116 .acceptedValue = "",
117 .example = "",
118 },
119 [8] = {
120 .name = "ldacPin=",
121 .description = "Sets the output value of LDAC pin.",
122 .acceptedValue = "Accepted values:\r\n\
123 \t0 - sets LDAC pin low.(default)\r\n\
124 \t1 - sets LDAC pin high.",
125 .example = "To set the LDAC pin high, type: ldacPin=1",
126 },
127 [9] = {
128 .name = "ldacPin?",
129 .description = "Displays the value of LDAC pin.",
130 .acceptedValue = "",
131 .example = "",
132 },
133 [10] = {
134 .name = "clrPin=",
135 .description = "Sets the output value of CLR pin.",
136 .acceptedValue = "Accepted values:\r\n\
137 \t0 - sets CLR pin low.\r\n\
138 \t1 - sets CLR pin high.(default)",
139 .example = "To set the CLR pin low, type: clrPin=0",
140 },
141 [11] = {
142 .name = "clrPin?",
143 .description = "Displays the value of CLR pin.",
144 .acceptedValue = "",
145 .example = "",
146 }
147 };
148
149 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
150
151 /******************************************************************************/
152 /************************ Variables Definitions *******************************/
153 /******************************************************************************/
154 cmdFunction cmdFunctions[12] = {GetHelp, SetLoad, SetLoadAll, SetLoadAndUpdate,
155 SetUpdateAll, GetReadback, SetClearToZero,
156 SetClearToMid, SetLdacPin, GetLdacPin,
157 SetClrPin, GetClrPin};
158
159 /* Variables holding information about the device */
160 AD5449_type_t deviceType;
161 unsigned char ldac = 0;
162 unsigned char clr = 1;
163 unsigned short max_value = 0;
164
165 /***************************************************************************//**
166 * @brief Displays error message.
167 *
168 * @return None.
169 *******************************************************************************/
170 void DisplayError(unsigned char funcNo)
171 {
172 /* Display error messages */
173 CONSOLE_Print("Invalid parameter!\r\n");
174 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
175 (char*)cmdList[funcNo].description, \
176 (char*)cmdList[funcNo].acceptedValue);
177 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
178 }
179
180 /***************************************************************************//**
181 * @brief Internal function for displaying all the command with its description.
182 *
183 * @return None.
184 *******************************************************************************/
185 void DisplayCmdList()
186 {
187 unsigned char displayCmd;
188
189 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
190 {
191 CONSOLE_Print("Invalid parameter!\r\n");
192 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
193 (char*)cmdList[displayCmd].description);
194 }
195 }
196
197 /***************************************************************************//**
198 * @brief Verify if the given parameter is between his valid limits.
199 *
200 * @param None.
201 *
202 * @return None.
203 *******************************************************************************/
204 void paramLimit(double* param,
205 unsigned short lowerLimit,
206 unsigned short upperLimit)
207 {
208 if(*param < lowerLimit)
209 {
210 *param = lowerLimit;
211 }
212 else
213 {
214 if(*param > upperLimit)
215 {
216 *param = upperLimit;
217 }
218 }
219 }
220
221 /***************************************************************************//**
222 * @brief Displays all available commands.
223 *
224 * @return None.
225 *******************************************************************************/
226 void GetHelp(double* param, char paramNo) // "help?" command
227 {
228 unsigned char displayCmd;
229
230 CONSOLE_Print("Available commands:\r\n");
231 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
232 {
233 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
234 (char*)cmdList[displayCmd].description,
235 (char*)cmdList[displayCmd].acceptedValue);
236 }
237 }
238
239 /***************************************************************************//**
240 * @brief Set the current device type.
241 *
242 * @return None.
243 *******************************************************************************/
244 void DoDeviceLock(void)
245 {
246 char device = 0;
247 char deviceLocked = -1;
248
249 while(deviceLocked < 0)
250 {
251 CONSOLE_Print("Please specify your device.\r\n\
252 For AD5415 type 0\r\n\
253 For AD5426 type 1\r\n\
254 For AD5429 type 2\r\n\
255 For AD5432 type 3\r\n\
256 For AD5439 type 4\r\n\
257 For AD5443 type 5\r\n\
258 For AD5449 type 6\r\n");
259 CONSOLE_GetCommand(&device);
260 if((device >= 0x30) && (device <= 0x36))
261 {
262 deviceLocked = 1;
263 switch(device)
264 {
265 case 0x30 :
266 {
267 deviceType = ID_AD5415;
268 max_value = 0xFFF;
269 break;
270 }
271 case 0x31 :
272 {
273 deviceType = ID_AD5426;
274 max_value = 0xFF;
275 break;
276 }
277 case 0x32 :
278 {
279 deviceType = ID_AD5429;
280 max_value = 0xFF;
281 break;
282 }
283 case 0x33 :
284 {
285 deviceType = ID_AD5432;
286 max_value = 0x3FF;
287 break;
288 }
289 case 0x34 :
290 {
291 deviceType = ID_AD5439;
292 max_value = 0x3FF;
293 break;
294 }
295 case 0x35 :
296 {
297 deviceType = ID_AD5443;
298 max_value = 0xFFF;
299 break;
300 }
301 case 0x36 :
302 {
303 deviceType = ID_AD5449;
304 max_value = 0xFFF;
305 break;
306 }
307 }
308 }
309 else
310 {
311 CONSOLE_Print("Please type the number corresponding for \
312 your device!\r\n");
313 }
314 }
315 }
316
317 /***************************************************************************//**
318 * @brief Initializes the device.
319 *
320 * @return - The result of the initialization.
321 * Example: ERROR - the device was not initialized or the device
322 * is not present.
323 * SUCCES - the device was initialized and the device
324 * is present.
325 *******************************************************************************/
326 char DoDeviceInit(void)
327 {
328
329 DoDeviceLock();
330
331 if(AD5449_Init(deviceType) == 0)
332 {
333 CONSOLE_Print("Device OK\r\n");
334 DisplayCmdList();
335 return SUCCESS;
336 }
337 else
338 {
339 CONSOLE_Print("Device Error\r\n");
340 return ERROR;
341 }
342 }
343
344 /***************************************************************************//**
345 * @brief Loads and updates the selected DAC with a given value.
346 *
347 * @param param[0] - selected DAC.
348 * param[1] - value to be written in register.
349 *
350 * @return None.
351 *******************************************************************************/
352 void SetLoadAndUpdate(double* param, char paramNo) // "loadAndUpdate=" command
353 {
354
355 unsigned char channel = 0;
356 unsigned short dacValue = 0;
357
358 /* Check if the parameters are valid */
359 if(paramNo >= 2)
360 {
361 paramLimit(&param[0], 0, 1);
362 paramLimit(&param[1], 0, max_value);
363
364 channel = (unsigned char)param[0];
365 dacValue = (unsigned short)param[1];
366
367 AD5449_LoadUpdateChannel(channel, dacValue);
368 /* Send feedback to user */
369 CONSOLE_Print("channel=%d; value=%d\r\n",channel, dacValue);
370 }
371 else
372 {
373 /* Display error messages */
374 DisplayError(3);
375 }
376 }
377
378
379 /***************************************************************************//**
380 * @brief Loads selected DAC input register with a given value.
381 *
382 * @param param[0] - selected DAC.
383 * param[1] - value to be written in register.
384 *
385 * @return None.
386 *******************************************************************************/
387 void SetLoad(double* param, char paramNo) // "load=" command
388 {
389 unsigned char channel = 0;
390 unsigned short dacValue = 0;
391
392 /* Check if the parameters are valid */
393 if(paramNo >= 2)
394 {
395 paramLimit(&param[0], 0, 1);
396 paramLimit(&param[1], 0, max_value);
397
398 channel = (unsigned char)param[0];
399 dacValue = (unsigned short)param[1];
400
401 AD5449_LoadChannel(channel, dacValue);
402 /* Send feedback to user */
403 CONSOLE_Print("channel=%d; value=%d\r\n",channel, dacValue);
404 }
405 else
406 {
407 /* Display error messages */
408 DisplayError(1);
409 }
410 }
411
412 /***************************************************************************//**
413 * @brief Sets the output value of LDAC pin.
414 *
415 * @param param[0] - value to be set for LDAC pin.
416 *
417 * @return None.
418 *******************************************************************************/
419 void SetLdacPin(double* param, char paramNo) // "ldacPin=" command
420 {
421 unsigned char status = 0;
422
423 /* Check if the parameter is valid */
424 if(paramNo >= 1)
425 {
426 paramLimit(&param[0], 0, 1);
427
428 status = (unsigned char) param[0];
429
430 if (status == 0)
431 {
432 AD5449_LDAC_LOW;
433 ldac = 0;
434 }
435 else
436 {
437 if (status == 1)
438 {
439 AD5449_LDAC_HIGH;
440 ldac = 1;
441 }
442 }
443 /* Send feedback to user */
444 CONSOLE_Print("%s%d\r\n",(char*)cmdList[8].name, status);
445 }
446 else
447 {
448 /* Display error messages */
449 DisplayError(8);
450 }
451 }
452
453 /***************************************************************************//**
454 * @brief Sets the output value of CLR pin.
455 *
456 * @param param[0] - value to be set for CLR pin.
457 *
458 * @return None.
459 *******************************************************************************/
460 void SetClrPin(double* param, char paramNo) // "clrPin=" command
461 {
462 unsigned char status = 0;
463
464 /* Check if the parameter is valid */
465 if(paramNo >= 1)
466 {
467 paramLimit(&param[0], 0, 1);
468
469 status = (unsigned char) param[0];
470
471 if (status == 0)
472 {
473 AD5449_CLR_LOW;
474 clr = 0;
475 }
476 else
477 {
478 if (status == 1)
479 {
480 AD5449_CLR_HIGH;
481 clr = 1;
482 }
483 }
484 /* Send feedback to user */
485 CONSOLE_Print("%s%d\r\n",(char*)cmdList[10].name, status);
486 }
487 else
488 {
489 /* Display error messages */
490 DisplayError(10);
491 }
492 }
493
494 /***************************************************************************//**
495 * @brief Reads from the selected DAC register.
496 *
497 * @param param[0] - selected DAC.
498 *
499 * @return None.
500 *******************************************************************************/
501 void GetReadback(double* param, char paramNo) // "readback?" command
502 {
503 unsigned char channel = 0;
504 unsigned short dacValue = 0;
505
506 /* Check if the parameter is valid */
507 if(paramNo >= 1)
508 {
509 paramLimit(&param[0], 0, 1);
510 channel = (unsigned char)param[0];
511 dacValue = AD5449_ReadbackChannel(channel);
512 /* Send feedback to user */
513 CONSOLE_Print("register=%d for channel %d\r\n", dacValue, channel);
514 }
515 else
516 {
517 /* Display error messages */
518 DisplayError(5);
519 }
520 }
521
522 /***************************************************************************//**
523 * @brief Loads both DAC input registers with a given value.
524 *
525 * @param param[0] - value to be written in both registers.
526 *
527 * @return None.
528 *******************************************************************************/
529 void SetLoadAll(double* param, char paramNo) // "loadBoth=" command
530 {
531 unsigned short dacValue = 0;
532
533 if((deviceType == ID_AD5426) |
534 (deviceType == ID_AD5432) |
535 (deviceType == ID_AD5443) )
536 {
537 CONSOLE_Print("This function is not supported \
538 by the current device!\n\r");
539 }
540 else
541 {
542 /* Check if the parameter is valid */
543 if(paramNo >= 1)
544 {
545 paramLimit(&param[0], 0, max_value);
546 dacValue = (unsigned short)param[0];
547 AD5449_LoadAll(dacValue);
548 /* Send feedback to user */
549 CONSOLE_Print("value=%d\r\n", dacValue);
550 }
551 else
552 {
553 /* Display error messages */
554 DisplayError(2);
555 }
556 }
557 }
558
559 /***************************************************************************//**
560 * @brief Updates both DAC outputs.
561 *
562 * @param None.
563 *
564 * @return None.
565 *******************************************************************************/
566 void SetUpdateAll(double* param, char paramNo) // "updateBoth!" command
567 {
568 if((deviceType == ID_AD5426) |
569 (deviceType == ID_AD5432) |
570 (deviceType == ID_AD5443) )
571 {
572 CONSOLE_Print("This function is not supported \
573 by the current device!\n\r");
574 }
575 else
576 {
577 AD5449_UpdateAll();
578 /* Send feedback to user */
579 CONSOLE_Print("Both DAC outputs were updated.\r\n");
580 }
581 }
582
583 /***************************************************************************//**
584 * @brief Clears both DAC outputs to zero scale.
585 *
586 * @param None.
587 *
588 * @return None.
589 *******************************************************************************/
590 void SetClearToZero(double* param, char paramNo) // "clearToZero!" command
591 {
592 AD5449_ClearScaleSetup(AD5449_ZERO_SCALE);
593 /* Send feedback to user */
594 CONSOLE_Print("Both DAC outputs were cleared to zero scale.\r\n");
595 }
596
597 /***************************************************************************//**
598 * @brief Clears both DAC outputs to midscale.
599 *
600 * @param None.
601 *
602 * @return None.
603 *******************************************************************************/
604 void SetClearToMid(double* param, char paramNo) // "clearToMid!" command
605 {
606 AD5449_ClearScaleSetup(AD5449_MID_SCALE);
607 /* Send feedback to user */
608 CONSOLE_Print("Both DAC outputs were cleared to midscale.\r\n");
609 }
610
611 /***************************************************************************//**
612 * @brief Displays the value of LDAC pin.
613 *
614 * @param None.
615 *
616 * @return None.
617 *******************************************************************************/
618 void GetLdacPin(double* param, char paramNo) // "ldacPin?" command
619 {
620 /* Send requested value to user */
621 CONSOLE_Print("LDAC=%d\r\n",ldac);
622 }
623
624 /***************************************************************************//**
625 * @brief Displays the value of CLR pin.
626 *
627 * @param None.
628 *
629 * @return None.
630 *******************************************************************************/
631 void GetClrPin(double* param, char paramNo) // "clrPin?" command
632 {
633 /* Send requested value to user */
634 CONSOLE_Print("CLR=%d\r\n",clr);
635 }