MMA8451Q8g

Fork of MMA8451Q8b by Stanley Cohen

Committer:
scohennm
Date:
Mon Feb 22 16:54:40 2016 +0000
Revision:
12:12b18fdb72f7
Parent:
9:3229b6691c89
Child:
13:b58b302c53f6
updated active/standby mode change

Who changed what in which revision?

UserRevisionLine numberNew contents of line
scohennm 9:3229b6691c89 1 /* Copyright (c) 2010-2011 mbed.org, MIT License
scohennm 9:3229b6691c89 2 *
scohennm 9:3229b6691c89 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
scohennm 9:3229b6691c89 4 * and associated documentation files (the "Software"), to deal in the Software without
scohennm 9:3229b6691c89 5 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
scohennm 9:3229b6691c89 6 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
scohennm 9:3229b6691c89 7 * Software is furnished to do so, subject to the following conditions:
scohennm 9:3229b6691c89 8 *
scohennm 9:3229b6691c89 9 * The above copyright notice and this permission notice shall be included in all copies or
scohennm 9:3229b6691c89 10 * substantial portions of the Software.
scohennm 9:3229b6691c89 11 *
scohennm 9:3229b6691c89 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
scohennm 9:3229b6691c89 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
scohennm 9:3229b6691c89 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
scohennm 9:3229b6691c89 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
scohennm 9:3229b6691c89 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
scohennm 9:3229b6691c89 17 */
scohennm 9:3229b6691c89 18 // Modify to change full scale gravity range 141207 sc
scohennm 9:3229b6691c89 19
scohennm 9:3229b6691c89 20
scohennm 9:3229b6691c89 21 #include "MMA8451Q8g.h"
scohennm 9:3229b6691c89 22
scohennm 9:3229b6691c89 23 #define REG_WHO_AM_I 0x0D
scohennm 9:3229b6691c89 24 #define REG_CTRL_REG_1 0x2A
scohennm 9:3229b6691c89 25 #define REG_OUT_X_MSB 0x01
scohennm 9:3229b6691c89 26 #define REG_OUT_Y_MSB 0x03
scohennm 9:3229b6691c89 27 #define REG_OUT_Z_MSB 0x05
scohennm 9:3229b6691c89 28 #define XYZ_DATA_CFG 0x0E
scohennm 9:3229b6691c89 29 // adding tap config register adderesses
scohennm 9:3229b6691c89 30 #define REG_PULSE_SRC 0x22
scohennm 9:3229b6691c89 31 #define REG_PULSE_CFG 0x21
scohennm 9:3229b6691c89 32
scohennm 9:3229b6691c89 33 #define UINT14_MAX 16383
scohennm 9:3229b6691c89 34
scohennm 9:3229b6691c89 35 #define MAX_2G 0x00
scohennm 9:3229b6691c89 36 #define MAX_4G 0x01
scohennm 9:3229b6691c89 37 #define MAX_8G 0x02
scohennm 9:3229b6691c89 38
scohennm 9:3229b6691c89 39 #define NUM_DATA 2
scohennm 9:3229b6691c89 40 #define GSCALING 1024.0
scohennm 9:3229b6691c89 41 #define ADDRESS_INDEX 0
scohennm 9:3229b6691c89 42 #define DATA_INDEX 1
scohennm 9:3229b6691c89 43
scohennm 9:3229b6691c89 44 float gScaling[3] = {4095.0,2048.0,1024.0}; //scaling for acceleration of gravity values
scohennm 9:3229b6691c89 45
scohennm 9:3229b6691c89 46 MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) {
scohennm 9:3229b6691c89 47 // activate the peripheral
scohennm 9:3229b6691c89 48 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
scohennm 9:3229b6691c89 49 writeRegs(data, 2);
scohennm 9:3229b6691c89 50 }
scohennm 9:3229b6691c89 51
scohennm 9:3229b6691c89 52 MMA8451Q::~MMA8451Q() { }
scohennm 9:3229b6691c89 53
scohennm 9:3229b6691c89 54 uint8_t MMA8451Q::getWhoAmI() {
scohennm 9:3229b6691c89 55 uint8_t who_am_i = 0;
scohennm 9:3229b6691c89 56 readRegs(REG_WHO_AM_I, &who_am_i, 1);
scohennm 9:3229b6691c89 57 return who_am_i;
scohennm 9:3229b6691c89 58 }
scohennm 12:12b18fdb72f7 59 void MMA8451Q::setStandbyMode(){
scohennm 12:12b18fdb72f7 60 #define ACTIVEMASK 0x01
scohennm 12:12b18fdb72f7 61 uint8_t registerData[1];
scohennm 12:12b18fdb72f7 62 uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
scohennm 12:12b18fdb72f7 63
scohennm 12:12b18fdb72f7 64 readRegs(REG_CTRL_REG_1, registerData, 1);
scohennm 12:12b18fdb72f7 65 data[1] = registerData[0] & ~ACTIVEMASK;
scohennm 12:12b18fdb72f7 66 writeRegs(data, NUM_DATA); // put in standby
scohennm 12:12b18fdb72f7 67 }
scohennm 12:12b18fdb72f7 68 void MMA8451Q::setActiveMode(){
scohennm 12:12b18fdb72f7 69 #define ACTIVEMASK 0x01
scohennm 12:12b18fdb72f7 70 uint8_t registerData[1];
scohennm 12:12b18fdb72f7 71 uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
scohennm 12:12b18fdb72f7 72
scohennm 12:12b18fdb72f7 73 readRegs(REG_CTRL_REG_1, registerData, 1);
scohennm 12:12b18fdb72f7 74 data[1] = registerData[0] | ACTIVEMASK;
scohennm 12:12b18fdb72f7 75 writeRegs(data, NUM_DATA); // put in standby
scohennm 12:12b18fdb72f7 76 }
scohennm 12:12b18fdb72f7 77
scohennm 9:3229b6691c89 78 void MMA8451Q::setGLimit(int gSelect) {
scohennm 9:3229b6691c89 79 uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00};
scohennm 9:3229b6691c89 80 gChosen = gSelect;
scohennm 12:12b18fdb72f7 81 setStandbyMode();
scohennm 9:3229b6691c89 82 data[ADDRESS_INDEX ] = XYZ_DATA_CFG;
scohennm 9:3229b6691c89 83 data[DATA_INDEX] = gChosen;
scohennm 9:3229b6691c89 84 writeRegs(data, 2);// change g limit
scohennm 12:12b18fdb72f7 85 setActiveMode(); // make active
scohennm 9:3229b6691c89 86 }
scohennm 9:3229b6691c89 87 // add Pusle configuration register access
scohennm 9:3229b6691c89 88 // Also add function prototype to .h file
scohennm 9:3229b6691c89 89 void MMA8451Q::setPulseConfiguration(uint8_t latch,uint8_t axisSet) {
scohennm 9:3229b6691c89 90 uint8_t configBits;
scohennm 12:12b18fdb72f7 91 uint8_t data[NUM_DATA] = {REG_PULSE_CFG, 0x00}; // put in standby mode
scohennm 9:3229b6691c89 92 // set up configuration bits.
scohennm 9:3229b6691c89 93 configBits = latch | axisSet;
scohennm 12:12b18fdb72f7 94 setStandbyMode(); // put in standby
scohennm 9:3229b6691c89 95 data[DATA_INDEX] = configBits;
scohennm 9:3229b6691c89 96 writeRegs(data, NUM_DATA);
scohennm 12:12b18fdb72f7 97 setActiveMode(); // make active
scohennm 9:3229b6691c89 98 }
scohennm 9:3229b6691c89 99
scohennm 9:3229b6691c89 100 // Make a genric register updater for those commands that need to put the
scohennm 9:3229b6691c89 101 // accelerometer in standby mode
scohennm 9:3229b6691c89 102 void MMA8451Q::setRegisterInStandby(uint8_t regAddress,uint8_t regData) {
scohennm 9:3229b6691c89 103 uint8_t configBits;
scohennm 9:3229b6691c89 104 uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; // put in standby mode
scohennm 9:3229b6691c89 105 // set up configuration bits.
scohennm 9:3229b6691c89 106 configBits = regData;
scohennm 12:12b18fdb72f7 107 setStandbyMode(); // put in standby
scohennm 9:3229b6691c89 108 data[ADDRESS_INDEX ] = regAddress;
scohennm 9:3229b6691c89 109 data[DATA_INDEX] = configBits;
scohennm 9:3229b6691c89 110 writeRegs(data, NUM_DATA);
scohennm 12:12b18fdb72f7 111 setActiveMode(); // put back in active mode
scohennm 9:3229b6691c89 112 }
scohennm 9:3229b6691c89 113
scohennm 9:3229b6691c89 114 float MMA8451Q::getAccX() {
scohennm 9:3229b6691c89 115 return (float(getAccAxis(REG_OUT_X_MSB))/gScaling[gChosen]);
scohennm 9:3229b6691c89 116 }
scohennm 9:3229b6691c89 117
scohennm 9:3229b6691c89 118
scohennm 9:3229b6691c89 119 float MMA8451Q::getAccY() {
scohennm 9:3229b6691c89 120 return (float(getAccAxis(REG_OUT_Y_MSB))/gScaling[gChosen]);
scohennm 9:3229b6691c89 121 }
scohennm 9:3229b6691c89 122
scohennm 9:3229b6691c89 123 float MMA8451Q::getAccZ() {
scohennm 9:3229b6691c89 124 return (float(getAccAxis(REG_OUT_Z_MSB))/gScaling[gChosen]);
scohennm 9:3229b6691c89 125 }
scohennm 9:3229b6691c89 126
scohennm 9:3229b6691c89 127 void MMA8451Q::getAccAllAxis(float * res) {
scohennm 9:3229b6691c89 128 res[0] = getAccX();
scohennm 9:3229b6691c89 129 res[1] = getAccY();
scohennm 9:3229b6691c89 130 res[2] = getAccZ();
scohennm 9:3229b6691c89 131 }
scohennm 9:3229b6691c89 132
scohennm 9:3229b6691c89 133 int16_t MMA8451Q::getAccAxis(uint8_t addr) {
scohennm 9:3229b6691c89 134 int16_t acc;
scohennm 9:3229b6691c89 135 uint8_t res[2];
scohennm 9:3229b6691c89 136 readRegs(addr, res, 2);
scohennm 9:3229b6691c89 137
scohennm 9:3229b6691c89 138 acc = (res[0] << 6) | (res[1] >> 2);
scohennm 9:3229b6691c89 139 if (acc > UINT14_MAX/2)
scohennm 9:3229b6691c89 140 acc -= UINT14_MAX;
scohennm 9:3229b6691c89 141
scohennm 9:3229b6691c89 142 return acc;
scohennm 9:3229b6691c89 143 }
scohennm 9:3229b6691c89 144
scohennm 9:3229b6691c89 145 void MMA8451Q::readRegs(int addr, uint8_t * data, int len) {
scohennm 9:3229b6691c89 146 char t[1] = {addr};
scohennm 9:3229b6691c89 147 m_i2c.write(m_addr, t, 1, true);
scohennm 9:3229b6691c89 148 m_i2c.read(m_addr, (char *)data, len);
scohennm 9:3229b6691c89 149 }
scohennm 9:3229b6691c89 150
scohennm 9:3229b6691c89 151 void MMA8451Q::writeRegs(uint8_t * data, int len) {
scohennm 9:3229b6691c89 152 m_i2c.write(m_addr, (char *)data, len);
scohennm 9:3229b6691c89 153 }