Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Commanad.c
3 * @brief Implementation of the commands given by user through UART for AD5110.
4 * @author Lucian Sin ([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 modification,
12 * 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 IMPLIED
30 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT, MERCHANTABILITY
31 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
32 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
33 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
34 * INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
35 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
36 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
37 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
38 * 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 "ad5110.h"
48
49 /******************************************************************************/
50 /************************ Constants Definitions *******************************/
51 /******************************************************************************/
52
53 const struct cmd_info cmdList[] = {
54 [0] = {
55 .name = "help?",
56 .description = "Displays all available commands.",
57 .acceptedValue = "",
58 .example = "",
59 },
60 [1] = {
61 .name = "reset!",
62 .description = "Makes a software reset of the device.",
63 .acceptedValue = "",
64 .example = "",
65 },
66 [2] = {
67 .name = "rdac=",
68 .description = "Writes to the RDAC register..",
69 .acceptedValue = "Accepted values:\r\n\
70 \t0 .. 127 - the value written to RDAC.",
71 .example = "To set the rdac value to 100, type: rdac=100",
72 },
73 [3] = {
74 .name = "rdac?",
75 .description = "Displays the last written value in RDAC register.",
76 .acceptedValue = "",
77 .example = "",
78 },
79 [4] = {
80 .name = "rdacToEeprom!",
81 .description = "Writes the content of RDAC register to EEPROM.",
82 .acceptedValue = "",
83 .example = "",
84 },
85 [5] = {
86 .name = "wiper?",
87 .description = "Displays the wiper resistance from EEPROM.",
88 .acceptedValue = "",
89 .example = "",
90 },
91 [6] = {
92 .name = "tolerance?",
93 .description = "Displays the resistance tolerance from EEPROM.",
94 .acceptedValue = "",
95 .example = "",
96 },
97 [7] = {
98 .name = "power=",
99 .description = "Turns on/off the device.",
100 .acceptedValue = "Accepted values:\r\n\
101 \t0 - turns off the device.\r\n\
102 \t1 - turns on the device.",
103 .example = "To turn off the device, type power=0",
104 },
105 [8] ={
106 .name = "power?",
107 .description = "Displays the power status of the device.",
108 .acceptedValue = "",
109 .example = "",
110 },
111 };
112
113
114 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
115
116 /******************************************************************************/
117 /************************ Variables Definitions *******************************/
118 /******************************************************************************/
119 cmdFunction cmdFunctions[9] = {GetHelp, DoReset, SetRdac, GetRdac,
120 DoRdacToEeprom, GetWiper, GetTolerance, SetPower,
121 GetPower};
122 /* Variables holding information about the device */
123 unsigned char power = 1; // Power status of the device
124
125 /***************************************************************************//**
126 * @brief Displays all available commands.
127 *
128 * @return None.
129 *******************************************************************************/
130 void GetHelp(double* param, char paramNo) // "help?" command
131 {
132 unsigned char displayCmd;
133
134 CONSOLE_Print("Available commands:\r\n");
135 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
136 {
137 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
138 (char*)cmdList[displayCmd].description,
139 (char*)cmdList[displayCmd].acceptedValue);
140 }
141 }
142
143 /***************************************************************************//**
144 * @brief Internal function for displaying all the command with its description.
145 *
146 * @return None.
147 *******************************************************************************/
148 void DisplayCmdList()
149 {
150 unsigned char displayCmd;
151
152 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
153 {
154 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
155 (char*)cmdList[displayCmd].description);
156 }
157 }
158
159 /***************************************************************************//**
160 * @brief Displays error message.
161 *
162 * @return None.
163 *******************************************************************************/
164 void DisplayError(unsigned char funcNo)
165 {
166 /* Display error messages */
167 CONSOLE_Print("Invalid parameter!\r\n");
168 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
169 (char*)cmdList[funcNo].description, \
170 (char*)cmdList[funcNo].acceptedValue);
171 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
172 }
173
174 /***************************************************************************//**
175 * @brief Initializes the device.
176 *
177 * @return - The result of the initialization.
178 * Example: ERROR - the device was not initialized or the device
179 * is not present.
180 * SUCCES - the device was initialized and the device
181 * is present.
182 *******************************************************************************/
183 char DoDeviceInit(void)
184 {
185 if(ad5110_Init(ad5110_I2C_ADDR) == 0)
186 {
187 CONSOLE_Print("AD5110 OK\r\n");
188 DisplayCmdList();
189 return SUCCESS;
190 }
191 else
192 {
193 CONSOLE_Print("AD5110 Error\r\n");
194 return ERROR;
195 }
196 }
197
198 /***************************************************************************//**
199 * @brief Makes a software reset of the device.
200 *
201 * @return None.
202 *******************************************************************************/
203 void DoReset(double* param, char paramNo) // "reset!" command
204 {
205 ad5110_Reset();
206 /* Send feedback to user */
207 CONSOLE_Print("Device was reset.\r\n");
208 }
209
210 /***************************************************************************//**
211 * @brief Writes to the RDAC register.
212 *
213 * @param param[0] = value to be written to RDAC.
214 *
215 * @return None.
216 *******************************************************************************/
217 void SetRdac(double* param, char paramNo) // "rdac=" command
218 {
219 unsigned char rdac;
220
221 /* Check if the parameter is valid */
222 if(paramNo >= 1)
223 {
224 if(param[0] < 0)
225 {
226 param[0] = 0;
227 }
228 if(param[0] > 127)
229 {
230 param[0] = 127;
231 }
232
233 rdac = (unsigned char)param[0];
234 /* Write to RDAC register */
235 ad5110_WriteRdac(rdac);
236 /* Send feedback to user */
237 CONSOLE_Print("%s%d\r\n",(char*)cmdList[2].name,rdac);
238 }
239 else
240 {
241 /* Display error messages */
242 DisplayError(2);
243
244 }
245 }
246
247 /***************************************************************************//**
248 * @brief Displays the last written value in RDAC register.
249 *
250 * @return None.
251 *******************************************************************************/
252 void GetRdac(double* param, char paramNo) // "rdac?" command
253 {
254 unsigned char rdac;
255
256 rdac = ad5110_ReadRdac();
257 /* Send the requested value to user */
258 CONSOLE_Print("%s%d\r\n",(char*)cmdList[2].name,rdac);
259 }
260
261 /***************************************************************************//**
262 * @brief Writes the content of RDAC register to EEPROM.
263 *
264 * @return None.
265 *******************************************************************************/
266 void DoRdacToEeprom(double* param, char paramNo) // "rdacToEeprom!" command
267 {
268 ad5110_WriteRdacEeprom();
269 /* Send feedback to user */
270 CONSOLE_Print("RDAC register was written to EEPROM.\r\n");
271 }
272
273 /***************************************************************************//**
274 * @brief Displays the wiper resistance from EEPROM.
275 *
276 * @return None.
277 *******************************************************************************/
278 void GetWiper(double* param, char paramNo) // "wiper?" command
279 {
280 unsigned char wiper;
281
282 wiper = ad5110_ReadWiper();
283 /* Send the requested value to user */
284 CONSOLE_Print("wiper=%d\r\n",wiper);
285 }
286
287 /***************************************************************************//**
288 * @brief Displays the resistance tolerance from EEPROM.
289 *
290 * @return None.
291 *******************************************************************************/
292 void GetTolerance(double* param, char paramNo) // "tolerance?" command
293 {
294 unsigned char tolerance;
295
296 tolerance = ad5110_ReadResTolerance();
297 /* Send the requested value to user */
298 CONSOLE_Print("tolerance=%d\r\n",tolerance);
299 }
300
301 /***************************************************************************//**
302 * @brief Turns on/off the device.
303 *
304 * @param param[0] - power status of the device.
305 * Example: 0 - device turned off;
306 * 1 - device turned on.
307 *
308 * @return None.
309 *******************************************************************************/
310 void SetPower(double* param, char paramNo) // "power=" command
311 {
312 /* Check if the parameter is valid */
313 if(paramNo >= 1)
314 {
315 if(param[0] < 0)
316 {
317 param[0] = 0;
318 }
319 if(param[0] > 1)
320 {
321 param[0] = 1;
322 }
323 power = (unsigned char)param[0];
324 /* Turns on/off the device. */
325 ad5110_ShutDown(!power);
326 /* Send feedback to user */
327 CONSOLE_Print("%s%d\r\n",(char*)cmdList[7].name,power);
328 }
329 else
330 {
331 /* Display error messages */
332 DisplayError(7);
333 }
334 }
335
336 /***************************************************************************//**
337 * @brief Displays the power status of the device.
338 *
339 * @return None.
340 *******************************************************************************/
341 void GetPower(double* param, char paramNo) // "power?" command
342 {
343 /* Send feedback to user */
344 CONSOLE_Print("%s%d\r\n",(char*)cmdList[7].name,power);
345 }