Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Commanad.c
3 * @brief Implementation of the commands given by user through UART for CN0202.
4 * @author Lucian Sin ([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 "Console.h"
47 #include "ad5662.h"
48 #include "ad5750.h"
49 /******************************************************************************/
50 /************************ Constants Definitions *******************************/
51 /******************************************************************************/
52
53 const struct cmd_info cmdList[] = {
54 [0] = {
55 .name = "help?",
56 .description = "Displays all available commands.",
57 .acceptedValue = "",
58 .example = "",
59 },
60 [1] = {
61 .name = "register=",
62 .description = "Write a value to the DAC register.",
63 .acceptedValue = "Accepted values:\r\n\
64 \t0 .. 65535 - value to be written in register.",
65 .example = "To write 20000 to DAC register, type: register=20000",
66 },
67 [2] = {
68 .name = "register?",
69 .description = "Displays the last value written to the DAC register.",
70 .acceptedValue = "",
71 .example = "",
72 },
73 [3] = {
74 .name = "ad5750clrPin=",
75 .description = "Sets the output value of CLR pin.",
76 .acceptedValue = "Accepted values:\r\n\
77 \t0 - sets the CLR pin low.(default)\r\n\
78 \t1 - sets the CLR pin high",
79 .example = "To set the CLR pin high, type: ad5750clrPin=1",
80 },
81 [4] = {
82 .name = "ad5750clrPin?",
83 .description = "Displays the output value of CLR pin.",
84 .acceptedValue = "",
85 .example = "",
86 },
87 [5] = {
88 .name = "addressA0=",
89 .description = "Sets the value of A0 address bit(JP1).",
90 .acceptedValue = "Accepted values:\r\n\
91 \t0 - address is 0b000.(default)\r\n\
92 \t1 - address is 0b001.",
93 .example = "To set the AD5750 address bit to 0b001, type: addressA0=1",
94 },
95 [6] = {
96 .name = "addressA0?",
97 .description = "Displays the value of A0 address bit(JP1).",
98 .acceptedValue = "",
99 .example = "",
100 },
101 [7] = {
102 .name = "range=",
103 .description = "Sets the output range for AD5750.",
104 .acceptedValue = "Accepted values:r\n\
105 \t0 -> 0V to 5V.\r\n\
106 \t1 -> 0V to 6V.\r\n\
107 \t2 -> 0V to 10V.\r\n\
108 \t3 -> 0V to 12V.\r\n\
109 \t4 -> -2.5V to +2.5V.\r\n\
110 \t5 -> -5V to +5V.\r\n\
111 \t6 -> -6V to +6V.\r\n\
112 \t7 -> -10V to +10V.\r\n\
113 \t8 -> -12V to +12V.\r\n\
114 \t9 -> 4mA to 20mA(internal).\r\n\
115 \t10 -> 4mA to 20mA(external).\r\n\
116 \t11 -> 0mA to 20mA(internal).\r\n\
117 \t12 -> 0mA to 20mA(external).\r\n\
118 \t13 -> 0mA to 24mA(internal).\r\n\
119 \t14 -> 0mA to 24mA(external).\r\n\
120 \t15 -> -20mA to +20mA(internal).\r\n\
121 \t16 -> -20mA to +20mA(external).\r\n\
122 \t17 -> -24mA to +24mA(internal).\r\n\
123 \t18 -> -24mA to +24mA(external).\r\n\
124 \t19 -> 3.92mA to 20.4mA(internal).\r\n\
125 \t20 -> 0mA to 20.4mA(internal).\r\n\
126 \t21 -> 0mA to 24.5mA(internal).",
127 .example = "To set the output range to 0V to 10V, type: range=2",
128 },
129 [8] = {
130 .name = "range?",
131 .description = "Displays the current output range.",
132 .acceptedValue = "",
133 .example = "",
134 },
135 [9] = {
136 .name = "fault?",
137 .description = "Displays the list of possible faults.",
138 .acceptedValue = "",
139 .example = "",
140 },
141 };
142
143 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
144
145 const unsigned long MAX_DAC_VALUE = 0xFFFF;
146 extern const unsigned char RANGE_CONFIG[22];
147
148 /******************************************************************************/
149 /************************ Variables Definitions *******************************/
150 /******************************************************************************/
151 cmdFunction cmdFunctions[] = {GetHelp, SetRegisterDac, GetRegisterDac,
152 SetAD5750clrPin,GetAD5750clrPin,SetAD5750AddressA0,
153 GetAD5750AddressA0,SetAD5750Range,GetAD5750Range,
154 GetAD5750Fault};
155
156 /* Variables holding information about the device */
157 unsigned char clr = 0;
158 unsigned char bitA0 = 0;
159 unsigned char rangeVal = 0;
160 unsigned long dacValue = 0;
161
162 /******************************************************************************/
163 /*************************** Functions Definitions ****************************/
164 /******************************************************************************/
165
166 /***************************************************************************//**
167 * @brief Displays all available commands.
168 *
169 * @return None.
170 *******************************************************************************/
171 void GetHelp(double* param, char paramNo) // "help?" command
172 {
173 unsigned char displayCmd;
174
175 CONSOLE_Print("Available commands:\r\n");
176 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
177 {
178 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
179 (char*)cmdList[displayCmd].description,
180 (char*)cmdList[displayCmd].acceptedValue);
181 }
182 }
183
184 /***************************************************************************//**
185 * @brief Internal function for displaying all the command with its description.
186 *
187 * @return None.
188 *******************************************************************************/
189 void DisplayCmdList()
190 {
191 unsigned char displayCmd;
192
193 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
194 {
195 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
196 (char*)cmdList[displayCmd].description);
197 }
198 }
199
200 /***************************************************************************//**
201 * @brief Displays error message.
202 *
203 * @return None.
204 *******************************************************************************/
205 void DisplayError(unsigned char funcNo)
206 {
207 /* Display error messages */
208 CONSOLE_Print("Invalid parameter!\r\n");
209 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
210 (char*)cmdList[funcNo].description, \
211 (char*)cmdList[funcNo].acceptedValue);
212 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
213 }
214
215 /***************************************************************************//**
216 * @brief Initializes the device.
217 *
218 * @return - The result of the initialization.
219 * Example: ERROR - the device was not initialized or the device
220 * is not present.
221 * SUCCES - the device was initialized and the device
222 * is present.
223 *******************************************************************************/
224 char DoDeviceInit(void)
225 {
226 if(ad5750_Init() == 0)
227 {
228 CONSOLE_Print("Device OK\r\n");
229 DisplayCmdList();
230 return SUCCESS;
231 }
232 else
233 {
234 CONSOLE_Print("Device Error\r\n");
235 return ERROR;
236 }
237 }
238
239 /***************************************************************************//*
240 * @brief Sets the shift register of the DAC with the given value.
241 *
242 * @param The value which will be written to the shift register.
243 *
244 * @return None.
245 ******************************************************************************/
246 void SetRegisterDac(double* param, char paramNo) // "register=" command
247 {
248 /* Check if the parameter is valid */
249 if(paramNo >= 1)
250 {
251 if(param[0] < 0)
252 {
253 param[0] = 0;
254 }
255 if(param[0] > MAX_DAC_VALUE)
256 {
257 param[0] = MAX_DAC_VALUE;
258 }
259
260 dacValue = (unsigned long)param[0];
261 dacValue |= AD5662_LOAD << 16; // normal operation
262 ad5662_Write24Bits(dacValue);
263 /* Send feedback to user */
264 CONSOLE_Print("%s%d\r\n",(char*)cmdList[1].name, dacValue);
265 }
266 else
267 {
268 /* Display error messages */
269 DisplayError(1);
270 }
271 }
272
273 /***************************************************************************//**
274 * @brief Displays the last value written to the DAC register.
275 *
276 * @param None.
277 *
278 * @return None.
279 *******************************************************************************/
280 void GetRegisterDac(double* param, char paramNo) // "register?" command
281 {
282 /* Send requested value to user */
283 CONSOLE_Print("%s%d\r\n",(char*)cmdList[1].name, dacValue);
284 }
285
286 /***************************************************************************//**
287 * @brief Sets the output value of AD5750 CLR pin.
288 *
289 * @param param[0] - value to be set for CLR pin.
290 *
291 * @return None.
292 *******************************************************************************/
293 void SetAD5750clrPin(double* param, char paramNo) // "ad5750clrPin=" command
294 {
295 /* Check if the parameter is valid */
296 if(paramNo >= 1)
297 {
298 if(param[0] < 0)
299 {
300 param[0] = 0;
301 }
302 if(param[0] > 1)
303 {
304 param[0] = 1;
305 }
306 clr = (unsigned char)param[0];
307 if (clr == 1)
308 {
309 AD5750_CLR_HIGH;
310 }
311 else
312 {
313 AD5750_CLR_LOW;
314 }
315 /* Send feedback to user */
316 CONSOLE_Print("%s%d\r\n",(char*)cmdList[3].name, clr);
317 }
318 else
319 {
320 /* Display error messages */
321 DisplayError(3);
322 }
323 }
324
325 /***************************************************************************//**
326 * @brief Displays the value of AD5750 CLR pin.
327 *
328 * @param None.
329 *
330 * @return None.
331 *******************************************************************************/
332 void GetAD5750clrPin(double* param, char paramNo) // "ad5750clrPin?" command
333 {
334 /* Send requested value to user */
335 CONSOLE_Print("%s%d\r\n",(char*)cmdList[3].name, clr);
336 }
337
338 /***************************************************************************//**
339 * @brief Sets the A0 address bit from AD5750(from JP1 on the board).
340 *
341 * @param param[0] - value to be set for A0 bit.
342 *
343 * @return None.
344 *******************************************************************************/
345 void SetAD5750AddressA0(double* param, char paramNo) //"addressA0=" command
346 {
347 /* Check if the parameter is valid */
348 if(paramNo >= 1)
349 {
350 if(param[0] < 0)
351 {
352 param[0] = 0;
353 }
354 if(param[0] > 1)
355 {
356 param[0] = 1;
357 }
358 bitA0 = (unsigned char)param[0];
359 /* Send feedback to user */
360 CONSOLE_Print("%s%d\r\n",(char*)cmdList[5].name, bitA0);
361 }
362 else
363 {
364 /* Display error messages */
365 DisplayError(5);
366 }
367 }
368
369 /***************************************************************************//**
370 * @brief Displays the value of A0 address bit from AD5750.
371 *
372 * @param None.
373 *
374 * @return None.
375 *******************************************************************************/
376 void GetAD5750AddressA0(double* param, char paramNo) //"addressA0?" command
377 {
378 /* Send feedback to user */
379 CONSOLE_Print("%s%d\r\n",(char*)cmdList[5].name, bitA0);
380 }
381
382 /***************************************************************************//**
383 * @brief Sets the value of AD5750 output range.
384 *
385 * @param param[0] - value to be set for output range.
386 *
387 * @return None.
388 *******************************************************************************/
389 void SetAD5750Range(double* param, char paramNo) //"range=" command
390 {
391 unsigned short serialWord = 0;
392
393 /* Check if the parameter is valid */
394 if(paramNo >= 1)
395 {
396 if(param[0] < 0)
397 {
398 param[0] = 0;
399 }
400 if(param[0] > 21)
401 {
402 param[0] = 21;
403 }
404 rangeVal = (unsigned char)param[0];
405 serialWord = AD5750_CONTROL_ADDR(bitA0) +
406 AD5750_CONTROL_WRITE +
407 AD5750_CONTROL_RANGE(RANGE_CONFIG[rangeVal] & 0xF) +
408 AD5750_CONTROL_OUTEN +
409 ((RANGE_CONFIG[rangeVal] & 0x10) >> 1);
410 ad5750_Write16Bits(serialWord);
411 /* Send feedback to user */
412 CONSOLE_Print("%s%d\r\n",(char*)cmdList[7].name, rangeVal);
413 }
414 else
415 {
416 /* Display error messages */
417 DisplayError(6);
418 }
419 }
420
421 /***************************************************************************//**
422 * @brief Displays the value of AD5750 output range.
423 *
424 * @param None.
425 *
426 * @return None.
427 *******************************************************************************/
428 void GetAD5750Range(double* param, char paramNo) //"range?" command
429 {
430 /* Send feedback to user */
431 CONSOLE_Print("%s%d\r\n",(char*)cmdList[7].name, rangeVal);
432 }
433
434 /***************************************************************************//**
435 * @brief Displays the list of possible faults.
436 *
437 * @param None.
438 *
439 * @return None.
440 *******************************************************************************/
441 void GetAD5750Fault(double* param, char paramNo) //"fault?" command
442 {
443 unsigned short serialWord = 0;
444 unsigned short statusRegister = 0;
445 unsigned char pecError = 0;
446 unsigned char overTemp = 0;
447 unsigned char iOutFault = 0;
448 unsigned char vOutFault = 0;
449
450 serialWord = AD5750_STATUS_ADDR(bitA0) + AD5750_STATUS_READ;
451 statusRegister = ad5750_Read16Bits(serialWord);
452 pecError = (statusRegister & AD5750_STATUS_PEC_ERR) >> 3;
453 overTemp = (statusRegister & AD5750_STATUS_OVER_TEMP) >> 2;
454 iOutFault = (statusRegister & AD5750_STATUS_IOUT_FAULT) >> 1;
455 vOutFault = (statusRegister & AD5750_STATUS_VOUT_FAULT) >> 0;
456
457 /* Send feedback to user */
458 /* "Error = 0" means there is no error. */
459 /* "Error = 1" means there is an error. */
460 CONSOLE_Print("PEC Error=%d\r\n", pecError);
461 CONSOLE_Print("OVERTEMP Error=%d\r\n", overTemp);
462 CONSOLE_Print("IOUT Fault=%d\r\n", iOutFault);
463 CONSOLE_Print("VOUT Fault=%d\r\n", vOutFault);
464 }