Vins version of FXOS8700CQ library for use in mbed online compiler
Fork of FXOS8700CQ by
Revision 5:7bdb0d5e5287, committed 2016-01-10
- Comitter:
- maclobdell
- Date:
- Sun Jan 10 17:45:05 2016 +0000
- Parent:
- 4:e2fe752b881e
- Commit message:
- added interrupt and motion detection support
Changed in this revision
FXOS8700CQ.cpp | Show annotated file Show diff for this revision Revisions of this file |
FXOS8700CQ.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r e2fe752b881e -r 7bdb0d5e5287 FXOS8700CQ.cpp --- a/FXOS8700CQ.cpp Tue Jun 03 19:02:19 2014 +0000 +++ b/FXOS8700CQ.cpp Sun Jan 10 17:45:05 2016 +0000 @@ -30,20 +30,26 @@ FXOS8700CQ_CTRL_REG2_SMODS2(3) | // 0b11 gives low power sleep oversampling mode FXOS8700CQ_CTRL_REG2_MODS2(1); // 0b01 gives low noise, low power oversampling mode +//MPL - don't configure interrupts here. +/* // No configuration changes from default 0x00 in CTRL_REG3 // Interrupts will be active low, their outputs in push-pull mode data[3] = 0x00; + // FXOS8700CQ_CTRL_REG4; - data[4] = + data[4] = FXOS8700CQ_CTRL_REG4_INT_EN_DRDY; // Enable the Data-Ready interrupt // No configuration changes from default 0x00 in CTRL_REG5 // Data-Ready interrupt will appear on INT2 data[5] = 0x00; - // Write to the 5 CTRL_REG registers - write_regs(data, 6); + // Write to the 4 CTRL_REG registers + + write_regs(data, 6); +*/ + write_regs(data, 3); // FXOS8700CQ_XYZ_DATA_CFG data[0] = FXOS8700CQ_XYZ_DATA_CFG; @@ -69,6 +75,7 @@ // Write to the 3 M_CTRL_REG registers write_regs(data, 4); + // Peripheral is configured, but disabled enabled = false; } @@ -88,6 +95,73 @@ enabled = true; } +//MPL +uint8_t FXOS8700CQ::config_int( void) +{ + + /*don't enable interrupts in constructor. Do that here. */ + + /* todo: pass in a structure and have this function enable the interrupts you want, set the pin you want them to come out on, and set whether active high or low */ + + uint8_t data[2]; + + //external interrupt signal is active low & push-pull by default (bit value 0). + data[0] = FXOS8700CQ_CTRL_REG3; + data[1] = 0x08; //enable freefall/motion detection interrupt to wake sensor from sleep mode + write_regs(data, 2); + + data[0] = FXOS8700CQ_CTRL_REG4; + data[1] = 0x04; //enable freefall/motion detection interrupt + write_regs(data, 2); + + //by default it comes out on INT2 + //INT2 connected to PTD1 on FRDM-KL26Z + data[0] = FXOS8700CQ_CTRL_REG5; + data[1] = 0x00; //interrupts come out on INT2 + write_regs(data, 2); + + //todo: what is the correct return value for okay? + return 99; +} + +//MPL +void FXOS8700CQ::clear_int( void) +{ + /* todo: pass in a structure and have this function clear the interrupts you want */ + + //this is only for the freefall / motion detection + + uint8_t data[2]; + + read_regs(FXOS8700CQ_INT_SOURCE, &data[1], 1); // need to clear this because we had selected to latch the interrupt + //printf("INT_SOURCE = %x\n",data[1]); + + read_regs(FXOS8700CQ_A_FFMT_SRC, &data[1], 1); //clear the motion interrupt because we enabled it + //printf("A_FFMT_SRC = %x\n",data[1]); + +} + +//MPL +uint8_t FXOS8700CQ::config_feature( void) +{ + //dont enable specific features (tap detection, motion detection, etc) in constructor - do that here +//todo: should this be in a separate function that allows users to select what capabilities to enable? + +//MPL - enable motion detection + + uint8_t data[2]; + + data[0] = FXOS8700CQ_A_FFMT_CFG; + data[1] = 0x78; //(don't latch event in register.) bit6: 1 motion flag. bits5,4,3: 1 enable detection in all 3 accel axis + write_regs(data, 2); + + data[0] = FXOS8700CQ_A_FFMT_THS; + data[1] = 0x07; //will this work?? //do I need to enable the debounce filter? + write_regs(data, 2); + + return 0; // not implemented return yet + +} void FXOS8700CQ::disable(void) { uint8_t data[2];
diff -r e2fe752b881e -r 7bdb0d5e5287 FXOS8700CQ.h --- a/FXOS8700CQ.h Tue Jun 03 19:02:19 2014 +0000 +++ b/FXOS8700CQ.h Sun Jan 10 17:45:05 2016 +0000 @@ -29,6 +29,27 @@ #define FXOS8700CQ_M_CTRL_REG2 0x5C #define FXOS8700CQ_M_CTRL_REG3 0x5D +/* start MPL additions */ +//MPL interrupts +#define FXOS8700CQ_INT_SOURCE 0x0C +//#define FXOS8700CQ_CTRL_REG2 0x2B //bit 2 turns on auto-sleep +//#define FXOS8700CQ_CTRL_REG3 0x2C //interrupt control register +//#define FXOS8700CQ_CTRL_REG4 0x2D //interrupt enable register +//#define FXOS8700CQ_CTRL_REG5 0x2E //interrupt routing config register, by default all routed to INT2 + +//MPL motion detection +#define FXOS8700CQ_A_FFMT_CFG 0x15 +#define FXOS8700CQ_A_FFMT_SRC 0x16 +#define FXOS8700CQ_A_FFMT_THS 0x17 +#define FXOS8700CQ_A_FFMT_COUNT 0x18 +#define FXOS8700CQ_A_FFMT_THS_X_MSB 0x73 +#define FXOS8700CQ_A_FFMT_THS_X_LSB 0x74 +#define FXOS8700CQ_A_FFMT_THS_Y_MSB 0x75 +#define FXOS8700CQ_A_FFMT_THS_Y_LSB 0x76 +#define FXOS8700CQ_A_FFMT_THS_Z_MSB 0x77 +#define FXOS8700CQ_A_FFMT_THS_Z_LSB 0x78 +/* end MPL additions */ + // FXOS8700CQ configuration macros, per register #define FXOS8700CQ_CTRL_REG1_ASLP_RATE2(x) (x << 6) // x is 2-bit @@ -161,7 +182,26 @@ */ uint8_t get_accel_scale(void); + /** + * configure external interrupts + * + * @return 2, 4, or 8, depending on part configuration; 0 on error + */ + uint8_t config_int(void); + + /** + * configure feature (tap detection, motion detection, etc) + * + * @return 2, 4, or 8, depending on part configuration; 0 on error + */ + uint8_t config_feature(void); + /** + * clear interrupt + * + * @return 2, 4, or 8, depending on part configuration; 0 on error + */ + void clear_int(void); private: I2C dev_i2c; // instance of the mbed I2C class