Debian Codegraph @

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

raw symbols SCIP
1 /**************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for AD5415.
4 * @author Istvan Csomortani ([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 "AD5425.h"
47 #include "TIME.h"
48
49 /******************************************************************************/
50 /************************ Constants Definitions *******************************/
51 /******************************************************************************/
52 #define MAX_VALUE 0xFF
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 = "register=",
63 .description = "Loads the DAC input register with a given value.",
64 .acceptedValue = "Accepted values:\r\n\
65 \tvalue:\r\n \
66 \t0 .. 256 - value to be written in register.",
67 .example = "To load DAC B input register with 128, type: register=128",
68 },
69 [2] = {
70 .name = "ldacPin=",
71 .description = "Sets the output value of LDAC pin.",
72 .acceptedValue = "Accepted values:\r\n\
73 \t0 - sets LDAC pin low.(default)\r\n\
74 \t1 - sets LDAC pin high.",
75 .example = "To set the LDAC pin high, type: ldacPin=1",
76 },
77 [3] = {
78 .name = "ldacPin?",
79 .description = "Displays the value of LDAC pin.",
80 .acceptedValue = "",
81 .example = "",
82 }
83 };
84
85 const char cmdNo = (sizeof(cmdList) / sizeof(struct cmd_info));
86
87 /******************************************************************************/
88 /************************ Variables Definitions *******************************/
89 /******************************************************************************/
90 cmdFunction cmdFunctions[12] = {GetHelp, SetRegister, SetLdacPin, GetLdacPin};
91
92 /* Variables holding information about the device */
93 unsigned char ldac = 0;
94
95 /***************************************************************************//**
96 * @brief Displays error message.
97 *
98 * @return None.
99 *******************************************************************************/
100 void DisplayError(unsigned char funcNo)
101 {
102 /* Display error messages */
103 CONSOLE_Print("Invalid parameter!\r\n");
104 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[funcNo].name, \
105 (char*)cmdList[funcNo].description, \
106 (char*)cmdList[funcNo].acceptedValue);
107 CONSOLE_Print("Example: %s\r\n", (char*)cmdList[funcNo].example);
108 }
109
110 /***************************************************************************//**
111 * @brief Internal function for displaying all the command with its description.
112 *
113 * @return None.
114 *******************************************************************************/
115 void DisplayCmdList()
116 {
117 unsigned char displayCmd;
118
119 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
120 {
121 CONSOLE_Print("%s - %s\r\n", (char*)cmdList[displayCmd].name, \
122 (char*)cmdList[displayCmd].description);
123 }
124 }
125
126 /***************************************************************************//**
127 * @brief Verify if the given parameter is between his valid limits.
128 *
129 * @param None.
130 *
131 * @return None.
132 *******************************************************************************/
133 void paramLimit(double* param,
134 unsigned short lowerLimit,
135 unsigned short upperLimit)
136 {
137 if(*param < lowerLimit)
138 {
139 *param = lowerLimit;
140 }
141 else
142 {
143 if(*param > upperLimit)
144 {
145 *param = upperLimit;
146 }
147 }
148 }
149
150 /**************************************************************************//***
151 * @brief Displays all available commands.
152 *
153 * @return None.
154 *******************************************************************************/
155 void GetHelp(double* param, char paramNo) // "help?" command
156 {
157 unsigned char displayCmd;
158
159 CONSOLE_Print("Available commands:\r\n");
160 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
161 {
162 CONSOLE_Print("%s - %s %s\r\n", (char*)cmdList[displayCmd].name,
163 (char*)cmdList[displayCmd].description,
164 (char*)cmdList[displayCmd].acceptedValue);
165 }
166 }
167
168 /***************************************************************************//**
169 * @brief Initializes the device.
170 *
171 * @return - The result of the initialization.
172 * Example: ERROR - the device was not initialized or the device
173 * is not present.
174 * SUCCES - the device was initialized and the device
175 * is present.
176 *******************************************************************************/
177 char DoDeviceInit(void)
178 {
179 if(AD5425_Init() == 0)
180 {
181 CONSOLE_Print("AD5425 OK\r\n");
182 DisplayCmdList();
183 return SUCCESS;
184 }
185 else
186 {
187 CONSOLE_Print("AD5425 Error\r\n");
188 return ERROR;
189 }
190 }
191
192 /***************************************************************************//**
193 * @brief Loads the DAC register with a given value.
194 *
195 * @param param[0] - value to be written in register.
196 *
197 * @return None.
198 *******************************************************************************/
199 void SetRegister(double* param, char paramNo) // "loadAndUpdate=" command
200 {
201 unsigned char dacValue = 0;
202
203 /* Check if the parameters are valid */
204 if(paramNo >= 1)
205 {
206 paramLimit(&param[0], 0, MAX_VALUE);
207 dacValue = (unsigned char)param[0];
208 AD5425_SetRegister(dacValue);
209 /* Send feedback to user */
210 CONSOLE_Print("value=%d\r\n", dacValue);
211 }
212 else
213 {
214 /* Display error messages */
215 DisplayError(1);
216 }
217 }
218
219 /***************************************************************************//**
220 * @brief Sets the output value of LDAC pin.
221 *
222 * @param param[0] - value to be set for LDAC pin.
223 *
224 * @return None.
225 *******************************************************************************/
226 void SetLdacPin(double* param, char paramNo) // "ldacPin=" command
227 {
228 unsigned char status = 0;
229
230 /* Check if the parameter is valid */
231 if(paramNo >= 1)
232 {
233 paramLimit(&param[0], 0, 1);
234
235 status = (unsigned char) param[0];
236
237 if (status == 0)
238 {
239 AD5425_LDAC_LOW;
240 ldac = 0;
241 }
242 else
243 {
244 if (status == 1)
245 {
246 AD5425_LDAC_HIGH;
247 ldac = 1;
248 }
249 }
250 /* Send feedback to user */
251 CONSOLE_Print("%s%d\r\n",(char*)cmdList[2].name, status);
252 }
253 else
254 {
255 /* Display error messages */
256 DisplayError(2);
257 }
258 }
259
260 /***************************************************************************//**
261 * @brief Displays the value of LDAC pin.
262 *
263 * @param None.
264 *
265 * @return None.
266 *******************************************************************************/
267 void GetLdacPin(double* param, char paramNo) // "ldacPin?" command
268 {
269
270 /* Send requested value to user */
271 CONSOLE_Print("LDAC=%d\r\n",ldac);
272 }
273