mma8451q driver

Dependents:   nRF51822_DataLogger_PowerImpulseCounter scpi_sx127x NAMote72_Utility

mma8451q.cpp

Committer:
dudmuck
Date:
2015-05-08
Revision:
1:778b685c3ad0
Parent:
0:cb0046a629c1
Child:
2:4bc96749141e

File content as of revision 1:778b685c3ad0:

#include "mma8451q.h"
/* turn on: CTRL_REG1 active_bit = 1
 * back to standby: CTRL_REG1 active_bit = 0
 */

/* STANDBY: SYSMOD = 00 */

/*
 * MMA8451 I2C address
 */ 
#define MMA8451_I2C_ADDRESS                          0x38 //0x1C


MMA8451Q::MMA8451Q(I2C& r, DigitalIn& int_pin) : m_i2c(r), m_int_pin(int_pin)
{
    /* INT pins on this chip default to push-pull output */
    write(MMA8451_CTRL_REG3, 0x01);    // set PP_OD
    /* INT1 and INT2 are tied together */
    
}

MMA8451Q::~MMA8451Q()
{
}

void MMA8451Q::read(uint8_t addr, uint8_t *dst_buf, int length)
{
    char cmd[2];

    cmd[0] = addr;
    if (m_i2c.write(MMA8451_I2C_ADDRESS, cmd, 1, true))
        printf("MMA write-fail %02x\n", addr);
    if (m_i2c.read(MMA8451_I2C_ADDRESS, (char *)dst_buf, length))
        printf("MMA read-fail\n");
}

uint8_t MMA8451Q::read_single(uint8_t addr)
{
    char cmd[2];

    cmd[0] = addr;
    if (m_i2c.write(MMA8451_I2C_ADDRESS, cmd, 1, true))
        printf("MMA write-fail %02x\n", addr);
    if (m_i2c.read(MMA8451_I2C_ADDRESS, cmd, 1))
        printf("MMA read-fail\n");

    return cmd[0];
}

void MMA8451Q::print_regs()
{
    printf("ID: %02x\n", read_single(MMA8451_ID));
    printf("sysmod:%02x\n", read_single(MMA8451_SYSMOD));
    ctrl_reg1.octet = read_single(MMA8451_CTRL_REG1);
    printf("ctrl_reg1:%02x\n", ctrl_reg1.octet);
    printf("ctrl_reg2:%02x\n", read_single(MMA8451_CTRL_REG2));
    printf("ctrl_reg3:%02x\n", read_single(MMA8451_CTRL_REG3)); /* TODO: PP_OD is bit 0 (1=open drain) */
    printf("(int en) ctrl_reg4:%02x\n", read_single(MMA8451_CTRL_REG4));
    printf("(int cfg) ctrl_reg5:%02x\n", read_single(MMA8451_CTRL_REG5));
    printf("status:%02x\n", read_single(MMA8451_STATUS)); 
    /* (interrupt status) int src at 0x0c (MMA8451_INT_SOURCE): data ready, motion/freefall, pulse, orientation, transient, auto sleep */
    printf("INT_SOURCE:%02x\n", read_single(MMA8451_INT_SOURCE)); 
}

void MMA8451Q::write(uint8_t addr, uint8_t data)
{
    uint8_t cmd[2];
    
    cmd[0] = addr;
    cmd[1] = data;

    if (m_i2c.write(MMA8451_I2C_ADDRESS, (char *)cmd, 2))
        printf("MMA write-fail %02x\n", addr);
}

void MMA8451Q::set_active(char arg)
{
    char cmd[2];
    
    cmd[0] = MMA8451_CTRL_REG1;
    cmd[1] = arg;

    if (m_i2c.write(MMA8451_I2C_ADDRESS, cmd, 2))
        printf("MMA write-fail %02x\n", cmd[0]);
}

bool MMA8451Q::get_active(void)
{
    uint8_t ret = read_single(MMA8451_CTRL_REG1);
    //printf("CTRL_REG1: %x\n", ret);
    if (ret & 1)
        return true;
    else
        return false;
}


void MMA8451Q::transient_detect()
{
    ctrl_reg1.octet = read_single(MMA8451_CTRL_REG1);
    /* AN4071 Sensors Freescale Semiconductor, Inc.
     * 7.1 Example Steps for Configuring Transient Detection
     * Change in X or Y > 0.5g for 50 ms at 100 Hz ODR, Normal mode */

    /* Step 1: Put the device in Standby Mode: Register 0x2A CTRL_REG1 */
    ctrl_reg1.bits.ACTIVE = 0;
    write(MMA8451_CTRL_REG1, ctrl_reg1.octet);
    ctrl_reg1.bits.DR = 3; //Set device in 100 Hz ODR, Standby
    write(MMA8451_CTRL_REG1, ctrl_reg1.octet);

    /* Step 2: Enable X and Y Axes and enable the latch: Register 0x1D Configuration Register */
    transient_cfg.octet = 0;
    transient_cfg.bits.ELE = 1; // enable latch
    transient_cfg.bits.YTEFE = 1;   // enable Y
    transient_cfg.bits.XTEFE = 1;   // enable X
    transient_cfg.bits.ZTEFE = 1;   // enable Z
    write(MMA8451_TRANSIENT_CFG, transient_cfg.octet);

    /* Step 3: Set the Threshold: Register 0x1F 
     * Note: Step count is 0.063g per count, 0.5g / 0.063g = 7.93.
     * Therefore set the threshold to 8 counts */
    write(MMA8451_TRANSIENT_THS, 8);

    /* Step 4: Set the Debounce Counter for 50 ms: Register 0x20
     * Note: 100 Hz ODR, therefore 10 ms step sizes */
    write(MMA8451_TRANSIENT_COUNT, 5);

    /* Step 5: Enable Transient Detection Interrupt in the System (CTRL_REG4) */
    ctrl_reg4.octet = 0;
    ctrl_reg4.bits.INT_EN_TRANS = 1;
    write(MMA8451_CTRL_REG4, ctrl_reg4.octet);

    /* Step 6: Route the Transient Interrupt to INT 1 hardware pin (CTRL_REG5) */
    ctrl_reg5.octet = 0;
    ctrl_reg5.bits.INT_CFG_TRANS = 1;
    write(MMA8451_CTRL_REG5, ctrl_reg5.octet);

    /* Step 7: Put the device in Active Mode: Register 0x2A CTRL_REG1 */
    ctrl_reg1.octet = read_single(MMA8451_CTRL_REG1);
    ctrl_reg1.bits.ACTIVE = 1;
    write(MMA8451_CTRL_REG1, ctrl_reg1.octet);

    /* Step 8: Write Interrupt Service Routine Reading the
     * System Interrupt Status and the Transient Status */
}

void MMA8451Q::service()
{
    mma_int_source_t int_src;
    if (m_int_pin)
        return; // no interrupt
        
    int_src.octet = read_single(MMA8451_INT_SOURCE);
    
    if (int_src.bits.SRC_DRDY) {
        read(MMA8451_OUT_X_MSB, out.octets, 6);
    }
    if (int_src.bits.SRC_FF_MT) {
        read_single(MMA8451_FF_MT_SRC);
    }
    if (int_src.bits.SRC_PULSE) {
        read_single(MMA8451_PULSE_SRC);
    }
    if (int_src.bits.SRC_LNDPRT) {
        read_single(MMA8451_PL_STATUS);
    }                        
    if (int_src.bits.SRC_TRANS) {
        transient_src_t t_src;
        t_src.octet = read_single(MMA8451_TRANSIENT_SRC);
        printf("transient src:%x ", t_src.octet);
        if (t_src.bits.XTRANSE)
            printf("X_Pol:%d ", t_src.bits.X_Trans_Pol);
        if (t_src.bits.YTRANSE)
            printf("Y_Pol:%d ", t_src.bits.Y_Trans_Pol);
        if (t_src.bits.ZTRANSE)
            printf("Z_Pol:%d ", t_src.bits.Z_Trans_Pol);
        printf("\n");        
    }
    
    if (int_src.bits.SRC_ASLP) {
        read_single(MMA8451_SYSMOD);
    }
    
}