Contiki 2.5
acc-adxl345.h
Go to the documentation of this file.
1 /* Copyright (c) 2010, Ulf Kulau
2  *
3  * Permission is hereby granted, free of charge, to any person
4  * obtaining a copy of this software and associated documentation
5  * files (the "Software"), to deal in the Software without
6  * restriction, including without limitation the rights to use,
7  * copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following
10  * conditions:
11  *
12  * The above copyright notice and this permission notice shall be
13  * included in all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
17  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
19  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
20  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
22  * OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
25 /**
26  * \addtogroup Device Interfaces
27  *
28  * \section about About
29  * The interfaces are the second layer above the hardware and aim to separate
30  * the discrete hardware from the user application layer.
31  *
32  * \section usage Usage
33  *
34  * The interfaces are compatible with contiki os and can be directly used
35  * in a process environment.
36  *
37  * \section lic License
38  *
39  * <pre>Copyright (c) 2009, Ulf Kulau <kulau@ibr.cs.tu-bs.de>
40  *
41  * Permission is hereby granted, free of charge, to any person
42  * obtaining a copy of this software and associated documentation
43  * files (the "Software"), to deal in the Software without
44  * restriction, including without limitation the rights to use,
45  * copy, modify, merge, publish, distribute, sublicense, and/or sell
46  * copies of the Software, and to permit persons to whom the
47  * Software is furnished to do so, subject to the following
48  * conditions:
49  *
50  * The above copyright notice and this permission notice shall be
51  * included in all copies or substantial portions of the Software.
52  *
53  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
54  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
55  * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
56  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
57  * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
58  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
59  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
60  * OTHER DEALINGS IN THE SOFTWARE.</pre>
61  *
62  * @{
63  *
64  * \defgroup adxl345_interface Accelerometer Interface (ADXL345)
65  *
66  * <p>In previous Projects we evaluated some accelerometer sensors and the digital ADXL345 is
67  * the best choice for an sensor node application. All accelerometers have nearly the same
68  * performance characteristics, but he ADXL345 is very cheap and has the lowest power
69  * consumption.
70  * </p>
71  * @{
72  *
73  */
74 
75 /**
76  * \file
77  * ADXL345 Accelerometer interface definitions
78  * \author
79  * Ulf Kulau <kulau@ibr.cs.tu-bs.de>
80  */
81 
82 #ifndef ADXL345_H_
83 #define ADXL345_H_
84 
85 #include "../drv/mspi-drv.h"
86 #include <stdio.h>
87 #include <util/delay.h>
88 
89 /*!
90  * SPI device order. The chip select number where the
91  * ADXL345 is connected to the BCD-decimal decoder
92  */
93 #define ADXL345_CS 2
94 
95 /*!
96  * ADXL Data Format Register
97  */
98 #define ADXL345_DATA_FORMAT_REG 0x31
99 /*!
100  * ADXL Data Format Register Data:
101  *<table border="1">
102  * <tr>
103  * <th> D7 </th>
104  * <th> D6 </th>
105  * <th> D5 </th>
106  * <th> D4 </th>
107  * <th> D3 </th>
108  * <th> D2 </th>
109  * <th> D1 </th>
110  * <th> D0 </th>
111  * </tr>
112  * <tr>
113  * <td> SELF_TEST </td>
114  * <td> SPI </td>
115  * <td> INT_INVERT </td>
116  * <td> 0 </td>
117  * <td> FULL_RES </td>
118  * <td> Justify </td>
119  * <td> Range </td>
120  * <td> Range </td>
121  * </tr>
122  * </table>
123  * \note For further information use the ADXL345 Datasheet
124  * \note Default value: 0x00
125  */
126 #define ADXL345_DATA_FORMAT_DATA 0x00
127 
128 /*!
129  * ADXL Power Control Register
130  */
131 #define ADXL345_POWER_CTL_REG 0x2D
132 /*!
133  * ADXL Power Control Register Data:
134  *<table border="1">
135  * <tr>
136  * <th> D7 </th>
137  * <th> D6 </th>
138  * <th> D5 </th>
139  * <th> D4 </th>
140  * <th> D3 </th>
141  * <th> D2 </th>
142  * <th> D1 </th>
143  * <th> D0 </th>
144  * </tr>
145  * <tr>
146  * <td> 0 </td>
147  * <td> 0 </td>
148  * <td> Link </td>
149  * <td> Auto Sleep </td>
150  * <td> Measure </td>
151  * <td> Sleep </td>
152  * <td> Wakeup </td>
153  * <td> Wakeup </td>
154  * </tr>
155  * </table>
156  * \note For further information use the ADXL345 Datasheet
157  * \note Default value: 0x00
158  */
159 #define ADXL345_POWER_CTL_DATA 0x08
160 
161 /*!
162  * ADXL Data Rate and Power Mode Control Register
163  */
164 #define ADXL345_BW_RATE_REG 0x2C
165 /*!
166  * ADXL Data Rate and Power Mode Control Register Data:
167  *<table border="1">
168  * <tr>
169  * <th> D7 </th>
170  * <th> D6 </th>
171  * <th> D5 </th>
172  * <th> D4 </th>
173  * <th> D3 </th>
174  * <th> D2 </th>
175  * <th> D1 </th>
176  * <th> D0 </th>
177  * </tr>
178  * <tr>
179  * <td> 0 </td>
180  * <td> 0 </td>
181  * <td> 0 </td>
182  * <td> LOW_POWER </td>
183  * <td> Rate_3 </td>
184  * <td> Rate_2 </td>
185  * <td> Rate_1 </td>
186  * <td> Rate_0 </td>
187  * </tr>
188  * </table>
189  * \note For further information use the ADXL345 Datasheet
190  * \note Default value: 0x0A
191  */
192 #define ADXL345_BW_RATE_DATA 0x0A
193 
194 /*\cond*/
195 /*Device ID Register and value*/
196 #define ADXL345_DEVICE_ID_REG 0x00
197 #define ADXL345_DEVICE_ID_DATA 0xE5
198 /*Acceleration Data register (high/low)*/
199 #define ADXL345_OUTX_LOW_REG 0x32
200 #define ADXL345_OUTX_HIGH_REG 0x33
201 #define ADXL345_OUTY_LOW_REG 0x34
202 #define ADXL345_OUTY_HIGH_REG 0x35
203 #define ADXL345_OUTZ_LOW_REG 0x36
204 #define ADXL345_OUTZ_HIGH_REG 0x37
205 /*\endcond*/
206 
207 
208 typedef struct {
209  uint16_t acc_x_value;
210  uint16_t acc_y_value;
211  uint16_t acc_z_value;
212 }acc_data_t;
213 
214 /**
215  * \brief Initialize the ADXL345 Acceleration Sensor
216  *
217  * The specific sensor settings are provided by
218  * <ul>
219  * <li> ADXL345_DATA_FORMAT_DATA
220  * <li> ADXL345_POWER_CTL_DATA
221  * <li> ADXL345_BW_RATE_DATA
222  * </ul>
223  * \return <ul>
224  * <li> 0 ADXL345 available
225  * <li> -1 ADXL345 not available
226  * </ul>
227  */
228 int8_t adxl345_init(void);
229 
230 /**
231  * \brief This function returns the current measured acceleration
232  * at the x-axis of the adxl345
233  *
234  * \return current x-axis acceleration value
235  */
236 uint16_t adxl345_get_x_acceleration(void);
237 
238 /**
239  * \brief This function returns the current measured acceleration
240  * at the y-axis of the adxl345
241  *
242  * \return current y-axis acceleration value
243  */
244 uint16_t adxl345_get_y_acceleration(void);
245 
246 /**
247  * \brief This function returns the current measured acceleration
248  * at the z-axis of the adxl345
249  *
250  * \return current z-axis acceleration value
251  */
252 uint16_t adxl345_get_z_acceleration(void);
253 
254 
255 /**
256  * \brief This function returns the current measured acceleration
257  * of all axis (x,y,z)
258  *
259  * \return current acceleration value of all axis
260  */
261 acc_data_t adxl345_get_acceleration(void);
262 
263  /**
264  * \brief This function writes data to the given register
265  * of the ADXL345
266  * \param reg The register address
267  * \param data The data value
268  */
269 void adxl345_write(uint8_t reg, uint8_t data);
270 
271 /**
272  * \brief This function reads from the given register
273  * of the ADXL345
274  * \param reg The register address
275  * \return The data value
276  */
277 uint8_t adxl345_read(uint8_t reg);
278 
279 
280 #endif /* ADXL345_H_ */