I2C sensor test program, derived from testI2C program. Simple test for FXOS8700CQ, HIH6130, MAG3110, MMA8451Q, MMA8452Q, MPL3115A2, MAX44000, MAX44005, MAX44008, MAX30101 included beside simple I2C read/write from testI2C.
Dependencies: FXOS8700CQ HIH6130 IS31SE5000 MAG3110 MAX44000 MAX44005 MAX44008 MMA8451Q MMA8452Q MPL3115A2 VEML6040 VEML6075 mbed vt100 LM75B FXAS21002 MAX30101 VCNL4020 VCNL4100
Diff: MSU.cpp
- Revision:
- 14:1e6e4fdf90d0
- Parent:
- 12:b3dff3bbb1eb
- Child:
- 15:17416bd1b51c
--- a/MSU.cpp Fri Mar 10 04:19:05 2017 +0000 +++ b/MSU.cpp Mon Mar 27 08:04:29 2017 +0000 @@ -16,7 +16,7 @@ #include "LM75B.h" #include "FXAS21002.h" #include "S11059.h" - +#include "MAX30101.h" extern int test_loop ; extern int interval ; /* wait interval ms */ @@ -43,6 +43,96 @@ {0x00, 0, 0} } ; +MAX30101 *max30101 = 0 ; +#ifndef FIFO_DEPTH +#define FIFO_DEPTH 32 +#endif + +void doDemo(void) +{ + int i ; + float humidity = 0 ; + float temperature = 0 ; + uint8_t mode ; + uint8_t txdata, rxdata ; + uint16_t als ; + uint8_t prox, overflow ; + uint32_t data, alt, bar, tmp ; + uint16_t sample_time ; + + HIH6130 *hih6130 = new HIH6130(PIN_SDA, PIN_SCL, MSU_HIH6130_ADDRESS) ; + MMA8452Q *mma8452q = new MMA8452Q(PIN_SDA, PIN_SCL, MSU_MMA8452Q_ADDRESS) ; + MAX44000 *max44000 = new MAX44000(PIN_SDA, PIN_SCL, MSU_MAX44000_ADDRESS) ; + rxdata = 0xF1 ; + max44000->setRxConfig(rxdata) ; + txdata = 0x0F ; /* light LED! */ + max44000->setTxConfig(txdata) ; + mode = 0x30 ; // ALS & PROX mode + max44000->setMainConfig(mode) ; + + MPL3115A2 *mpl3115a2 = new MPL3115A2(PIN_SDA, PIN_SCL, MSU_MPL3115A2_ADDRESS) ; + tty->cls() ; + tty->locate(10, 1) ; + printf("=== Multi Sensor Demo (%s) ===\n", BOARD_NAME) ; + tty->putStr(2, 3, "MMA8451Q") ; + tty->putStr(2, 10, "MAX44000") ; + tty->putStr(40, 3, "HIH6130") ; + tty->putStr(40, 10, "MPL3115A2") ; + for (i = 0 ; i < test_loop ; i++ ) { + + /* MMA8451Q */ + tty->locate(2, 5) ; + printf("X:%.2f Y:%.2f Z:%.2f", + mma8452q->getAccX(), + mma8452q->getAccY(), + mma8452q->getAccZ()) ; + + if ((i % 10) == 0) { + /* HIH6130 */ + tty->locate(40, 5) ; + hih6130->getValue(&humidity, &temperature) ; + printf("Temp %.1f C", temperature) ; + tty->locate(40, 7) ; + printf("Humidity %.1f %%\n", humidity) ; + /* MAX44000 */ + tty->locate(2, 12) ; + max44000->getADC(&overflow, &als, &prox) ; + printf("ALS[ %04X ]", als) ; + if (overflow) { + printf(" overflow ") ; + } else { + printf(" ") ; + } + tty->locate(2, 14) ; + printf("PRX[ %02X ]\n", prox) ; + /* MPL3115A2 */ + + mpl3115a2->modeAlt() ; + sample_time = mpl3115a2->OneShot() ; + wait(((double)sample_time)/1000.0) ; + alt = mpl3115a2->getAltitude() ; + mpl3115a2->modeBar() ; + sample_time = mpl3115a2->OneShot() ; + wait(((double)sample_time)/1000.0) ; + bar = mpl3115a2->getPressure() ; + tmp = mpl3115a2->getTemperature() ; + tty->locate(40, 11) ; + printf("Altitude: %d", (alt >> 16)&0xFFFF) ; + tty->locate(40, 13) ; + printf("Pressure: %d", (bar >> 6)) ; + tty->locate(40, 15) ; + printf("Temperature: %d", (tmp >> 8) ) ; + } + } + txdata = 0x00 ; /* dim LED! */ + max44000->setTxConfig(txdata) ; + tty->locate(1, 20) ; + delete mpl3115a2 ; + delete max44000 ; + delete mma8452q ; + delete hih6130 ; +} + void testLM75B(void) { int8_t itemp = 0 ; @@ -290,12 +380,6 @@ delete max44008 ; } -void testMAX30101(void) -{ - printf("test MAX30101\n") ; - printf("Sorry not implemented yet\n") ; -} - void testMPL3115A2(void) { uint32_t data, alt, bar, tmp ; @@ -372,4 +456,181 @@ wait_ms(interval) ; } delete s11059 ; +} + +void init_max30101(void) +{ + uint32_t slot_config = 0 ; + uint8_t mode ; + + max30101 = new MAX30101(PIN_SDA, PIN_SCL, MAX30101_I2C_ADDRESS) ; + max30101->reset() ; + + /* config for Mode Configuration (0x09) */ + + mode = 0x07 ; /* Green, Red, and/or IR */ + // mode = 0x03 ; /* SpO2 mode */ + max30101->setMODE_CONFIG(mode) ; + + /* config for FIFO Configuration (0x08) */ + mode = + // (0x02 << 5) /* SMP_AVE = 2 -> avarage of 4 data */ + (0x05 << 5) /* SMP_AVE = 5 -> avarage of 32 data */ +// | (0x01 << 4) /* FIFO_ROLLOVER_EN = 1 */ + | (0x00 << 4) /* FIFO_ROLLOVER_EN = 0 */ + | (0x0C) /* FIFO ALMOST FULL at 12 (0x0C) */ + ; + max30101->setFIFO_CONFIG(mode) ; + + max30101->setLED1_PA(0x80) ; + max30101->setLED2_PA(0x80) ; +// max30101->setLED3_PA(0x80) ; +// max30101->setPILOT_PA(0x80) ; +#if 0 + slot_config = + (0x02 << 24) /* SLOT2 LED2(IR) */ + | (0x01 << 16) /* SLOT1 LED1(RED) */ + | (0x00 << 8) /* SLOT4 none */ + | (0x03) /* SLOT3 LED3(GREEN) */ + ; + max30101->setSLOT(slot_config) ; +#endif + mode = 0x01 ; + max30101->setPROX_INT_THR(mode) ; +} + +void readSPO2_FIFO(void) +{ + uint32_t data[2] ; + uint8_t fifo_wr_ptr, fifo_rd_ptr ; + int num_available_samples = 0 ; + int num_samples_to_read = 6 ; + int i ; + + fifo_wr_ptr = max30101->getFIFO_WR_PTR() ; + fifo_rd_ptr = max30101->getFIFO_RD_PTR() ; + num_available_samples = (fifo_wr_ptr + FIFO_DEPTH - fifo_rd_ptr) % FIFO_DEPTH ; + + if (num_available_samples < num_samples_to_read) { + num_samples_to_read = num_available_samples / 2 ; + } else { + num_samples_to_read = num_available_samples ; + } + printf("=== %d data %d to read===\n", + num_available_samples, num_samples_to_read) ; + for (i = 0 ; i < num_samples_to_read ; i++ ) { + data[0] = max30101->readFIFO() ; /* LED1 */ + data[1] = max30101->readFIFO() ; /* IR */ +// printf("LED1: 0x%05X, IR: 0x%05X\n", data[0], data[1]) ; + } +// max30101->setFIFO_WR_PTR(0) ; +// max30101->setFIFO_RD_PTR(0) ; +} + +void SPO2_isr(void) +{ + uint16_t flag, config ; + float temp ; + int temp_int, temp_frac ; + + flag = max30101->getIntStatus() ; + config = max30101->getIntEnable() ; +// printf("Int: 0x%04X Enable: 0x%04X\n", flag, config) ; + if (flag & INT_ST_A_FULL) { /* A_FULL_EN */ +// printf("FIFO almost full!\n") ; + readSPO2_FIFO() ; + } + if (flag & INT_ST_PPG_RGY) { /* New FIFO Data Ready */ + printf("New FIFO Data Ready\n") ; + } + if (flag & INT_ST_ALC_OVF) { /* Ambient Light Cancellaration Overflow */ + printf("Ambient Light Cancellaration Overflow\n") ; + } + if (flag & INT_ST_PROX_INT) {/* Proximity Threshold Triggered */ + printf("Proximity Threshold Triggered\n") ; + } + if (flag & INT_ST_PWR_RDY) {/* Power Ready Flag */ + printf("Power Ready!\n") ; + } + if (flag & INT_ST_DIE_TEMP_RDY) {/* Internal Temperature Ready Flag */ + printf("DIE Temperature Ready!\n") ; + temp_int = max30101->getTEMP_INT() ; + temp_frac = max30101->getTEMP_FRAC() ; + temp = ((float)temp_int)+(((float)temp_frac)/16.0) ; + printf("Temp: %.2f\n", temp) ; + } +} + +void initSPO2(void) +{ + uint8_t config_byte = 0 ; + uint16_t slot_config = 0 ; + + /* config for SPO2 (0x0A) */ + config_byte = + (0x01 << 5) /* SPO2_ADC_RGE */ + // | (0x05 << 3) /* SPO2_SR 101 = 1000 samples/sec */ + | (0x00 << 3) /* SPO2_SR 000 = 50 samples/sec */ + | (0x00) /* LED_PW 00 = 69us */ + ; + max30101->setSPO2_CONFIG(config_byte) ; + + /* slot configuration */ + slot_config = + (0x02 << 12) /* SLOT2 LED2(IR) */ + | (0x01 << 8) /* SLOT1 LED1(RED) */ + | (0x00 << 4) /* SLOT4 none */ + | (0x00) /* SLOT3 none */ + ; + max30101->setSLOT(slot_config) ; + + /* Inititalize FIFO */ + /* config for FIFO Configuration (0x08) */ + config_byte = + (0x02 << 5) /* SMP_AVE = 2 -> avarage of 4 data */ + | (0x01 << 4) /* FIFO_ROLLOVER_EN = 1 */ +// | (0x0C) /* FIFO ALMOST FULL at 12 (0x0C) */ + | (0x09) /* FIFO ALMOST FULL at 9 */ + ; + max30101->setFIFO_CONFIG(config_byte) ; + + +} + +void testMAX30101(void) +{ + uint16_t int_config = 0 ; + uint8_t flag = 0 ; + float temp = 0.0 ; + uint8_t id, rev ; + InterruptIn *int0 ; + MAX30101 *max30101 ; + + int0 = new InterruptIn(PIN_INT1) ; + + max30101 = new MAX30101(PIN_SDA, PIN_SCL, MSU_MAX30101_ADDRESS) ; + + init_max30101() ; + id = max30101->getID() ; + rev = max30101->getRev() ; + printf("MAX30101 ID: 0x%02X, Rev: 0x%02X\n", id, rev) ; + + initSPO2() ; + int0->fall(&SPO2_isr) ; + max30101->setMODE_CONFIG(MODE_SPO2) ; + max30101->setIntEnable( INT_EN_A_FULL | INT_EN_DIE_TEMP_RDY ) ; + int_config = max30101->getIntEnable() ; + printf("Int enable: 0x%04X\n", int_config) ; + flag = max30101->getIntStatus() ; + + for (int i = 0 ; i < test_loop ; i++ ) { + max30101->setTEMP_EN() ; /* trigger temperature read */ + temp = max30101->getTEMP() ; + flag = max30101->getIntStatus() ; + printf("Temp = %.2f interrupt flags = 0x%02X\n", temp, flag) ; + wait(1) ; + } + + delete max30101 ; + delete int0 ; } \ No newline at end of file