Contiki-NG
adxl345.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2010, Swedish Institute of Computer Science.
3  * Copyright (c) 2016, Zolertia <http://www.zolertia.com>
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * 3. Neither the name of the Institute nor the names of its contributors
15  * may be used to endorse or promote products derived from this software
16  * without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  * This file is part of the Contiki operating system.
31  *
32  */
33 /*---------------------------------------------------------------------------*/
34 /**
35  * \file
36  * Device drivers header file for adxl345 accelerometer in Zolertia Z1.
37  * \author
38  * Marcus Lundén, SICS <mlunden@sics.se>
39  * Enric Calvo, Zolertia <ecalvo@zolertia.com>
40  * Antonio Lignan, Zolertia <alinan@zolertia.com>
41  */
42 /*---------------------------------------------------------------------------*/
43 #ifndef ADXL345_H_
44 #define ADXL345_H_
45 #include <stdio.h>
46 #include "dev/i2cmaster.h"
47 #include "lib/sensors.h"
48 /*---------------------------------------------------------------------------*/
49 /* Used in accm_read_axis(), eg accm_read_axis(X_AXIS) */
50 enum ADXL345_AXIS {
51  X_AXIS = 0,
52  Y_AXIS = 2,
53  Z_AXIS = 4,
54 };
55 /* -------------------------------------------------------------------------- */
56 /* Init the accelerometer: ports, pins, registers, interrupts (none enabled),
57  * I2C, default threshold values etc.
58  */
59 void accm_init(void);
60 
61 /* Read an axis of the accelerometer (x, y or z). Return value is a signed 10
62  * bit int.
63  * The resolution of the acceleration measurement can be increased up to 13 bit,
64  * but will change the data format of this read out. Refer to the data sheet if
65  * so is wanted/needed.
66  */
67 int16_t accm_read_axis(enum ADXL345_AXIS axis);
68 
69 /* Sets the g-range, ie the range the accelerometer measures (ie 2g means -2 to
70  * +2 g on every axis). Possible values:
71  * - ADXL345_RANGE_2G
72  * - ADXL345_RANGE_4G
73  * - ADXL345_RANGE_8G
74  * - ADXL345_RANGE_16G
75  */
76 int accm_set_grange(uint8_t grange);
77 
78 /* Map interrupt (FF, tap, dbltap etc) to interrupt pin (IRQ_INT1, IRQ_INT2).
79  * This must come after accm_init() as the registers will otherwise be
80  * overwritten.
81  */
82 int accm_set_irq(uint8_t int1, uint8_t int2);
83 
84 /* Macros for setting the pointers to callback functions from the interrupts.
85  * The function will be called with an uint8_t as parameter, containing the
86  * interrupt flag register from the ADXL345. That way, several interrupts can be
87  * mapped to the same pin and be read
88  */
89 #define ACCM_REGISTER_INT1_CB(ptr) accm_int1_cb = ptr;
90 #define ACCM_REGISTER_INT2_CB(ptr) accm_int2_cb = ptr;
91 /* -------------------------------------------------------------------------- */
92 /* Application definitions, change if required by application. */
93 
94 /* Time after an interrupt that subsequent interrupts are suppressed. Should
95  * later be turned into one specific time per type of interrupt (tap, freefall.
96  * etc)
97  */
98 #define SUPPRESS_TIME_INT1 CLOCK_SECOND/4
99 #define SUPPRESS_TIME_INT2 CLOCK_SECOND/4
100 
101 /* Suggested defaults according to the data sheet etc */
102 #define ADXL345_THRESH_TAP_DEFAULT 0x48 /* 4.5g (0x30 == 3.0g) */
103 #define ADXL345_OFSX_DEFAULT 0x00 /* for calibration only */
104 #define ADXL345_OFSY_DEFAULT 0x00
105 #define ADXL345_OFSZ_DEFAULT 0x00
106 #define ADXL345_DUR_DEFAULT 0x20 /* 20 ms (datasheet: 10ms++) */
107 #define ADXL345_LATENT_DEFAULT 0x50 /* 100 ms (datasheet: 20ms++) */
108 #define ADXL345_WINDOW_DEFAULT 0xFF /* 320 ms (datasheet: 80ms++) */
109 #define ADXL345_THRESH_ACT_DEFAULT 0x15 /* 1.3g (62.5 mg/LSB) */
110 #define ADXL345_THRESH_INACT_DEFAULT 0x08 /* 0.5g (62.5 mg/LSB) */
111 #define ADXL345_TIME_INACT_DEFAULT 0x02 /* 2 s (1 s/LSB) */
112 #define ADXL345_ACT_INACT_CTL_DEFAULT 0xFF /* all axis, ac-coupled */
113 #define ADXL345_THRESH_FF_DEFAULT 0x09 /* 563 mg */
114 #define ADXL345_TIME_FF_DEFAULT 0x20 /* 60 ms */
115 #define ADXL345_TAP_AXES_DEFAULT 0x07 /* all axis, no suppression */
116 
117 #define ADXL345_BW_RATE_DEFAULT (0x00 | ADXL345_SRATE_100) /* 100 Hz */
118 /* link bit set, no autosleep, start normal measuring */
119 #define ADXL345_POWER_CTL_DEFAULT 0x28
120 #define ADXL345_INT_ENABLE_DEFAULT 0x00 /* no interrupts enabled */
121 #define ADXL345_INT_MAP_DEFAULT 0x00 /* all mapped to int_1 */
122 
123 /* XXX NB: In the data format register, data format of axis readings is chosen
124  * between left or right justify. This affects the position of the MSB/LSB and is
125  * different depending on g-range and resolution. If changed, make sure this is
126  * reflected in the _read_axis() function. Also, the resolution can be increased
127  * from 10 bit to at most 13 bit, but this also changes position of MSB etc on data
128  * format so check this in read_axis() too.
129  */
130 /* right-justify, 2g, 10-bit mode, int is active high */
131 #define ADXL345_DATA_FORMAT_DEFAULT (0x00 | ADXL345_RANGE_2G)
132 #define ADXL345_FIFO_CTL_DEFAULT 0x00 /* FIFO bypass mode */
133 
134 /* -------------------------------------------------------------------------- */
135 /* Reference definitions, should not be changed */
136 /* adxl345 slave address */
137 #define ADXL345_ADDR 0x53
138 
139 /* ADXL345 registers */
140 #define ADXL345_DEVID 0x00
141 /* registers 0x01 to 0x1C are reserved, do not access */
142 #define ADXL345_THRESH_TAP 0x1D
143 #define ADXL345_OFSX 0x1E
144 #define ADXL345_OFSY 0x1F
145 #define ADXL345_OFSZ 0x20
146 #define ADXL345_DUR 0x21
147 #define ADXL345_LATENT 0x22
148 #define ADXL345_WINDOW 0x23
149 #define ADXL345_THRESH_ACT 0x24
150 #define ADXL345_THRESH_INACT 0x25
151 #define ADXL345_TIME_INACT 0x26
152 #define ADXL345_ACT_INACT_CTL 0x27
153 #define ADXL345_THRESH_FF 0x28
154 #define ADXL345_TIME_FF 0x29
155 #define ADXL345_TAP_AXES 0x2A
156 #define ADXL345_ACT_TAP_STATUS 0x2B
157 #define ADXL345_BW_RATE 0x2C
158 #define ADXL345_POWER_CTL 0x2D
159 #define ADXL345_INT_ENABLE 0x2E
160 #define ADXL345_INT_MAP 0x2F
161 #define ADXL345_INT_SOURCE 0x30
162 #define ADXL345_DATA_FORMAT 0x31
163 #define ADXL345_DATAX0 0x32 /* read only, LSByte X, two's complement */
164 #define ADXL345_DATAX1 0x33 /* read only, MSByte X */
165 #define ADXL345_DATAY0 0x34 /* read only, LSByte Y */
166 #define ADXL345_DATAY1 0x35 /* read only, MSByte X */
167 #define ADXL345_DATAZ0 0x36 /* read only, LSByte Z */
168 #define ADXL345_DATAZ1 0x37 /* read only, MSByte X */
169 #define ADXL345_FIFO_CTL 0x38
170 #define ADXL345_FIFO_STATUS 0x39 /* read only */
171 
172 /* ADXL345 interrupts */
173 #define ADXL345_INT_DISABLE 0X00 /* used for disabling interrupts */
174 #define ADXL345_INT_OVERRUN 0X01
175 #define ADXL345_INT_WATERMARK 0X02
176 #define ADXL345_INT_FREEFALL 0X04
177 #define ADXL345_INT_INACTIVITY 0X08
178 #define ADXL345_INT_ACTIVITY 0X10
179 #define ADXL345_INT_DOUBLETAP 0X20
180 #define ADXL345_INT_TAP 0X40
181 #define ADXL345_INT_DATAREADY 0X80
182 
183 /* Accelerometer hardware ports, pins and registers on the msp430 µC */
184 #define ADXL345_DIR P1DIR
185 #define ADXL345_PIN P1PIN
186 #define ADXL345_REN P1REN
187 #define ADXL345_SEL P1SEL
188 #define ADXL345_SEL2 P1SEL2
189 #define ADXL345_INT1_PIN (1<<6) /* P1.6 */
190 #define ADXL345_INT2_PIN (1<<7) /* P1.7 */
191 #define ADXL345_IES P1IES
192 #define ADXL345_IE P1IE
193 #define ADXL345_IFG P1IFG
194 #define ADXL345_VECTOR PORT1_VECTOR
195 
196 /* g-range for DATA_FORMAT register */
197 #define ADXL345_RANGE_2G 0x00
198 #define ADXL345_RANGE_4G 0x01
199 #define ADXL345_RANGE_8G 0x02
200 #define ADXL345_RANGE_16G 0x03
201 
202 
203 /* The adxl345 has programmable sample rates, but unexpected results may occur
204  * if the wrong rate and I2C bus speed is used (see datasheet p 17). Sample
205  * rates in Hz. This setting does not change the internal sampling rate, just
206  * how often it is piped to the output registers (ie the interrupt features use
207  * the full sample rate internally).
208  * Example use:
209  * adxl345_set_reg(ADXL345_BW_RATE, ((_ADXL345_STATUS & LOW_POWER)
210  * | ADXL345_SRATE_50));
211  */
212 
213 /* XXX NB don't use at all as I2C data rate<= 400kHz */
214 #define ADXL345_SRATE_3200 0x0F
215 /* XXX NB don't use at all as I2C data rate<= 400kHz */
216 #define ADXL345_SRATE_1600 0x0E
217 #define ADXL345_SRATE_800 0x0D /* when I2C data rate == 400 kHz */
218 #define ADXL345_SRATE_400 0x0C /* when I2C data rate == 400 kHz */
219 #define ADXL345_SRATE_200 0x0B /* when I2C data rate >= 100 kHz */
220 #define ADXL345_SRATE_100 0x0A /* when I2C data rate >= 100 kHz */
221 #define ADXL345_SRATE_50 0x09 /* when I2C data rate >= 100 kHz */
222 #define ADXL345_SRATE_25 0x08 /* when I2C data rate >= 100 kHz */
223 #define ADXL345_SRATE_12_5 0x07 /* 12.5 Hz, when I2C data rate >= 100 kHz */
224 #define ADXL345_SRATE_6_25 0x06 /* when I2C data rate >= 100 kHz */
225 #define ADXL345_SRATE_3_13 0x05 /* when I2C data rate >= 100 kHz */
226 #define ADXL345_SRATE_1_56 0x04 /* when I2C data rate >= 100 kHz */
227 #define ADXL345_SRATE_0_78 0x03 /* when I2C data rate >= 100 kHz */
228 #define ADXL345_SRATE_0_39 0x02 /* when I2C data rate >= 100 kHz */
229 #define ADXL345_SRATE_0_20 0x01 /* when I2C data rate >= 100 kHz */
230 #define ADXL345_SRATE_0_10 0x00 /* 0.10 Hz, when I2C data rate >= 100 kHz */
231 /* -------------------------------------------------------------------------- */
232 /* Callback pointers for the interrupts */
233 extern void (*accm_int1_cb)(uint8_t reg);
234 extern void (*accm_int2_cb)(uint8_t reg);
235 /* -------------------------------------------------------------------------- */
236 #define ACCM_INT1 0x01
237 #define ACCM_INT2 0x02
238 #define ADXL345_SUCCESS 0x00
239 #define ADXL345_ERROR (-1)
240 /* -------------------------------------------------------------------------- */
241 #define ADXL345_SENSOR "ADXL345 sensor"
242 /* -------------------------------------------------------------------------- */
243 extern const struct sensors_sensor adxl345;
244 /* -------------------------------------------------------------------------- */
245 /* -------------------------------------------------------------------------- */
246 #endif /* ifndef ADXL345_H_ */
I2C communication device driver header file for Zolertia Z1 sensor node.