Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for AD5111.
4 * @author Lucian Sin ([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 "Console.h"
46 #include "AD5111.h"
47 #include "TIME.h"
48
49 /******************************************************************************/
50 /************************ Constants Definitions *******************************/
51 /******************************************************************************/
52 #define MAX_POS 128
53
54 const struct cmd_info cmdList[] = {
55 [0] = {
56 .name = "help?",
57 .description = "Displays all available commands.",
58 .acceptedValue = "",
59 .example = "",
60 },
61 [1] = {
62 .name = "increment=",
63 .description = "Increases the resistor value with a number of steps.",
64 .acceptedValue = "Accepted values:\r\n\
65 \t0 .. 128 - number of steps to increment RDAC value.",
66 .example = "To increment RDAC value by 10 steps, type: increment=10",
67 },
68 [2] = {
69 .name = "decrement=",
70 .description = "Decreases the resistor value with a number of steps.",
71 .acceptedValue = "Accepted values:\r\n\
72 \t0 .. 128 - number of steps to decrement RDAC value.",
73 .example = "To decrement RDAC value by 25 steps, type: decrement=25",
74 },
75 [3] = {
76 .name = "store!",
77 .description = "Device memorizes the current RDAC value in EEPROM.",
78 .acceptedValue = "",
79 .example = "",
80 },
81 [4] = {
82 .name = "shutdown!",
83 .description = " Device enters in shutdown mode.",
84 .acceptedValue = "",
85 .example = "",
86 },
87 };
88
89 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
90
91 /******************************************************************************/
92 /************************ Variables Definitions *******************************/
93 /******************************************************************************/
94 cmdFunction cmdFunctions[5] = {GetHelp, DoIncrement, DoDecrement, DoStore,
95 DoShutdown};
96
97 /***************************************************************************//**
98 * @brief Displays error message.
99 *
100 * @return None.
101 *******************************************************************************/
102 void DisplayError(unsigned char funcNo)
103 {
104 /* Display error messages */
105 CONSOLE_Print("Invalid parameter!\r\n");
106 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
107 (char*)cmdList[funcNo].description, \
108 (char*)cmdList[funcNo].acceptedValue);
109 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
110 }
111
112 /***************************************************************************//**
113 * @brief Internal function for displaying all the command with its description.
114 *
115 * @return None.
116 *******************************************************************************/
117 void DisplayCmdList()
118 {
119 unsigned char displayCmd;
120
121 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
122 {
123 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
124 (char*)cmdList[displayCmd].description);
125 }
126 }
127
128 /***************************************************************************//**
129 * @brief Verify if the given parameter is between his valid limits.
130 *
131 * @param None.
132 *
133 * @return None.
134 *******************************************************************************/
135 void paramLimit(double* param,
136 unsigned short lowerLimit,
137 unsigned short upperLimit)
138 {
139 if(*param < lowerLimit)
140 {
141 *param = lowerLimit;
142 }
143 else
144 {
145 if(*param > upperLimit)
146 {
147 *param = upperLimit;
148 }
149 }
150 }
151
152 /**************************************************************************//***
153 * @brief Displays all available commands.
154 *
155 * @return None.
156 *******************************************************************************/
157 void GetHelp(double* param, char paramNo) // "help?" command
158 {
159 unsigned char displayCmd;
160
161 CONSOLE_Print("Available commands:\r\n");
162 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
163 {
164 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
165 (char*)cmdList[displayCmd].description,
166 (char*)cmdList[displayCmd].acceptedValue);
167 }
168 }
169
170 /***************************************************************************//**
171 * @brief Initializes the device.
172 *
173 * @return - The result of the initialization.
174 * Example: ERROR - the device was not initialized or the device
175 * is not present.
176 * SUCCES - the device was initialized and the device
177 * is present.
178 *******************************************************************************/
179 char DoDeviceInit(void)
180 {
181 if(AD5111_Init() == 0)
182 {
183 CONSOLE_Print("AD5111 OK\r\n");
184 DisplayCmdList();
185 return SUCCESS;
186 }
187 else
188 {
189 CONSOLE_Print("AD5111 Error\r\n");
190 return ERROR;
191 }
192 }
193
194 /**************************************************************************//***
195 * @brief Executes the step-up of the resistance.
196 *
197 * @return None.
198 *******************************************************************************/
199 void DoIncrement(double* param, char paramNo) // "increment=" command
200 {
201 unsigned char stepNo = 0;
202 unsigned char counter = 0;
203
204 if(paramNo >= 1)
205 {
206 paramLimit(&param[0], 0, MAX_POS);
207 stepNo = (unsigned char)param[0];
208 for (counter = 0; counter < stepNo; counter++)
209 {
210 incValue();
211 }
212 CONSOLE_Print("Resistor value was increased with %d steps.\r\n", stepNo);
213 }
214 else
215 {
216 /* Display error messages */
217 DisplayError(1);
218 }
219 }
220
221 /**************************************************************************//***
222 * @brief Executes the step-down of the resistance.
223 *
224 * @return None.
225 *******************************************************************************/
226 void DoDecrement(double* param, char paramNo) // "decrement=" command
227 {
228 unsigned char stepNo = 0;
229 unsigned char counter = 0;
230
231 if(paramNo >= 1)
232 {
233 paramLimit(&param[0], 0, MAX_POS);
234 stepNo = (unsigned char)param[0];
235 for (counter = 0; counter < stepNo; counter++)
236 {
237 decValue();
238 }
239 CONSOLE_Print("Resistor value was decreased with %d steps.\r\n", stepNo);
240 }
241 else
242 {
243 /* Display error messages */
244 DisplayError(2);
245 }
246 }
247 /**************************************************************************//***
248 * @brief Executes a shutdown operation on the device.
249 *
250 * @return None.
251 *******************************************************************************/
252 void DoShutdown(double* param, char paramNo) // "shutdown!" command
253 {
254 shutDown();
255 CONSOLE_Print("Device entered in shutdown mode.\r\n");
256 }
257
258 /**************************************************************************//***
259 * @brief Executes a write operation to EEPROM of RDAC value.
260 *
261 * @return None.
262 *******************************************************************************/
263 void DoStore(double* param, char paramNo) // "store!" command
264 {
265 saveMem();
266 CONSOLE_Print("Device memorized the current RDAC value in EEPROM.\r\n");
267 }