Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodDA1.
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 "AD7303.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51
52 /*!< List of available commands */
53 const char* cmdList[] = {"help?",
54 "power=",
55 "power?",
56 "voltage=",
57 "voltage?",
58 "register=",
59 "register?"};
60 const char* cmdDescription[] = {
61 " - Displays all available commands.",
62 " - Powers off/on the selected channel. Accepted values: \r\n\
63 \tchannel:\r\n\
64 \t0 - Select channel A1. \r\n\
65 \t1 - Select channel B1. \r\n\
66 \tpower status: \r\n\
67 \t0 - Power off. \r\n\
68 \t1 - Power on.",
69 " - Displays the power status of the selected channel. Accepted values: \r\n\
70 \t0 - Select channel A1. \r\n\
71 \t1 - Select channel B1.",
72 " - Sets the output voltage of the selected channel. Accepted values: \r\n\
73 \tchannel:\r\n\
74 \t0 - Select channel A1. \r\n\
75 \t1 - Select channel B1. \r\n\
76 \toutput voltage: \r\n\
77 \t0 .. 3300 - Desired output voltage in milivolts",
78 " - Displays last written voltage value to the DAC for selected channel. \
79 Accepted values: \r\n\
80 \t0 - Select channel A1. \r\n\
81 \t1 - Select channel B1.",
82 " - Writes to the DAC register of the selected channel. Accepted values: \r\n\
83 \tchannel:\r\n\
84 \t0 - Select channel A1. \r\n\
85 \t1 - Select channel B1. \r\n\
86 \tregister value: \r\n\
87 \t0 .. 255 - Value written to the DAC.",
88 " - Displays last written value in the DAC register for selected channel. \
89 Accepted values: \r\n\
90 \t0 - Select channel A1. \r\n\
91 \t1 - Select channel B1."
92 };
93 const char* cmdExample[] = {
94 "",
95 "To power off the channel B1, type: power=1 0",
96 "To display the power status of the channel B1, type: power?1",
97 "To set the voltage for channel A1 to 2575mV, type: voltage=0 2575",
98 "To display the last written voltage value for channel B1, type: voltage?1",
99 "To set the register value for channel A1 to 150, type: register=0 150",
100 "To display the last written value in register for channel A1, type: register?0"};
101 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
102 const float VREF = 1.65; /*!< Vref = VDD/2 */
103
104 /******************************************************************************/
105 /************************ Variables Definitions *******************************/
106 /******************************************************************************/
107 cmdFunction cmdFunctions[7] = {GetHelp, SetPower, GetPower, SetVoltage,
108 GetVoltage, SetRegister, GetRegister};
109
110 /*!< Variables holding information about the device */
111 unsigned short registerValue = 0; /*!< Last register value written to the device */
112 unsigned char powerMode = 1; /*!< Last powerMode value written to the device */
113 unsigned char currentChannel = 0; /*!< Last channel selected */
114 unsigned char controlBits = 0; /*!< Upper byte of the Shift Register */
115 unsigned char dacRegArray[2] = {0, 0};
116 unsigned char powerArray[2] = {1, 1};
117
118 /***************************************************************************//**
119 * @brief Displays all available commands.
120 *
121 * @return None.
122 *******************************************************************************/
123 void GetHelp(double* param, char paramNo) /*!< "help?" command */
124 {
125 char displayCmd;
126
127 CONSOLE_Print("Available commands:\r\n");
128 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
129 {
130 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
131 (char*)cmdDescription[displayCmd]);
132 }
133 }
134
135 /***************************************************************************//**
136 * @brief Initializes the device.
137 *
138 * @return - The result of the initialization.
139 * Example: ERROR - the device was not initialized or the device
140 * is not present.
141 * SUCCES - the device was initialized and the device
142 * is present.
143 *******************************************************************************/
144 char DoDeviceInit(void)
145 {
146 if(AD7303_Init() == 0)
147 {
148 CONSOLE_Print("AD7303 OK\r\n");
149 GetHelp(NULL, 0);
150 return SUCCESS;
151 }
152 else
153 {
154 CONSOLE_Print("AD7303 Error\r\n");
155 return ERROR;
156 }
157 }
158
159 /***************************************************************************//**
160 * @brief Powers off/on the current channel.
161 *
162 * @return None.
163 *******************************************************************************/
164 void SetPower(double* param, char paramNo) /*!< "power=" command */
165 {
166 double outVoltage = 0;
167 double tempFloat = 0;
168
169 /*!< Check if the parameters are valid */
170 if(paramNo >= 2)
171 {
172 if(param[0] < 0)
173 {
174 param[0] = 0;
175 }
176 if(param[0] > 1)
177 {
178 param[0] = 1;
179 }
180 currentChannel = (char)param[0];
181
182 if(param[1] < 0)
183 {
184 param[1] = 0;
185 }
186 if(param[1] > 1)
187 {
188 param[1] = 1;
189 }
190 powerMode = (unsigned char)param[1];
191
192 powerArray[currentChannel] = powerMode;
193 /*!< Set the power mode of the selected channel. */
194 controlBits &= ~((AD7303_PDA * !currentChannel) |
195 (AD7303_PDB * currentChannel));
196 if(powerMode == 0)
197 {
198 controlBits |= (AD7303_PDA * !currentChannel) |
199 (AD7303_PDB * currentChannel);
200 }
201 AD7303_Write(controlBits, dacRegArray[currentChannel]);
202
203 /*!< Send feedback to user */
204 CONSOLE_Print("%s%d for channel ",(char*)cmdList[1],powerMode);
205 if(currentChannel == 0)
206 {
207 CONSOLE_Print("A1\r\n");
208 }
209 else
210 {
211 CONSOLE_Print("B1\r\n");
212 }
213 }
214 else
215 {
216 /*!< Display error messages */
217 CONSOLE_Print("Invalid parameter!\r\n");
218 CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]);
219 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]);
220 }
221 }
222
223 /***************************************************************************//**
224 * @brief Displays the power status for selected channel.
225 *
226 * @return None.
227 *******************************************************************************/
228 void GetPower(double* param, char paramNo) /*!< "power?" command */
229 {
230 /*!< Check if the parameter is valid */
231 if(paramNo >= 1)
232 {
233 if (param[0] < 0)
234 {
235 param[0] = 0;
236 }
237 if (param[0] > 1)
238 {
239 param[0] = 1;
240 }
241 currentChannel = (char)param[0];
242 /*!< Send feedback to user */
243 CONSOLE_Print("%s%d for channel ",(char*)cmdList[1],
244 powerArray[currentChannel]);
245 if(currentChannel == 0)
246 {
247 CONSOLE_Print("A1\r\n");
248 }
249 else
250 {
251 CONSOLE_Print("B1\r\n");
252 }
253 }
254 else
255 {
256 /*!< Display error messages */
257 CONSOLE_Print("Invalid parameter!\r\n");
258 CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]);
259 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]);
260 }
261 }
262
263 /***************************************************************************//**
264 * @brief Sets the output voltage of the selected channel.
265 *
266 * @return None.
267 *******************************************************************************/
268 void SetVoltage(double* param, char paramNo) /*!< "voltage=" command */
269 {
270 double outVoltage = 0;
271 double tempFloat = 0;
272
273 /*!< Check if the parameters are valid */
274 if(paramNo >= 2)
275 {
276 if (param[0] < 0)
277 {
278 param[0] = 0;
279 }
280 if (param[0] > 1)
281 {
282 param[0] = 1;
283 }
284 currentChannel = (char)param[0];
285
286 if(param[1] < 0)
287 {
288 param[1] = 0;
289 }
290 if(param[1] > 3299.999)
291 {
292 param[1] = 3299.999;
293 }
294 outVoltage = param[1] / 1000;
295
296 /*!< Find the binary value corresponding to the output voltage*/
297 tempFloat = (outVoltage / (double)(2 * VREF)) * 256;
298 registerValue = (unsigned char) tempFloat;
299 dacRegArray[currentChannel] = registerValue;
300 /*!< Select a channel and write to its register */
301 AD7303_Write(controlBits, registerValue);
302 outVoltage = 2000 * VREF * ((double)registerValue / 256);
303 /*!< Send feedback to user */
304 if (powerArray[currentChannel] == 0)
305 {
306 CONSOLE_Print("The selected channel is disabled. Please use 'power=%d 1'\
307 command in order to enable it.\r\n",currentChannel);
308 }
309 else
310 {
311 CONSOLE_Print("%s%d [mV] for channel ",(char*)cmdList[3],(short)outVoltage);
312 if(currentChannel == 0)
313 {
314 CONSOLE_Print("A1\r\n");
315 }
316 else
317 {
318 CONSOLE_Print("B1\r\n");
319 }
320 }
321 }
322 else
323 {
324 /*!< Display error messages */
325 CONSOLE_Print("Invalid parameter!\r\n");
326 CONSOLE_Print("%s%s\r\n", (char*)cmdList[3], (char*)cmdDescription[3]);
327 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[3]);
328 }
329 }
330
331 /***************************************************************************//**
332 * @brief Displays last written voltage value to the DAC for selected channel.
333 *
334 * @return None.
335 *******************************************************************************/
336 void GetVoltage(double* param, char paramNo) /*!< "voltage?" command */
337 {
338 double outVoltage = 0;
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
352 currentChannel = (char)param[0];
353 registerValue = dacRegArray[currentChannel];
354 /*!< Get the output voltage using data from registerValue */
355 outVoltage = 2000 * VREF * ((double)registerValue / 256);
356 /*!< Send the requested value to user */
357 if (powerArray[currentChannel] == 0)
358 {
359 CONSOLE_Print("The selected channel is disabled. Please use 'power=%d 1'\
360 command in order to enable it.\r\n",currentChannel);
361 }
362 else
363 {
364 CONSOLE_Print("%s%d [mV] for channel ",(char*)cmdList[3],(short)outVoltage);
365 if(currentChannel == 0)
366 {
367 CONSOLE_Print("A1\r\n");
368 }
369 else
370 {
371 CONSOLE_Print("B1\r\n");
372 }
373 }
374 }
375 else
376 {
377 /*!< Display error messages */
378 CONSOLE_Print("Invalid parameter!\r\n");
379 CONSOLE_Print("%s%s\r\n", (char*)cmdList[4], (char*)cmdDescription[4]);
380 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[4]);
381 }
382 }
383
384 /***************************************************************************//**
385 * @brief Writes to the DAC register of the selected channel.
386 *
387 * @return None.
388 *******************************************************************************/
389 void SetRegister(double* param, char paramNo) /*!< "register=" command */
390 {
391 double outVoltage = 0;
392
393 /*!< Check if the parameters are valid */
394 if(paramNo >= 2)
395 {
396 if(param[0] < 0)
397 {
398 param[0] = 0;
399 }
400 if(param[0] > 1)
401 {
402 param[0] = 1;
403 }
404 currentChannel = (char)param[0];
405 if(param[1] < 0)
406 {
407 param[1] = 0;
408 }
409 if(param[1] > 255)
410 {
411 param[1] = 255;
412 }
413 registerValue = (unsigned short)param[1];
414 dacRegArray[currentChannel] = registerValue;
415 /* Select a channel and write to its register */
416 AD7303_Write(controlBits, registerValue);
417 /*!< Send the requested value to user */
418 if (powerArray[currentChannel] == 0)
419 {
420 CONSOLE_Print("The selected channel is disabled. Please use 'power=%d 1'\
421 command in order to enable it.\r\n",currentChannel);
422 }
423 else
424 {
425 CONSOLE_Print("%s%d for channel ",(char*)cmdList[5],registerValue);
426 if(currentChannel == 0)
427 {
428 CONSOLE_Print("A1\r\n");
429 }
430 else
431 {
432 CONSOLE_Print("B1\r\n");
433 }
434 }
435 }
436 else
437 {
438 /*!< Display error messages */
439 CONSOLE_Print("Invalid parameter!\r\n");
440 CONSOLE_Print("%s%s\r\n", (char*)cmdList[5], (char*)cmdDescription[5]);
441 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[5]);
442 }
443 }
444
445 /***************************************************************************//**
446 * @brief Displays last written value in the DAC register for selected channel.
447 *
448 * @return None.
449 *******************************************************************************/
450 void GetRegister(double* param, char paramNo) /*!< "register?" command */
451 {
452
453 /*!< Check if the parameter is valid */
454 if(paramNo >= 1)
455 {
456 if (param[0] < 0)
457 {
458 param[0] = 0;
459 }
460 if (param[0] > 1)
461 {
462 param[0] = 1;
463 }
464
465 currentChannel = (char)param[0];
466 registerValue = dacRegArray[currentChannel];
467 /*!< Send the requested value to user */
468 if (powerArray[currentChannel] == 0)
469 {
470 CONSOLE_Print("The selected channel is disabled. Please use 'power=%d 1'\
471 command in order to enable it.\r\n",currentChannel);
472 }
473 else
474 {
475 CONSOLE_Print("%s%d for channel ",(char*)cmdList[5],registerValue);
476 if(currentChannel == 0)
477 {
478 CONSOLE_Print("A1\r\n");
479 }
480 else
481 {
482 CONSOLE_Print("B1\r\n");
483 }
484 }
485 }
486 else
487 {
488 /*!< Display error messages */
489 CONSOLE_Print("Invalid parameter!\r\n");
490 CONSOLE_Print("%s%s\r\n", (char*)cmdList[6], (char*)cmdDescription[6]);
491 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[6]);
492 }
493 }