Dan Bryant / firstRig

Dependencies:   BLE_API PinDetect mbed-src-bmd-200

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA845x.h Source File

MMA845x.h

Go to the documentation of this file.
00001 /**
00002  * @file    MMA845x.h
00003  * @brief   Device driver - MMA845x 3-axis accelerometer IC
00004  * @author  sam grove
00005  * @version 1.0
00006  * @see     http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8451Q.pdf
00007  * @see     http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf
00008  * @see     http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8453Q.pdf
00009  *
00010  * Copyright (c) 2013
00011  *
00012  * Licensed under the Apache License, Version 2.0 (the "License");
00013  * you may not use this file except in compliance with the License.
00014  * You may obtain a copy of the License at
00015  *
00016  *     http://www.apache.org/licenses/LICENSE-2.0
00017  *
00018  * Unless required by applicable law or agreed to in writing, software
00019  * distributed under the License is distributed on an "AS IS" BASIS,
00020  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00021  * See the License for the specific language governing permissions and
00022  * limitations under the License.
00023  */
00024  
00025 #ifndef MMA845X_H
00026 #define MMA845X_H
00027 
00028 #include "mbed.h"
00029 
00030 /** Using the Sparkfun SEN-10955
00031  *
00032  * Example:
00033  * @code
00034  *  #include "mbed.h"
00035  *  #include "MMA845x.h"
00036  *
00037 
00038  * 
00039  *  int main() 
00040  *  {
00041 
00042  *  }
00043  * @endcode
00044  */
00045 
00046 
00047 /**
00048  *  @class MMA845x_DATA
00049  *  @brief API abstraction for the MMA845x 3-axis accelerometer IC data
00050  */  
00051 class MMA845x_DATA
00052 {
00053 public:
00054     
00055     volatile uint16_t _x ;   /*!< volatile data variable */
00056     volatile uint16_t _y ;   /*!< volatile data variable */
00057     volatile uint16_t _z ;   /*!< volatile data variable */
00058    
00059     /** Create the MMA845x_DATA object initialized to the parameter (or 0 if none)
00060      *  @param x - the init value of _x
00061      *  @param y - the init value of _y
00062      *  @param x - the init value of _z
00063      */
00064     MMA845x_DATA(uint16_t x = 0, uint16_t y = 0, uint16_t z = 0) : _x (x), _y (y), _z (z) {}
00065     
00066     /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another
00067      *  @param rhs - an object of the same type to assign ourself the same values of
00068      *  @return this
00069      */
00070     MMA845x_DATA &operator= (MMA845x_DATA const &rhs)
00071     {
00072         _x  = rhs._x ;
00073         _y  = rhs._y ;
00074         _z  = rhs._z ;
00075         
00076         return *this;
00077     }
00078     
00079     /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another
00080      *  @param val - Assign each data member (_x, _y, _z) this value
00081      *  @return this
00082      */
00083     MMA845x_DATA &operator= (uint16_t const val)
00084     {
00085         _x  = _y  = _z  = val;
00086         
00087         return *this;
00088     }
00089     
00090     /** Overloaded '==' operator to allow shorthand coding, test objects to one another
00091      *  @param rhs - the object to compare against
00092      *  @return 1 if the data members are the same and 0 otherwise
00093      */
00094     bool operator== (MMA845x_DATA const &rhs) const
00095     {
00096         return ((_x  == rhs._x )&&(_y  == rhs._y )&&(_z  == rhs._z )) ? 1 : 0;
00097     }
00098     
00099 };
00100 
00101 /**
00102  *  @class MMA845x
00103  *  @brief API abstraction for the MMA845x 3-axis accelerometer IC
00104  */ 
00105 class MMA845x
00106 {  
00107 public:
00108     
00109     /**
00110      *  @enum MMA845x_SA0
00111      *  @brief Possible terminations for the ADDR pin
00112      */ 
00113     enum MMA845x_SA0
00114     { 
00115         SA0_VSS  = 0, /*!< SA0 connected to VSS */
00116         SA0_VDD       /*!< SA0 connected to VDD */
00117     };
00118     
00119     /**
00120      *  @enum MMA845x_WHO_AM_I
00121      *  @brief Device ID's that this class is compatible with
00122      */ 
00123     enum MMA845x_WHO_AM_I
00124     { 
00125         MMA8451  = 0x1a, /*!< MMA8451 WHO_AM_I register content */
00126         MMA8452  = 0x2a, /*!< MMA8452 WHO_AM_I register content */
00127         MMA8453  = 0x3a, /*!< MMA8453 WHO_AM_I register content */
00128         MMA8652  = 0x4a, /*!< MMA8652 WHO_AM_I register content */
00129     };
00130     
00131     /**
00132      *  @enum MMA845x_REGISTER
00133      *  @brief The device register map
00134      */
00135     enum MMA845x_REGISTER
00136     {
00137         STATUS = 0x0,
00138         OUT_X_MSB, OUT_X_LSB, OUT_Y_MSB, OUT_Y_LSB, OUT_Z_MSB, OUT_Z_LSB,
00139         
00140         F_SETUP = 0x9, TRIG_CFG, // only available on the MMA8451 variant
00141         
00142         SYSMOD = 0xb,
00143         INT_SOURCE, WHO_AM_I, XYZ_DATA_CFG, HP_FILTER_CUTOFF, PL_STATUS,
00144         PL_CFG, PL_COUNT, PL_BF_ZCOMP, P_L_THS_REG, FF_MT_CFG, FF_MT_SRC,
00145         FF_MT_THS, FF_MT_COUNT,
00146         
00147         TRANSIENT_CFG = 0x1d,
00148         TRANSIENT_SRC, TRANSIENT_THS, TRANSIENT_COUNT, PULSE_CFG, PULSE_SRC,
00149         PULSE_THSX, PULSE_THSY, PULSE_THSZ, PULSE_TMLT, PULSE_LTCY, PULSE_WIND,
00150         ASLP_COUNT, CTRL_REG1, CTRL_REG2, CTRL_REG3, CTRL_REG4, CTRL_REG5,
00151         OFF_X, OFF_Y, OFF_Z
00152     };
00153         
00154     /** Create the MMA845x object
00155      *  @param i2c - A defined I2C object
00156      *  @param int1 - A defined InterruptIn object
00157      *  @param int2 - A defined InterruptIn object
00158      *  @param i2c_addr - Connection of the address line
00159      */ 
00160     MMA845x(I2C &i2c, PinName p_int1, PinName p_int2, MMA845x_SA0 const i2c_addr) : _int1(p_int1), _int2(p_int2)
00161     //MMA845x(I2C &i2c, InterruptIn int1, InterruptIn int2, MMA845x_SA0 const i2c_addr)
00162     {
00163         _i2c = &i2c;
00164     
00165         _i2c_addr = (0x1d << 1) | i2c_addr;
00166         
00167         _int1_waiting = false;
00168         _intMode = 0;
00169         
00170         _int1.fall(this, &MMA845x::interrupt_1);
00171         _int1.mode(PullUp);
00172     };
00173     
00174     /** Get the X data
00175      *  @return The last valid reading from the accelerometer
00176      */
00177     uint16_t getX(void) const;
00178     
00179     /** Get the Y data
00180      *  @return The last valid reading from the accelerometer
00181      */
00182     uint16_t getY(void) const;
00183     
00184     /** Get the Z data
00185      *  @return The last valid reading from the accelerometer
00186      */
00187     uint16_t getZ(void) const;
00188     
00189     /** Get the XYZ data structure
00190      *  @return The last valid reading from the accelerometer
00191      */
00192     MMA845x_DATA getXYZ(void) const;
00193     
00194     void enableDataReadyMode(void) const;
00195     void enableMotionMode(void) const;
00196     void enablePulseMode(void) const;
00197     void enableOrientationMode(void) const;
00198     void enableTransitMode(void) const;
00199     void enableAutoSleepMode(void) const;
00200     void enableFIFOMode(void) const;
00201     /** Put the MMA845x in the lowest possible power mode and suspend operation
00202      */
00203     void disable(void);
00204     
00205     /** Write to a register (exposed for debugging reasons)
00206      *  Note: most writes are only valid in stop mode
00207      *  @param reg - The register to be written
00208      *  @param data - The data to be written
00209      */
00210     void writeRegister(uint8_t const reg, uint8_t const data) const;
00211     
00212     /** Read from a register (exposed for debugging reasons)
00213      *  @param reg - The register to read from
00214      *  @return The register contents
00215      */
00216     uint8_t readRegister(uint8_t const reg) const;
00217     
00218     /** print the register map and values to the console
00219      */
00220     void registerDump(void) const;
00221     
00222     /** Set display mode of interrupt
00223      *  @return The last valid reading from the accelerometer
00224      */
00225     void setIntMode(int);
00226     void attachZAxisPulse(void (*function)(void));
00227     void interrupt_1(void);
00228     void interrupt_handler(void);
00229     
00230 private:
00231     
00232     I2C             *_i2c;
00233     InterruptIn     _int1;
00234     InterruptIn     _int2;
00235     uint8_t         _i2c_addr;
00236     MMA845x_DATA    _data;
00237     bool            _int1_waiting;
00238     int             _intMode;
00239     FunctionPointer _callbackZAxisPulse;
00240     
00241     void init(void) const;
00242 };
00243 
00244 #endif