MMA8452Q I2C library Note: The I2C address is expected to be 7bit address unlike other I2C lib in mbed.

Dependents:   test_MMA8452Q HelloWorld testSensor

Committer:
Rhyme
Date:
Sun Dec 13 08:05:49 2015 +0000
Revision:
0:c315c3fd48fb
First commit as a library;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Rhyme 0:c315c3fd48fb 1 /* Copyright (c) 2013-2015 Design Methodology Lab
Rhyme 0:c315c3fd48fb 2 * This driver was delived from MMA8451Q driver at mbed
Rhyme 0:c315c3fd48fb 3 */
Rhyme 0:c315c3fd48fb 4 /*
Rhyme 0:c315c3fd48fb 5 * Copyright (c) 2010-2011 mbed.org, MIT License
Rhyme 0:c315c3fd48fb 6 *
Rhyme 0:c315c3fd48fb 7 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
Rhyme 0:c315c3fd48fb 8 * and associated documentation files (the "Software"), to deal in the Software without
Rhyme 0:c315c3fd48fb 9 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
Rhyme 0:c315c3fd48fb 10 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
Rhyme 0:c315c3fd48fb 11 * Software is furnished to do so, subject to the following conditions:
Rhyme 0:c315c3fd48fb 12 *
Rhyme 0:c315c3fd48fb 13 * The above copyright notice and this permission notice shall be included in all copies or
Rhyme 0:c315c3fd48fb 14 * substantial portions of the Software.
Rhyme 0:c315c3fd48fb 15 *
Rhyme 0:c315c3fd48fb 16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
Rhyme 0:c315c3fd48fb 17 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
Rhyme 0:c315c3fd48fb 18 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
Rhyme 0:c315c3fd48fb 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
Rhyme 0:c315c3fd48fb 20 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
Rhyme 0:c315c3fd48fb 21 */
Rhyme 0:c315c3fd48fb 22
Rhyme 0:c315c3fd48fb 23 #include "MMA8452Q.h"
Rhyme 0:c315c3fd48fb 24
Rhyme 0:c315c3fd48fb 25 #define REG_CTRL_REG_1 0x2A
Rhyme 0:c315c3fd48fb 26 #define REG_OUT_X_MSB 0x01
Rhyme 0:c315c3fd48fb 27 #define REG_OUT_X_LSB 0x02
Rhyme 0:c315c3fd48fb 28 #define REG_OUT_Y_MSB 0x03
Rhyme 0:c315c3fd48fb 29 #define REG_OUT_Y_LSB 0x04
Rhyme 0:c315c3fd48fb 30 #define REG_OUT_Z_MSB 0x05
Rhyme 0:c315c3fd48fb 31 #define REG_OUT_Z_LSB 0x06
Rhyme 0:c315c3fd48fb 32 #define REG_SYSMOD 0x0B
Rhyme 0:c315c3fd48fb 33 #define REG_WHO_AM_I 0x0D
Rhyme 0:c315c3fd48fb 34 #define REG_ZYZ_DATA_CFG 0x0E
Rhyme 0:c315c3fd48fb 35 #define REG_HP_FILTER_CUTOFF 0x0F
Rhyme 0:c315c3fd48fb 36 #define REG_PL_STATUS 0x10
Rhyme 0:c315c3fd48fb 37 #define REG_PL_CFG 0x11
Rhyme 0:c315c3fd48fb 38 #define REG_PL_COUNT 0x12
Rhyme 0:c315c3fd48fb 39 #define REG_PL_BF_ZCOMP 0x13
Rhyme 0:c315c3fd48fb 40 #define REG_P_L_THS_REG 0x14
Rhyme 0:c315c3fd48fb 41 #define REG_FF_MT_CFG 0x15
Rhyme 0:c315c3fd48fb 42 #define REG_FF_MT_SRC 0x16
Rhyme 0:c315c3fd48fb 43 #define REG_FF_MT_THS 0x17
Rhyme 0:c315c3fd48fb 44 #define REG_FF_MT_COUNT 0x18
Rhyme 0:c315c3fd48fb 45 #define REG_TRANSIENT_CFG 0x1D
Rhyme 0:c315c3fd48fb 46 #define REG_TRANSIENT_SRC 0x1E
Rhyme 0:c315c3fd48fb 47 #define REG_TRANSIENT_THS 0x1F
Rhyme 0:c315c3fd48fb 48 #define REG_TRANSIENT_COUNT 0x20
Rhyme 0:c315c3fd48fb 49 #define REG_PULSE_CFG 0x21
Rhyme 0:c315c3fd48fb 50 #define REG_PULSE_SRC 0x22
Rhyme 0:c315c3fd48fb 51 #define REG_PULSE_THSX 0x23
Rhyme 0:c315c3fd48fb 52 #define REG_PULSE_THSY 0x24
Rhyme 0:c315c3fd48fb 53 #define REG_PULSE_THSZ 0x25
Rhyme 0:c315c3fd48fb 54 #define REG_PULSE_TMLT 0x26
Rhyme 0:c315c3fd48fb 55 #define REG_PULSE_LTCY 0x27
Rhyme 0:c315c3fd48fb 56 #define REG_PULSE_WIND 0x28
Rhyme 0:c315c3fd48fb 57 #define REG_ASLP_COUNT 0x29
Rhyme 0:c315c3fd48fb 58 #define REG_CTRL_REG1 0x2A
Rhyme 0:c315c3fd48fb 59 #define REG_CTRL_REG2 0x2B
Rhyme 0:c315c3fd48fb 60 #define REG_CTRL_REG3 0x2C
Rhyme 0:c315c3fd48fb 61 #define REG_CTRL_REG4 0x2D
Rhyme 0:c315c3fd48fb 62 #define REG_CTRL_REG5 0x2E
Rhyme 0:c315c3fd48fb 63 #define REG_OFF_X 0x2F
Rhyme 0:c315c3fd48fb 64 #define REG_OFF_Y 0x30
Rhyme 0:c315c3fd48fb 65 #define REG_OFF_Z 0x31
Rhyme 0:c315c3fd48fb 66
Rhyme 0:c315c3fd48fb 67 #define UINT14_MAX 16383
Rhyme 0:c315c3fd48fb 68
Rhyme 0:c315c3fd48fb 69 MMA8452Q::MMA8452Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) {
Rhyme 0:c315c3fd48fb 70 // activate the peripheral
Rhyme 0:c315c3fd48fb 71 uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
Rhyme 0:c315c3fd48fb 72 writeRegs(data, 2);
Rhyme 0:c315c3fd48fb 73 }
Rhyme 0:c315c3fd48fb 74
Rhyme 0:c315c3fd48fb 75 MMA8452Q::~MMA8452Q() { }
Rhyme 0:c315c3fd48fb 76
Rhyme 0:c315c3fd48fb 77 uint8_t MMA8452Q::getWhoAmI() {
Rhyme 0:c315c3fd48fb 78 uint8_t who_am_i = 0;
Rhyme 0:c315c3fd48fb 79 readRegs(REG_WHO_AM_I, &who_am_i, 1);
Rhyme 0:c315c3fd48fb 80 return who_am_i;
Rhyme 0:c315c3fd48fb 81 }
Rhyme 0:c315c3fd48fb 82
Rhyme 0:c315c3fd48fb 83 float MMA8452Q::getAccX() {
Rhyme 0:c315c3fd48fb 84 return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
Rhyme 0:c315c3fd48fb 85 }
Rhyme 0:c315c3fd48fb 86
Rhyme 0:c315c3fd48fb 87 float MMA8452Q::getAccY() {
Rhyme 0:c315c3fd48fb 88 return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0);
Rhyme 0:c315c3fd48fb 89 }
Rhyme 0:c315c3fd48fb 90
Rhyme 0:c315c3fd48fb 91 float MMA8452Q::getAccZ() {
Rhyme 0:c315c3fd48fb 92 return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0);
Rhyme 0:c315c3fd48fb 93 }
Rhyme 0:c315c3fd48fb 94
Rhyme 0:c315c3fd48fb 95 void MMA8452Q::getAccAllAxis(float * res) {
Rhyme 0:c315c3fd48fb 96 res[0] = getAccX();
Rhyme 0:c315c3fd48fb 97 res[1] = getAccY();
Rhyme 0:c315c3fd48fb 98 res[2] = getAccZ();
Rhyme 0:c315c3fd48fb 99 }
Rhyme 0:c315c3fd48fb 100
Rhyme 0:c315c3fd48fb 101 int16_t MMA8452Q::getAccAxis(uint8_t addr) {
Rhyme 0:c315c3fd48fb 102 int16_t acc;
Rhyme 0:c315c3fd48fb 103 uint8_t res[2];
Rhyme 0:c315c3fd48fb 104 readRegs(addr, res, 2);
Rhyme 0:c315c3fd48fb 105
Rhyme 0:c315c3fd48fb 106 acc = (res[0] << 6) | (res[1] >> 2);
Rhyme 0:c315c3fd48fb 107 if (acc > UINT14_MAX/2)
Rhyme 0:c315c3fd48fb 108 acc -= UINT14_MAX;
Rhyme 0:c315c3fd48fb 109
Rhyme 0:c315c3fd48fb 110 return acc;
Rhyme 0:c315c3fd48fb 111 }
Rhyme 0:c315c3fd48fb 112
Rhyme 0:c315c3fd48fb 113 void MMA8452Q::readRegs(int addr, uint8_t * data, int len) {
Rhyme 0:c315c3fd48fb 114 char t[1] = {addr};
Rhyme 0:c315c3fd48fb 115 m_i2c.write(m_addr, t, 1, true);
Rhyme 0:c315c3fd48fb 116 m_i2c.read(m_addr, (char *)data, len);
Rhyme 0:c315c3fd48fb 117 }
Rhyme 0:c315c3fd48fb 118
Rhyme 0:c315c3fd48fb 119 void MMA8452Q::writeRegs(uint8_t * data, int len) {
Rhyme 0:c315c3fd48fb 120 m_i2c.write(m_addr, (char *)data, len);
Rhyme 0:c315c3fd48fb 121 }