Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for ad5172 driver.
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 "ad5172.h"
48 #include "TIME.h"
49
50 #define VDD 3.2
51 #define VDD_VSS VDD/2
52 #define MAXPOS 255
53
54 /******************************************************************************/
55 /************************ Constants Definitions *******************************/
56 /******************************************************************************/
57 const struct cmd_info cmdList[] = {
58 [0] = {
59 .name = "help?",
60 .description = "Displays all available commands.",
61 .acceptedValue = "None",
62 .example = "",
63 },
64 [1] = {
65 .name = "rdac=",
66 .description = "Load the wiper register with a give value.",
67 .acceptedValue = "Accepted value:\r\n\
68 \tchannel:\r\n\
69 \t0 to 3 in function of the device type\r\n\
70 \tvalue:\r\n\
71 \t0 .. 255 - wiper register value",
72 .example = "To load the wiper register of channel B with 128, \
73 type: rdac=1 128",
74 },
75 [2] = {
76 .name = "rdac?",
77 .description = "Read back the value of the wiper register.",
78 .acceptedValue = "Accepted values:\r\n\
79 \tchannel:\r\n\
80 \t0 .. 3 in function of the device type",
81 .example = "To read back the value of the wiper register of channel B, \
82 type: rdac?1",
83 },
84 [3] = {
85 .name = "wbuf1?",
86 .description = "Read back the value of the Wiper Buffer in voltage. \
87 (VDD=3.2V)",
88 .acceptedValue = "None",
89 .example = "",
90 },
91 [4] = {
92 .name = "shutdown=",
93 .description = "Shutdown connects wiper to B terminal and open circuits\
94 the A terminal.",
95 .acceptedValue = "Accepted values:\r\n\
96 \tchannel:\r\n\
97 \t0 .. 3 in function of the device type",
98 .example = "To shutdown (or bring back from shutdown) the RDAC of the channel 0, type: shutdown=0",
99 },
100 [5] = {
101 .name = "shutdown?",
102 .description = "Notify about the state of the selected RDAC. (It is in shutdown or not.)",
103 .acceptedValue = "Accepted values:\r\n\
104 \tchannel:\r\n\
105 \t0 .. 3 in function of the device type",
106 .example = "To verify the state of RDAC at channel 1, type shutdown?1",
107 }
108 };
109
110 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
111
112 /******************************************************************************/
113 /************************ Variables Definitions *******************************/
114 /******************************************************************************/
115 cmdFunction cmdFunctions[6] = {GetHelp, SetRdac, GetRdac, GetWBuf1, SetShutdown,
116 GetShutdown};
117
118 AD5172_type_t deviceType;
119
120 unsigned char channelNo = 0;
121 unsigned char channelState[4] = {0,};
122 unsigned short rdacBuffer[4] = {0,};
123
124 /***************************************************************************//**
125 * @brief Internal function for displaying error messages.
126 *
127 * @return None.
128 *******************************************************************************/
129 void DisplayError(unsigned char funcNo)
130 {
131 /* Display error messages */
132 CONSOLE_Print("Invalid parameter!\r\n");
133 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
134 (char*)cmdList[funcNo].description,
135 (char*)cmdList[funcNo].acceptedValue);
136 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
137 }
138
139 /***************************************************************************//**
140 * @brief Internal function for displaying all the command with its description.
141 *
142 * @return None.
143 *******************************************************************************/
144 void DisplayCmdList()
145 {
146 unsigned char displayCmd;
147
148 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
149 {
150 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
151 (char*)cmdList[displayCmd].description);
152 }
153 }
154
155 /***************************************************************************//**
156 * @brief Verify if the given parameter is between his valid limits.
157 * Note: This function is internal!
158 *
159 * @param None.
160 *
161 * @return None.
162 *******************************************************************************/
163 void paramLimit(double* param,
164 unsigned short lowerLimit,
165 unsigned short upperLimit)
166 {
167 if(*param < lowerLimit)
168 {
169 *param = lowerLimit;
170 }
171 else
172 {
173 if(*param > upperLimit)
174 {
175 *param = upperLimit;
176 }
177 }
178 }
179
180 /***************************************************************************//**
181 * @brief Displays all available commands.
182 *
183 * @return None.
184 *******************************************************************************/
185 void GetHelp(double* param, char paramNo) // "help?" command
186 {
187 unsigned char displayCmd;
188
189 CONSOLE_Print("Available commands:\r\n");
190 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
191 {
192 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
193 (char*)cmdList[displayCmd].description,
194 (char*)cmdList[displayCmd].acceptedValue);
195 }
196 }
197
198 /***************************************************************************//**
199 * @brief Set the current device type.
200 *
201 * @return None.
202 *******************************************************************************/
203 void DoDeviceLock(void)
204 {
205 char device = 0;
206 char deviceLocked = -1;
207
208 while(deviceLocked < 0)
209 {
210 CONSOLE_Print("Please specify your device.\r\n\
211 For AD5162 type: 0\r\n\
212 For AD5172 type: 1\r\n\
213 For AD8403 type: 2\r\n");
214
215 CONSOLE_GetCommand(&device);
216 if((device >= 0x30) && (device <= 0x33))
217 {
218 deviceLocked = 1;
219 switch(device)
220 {
221 case 0x30 :
222 {
223 deviceType = ID_AD5162;
224 channelNo = 2;
225 break;
226 }
227 case 0x31 :
228 {
229 deviceType = ID_AD5172;
230 channelNo = 2;
231 break;
232 }
233 case 0x32 :
234 {
235 deviceType = ID_AD8403;
236 channelNo = 4;
237 break;
238 }
239 }
240 }
241 else
242 {
243 CONSOLE_Print("Please type the number corresponding for \
244 your device!\r\n");
245 }
246 }
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 DoDeviceLock();
261
262 if(AD5172_Init(deviceType) == 0)
263 {
264 CONSOLE_Print("Device OK\r\n");
265 DisplayCmdList();
266 return SUCCESS;
267 }
268 else
269 {
270 CONSOLE_Print("Device Error\r\n");
271 return ERROR;
272 }
273 }
274
275 /***************************************************************************//**
276 * @brief Set resistor value.
277 *
278 * @param - param[0] - The number of the channel, which have to updated
279 * - param[1] - The desired value of the resistor
280 *
281 * @return - NONE
282 *
283 *******************************************************************************/
284 void SetRdac(double* param, char paramNo)
285 {
286 unsigned char channel = 0;
287 unsigned char data8 = 0;
288
289 /* Check if the parameter is valid */
290 if(paramNo >= 2)
291 {
292 paramLimit(&param[0], 0, channelNo-1);
293 paramLimit(&param[1], 0, MAXPOS);
294 channel = (unsigned char)param[0];
295 data8 = (unsigned char)param[1];
296 /* Write to RDAC register */
297 AD5172_Write(channel, 0, data8);
298 rdacBuffer[channel] = data8;
299 /* Send feedback to user */
300 CONSOLE_Print("RDAC[%d]=%d\r\n", channel, data8);
301 }
302 else
303 {
304 /* Display error messages */
305 DisplayError(1);
306 }
307 }
308
309 /***************************************************************************//**
310 * @brief Get resistor value.
311 *
312 * @param - param[0] - The number of the channel, which have to updated
313 *
314 * @return - NONE
315 *
316 *******************************************************************************/
317 void GetRdac(double* param, char paramNo)
318 {
319 unsigned char channel = 0;
320
321 /* Check if the parameter is valid */
322 if(paramNo >= 1)
323 {
324 paramLimit(&param[0], 0, channelNo-1);
325 channel = (unsigned char)param[0];
326 /* Print the value of RDAC to UART */
327 if(deviceType == ID_AD5172)
328 {
329 CONSOLE_Print("RDAC[%d]=%d\r\n", channel, \
330 AD5172_Read(channel, NULL));
331 }
332 else
333 {
334 CONSOLE_Print("RDAC[%d]=%d\r\n", channel, \
335 rdacBuffer[channel]);
336 }
337
338 }
339 else
340 {
341 /* Display error messages */
342 DisplayError(2);
343 }
344 }
345
346 /***************************************************************************//**
347 * @brief Get WBUF_1 voltage value.
348 *
349 * @param - NONE
350 *
351 * @return - NONE
352 *
353 *******************************************************************************/
354 void GetWBuf1(double* param, char paramNo)
355 {
356 float wBuf1 = 0;
357
358 wBuf1 = VDD_VSS * ((float)rdacBuffer[0]/256);
359 CONSOLE_Print("WBUF1 = %1.2fV\n\r", wBuf1);
360 }
361
362 /***************************************************************************//**
363 * @brief Shutdown the RDACs. Shutdown connects wiper to B terminal and open
364 * circuits the A terminal. It does not change the contents of the
365 * wiper register.
366 *
367 * @param - param[0] - The number of the channel (0.. 3 in function of the
368 * actual device)
369 *
370 * @return - NONE
371 *
372 *******************************************************************************/
373 void SetShutdown(double* param, char paramNo)
374 {
375 unsigned char channel = 0;
376 unsigned char command = 0;
377
378 if((deviceType == ID_AD5172) ||
379 (deviceType == ID_AD8403))
380 {
381 /* Check if the parameter is valid */
382 if(paramNo >= 1)
383 {
384 paramLimit(&param[0], 0, channelNo-1);
385 channel = (unsigned char)param[0];
386 /* Toggle the shutdown bit */
387 if(channelState[channel]) // setup the previous state
388 {
389 command |= AD5172_CNTR_SD;
390 }
391 command ^= AD5172_CNTR_SD; // toggle the shutdown bit
392 channelState[channel] ^= 0x1;
393 if(deviceType == ID_AD5172)
394 {
395 AD5172_Write(channel, command, 0);
396 }
397 else //ID_AD8403
398 {
399 if(channelState[channel])
400 {
401 AD5172_SHUTDOWN_LOW;
402 }
403 else
404 {
405 AD5172_SHUTDOWN_HIGH;
406 }
407 }
408 /* Send feedback to user */
409 if((command & AD5172_CNTR_SD) != 0)
410 {
411 CONSOLE_Print("RDACs OFF.\r\n");
412 }
413 else
414 {
415 CONSOLE_Print("RDACs ON.\r\n");
416 }
417 }
418 else
419 {
420 /* Display error messages */
421 DisplayError(4);
422 }
423 }
424 else
425 {
426 CONSOLE_Print("This function is not supported by the current \
427 device.\r\n");
428 }
429 }
430
431 /***************************************************************************//**
432 * @brief Notify the user from the state of an RDAC
433 *
434 * @param - NONE
435 *
436 * @return - NONE
437 *
438 *******************************************************************************/
439 void GetShutdown(double* param, char paramNo)
440 {
441 unsigned char channel = 0;
442
443 if((deviceType == ID_AD5172) ||
444 (deviceType == ID_AD8403))
445 {
446 /* Check if the parameter is valid */
447 if(paramNo >= 1)
448 {
449 paramLimit(&param[0], 0, channelNo-1);
450 channel = (unsigned char)param[0];
451 /* Send feedback to user */
452 if(channelState[channel] != 0)
453 {
454 CONSOLE_Print("Shutdown pin is in active state.\r\n");
455 }
456 else
457 {
458 CONSOLE_Print("Shutdown pin is in inactive state.\r\n");
459 }
460 }
461 else
462 {
463 /* Display error messages */
464 DisplayError(4);
465 }
466 }
467 else
468 {
469 CONSOLE_Print("This function is not supported by the current \
470 device.\r\n");
471 }
472 }