Debian Codegraph @

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

raw symbols SCIP
1 /***************************************************************************//**
2 * @file Command.c
3 * @brief Implementation of the commands given by user through UART for PmodACL.
4 * @author Mihai Bancisor ([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 "Communication.h"
45 #include "Command.h"
46 #include "ADXL345.h"
47 #include "Console.h"
48
49 /******************************************************************************/
50 /************************ Constants Definitions *******************************/
51 /******************************************************************************/
52
53 /*!< List of available commands */
54 const char* cmdList[] = {
55 "help?",
56 "communication=",
57 "communication?",
58 "acceleration?",
59 "accelerationX?",
60 "accelerationY?",
61 "accelerationZ?",
62 "interrupts?"};
63
64 /*!< Description of available commands */
65 const char* cmdDescription[] = {
66 " - Displays all available commands.",
67 " - Selects the communication interface. Accepted values:\r\n\
68 \t0 - SPI. \r\n\
69 \t1 - I2C.",
70 " - Displays the selected communication interface.",
71 " - Displays the acceleration on XYZ axis.",
72 " - Displays the acceleration on X axis.",
73 " - Displays the acceleration on Y axis.",
74 " - Displays the acceleration on Z axis.",
75 " - Displays the state of the interrupts."};
76
77 /*!< Usage example of available commands */
78 const char* cmdExample[] = {
79 "",
80 "To select the I2C communication port, type communication=1",
81 "",
82 "",
83 "",
84 "",
85 "",
86 ""};
87
88 const char cmdNo = (sizeof(cmdList) / sizeof(const char*));
89
90 /******************************************************************************/
91 /************************ Variables Definitions *******************************/
92 /******************************************************************************/
93
94 cmdFunction cmdFunctions[8] = {GetHelp, SetCommunication,
95 GetCommunication, GetAcceleration,
96 GetAccelerationX, GetAccelerationY,
97 GetAccelerationZ, GetInterrupts};
98
99 float x = 0; /*!< X-axis's output data. */
100 float y = 0; /*!< Y-axis's output data. */
101 float z = 0; /*!< Z-axis's output data. */
102 unsigned char intSource = 0; /*!< Value of the ADXL345_INT_SOURCE register. */
103 unsigned char communicationPort = 0; /*!< Type of communication port. */
104 unsigned char adxlInitializedOk = 0; /*!< Initialization status. */
105 unsigned short interruptsOccured = 0; /*!< Interrupts occured. */
106
107 /***************************************************************************//**
108 * @brief Displays all available commands.
109 *
110 * @return None.
111 *******************************************************************************/
112 void GetHelp(double* param, char paramNo) /*!< "help?" command */
113 {
114 char displayCmd;
115
116 CONSOLE_Print("Available commands:\r\n");
117 for(displayCmd = 0; displayCmd < cmdNo; displayCmd++)
118 {
119 CONSOLE_Print("%s%s\r\n", (char*)cmdList[displayCmd],
120 (char*)cmdDescription[displayCmd]);
121 }
122 }
123
124 /***************************************************************************//**
125 * @brief Initializes the device.
126 *
127 * @return - The result of the initialization.
128 * Example: ERROR - the device was not initialized or the device
129 * is not present.
130 * SUCCES - the device was initialized and the device
131 * is present.
132 *******************************************************************************/
133 char DoDeviceInit(void)
134 {
135 char receivedChar = 0;
136 char charNumber = 0;
137 double commProtocol = 0;
138 char command[10];
139
140 CONSOLE_Print("\nPlease select the communication interface. Accepted values:\r\n\
141 \t0 - SPI. \r\n\
142 \t1 - I2C.\r\n");
143 CONSOLE_Print("%s",cmdList[1]);
144
145 /*!< Read the character entered by user through UART. */
146 while((receivedChar != '\n') && (receivedChar != '\r'))
147 {
148 UART_ReadChar(&receivedChar);
149 command[charNumber++] = receivedChar;
150 }
151 commProtocol = (double)(command[0] - 0x30);
152
153 /*!< Initialize the device with desired interface. */
154 if(ADXL345_Init(commProtocol) == 0)
155 {
156 SetCommunication(&commProtocol, 1);
157 /*!< Display all available commands. */
158 GetHelp(NULL, 0);
159 return SUCCESS;
160 }
161 else
162 {
163 CONSOLE_Print("ADXL345 Error\r\n");
164 return ERROR;
165 }
166 }
167
168 /***************************************************************************//**
169 * @brief Selects the communication port for ADXL345.
170 *
171 * @param param[0] - selected communication port;
172 * paramNo - number of parameters (must be >=1); only the first parameter is taken into account;
173 *
174 * @return None.
175 *******************************************************************************/
176 void SetCommunication(double* param, char paramNo) /*!< "communication=" command */
177 {
178 if(paramNo >= 1)
179 {
180 if(param[0] == 0)
181 {
182 communicationPort = ADXL345_SPI_COMM;
183 }
184 else if(param[0] == 1)
185 {
186 communicationPort = ADXL345_I2C_COMM;
187 }
188 else
189 {
190 if(param[0] < 0)
191 {
192 communicationPort = ADXL345_SPI_COMM;
193 }
194 else
195 {
196 communicationPort = ADXL345_I2C_COMM;
197 }
198 }
199
200
201 /*!< Send feedback to user */
202 CONSOLE_Print("%s",(char*)cmdList[1]);
203 if(communicationPort == ADXL345_I2C_COMM)
204 {
205 CONSOLE_Print("I2C\r\n");
206 }
207 else
208 {
209 CONSOLE_Print("SPI\r\n");
210 }
211 /*!< Initialize device */
212 if(ADXL345_Init(communicationPort) == 0)
213 {
214 CONSOLE_Print("ADXL345 OK\r\n");
215 adxlInitializedOk = 1;
216 }
217 else
218 {
219 CONSOLE_Print("ADXL345 Error\r\n");
220 adxlInitializedOk = 0;
221 }
222 /*!< Configure device*/
223 ADXL345_SetTapDetection(ADXL345_SINGLE_TAP |
224 ADXL345_DOUBLE_TAP, /*!< Tap type. */
225 ADXL345_TAP_X_EN, /*!< Axis control. */
226 0x64, /*!< Tap duration.--10 */
227 0x20, /*!< Tap latency.--10 */
228 0x40, /*!< Tap window. */
229 0x10, /*!< Tap threshold. */
230 0x00); /*!< Interrupt Pin. */
231 ADXL345_SetFreeFallDetection(0x01, /*!< Free-fall detection enabled. */
232 0x05, /*!< Free-fall threshold. */
233 0x14, /*!< Time value for free-fall detection. */
234 0x00); /*!< Interrupt Pin. */
235 ADXL345_SetPowerMode(0x1); /*!< Measure mode. */
236 }
237 else
238 {
239 /*!< Display error messages. */
240 CONSOLE_Print("Invalid parameter!\r\n");
241 CONSOLE_Print("%s%s\r\n", (char*)cmdList[1], (char*)cmdDescription[1]);
242 CONSOLE_Print("Example: %s\r\n", (char*)cmdExample[1]);
243 }
244 }
245
246 /***************************************************************************//**
247 * @brief Displays the selected communication port.
248 *
249 * @return None.
250 *******************************************************************************/
251 void GetCommunication(double* param, char paramNo) /*!< "communication?" command */
252 {
253 if(adxlInitializedOk)
254 {
255 /*!< Send the requested value to user */
256 CONSOLE_Print("%s",cmdList[1]);
257 if(communicationPort == ADXL345_I2C_COMM)
258 {
259 CONSOLE_Print("I2C\r\n");
260 }
261 else
262 {
263 CONSOLE_Print("SPI\r\n");
264 }
265 }
266 else
267 {
268 CONSOLE_Print("ADXL345 was not succesfully initialized. \
269 Select the ADXL345 comm. port using the 'communication=' command.\r\n ");
270 }
271 }
272
273 /***************************************************************************//**
274 * @brief Displays the acceleration on all 3 axes.
275 *
276 * @return None.
277 *******************************************************************************/
278 void GetAcceleration(double* param, char paramNo) /*!< "acceleration?" command */
279 {
280 unsigned short samplesNumber = 0;
281
282 if(adxlInitializedOk)
283 {
284 /*!< Read and displays 30 samples from the accelerometer. */
285 samplesNumber = 30;
286 while(samplesNumber)
287 {
288 /*!< Read the requested value from the device */
289 ADXL345_GetGxyz(&x,&y,&z);
290 /*!< Send the requested value to user */
291 CONSOLE_Print("x axis: %.3f\r\n",x);
292 CONSOLE_Print("y axis: %.3f\r\n",y);
293 CONSOLE_Print("z axis: %.3f\r\n",z);
294 TIME_DelayMs(200);
295 samplesNumber--;
296 }
297 }
298 else
299 {
300 CONSOLE_Print("ADXL345 was not succesfully initialized. \
301 Select the ADXL345 comm. port using the 'communication=' command.\r\n");
302 }
303 }
304
305 /***************************************************************************//**
306 * @brief Displays the acceleration on X axis.
307 *
308 * @return None.
309 *******************************************************************************/
310 void GetAccelerationX(double* param, char paramNo) /*!< "accelerationX?" command */
311 {
312 unsigned short samplesNumber = 0;
313
314 if(adxlInitializedOk)
315 {
316 /*!< Read and displays 30 samples from the accelerometer. */
317 samplesNumber = 30;
318 while(samplesNumber)
319 {
320 /*!< Read the requested value from the device */
321 ADXL345_GetGxyz(&x,&y,&z);
322 /*!< Send the requested value to user */
323 CONSOLE_Print("x axis: %.3f\r\n",x);
324 TIME_DelayMs(200);
325 samplesNumber--;
326 }
327 }
328 else
329 {
330 CONSOLE_Print("ADXL345 was not succesfully initialized. \
331 Select the ADXL345 comm. port using the 'communication=' command.\r\n");
332 }
333 }
334
335 /***************************************************************************//**
336 * @brief Displays the acceleration on Y axis.
337 *
338 * @return None.
339 *******************************************************************************/
340 void GetAccelerationY(double* param, char paramNo) /*!< "accelerationY?" command */
341 {
342 unsigned short samplesNumber = 0;
343
344 if(adxlInitializedOk)
345 {
346 /*!< Read and displays 30 samples from the accelerometer. */
347 samplesNumber = 30;
348 while(samplesNumber)
349 {
350 /*!< Read the requested value from the device */
351 ADXL345_GetGxyz(&x,&y,&z);
352 /*!< Send the requested value to user */
353 CONSOLE_Print("y axis: %.3f\r\n",y);
354 TIME_DelayMs(200);
355 samplesNumber--;
356 }
357 }
358 else
359 {
360 CONSOLE_Print("ADXL345 was not succesfully initialized. \
361 Select the ADXL345 comm. port using the 'communication=' command.\r\n");
362 }
363 }
364
365 /***************************************************************************//**
366 * @brief Displays the acceleration on Z axis.
367 *
368 * @return None.
369 *******************************************************************************/
370 void GetAccelerationZ(double* param, char paramNo) /*!< "accelerationZ?" command */
371 {
372 unsigned short samplesNumber = 0;
373
374 if(adxlInitializedOk)
375 {
376 /*!< Read and displays 30 samples from the accelerometer. */
377 samplesNumber = 30;
378 while(samplesNumber)
379 {
380 /*!< Read the requested value from the device */
381 ADXL345_GetGxyz(&x,&y,&z);
382 /*!< Send the requested value to user */
383 CONSOLE_Print("z axis: %.3f\r\n",z);
384 TIME_DelayMs(200);
385 samplesNumber--;
386 }
387 }
388 else
389 {
390 CONSOLE_Print("ADXL345 was not succesfully initialized. \
391 Select the ADXL345 comm. port using the 'communication=' command.\r\n");
392 }
393 }
394
395 /***************************************************************************//**
396 * @brief Displays the state of the interrupts.
397 *
398 * @return None.
399 *******************************************************************************/
400 void GetInterrupts(double* param, char paramNo) /*!< "interrupts?" command */
401 {
402 if(adxlInitializedOk)
403 {
404 interruptsOccured = 0;
405 /*!< Read the requested value from the device */
406 intSource = ADXL345_GetRegisterValue(ADXL345_INT_SOURCE);
407 /*!< Send the requested value to user */
408 if((intSource & ADXL345_SINGLE_TAP) != 0)
409 {
410 CONSOLE_Print("Single Tap.\n");
411 interruptsOccured = 1;
412 }
413 if((intSource & ADXL345_DOUBLE_TAP) != 0)
414 {
415 CONSOLE_Print("Double Tap.\n");
416 interruptsOccured = 1;
417 }
418 if((intSource & ADXL345_FREE_FALL) != 0)
419 {
420 CONSOLE_Print("Free Fall.\n");
421 interruptsOccured = 1;
422 }
423 if(interruptsOccured == 0)
424 {
425 /*!< Send feedback to user */
426 CONSOLE_Print("None.\n");
427 }
428 }
429 else
430 {
431 CONSOLE_Print("ADXL345 was not succesfully initialized. \
432 Select the ADXL345 comm. port using the 'communication=' command.\r\n");
433 }
434
435 }