Debian Codegraph @

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

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