Debian Codegraph @

thirdparty/analogdevicesinc/no-OS/drivers/ADXL345/ADXL345.c

raw symbols SCIP
1 /***************************************************************************//**
2 * @file ADXL345.c
3 * @brief Implementation of ADXL345 Driver.
4 * @author DBogdan ([email protected])
5 ********************************************************************************
6 * Copyright 2012(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 * SVN Revision: $WCREV$
41 *******************************************************************************/
42
43 /******************************************************************************/
44 /***************************** Include Files **********************************/
45 /******************************************************************************/
46 #include "ADXL345.h" // ADXL345 definitions.
47 #include "Communication.h" // Communication definitions.
48
49 /******************************************************************************/
50 /************************ Variables Definitions *******************************/
51 /******************************************************************************/
52 char communicationType = 0;
53 char selectedRange = 0;
54 char fullResolutionSet = 0;
55
56 /******************************************************************************/
57 /************************ Functions Definitions *******************************/
58 /******************************************************************************/
59
60 /***************************************************************************//**
61 * @brief Reads the value of a register.
62 *
63 * @param registerAddress - Address of the register.
64 *
65 * @return registerValue - Value of the register.
66 *******************************************************************************/
67 unsigned char ADXL345_GetRegisterValue(unsigned char registerAddress)
68 {
69 unsigned char dataBuffer[2] = {0, 0};
70 unsigned char registerValue = 0;
71
72 if(communicationType == ADXL345_SPI_COMM)
73 {
74 dataBuffer[0] = ADXL345_SPI_READ | registerAddress;
75 dataBuffer[1] = 0;
76 SPI_Read(ADXL345_SLAVE_ID, dataBuffer, 2);
77 registerValue = dataBuffer[1];
78 }
79 else
80 {
81 I2C_Write(ADXL345_ADDRESS, // Address of the slave device.
82 &registerAddress, // Transmission data.
83 1, // Number of bytes to write.
84 0); // Stop condition control.
85 I2C_Read(ADXL345_ADDRESS, // Address of the slave device.
86 &registerValue, // Received data.
87 1, // Number of bytes to read.
88 1); // Stop condition control.
89 }
90
91 return registerValue;
92 }
93
94 /***************************************************************************//**
95 * @brief Writes data into a register.
96 *
97 * @param registerAddress - Address of the register.
98 * @param registerValue - Data value to write.
99 *
100 * @return None.
101 *******************************************************************************/
102 void ADXL345_SetRegisterValue(unsigned char registerAddress,
103 unsigned char registerValue)
104 {
105 unsigned char dataBuffer[2] = {0, 0};
106
107 if(communicationType == ADXL345_SPI_COMM)
108 {
109 dataBuffer[0] = ADXL345_SPI_WRITE | registerAddress;
110 dataBuffer[1] = registerValue;
111 SPI_Write(ADXL345_SLAVE_ID, dataBuffer, 2);
112 }
113 else
114 {
115 dataBuffer[0] = registerAddress;
116 dataBuffer[1] = registerValue;
117 I2C_Write(ADXL345_ADDRESS, dataBuffer, 2, 1);
118 }
119 }
120
121 /***************************************************************************//**
122 * @brief Initializes the communication peripheral and checks if the ADXL345
123 * part is present.
124 *
125 * @param commProtocol - SPI or I2C protocol.
126 * Example: ADXL345_SPI_COMM
127 * ADXL345_I2C_COMM
128 *
129 * @return status - Result of the initialization procedure.
130 * Example: -1 - I2C/SPI peripheral was not initialized or
131 * ADXL345 part is not present.
132 * 0 - I2C/SPI peripheral is initialized and
133 * ADXL345 part is present.
134 *******************************************************************************/
135 char ADXL345_Init(char commProtocol)
136 {
137 unsigned char status = 0;
138
139 communicationType = commProtocol;
140 if(commProtocol == ADXL345_SPI_COMM)
141 {
142 status = SPI_Init(0, // Transfer format.
143 1000000, // SPI clock frequency.
144 1, // SPI clock polarity.
145 0); // SPI clock edge.
146 }
147 else
148 {
149 status = I2C_Init(100000); // I2C clock frequency.
150 }
151 if(ADXL345_GetRegisterValue(ADXL345_DEVID) != ADXL345_ID)
152 {
153 status = -1;
154 }
155 selectedRange = 2; // Measurement Range: +/- 2g (reset default).
156 fullResolutionSet = 0;
157
158 return status;
159 }
160
161 /***************************************************************************//**
162 * @brief Places the device into standby/measure mode.
163 *
164 * @param pwrMode - Power mode.
165 * Example: 0x0 - standby mode.
166 * 0x1 - measure mode.
167 *
168 * @return None.
169 *******************************************************************************/
170 void ADXL345_SetPowerMode(unsigned char pwrMode)
171 {
172 unsigned char oldPowerCtl = 0;
173 unsigned char newPowerCtl = 0;
174
175 oldPowerCtl = ADXL345_GetRegisterValue(ADXL345_POWER_CTL);
176 newPowerCtl = oldPowerCtl & ~ADXL345_PCTL_MEASURE;
177 newPowerCtl = newPowerCtl | (pwrMode * ADXL345_PCTL_MEASURE);
178 ADXL345_SetRegisterValue(ADXL345_POWER_CTL, newPowerCtl);
179 }
180
181 /***************************************************************************//**
182 * @brief Reads the raw output data of each axis.
183 *
184 * @param x - X-axis's output data.
185 * @param y - Y-axis's output data.
186 * @param z - Z-axis's output data.
187 *
188 * @return None.
189 *******************************************************************************/
190 void ADXL345_GetXyz(short* x,
191 short* y,
192 short* z)
193 {
194 unsigned char firstRegAddress = ADXL345_DATAX0;
195 unsigned char readBuffer[7] = {0, 0, 0, 0, 0, 0, 0};
196
197 if(communicationType == ADXL345_SPI_COMM)
198 {
199 readBuffer[0] = ADXL345_SPI_READ |
200 ADXL345_SPI_MB |
201 firstRegAddress;
202 SPI_Read(ADXL345_SLAVE_ID, readBuffer, 7);
203 /* x = ((ADXL345_DATAX1) << 8) + ADXL345_DATAX0 */
204 *x = ((short)readBuffer[2] << 8) + readBuffer[1];
205 /* y = ((ADXL345_DATAY1) << 8) + ADXL345_DATAY0 */
206 *y = ((short)readBuffer[4] << 8) + readBuffer[3];
207 /* z = ((ADXL345_DATAZ1) << 8) + ADXL345_DATAZ0 */
208 *z = ((short)readBuffer[6] << 8) + readBuffer[5];
209 }
210 else
211 {
212 I2C_Write(ADXL345_ADDRESS, // Adress of the slave device.
213 &firstRegAddress, // Transmission data.
214 1, // Number of bytes to write.
215 0); // Stop condition control.
216 I2C_Read(ADXL345_ADDRESS, // Adress of the slave device.
217 readBuffer, // Received data.
218 6, // Number of bytes to read.
219 1); // Stop condition control.
220 /* x = ((ADXL345_DATAX1) << 8) + ADXL345_DATAX0 */
221 *x = ((short)readBuffer[1] << 8) + readBuffer[0];
222 /* y = ((ADXL345_DATAY1) << 8) + ADXL345_DATAY0 */
223 *y = ((short)readBuffer[3] << 8) + readBuffer[2];
224 /* z = ((ADXL345_DATAZ1) << 8) + ADXL345_DATAZ0 */
225 *z = ((short)readBuffer[5] << 8) + readBuffer[4];
226 }
227 }
228
229 /***************************************************************************//**
230 * @brief Reads the raw output data of each axis and converts it to g.
231 *
232 * @param x - X-axis's output data.
233 * @param y - Y-axis's output data.
234 * @param z - Z-axis's output data.
235 *
236 * @return None.
237 *******************************************************************************/
238 void ADXL345_GetGxyz(float* x,
239 float* y,
240 float* z)
241 {
242 short xData = 0; // X-axis's output data.
243 short yData = 0; // Y-axis's output data.
244 short zData = 0; // Z-axis's output data.
245
246 ADXL345_GetXyz(&xData, &yData, &zData);
247 *x = (float)(fullResolutionSet ? (xData * ADXL345_SCALE_FACTOR) :
248 (xData * ADXL345_SCALE_FACTOR * (selectedRange >> 1)));
249 *y = (float)(fullResolutionSet ? (yData * ADXL345_SCALE_FACTOR) :
250 (yData * ADXL345_SCALE_FACTOR * (selectedRange >> 1)));
251 *z = (float)(fullResolutionSet ? (zData * ADXL345_SCALE_FACTOR) :
252 (zData * ADXL345_SCALE_FACTOR * (selectedRange >> 1)));
253 }
254
255 /***************************************************************************//**
256 * @brief Enables/disables the tap detection.
257 *
258 * @param tapType - Tap type (none, single, double).
259 * Example: 0x0 - disables tap detection.
260 * ADXL345_SINGLE_TAP - enables single tap
261 * detection.
262 * ADXL345_DOUBLE_TAP - enables double tap
263 * detection.
264 * @param tapAxes - Axes which participate in tap detection.
265 * Example: 0x0 - disables axes participation.
266 * ADXL345_TAP_X_EN - enables x-axis participation.
267 * ADXL345_TAP_Y_EN - enables y-axis participation.
268 * ADXL345_TAP_Z_EN - enables z-axis participation.
269 * @param tapDur - Tap duration. The scale factor is 625us is/LSB.
270 * @param tapLatent - Tap latency. The scale factor is 1.25 ms/LSB.
271 * @param tapWindow - Tap window. The scale factor is 1.25 ms/LSB.
272 * @param tapThresh - Tap threshold. The scale factor is 62.5 mg/LSB.
273 * @param tapInt - Interrupts pin.
274 * Example: 0x0 - interrupts on INT1 pin.
275 * ADXL345_SINGLE_TAP - single tap interrupts on
276 * INT2 pin.
277 * ADXL345_DOUBLE_TAP - double tap interrupts on
278 * INT2 pin.
279 *
280 * @return None.
281 *******************************************************************************/
282 void ADXL345_SetTapDetection(unsigned char tapType,
283 unsigned char tapAxes,
284 unsigned char tapDur,
285 unsigned char tapLatent,
286 unsigned char tapWindow,
287 unsigned char tapThresh,
288 unsigned char tapInt)
289 {
290 unsigned char oldTapAxes = 0;
291 unsigned char newTapAxes = 0;
292 unsigned char oldIntMap = 0;
293 unsigned char newIntMap = 0;
294 unsigned char oldIntEnable = 0;
295 unsigned char newIntEnable = 0;
296
297 oldTapAxes = ADXL345_GetRegisterValue(ADXL345_TAP_AXES);
298 newTapAxes = oldTapAxes & ~(ADXL345_TAP_X_EN |
299 ADXL345_TAP_Y_EN |
300 ADXL345_TAP_Z_EN);
301 newTapAxes = newTapAxes | tapAxes;
302 ADXL345_SetRegisterValue(ADXL345_TAP_AXES, newTapAxes);
303 ADXL345_SetRegisterValue(ADXL345_DUR, tapDur);
304 ADXL345_SetRegisterValue(ADXL345_LATENT, tapLatent);
305 ADXL345_SetRegisterValue(ADXL345_WINDOW, tapWindow);
306 ADXL345_SetRegisterValue(ADXL345_THRESH_TAP, tapThresh);
307 oldIntMap = ADXL345_GetRegisterValue(ADXL345_INT_MAP);
308 newIntMap = oldIntMap & ~(ADXL345_SINGLE_TAP | ADXL345_DOUBLE_TAP);
309 newIntMap = newIntMap | tapInt;
310 ADXL345_SetRegisterValue(ADXL345_INT_MAP, newIntMap);
311 oldIntEnable = ADXL345_GetRegisterValue(ADXL345_INT_ENABLE);
312 newIntEnable = oldIntEnable & ~(ADXL345_SINGLE_TAP | ADXL345_DOUBLE_TAP);
313 newIntEnable = newIntEnable | tapType;
314 ADXL345_SetRegisterValue(ADXL345_INT_ENABLE, newIntEnable);
315 }
316
317 /***************************************************************************//**
318 * @brief Enables/disables the activity detection.
319 *
320 * @param actOnOff - Enables/disables the activity detection.
321 * Example: 0x0 - disables the activity detection.
322 * 0x1 - enables the activity detection.
323 * @param actAxes - Axes which participate in detecting activity.
324 * Example: 0x0 - disables axes participation.
325 * ADXL345_ACT_X_EN - enables x-axis participation.
326 * ADXL345_ACT_Y_EN - enables y-axis participation.
327 * ADXL345_ACT_Z_EN - enables z-axis participation.
328 * @param actAcDc - Selects dc-coupled or ac-coupled operation.
329 * Example: 0x0 - dc-coupled operation.
330 * ADXL345_ACT_ACDC - ac-coupled operation.
331 * @param actThresh - Threshold value for detecting activity. The scale factor
332 is 62.5 mg/LSB.
333 * @patam actInt - Interrupts pin.
334 * Example: 0x0 - activity interrupts on INT1 pin.
335 * ADXL345_ACTIVITY - activity interrupts on INT2
336 * pin.
337 * @return None.
338 *******************************************************************************/
339 void ADXL345_SetActivityDetection(unsigned char actOnOff,
340 unsigned char actAxes,
341 unsigned char actAcDc,
342 unsigned char actThresh,
343 unsigned char actInt)
344 {
345 unsigned char oldActInactCtl = 0;
346 unsigned char newActInactCtl = 0;
347 unsigned char oldIntMap = 0;
348 unsigned char newIntMap = 0;
349 unsigned char oldIntEnable = 0;
350 unsigned char newIntEnable = 0;
351
352 oldActInactCtl = ADXL345_GetRegisterValue(ADXL345_INT_ENABLE);
353 newActInactCtl = oldActInactCtl & ~(ADXL345_ACT_ACDC |
354 ADXL345_ACT_X_EN |
355 ADXL345_ACT_Y_EN |
356 ADXL345_ACT_Z_EN);
357 newActInactCtl = newActInactCtl | (actAcDc | actAxes);
358 ADXL345_SetRegisterValue(ADXL345_ACT_INACT_CTL, newActInactCtl);
359 ADXL345_SetRegisterValue(ADXL345_THRESH_ACT, actThresh);
360 oldIntMap = ADXL345_GetRegisterValue(ADXL345_INT_MAP);
361 newIntMap = oldIntMap & ~(ADXL345_ACTIVITY);
362 newIntMap = newIntMap | actInt;
363 ADXL345_SetRegisterValue(ADXL345_INT_MAP, newIntMap);
364 oldIntEnable = ADXL345_GetRegisterValue(ADXL345_INT_ENABLE);
365 newIntEnable = oldIntEnable & ~(ADXL345_ACTIVITY);
366 newIntEnable = newIntEnable | (ADXL345_ACTIVITY * actOnOff);
367 ADXL345_SetRegisterValue(ADXL345_INT_ENABLE, newIntEnable);
368 }
369
370 /***************************************************************************//**
371 * @brief Enables/disables the inactivity detection.
372 *
373 * @param inactOnOff - Enables/disables the inactivity detection.
374 * Example: 0x0 - disables the inactivity detection.
375 * 0x1 - enables the inactivity detection.
376 * @param inactAxes - Axes which participate in detecting inactivity.
377 * Example: 0x0 - disables axes participation.
378 * ADXL345_INACT_X_EN - enables x-axis.
379 * ADXL345_INACT_Y_EN - enables y-axis.
380 * ADXL345_INACT_Z_EN - enables z-axis.
381 * @param inactAcDc - Selects dc-coupled or ac-coupled operation.
382 * Example: 0x0 - dc-coupled operation.
383 * ADXL345_INACT_ACDC - ac-coupled operation.
384 * @param inactThresh - Threshold value for detecting inactivity. The scale
385 factor is 62.5 mg/LSB.
386 * @param inactTime - Inactivity time. The scale factor is 1 sec/LSB.
387 * @patam inactInt - Interrupts pin.
388 * Example: 0x0 - inactivity interrupts on INT1 pin.
389 * ADXL345_INACTIVITY - inactivity interrupts on
390 * INT2 pin.
391 *
392 * @return None.
393 *******************************************************************************/
394 void ADXL345_SetInactivityDetection(unsigned char inactOnOff,
395 unsigned char inactAxes,
396 unsigned char inactAcDc,
397 unsigned char inactThresh,
398 unsigned char inactTime,
399 unsigned char inactInt)
400 {
401 unsigned char oldActInactCtl = 0;
402 unsigned char newActInactCtl = 0;
403 unsigned char oldIntMap = 0;
404 unsigned char newIntMap = 0;
405 unsigned char oldIntEnable = 0;
406 unsigned char newIntEnable = 0;
407
408 oldActInactCtl = ADXL345_GetRegisterValue(ADXL345_INT_ENABLE);
409 newActInactCtl = oldActInactCtl & ~(ADXL345_INACT_ACDC |
410 ADXL345_INACT_X_EN |
411 ADXL345_INACT_Y_EN |
412 ADXL345_INACT_Z_EN);
413 newActInactCtl = newActInactCtl | (inactAcDc | inactAxes);
414 ADXL345_SetRegisterValue(ADXL345_ACT_INACT_CTL, newActInactCtl);
415 ADXL345_SetRegisterValue(ADXL345_THRESH_INACT, inactThresh);
416 ADXL345_SetRegisterValue(ADXL345_TIME_INACT, inactTime);
417 oldIntMap = ADXL345_GetRegisterValue(ADXL345_INT_MAP);
418 newIntMap = oldIntMap & ~(ADXL345_INACTIVITY);
419 newIntMap = newIntMap | inactInt;
420 ADXL345_SetRegisterValue(ADXL345_INT_MAP, newIntMap);
421 oldIntEnable = ADXL345_GetRegisterValue(ADXL345_INT_ENABLE);
422 newIntEnable = oldIntEnable & ~(ADXL345_INACTIVITY);
423 newIntEnable = newIntEnable | (ADXL345_INACTIVITY * inactOnOff);
424 ADXL345_SetRegisterValue(ADXL345_INT_ENABLE, newIntEnable);
425 }
426
427 /***************************************************************************//**
428 * @brief Enables/disables the free-fall detection.
429 *
430 * @param ffOnOff - Enables/disables the free-fall detection.
431 * Example: 0x0 - disables the free-fall detection.
432 * 0x1 - enables the free-fall detection.
433 * @param ffThresh - Threshold value for free-fall detection. The scale factor
434 is 62.5 mg/LSB.
435 * @param ffTime - Time value for free-fall detection. The scale factor is
436 5 ms/LSB.
437 * @param ffInt - Interrupts pin.
438 * Example: 0x0 - free-fall interrupts on INT1 pin.
439 * ADXL345_FREE_FALL - free-fall interrupts on
440 * INT2 pin.
441 * @return None.
442 *******************************************************************************/
443 void ADXL345_SetFreeFallDetection(unsigned char ffOnOff,
444 unsigned char ffThresh,
445 unsigned char ffTime,
446 unsigned char ffInt)
447 {
448 unsigned char oldIntMap = 0;
449 unsigned char newIntMap = 0;
450 unsigned char oldIntEnable = 0;
451 unsigned char newIntEnable = 0;
452
453 ADXL345_SetRegisterValue(ADXL345_THRESH_FF, ffThresh);
454 ADXL345_SetRegisterValue(ADXL345_TIME_FF, ffTime);
455 oldIntMap = ADXL345_GetRegisterValue(ADXL345_INT_MAP);
456 newIntMap = oldIntMap & ~(ADXL345_FREE_FALL);
457 newIntMap = newIntMap | ffInt;
458 ADXL345_SetRegisterValue(ADXL345_INT_MAP, newIntMap);
459 oldIntEnable = ADXL345_GetRegisterValue(ADXL345_INT_ENABLE);
460 newIntEnable = oldIntEnable & ~ADXL345_FREE_FALL;
461 newIntEnable = newIntEnable | (ADXL345_FREE_FALL * ffOnOff);
462 ADXL345_SetRegisterValue(ADXL345_INT_ENABLE, newIntEnable);
463 }
464
465 /***************************************************************************//**
466 * @brief Sets an offset value for each axis (Offset Calibration).
467 *
468 * @param xOffset - X-axis's offset.
469 * @param yOffset - Y-axis's offset.
470 * @param zOffset - Z-axis's offset.
471 *
472 * @return None.
473 *******************************************************************************/
474 void ADXL345_SetOffset(unsigned char xOffset,
475 unsigned char yOffset,
476 unsigned char zOffset)
477 {
478 ADXL345_SetRegisterValue(ADXL345_OFSX, xOffset);
479 ADXL345_SetRegisterValue(ADXL345_OFSY, yOffset);
480 ADXL345_SetRegisterValue(ADXL345_OFSZ, yOffset);
481 }
482
483 /***************************************************************************//**
484 * @brief Selects the measurement range.
485 *
486 * @param gRange - Range option.
487 * Example: ADXL345_RANGE_PM_2G - +-2 g
488 * ADXL345_RANGE_PM_4G - +-4 g
489 * ADXL345_RANGE_PM_8G - +-8 g
490 * ADXL345_RANGE_PM_16G - +-16 g
491 * @param fullRes - Full resolution option.
492 * Example: 0x0 - Disables full resolution.
493 * ADXL345_FULL_RES - Enables full resolution.
494 *
495 * @return None.
496 *******************************************************************************/
497 void ADXL345_SetRangeResolution(unsigned char gRange, unsigned char fullRes)
498 {
499 unsigned char oldDataFormat = 0;
500 unsigned char newDataFormat = 0;
501
502 oldDataFormat = ADXL345_GetRegisterValue(ADXL345_DATA_FORMAT);
503 newDataFormat = oldDataFormat & ~(ADXL345_RANGE(0x3) | ADXL345_FULL_RES);
504 newDataFormat = newDataFormat | ADXL345_RANGE(gRange) | fullRes;
505 ADXL345_SetRegisterValue(ADXL345_DATA_FORMAT, newDataFormat);
506 selectedRange = (1 << (gRange + 1));
507 fullResolutionSet = fullRes ? 1 : 0;
508 }