MMA8451Q8g
Fork of MMA8451Q8b by
MMA8451Q8g.cpp@9:3229b6691c89, 2015-02-09 (annotated)
- Committer:
- scohennm
- Date:
- Mon Feb 09 20:05:35 2015 +0000
- Revision:
- 9:3229b6691c89
- Child:
- 12:12b18fdb72f7
Update for generic tap program
Who changed what in which revision?
User | Revision | Line number | New 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 | 9:3229b6691c89 | 59 | void MMA8451Q::setGLimit(int gSelect) { |
scohennm | 9:3229b6691c89 | 60 | uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; |
scohennm | 9:3229b6691c89 | 61 | gChosen = gSelect; |
scohennm | 9:3229b6691c89 | 62 | writeRegs(data, NUM_DATA); // put in standby |
scohennm | 9:3229b6691c89 | 63 | data[ADDRESS_INDEX ] = XYZ_DATA_CFG; |
scohennm | 9:3229b6691c89 | 64 | data[DATA_INDEX] = gChosen; |
scohennm | 9:3229b6691c89 | 65 | writeRegs(data, 2);// change g limit |
scohennm | 9:3229b6691c89 | 66 | data[ADDRESS_INDEX ] = REG_CTRL_REG_1; |
scohennm | 9:3229b6691c89 | 67 | data[DATA_INDEX] = 0x01; |
scohennm | 9:3229b6691c89 | 68 | writeRegs(data, 2); // make active |
scohennm | 9:3229b6691c89 | 69 | } |
scohennm | 9:3229b6691c89 | 70 | // add Pusle configuration register access |
scohennm | 9:3229b6691c89 | 71 | // Also add function prototype to .h file |
scohennm | 9:3229b6691c89 | 72 | void MMA8451Q::setPulseConfiguration(uint8_t latch,uint8_t axisSet) { |
scohennm | 9:3229b6691c89 | 73 | uint8_t configBits; |
scohennm | 9:3229b6691c89 | 74 | uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; // put in standby mode |
scohennm | 9:3229b6691c89 | 75 | // set up configuration bits. |
scohennm | 9:3229b6691c89 | 76 | configBits = latch | axisSet; |
scohennm | 9:3229b6691c89 | 77 | writeRegs(data, NUM_DATA); // put in standby |
scohennm | 9:3229b6691c89 | 78 | data[ADDRESS_INDEX ] = REG_PULSE_CFG; |
scohennm | 9:3229b6691c89 | 79 | data[DATA_INDEX] = configBits; |
scohennm | 9:3229b6691c89 | 80 | writeRegs(data, NUM_DATA); |
scohennm | 9:3229b6691c89 | 81 | data[ADDRESS_INDEX ] = REG_CTRL_REG_1; // put back in active mode |
scohennm | 9:3229b6691c89 | 82 | data[DATA_INDEX] = 0x01; |
scohennm | 9:3229b6691c89 | 83 | writeRegs(data, 2); // make active |
scohennm | 9:3229b6691c89 | 84 | } |
scohennm | 9:3229b6691c89 | 85 | |
scohennm | 9:3229b6691c89 | 86 | // Make a genric register updater for those commands that need to put the |
scohennm | 9:3229b6691c89 | 87 | // accelerometer in standby mode |
scohennm | 9:3229b6691c89 | 88 | void MMA8451Q::setRegisterInStandby(uint8_t regAddress,uint8_t regData) { |
scohennm | 9:3229b6691c89 | 89 | uint8_t configBits; |
scohennm | 9:3229b6691c89 | 90 | uint8_t data[NUM_DATA] = {REG_CTRL_REG_1, 0x00}; // put in standby mode |
scohennm | 9:3229b6691c89 | 91 | // set up configuration bits. |
scohennm | 9:3229b6691c89 | 92 | configBits = regData; |
scohennm | 9:3229b6691c89 | 93 | writeRegs(data, NUM_DATA); // put in standby |
scohennm | 9:3229b6691c89 | 94 | data[ADDRESS_INDEX ] = regAddress; |
scohennm | 9:3229b6691c89 | 95 | data[DATA_INDEX] = configBits; |
scohennm | 9:3229b6691c89 | 96 | writeRegs(data, NUM_DATA); |
scohennm | 9:3229b6691c89 | 97 | data[ADDRESS_INDEX ] = REG_CTRL_REG_1; // put back in active mode |
scohennm | 9:3229b6691c89 | 98 | data[DATA_INDEX] = 0x01; |
scohennm | 9:3229b6691c89 | 99 | writeRegs(data, 2); // make active |
scohennm | 9:3229b6691c89 | 100 | } |
scohennm | 9:3229b6691c89 | 101 | |
scohennm | 9:3229b6691c89 | 102 | float MMA8451Q::getAccX() { |
scohennm | 9:3229b6691c89 | 103 | return (float(getAccAxis(REG_OUT_X_MSB))/gScaling[gChosen]); |
scohennm | 9:3229b6691c89 | 104 | } |
scohennm | 9:3229b6691c89 | 105 | |
scohennm | 9:3229b6691c89 | 106 | |
scohennm | 9:3229b6691c89 | 107 | float MMA8451Q::getAccY() { |
scohennm | 9:3229b6691c89 | 108 | return (float(getAccAxis(REG_OUT_Y_MSB))/gScaling[gChosen]); |
scohennm | 9:3229b6691c89 | 109 | } |
scohennm | 9:3229b6691c89 | 110 | |
scohennm | 9:3229b6691c89 | 111 | float MMA8451Q::getAccZ() { |
scohennm | 9:3229b6691c89 | 112 | return (float(getAccAxis(REG_OUT_Z_MSB))/gScaling[gChosen]); |
scohennm | 9:3229b6691c89 | 113 | } |
scohennm | 9:3229b6691c89 | 114 | |
scohennm | 9:3229b6691c89 | 115 | void MMA8451Q::getAccAllAxis(float * res) { |
scohennm | 9:3229b6691c89 | 116 | res[0] = getAccX(); |
scohennm | 9:3229b6691c89 | 117 | res[1] = getAccY(); |
scohennm | 9:3229b6691c89 | 118 | res[2] = getAccZ(); |
scohennm | 9:3229b6691c89 | 119 | } |
scohennm | 9:3229b6691c89 | 120 | |
scohennm | 9:3229b6691c89 | 121 | int16_t MMA8451Q::getAccAxis(uint8_t addr) { |
scohennm | 9:3229b6691c89 | 122 | int16_t acc; |
scohennm | 9:3229b6691c89 | 123 | uint8_t res[2]; |
scohennm | 9:3229b6691c89 | 124 | readRegs(addr, res, 2); |
scohennm | 9:3229b6691c89 | 125 | |
scohennm | 9:3229b6691c89 | 126 | acc = (res[0] << 6) | (res[1] >> 2); |
scohennm | 9:3229b6691c89 | 127 | if (acc > UINT14_MAX/2) |
scohennm | 9:3229b6691c89 | 128 | acc -= UINT14_MAX; |
scohennm | 9:3229b6691c89 | 129 | |
scohennm | 9:3229b6691c89 | 130 | return acc; |
scohennm | 9:3229b6691c89 | 131 | } |
scohennm | 9:3229b6691c89 | 132 | |
scohennm | 9:3229b6691c89 | 133 | void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { |
scohennm | 9:3229b6691c89 | 134 | char t[1] = {addr}; |
scohennm | 9:3229b6691c89 | 135 | m_i2c.write(m_addr, t, 1, true); |
scohennm | 9:3229b6691c89 | 136 | m_i2c.read(m_addr, (char *)data, len); |
scohennm | 9:3229b6691c89 | 137 | } |
scohennm | 9:3229b6691c89 | 138 | |
scohennm | 9:3229b6691c89 | 139 | void MMA8451Q::writeRegs(uint8_t * data, int len) { |
scohennm | 9:3229b6691c89 | 140 | m_i2c.write(m_addr, (char *)data, len); |
scohennm | 9:3229b6691c89 | 141 | } |