Device driver for the Freescale MMA845x family of accelerometers.
Fork of MMA845x by
MMA845x.h@0:9d1e3a344e4f, 2013-03-29 (annotated)
- Committer:
- sam_grove
- Date:
- Fri Mar 29 21:51:11 2013 +0000
- Revision:
- 0:9d1e3a344e4f
- Child:
- 1:37ddb4739f02
Work in progress
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sam_grove | 0:9d1e3a344e4f | 1 | /** |
sam_grove | 0:9d1e3a344e4f | 2 | * @file MMA845x.h |
sam_grove | 0:9d1e3a344e4f | 3 | * @brief Device driver - MMA845x 3-axis accelerometer IC |
sam_grove | 0:9d1e3a344e4f | 4 | * @author sam grove |
sam_grove | 0:9d1e3a344e4f | 5 | * @version 1.0 |
sam_grove | 0:9d1e3a344e4f | 6 | * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8451Q.pdf |
sam_grove | 0:9d1e3a344e4f | 7 | * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8452Q.pdf |
sam_grove | 0:9d1e3a344e4f | 8 | * @see http://cache.freescale.com/files/sensors/doc/data_sheet/MMA8453Q.pdf |
sam_grove | 0:9d1e3a344e4f | 9 | * |
sam_grove | 0:9d1e3a344e4f | 10 | * Copyright (c) 2013 |
sam_grove | 0:9d1e3a344e4f | 11 | * |
sam_grove | 0:9d1e3a344e4f | 12 | * Licensed under the Apache License, Version 2.0 (the "License"); |
sam_grove | 0:9d1e3a344e4f | 13 | * you may not use this file except in compliance with the License. |
sam_grove | 0:9d1e3a344e4f | 14 | * You may obtain a copy of the License at |
sam_grove | 0:9d1e3a344e4f | 15 | * |
sam_grove | 0:9d1e3a344e4f | 16 | * http://www.apache.org/licenses/LICENSE-2.0 |
sam_grove | 0:9d1e3a344e4f | 17 | * |
sam_grove | 0:9d1e3a344e4f | 18 | * Unless required by applicable law or agreed to in writing, software |
sam_grove | 0:9d1e3a344e4f | 19 | * distributed under the License is distributed on an "AS IS" BASIS, |
sam_grove | 0:9d1e3a344e4f | 20 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
sam_grove | 0:9d1e3a344e4f | 21 | * See the License for the specific language governing permissions and |
sam_grove | 0:9d1e3a344e4f | 22 | * limitations under the License. |
sam_grove | 0:9d1e3a344e4f | 23 | */ |
sam_grove | 0:9d1e3a344e4f | 24 | |
sam_grove | 0:9d1e3a344e4f | 25 | #ifndef MMA845X_H |
sam_grove | 0:9d1e3a344e4f | 26 | #define MMA845X_H |
sam_grove | 0:9d1e3a344e4f | 27 | |
sam_grove | 0:9d1e3a344e4f | 28 | #include "mbed.h" |
sam_grove | 0:9d1e3a344e4f | 29 | |
sam_grove | 0:9d1e3a344e4f | 30 | /** Using the Sparkfun SEN-10955 |
sam_grove | 0:9d1e3a344e4f | 31 | * |
sam_grove | 0:9d1e3a344e4f | 32 | * Example: |
sam_grove | 0:9d1e3a344e4f | 33 | * @code |
sam_grove | 0:9d1e3a344e4f | 34 | * #include "mbed.h" |
sam_grove | 0:9d1e3a344e4f | 35 | * #include "MMA845x.h" |
sam_grove | 0:9d1e3a344e4f | 36 | * |
sam_grove | 0:9d1e3a344e4f | 37 | |
sam_grove | 0:9d1e3a344e4f | 38 | * |
sam_grove | 0:9d1e3a344e4f | 39 | * int main() |
sam_grove | 0:9d1e3a344e4f | 40 | * { |
sam_grove | 0:9d1e3a344e4f | 41 | |
sam_grove | 0:9d1e3a344e4f | 42 | * } |
sam_grove | 0:9d1e3a344e4f | 43 | * @endcode |
sam_grove | 0:9d1e3a344e4f | 44 | */ |
sam_grove | 0:9d1e3a344e4f | 45 | |
sam_grove | 0:9d1e3a344e4f | 46 | |
sam_grove | 0:9d1e3a344e4f | 47 | /** |
sam_grove | 0:9d1e3a344e4f | 48 | * @class MMA845x_DATA |
sam_grove | 0:9d1e3a344e4f | 49 | * @brief API abstraction for the MMA845x 3-axis accelerometer IC data |
sam_grove | 0:9d1e3a344e4f | 50 | */ |
sam_grove | 0:9d1e3a344e4f | 51 | class MMA845x_DATA |
sam_grove | 0:9d1e3a344e4f | 52 | { |
sam_grove | 0:9d1e3a344e4f | 53 | public: |
sam_grove | 0:9d1e3a344e4f | 54 | |
sam_grove | 0:9d1e3a344e4f | 55 | volatile uint16_t _x; /*!< volatile data variable */ |
sam_grove | 0:9d1e3a344e4f | 56 | volatile uint16_t _y; /*!< volatile data variable */ |
sam_grove | 0:9d1e3a344e4f | 57 | volatile uint16_t _z; /*!< volatile data variable */ |
sam_grove | 0:9d1e3a344e4f | 58 | |
sam_grove | 0:9d1e3a344e4f | 59 | /** Create the MMA845x_DATA object initialized to the parameter (or 0 if none) |
sam_grove | 0:9d1e3a344e4f | 60 | * @param x - the init value of _x |
sam_grove | 0:9d1e3a344e4f | 61 | * @param y - the init value of _y |
sam_grove | 0:9d1e3a344e4f | 62 | * @param x - the init value of _z |
sam_grove | 0:9d1e3a344e4f | 63 | */ |
sam_grove | 0:9d1e3a344e4f | 64 | MMA845x_DATA(uint16_t x = 0, uint16_t y = 0, uint16_t z = 0) : _x(x), _y(y), _z(z) {} |
sam_grove | 0:9d1e3a344e4f | 65 | |
sam_grove | 0:9d1e3a344e4f | 66 | /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another |
sam_grove | 0:9d1e3a344e4f | 67 | * @param rhs - an object of the same type to assign ourself the same values of |
sam_grove | 0:9d1e3a344e4f | 68 | * @return this |
sam_grove | 0:9d1e3a344e4f | 69 | */ |
sam_grove | 0:9d1e3a344e4f | 70 | MMA845x_DATA &operator= (MMA845x_DATA const &rhs) |
sam_grove | 0:9d1e3a344e4f | 71 | { |
sam_grove | 0:9d1e3a344e4f | 72 | _x = rhs._x; |
sam_grove | 0:9d1e3a344e4f | 73 | _y = rhs._y; |
sam_grove | 0:9d1e3a344e4f | 74 | _z = rhs._z; |
sam_grove | 0:9d1e3a344e4f | 75 | |
sam_grove | 0:9d1e3a344e4f | 76 | return *this; |
sam_grove | 0:9d1e3a344e4f | 77 | } |
sam_grove | 0:9d1e3a344e4f | 78 | |
sam_grove | 0:9d1e3a344e4f | 79 | /** Overloaded '=' operator to allow shorthand coding, assigning objects to one another |
sam_grove | 0:9d1e3a344e4f | 80 | * @param val - Assign each data member (_x, _y, _z) this value |
sam_grove | 0:9d1e3a344e4f | 81 | * @return this |
sam_grove | 0:9d1e3a344e4f | 82 | */ |
sam_grove | 0:9d1e3a344e4f | 83 | MMA845x_DATA &operator= (uint16_t const val) |
sam_grove | 0:9d1e3a344e4f | 84 | { |
sam_grove | 0:9d1e3a344e4f | 85 | _x = _y = _z = val; |
sam_grove | 0:9d1e3a344e4f | 86 | |
sam_grove | 0:9d1e3a344e4f | 87 | return *this; |
sam_grove | 0:9d1e3a344e4f | 88 | } |
sam_grove | 0:9d1e3a344e4f | 89 | |
sam_grove | 0:9d1e3a344e4f | 90 | /** Overloaded '==' operator to allow shorthand coding, test objects to one another |
sam_grove | 0:9d1e3a344e4f | 91 | * @param rhs - the object to compare against |
sam_grove | 0:9d1e3a344e4f | 92 | * @return 1 if the data members are the same and 0 otherwise |
sam_grove | 0:9d1e3a344e4f | 93 | */ |
sam_grove | 0:9d1e3a344e4f | 94 | bool operator== (MMA845x_DATA const &rhs) const |
sam_grove | 0:9d1e3a344e4f | 95 | { |
sam_grove | 0:9d1e3a344e4f | 96 | return ((_x == rhs._x)&&(_y == rhs._y)&&(_z == rhs._z)) ? 1 : 0; |
sam_grove | 0:9d1e3a344e4f | 97 | } |
sam_grove | 0:9d1e3a344e4f | 98 | |
sam_grove | 0:9d1e3a344e4f | 99 | }; |
sam_grove | 0:9d1e3a344e4f | 100 | |
sam_grove | 0:9d1e3a344e4f | 101 | /** |
sam_grove | 0:9d1e3a344e4f | 102 | * @class MMA845x |
sam_grove | 0:9d1e3a344e4f | 103 | * @brief API abstraction for the MMA845x 3-axis accelerometer IC |
sam_grove | 0:9d1e3a344e4f | 104 | */ |
sam_grove | 0:9d1e3a344e4f | 105 | class MMA845x |
sam_grove | 0:9d1e3a344e4f | 106 | { |
sam_grove | 0:9d1e3a344e4f | 107 | public: |
sam_grove | 0:9d1e3a344e4f | 108 | |
sam_grove | 0:9d1e3a344e4f | 109 | /** |
sam_grove | 0:9d1e3a344e4f | 110 | * @enum MMA845x_SA0 |
sam_grove | 0:9d1e3a344e4f | 111 | * @brief Possible terminations for the ADDR pin |
sam_grove | 0:9d1e3a344e4f | 112 | */ |
sam_grove | 0:9d1e3a344e4f | 113 | enum MMA845x_SA0 |
sam_grove | 0:9d1e3a344e4f | 114 | { |
sam_grove | 0:9d1e3a344e4f | 115 | SA0_VSS = 0, /*!< SA0 connected to VSS */ |
sam_grove | 0:9d1e3a344e4f | 116 | SA0_VDD /*!< SA0 connected to VDD */ |
sam_grove | 0:9d1e3a344e4f | 117 | }; |
sam_grove | 0:9d1e3a344e4f | 118 | |
sam_grove | 0:9d1e3a344e4f | 119 | /** |
sam_grove | 0:9d1e3a344e4f | 120 | * @enum MMA845x_WHO_AM_I |
sam_grove | 0:9d1e3a344e4f | 121 | * @brief Device ID's that this class is compatible with |
sam_grove | 0:9d1e3a344e4f | 122 | */ |
sam_grove | 0:9d1e3a344e4f | 123 | enum MMA845x_WHO_AM_I |
sam_grove | 0:9d1e3a344e4f | 124 | { |
sam_grove | 0:9d1e3a344e4f | 125 | MMA8451 = 0x1a, /*!< MMA8451 WHO_AM_I register content */ |
sam_grove | 0:9d1e3a344e4f | 126 | MMA8452 = 0x2a, /*!< MMA8452 WHO_AM_I register content */ |
sam_grove | 0:9d1e3a344e4f | 127 | MMA8453 = 0x3a, /*!< MMA8453 WHO_AM_I register content */ |
sam_grove | 0:9d1e3a344e4f | 128 | }; |
sam_grove | 0:9d1e3a344e4f | 129 | |
sam_grove | 0:9d1e3a344e4f | 130 | /** |
sam_grove | 0:9d1e3a344e4f | 131 | * @enum MMA845x_REGISTER |
sam_grove | 0:9d1e3a344e4f | 132 | * @brief The device register map |
sam_grove | 0:9d1e3a344e4f | 133 | */ |
sam_grove | 0:9d1e3a344e4f | 134 | enum MMA845x_REGISTER |
sam_grove | 0:9d1e3a344e4f | 135 | { |
sam_grove | 0:9d1e3a344e4f | 136 | STATUS = 0x0, |
sam_grove | 0:9d1e3a344e4f | 137 | OUT_X_MSB, OUT_X_LSB, OUT_Y_MSB, OUT_Y_LSB, OUT_Z_MSB, OUT_Z_LSB, |
sam_grove | 0:9d1e3a344e4f | 138 | |
sam_grove | 0:9d1e3a344e4f | 139 | F_SETUP = 0x9, TRIG_CFG, // only available on the MMA8451 variant |
sam_grove | 0:9d1e3a344e4f | 140 | |
sam_grove | 0:9d1e3a344e4f | 141 | SYSMOD = 0xb, |
sam_grove | 0:9d1e3a344e4f | 142 | INT_SOURCE, WHO_AM_I, XYZ_DATA_CFG, HP_FILTER_CUTOFF, PL_STATUS, |
sam_grove | 0:9d1e3a344e4f | 143 | PL_CFG, PL_COUNT, PL_BF_ZCOMP, P_L_THS_REG, FF_MT_CFG, FF_MT_SRC, |
sam_grove | 0:9d1e3a344e4f | 144 | FF_MT_THS, FF_MT_COUNT, |
sam_grove | 0:9d1e3a344e4f | 145 | |
sam_grove | 0:9d1e3a344e4f | 146 | TRANSIENT_CFG = 0x1d, |
sam_grove | 0:9d1e3a344e4f | 147 | TRANSIENT_SRC, TRANSIENT_THS, TRANSIENT_COUNT, PULSE_CFG, PULSE_SRC, |
sam_grove | 0:9d1e3a344e4f | 148 | PULSE_THSX, PULSE_THSY, PULSE_THSZ, PULSE_TMLT, PULSE_LTCY, PULSE_WIND, |
sam_grove | 0:9d1e3a344e4f | 149 | ASLP_COUNT, CTRL_REG1, CTRL_REG2, CTRL_REG3, CTRL_REG4, CTRL_REG5, |
sam_grove | 0:9d1e3a344e4f | 150 | OFF_X, OFF_Y, OFF_Z |
sam_grove | 0:9d1e3a344e4f | 151 | }; |
sam_grove | 0:9d1e3a344e4f | 152 | |
sam_grove | 0:9d1e3a344e4f | 153 | /** Create the MMA845x object |
sam_grove | 0:9d1e3a344e4f | 154 | * @param i2c - A defined I2C object |
sam_grove | 0:9d1e3a344e4f | 155 | * @param int1 - A defined InterruptIn object |
sam_grove | 0:9d1e3a344e4f | 156 | * @param int2 - A defined InterruptIn object |
sam_grove | 0:9d1e3a344e4f | 157 | * @param i2c_addr - Connection of the address line |
sam_grove | 0:9d1e3a344e4f | 158 | */ |
sam_grove | 0:9d1e3a344e4f | 159 | MMA845x(I2C &i2c, InterruptIn &int1, InterruptIn &int2, MMA845x_SA0 const i2c_addr); |
sam_grove | 0:9d1e3a344e4f | 160 | |
sam_grove | 0:9d1e3a344e4f | 161 | /** Get the X data |
sam_grove | 0:9d1e3a344e4f | 162 | * @return The last valid reading from the accelerometer |
sam_grove | 0:9d1e3a344e4f | 163 | */ |
sam_grove | 0:9d1e3a344e4f | 164 | uint16_t getX(void) const; |
sam_grove | 0:9d1e3a344e4f | 165 | |
sam_grove | 0:9d1e3a344e4f | 166 | /** Get the Y data |
sam_grove | 0:9d1e3a344e4f | 167 | * @return The last valid reading from the accelerometer |
sam_grove | 0:9d1e3a344e4f | 168 | */ |
sam_grove | 0:9d1e3a344e4f | 169 | uint16_t getY(void) const; |
sam_grove | 0:9d1e3a344e4f | 170 | |
sam_grove | 0:9d1e3a344e4f | 171 | /** Get the Z data |
sam_grove | 0:9d1e3a344e4f | 172 | * @return The last valid reading from the accelerometer |
sam_grove | 0:9d1e3a344e4f | 173 | */ |
sam_grove | 0:9d1e3a344e4f | 174 | uint16_t getZ(void) const; |
sam_grove | 0:9d1e3a344e4f | 175 | |
sam_grove | 0:9d1e3a344e4f | 176 | /** Get the XYZ data structure |
sam_grove | 0:9d1e3a344e4f | 177 | * @return The last valid reading from the accelerometer |
sam_grove | 0:9d1e3a344e4f | 178 | */ |
sam_grove | 0:9d1e3a344e4f | 179 | MMA845x_DATA getXYZ(void) const; |
sam_grove | 0:9d1e3a344e4f | 180 | |
sam_grove | 0:9d1e3a344e4f | 181 | void enableDataReadyMode(void) const; |
sam_grove | 0:9d1e3a344e4f | 182 | void enableMotionMode(void) const; |
sam_grove | 0:9d1e3a344e4f | 183 | void enablePulseMode(void) const; |
sam_grove | 0:9d1e3a344e4f | 184 | void enableOrientationMode(void) const; |
sam_grove | 0:9d1e3a344e4f | 185 | void enableTransitMode(void) const; |
sam_grove | 0:9d1e3a344e4f | 186 | void enableAutoSleepMode(void) const; |
sam_grove | 0:9d1e3a344e4f | 187 | void enableFIFOMode(void) const; |
sam_grove | 0:9d1e3a344e4f | 188 | |
sam_grove | 0:9d1e3a344e4f | 189 | /** Put the MMA845x in the lowest possible power mode and suspend operation |
sam_grove | 0:9d1e3a344e4f | 190 | */ |
sam_grove | 0:9d1e3a344e4f | 191 | void disable(void); |
sam_grove | 0:9d1e3a344e4f | 192 | |
sam_grove | 0:9d1e3a344e4f | 193 | /** Write to a register (exposed for debugging reasons) |
sam_grove | 0:9d1e3a344e4f | 194 | * Note: most writes are only valid in stop mode |
sam_grove | 0:9d1e3a344e4f | 195 | * @param reg - The register to be written |
sam_grove | 0:9d1e3a344e4f | 196 | * @param data - The data to be written |
sam_grove | 0:9d1e3a344e4f | 197 | */ |
sam_grove | 0:9d1e3a344e4f | 198 | void writeRegister(uint8_t const reg, uint8_t const data) const; |
sam_grove | 0:9d1e3a344e4f | 199 | |
sam_grove | 0:9d1e3a344e4f | 200 | /** Read from a register (exposed for debugging reasons) |
sam_grove | 0:9d1e3a344e4f | 201 | * @param reg - The register to read from |
sam_grove | 0:9d1e3a344e4f | 202 | * @return The register contents |
sam_grove | 0:9d1e3a344e4f | 203 | */ |
sam_grove | 0:9d1e3a344e4f | 204 | uint8_t readRegister(uint8_t const reg) const; |
sam_grove | 0:9d1e3a344e4f | 205 | |
sam_grove | 0:9d1e3a344e4f | 206 | /** print the register map and values to the console |
sam_grove | 0:9d1e3a344e4f | 207 | */ |
sam_grove | 0:9d1e3a344e4f | 208 | void registerDump(void) const; |
sam_grove | 0:9d1e3a344e4f | 209 | |
sam_grove | 0:9d1e3a344e4f | 210 | private: |
sam_grove | 0:9d1e3a344e4f | 211 | |
sam_grove | 0:9d1e3a344e4f | 212 | I2C *_i2c; |
sam_grove | 0:9d1e3a344e4f | 213 | InterruptIn *_int1; |
sam_grove | 0:9d1e3a344e4f | 214 | InterruptIn *_int2; |
sam_grove | 0:9d1e3a344e4f | 215 | uint8_t _i2c_addr; |
sam_grove | 0:9d1e3a344e4f | 216 | MMA845x_DATA _data; |
sam_grove | 0:9d1e3a344e4f | 217 | |
sam_grove | 0:9d1e3a344e4f | 218 | void init(void) const; |
sam_grove | 0:9d1e3a344e4f | 219 | }; |
sam_grove | 0:9d1e3a344e4f | 220 | |
sam_grove | 0:9d1e3a344e4f | 221 | #endif |