Republished Library, to be refined for use with the SparkFun 9DOF in HARP project.

Dependents:   vmConfort_v6

Fork of ADXL345 by Tyler Weaver

Revision:
11:c0fe48a352ca
Parent:
10:d81793e01ec4
--- a/ADXL345.h	Tue Nov 06 17:36:40 2012 +0000
+++ b/ADXL345.h	Wed Jan 16 11:10:19 2013 +0000
@@ -1,12 +1,10 @@
 /**
- * @file ADXL345.h
- * @author Tyler Weaver
- * @author Uwe Gartmann
- * @author Used ITG3200 library developed Peter Swanson as template
- * A special thanks to Ewout van Bekkum for all his patient help in developing this library!
+ * @author Aaron Berk
  *
  * @section LICENSE
  *
+ * Copyright (c) 2010 ARM Limited
+ *
  * Permission is hereby granted, free of charge, to any person obtaining a copy
  * of this software and associated documentation files (the "Software"), to deal
  * in the Software without restriction, including without limitation the rights
@@ -27,26 +25,22 @@
  *
  * @section DESCRIPTION
  *
- * ADXL345, triple axis, I2C interface, accelerometer.
- *
- * Modified for use with 9DOF sensor stick (no interupt pin access) on HARP project.
+ * ADXL345, triple axis, digital interface, accelerometer.
  *
  * Datasheet:
  *
  * http://www.analog.com/static/imported-files/data_sheets/ADXL345.pdf
- */
-
-
+ */  
 
 #ifndef ADXL345_H
 #define ADXL345_H
 
-/*
+/**
  * Includes
  */
 #include "mbed.h"
 
-/*
+/**
  * Defines
  */
 //Registers.
@@ -93,156 +87,53 @@
 #define ADXL345_12HZ5       0x07
 #define ADXL345_6HZ25       0x06
 
-// DATA_FORMAT register
-#define ADXL345_2G          0x00
-#define ADXL345_4G          0x01
-#define ADXL345_8G          0x02
-#define ADXL345_16G         0x03
-#define ADXL345_LJUST       0x04
-#define ADXL345_FULL_RES    0x08
-#define ADXL345_INT_LOW     0x10
-#define ADXL345_SELF_TEST   0x80
-
-// read or write bytes
-#define ADXL345_READ    0xA7
-#define ADXL345_WRITE   0xA6
-#define ADXL345_ADDRESS 0x53   //the ADXL345 7-bit address is 0x53 when ALT ADDRESS is low as it is on the sparkfun chip: when ALT ADDRESS is high the address is 0x1D
-
-/////////////when ALT ADDRESS pin is high:
-//#define ADXL345_READ    0x3B
-//#define ADXL345_WRITE   0x3A
-//#define ADXL345_ADDRESS 0x1D
+#define ADXL345_SPI_READ    0x80
+#define ADXL345_SPI_WRITE   0x00
+#define ADXL345_MULTI_BYTE  0x60
 
 #define ADXL345_X           0x00
 #define ADXL345_Y           0x01
 #define ADXL345_Z           0x02
 
-// modes
-#define MeasurementMode     0x08
-
 /**
- * ADXL345 triple axis accelerometer.
+ * ADXL345 triple axis, digital interface, accelerometer.
  */
-class ADXL345
-{
+class ADXL345 {
 
 public:
 
-    static const char RANGE_BITS = 0x3;
-    static const char RANGE_2G = 0x0;
-    static const char RANGE_4G = 0x1;
-    static const char RANGE_8G = 0x2;
-    static const char RANGE_16G = 0x3;
-
     /**
      * Constructor.
      *
-     * @param mosi mbed pin to use for SDA line of I2C interface.
-     * @param sck mbed pin to use for SCL line of I2C interface.
-     */
-    ADXL345(PinName sda, PinName scl);
-
-
-    /**
-     * Constructor that accepts external i2c interface object.
-     *
-     * @param i2c The I2C interface object to use.
+     * @param mosi mbed pin to use for MOSI line of SPI interface.
+     * @param miso mbed pin to use for MISO line of SPI interface.
+     * @param sck mbed pin to use for SCK line of SPI interface.
+     * @param cs mbed pin to use for not chip select line of SPI interface.
      */
-    ADXL345(I2C &i2c) : i2c_(i2c), myI2c(NULL) {init();}
-
-    /**
-     * Destructor that frees self-allocated I2C object.
-     */
-    ~ADXL345();
-
-    void init();
-
-    /**
-     * Get the output of all three axes.
-     *
-     * @param Pointer to a buffer to hold the accelerometer value for the
-     *        x-axis, y-axis and z-axis [in that order].
-     */
-    void getXYZ(int16_t* readings);
+    ADXL345(PinName mosi, PinName miso, PinName sck, PinName cs);
 
     /**
      * Read the device ID register on the device.
      *
      * @return The device ID code [0xE5]
      */
-    char getDeviceID(void);
-
-    /**
-    * Set the power mode.
-    *
-    * @param mode 0 -> Normal operation.
-    *             1 -> Reduced power operation.
-    */
-    int setPowerMode(char mode);
-
-    /**
-    * Get the BW_RATE register contents
-    *
-    * @returns The contents of BW_RATE register
-    */
-    char getBwRateReg();
-    
-    /**
-    * Set the BW_RATE register contents
-    *
-    * @param Byte to write to BW_RATE register
-    */
-    int setBwRateReg(char);
-
-    /**
-    * Set the power control settings.
-    *
-    * See datasheet for details.
-    *
-    * @param The control byte to write to the POWER_CTL register.
-    */
-    int setPowerControl(char settings);
+    int getDevId(void);
 
     /**
-    * Get the power control settings.
-    *
-    * See datasheet for details.
-    *
-    * @return The contents of the POWER_CTL register.
-    */
-    char getPowerControl(void);
-
-
-    /**
-     * Get the data format settings.
+     * Read the tap threshold on the device.
      *
-     * @return The contents of the DATA_FORMAT register.
+     * @return The tap threshold as an 8-bit number with a scale factor of
+     *         62.5mg/LSB.
      */
-
-    char getDataFormatControl(void);
+    int getTapThreshold(void);
 
     /**
-     * Set the data format settings.
-     *
-     * @param settings The control byte to write to the DATA_FORMAT register.
-     */
-    int setDataFormatControl(char settings);
-
-    /**
-     * Set the data format settings with only specified bits.
+     * Set the tap threshold.
      *
-     * @param settings The control byte to write to the DATA_FORMAT register.
-     * @param mask The mask bits that
+     * @param The tap threshold as an 8-bit number with a scale factor of
+     *        62.5mg/LSB.
      */
-    int setDataFormatControl(char settings, char mask, char *prev = NULL);
-
-    /**
-     * Set the data rate.
-     *
-     * @param rate The rate code (see #defines or datasheet).
-     */
-    int setDataRate(char rate);
-
+    void setTapThreshold(int threshold);
 
     /**
      * Get the current offset for a particular axis.
@@ -253,7 +144,7 @@
      * @return The current offset as an 8-bit 2's complement number with scale
      *         factor 15.6mg/LSB.
      */
-    char getOffset(char axis);
+    int getOffset(int axis);
 
     /**
      * Set the offset for a particular axis.
@@ -264,44 +155,7 @@
      * @param offset The offset as an 8-bit 2's complement number with scale
      *               factor 15.6mg/LSB.
      */
-    int setOffset(char axis, char offset);
-
-    /**
-     * Get the FIFO control settings.
-     *
-     * @return The contents of the FIFO_CTL register.
-     */
-    char getFifoControl(void);
-
-    /**
-     * Set the FIFO control settings.
-     *
-     * @param The control byte to write to the FIFO_CTL register.
-     */
-    int setFifoControl(char settings);
-
-    /**
-     * Get FIFO status.
-     *
-     * @return The contents of the FIFO_STATUS register.
-     */
-    char getFifoStatus(void);
-
-    /**
-     * Read the tap threshold on the device.
-     *
-     * @return The tap threshold as an 8-bit number with a scale factor of
-     *         62.5mg/LSB.
-     */
-    char getTapThreshold(void);
-
-    /**
-     * Set the tap threshold.
-     *
-     * @param The tap threshold as an 8-bit number with a scale factor of
-     *        62.5mg/LSB.
-     */
-    int setTapThreshold(char threshold);
+    void setOffset(int axis, char offset);
 
     /**
      * Get the tap duration required to trigger an event.
@@ -309,7 +163,7 @@
      * @return The max time that an event must be above the tap threshold to
      *         qualify as a tap event, in microseconds.
      */
-    float getTapDuration(void);
+    int getTapDuration(void);
 
     /**
      * Set the tap duration required to trigger an event.
@@ -320,7 +174,7 @@
      *                    625us/LSB. A value of 0 disables the single/double
      *                    tap functions.
      */
-    int setTapDuration(short int duration_us);
+    void setTapDuration(int duration_us);
 
     /**
      * Get the tap latency between the detection of a tap and the time window.
@@ -339,7 +193,7 @@
      *                   second tap event can be detected in milliseconds.
      *                   A value of 0 disables the double tap function.
      */
-    int setTapLatency(short int latency_ms);
+    void setTapLatency(int latency_ms);
 
     /**
      * Get the time of window between tap latency and a double tap.
@@ -356,7 +210,7 @@
      *                  time during which a second valid tap can begin,
      *                  in milliseconds.
      */
-    int setWindowTime(short int window_ms);
+    void setWindowTime(int window_ms);
 
     /**
      * Get the threshold value for detecting activity.
@@ -364,7 +218,7 @@
      * @return The threshold value for detecting activity as an 8-bit number.
      *         Scale factor is 62.5mg/LSB.
      */
-    char getActivityThreshold(void);
+    int getActivityThreshold(void);
 
     /**
      * Set the threshold value for detecting activity.
@@ -374,7 +228,7 @@
      *                  result in undesirable behavior if the activity
      *                  interrupt is enabled.
      */
-    int setActivityThreshold(char threshold);
+    void setActivityThreshold(int threshold);
 
     /**
      * Get the threshold value for detecting inactivity.
@@ -382,7 +236,7 @@
      * @return The threshold value for detecting inactivity as an 8-bit number.
      *         Scale factor is 62.5mg/LSB.
      */
-    char getInactivityThreshold(void);
+    int getInactivityThreshold(void);
 
     /**
      * Set the threshold value for detecting inactivity.
@@ -390,7 +244,7 @@
      * @param threshold The threshold value for detecting inactivity as an
      *                  8-bit number. Scale factor is 62.5mg/LSB.
      */
-    int setInactivityThreshold(char threshold);
+    void setInactivityThreshold(int threshold);
 
     /**
      * Get the time required for inactivity to be declared.
@@ -399,8 +253,8 @@
      *         inactivity threshold for inactivity to be declared, in
      *         seconds.
      */
-    char getTimeInactivity(void);
-
+    int getTimeInactivity(void);
+    
     /**
      * Set the time required for inactivity to be declared.
      *
@@ -410,8 +264,8 @@
      *                   interrupt when the output data is less than the
      *                   threshold inactivity.
      */
-    int setTimeInactivity(char timeInactivity);
-
+    void setTimeInactivity(int timeInactivity);
+    
     /**
      * Get the activity/inactivity control settings.
      *
@@ -429,8 +283,8 @@
      *
      * @return The contents of the ACT_INACT_CTL register.
      */
-    char getActivityInactivityControl(void);
-
+    int getActivityInactivityControl(void);
+    
     /**
      * Set the activity/inactivity control settings.
      *
@@ -448,26 +302,26 @@
      *
      * @param settings The control byte to write to the ACT_INACT_CTL register.
      */
-    int setActivityInactivityControl(char settings);
-
+    void setActivityInactivityControl(int settings);
+    
     /**
      * Get the threshold for free fall detection.
      *
      * @return The threshold value for free-fall detection, as an 8-bit number,
      *         with scale factor 62.5mg/LSB.
      */
-    char getFreefallThreshold(void);
-
+    int getFreefallThreshold(void);
+    
     /**
      * Set the threshold for free fall detection.
      *
      * @return The threshold value for free-fall detection, as an 8-bit number,
-     *         with scale factor 62.5mg/LSB. A value of 0 may result in
+     *         with scale factor 62.5mg/LSB. A value of 0 may result in 
      *         undesirable behavior if the free-fall interrupt is enabled.
      *         Values between 300 mg and 600 mg (0x05 to 0x09) are recommended.
      */
-    int setFreefallThreshold(char threshold);
-
+    void setFreefallThreshold(int threshold);
+    
     /**
      * Get the time required to generate a free fall interrupt.
      *
@@ -475,19 +329,19 @@
      *         the freefall threshold to generate a free-fall interrupt, in
      *         milliseconds.
      */
-    char getFreefallTime(void);
-
+    int getFreefallTime(void);
+    
     /**
      * Set the time required to generate a free fall interrupt.
      *
      * @return The minimum time that the value of all axes must be less than
      *         the freefall threshold to generate a free-fall interrupt, in
      *         milliseconds. A value of 0 may result in undesirable behavior
-     *         if the free-fall interrupt is enabled. Values between 100 ms
+     *         if the free-fall interrupt is enabled. Values between 100 ms 
      *         and 350 ms (0x14 to 0x46) are recommended.
      */
-    int setFreefallTime(short int freefallTime_ms);
-
+    void setFreefallTime(int freefallTime_ms);
+    
     /**
      * Get the axis tap settings.
      *
@@ -501,9 +355,9 @@
      * See datasheet for more details.
      *
      * @return The contents of the TAP_AXES register.
-     */
-    char getTapAxisControl(void);
-
+     */ 
+    int getTapAxisControl(void);
+    
     /**
      * Set the axis tap settings.
      *
@@ -518,141 +372,194 @@
      *
      * @param The control byte to write to the TAP_AXES register.
      */
-    int setTapAxisControl(char settings);
-
+    void setTapAxisControl(int settings);
+    
     /**
      * Get the source of a tap.
      *
      * @return The contents of the ACT_TAP_STATUS register.
      */
-    char getTapSource(void);
-
+    int getTapSource(void);
+    
+    /**
+     * Set the power mode.
+     *
+     * @param mode 0 -> Normal operation.
+     *             1 -> Reduced power operation.
+     */
+    void setPowerMode(char mode);
+    
+    /**
+     * Set the data rate.
+     *
+     * @param rate The rate code (see #defines or datasheet).
+     */
+    void setDataRate(int rate);
+    
     /**
-    * Get the interrupt enable settings.
-    *
-    * @return The contents of the INT_ENABLE register.
-    */
-
-    char getInterruptEnableControl(void);
-
+     * Get the power control settings.
+     *
+     * See datasheet for details.
+     *
+     * @return The contents of the POWER_CTL register.
+     */
+    int getPowerControl(void);
+    
+    /**
+     * Set the power control settings.
+     *
+     * See datasheet for details.
+     *
+     * @param The control byte to write to the POWER_CTL register.
+     */
+    void setPowerControl(int settings);
+    
+    /**
+     * Get the interrupt enable settings.
+     *
+     * @return The contents of the INT_ENABLE register.
+     */
+    int getInterruptEnableControl(void);
+    
     /**
      * Set the interrupt enable settings.
      *
      * @param settings The control byte to write to the INT_ENABLE register.
      */
-    int setInterruptEnableControl(char settings);
-
+    void setInterruptEnableControl(int settings);
+    
     /**
      * Get the interrupt mapping settings.
      *
      * @return The contents of the INT_MAP register.
      */
-    char getInterruptMappingControl(void);
-
+    int getInterruptMappingControl(void);
+    
     /**
      * Set the interrupt mapping settings.
      *
      * @param settings The control byte to write to the INT_MAP register.
      */
-    int setInterruptMappingControl(char settings);
-
+    void setInterruptMappingControl(int settings);
+    
     /**
      * Get the interrupt source.
      *
      * @return The contents of the INT_SOURCE register.
      */
-    char getInterruptSource(void);
-
+    int getInterruptSource(void);
+    
+    /**
+     * Get the data format settings.
+     *
+     * @return The contents of the DATA_FORMAT register.
+     */
+    int getDataFormatControl(void);
+    
+    /**
+     * Set the data format settings.
+     *
+     * @param settings The control byte to write to the DATA_FORMAT register.
+     */
+    void setDataFormatControl(int settings);
+    
     /**
-    * Calibrate using the calibrate routine, sets the offset registers
-    *
-    * 100 samples at 100Hz (1 second)
-    * Sensor should be in x=0g,y=0g,z=1g orientation and held still
-    *
-    * @param pointer to timer object (should already be started)
-    * @param store_output true -> stores offsets in local file system, false -> only offset registers are set
-    * @param serial output (pc) for debugging  
-    */
-    void calibrate(Timer*, bool, Serial*);
+     * Get the output of all three axes.
+     *
+     * @param Pointer to a buffer to hold the accelerometer value for the
+     *        x-axis, y-axis and z-axis [in that order].
+     */
+    void getOutput(int* readings);
+    
+    
+      /**
+     * Get acceleration of X axis.
+     *
+     *@return Accelerometer value for the x-axis.
+     */
+     int getAx();
+     
+     
+     /**
+     * Get acceleration of Y axis.
+     *
+     *@return Accelerometer value for the y-axis
+     */
+     int getAy();
+     
+     
+      /**
+     * Get acceleration of Z axis.
+     *
+     *@return Accelerometer value for the z-axis
+     */
+     int getAz();
+    
+    
+    
+    
+    
     /**
-    * Calibrate using the calibrate routine, sets the offset registers
-    *
-    * 100 samples at 100Hz (1 second)
-    * Sensor should be in x=0g,y=0g,z=1g orientation and held still
-    *
-    * @param pointer to timer object (should already be started)
-    * @param store_output true -> stores offsets in local file system, false -> only offset registers are set
-    */
-    void calibrate(Timer*, bool);
-
+     * Get the FIFO control settings.
+     *
+     * @return The contents of the FIFO_CTL register.
+     */
+    int getFifoControl(void);
+    
+    /**
+     * Set the FIFO control settings.
+     *
+     * @param The control byte to write to the FIFO_CTL register.
+     */
+    void setFifoControl(int settings);
+    
+    /**
+     * Get FIFO status.
+     *
+     * @return The contents of the FIFO_STATUS register.
+     */
+    int getFifoStatus(void);
+    
 private:
 
-    I2C &i2c_;
-
-    I2C *myI2c;
+    SPI        spi_;
+    DigitalOut nCS_;
 
     /**
      * Read one byte from a register on the device.
      *
-     * @param: - the address to be read from
+     * @param address Address of the register to read.
      *
-     * @return: the value of the data read
+     * @return The contents of the register address.
      */
-    char SingleByteRead(char address);
+    int oneByteRead(int address);
 
     /**
      * Write one byte to a register on the device.
      *
-     * @param:
-        - address of the register to write to.
-        - the value of the data to store
+     * @param address Address of the register to write to.
+     * @param data The data to write into the register.
      */
-    int SingleByteWrite(char address, char data);
-
-    /**
-     * Read several consecutive bytes on the device and store them in a given location.
-     *
-     * @param startAddress: The address of the first register to read from.
-     * @param ptr_output: a pointer to the location to store the data being read
-     * @param size: The number of bytes to read.
-     */
-    void multiByteRead(char startAddress, char* ptr_output, int size);
+    void oneByteWrite(int address, char data);
 
     /**
-     * Write several consecutive bytes  on the device.
+     * Read several consecutive bytes on the device.
      *
-     * @param startAddress: The address of the first register to write to.
-     * @param ptr_data: Pointer to a location which contains the data to write.
-     * @param size: The number of bytes to write.
-     */
-    int multiByteWrite(char startAddress, char* ptr_data, int size);
-    
-    /**
-     * Converts little-endian 2's complement byte pair to native byte order of
-     * the CPU and then sign extend it to the CPU's register size.
-     *
-     * Implemented here to make the compiler inline expand it.
+     * @param startAddress The address of the first register to read from.
+     * @param buffer Pointer to a buffer to store data read from the device.
+     * @param size The number of bytes to read.
      */
-    static int wordExtend(const char rx[2]) {
-        // Readings are expressed in 16bit 2's complement, so we must first
-        // concatenate two bytes to make a word and sign extend it to obtain
-        // correct negative values.
-        // ARMCC compiles char as unsigned, which means no sign extension is
-        // performed during bitwise operations to chars. But we should make sure
-        // that lower byte won't extend its sign past upper byte for other
-        // compilers if we want to keep it portable.
-        return int16_t(((unsigned char)rx[1] << 8) | (unsigned char)rx[0]);
-    }
-    
+    void multiByteRead(int startAddress, char* buffer, int size);
+
     /**
-    * Sample 100 times and average
-    *
-    * @param period of sample rate
-    * @param array to hold raw data, should be int16_t[100][3] (sample,axis)
-    * @param array to hold averages, should be 3 in length
-    * @param pointer to timer object
-    */
-    void sample100avg(float, int16_t[][3], int16_t*, Timer*);
+     * Write several consecutive bytes on the device.
+     *
+     * @param startAddress The address of the first register to write to.
+     * @param buffer Pointer to a buffer which contains the data to write.
+     * @param size The number of bytes to write.
+     */
+    void multiByteWrite(int startAddress, char* buffer, int size);
+
 };
 
 #endif /* ADXL345_H */