a
Fork of FRDM_MMA8451Q by
MMA8451Q.cpp
- Committer:
- clemente
- Date:
- 2013-05-28
- Revision:
- 5:695063448f2a
- Parent:
- 3:db7126dbd63f
- Child:
- 6:c52175d13e0a
File content as of revision 5:695063448f2a:
/* Copyright (c) 2010-2011 mbed.org, MIT License * * Permission is hereby granted, free of charge, to any person obtaining a copy of this software * and associated documentation files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the * Software is furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all copies or * substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */ #include "MMA8451Q.h" #define REG_WHO_AM_I 0x0D #define REG_CTRL_REG_1 0x2A #define REG_CTRL_REG_4 0x2D #define REG_CTRL_REG_5 0x2E #define REG_INT_SRC 0x0C #define REG_FF_MT_CFG 0x15 #define REG_FF_MT_SRC 0x16 #define REG_FF_MT_THS 0x17 #define REG_FF_MT_CNT 0x18 // #define REG_OUT_X_MSB 0x01 #define REG_OUT_Y_MSB 0x03 #define REG_OUT_Z_MSB 0x05 #define UINT14_MAX 16383 void (*fall_fptr)(void); void (*motion_fptr)(void); // InterruptIn MMA8451Q_Int1( PTA14); InterruptIn MMA8451Q_Int2( PTA15); MMA8451Q::MMA8451Q(PinName sda, PinName scl, int addr) : m_i2c(sda, scl), m_addr(addr) { // activate the peripheral uint8_t data[2] = {REG_CTRL_REG_1, 0x01}; writeRegs(data, 2); } MMA8451Q::~MMA8451Q() { MMA8451Q_Int1.fall( NULL); MMA8451Q_Int2.fall( NULL); fall_fptr = NULL; motion_fptr = NULL; } void MMA8451Q::FreFallDetection( void(*fptr)(void)) { // Example Steps for Configuring Linear Freefall Detection // X AND Y AND Z < 0.2g using MFF Function, 50 Hz ODR // Step 1: Put the device in Standby Mode: Register 0x2A CTRL_REG1 unsigned char data[2] = {REG_CTRL_REG_1, 0x20}; writeRegs(data, 2); // Step 2: Configuration Register set for Freefall Detection enabling “AND” condition, OAE = 0, Enabling X, // Y, Z and the Latch data[0] = REG_FF_MT_CFG; data[1] = 0x01; writeRegs(data, 2); // Step 3: Threshold Setting Value for the resulting acceleration < 0.2g // Note: The step count is 0.063g/count // • 0.2g/0.063g = 3.17 counts //Round to 3 counts data[0] = REG_FF_MT_THS; data[1] = 0x03; writeRegs(data, 2); // Step 4: Set the debounce counter to eliminate false positive readings for 50Hz sample rate with a // requirement of 120 ms timer, assuming Normal Mode. // Note: 120 ms/20 ms (steps) = 6 counts data[0] = REG_FF_MT_CNT; data[1] = 0x06; writeRegs(data, 2); // Step 5: Enable Motion/Freefall Interrupt Function in the System (CTRL_REG4) data[0] = REG_CTRL_REG_4; data[1] = 0x04; writeRegs(data, 2); // Step 6: Route the Motion/Freefall Interrupt Function to INT2 hardware pin (CTRL_REG5) data[0] = REG_CTRL_REG_5; data[1] = 0x00; writeRegs(data, 2); // Step 7: Put the device in Active Mode, 50 Hz data[0] = REG_CTRL_REG_1; data[1] = 0x21; writeRegs(data, 2); fall_fptr = fptr; MMA8451Q_Int2.fall( this, &MMA8451Q::Fall_IRQ); } void MMA8451Q::Fall_IRQ( void) { unsigned char t; // Determine source of the interrupt by first reading the system interrupt readRegs( REG_INT_SRC, &t, 1); // if ( (t & 0x04) == 0x04) { // Read the Motion/Freefall Function to clear the interrupt readRegs( REG_FF_MT_SRC, &t, 1); // Run the user supplied function fall_fptr(); } } void MMA8451Q::MotionDetection( void(*fptr)(void)) { // 6.1 Example Steps for Configuring Motion Detection // X or Y > 3g using MFF Function 4g, 100 Hz ODR, Normal Mode // Step 1: Put the device into Standby Mode: Register 0x2A CTRL_REG1 unsigned char data[2] = {REG_CTRL_REG_1, 0x18}; // Set the device in 100 Hz ODR, Standby writeRegs(data, 2); // Step 2: Set Configuration Register for Motion Detection by setting the “OR” condition OAE = 1, enabling // X, Y, and the latch data[0] = REG_FF_MT_CFG; data[1] = 0xD8; writeRegs(data, 2); // Step 3: Threshold Setting Value for the Motion detection of > 2g // Note: The step count is 0.063g/ count // • 2g/0.063g = 31.7; //Round up to 32 data[0] = REG_FF_MT_THS; data[1] = 0x20; writeRegs(data, 2); // Step 4: Set the debounce counter to eliminate false readings for 100 Hz sample rate with a requirement // of 100 ms timer. // Note: 100 ms/10 ms (steps) = 10 counts data[0] = REG_FF_MT_CNT; data[1] = 0x0A; writeRegs(data, 2); // Step 5: Enable Motion/Freefall Interrupt Function in the System (CTRL_REG4) data[0] = REG_CTRL_REG_4; data[1] = 0x04; writeRegs(data, 2); // Step 6: Route the Motion/Freefall Interrupt Function to INT1 hardware pin (CTRL_REG5) data[0] = REG_CTRL_REG_5; data[1] = 0x04; writeRegs(data, 2); // Step 7: Put the device in Active Mode data[0] = REG_CTRL_REG_1; data[1] = 0x19; writeRegs(data, 2); motion_fptr = fptr; MMA8451Q_Int1.fall( this, &MMA8451Q::Motion_IRQ); } void MMA8451Q::Motion_IRQ( void) { unsigned char t; // Determine source of the interrupt by first reading the system interrupt readRegs( REG_INT_SRC, &t, 1); // if ( (t & 0x04) == 0x04) { // Read the Motion/Freefall Function to clear the interrupt readRegs( REG_FF_MT_SRC, &t, 1); // Run the user supplied function motion_fptr(); } } void MMA8451Q::Active( void) { unsigned char t; // Activate the peripheral readRegs(REG_CTRL_REG_1, &t, 1); unsigned char data[2] = {REG_CTRL_REG_1, t|0x01}; writeRegs(data, 2); } void MMA8451Q::Standby( void) { unsigned char t; // Standby readRegs(REG_CTRL_REG_1, &t, 1); unsigned char data[2] = {REG_CTRL_REG_1, t&0xFE}; writeRegs(data, 2); } uint8_t MMA8451Q::getWhoAmI() { uint8_t who_am_i = 0; readRegs(REG_WHO_AM_I, &who_am_i, 1); return who_am_i; } float MMA8451Q::getAccX() { return (float(getAccAxis(REG_OUT_X_MSB))/4096.0); } float MMA8451Q::getAccY() { return (float(getAccAxis(REG_OUT_Y_MSB))/4096.0); } float MMA8451Q::getAccZ() { return (float(getAccAxis(REG_OUT_Z_MSB))/4096.0); } void MMA8451Q::getAccAllAxis(float * res) { res[0] = getAccX(); res[1] = getAccY(); res[2] = getAccZ(); } int16_t MMA8451Q::getAccAxis(uint8_t addr) { int16_t acc; uint8_t res[2]; readRegs(addr, res, 2); acc = (res[0] << 6) | (res[1] >> 2); if (acc > UINT14_MAX/2) acc -= UINT14_MAX; return acc; } void MMA8451Q::readRegs(int addr, uint8_t * data, int len) { char t[1] = {addr}; m_i2c.write(m_addr, t, 1, true); m_i2c.read(m_addr, (char *)data, len); } void MMA8451Q::writeRegs(uint8_t * data, int len) { m_i2c.write(m_addr, (char *)data, len); }