Contiki 2.6

adxl345.h

Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2010, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  */
00032 
00033 /**
00034  * \file
00035  *         Device drivers header file for adxl345 accelerometer in Zolertia Z1.
00036  * \author
00037  *         Marcus Lundén, SICS <mlunden@sics.se>
00038  *         Enric Calvo, Zolertia <ecalvo@zolertia.com>
00039  */
00040 
00041 #ifndef __ADXL345_H__
00042 #define __ADXL345_H__
00043 #include <stdio.h>
00044 #include "dev/i2cmaster.h"
00045 
00046 #define DEBUGLEDS 0
00047 #if DEBUGLEDS
00048   #undef LEDS_ON(x)
00049   #undef LEDS_OFF(x)
00050   #define LEDS_ON(x)    (LEDS_PxOUT &= ~x)
00051   #define LEDS_OFF(x)   (LEDS_PxOUT |= x)
00052 #else
00053   #undef LEDS_ON
00054   #undef LEDS_OFF
00055   #define LEDS_ON(x)
00056   #define LEDS_OFF(x)
00057 #endif
00058 
00059 #define LEDS_R        0x10
00060 #define LEDS_G        0x40
00061 #define LEDS_B        0x20
00062 #define L_ON(x)    (LEDS_PxOUT &= ~x)
00063 #define L_OFF(x)   (LEDS_PxOUT |= x)
00064 
00065 /* Used in accm_read_axis(), eg accm_read_axis(X_AXIS);*/
00066 enum ADXL345_AXIS {
00067   X_AXIS = 0,
00068   Y_AXIS = 2,
00069   Z_AXIS = 4,
00070 };
00071 
00072 /* -------------------------------------------------------------------------- */
00073 /* Init the accelerometer: ports, pins, registers, interrupts (none enabled), I2C,
00074     default threshold values etc. */
00075 void    accm_init(void);
00076 
00077 /* Write to a register.
00078     args:
00079       reg       register to write to
00080       val       value to write
00081 */
00082 void    accm_write_reg(uint8_t reg, uint8_t val);
00083 
00084 /* Write several registers from a stream.
00085     args:
00086       len       number of bytes to read
00087       data      pointer to where the data is read from
00088   First byte in stream must be the register address to begin writing to.
00089   The data is then written from the second byte and increasing. The address byte
00090   is not included in length len. */
00091 void    accm_write_stream(uint8_t len, uint8_t *data);
00092 
00093 /* Read one register.
00094     args:
00095       reg       what register to read
00096     returns the value of the read register
00097 */
00098 uint8_t    accm_read_reg(uint8_t reg);
00099 
00100 /* Read several registers in a stream.
00101     args:
00102       reg       what register to start reading from
00103       len       number of bytes to read
00104       whereto   pointer to where the data is saved
00105 */
00106 void    accm_read_stream(uint8_t reg, uint8_t len, uint8_t *whereto);
00107 
00108 /* Read an axis of the accelerometer (x, y or z). Return value is a signed 10 bit int.
00109   The resolution of the acceleration measurement can be increased up to 13 bit, but
00110   will change the data format of this read out. Refer to the data sheet if so is
00111   wanted/needed. */
00112 int16_t accm_read_axis(enum ADXL345_AXIS axis);
00113 
00114 /* Sets the g-range, ie the range the accelerometer measures (ie 2g means -2 to +2 g
00115     on every axis). Possible values:
00116         ADXL345_RANGE_2G
00117         ADXL345_RANGE_4G
00118         ADXL345_RANGE_8G
00119         ADXL345_RANGE_16G
00120     Example:
00121         accm_set_grange(ADXL345_RANGE_4G);
00122     */
00123 void    accm_set_grange(uint8_t grange);
00124 
00125 /* Map interrupt (FF, tap, dbltap etc) to interrupt pin (IRQ_INT1, IRQ_INT2).
00126     This must come after accm_init() as the registers will otherwise be overwritten. */
00127 void    accm_set_irq(uint8_t int1, uint8_t int2);
00128 
00129 /* Macros for setting the pointers to callback functions from the interrupts.
00130   The function will be called with an uint8_t as parameter, containing the interrupt
00131   flag register from the ADXL345. That way, several interrupts can be mapped to
00132   the same pin and be read from the  */
00133 #define ACCM_REGISTER_INT1_CB(ptr)   accm_int1_cb = ptr;
00134 #define ACCM_REGISTER_INT2_CB(ptr)   accm_int2_cb = ptr;
00135 /* -------------------------------------------------------------------------- */
00136 /* Application definitions, change if required by application. */
00137 
00138 /* Interrupt suppress periods */
00139 /*
00140 // XXX Not used yet.
00141 #define ADXL345_INT_OVERRUN_BACKOFF     CLOCK_SECOND/8
00142 #define ADXL345_INT_WATERMARK_BACKOFF   CLOCK_SECOND/8
00143 #define ADXL345_INT_FREEFALL_BACKOFF    CLOCK_SECOND/8
00144 #define ADXL345_INT_INACTIVITY_BACKOFF  CLOCK_SECOND/8
00145 #define ADXL345_INT_ACTIVITY_BACKOFF    CLOCK_SECOND/8
00146 #define ADXL345_INT_DOUBLETAP_BACKOFF   CLOCK_SECOND/8
00147 #define ADXL345_INT_TAP_BACKOFF         CLOCK_SECOND/8
00148 #define ADXL345_INT_DATAREADY_BACKOFF   CLOCK_SECOND/8
00149 */
00150 /* Time after an interrupt that subsequent interrupts are suppressed. Should later
00151   be turned into one specific time per type of interrupt (tap, freefall etc) */
00152 #define SUPPRESS_TIME_INT1    CLOCK_SECOND/4
00153 #define SUPPRESS_TIME_INT2    CLOCK_SECOND/4
00154 
00155 /* Suggested defaults according to the data sheet etc */
00156 #define ADXL345_THRESH_TAP_DEFAULT      0x48    // 4.5g (0x30 == 3.0g) (datasheet: 3g++)
00157 #define ADXL345_OFSX_DEFAULT            0x00    // for individual units calibration purposes
00158 #define ADXL345_OFSY_DEFAULT            0x00
00159 #define ADXL345_OFSZ_DEFAULT            0x00
00160 #define ADXL345_DUR_DEFAULT             0x20    // 20 ms (datasheet: 10ms++)
00161 #define ADXL345_LATENT_DEFAULT          0x50    // 100 ms (datasheet: 20ms++)
00162 #define ADXL345_WINDOW_DEFAULT          0xFF    // 320 ms (datasheet: 80ms++)
00163 #define ADXL345_THRESH_ACT_DEFAULT      0x15    // 1.3g (62.5 mg/LSB)
00164 #define ADXL345_THRESH_INACT_DEFAULT    0x08    // 0.5g (62.5 mg/LSB)
00165 #define ADXL345_TIME_INACT_DEFAULT      0x02    // 2 s (1 s/LSB)
00166 #define ADXL345_ACT_INACT_CTL_DEFAULT   0xFF    // all axis involved, ac-coupled
00167 #define ADXL345_THRESH_FF_DEFAULT       0x09    // 563 mg
00168 #define ADXL345_TIME_FF_DEFAULT         0x20    // 160 ms
00169 #define ADXL345_TAP_AXES_DEFAULT        0x07    // all axis, no suppression
00170 
00171 #define ADXL345_BW_RATE_DEFAULT         (0x00|ADXL345_SRATE_100)   // 100 Hz, normal operation
00172 #define ADXL345_POWER_CTL_DEFAULT       0x28      // link bit set, no autosleep, start normal measuring
00173 #define ADXL345_INT_ENABLE_DEFAULT      0x00    // no interrupts enabled
00174 #define ADXL345_INT_MAP_DEFAULT         0x00    // all mapped to int_1
00175 
00176 /* XXX NB: In the data format register, data format of axis readings is chosen
00177   between left or right justify. This affects the position of the MSB/LSB and is
00178   different depending on g-range and resolution. If changed, make sure this is
00179   reflected in the _read_axis() function. Also, the resolution can be increased
00180   from 10 bit to at most 13 bit, but this also changes position of MSB etc on data
00181   format so check this in read_axis() too. */
00182 #define ADXL345_DATA_FORMAT_DEFAULT     (0x00|ADXL345_RANGE_2G)    // right-justify, 2g, 10-bit mode, int is active high
00183 #define ADXL345_FIFO_CTL_DEFAULT        0x00    // FIFO bypass mode
00184 
00185 /* -------------------------------------------------------------------------- */
00186 /* Reference definitions, should not be changed */
00187 /* adxl345 slave address */
00188 #define ADXL345_ADDR            0x53
00189 
00190 /* ADXL345 registers */
00191 #define ADXL345_DEVID           0x00    // read only
00192 /* registers 0x01 to 0x1C are reserved, do not access */
00193 #define ADXL345_THRESH_TAP      0x1D
00194 #define ADXL345_OFSX            0x1E
00195 #define ADXL345_OFSY            0x1F
00196 #define ADXL345_OFSZ            0x20
00197 #define ADXL345_DUR             0x21
00198 #define ADXL345_LATENT          0x22
00199 #define ADXL345_WINDOW          0x23
00200 #define ADXL345_THRESH_ACT      0x24
00201 #define ADXL345_THRESH_INACT    0x25
00202 #define ADXL345_TIME_INACT      0x26
00203 #define ADXL345_ACT_INACT_CTL   0x27
00204 #define ADXL345_THRESH_FF       0x28
00205 #define ADXL345_TIME_FF         0x29
00206 #define ADXL345_TAP_AXES        0x2A
00207 #define ADXL345_ACT_TAP_STATUS  0x2B    // read only
00208 #define ADXL345_BW_RATE         0x2C
00209 #define ADXL345_POWER_CTL       0x2D
00210 #define ADXL345_INT_ENABLE      0x2E
00211 #define ADXL345_INT_MAP         0x2F
00212 #define ADXL345_INT_SOURCE      0x30    // read only
00213 #define ADXL345_DATA_FORMAT     0x31
00214 #define ADXL345_DATAX0          0x32    // read only, LSByte X, two's complement
00215 #define ADXL345_DATAX1          0x33    // read only, MSByte X
00216 #define ADXL345_DATAY0          0x34    // read only, LSByte Y
00217 #define ADXL345_DATAY1          0x35    // read only, MSByte X
00218 #define ADXL345_DATAZ0          0x36    // read only, LSByte Z
00219 #define ADXL345_DATAZ1          0x37    // read only, MSByte X
00220 #define ADXL345_FIFO_CTL        0x38
00221 #define ADXL345_FIFO_STATUS     0x39    // read only
00222 
00223 /* ADXL345 interrupts */
00224 #define ADXL345_INT_DISABLE     0X00    // used for disabling interrupts
00225 #define ADXL345_INT_OVERRUN     0X01
00226 #define ADXL345_INT_WATERMARK   0X02
00227 #define ADXL345_INT_FREEFALL    0X04
00228 #define ADXL345_INT_INACTIVITY  0X08
00229 #define ADXL345_INT_ACTIVITY    0X10
00230 #define ADXL345_INT_DOUBLETAP   0X20
00231 #define ADXL345_INT_TAP         0X40
00232 #define ADXL345_INT_DATAREADY   0X80
00233 
00234 /* Accelerometer hardware ports, pins and registers on the msp430 µC */
00235 #define ADXL345_DIR        P1DIR
00236 #define ADXL345_PIN        P1PIN
00237 #define ADXL345_REN        P1REN
00238 #define ADXL345_SEL        P1SEL
00239 #define ADXL345_SEL2       P1SEL2
00240 #define ADXL345_INT1_PIN   (1<<6)            // P1.6
00241 #define ADXL345_INT2_PIN   (1<<7)            // P1.7
00242 #define ADXL345_IES        P1IES
00243 #define ADXL345_IE         P1IE
00244 #define ADXL345_IFG        P1IFG
00245 #define ADXL345_VECTOR     PORT1_VECTOR
00246 
00247 /* g-range for DATA_FORMAT register */
00248 #define ADXL345_RANGE_2G    0x00
00249 #define ADXL345_RANGE_4G    0x01
00250 #define ADXL345_RANGE_8G    0x02
00251 #define ADXL345_RANGE_16G   0x03
00252 
00253 
00254 /* The adxl345 has programmable sample rates, but unexpected results may occur if the wrong 
00255   rate and I2C bus speed is used (see datasheet p 17). Sample rates in Hz. This
00256   setting does not change the internal sampling rate, just how often it is piped
00257   to the output registers (ie the interrupt features use the full sample rate
00258   internally).
00259 
00260   Example use:
00261     adxl345_set_reg(ADXL345_BW_RATE, ((_ADXL345_STATUS & LOW_POWER) | ADXL345_SRATE_50));
00262   */
00263 #define ADXL345_SRATE_3200      0x0F    // XXX NB don't use at all as I2C data rate<= 400kHz (see datasheet)
00264 #define ADXL345_SRATE_1600      0x0E    // XXX NB don't use at all as I2C data rate<= 400kHz (see datasheet)
00265 #define ADXL345_SRATE_800       0x0D    // when I2C data rate == 400 kHz
00266 #define ADXL345_SRATE_400       0x0C    // when I2C data rate == 400 kHz
00267 #define ADXL345_SRATE_200       0x0B    // when I2C data rate >= 100 kHz
00268 #define ADXL345_SRATE_100       0x0A    // when I2C data rate >= 100 kHz
00269 #define ADXL345_SRATE_50        0x09    // when I2C data rate >= 100 kHz
00270 #define ADXL345_SRATE_25        0x08    // when I2C data rate >= 100 kHz
00271 #define ADXL345_SRATE_12_5      0x07    // 12.5 Hz, when I2C data rate >= 100 kHz
00272 #define ADXL345_SRATE_6_25      0x06    // when I2C data rate >= 100 kHz
00273 #define ADXL345_SRATE_3_13      0x05    // when I2C data rate >= 100 kHz
00274 #define ADXL345_SRATE_1_56      0x04    // when I2C data rate >= 100 kHz
00275 #define ADXL345_SRATE_0_78      0x03    // when I2C data rate >= 100 kHz
00276 #define ADXL345_SRATE_0_39      0x02    // when I2C data rate >= 100 kHz
00277 #define ADXL345_SRATE_0_20      0x01    // when I2C data rate >= 100 kHz
00278 #define ADXL345_SRATE_0_10      0x00    // 0.10 Hz, when I2C data rate >= 100 kHz
00279 
00280 /* Callback pointers for the interrupts */
00281 extern void (*accm_int1_cb)(uint8_t reg);
00282 extern void (*accm_int2_cb)(uint8_t reg);
00283 
00284 /* Interrupt 1 and 2 events; ADXL345 signals interrupt on INT1 or INT2 pins,
00285   ISR is invoked and polls the accelerometer process which invokes the callbacks. */
00286 extern process_event_t int1_event, int2_event;   // static ?
00287 
00288 #define ACCM_INT1    0x01
00289 #define ACCM_INT2    0x02
00290 
00291 
00292 /* -------------------------------------------------------------------------- */
00293 #endif /* ifndef __ADXL345_H__ */