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