Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Commanad.c
3 * @brief Implementation of the commands given by user through UART for CN0235.
4 * @author Lucian Sin ([email protected])
5 *
6 *******************************************************************************
7 * Copyright 2014(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 "Console.h"
47 #include "ad7280a.h"
48 #include "Communication.h"
49 #include "TIME.h"
50
51 /******************************************************************************/
52 /************************ Constants Definitions *******************************/
53 /******************************************************************************/
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 = "adcCode?",
64 .description = "Displays the ADC code of one channel from selected \
65 device.",
66 .acceptedValue = "Accepted values:\r\n\
67 \tdevice:\r\n\
68 \t0 - master\r\n\
69 \t1 - slave\r\n\
70 \tregister address:\r\n\
71 \t 0 - Cell Voltage 1\r\n\
72 \t 1 - Cell Voltage 2\r\n\
73 \t 2 - Cell Voltage 3\r\n\
74 \t 3 - Cell Voltage 4\r\n\
75 \t 4 - Cell Voltage 5\r\n\
76 \t 5 - Cell Voltage 6\r\n\
77 \t 6 - AUX ADC 1\r\n\
78 \t 7 - AUX ADC 2\r\n\
79 \t 8 - AUX ADC 3\r\n\
80 \t 9 - AUX ADC 4\r\n\
81 \t10 - AUX ADC 5\r\n\
82 \t11 - AUX ADC 6",
83 .example = "To display the ADC code for Cell Voltage 4, master device,\
84 type: adcCode?0 3",
85 },
86 [2] = {
87 .name = "voltage?",
88 .description = "Displays the input voltage of one channel from \
89 selected device.",
90 .acceptedValue = "Accepted values:\r\n\
91 \tdevice:\r\n\
92 \t0 - master\r\n\
93 \t1 - slave\r\n \
94 \tregister address:\r\n\
95 \t 0 - Cell Voltage 1\r\n\
96 \t 1 - Cell Voltage 2\r\n\
97 \t 2 - Cell Voltage 3\r\n\
98 \t 3 - Cell Voltage 4\r\n\
99 \t 4 - Cell Voltage 5\r\n\
100 \t 5 - Cell Voltage 6\r\n\
101 \t 6 - AUX ADC 1\r\n\
102 \t 7 - AUX ADC 2\r\n\
103 \t 8 - AUX ADC 3\r\n\
104 \t 9 - AUX ADC 4\r\n\
105 \t10 - AUX ADC 5\r\n\
106 \t11 - AUX ADC 6",
107 .example = "To display the input voltage for AUX ADC 5, slave device, \
108 type: voltage?1 10",
109 },
110 [3] = {
111 .name = "register?",
112 .description = "Displays the content of one register from selected \
113 device.",
114 .acceptedValue = "Accepted values:\r\n\
115 \tdevice:\r\n\
116 \t0 - master\r\n\
117 \t1 - slave\r\n \
118 \tregister address:\r\n\
119 \t13 - Control high byte\r\n\
120 \t14 - Control low byte\r\n\
121 \t15 - Cell Overvoltage\r\n\
122 \t16 - Cell Undervoltage\r\n\
123 \t ... (see AD7280A_Datasheet p.28)\r\n\
124 \t29 - CNVST Control",
125 .example = "To display the content of Alert Register(address=19), \
126 master device, type: register?0 19",
127 },
128 [4] = {
129 .name = "register=",
130 .description = "Sets the content of one register from selected \
131 device.",
132 .acceptedValue = "Accepted values:\r\n\
133 \tdevice:\r\n\
134 \t0 - master\r\n\
135 \t1 - slave\r\n \
136 \tregister address:\r\n\
137 \t13 - Control high byte\r\n\
138 \t14 - Control low byte\r\n\
139 \t15 - Cell Overvoltage\r\n\
140 \t16 - Cell Undervoltage\r\n\
141 \t ... (see AD7280A_Datasheet p.28)\r\n\
142 \t29 - CNVST Control\r\n\
143 \tregister value:\r\n\
144 \t 0 .. 255 - value to be written inside the register.",
145 .example = "To set the content of Cell Overvoltage Register(address=15), \
146 master device, to 100, type: register=0 15 100",
147 },
148 [5] = {
149 .name = "selfTestAD7280A!",
150 .description = "Performs the self test for both AD7280A devices on the \
151 board (one master and one slave).",
152 .acceptedValue = "",
153 .example = "",
154 },
155 [6] = {
156 .name = "alertPinAD7280A?",
157 .description = "Reads the status of Alert Pin from AD7280A.",
158 .acceptedValue = "",
159 .example = "",
160 },
161 [7] = {
162 .name = "enableAD8280=",
163 .description = "Enables/disables the AD8280 device.",
164 .acceptedValue = "Accepted values:\r\n\
165 \t0 - disable the AD8280 device\r\n\
166 \t1 - enable the AD8280 device",
167 .example = "To enable AD8280 device, type: enableAD8280=1",
168 },
169 [8] = {
170 .name = "alarmPinOvAD8280?",
171 .description = "Reads the status of Overvoltage Alarm Pin from AD8280.",
172 .acceptedValue = "",
173 .example = "",
174 },
175 [9] = {
176 .name = "alarmPinUvAD8280?",
177 .description = "Reads the status of Undervoltage Alarm Pin from AD8280.",
178 .acceptedValue = "",
179 .example = "",
180 },
181 [10] = {
182 .name = "selfTestAD8280!",
183 .description = "Performs the self test for both AD8280 devices on the \
184 board (one master and one slave).",
185 .acceptedValue = "",
186 .example = "",
187 },
188 };
189
190 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
191
192 /******************************************************************************/
193 /************************ Variables Definitions *******************************/
194 /******************************************************************************/
195 cmdFunction cmdFunctions[] = {GetHelp, GetAdcCode, GetVoltage, GetRegister,
196 SetRegister, DoSelfTestAD7280A, GetAlertPinAD7280A,
197 SetEnableAD8280, GetAlarmPinOvAD8280,
198 GetAlarmPinUvAD8280, DoSelfTestAD8280};
199
200 /***************************************************************************//**
201 * @brief Displays all available commands.
202 *
203 * @return None.
204 *******************************************************************************/
205 void GetHelp(double* param, char paramNo) // "help?" command
206 {
207 unsigned char displayCmd;
208
209 CONSOLE_Print("Available commands:\r\n");
210 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
211 {
212 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
213 (char*)cmdList[displayCmd].description,
214 (char*)cmdList[displayCmd].acceptedValue);
215 }
216 }
217
218 /***************************************************************************//**
219 * @brief Internal function for displaying all the command with its description.
220 *
221 * @return None.
222 *******************************************************************************/
223 void DisplayCmdList()
224 {
225 unsigned char displayCmd;
226
227 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
228 {
229 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
230 (char*)cmdList[displayCmd].description);
231 }
232 }
233
234 /***************************************************************************//**
235 * @brief Displays error message.
236 *
237 * @return None.
238 *******************************************************************************/
239 void DisplayError(unsigned char funcNo)
240 {
241 /* Display error messages */
242 CONSOLE_Print("Invalid parameter!\r\n");
243 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
244 (char*)cmdList[funcNo].description, \
245 (char*)cmdList[funcNo].acceptedValue);
246 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
247 }
248
249 /***************************************************************************//**
250 * @brief Initializes the device.
251 *
252 * @return - The result of the initialization.
253 * Example: ERROR - the device was not initialized or the device
254 * is not present.
255 * SUCCES - the device was initialized and the device
256 * is present.
257 *******************************************************************************/
258 char DoDeviceInit(void)
259 {
260 if(AD7280A_Init() == 0)
261 {
262 CONSOLE_Print("Device OK\r\n");
263 DisplayCmdList();
264 return SUCCESS;
265 }
266 else
267 {
268 CONSOLE_Print("Device Error\r\n");
269 return ERROR;
270 }
271 }
272
273 /***************************************************************************//**
274 * @brief Displays the output ADC code of one channel from selected device.
275 *
276 * @param param[0] - device address
277 * Example: 0 - master.
278 * 1 - slave.
279 * param[1] - register address
280 * Example: 0x0 - Cell Voltage 1
281 * 0x1 - Cell Voltage 2
282 * ...
283 * 0x5 - Cell Voltage 6
284 * 0x6 - AUX ADC 1
285 * 0x7 - AUX ADC 2
286 * ...
287 * 0xB - AUX ADC 6
288 *
289 * @return None.
290 *******************************************************************************/
291 void GetAdcCode(double* param, char paramNo) // "adcCode?" command
292 {
293 unsigned short adcCode = 0;
294 unsigned char devAddr;
295 unsigned char readReg;
296
297 if (paramNo >= 2)
298 {
299 if (param[0] < 0)
300 {
301 param[0] = 0;
302 }
303 if (param[0] > 1)
304 {
305 param[0] = 1;
306 }
307 if (param[1] < 0)
308 {
309 param[1] = 0;
310 }
311 if (param[1] > 0xB)
312 {
313 param[1] = 0xB;
314 }
315
316 devAddr = (unsigned char)param[0];
317 readReg = (unsigned char)param[1];
318 adcCode = AD7280A_Read_Conversion(devAddr, readReg);
319 if (adcCode >= 0)
320 {
321 /* Send feedback to user */
322 CONSOLE_Print("ADC Code=%d for register address 0x%x, device %d.\r\n",
323 adcCode, readReg, devAddr);
324 }
325 else
326 {
327 CONSOLE_Print("An error occured during transmission.\r\n");
328 }
329 }
330 else
331 {
332 /* Display error messages */
333 DisplayError(1);
334 }
335 }
336
337 /***************************************************************************//**
338 * @brief Displays the input voltage of one channel from selected device.
339 *
340 * @param param[0] - device address
341 * Example: 0 - master.
342 * 1 - slave.
343 * param[1] - register address
344 * Example: 0x0 - Cell Voltage 1
345 * 0x1 - Cell Voltage 2
346 * ...
347 * 0x5 - Cell Voltage 6
348 * 0x6 - AUX ADC 1
349 * 0x7 - AUX ADC 2
350 * ...
351 * 0xB - AUX ADC 6
352 *
353 * @return None.
354 *******************************************************************************/
355 void GetVoltage(double* param, char paramNo) // "voltage?" command
356 {
357 unsigned short adcCode = 0;
358 unsigned char devAddr;
359 unsigned char readReg;
360 float result = 0;
361
362 if (paramNo >= 2)
363 {
364 if (param[0] < 0)
365 {
366 param[0] = 0;
367 }
368 if (param[0] > 1)
369 {
370 param[0] = 1;
371 }
372 if (param[1] < 0)
373 {
374 param[1] = 0;
375 }
376 if (param[1] > 0xB)
377 {
378 param[1] = 0xB;
379 }
380
381 devAddr = (unsigned char)param[0];
382 readReg = (unsigned char)param[1];
383 adcCode = AD7280A_Read_Conversion(devAddr, readReg);
384 if (adcCode >= 0)
385 {
386 /*CONSOLE_Print("ADC Code=%d for register address 0x%x, device %d.\
387 \r\n", adcCode, readReg, devAddr);*/
388 if (readReg <= 5)
389 {
390 result = AD7280A_Convert_Data(0, adcCode);
391 /* Send feedback to user */
392 CONSOLE_Print("Voltage=%.3f[V] for Cell Voltage %d, device %d.\
393 \r\n", result, (readReg + 1), devAddr);
394 }
395 else
396 {
397 result = AD7280A_Convert_Data(1, adcCode);
398 /* Send feedback to user */
399 CONSOLE_Print("Voltage=%.3f[V] for AUX ADC %d, device %d.\r\n",
400 result, (readReg - 5), devAddr);
401 }
402 }
403 else
404 {
405 CONSOLE_Print("An error occured during transmission.\r\n");
406 }
407 }
408 else
409 {
410 /* Display error messages */
411 DisplayError(2);
412 }
413 }
414
415 /***************************************************************************//**
416 * @brief Displays the content of one register from selected device.
417 *
418 * @param param[0] - device address
419 * Example: 0 - master.
420 * 1 - slave.
421 * param[1] - register address (only registers from Register Map with an
422 * address equal or higher than 0x0D)
423 * Example: 0x0D - Control high byte
424 * 0x0E - Control low byte
425 * ... (see AD7280A_Datasheet p.28)
426 * 0x1D - CNVST Control
427 *
428 * @return None.
429 *******************************************************************************/
430 void GetRegister(double* param, char paramNo) // "register?" command
431 {
432 unsigned short contentReg = 0;
433 unsigned char devAddr;
434 unsigned char readReg;
435
436 if (paramNo >= 2)
437 {
438 if (param[0] < 0)
439 {
440 param[0] = 0;
441 }
442 if (param[0] > 1)
443 {
444 param[0] = 1;
445 }
446 if (param[1] < 0x0D)
447 {
448 param[1] = 0x0D;
449 }
450 if (param[1] > 0x1D)
451 {
452 param[1] = 0x1D;
453 }
454
455 devAddr = (unsigned char)param[0];
456 readReg = (unsigned char)param[1];
457 contentReg = AD7280A_Read_Register(devAddr, readReg);
458 if (contentReg >= 0)
459 {
460 /* Send feedback to user */
461 CONSOLE_Print("Register content=0x%x for register address 0x%x, device \
462 %d.\r\n", contentReg, readReg, devAddr);
463 }
464 else
465 {
466 CONSOLE_Print("An error occured during transmission.\r\n");
467 }
468 }
469 else
470 {
471 /* Display error messages */
472 DisplayError(3);
473 }
474 }
475
476 /***************************************************************************//**
477 * @brief Sets the content of one register from selected device.
478 *
479 * @param param[0] - device address
480 * Example: 0 - master.
481 * 1 - slave.
482 * param[1] - register address (only registers from Register Map with an
483 * address equal or higher than 0x0D)
484 * Example: 0x0D - Control high byte
485 * 0x0E - Control low byte
486 * ... (see AD7280A_Datasheet p.28)
487 * 0x1D - CNVST Control
488 *
489 * @return None.
490 *******************************************************************************/
491 void SetRegister(double* param, char paramNo) // "register=" command
492 {
493 unsigned char devAddr;
494 unsigned char readReg;
495 unsigned char regVal;
496
497 if (paramNo >= 3)
498 {
499 if (param[0] < 0)
500 {
501 param[0] = 0;
502 }
503 if (param[0] > 1)
504 {
505 param[0] = 1;
506 }
507 if (param[1] < 0x0D)
508 {
509 param[1] = 0x0D;
510 }
511 if (param[1] > 0x1D)
512 {
513 param[1] = 0x1D;
514 }
515 if (param[2] < 0)
516 {
517 param[2] = 0;
518 }
519 if (param[2] > 0xFF)
520 {
521 param[2] = 0xFF;
522 }
523
524 devAddr = (unsigned char)param[0];
525 readReg = (unsigned char)param[1];
526 regVal = (unsigned char)param[2];
527 AD7280A_Write_Register(devAddr, readReg, regVal);
528 /* Send feedback to user */
529 CONSOLE_Print("Register content=0x%x for register address 0x%x, device \
530 %d.\r\n", regVal, readReg, devAddr);
531 }
532 else
533 {
534 /* Display error messages */
535 DisplayError(4);
536 }
537 }
538
539 /***************************************************************************//**
540 * @brief Performs the self test for both AD7280A devices on the board (one
541 * master and one slave).
542 *
543 * @param none.
544 *
545 * @return none.
546 *******************************************************************************/
547 void DoSelfTestAD7280A(double* param, char paramNo) // "selfTestAD7280A!" command
548 {
549 float selfTestRegA = 0;
550 float selfTestRegB = 0;
551
552 AD7280A_Selftest_All(&selfTestRegA, &selfTestRegB);
553 /* Send feedback to user */
554 CONSOLE_Print("Self Test Master voltage=%.3f[V]\r\n",selfTestRegA);
555 CONSOLE_Print("Self Test Slave voltage=%.3f[V]\r\n",selfTestRegB);
556 }
557
558 /***************************************************************************//**
559 * @brief Reads the status of Alert Pin from AD7280A.
560 *
561 * @param none.
562 *
563 * @return none.
564 *******************************************************************************/
565 void GetAlertPinAD7280A(double* param, char paramNo) // "alertPinAD7280A?" command
566 {
567 char alert = -1;
568
569 alert = AD7280A_Alert_Pin();
570 /* Send feedback to user */
571 CONSOLE_Print("Alert Pin=%d\r\n", alert);
572 }
573
574 /***************************************************************************//**
575 * @brief Enables/disables the AD8280 device.
576 *
577 * @param param[0] - Enable pin status.
578 *
579 * @return none.
580 *******************************************************************************/
581 void SetEnableAD8280(double* param, char paramNo) // "enableAD8280=" command
582 {
583 unsigned char enable;
584
585 if (paramNo >= 1)
586 {
587 if (param[0] < 0)
588 {
589 param[0] = 0;
590 }
591 if (param[0] > 1)
592 {
593 param[0] = 1;
594 }
595
596 enable = (unsigned char)param[0];
597 if (enable == 1)
598 {
599 GPIO3_HIGH; // ENBL_IN active
600 }
601 else
602 {
603 if (enable == 0)
604 {
605 GPIO3_LOW; // ENBL_IN inactive
606 }
607 }
608 /* Send feedback to user */
609 CONSOLE_Print("AD8280 ENABLE Pin=%d\r\n", enable);
610 }
611 else
612 {
613 /* Display error messages */
614 DisplayError(7);
615 }
616 }
617
618 /***************************************************************************//**
619 * @brief Reads the status of Overvoltage Alarm Pin from AD8280.
620 *
621 * @param none.
622 *
623 * @return none.
624 *******************************************************************************/
625 void GetAlarmPinOvAD8280(double* param, char paramNo) // "alarmPinOvAD8280?" command
626 {
627 char alarm = -1;
628
629 alarm = (GPIO_GET_DATA(GPIO_BASEADDR) & GPIO_1_MASK) ? 1 : 0;
630 /* Send feedback to user */
631 CONSOLE_Print("Overvoltage Alarm Pin=%d\r\n", alarm);
632 }
633
634 /***************************************************************************//**
635 * @brief Reads the status of Undervoltage Alarm Pin from AD8280.
636 *
637 * @param none.
638 *
639 * @return none.
640 *******************************************************************************/
641 void GetAlarmPinUvAD8280(double* param, char paramNo) // "alarmPinUvAD8280?" command
642 {
643 char alarm = -1;
644
645 alarm = (GPIO_GET_DATA(GPIO_BASEADDR) & GPIO_4_MASK) ? 1 : 0;
646 /* Send feedback to user */
647 CONSOLE_Print("Undervoltage Alarm Pin=%d\r\n", alarm);
648 }
649
650 /***************************************************************************//**
651 * @brief Performs the self test for both AD8280 devices on the board (one
652 * master and one slave).
653 *
654 * @param none.
655 *
656 * @return none.
657 *******************************************************************************/
658 void DoSelfTestAD8280(double* param, char paramNo) // "selfTestAD8280!" command
659 {
660 char alarmUV = -1;
661 char alarmOV = -1;
662
663 GPIO0_HIGH; // TEST_IN active
664 /* Wait 4 seconds */
665 TIME_DelayMs(2000);
666 TIME_DelayMs(2000);
667 GPIO0_LOW; // TEST_IN inactive
668
669 alarmUV = (GPIO_GET_DATA(GPIO_BASEADDR) & GPIO_4_MASK) ? 1 : 0;
670 alarmOV = (GPIO_GET_DATA(GPIO_BASEADDR) & GPIO_1_MASK) ? 1 : 0;
671
672 /* Send feedback to user */
673 if ((alarmUV == 0) && (alarmOV == 0))
674 {
675 CONSOLE_Print("AD8280 Self Test passed.\r\n");
676 }
677 else
678 {
679 CONSOLE_Print("AD8280 Self Test failed.\r\n");
680 }
681 }