Contiki-Inga 3.x
adxl345.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, TU Braunschweig.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  */
29 
30 /**
31  * \file
32  * ADXL345 Accelerometer interface definitions
33  * \author
34  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
35  * Enrico Jörns <joerns@ibr.cs.tu-bs.de>
36  */
37 
38 /**
39  * \addtogroup inga_sensors_driver
40  * @{
41  */
42 
43 /**
44  * \defgroup adxl345_interface ADXL345 Accelerometer Interface
45  *
46  * \section Registers and Bits
47  *
48  * All registers are named \c ADXL345_name_REG where \em name is the name
49  * of the register as described in the sensors datasheet.
50  *
51  * All bit positions are named \c ADXL345_name for single bit entries
52  * and ADXL345_name_L for multiple bit entries, where the position always
53  * means the entries LSB.
54  * @{
55  */
56 
57 #ifndef ADXL345_H_
58 #define ADXL345_H_
59 
60 #include "../dev/mspi.h"
61 #include <stdio.h>
62 #include <util/delay.h>
63 
64 /*!
65  * SPI device order.
66  * The chip select number where the
67  * ADXL345 is connected to the BCD-decimal decoder
68  */
69 #define ADXL345_CS 2
70 
71 /** Device ID Register */
72 #define ADXL345_DEVICE_ID_REG 0x00
73 
74 /** \name BW_RATE register/bits
75  * \{ */
76 /** ADXL Data Rate and Power Mode Control Register. */
77 #define ADXL345_BW_RATE_REG 0x2C
78 /** Low power bit pos. */
79 #define ADXL345_LOW_POWER 4
80 /** Rate bits [4] pos. */
81 #define ADXL345_RATE_L 0
82 /** \} */
83 
84 /** \name POWER_CTL register/bits
85  * \{ */
86 /** ADXL Power Control Register. */
87 #define ADXL345_POWER_CTL_REG 0x2D
88 /** Link bit pos.*/
89 #define ADXL345_LINK 5
90 /** AUTO_SLEEP bit pos.*/
91 #define ADXL345_AUTO_SLEEP 4
92 /** Measure bit pos.*/
93 #define ADXL345_MEASURE 3
94 /** Sleep bit pos.*/
95 #define ADXL345_SLEEP 2
96 /** Wakeup bits [2] LSB pos. */
97 #define ADXL345_WAKEUP_L 0
98 /** \} */
99 
100 /** \name DATA_FORMAT register/bits
101  * \{ */
102 /** ADXL Data Format Register Register. */
103 #define ADXL345_DATA_FORMAT_REG 0x31
104 /** SELF_TEST bit pos. */
105 #define ADXL345_SELF_TEST 7
106 /** SPI bit pos. */
107 #define ADXL345_SPI 6
108 /** INT_INVERT bit pos. */
109 #define ADXL345_INT_INVERT 5
110 /** FULL_RES bit pos. */
111 #define ADXL345_FULL_RES 3
112 /** Justify bit pos. */
113 #define ADXL345_JUSTIFY 2
114 /** Range bits [2] LSB pos. */
115 #define ADXL345_RANGE_L 0
116 /** \} */
117 
118 /** \name FIFO_CTL register/bits
119  * \{ */
120 /** FIFO control register */
121 #define ADXL345_FIFO_CTL_REG 0x38
122 /** FIFO_MODE bits [2] LSB pos. */
123 #define ADXL345_FIFO_MODE_L 6
124 /** Trigger bit pos. */
125 #define ADXL345_TRIGGER 5
126 /** Samples bits [5] LSB pos. */
127 #define ADXL345_SAMPLES_L 0
128 /** \} */
129 
130 /** \name FIFO_STATUS register/bits
131  * \{ */
132 /** FIFO status register */
133 #define ADXL345_FIFO_STATUS_REG 0x39
134 /** FIFO_TRIG bit pos. */
135 #define ADXL345_FIFO_TRIG 7
136 /** Entries bits [6] LSB pos. */
137 #define ADXL345_ENTRIES_L 0
138 /** \} */
139 
140 /** x Acceleration Data register (high) */
141 #define ADXL345_OUTX_LOW_REG 0x32
142 /** x Acceleration Data register (low) */
143 #define ADXL345_OUTX_HIGH_REG 0x33
144 /** y Acceleration Data register (high) */
145 #define ADXL345_OUTY_LOW_REG 0x34
146 /** y Acceleration Data register (low) */
147 #define ADXL345_OUTY_HIGH_REG 0x35
148 /** z Acceleration Data register (high) */
149 #define ADXL345_OUTZ_LOW_REG 0x36
150 /** z Acceleration Data register (low) */
151 #define ADXL345_OUTZ_HIGH_REG 0x37
152 
153 
154 /**
155  * \name g range settings
156  * \{
157  */
158 /** +/- 2g, 256 LSB/g */
159 #define ADXL345_MODE_2G (0x0 << ADXL345_RANGE_L)
160 /** +/- 4g, 128 LSB/g */
161 #define ADXL345_MODE_4G (0x1 << ADXL345_RANGE_L)
162 /** +/- 8g, 64 LSB/g */
163 #define ADXL345_MODE_8G (0x2 << ADXL345_RANGE_L)
164 /** +/- 16g, 32 LSB/g */
165 #define ADXL345_MODE_16G (0x3 << ADXL345_RANGE_L)
166 /** \} */
167 
168 /**
169  * \name FIFO mode settings
170  * \{
171  */
172 #define ADXL345_MODE_BYPASS (0x0 << ADXL345_FIFO_MODE_L)
173 #define ADXL345_MODE_FIFO (0x1 << ADXL345_FIFO_MODE_L)
174 #define ADXL345_MODE_STREAM (0x2 << ADXL345_FIFO_MODE_L)
175 #define ADXL345_MODE_TRIGGER (0x3 << ADXL345_FIFO_MODE_L)
176 /** \} */
177 
178 /**
179  * \name Power mode settings
180  * \{ */
181 #define ADXL345_PMODE_SLEEP 1
182 #define ADXL345_PMODE_WAKEUP 3
183 #define ADXL345_PMODE_STANDBY 4
184 /** \} */
185 
186 /**
187  * \name Values for output data rate
188  * \see ADXL345_BW_RATE_REG
189  * \{
190  */
191 /* ODR: 0.1 Hz, bandwith: 0.05Hz, I_DD: 23 µA */
192 #define ADXL345_ODR_0HZ10 (0x0 << ADXL345_RATE_L)
193 /* ODR: 0.2 Hz, bandwith: 0.1Hz, I_DD: 23 µA */
194 #define ADXL345_ODR_0HZ20 (0x1 << ADXL345_RATE_L)
195 /* ODR: 0.39 Hz, bandwith: 0.2Hz, I_DD: 23 µA */
196 #define ADXL345_ODR_0HZ39 (0x2 << ADXL345_RATE_L)
197 /* ODR: 0.78 Hz, bandwith: 0.39Hz, I_DD: 23 µA */
198 #define ADXL345_ODR_0HZ78 (0x3 << ADXL345_RATE_L)
199 /* ODR: 1.56 Hz, bandwith: x.xxHz, I_DD: 34 µA */
200 #define ADXL345_ODR_1HZ56 (0x4 << ADXL345_RATE_L)
201 /* ODR: 3.13 Hz, bandwith: x.xxHz, I_DD: 40 µA */
202 #define ADXL345_ODR_3HZ13 (0x5 << ADXL345_RATE_L)
203 /* ODR: 6.25 Hz, bandwith: x.xxHz, I_DD: 45 µA */
204 #define ADXL345_ODR_6HZ25 (0x6 << ADXL345_RATE_L)
205 /* ODR: 12.5 Hz, bandwith: x.xxHz, I_DD: 50 µA */
206 #define ADXL345_ODR_12HZ5 (0x7 << ADXL345_RATE_L)
207 /* ODR: 25 Hz, bandwith: x.xxHz, I_DD: 60 µA */
208 #define ADXL345_ODR_25HZ (0x8 << ADXL345_RATE_L)
209 /* ODR: 50 Hz, bandwith: x.xxHz, I_DD: 90 µA */
210 #define ADXL345_ODR_50HZ (0x9 << ADXL345_RATE_L)
211 /* ODR: 100 Hz, bandwith: x.xxHz, I_DD: 140 µA */
212 #define ADXL345_ODR_100HZ (0xA << ADXL345_RATE_L)
213 /* ODR: 200 Hz, bandwith: x.xxHz, I_DD: 140 µA */
214 #define ADXL345_ODR_200HZ (0xB << ADXL345_RATE_L)
215 /* ODR: 400 Hz, bandwith: x.xxHz, I_DD: 140 µA */
216 #define ADXL345_ODR_400HZ (0xC << ADXL345_RATE_L)
217 /* ODR: 800 Hz, bandwith: x.xxHz, I_DD: 140 µA */
218 #define ADXL345_ODR_800HZ (0xD << ADXL345_RATE_L)
219 /* ODR: 1600 Hz, bandwith: x.xxHz, I_DD: 90 µA */
220 #define ADXL345_ODR_1600HZ (0xE << ADXL345_RATE_L)
221 /* ODR: 3200 Hz, bandwith: x.xxHz, I_DD: 140 µA */
222 #define ADXL345_ODR_3200HZ (0xF << ADXL345_RATE_L)
223 
224 /** \} */
225 
226 typedef struct {
227  int16_t x;
228  int16_t y;
229  int16_t z;
230 } acc_data_t;
231 
232 /**
233  * \brief Checks if ADXL345 is available
234  *
235  * \retval 1 available
236  * \retval 0 not availabe
237  */
238 int8_t adxl345_available(void);
239 
240 /**
241  * \brief Initialize the ADXL345 accelerometer
242  *
243  * Default settings are: TODO
244  *
245  * \retval 0 Initialization succeeded
246  * \retval -1 Initialization failed
247  */
248 int8_t adxl345_init(void);
249 
250 /**
251  * Deinitilizes the ADX345 accelerometer.
252  *
253  * I.e. sets it in standby mode.
254  */
255 void adxl345_deinit(void);
256 
257 
258 /**
259  * Checks whether the device is ready or not by reading the ID.
260  * \retval 1 ready
261  * \retval 0 not ready
262  */
263 int8_t adxl345_ready(void);
264 
265 /**
266  *
267  * @param range
268  */
269 void adxl345_set_g_range(uint8_t range);
270 
271 /**
272  *
273  * @param rate
274  */
275 void adxl345_set_data_rate(uint8_t rate);
276 
277 /**
278  *
279  * @param mode One of ADXL345_MODE_BYPASS, ADXL345_MODE_FIFO, ADXL345_MODE_STREAM, ADXL345_MODE_TRIGGER
280  */
281 void adxl345_set_fifomode(uint8_t mode);
282 
283 /**
284  *
285  * @param mode One of ADXL345_PMODE_SLEEP, ADXL345_PMODE_WAKEUP, ADXL345_PMODE_STANDBY
286  */
287 void adxl345_set_powermode(uint8_t mode);
288 
289 /**
290  * Returns the current fill level of the internal FIFO (when operating in FIFO/Stream mode).
291  * @return Fill level [0 - 33]
292  */
293 uint8_t adxl345_get_fifo_level();
294 
295 /**
296  * \brief This function returns the current measured acceleration
297  * at the x-axis of the adxl345
298  *
299  * \return current x-axis acceleration value
300  */
301 int16_t adxl345_get_x(void);
302 
303 /**
304  * \brief This function returns the current measured acceleration
305  * at the y-axis of the adxl345
306  *
307  * \return current y-axis acceleration value
308  */
309 int16_t adxl345_get_y(void);
310 
311 /**
312  * \brief This function returns the current measured acceleration
313  * at the z-axis of the adxl345
314  *
315  * \return current z-axis acceleration value
316  */
317 int16_t adxl345_get_z(void);
318 
319 /**
320  * \brief This function returns the current measured acceleration
321  * of all axis (x,y,z)
322  * \note This is more efficient than reading each axis individually
323  *
324  * \return current acceleration value of all axis
325  */
326 acc_data_t adxl345_get(void);
327 
328 /** Convert raw value to mg value */
329 #define adxl345_raw_to_mg(raw) (raw * 62) / 16; // approx 3.9 scale factor
330 
331 
332 /**
333  * \brief This function writes data to the given register
334  * of the ADXL345
335  * \param reg The register address
336  * \param data The data value
337  */
338 void adxl345_write(uint8_t reg, uint8_t data);
339 
340 /**
341  * \brief This function reads from the given register
342  * of the ADXL345
343  * \param reg The register address
344  * \return The data value
345  */
346 uint8_t adxl345_read(uint8_t reg);
347 
348 /** @} */
349 /** @} */
350 
351 #endif /* ADXL345_H_ */