First commit
[hackover2013-badge-firmware.git] / drivers / sensors / adxl345 / adxl345.c
1 /**************************************************************************/
2 /*!
3 @file adxl345.c
4 @author K. Townsend (microBuilder.eu)
5
6 @brief Drivers for Analog Devices ADXL345 Accelerometer
7
8 @section DESCRIPTION
9
10 The ADXL345 is a digital accelerometer with 13-bit resolution, capable
11 of measuring up to +/-16g. This driver communicate using I2C.
12
13 @section LICENSE
14
15 Software License Agreement (BSD License)
16
17 Copyright (c) 2012, K. Townsend
18 All rights reserved.
19
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.
30
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.
41 */
42 /**************************************************************************/
43 #include "adxl345.h"
44 #include "core/systick/systick.h"
45
46 extern volatile uint8_t I2CMasterBuffer[I2C_BUFSIZE];
47 extern volatile uint8_t I2CSlaveBuffer[I2C_BUFSIZE];
48 extern volatile uint32_t I2CReadLength, I2CWriteLength;
49
50 static bool _adxl345Initialised = false;
51
52 /**************************************************************************/
53 /*!
54 @brief Sends a single command byte over I2C
55 */
56 /**************************************************************************/
57 static adxl345Error_t adxl345Write8 (uint8_t reg, uint8_t value)
58 {
59 adxl345Error_t error = ADXL345_ERROR_OK;
60
61 // Clear write buffers
62 uint32_t i;
63 for ( i = 0; i < I2C_BUFSIZE; i++ )
64 {
65 I2CMasterBuffer[i] = 0x00;
66 }
67
68 I2CWriteLength = 3;
69 I2CReadLength = 0;
70 I2CMasterBuffer[0] = ADXL345_ADDRESS; // I2C device address
71 I2CMasterBuffer[1] = reg; // Register
72 I2CMasterBuffer[2] = value;
73 i2cEngine();
74
75 // ToDo: Add in proper I2C error-checking
76 return error;
77 }
78
79 /**************************************************************************/
80 /*!
81 @brief Reads a 16 bit values over I2C
82 */
83 /**************************************************************************/
84 static adxl345Error_t adxl345Read8(uint8_t reg, uint8_t *value)
85 {
86 adxl345Error_t error = ADXL345_ERROR_OK;
87
88 // Clear write buffers
89 uint32_t i;
90 for ( i = 0; i < I2C_BUFSIZE; i++ )
91 {
92 I2CMasterBuffer[i] = 0x00;
93 }
94
95 I2CWriteLength = 2;
96 I2CReadLength = 1;
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;
101 i2cEngine();
102
103 // Shift values to create properly formed integer
104 *value = I2CSlaveBuffer[0];
105
106 // ToDo: Add in proper I2C error-checking
107 return error;
108 }
109
110 /**************************************************************************/
111 /*!
112 @brief Read the device ID (can be used to check connection)
113 */
114 /**************************************************************************/
115 adxl345Error_t adxl345GetDeviceID(uint8_t *id)
116 {
117 adxl345Error_t error = ADXL345_ERROR_OK;
118
119 // Check device ID register
120 error = adxl345Read8(ADXL345_REG_DEVID, id);
121 return error;
122 }
123
124 /**************************************************************************/
125 /*!
126 @brief Initialises the I2C block
127 */
128 /**************************************************************************/
129 adxl345Error_t adxl345Init(void)
130 {
131 uint8_t devid = 0x00;
132 adxl345Error_t error = ADXL345_ERROR_OK;
133
134 // Initialise I2C
135 if (i2cInit(I2CMASTER) == false)
136 {
137 return ADXL345_ERROR_I2CINIT; /* Fatal error */
138 }
139
140 // Check device ID register to see if everything is properly connected
141 error = adxl345Read8(ADXL345_REG_DEVID, &devid);
142 if (error)
143 {
144 return error;
145 }
146 else if (devid != 0xE5)
147 {
148 return ADXL345_ERROR_NOCONNECTION;
149 }
150
151 // Enable measurements
152 error = adxl345Write8(ADXL345_REG_POWER_CTL, 0x08);
153 if (error)
154 {
155 return error;
156 }
157
158 _adxl345Initialised = true;
159 return error;
160 }
161
162 /**************************************************************************/
163 /*!
164 @brief Gets the latest X/Y/Z values
165 */
166 /**************************************************************************/
167 adxl345Error_t adxl345GetXYZ(int16_t *x, int16_t *y, int16_t *z)
168 {
169 adxl345Error_t error = ADXL345_ERROR_OK;
170
171 // Clear write buffers
172 uint32_t i;
173 for ( i = 0; i < I2C_BUFSIZE; i++ )
174 {
175 I2CMasterBuffer[i] = 0x00;
176 }
177
178 I2CWriteLength = 2;
179 I2CReadLength = 6;
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;
184 i2cEngine();
185
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]);
190
191 // ToDo: Add in proper I2C error-checking
192 return error;
193 }
194
This page took 0.06435 seconds and 5 git commands to generate.