Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for AD5570.
4 * @author Istvan Csomortani ([email protected])
5 *******************************************************************************
6 * Copyright 2013(c) Analog Devices, Inc.
7 *
8 * All rights reserved.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions are met:
12 * - Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * - Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
17 * distribution.
18 * - Neither the name of Analog Devices, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
21 * - The use of this software may or may not infringe the patent rights
22 * of one or more patent holders. This license does not release you
23 * from the requirement that you obtain separate licenses from these
24 * patent holders to use this software.
25 * - Use of the software either in source or binary form, must be run
26 * on or directly connected to an Analog Devices Inc. component.
27 *
28 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
29 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
30 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
31 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
32 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
33 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
34 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
35 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
36 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
37 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
38 *
39 *******************************************************************************/
40
41 /******************************************************************************/
42 /***************************** Include Files **********************************/
43 /******************************************************************************/
44 #include "Command.h"
45 #include "Console.h"
46 #include "AD5570.h"
47 #include "TIME.h"
48
49 /******************************************************************************/
50 /************************ Constants Definitions *******************************/
51 /******************************************************************************/
52 #define MAX_VALUE 0xFFFF
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 = "Loads the DAC input register with a given value.",
64 .acceptedValue = "Accepted values:\r\n\
65 \tvalue:\r\n \
66 \t0 .. 65535 - value to be written into the register.",
67 .example = "To load DAC B input register with 128, type: register=128",
68 },
69 [2] = {
70 .name = "ldacPin=",
71 .description = "Sets the output value of LDAC_n pin.",
72 .acceptedValue = "Accepted values:\r\n\
73 \t0 - sets LDAC_n pin low.(default)\r\n\
74 \t1 - sets LDAC_n pin high.",
75 .example = "To set the LDAC_n pin high, type: ldacPin=1",
76 },
77 [3] = {
78 .name = "ldacPin?",
79 .description = "Displays the value of LDAC_n pin.",
80 .acceptedValue = "",
81 .example = "",
82 },
83 [4] = {
84 .name = "clrPin=",
85 .description = "Sets the output value of CLR_n pin.",
86 .acceptedValue = "Accepted values:\r\n\
87 \t0 - sets CLR_n pin low.\r\n\
88 \t1 - sets CLR_n pin high (default).",
89 .example = "To set the CLR_n pin low, type: clrPin=0",
90 },
91 [5] = {
92 .name = "clrPin?",
93 .description = "Displays the value of CLR_n pin.",
94 .acceptedValue = "",
95 .example = "",
96 },
97 [6] = {
98 .name = "pdPin=",
99 .description = "Sets the output value of PD_n pin.",
100 .acceptedValue = "Accepted values:\r\n\
101 \t0 - sets PD_n pin low.\r\n\
102 \t1 - sets PD_n pin high (default).",
103 .example = "To set the PD_n pin high, type: pdPin=1",
104 },
105 [7] = {
106 .name = "pdPin?",
107 .description = "Displays the value of PD_n pin.",
108 .acceptedValue = "",
109 .example = "",
110 }
111 };
112
113 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
114
115 /******************************************************************************/
116 /************************ Variables Definitions *******************************/
117 /******************************************************************************/
118 cmdFunction cmdFunctions[8] = {GetHelp, SetRegister, SetLdacPin, GetLdacPin, \
119 SetClrPin, GetClrPin, SetPdPin, GetPdPin};
120
121 /* Variables holding information about the device */
122 unsigned char ldac_n = 0;
123 unsigned char clr_n = 1;
124 unsigned char pd_n = 1;
125
126 /***************************************************************************//**
127 * @brief Displays error message.
128 *
129 * @return None.
130 *******************************************************************************/
131 void DisplayError(unsigned char funcNo)
132 {
133 /* Display error messages */
134 CONSOLE_Print("Invalid parameter!\r\n");
135 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
136 (char*)cmdList[funcNo].description, \
137 (char*)cmdList[funcNo].acceptedValue);
138 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
139 }
140
141 /***************************************************************************//**
142 * @brief Internal function for displaying all the command with its description.
143 *
144 * @return None.
145 *******************************************************************************/
146 void DisplayCmdList()
147 {
148 unsigned char displayCmd;
149
150 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
151 {
152 CONSOLE_Print("Invalid parameter!\r\n");
153 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
154 (char*)cmdList[displayCmd].description);
155 }
156 }
157
158 /***************************************************************************//**
159 * @brief Verify if the given parameter is between his valid limits.
160 *
161 * @param None.
162 *
163 * @return None.
164 *******************************************************************************/
165 void paramLimit(double* param,
166 unsigned short lowerLimit,
167 unsigned short upperLimit)
168 {
169 if(*param < lowerLimit)
170 {
171 *param = lowerLimit;
172 }
173 else
174 {
175 if(*param > upperLimit)
176 {
177 *param = upperLimit;
178 }
179 }
180 }
181
182 /**************************************************************************//***
183 * @brief Displays all available commands.
184 *
185 * @return None.
186 *******************************************************************************/
187 void GetHelp(double* param, char paramNo) // "help?" command
188 {
189 unsigned char displayCmd;
190
191 CONSOLE_Print("Available commands:\r\n");
192 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
193 {
194 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
195 (char*)cmdList[displayCmd].description,
196 (char*)cmdList[displayCmd].acceptedValue);
197 }
198 }
199
200 /***************************************************************************//**
201 * @brief Initializes the device.
202 *
203 * @return - The result of the initialization.
204 * Example: ERROR - the device was not initialized or the device
205 * is not present.
206 * SUCCES - the device was initialized and the device
207 * is present.
208 *******************************************************************************/
209 char DoDeviceInit(void)
210 {
211 if(AD5570_Init() == 0)
212 {
213 CONSOLE_Print("AD5570 OK\r\n");
214 DisplayCmdList();
215 return SUCCESS;
216 }
217 else
218 {
219 CONSOLE_Print("AD5570 Error\r\n");
220 return ERROR;
221 }
222 }
223
224 /***************************************************************************//**
225 * @brief Loads the DAC register with a given value.
226 *
227 * @param param[0] - value to be written in register.
228 *
229 * @return None.
230 *******************************************************************************/
231 void SetRegister(double* param, char paramNo) // "loadAndUpdate=" command
232 {
233 unsigned short dacValue = 0;
234
235 /* Check if the parameters are valid */
236 if(paramNo >= 1)
237 {
238 paramLimit(&param[0], 0, MAX_VALUE);
239 dacValue = (unsigned short)param[0];
240 AD5570_SetRegister(dacValue);
241 /* Send feedback to user */
242 CONSOLE_Print("value=%d\r\n", dacValue);
243 }
244 else
245 {
246 /* Display error messages */
247 DisplayError(1);
248 }
249 }
250
251 /***************************************************************************//**
252 * @brief Sets the output value of LDAC_n pin.
253 *
254 * @param param[0] - value to be set for LDAC_n pin.
255 *
256 * @return None.
257 *******************************************************************************/
258 void SetLdacPin(double* param, char paramNo) // "ldacPin=" command
259 {
260 unsigned char status = 0;
261
262 /* Check if the parameter is valid */
263 if(paramNo >= 1)
264 {
265 paramLimit(&param[0], 0, 1);
266
267 status = (unsigned char) param[0];
268
269 if (status == 0)
270 {
271 AD5570_LDAC_LOW;
272 ldac_n = 0;
273 }
274 else
275 {
276 if (status == 1)
277 {
278 AD5570_LDAC_HIGH;
279 ldac_n = 1;
280 }
281 }
282 /* Send feedback to user */
283 CONSOLE_Print("%s%d\r\n",(char*)cmdList[2], status);
284 }
285 else
286 {
287 /* Display error messages */
288 DisplayError(2);
289 }
290 }
291
292 /***************************************************************************//**
293 * @brief Displays the value of LDAC_n pin.
294 *
295 * @param None.
296 *
297 * @return None.
298 *******************************************************************************/
299 void GetLdacPin(double* param, char paramNo) // "ldacPin?" command
300 {
301
302 /* Send requested value to user */
303 CONSOLE_Print("LDAC_n=%d\r\n",ldac_n);
304 }
305
306 /***************************************************************************//**
307 * @brief Sets the output value of CLR_n pin.
308 *
309 * @param param[0] - value to be set for CLR_n pin.
310 *
311 * @return None.
312 *******************************************************************************/
313 void SetClrPin(double* param, char paramNo) // "clrPin=" command
314 {
315 unsigned char status = 0;
316
317 /* Check if the parameter is valid */
318 if(paramNo >= 1)
319 {
320 paramLimit(&param[0], 0, 1);
321
322 status = (unsigned char) param[0];
323
324 if (status == 0)
325 {
326 AD5570_CLR_LOW;
327 clr_n = 0;
328 }
329 else
330 {
331 if (status == 1)
332 {
333 AD5570_CLR_HIGH;
334 clr_n = 1;
335 }
336 }
337 /* Send feedback to user */
338 CONSOLE_Print("%s%d\r\n",(char*)cmdList[4], status);
339 }
340 else
341 {
342 /* Display error messages */
343 DisplayError(4);
344 }
345 }
346
347 /***************************************************************************//**
348 * @brief Displays the value of CLR_n pin.
349 *
350 * @param None.
351 *
352 * @return None.
353 *******************************************************************************/
354 void GetClrPin(double* param, char paramNo) // "clrPin?" command
355 {
356
357 /* Send requested value to user */
358 CONSOLE_Print("CLR_n=%d\r\n",clr_n);
359 }
360
361 /***************************************************************************//**
362 * @brief Sets the output value of PD_n pin.
363 *
364 * @param param[0] - value to be set for PD_n pin.
365 *
366 * @return None.
367 *******************************************************************************/
368 void SetPdPin(double* param, char paramNo) // "pdPin=" command
369 {
370 unsigned char status = 0;
371
372 /* Check if the parameter is valid */
373 if(paramNo >= 1)
374 {
375 paramLimit(&param[0], 0, 1);
376
377 status = (unsigned char) param[0];
378
379 if (status == 0)
380 {
381 AD5570_PD_LOW;
382 pd_n = 0;
383 }
384 else
385 {
386 if (status == 1)
387 {
388 AD5570_PD_HIGH;
389 pd_n = 1;
390 }
391 }
392 /* Send feedback to user */
393 CONSOLE_Print("%s%d\r\n",(char*)cmdList[6], status);
394 }
395 else
396 {
397 /* Display error messages */
398 DisplayError(6);
399 }
400 }
401
402 /***************************************************************************//**
403 * @brief Displays the value of PD_n pin.
404 *
405 * @param None.
406 *
407 * @return None.
408 *******************************************************************************/
409 void GetPdPin(double* param, char paramNo) // "pdPin?" command
410 {
411
412 /* Send requested value to user */
413 CONSOLE_Print("PD_n=%d\r\n",pd_n);
414 }