mma8451q driver with a few minor modifications
Fork of lib_mma8451q by
Diff: mma8451q.cpp
- Revision:
- 1:778b685c3ad0
- Parent:
- 0:cb0046a629c1
- Child:
- 2:4bc96749141e
diff -r cb0046a629c1 -r 778b685c3ad0 mma8451q.cpp --- a/mma8451q.cpp Wed Mar 18 00:58:44 2015 +0000 +++ b/mma8451q.cpp Fri May 08 01:31:18 2015 +0000 @@ -11,12 +11,11 @@ #define MMA8451_I2C_ADDRESS 0x38 //0x1C -MMA8451Q::MMA8451Q(I2C& r) : m_i2c(r) +MMA8451Q::MMA8451Q(I2C& r, DigitalIn& int_pin) : m_i2c(r), m_int_pin(int_pin) { -/* char cmd[2]; - - cmd[0] = MMA8451_ID; - m_i2c.read(MMA8451_I2C_ADDRESS, cmd, 1);*/ + /* INT pins on this chip default to push-pull output */ + write(MMA8451_CTRL_REG3, 0x01); // set PP_OD + /* INT1 and INT2 are tied together */ } @@ -30,9 +29,9 @@ cmd[0] = addr; if (m_i2c.write(MMA8451_I2C_ADDRESS, cmd, 1, true)) - printf("MMA8451Q write-fail\n"); + printf("MMA write-fail %02x\n", addr); if (m_i2c.read(MMA8451_I2C_ADDRESS, (char *)dst_buf, length)) - printf("MMA8451Q read-fail\n"); + printf("MMA read-fail\n"); } uint8_t MMA8451Q::read_single(uint8_t addr) @@ -41,21 +40,26 @@ cmd[0] = addr; if (m_i2c.write(MMA8451_I2C_ADDRESS, cmd, 1, true)) - printf("MMA8451Q write-fail\n"); + printf("MMA write-fail %02x\n", addr); if (m_i2c.read(MMA8451_I2C_ADDRESS, cmd, 1)) - printf("MMA8451Q read-fail\n"); + printf("MMA read-fail\n"); return cmd[0]; } void MMA8451Q::print_regs() { - printf("ID: %x\n", read_single(MMA8451_ID)); - printf("sysmod:%x\n", read_single(MMA8451_SYSMOD)); + 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:%x\n", ctrl_reg1.octet); - printf("ctrl_reg2:%x\n", read_single(MMA8451_CTRL_REG2)); - printf("status: %x\n", read_single(MMA8451_STATUS)); + 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) @@ -66,7 +70,7 @@ cmd[1] = data; if (m_i2c.write(MMA8451_I2C_ADDRESS, (char *)cmd, 2)) - printf("MMA8451Q write-fail\n"); + printf("MMA write-fail %02x\n", addr); } void MMA8451Q::set_active(char arg) @@ -77,14 +81,17 @@ cmd[1] = arg; if (m_i2c.write(MMA8451_I2C_ADDRESS, cmd, 2)) - printf("write-fail\n"); + printf("MMA write-fail %02x\n", cmd[0]); } -uint8_t MMA8451Q::get_active(void) +bool MMA8451Q::get_active(void) { uint8_t ret = read_single(MMA8451_CTRL_REG1); - printf("CTRL_REG1: %x\n", ret); - return ret; + //printf("CTRL_REG1: %x\n", ret); + if (ret & 1) + return true; + else + return false; } @@ -137,3 +144,41 @@ * 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); + } + +}