Maxim Integrated / Mbed OS MAX30101WING_HR_SPO2

Dependencies:   MAX30101 max32630fthr

Fork of MAX30101_HR_SPO2 by John Greene

Committer:
coreyharris
Date:
Mon Aug 21 15:43:51 2017 +0000
Revision:
1:471e2b722d24
Parent:
0:0bd4103885bf
Child:
2:54182d6a168f
Added comments;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
coreyharris 0:0bd4103885bf 1 #include "mbed.h"
coreyharris 0:0bd4103885bf 2 #include "max32630fthr.h"
coreyharris 0:0bd4103885bf 3 #include "MAX30101.h"
coreyharris 0:0bd4103885bf 4
coreyharris 0:0bd4103885bf 5 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
coreyharris 0:0bd4103885bf 6
coreyharris 0:0bd4103885bf 7 bool op_sensor_config(MAX30101 &op_sensor);
coreyharris 0:0bd4103885bf 8
coreyharris 0:0bd4103885bf 9 /* Op Sensor FIFO nearly full callback */
coreyharris 1:471e2b722d24 10 volatile bool op_sensorIntFlag = 0;
coreyharris 0:0bd4103885bf 11 void op_sensor_callback()
coreyharris 0:0bd4103885bf 12 {
coreyharris 1:471e2b722d24 13 op_sensorIntFlag = 1;
coreyharris 0:0bd4103885bf 14 }
coreyharris 0:0bd4103885bf 15
coreyharris 0:0bd4103885bf 16 int main()
coreyharris 0:0bd4103885bf 17 {
coreyharris 1:471e2b722d24 18 Serial pc(USBTX, USBRX); // Use USB debug probe for serial link
coreyharris 1:471e2b722d24 19 pc.baud(115200); // Baud rate = 115200
coreyharris 0:0bd4103885bf 20
coreyharris 1:471e2b722d24 21 DigitalOut rLed(LED1, LED_OFF); // Debug LEDs
coreyharris 0:0bd4103885bf 22 DigitalOut gLed(LED2, LED_OFF);
coreyharris 0:0bd4103885bf 23 DigitalOut bLed(LED3, LED_OFF);
coreyharris 0:0bd4103885bf 24
coreyharris 1:471e2b722d24 25 InterruptIn op_sensor_int(P3_2); // Config P3_2 as int. in for
coreyharris 1:471e2b722d24 26 op_sensor_int.fall(&op_sensor_callback); // FIFO ready interrupt
coreyharris 0:0bd4103885bf 27
coreyharris 1:471e2b722d24 28 I2C i2cBus(I2C1_SDA, I2C1_SCL); // I2C bus, P3_4 = SDA, P3_5 = SCL
coreyharris 0:0bd4103885bf 29
coreyharris 0:0bd4103885bf 30 MAX30101 * op_sensor;
coreyharris 0:0bd4103885bf 31 op_sensor = new MAX30101(i2cBus);
coreyharris 1:471e2b722d24 32 int rc = op_sensor_config(*op_sensor);
coreyharris 0:0bd4103885bf 33
coreyharris 0:0bd4103885bf 34 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 35 uint8_t fifoData[MAX30101::MAX_FIFO_BYTES];
coreyharris 0:0bd4103885bf 36 uint16_t idx, readBytes;
coreyharris 0:0bd4103885bf 37 while(1) {
coreyharris 0:0bd4103885bf 38
coreyharris 1:471e2b722d24 39 if( rc == 0 ) {
coreyharris 0:0bd4103885bf 40
coreyharris 1:471e2b722d24 41 if(op_sensorIntFlag) {
coreyharris 0:0bd4103885bf 42 pc.printf("Interrupt seen...\r\n");
coreyharris 1:471e2b722d24 43 op_sensorIntFlag = 0; // Lower interrupt flag
coreyharris 1:471e2b722d24 44 rc = op_sensor->getInterruptStatus(ints); // Read interrupt status
coreyharris 0:0bd4103885bf 45
coreyharris 1:471e2b722d24 46 if((rc == 0) && (ints.bits.a_full)) {
coreyharris 1:471e2b722d24 47 rc = op_sensor->readFIFO(MAX30101::OneLedChannel, fifoData, readBytes); // Read FIFO
coreyharris 0:0bd4103885bf 48
coreyharris 1:471e2b722d24 49 if(rc == 0) {
coreyharris 0:0bd4103885bf 50 pc.printf("FIFO Read, received %d bytes\r\n", readBytes);
coreyharris 0:0bd4103885bf 51
coreyharris 1:471e2b722d24 52 for(idx = 0; idx < readBytes; idx++) {
coreyharris 1:471e2b722d24 53 pc.printf("fifoData[0x%04x] = 0x%02x\r\n", idx, fifoData[idx]); // Print results
coreyharris 0:0bd4103885bf 54 }
coreyharris 0:0bd4103885bf 55 pc.printf("\r\n");
coreyharris 0:0bd4103885bf 56 }
coreyharris 0:0bd4103885bf 57 }
coreyharris 0:0bd4103885bf 58 }
coreyharris 1:471e2b722d24 59
coreyharris 0:0bd4103885bf 60 } else {
coreyharris 0:0bd4103885bf 61
coreyharris 0:0bd4103885bf 62 pc.printf("Something went wrong, check the I2C bus or power connections... \r\n");
coreyharris 0:0bd4103885bf 63 bLed = LED_OFF;
coreyharris 0:0bd4103885bf 64 gLed = LED_OFF;
coreyharris 0:0bd4103885bf 65
coreyharris 0:0bd4103885bf 66 while(1)
coreyharris 0:0bd4103885bf 67 {
coreyharris 0:0bd4103885bf 68 rLed = !rLed;
coreyharris 0:0bd4103885bf 69 wait(0.5);
coreyharris 0:0bd4103885bf 70 }
coreyharris 0:0bd4103885bf 71
coreyharris 0:0bd4103885bf 72 }
coreyharris 0:0bd4103885bf 73
coreyharris 0:0bd4103885bf 74 }
coreyharris 0:0bd4103885bf 75
coreyharris 0:0bd4103885bf 76 }
coreyharris 0:0bd4103885bf 77
coreyharris 0:0bd4103885bf 78
coreyharris 0:0bd4103885bf 79 bool op_sensor_config(MAX30101 &op_sensor) {
coreyharris 0:0bd4103885bf 80
coreyharris 0:0bd4103885bf 81 //Reset Device
coreyharris 0:0bd4103885bf 82 MAX30101::ModeConfiguration_u modeConfig;
coreyharris 0:0bd4103885bf 83 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 84 modeConfig.bits.reset = 1;
coreyharris 0:0bd4103885bf 85 int32_t rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 86
coreyharris 0:0bd4103885bf 87
coreyharris 0:0bd4103885bf 88 //enable MAX30101 interrupts
coreyharris 0:0bd4103885bf 89 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 90 if(rc == 0)
coreyharris 0:0bd4103885bf 91 {
coreyharris 0:0bd4103885bf 92 ints.all = 0;
coreyharris 1:471e2b722d24 93 ints.bits.die_temp = 1; // Enable FIFO almost full interrupt
coreyharris 1:471e2b722d24 94 ints.bits.a_full = 1; // Enable internal die temp. interrupt
coreyharris 0:0bd4103885bf 95 rc = op_sensor.enableInterrupts(ints);
coreyharris 0:0bd4103885bf 96 }
coreyharris 0:0bd4103885bf 97
coreyharris 0:0bd4103885bf 98 //configure FIFO
coreyharris 0:0bd4103885bf 99 MAX30101::FIFO_Configuration_u fifoConfig;
coreyharris 0:0bd4103885bf 100 if(rc == 0)
coreyharris 0:0bd4103885bf 101 {
coreyharris 0:0bd4103885bf 102 fifoConfig.all = 0;
coreyharris 1:471e2b722d24 103 fifoConfig.bits.fifo_a_full = 15; // Max level of 15 samples
coreyharris 1:471e2b722d24 104 fifoConfig.bits.sample_average = MAX30101::AveragedSamples_4; // Average 4 samples
coreyharris 0:0bd4103885bf 105 rc = op_sensor.setFIFOConfiguration(fifoConfig);
coreyharris 0:0bd4103885bf 106 }
coreyharris 0:0bd4103885bf 107
coreyharris 0:0bd4103885bf 108 MAX30101::SpO2Configuration_u spo2Config;
coreyharris 0:0bd4103885bf 109 if(rc == 0)
coreyharris 0:0bd4103885bf 110 {
coreyharris 1:471e2b722d24 111 spo2Config.all = 0;
coreyharris 1:471e2b722d24 112 spo2Config.bits.spo2_sr = MAX30101::SR_400_Hz; // SpO2 SR = 400Hz
coreyharris 0:0bd4103885bf 113 rc = op_sensor.setSpO2Configuration(spo2Config);
coreyharris 0:0bd4103885bf 114 }
coreyharris 0:0bd4103885bf 115
coreyharris 0:0bd4103885bf 116 //Set LED1 drive current
coreyharris 0:0bd4103885bf 117 if(rc == 0)
coreyharris 0:0bd4103885bf 118 {
coreyharris 0:0bd4103885bf 119 //Heart Rate only
coreyharris 1:471e2b722d24 120 rc = op_sensor.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x1F); // LED1 = 6.4mA
coreyharris 0:0bd4103885bf 121 }
coreyharris 0:0bd4103885bf 122
coreyharris 0:0bd4103885bf 123 //Set operating mode
coreyharris 0:0bd4103885bf 124 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 125 if(rc == 0)
coreyharris 0:0bd4103885bf 126 {
coreyharris 1:471e2b722d24 127 modeConfig.bits.mode = MAX30101::HeartRateMode; // Heart-rate only
coreyharris 0:0bd4103885bf 128 rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 129 }
coreyharris 0:0bd4103885bf 130
coreyharris 0:0bd4103885bf 131
coreyharris 0:0bd4103885bf 132 return rc;
coreyharris 0:0bd4103885bf 133 }