Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodCDC1.
4 * @author Dan Nechita ([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 "AD7156.h"
46 #include "Console.h"
47
48 /******************************************************************************/
49 /************************ Constants Definitions *******************************/
50 /******************************************************************************/
51 /*!< List of available commands */
52 const char* cmdList[] = {"help?",
53 "channelRange?",
54 "channelRange=",
55 "powerMode?",
56 "powerMode=",
57 "channelEnable=",
58 "samples?",
59 "buttons?",
60 "reset!"};
61
62 /*!< List of commands' descriptions */
63 const char* cmdDescription[] = {
64 " - Displays all available commands.",
65 " - Displays the set range for the selected channel. Accepted values: \r\n\
66 \t1 - Displays the channel 1's range. \r\n\
67 \t2 - Displays the channel 2's range.",
68 " - Selects the range of the selected channel. Accepted values: \r\n\
69 \tchannel:\r\n\
70 \t1 - Select channel 1. \r\n\
71 \t2 - Select channel 2. \r\n\
72 \trange:\r\n\
73 \t0 - 0 .. 2 pF. \r\n\
74 \t1 - 0 .. 0.5 pF. \r\n\
75 \t2 - 0 .. 1 pF. \r\n\
76 \t3 - 0 .. 4 pF.",
77 " - Displays the current power mode.",
78 " - Selects the power mode of the device. Accepted values: \r\n\
79 \t0 - Idle. \r\n\
80 \t1 - Continuous conversions. \r\n\
81 \t2 - Single conversion. \r\n\
82 \t3 - Power-down.",
83 " - Enables or disabled conversions on selected channel. Accepted values: \r\n\
84 \tchannel:\r\n\
85 \t1 - Select channel 1. \r\n\
86 \t2 - Select channel 2. \r\n\
87 \tenable:\r\n\
88 \t0 - Disable conversions on selected channel. \r\n\
89 \t1 - Enable conversions on selected channel.",
90 " - Reads 50 samples from the enabled channels and displays the data in pF.",
91 " - Checks if any of the two buttons are pressed or released.",
92 " - Resets the device."};
93
94
95 const char* cmdExample[] = {"",
96 "To get the channel 1's range, type: channelRange?1",
97 "To set the channel 1's range to 2, type: channelRange=1 2",
98 "",
99 "To set the power mode to idle, type: powerMode=0",
100 "To enable channel2, type: channelEnable=2 1",
101 ""};
102
103 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
104
105 /******************************************************************************/
106 /************************ Variables Definitions *******************************/
107 /******************************************************************************/
108 cmdFunction cmdFunctions[9] = {GetHelp, GetRange, SetRange, GetPowerMode,\
109 SetPowerMode, EnableCh, GetSamples, \
110 GetButtonsStatus, DoReset};
111 unsigned char powerMode = 1;
112 unsigned char chRange[2] = {0, 0};
113 unsigned char chEnable[2] = {1, 1};
114
115 /******************************************************************************/
116 /************************ Functions Description *******************************/
117 /******************************************************************************/
118
119 /***************************************************************************//**
120 * @brief Displays all available commands.
121 *
122 * @return None.
123 *******************************************************************************/
124 void GetHelp(double* param, char paramNo) /*!< "help?" command */
125 {
126 char displayCmd;
127
128 CONSOLE_Print("Available commands:\r\n");
129 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
130 {
131 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
132 (char*)cmdDescription[displayCmd]);
133 }
134 }
135
136 /***************************************************************************//**
137 * @brief Initializes the device.
138 *
139 * @return - The result of the initialization.
140 * Example: ERROR - the device was not initialized or the device
141 * is not present.
142 * SUCCES - the device was initialized and the device
143 * is present.
144 *******************************************************************************/
145 char DoDeviceInit(void)
146 {
147 if(AD7156_Init() == 0)
148 {
149 CONSOLE_Print("AD7156 OK\r\n");
150 GetHelp(NULL, 0);
151
152 return SUCCESS;
153 }
154 else
155 {
156 CONSOLE_Print("AD7156 Error\r\n");
157
158 return ERROR;
159 }
160 }
161
162 /***************************************************************************//**
163 * @brief Displays the set range for the selected channel.
164 *
165 * @param param[0] - selected channel;
166 * paramNo - number of parameters (must be >=1); only the first parameter is taken into account;
167 *
168 * @return None.
169 *******************************************************************************/
170 void GetRange(double* param, char paramNo) /*!< "channelrange?" command */
171 {
172 if(paramNo >= 1)
173 {
174 if(param[0] < 1)
175 {
176 param[0] = 1;
177 }
178 if(param[0] > 2)
179 {
180 param[0] = 2;
181 }
182 CONSOLE_Print("Channel %d range: %d\r\n",
183 (char)param[0],
184 chRange[(char)param[0]-1]);
185 }
186 else
187 {
188 /*!< Display error messages. */
189 CONSOLE_Print("Invalid parameter!\r\n");
190 CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]);
191 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]);
192 }
193 }
194
195 /***************************************************************************//**
196 * @brief Selects the range of the selected channel.
197 *
198 * @param param[0] - selected channel;
199 * param[1] - selected range;
200 * paramNo - number of parameters (must be >=2); only the first 2 parameters are taken into account;
201 *
202 * @return None.
203 *******************************************************************************/
204 void SetRange(double* param, char paramNo) /*!< "channelrange=" command */
205 {
206 if(paramNo >= 2)
207 {
208 if(param[0] < 1)
209 {
210 param[0] = 1;
211 }
212 if(param[0] > 2)
213 {
214 param[0] = 2;
215 }
216 if(param[1] < 0)
217 {
218 param[1] = 0;
219 }
220 if(param[1] > 3)
221 {
222 param[1] = 3;
223 }
224
225 chRange[(char)param[0]-1] = param[1];
226 /*!< Send feedback to user. */
227 CONSOLE_Print("Channel %d range: %d\r\n",
228 (char)param[0],
229 chRange[(char)param[0]-1]);
230 }
231 else
232 {
233 /*!< Display error messages. */
234 CONSOLE_Print("Invalid parameter!\r\n");
235 CONSOLE_Print("%s%s\r\n", (char*)cmdList[2], (char*)cmdDescription[2]);
236 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[2]);
237 }
238 }
239
240 /***************************************************************************//**
241 * @brief Displays the current power mode.
242 *
243 * @return None.
244 *******************************************************************************/
245 void GetPowerMode(double* param, char paramNo) /*!< "powermode?" command */
246 {
247 /*!< Send feedback to user. */
248 CONSOLE_Print("%s%d\r\n",(char*)cmdList[4],powerMode);
249 }
250
251 /***************************************************************************//**
252 * @brief Selects the power mode of the device.
253 *
254 * @param param[0] - selected power mode;
255 * paramNo - number of parameters (must be >=1); only the first parameter is taken into account;
256 *
257 * @return None.
258 *******************************************************************************/
259 void SetPowerMode(double* param, char paramNo) /*!< "powermode=" command */
260 {
261 if(paramNo >= 1)
262 {
263 if(param[0] < 0)
264 {
265 param[0] = 0;
266 }
267 if(param[0] > 3)
268 {
269 param[0] = 3;
270 }
271 powerMode = (char)param[0];
272 AD7156_SetPowerMode(powerMode);
273 /*!< Send feedback to user. */
274 CONSOLE_Print("%s%d\r\n",(char*)cmdList[4],powerMode);
275 }
276 else
277 {
278 /*!< Display error messages. */
279 CONSOLE_Print("Invalid parameter!\r\n");
280 CONSOLE_Print("%s%s\r\n", (char*)cmdList[4], (char*)cmdDescription[4]);
281 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[4]);
282 }
283 }
284
285 /***************************************************************************//**
286 * @brief Enables or disabled conversions on selected channel.
287 *
288 * @param param[0] - selected channel;
289 * param[1] - 0 - disable selected channel;
290 * - 1 - enable delected channel;
291 * paramNo - number of parameters (must be >=2); only the first 2 parameters are taken into account;
292 *
293 * @return None.
294 *******************************************************************************/
295 void EnableCh(double* param, char paramNo) /*!< "channelenable=" command */
296 {
297 if(paramNo >= 2)
298 {
299 if(param[0] < 1)
300 {
301 param[0] = 1;
302 }
303 if(param[0] > 2)
304 {
305 param[0] = 2;
306 }
307 if(param[1] < 0)
308 {
309 param[1] = 0;
310 }
311 if(param[1] > 1)
312 {
313 param[1] = 1;
314 }
315
316 if (param[0] == 1)
317 {
318 AD7156_ChannelState(AD7156_CHANNEL1, param[1]);
319 }
320 else
321 {
322 AD7156_ChannelState(AD7156_CHANNEL2, param[1]);
323 }
324 chEnable[(char)param[0]-1] = (char)param[1];
325 /*!< Send feedback to user. */
326 CONSOLE_Print("Channel %d status: %d\r\n", (char)param[0],
327 chEnable[(char)param[0]-1]);
328 }
329 else
330 {
331 /*!< Display error messages. */
332 CONSOLE_Print("Invalid parameter!\r\n");
333 CONSOLE_Print("%s%s\r\n", (char*)cmdList[5], (char*)cmdDescription[5]);
334 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[5]);
335 }
336
337 }
338
339 /***************************************************************************//**
340 * @brief Reads 50 samples from the enabled channels and displays the data in pF.
341 *
342 * @return None.
343 *******************************************************************************/
344 void GetSamples(double* param, char paramNo) /*!< "samples?" command */
345 {
346 float ch1 = 0; /*!< Value of the capacitance measured(pF). */
347 float ch2 = 0; /*!< Value of the capacitance measured(pF). */
348 long samplesNr = 0; /*!< Number of samples to be read. */
349
350 if (powerMode == 3) /*!< power-down mode */
351 {
352 CONSOLE_Print("Device in power-down mode. Please change the power mode \
353 in order to display the samples.\r\n");
354 }
355 else if (powerMode == 1) /*!< Continuous conversions mode */
356 {
357 /*!< Display 50 samples from channel1 and channel2. */
358 for(samplesNr = 0; samplesNr < 50; samplesNr++)
359 {
360 if (chEnable[0] == 1)
361 {
362 ch1 = AD7156_WaitReadChannelCapacitance(AD7156_CHANNEL1);
363 }
364 else
365 {
366 ch1 = AD7156_ReadChannelCapacitance(AD7156_CHANNEL1);
367 }
368
369 if (chEnable[1] == 1)
370 {
371 ch2 = AD7156_WaitReadChannelCapacitance(AD7156_CHANNEL2);
372 }
373 else
374 {
375 ch2 = AD7156_ReadChannelCapacitance(AD7156_CHANNEL2);
376 }
377
378 CONSOLE_Print("Ch1: %.3f pF\r\n",ch1);
379 CONSOLE_Print("Ch2: %.3f pF\r\n",ch2);
380 TIME_DelayMs(100);
381 }
382 }
383 else /*!< Idle mode or single conversion mode */
384 {
385 /*!< Display 50 samples from channel1 and channel2. */
386 for(samplesNr = 0; samplesNr < 50; samplesNr++)
387 {
388 ch1 = AD7156_ReadChannelCapacitance(AD7156_CHANNEL1);
389 ch2 = AD7156_ReadChannelCapacitance(AD7156_CHANNEL2);
390 CONSOLE_Print("Ch1: %.3f pF\r\n",ch1);
391 CONSOLE_Print("Ch2: %.3f pF\r\n",ch2);
392 TIME_DelayMs(100);
393 }
394 }
395 }
396
397 /***************************************************************************//**
398 * @brief Checks if any of the two buttons are pressed or released.
399 *
400 * @return None.
401 *******************************************************************************/
402 void GetButtonsStatus(double* param, char paramNo) /*!< "buttons?" command */
403 {
404 unsigned char status = 0; /*!< The content of the STATUS register. */
405 unsigned char btnEvent = 0; /*!< Holds the number of button actions. */
406 unsigned char prevBtn1State = 0; /*!< Previous state of Button1(ON/OFF). */
407 unsigned char prevBtn2State = 0; /*!< Previous state of Button2(ON/OFF). */
408
409 /*!< Configure device. */
410 AD7156_SetPowerMode(AD7156_CONV_MODE_IDLE);
411 AD7156_SetThresholdMode(AD7156_THR_MODE_NEGATIVE,
412 AD7156_FIXED_THRESHOLD);
413 AD7156_SetThreshold(AD7156_CHANNEL1, 1.0); /*!< Threshold = 1.0pF */
414 AD7156_SetThreshold(AD7156_CHANNEL2, 1.0); /*!< Threshold = 1.0pF */
415 AD7156_SetPowerMode(AD7156_CONV_MODE_CONT_CONV);
416 /*!< Start polling buttons(channels) state. */
417 CONSOLE_Print("Start button polling.\r\n");
418 btnEvent = 0;
419 prevBtn1State = 0;
420 prevBtn2State = 0;
421 /*!< After 15 button actions the execution returns to the menu. */
422 while(btnEvent <= 15)
423 {
424 /*!< Read the STATUS register. */
425 AD7156_GetRegisterValue(&status, AD7156_REG_STATUS, 1);
426 /*!< Check for changes in the button state. */
427 if(((status & AD7156_STATUS_OUT1) !=0) &&
428 (prevBtn1State == 0))
429 {
430 CONSOLE_Print("BTN1 - pressed.\r\n");
431 prevBtn1State = 1;
432 btnEvent++;
433 }
434 else if(((status & AD7156_STATUS_OUT1) == 0) &&
435 (prevBtn1State == 1))
436 {
437 CONSOLE_Print("BTN1 - released.\r\n");
438 prevBtn1State = 0;
439 btnEvent++;
440 }
441 if(((status & AD7156_STATUS_OUT2) !=0) &&
442 (prevBtn2State == 0))
443 {
444 CONSOLE_Print("BTN2 - pressed.\r\n");
445 prevBtn2State = 1;
446 btnEvent++;
447 }
448 else if(((status & AD7156_STATUS_OUT2) == 0) &&
449 (prevBtn2State == 1))
450 {
451 CONSOLE_Print("BTN2 - released.\r\n");
452 prevBtn2State = 0;
453 btnEvent++;
454 }
455 }
456 CONSOLE_Print("Button polling finished.\r\n");
457 /*!< Return to previous power mode. */
458 AD7156_SetPowerMode(powerMode);
459 }
460
461 /***************************************************************************//**
462 * @brief Resets the device.
463 *
464 * @return None.
465 *******************************************************************************/
466 void DoReset(double* param, char paramNo) /*!< "reset!" command */
467 {
468 AD7156_Reset();
469 powerMode = 1;
470 chEnable[0] = 1;
471 chEnable[1] = 1;
472 /* Send feedback to user. */
473 CONSOLE_Print("The device was reset.\r\n");
474 }