Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodDA3.
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 "AD5541A.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51
52 /*!< List of available commands */
53 const char* cmdList[] = {"help?",
54 "voltage=",
55 "voltage?",
56 "register=",
57 "register?"};
58 const char* cmdDescription[] = {
59 " - Displays all available commands.",
60 " - Sets the DAC output voltage. Accepted values: \r\n\
61 \t0 .. 2500 - desired output voltage in milivolts.",
62 " - Displays last written voltage value to the DAC.",
63 " - Writes to the DAC register. Accepted values: \r\n\
64 \t0 .. 65535 - the value written to the DAC.",
65 " - Displays last written value in the DAC register."};
66 const char* cmdExample[] = {
67 "",
68 "To set the voltage to 1250.125mV, type: voltage=1250.125",
69 "",
70 "To set the register value to 10000, type: register=10000",
71 ""};
72 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
73 const float VREF = 2.5; /*!< Vref = 2.5V */
74
75 /******************************************************************************/
76 /************************ Variables Definitions *******************************/
77 /******************************************************************************/
78 cmdFunction cmdFunctions[5] = {GetHelp, SetVoltage, GetVoltage, SetRegister,
79 GetRegister};
80
81 /*!< Variables holding information about the device */
82 unsigned short registerValue = 0; /*!< Last register value written to the device */
83
84 /***************************************************************************//**
85 * @brief Displays all available commands.
86 *
87 * @return None.
88 *******************************************************************************/
89 void GetHelp(double* param, char paramNo) /*!< "help?" command */
90 {
91 char displayCmd;
92
93 CONSOLE_Print("Available commands:\r\n");
94 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
95 {
96 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
97 (char*)cmdDescription[displayCmd]);
98 }
99 }
100
101 /***************************************************************************//**
102 * @brief Initializes the device.
103 *
104 * @return - The result of the initialization.
105 * Example: ERROR - the device was not initialized or the device
106 * is not present.
107 * SUCCES - the device was initialized and the device
108 * is present.
109 *******************************************************************************/
110 char DoDeviceInit(void)
111 {
112 if(AD5541A_Init() == 0)
113 {
114 CONSOLE_Print("AD5541A OK\r\n");
115 GetHelp(NULL, 0);
116 return SUCCESS;
117 }
118 else
119 {
120 CONSOLE_Print("AD5541A Error\r\n");
121 return ERROR;
122 }
123 }
124
125 /***************************************************************************//**
126 * @brief Sets the DAC output voltage.
127 *
128 * @return None.
129 *******************************************************************************/
130 void SetVoltage(double* param, char paramNo) /*!< "voltage=" command */
131 {
132 double outVoltage = 0;
133 double tempFloat = 0;
134
135 /*!< Check if the parameter is valid */
136 if(paramNo >= 1)
137 {
138 if(param[0] < 0)
139 {
140 param[0] = 0;
141 }
142 if(param[0] > 2499.999)
143 {
144 param[0] = 2499.999;
145 }
146
147 outVoltage = param[0] / 1000;
148 /*!< Find the binary value corresponding to the output voltage*/
149 tempFloat = (outVoltage / (double) VREF) * 65536;
150 registerValue = (short) tempFloat;
151 /*!< Write to DAC register */
152 AD5541A_SetRegisterValue(registerValue);
153 outVoltage = 1000 * VREF * ((double)registerValue / 65536);
154 /*!< Send feedback to user */
155 CONSOLE_Print("%s%.3f [mV]\r\n",(char*)cmdList[1],outVoltage);
156 }
157 else
158 {
159 /*!< Display error messages */
160 CONSOLE_Print("Invalid parameter!\r\n");
161 CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]);
162 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]);
163 }
164 }
165
166 /***************************************************************************//**
167 * @brief Displays last written voltage value to the DAC.
168 *
169 * @return None.
170 *******************************************************************************/
171 void GetVoltage(double* param, char paramNo) /*!< "voltage?" command */
172 {
173 double outVoltage = 0;
174
175 /*!< Get the output voltage using data from registerValue */
176 outVoltage = 1000 * VREF * ((double)registerValue / 65536);
177 /*!< Send feedback to user */
178 CONSOLE_Print("%s%.3f [mV]\r\n",(char*)cmdList[1],outVoltage);
179 }
180
181 /***************************************************************************//**
182 * @brief Writes to the DAC register.
183 *
184 * @return None.
185 *******************************************************************************/
186 void SetRegister(double* param, char paramNo) /*!< "register=" command */
187 {
188 double outVoltage = 0;
189 double tempFloat = 0;
190
191 /*!< Check if the parameter is valid */
192 if(paramNo >= 1)
193 {
194 if(param[0] < 0)
195 {
196 param[0] = 0;
197 }
198 if(param[0] > 65535)
199 {
200 param[0] = 65535;
201 }
202
203 registerValue = (unsigned short)param[0];
204 /*!< Write to DAC register */
205 AD5541A_SetRegisterValue(registerValue);
206 /*!< Send the requested value to user */
207 CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],registerValue);
208 }
209 else
210 {
211 /*!< Display error messages */
212 CONSOLE_Print("Invalid parameter!\r\n");
213 CONSOLE_Print("%s%s\r\n", (char*)cmdList[3], (char*)cmdDescription[3]);
214 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[3]);
215 }
216 }
217
218 /***************************************************************************//**
219 * @brief Displays last written value in the DAC register.
220 *
221 * @return None.
222 *******************************************************************************/
223 void GetRegister(double* param, char paramNo) /*!< "register?" command */
224 {
225 /*!< Send the requested value to user */
226 CONSOLE_Print("%s%d\r\n",(char*)cmdList[3],registerValue);
227 }
228