1 /**************************************************************************/
4 @author K. Townsend (microBuilder.eu)
6 @brief Drivers for Analog Devices ADXL345 Accelerometer
10 The ADXL345 is a digital accelerometer with 13-bit resolution, capable
11 of measuring up to +/-16g. This driver communicate using I2C.
15 Software License Agreement (BSD License)
17 Copyright (c) 2012, K. Townsend
20 Redistribution and use in source and binary forms, with or without
21 modification, are permitted provided that the following conditions are met:
22 1. Redistributions of source code must retain the above copyright
23 notice, this list of conditions and the following disclaimer.
24 2. Redistributions in binary form must reproduce the above copyright
25 notice, this list of conditions and the following disclaimer in the
26 documentation and/or other materials provided with the distribution.
27 3. Neither the name of the copyright holders nor the
28 names of its contributors may be used to endorse or promote products
29 derived from this software without specific prior written permission.
31 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ''AS IS'' AND ANY
32 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
33 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
34 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER BE LIABLE FOR ANY
35 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
36 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
37 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
38 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
39 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
40 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42 /**************************************************************************/
44 #include "core/systick/systick.h"
46 extern volatile uint8_t I2CMasterBuffer
[I2C_BUFSIZE
];
47 extern volatile uint8_t I2CSlaveBuffer
[I2C_BUFSIZE
];
48 extern volatile uint32_t I2CReadLength
, I2CWriteLength
;
50 static bool _adxl345Initialised
= false;
52 /**************************************************************************/
54 @brief Sends a single command byte over I2C
56 /**************************************************************************/
57 static adxl345Error_t
adxl345Write8 (uint8_t reg
, uint8_t value
)
59 adxl345Error_t error
= ADXL345_ERROR_OK
;
61 // Clear write buffers
63 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
65 I2CMasterBuffer
[i
] = 0x00;
70 I2CMasterBuffer
[0] = ADXL345_ADDRESS
; // I2C device address
71 I2CMasterBuffer
[1] = reg
; // Register
72 I2CMasterBuffer
[2] = value
;
75 // ToDo: Add in proper I2C error-checking
79 /**************************************************************************/
81 @brief Reads a 16 bit values over I2C
83 /**************************************************************************/
84 static adxl345Error_t
adxl345Read8(uint8_t reg
, uint8_t *value
)
86 adxl345Error_t error
= ADXL345_ERROR_OK
;
88 // Clear write buffers
90 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
92 I2CMasterBuffer
[i
] = 0x00;
97 I2CMasterBuffer
[0] = ADXL345_ADDRESS
; // I2C device address
98 I2CMasterBuffer
[1] = reg
; // Command register
99 // Append address w/read bit
100 I2CMasterBuffer
[2] = ADXL345_ADDRESS
| ADXL345_READBIT
;
103 // Shift values to create properly formed integer
104 *value
= I2CSlaveBuffer
[0];
106 // ToDo: Add in proper I2C error-checking
110 /**************************************************************************/
112 @brief Read the device ID (can be used to check connection)
114 /**************************************************************************/
115 adxl345Error_t
adxl345GetDeviceID(uint8_t *id
)
117 adxl345Error_t error
= ADXL345_ERROR_OK
;
119 // Check device ID register
120 error
= adxl345Read8(ADXL345_REG_DEVID
, id
);
124 /**************************************************************************/
126 @brief Initialises the I2C block
128 /**************************************************************************/
129 adxl345Error_t
adxl345Init(void)
131 uint8_t devid
= 0x00;
132 adxl345Error_t error
= ADXL345_ERROR_OK
;
135 if (i2cInit(I2CMASTER
) == false)
137 return ADXL345_ERROR_I2CINIT
; /* Fatal error */
140 // Check device ID register to see if everything is properly connected
141 error
= adxl345Read8(ADXL345_REG_DEVID
, &devid
);
146 else if (devid
!= 0xE5)
148 return ADXL345_ERROR_NOCONNECTION
;
151 // Enable measurements
152 error
= adxl345Write8(ADXL345_REG_POWER_CTL
, 0x08);
158 _adxl345Initialised
= true;
162 /**************************************************************************/
164 @brief Gets the latest X/Y/Z values
166 /**************************************************************************/
167 adxl345Error_t
adxl345GetXYZ(int16_t *x
, int16_t *y
, int16_t *z
)
169 adxl345Error_t error
= ADXL345_ERROR_OK
;
171 // Clear write buffers
173 for ( i
= 0; i
< I2C_BUFSIZE
; i
++ )
175 I2CMasterBuffer
[i
] = 0x00;
180 I2CMasterBuffer
[0] = ADXL345_ADDRESS
; // I2C device address
181 I2CMasterBuffer
[1] = ADXL345_REG_DATAX0
;
182 // Append address w/read bit
183 I2CMasterBuffer
[2] = ADXL345_ADDRESS
| ADXL345_READBIT
;
186 // Shift values to create properly formed integer
187 *x
= (I2CSlaveBuffer
[1] << 8) | (I2CSlaveBuffer
[0]);
188 *y
= (I2CSlaveBuffer
[3] << 8) | (I2CSlaveBuffer
[2]);
189 *z
= (I2CSlaveBuffer
[5] << 8) | (I2CSlaveBuffer
[4]);
191 // ToDo: Add in proper I2C error-checking