MSS / MMA8452Q

Dependents:   test_MMA8452Q HelloWorld testSensor

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MMA8452Q.cpp Source File

MMA8452Q.cpp

00001 /* Copyright (c) 2013-2015 Design Methodology Lab
00002  * This driver was delived from MMA8451Q driver at mbed
00003 */
00004 /*
00005 * Copyright (c) 2010-2011 mbed.org, MIT License
00006 *
00007 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00008 * and associated documentation files (the "Software"), to deal in the Software without
00009 * restriction, including without limitation the rights to use, copy, modify, merge, publish,
00010 * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the
00011 * Software is furnished to do so, subject to the following conditions:
00012 *
00013 * The above copyright notice and this permission notice shall be included in all copies or
00014 * substantial portions of the Software.
00015 *
00016 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00017 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00018 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00019 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00021 */
00022 
00023 #include "MMA8452Q.h"
00024 
00025 #define REG_CTRL_REG_1    0x2A
00026 #define REG_OUT_X_MSB     0x01
00027 #define REG_OUT_X_LSB     0x02
00028 #define REG_OUT_Y_MSB     0x03
00029 #define REG_OUT_Y_LSB     0x04
00030 #define REG_OUT_Z_MSB     0x05
00031 #define REG_OUT_Z_LSB     0x06
00032 #define REG_SYSMOD        0x0B
00033 #define REG_WHO_AM_I      0x0D
00034 #define REG_ZYZ_DATA_CFG  0x0E
00035 #define REG_HP_FILTER_CUTOFF 0x0F
00036 #define REG_PL_STATUS     0x10
00037 #define REG_PL_CFG        0x11
00038 #define REG_PL_COUNT      0x12
00039 #define REG_PL_BF_ZCOMP   0x13
00040 #define REG_P_L_THS_REG   0x14
00041 #define REG_FF_MT_CFG     0x15
00042 #define REG_FF_MT_SRC     0x16
00043 #define REG_FF_MT_THS     0x17
00044 #define REG_FF_MT_COUNT   0x18
00045 #define REG_TRANSIENT_CFG 0x1D
00046 #define REG_TRANSIENT_SRC 0x1E
00047 #define REG_TRANSIENT_THS 0x1F
00048 #define REG_TRANSIENT_COUNT 0x20
00049 #define REG_PULSE_CFG     0x21
00050 #define REG_PULSE_SRC     0x22
00051 #define REG_PULSE_THSX    0x23
00052 #define REG_PULSE_THSY    0x24
00053 #define REG_PULSE_THSZ    0x25
00054 #define REG_PULSE_TMLT    0x26
00055 #define REG_PULSE_LTCY    0x27
00056 #define REG_PULSE_WIND    0x28
00057 #define REG_ASLP_COUNT    0x29
00058 #define REG_CTRL_REG1     0x2A
00059 #define REG_CTRL_REG2     0x2B
00060 #define REG_CTRL_REG3     0x2C
00061 #define REG_CTRL_REG4     0x2D
00062 #define REG_CTRL_REG5     0x2E
00063 #define REG_OFF_X         0x2F
00064 #define REG_OFF_Y         0x30
00065 #define REG_OFF_Z         0x31
00066 
00067 #define UINT14_MAX        16383
00068 
00069 MMA8452Q::MMA8452Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr<<1) {
00070     // activate the peripheral
00071     uint8_t data[2] = {REG_CTRL_REG_1, 0x01};
00072     writeRegs(data, 2);
00073 }
00074 
00075 MMA8452Q::~MMA8452Q() { }
00076 
00077 uint8_t MMA8452Q::getWhoAmI() {
00078     uint8_t who_am_i = 0;
00079     readRegs(REG_WHO_AM_I, &who_am_i, 1);
00080     return who_am_i;
00081 }
00082 
00083 float MMA8452Q::getAccX() {
00084     return (float(getAccAxis(REG_OUT_X_MSB))/4096.0);
00085 }
00086 
00087 float MMA8452Q::getAccY() {
00088     return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0);
00089 }
00090 
00091 float MMA8452Q::getAccZ() {
00092     return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0);
00093 }
00094 
00095 void MMA8452Q::getAccAllAxis(float * res) {
00096     res[0] = getAccX();
00097     res[1] = getAccY();
00098     res[2] = getAccZ();
00099 }
00100 
00101 int16_t MMA8452Q::getAccAxis(uint8_t addr) {
00102     int16_t acc;
00103     uint8_t res[2];
00104     readRegs(addr, res, 2);
00105 
00106     acc = (res[0] << 6) | (res[1] >> 2);
00107     if (acc > UINT14_MAX/2)
00108         acc -= UINT14_MAX;
00109 
00110     return acc;
00111 }
00112 
00113 void MMA8452Q::readRegs(int addr, uint8_t * data, int len) {
00114     char t[1] = {addr};
00115     m_i2c.write(m_addr, t, 1, true);
00116     m_i2c.read(m_addr, (char *)data, len);
00117 }
00118 
00119 void MMA8452Q::writeRegs(uint8_t * data, int len) {
00120     m_i2c.write(m_addr, (char *)data, len);
00121 }