Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/Microchip/PIC32MX320F128H/PmodDA5/Command.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodDA5.
4 * @author DNechita ([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 "AD5781.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51 /*!< List of available commands */
52 const char* cmdList[] ={"help?",
53 "reset!",
54 "coding=",
55 "coding?",
56 "register=",
57 "register?",
58 "voltage=",
59 "voltage?",
60 "output=",
61 "output?",
62 "rbuf=",
63 "rbuf?"};
64 const char* cmdDescription[] = {
65 " - Displays all available commands.",
66 " - Resets the AD5781 device.",
67 " - Selects the coding style. Accepted values: \r\n\
68 \t0 - Offset binary coding. \r\n\
69 \t1 - Two's complement coding.(default)",
70 " - Display the current coding style.",
71 " - Writes to the DAC register. Accepted values: \r\n\
72 \t0 .. 262143 - the value written to the DAC.",
73 " - Displays last written value to the DAC register.",
74 " - Sets the DAC output voltage. Accepted values: \r\n\
75 \t-10 .. +10 - desired output voltage in volts.",
76 " - Displays the output voltage.",
77 " - Selects the DAC output state. Accepted values:\r\n\
78 \t0 - Normal state.\r\n\
79 \t1 - Clamped via 6KOhm to AGND.\r\n\
80 \t2 - Tristate.(default)",
81 " - Displays the DAC output state.",
82 " - Sets/resets the RBUF bit from control register. Accepted values:\r\n\
83 \t0 - RBUF is reset.\r\n\
84 \t1 - RBUF is set.(default)",
85 " - Displays the value of RBUF bit from control register."};
86 const char* cmdExample[] = {
87 "",
88 "",
89 "To select two's complement coding, type: coding=1",
90 "",
91 "To set the register value to 100000, type: register=100000",
92 "",
93 "To set the output voltage to 5V, type: voltage=5",
94 "",
95 "To set the DAC output state to normal, type: output=0",
96 "",
97 "To set the RBUF bit to 0, type: rbuf=0",
98 ""};
99 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
100 const char VREFN = -10;
101 const char VREFP = 10;
102 const unsigned long FULLSCALE = 262144;
103
104 /******************************************************************************/
105 /************************ Variables Definitions *******************************/
106 /******************************************************************************/
107 cmdFunction cmdFunctions[12] = {GetHelp, DoReset, SetCoding, GetCoding,
108 SetRegister, GetRegister, SetVoltage, GetVoltage,
109 SetOutput, GetOutput, SetRbuf, GetRbuf};
110
111 /*!< Variables holding information about the device */
112 unsigned long registerValue = 0; /*!< Last register value written to the device */
113 long codingStyle = 1; /*!< Selected coding style */
114 unsigned char output = 1; /*!< Selected output state */
115 unsigned char rbuf = 1; /*!< RBUF bit */
116
117 /***************************************************************************//**
118 * @brief Displays all available commands.
119 *
120 * @return None.
121 *******************************************************************************/
122 void GetHelp(double* param, char paramNo) /*!< "help?" command */
123 {
124 char displayCmd;
125
126 CONSOLE_Print("Available commands:\r\n");
127 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
128 {
129 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
130 (char*)cmdDescription[displayCmd]);
131 }
132 }
133
134 /***************************************************************************//**
135 * @brief Initializes the device.
136 *
137 * @return - The result of the initialization.
138 * Example: ERROR - the device was not initialized or the device
139 * is not present.
140 * SUCCES - the device was initialized and the device
141 * is present.
142 *******************************************************************************/
143 char DoDeviceInit(void)
144 {
145 if(AD5781_Init() == 0)
146 {
147 CONSOLE_Print("AD5781 OK\r\n");
148 GetHelp(NULL, 0);
149 return SUCCESS;
150 }
151 else
152 {
153 CONSOLE_Print("AD5781 Error\r\n");
154 return ERROR;
155 }
156 }
157
158 /***************************************************************************//**
159 * @brief Resets the device.
160 *
161 * @return None.
162 *******************************************************************************/
163 void DoReset(double* param, char paramNo) /*!< "reset!" command */
164 {
165 /*!< Resets the device to its power-on state. */
166 AD5781_SoftInstruction(AD5781_SOFT_CTRL_RESET);
167 codingStyle = 1;
168 registerValue = 0;
169 output = 1;
170 rbuf = 1;
171 /*!< Send feedback to user */
172 CONSOLE_Print("Device was reset.\r\n");
173 }
174
175 /***************************************************************************//**
176 * @brief Selects the coding style.
177 *
178 * @return None.
179 *******************************************************************************/
180 void SetCoding(double* param, char paramNo) /*!< "coding=" command */
181 {
182 double volt = 0;
183 unsigned long regValue = 0;
184
185 /*!< Check if the parameter is valid */
186 if(paramNo >= 1)
187 {
188 if(param[0] < 0)
189 {
190 param[0] = 0;
191 }
192 if(param[0] > 1)
193 {
194 param[0] = 1;
195 }
196 codingStyle = (char)param[0];
197
198 if(codingStyle == 1)
199 {
200 AD5781_Setup(0);
201 /*!< Write to DAC register */
202 AD5781_SetDacValue(registerValue);
203 regValue = registerValue;
204 regValue = (regValue & 0x20000) ?
205 (regValue & ~0x20000) :
206 (regValue | 0x20000);
207 /*!< Send feedback to user for coding change */
208 CONSOLE_Print("%s%d(two's complement)\r\n",cmdList[2],codingStyle);
209 }
210 else
211 {
212 AD5781_Setup(AD5781_CTRL_BIN2SC);
213 /*!< Write to DAC register */
214 AD5781_SetDacValue(registerValue);
215 regValue = registerValue;
216 /*!< Send feedback to user for coding change */
217 CONSOLE_Print("%s%d(offset binary)\r\n",cmdList[2], codingStyle);
218 }
219 volt = ((float)((VREFP - VREFN) * regValue) / (FULLSCALE - 1)) + VREFN ;
220 /*!< Send the requested value to user for implicit voltage change */
221 CONSOLE_Print("voltage=%.3f [V]\r\n",volt);
222 }
223 else
224 {
225 /*!< Display error messages */
226 CONSOLE_Print("Invalid parameter!\r\n");
227 CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]);
228 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]);
229 }
230 }
231
232 /***************************************************************************//**
233 * @brief Display the current coding style.
234 *
235 * @return None.
236 *******************************************************************************/
237 void GetCoding(double* param, char paramNo) /*!< "coding?" command */
238 {
239 /*!< Send the requested value to user */
240 if (codingStyle == 0)
241 {
242 CONSOLE_Print("coding=%d(offset binary)\r\n", codingStyle);
243 }
244 else
245 {
246 CONSOLE_Print("coding=%d(two's complement)\r\n", codingStyle);
247 }
248 }
249
250 /***************************************************************************//**
251 * @brief Writes to the DAC register.
252 *
253 * @return None.
254 *******************************************************************************/
255 void SetRegister(double* param, char paramNo) /*!< "register=" command */
256 {
257 /*!< Check if the parameter is valid */
258 if(paramNo >= 1)
259 {
260 if(param[0] < 0)
261 {
262 param[0] = 0;
263 }
264 if(param[0] > 262143)
265 {
266 param[0] = 262143;
267 }
268 registerValue = (long)param[0];
269 /*!< Write to DAC register */
270 AD5781_SetDacValue(registerValue);
271 /*!< Send feedback to user */
272 CONSOLE_Print("%s%d\r\n",cmdList[4],registerValue);
273 }
274 else
275 {
276 /*!< Display error messages */
277 CONSOLE_Print("Invalid parameter!\r\n");
278 CONSOLE_Print("%s%s\r\n", (char*)cmdList[4], (char*)cmdDescription[4]);
279 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[4]);
280 }
281 }
282
283 /***************************************************************************//**
284 * @brief Displays last written value to the DAC register.
285 *
286 * @return None.
287 *******************************************************************************/
288 void GetRegister(double* param, char paramNo) /*!< "register?" command */
289 {
290 /*!< Send the requested value to user */
291 CONSOLE_Print("%s%d\r\n",cmdList[4],registerValue);
292 }
293
294 /***************************************************************************//**
295 * @brief Sets the DAC output voltage.
296 *
297 * @return None.
298 *******************************************************************************/
299 void SetVoltage(double* param, char paramNo) /*!< "voltage=" command */
300 {
301 double volt = 0;
302
303 /*!< Check if the parameter is valid */
304 if(paramNo >= 1)
305 {
306 if(param[0] < -10)
307 {
308 param[0] = -10;
309 }
310 if(param[0] > 10)
311 {
312 param[0] = 10;
313 }
314 volt = (float)param[0];
315
316 registerValue = (unsigned long)((volt - VREFN) * (FULLSCALE - 1)
317 / (VREFP - VREFN) + 0.5);
318 /*!< Computes the actual voltage that will be set. */
319 volt = ((float)((VREFP - VREFN) * registerValue) / (FULLSCALE - 1))
320 + VREFN ;
321 if (codingStyle == 1)
322 {
323 if(volt < 0)
324 {
325 registerValue += 0x20000;
326 }
327 else
328 {
329 registerValue -= 0x20000;
330 }
331 }
332 /*!< Write to DAC register */
333 AD5781_SetDacValue(registerValue);
334 /*!< Send feedback to user */
335 CONSOLE_Print("%s%.3f [V]\r\n",cmdList[6], volt);
336 }
337 else
338 {
339 /*!< Display error messages */
340 CONSOLE_Print("Invalid parameter!\r\n");
341 CONSOLE_Print("%s%s\r\n", (char*)cmdList[6], (char*)cmdDescription[6]);
342 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[6]);
343 }
344 }
345
346 /***************************************************************************//**
347 * @brief Displays the output voltage.
348 *
349 * @return None.
350 *******************************************************************************/
351 void GetVoltage(double* param, char paramNo) /*!< "voltage?" command */
352 {
353 double volt = 0;
354 unsigned long regValue = 0;
355
356 /*!< Get the output voltage using data from regValue */
357 regValue = registerValue;
358 if (codingStyle == 1)
359 {
360 regValue = (regValue & 0x20000) ?
361 (regValue & ~0x20000) :
362 (regValue | 0x20000);
363 }
364 volt = ((float)((VREFP - VREFN) * regValue) / (FULLSCALE - 1)) + VREFN ;
365 /*!< Send the requested value to user */
366 CONSOLE_Print("voltage=%.3f [V]\r\n",volt);
367 }
368
369 /***************************************************************************//**
370 * @brief Selects the DAC output state.
371 *
372 * @return None.
373 *******************************************************************************/
374 void SetOutput(double* param, char paramNo) /*!< "output=" command */
375 {
376 /*!< Check if the parameter is valid */
377 if(paramNo >= 1)
378 {
379 if(param[0] < 0)
380 {
381 param[0] = 0;
382 }
383 if(param[0] > 2)
384 {
385 param[0] = 2;
386 }
387 output = (unsigned char)param[0];
388 /*!< Set the DAC output to the selected operating mode. */
389 AD5781_DacOuputState(output);
390 /*!< Send feedback to user */
391 CONSOLE_Print("%s%d\r\n",cmdList[8],output);
392 }
393 else
394 {
395 /*!< Display error messages */
396 CONSOLE_Print("Invalid parameter!\r\n");
397 CONSOLE_Print("%s%s\r\n", (char*)cmdList[8], (char*)cmdDescription[8]);
398 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[8]);
399 }
400 }
401
402 /***************************************************************************//**
403 * @brief Displays the DAC output state.
404 *
405 * @return None.
406 *******************************************************************************/
407 void GetOutput(double* param, char paramNo) /*!< "output?" command */
408 {
409 /*!< Send the requested value to user */
410 CONSOLE_Print("%s%d\r\n",cmdList[8],output);
411
412 }
413
414 /***************************************************************************//**
415 * @brief Sets/resets the RBUF bit from control register.
416 *
417 * @return None.
418 *******************************************************************************/
419 void SetRbuf(double* param, char paramNo) /*!< "rbuf=" command */
420 {
421 long regValue = 0;
422
423 /*!< Check if the parameter is valid */
424 if(paramNo >= 1)
425 {
426 if(param[0] < 0)
427 {
428 param[0] = 0;
429 }
430 if(param[0] > 1)
431 {
432 param[0] = 1;
433 }
434 rbuf = (unsigned char)param[0];
435
436 regValue = AD5781_GetRegisterValue(AD5781_REG_CTRL);
437 regValue &= ~AD5781_CTRL_RBUF;
438 regValue |= rbuf * AD5781_CTRL_RBUF;
439 regValue = AD5781_SetRegisterValue(AD5781_REG_CTRL,regValue);
440
441 /*!< Send feedback to user */
442 CONSOLE_Print("%s%d\r\n",(char*)cmdList[10],rbuf);
443 }
444 else
445 {
446 /*!< Display error messages */
447 CONSOLE_Print("Invalid parameter!\r\n");
448 CONSOLE_Print("%s%s\r\n", (char*)cmdList[10], (char*)cmdDescription[10]);
449 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[10]);
450 }
451 }
452
453 /***************************************************************************//**
454 * @brief Displays the value of RBUF bit from control register.
455 *
456 * @return None.
457 *******************************************************************************/
458 void GetRbuf(double* param, char paramNo) /*!< "rbuf?" command */
459 {
460 /*!< Send the requested value to user */
461 CONSOLE_Print("%s%d\r\n",cmdList[10],rbuf);
462
463 }