ADXL362 accelerometer library

Dependents:   ADXL362-helloworld EVAL_ADXL362_ARDZ Lab6 Lab6 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers ADXL362.h Source File

ADXL362.h

00001 #ifndef ADXL362_H_
00002 #define ADXL362_H_
00003 
00004 /**
00005 *   @file     ADXL362.cpp
00006 *   @brief    Header file for ADXL362
00007 *   @author   Analog Devices Inc.
00008 *
00009 * For support please go to:
00010 * Github: https://github.com/analogdevicesinc/mbed-adi
00011 * Support: https://ez.analog.com/community/linux-device-drivers/microcontroller-no-os-drivers
00012 * Product: http://www.analog.com/adxl362
00013 * More: https://wiki.analog.com/resources/tools-software/mbed-drivers-all
00014 
00015 ********************************************************************************
00016 * Copyright 2016(c) Analog Devices, Inc.
00017 *
00018 * All rights reserved.
00019 *
00020 * Redistribution and use in source and binary forms, with or without
00021 * modification, are permitted provided that the following conditions are met:
00022 *  - Redistributions of source code must retain the above copyright
00023 *    notice, this list of conditions and the following disclaimer.
00024 *  - Redistributions in binary form must reproduce the above copyright
00025 *    notice, this list of conditions and the following disclaimer in
00026 *    the documentation and/or other materials provided with the
00027 *    distribution.
00028 *  - Neither the name of Analog Devices, Inc. nor the names of its
00029 *    contributors may be used to endorse or promote products derived
00030 *    from this software without specific prior written permission.
00031 *  - The use of this software may or may not infringe the patent rights
00032 *    of one or more patent holders.  This license does not release you
00033 *    from the requirement that you obtain separate licenses from these
00034 *    patent holders to use this software.
00035 *  - Use of the software either in source or binary form, must be run
00036 *    on or directly connected to an Analog Devices Inc. component.
00037 *
00038 * THIS SOFTWARE IS PROVIDED BY ANALOG DEVICES "AS IS" AND ANY EXPRESS OR
00039 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, NON-INFRINGEMENT,
00040 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
00041 * IN NO EVENT SHALL ANALOG DEVICES BE LIABLE FOR ANY DIRECT, INDIRECT,
00042 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00043 * LIMITED TO, INTELLECTUAL PROPERTY RIGHTS, PROCUREMENT OF SUBSTITUTE GOODS OR
00044 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00045 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00046 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00047 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00048 *
00049 ********************************************************************************/
00050 
00051 #include "mbed.h"
00052 
00053 /**
00054  * @brief Analog devices ADXL362 Digital Output MEMS Accelerometer
00055  */
00056 class ADXL362
00057 {
00058 public:
00059 
00060     /* Temperature parameters */
00061     typedef enum {
00062         DEVID_AD = 0x00,
00063         DEVID_MST = 0x01,
00064         PARTID = 0x02,
00065         REVID = 0x03,
00066         XDATA = 0x08,
00067         YDATA = 0x09,
00068         ZDATA = 0x0A,
00069         STATUS = 0x0B,
00070         FIFO_ENTRIES_L = 0x0C,
00071         FIFO_ENTRIES_H = 0x0D,
00072         XDATA_L = 0x0E,
00073         XDATA_H = 0x0F,
00074         YDATA_L = 0x10,
00075         YDATA_H = 0x11,
00076         ZDATA_L = 0x12,
00077         ZDATA_H = 0x13,
00078         TEMP_L = 0x14,
00079         TEMP_H = 0x15,
00080         // Reserved = 0x16;
00081         // Reserved = 0x17;
00082         SOFT_RESET = 0x1F,
00083         THRESH_ACT_L = 0x20,
00084         THRESH_ACT_H = 0x21,
00085         TIME_ACT = 0x22,
00086         THRESH_INACT_L = 0x23,
00087         THRESH_INACT_H = 0x24,
00088         TIME_INACT_L = 0x25,
00089         TIME_INACT_H = 0x26,
00090         ACT_INACT_CTL = 0x27,
00091         FIFO_CONTROL = 0x28,
00092         FIFO_SAMPLES = 0x29,
00093         INTMAP1 = 0x2A,
00094         INTMAP2 = 0x2B,
00095         FILTER_CTL = 0x2C,
00096         POWER_CTL = 0x2D,
00097         SELF_TEST = 0x2E,
00098     } ADXL362_register_t;
00099 
00100     typedef enum {
00101         STANDBY = 0x00,
00102         MEASUREMENT = 0x02
00103     } ADXL362_modes_t;
00104 
00105     typedef enum {
00106         ERR_USER_REGS = 0x80,
00107         AWAKE = 0x40,
00108         INACT = 0x20,
00109         ACT = 0x10,
00110         FIFO_OVERRUN = 0x08,
00111         FIFO_WATERMARK = 0x04,
00112         FIFO_READY = 0x02,
00113         DATA_READY = 0x01
00114     } ADXL362_STATUS_reg_bits_t;
00115 
00116     typedef enum {
00117         LINKLOOP1 = 0x20,
00118         LINKLOOP0 = 0x10,
00119         DEFAULTMODE = 0x00,
00120         LINKED_MODE = 0x10,
00121         LOOP_MODE = 0x30,
00122         INACT_REF = 0x08,
00123         INACT_EN = 0x04,
00124         ACT_REF = 0x02,
00125         ACT_EN = 0x01
00126     } ADXL362_ACT_INACT_CTL_reg_bits_t;
00127 
00128     typedef enum {
00129         AH = 0x08,
00130         FIFO_TEMP = 0x04,
00131         FIFO_MODE1 = 0x02,
00132         FIFO_MODE = 0x01,
00133     } ADXL362_FIFO_CONTROL_reg_bits_t;
00134 
00135     typedef enum {
00136         FIFO_DISABLED = 0x00,
00137         FIFO_OLDEST = 0x01,
00138         FIFO_STREAM = 0x02,
00139         FIFO_TRIGGERED = 0x03,
00140     } ADXL362_FIFO_modes_t;
00141 
00142     typedef enum {
00143         INT_LOW = 0x80,
00144         INT_AWAKE = 0x40,
00145         INT_INACT = 0x20,
00146         INT_ACT = 0x10,
00147         INT_FIFO_OVERRUN = 0x08,
00148         INT_FIFO_WATERMARK = 0x04,
00149         INT_FIFO_READY = 0x02,
00150         INT_DATA_READY = 0x01
00151     } ADXL362_INTMAP_reg_bits_t;
00152 
00153     typedef enum {
00154         RANGE1 = 0x80,
00155         RANGE0 = 0x40,
00156         RANGE2G = 0x00,
00157         RANGE4G = 0x40,
00158         RANGE8G = 0x80,
00159         HALF_BW = 0x10,
00160         EXT_SAMPLE = 0x08,
00161         ODR2 = 0x04,
00162         ODR1 = 0x02,
00163         ODR0 = 0x01,
00164         ODR12HZ = 0x00,
00165         ODR25HZ = 0x01,
00166         ODR50Hz = 0x02,
00167         ODR100HZ = 0x03,
00168         ODR200Hz = 0x04,
00169         ODR400HZ = 0x07
00170     } ADXL362_FILTER_CTL_reg_bits_t;
00171 
00172     typedef enum {
00173         EXT_CLK = 0x40,
00174         LOW_NOISE1 = 0x20,
00175         LOW_NOISE0 = 0x10,
00176         NORMAL_OPERATION = 0x00,
00177         LOW_NOISE = 0x10,
00178         ULTRALOW_NOISE = 0x20,
00179         WAKEUP = 0x08,
00180         AUTOSLEEP = 0x04,
00181         MEASURE1 = 0x02,
00182         MEASURE0 = 0x01,
00183     } ADXL362_POWER_CTL_reg_bits_t;
00184 
00185     /** SPI configuration & constructor */
00186     ADXL362(PinName CS = SPI_CS, PinName MOSI = SPI_MOSI, PinName MISO =
00187                 SPI_MISO, PinName SCK = SPI_SCK);
00188     void frequency(int hz);
00189 
00190     /** Low level SPI bus comm methods */
00191     void reset(void);
00192     void write_reg(ADXL362_register_t reg, uint8_t data);
00193     uint8_t read_reg(ADXL362_register_t reg);
00194     uint16_t read_reg_u16(ADXL362_register_t reg);
00195     void write_reg_u16(ADXL362_register_t reg, uint16_t data);
00196 
00197     /** ADXL general register R/W methods */
00198     void set_power_ctl_reg(uint8_t data);
00199     void set_filter_ctl_reg(uint8_t data);
00200     uint8_t read_status();
00201     void set_mode(ADXL362_modes_t mode);
00202 
00203     /** ADXL X/Y/Z/T scanning methods*/
00204     uint64_t scan();
00205     uint8_t scanx_u8();
00206     uint16_t scanx();
00207     uint8_t scany_u8();
00208     uint16_t scany();
00209     uint8_t scanz_u8();
00210     uint16_t scanz();
00211     uint16_t scant();
00212 
00213     /** ADXL362 activity methods */
00214     void set_activity_threshold(uint16_t threshold);
00215     void set_activity_time(uint8_t time);
00216     void set_inactivity_threshold(uint16_t threshold);
00217     void set_inactivity_time(uint16_t time);
00218     void set_act_inact_ctl_reg(uint8_t data);
00219 
00220     /** ADXL362 interrupt methods */
00221     void set_interrupt1_pin(PinName in, uint8_t data, void (*callback_rising)(void), void (*callback_falling)(void), PinMode pull = PullNone);
00222     void set_interrupt2_pin(PinName in, uint8_t data, void (*callback_rising)(void), void (*callback_falling)(void), PinMode pull = PullNone);
00223     void enable_interrupt1();
00224     void enable_interrupt2();
00225     void disable_interrupt1();
00226     void disable_interrupt2();
00227 
00228     void set_polling_interrupt1_pin(PinName in, uint8_t data, PinMode pull = PullNone);
00229     void set_polling_interrupt2_pin(PinName in, uint8_t data, PinMode pull = PullNone);
00230 
00231     bool get_int1();
00232     bool get_int2();
00233 
00234     /** ADXL362 FIFO methods */
00235     uint16_t fifo_read_nr_of_entries();
00236     void fifo_setup(bool store_temp, ADXL362_FIFO_modes_t mode, uint16_t nr_of_entries);
00237     uint16_t fifo_read_u16();
00238     uint64_t fifo_scan();
00239 
00240     SPI adxl362;    ///< SPI instance of the ADXL362
00241     DigitalOut cs; ///< DigitalOut instance for the chipselect of the ADXL362
00242 
00243 private:
00244 
00245     InterruptIn *_int1;
00246     InterruptIn *_int2;
00247     DigitalIn _int1_poll;
00248     DigitalIn _int2_poll;
00249     bool _int1_act_low;
00250     bool _int2_act_low;
00251     bool _temp_stored_in_fifo;
00252 
00253     const static uint8_t _DUMMY_BYTE = 0xAA;
00254     const static uint8_t _WRITE_REG_CMD = 0x0A; // write register
00255     const static uint8_t _READ_REG_CMD = 0x0B; // read register
00256     const static uint8_t _READ_FIFO_CMD = 0x0D; // read FIFO
00257     const static uint8_t _SPI_MODE = 0;
00258 };
00259 
00260 #endif