Maxim Integrated / Mbed OS MAX30101WING_HR_SPO2

Dependencies:   MAX30101 max32630fthr

Fork of MAX30101_HR_SPO2 by John Greene

Committer:
coreyharris
Date:
Tue Aug 22 21:51:07 2017 +0000
Revision:
4:14d1b87cd3c4
Parent:
3:a580414c44ce
Child:
5:2f708191f1bd
Comments and whitespace revisions (following code review).; ; Need to figure out:;  - Interrupt bug

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 4:14d1b87cd3c4 28 I2C i2cBus(I2C1_SDA, I2C1_SCL); // I2C bus, P3_4 = SDA, P3_5 = SCL
coreyharris 0:0bd4103885bf 29
coreyharris 4:14d1b87cd3c4 30 MAX30101 op_sensor(i2cBus); // Create new MAX30101 on i2cBus
coreyharris 4:14d1b87cd3c4 31 int rc = op_sensor_config(op_sensor); // Config sensor, return 0 on success
coreyharris 0:0bd4103885bf 32
coreyharris 0:0bd4103885bf 33 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 34 uint8_t fifoData[MAX30101::MAX_FIFO_BYTES];
coreyharris 0:0bd4103885bf 35 uint16_t idx, readBytes;
coreyharris 2:54182d6a168f 36 int16_t opSample;
coreyharris 2:54182d6a168f 37 uint32_t sample;
coreyharris 0:0bd4103885bf 38 while(1) {
coreyharris 2:54182d6a168f 39
coreyharris 1:471e2b722d24 40 if( rc == 0 ) {
coreyharris 4:14d1b87cd3c4 41
coreyharris 4:14d1b87cd3c4 42 // Check if op_sensor interrupt asserted
coreyharris 1:471e2b722d24 43 if(op_sensorIntFlag) {
coreyharris 4:14d1b87cd3c4 44
coreyharris 0:0bd4103885bf 45 pc.printf("Interrupt seen...\r\n");
coreyharris 1:471e2b722d24 46 op_sensorIntFlag = 0; // Lower interrupt flag
coreyharris 4:14d1b87cd3c4 47 rc = op_sensor.getInterruptStatus(ints); // Read interrupt status
coreyharris 0:0bd4103885bf 48
coreyharris 4:14d1b87cd3c4 49 // Check if FIFO almost full interrupt asserted
coreyharris 1:471e2b722d24 50 if((rc == 0) && (ints.bits.a_full)) {
coreyharris 4:14d1b87cd3c4 51
coreyharris 4:14d1b87cd3c4 52 // Read FIFO
coreyharris 4:14d1b87cd3c4 53 rc = op_sensor.readFIFO(MAX30101::OneLedChannel, fifoData, readBytes);
coreyharris 0:0bd4103885bf 54
coreyharris 1:471e2b722d24 55 if(rc == 0) {
coreyharris 4:14d1b87cd3c4 56
coreyharris 2:54182d6a168f 57 pc.printf("FIFO Read, received %d samples\r\n\r\n", readBytes/3);
coreyharris 0:0bd4103885bf 58
coreyharris 4:14d1b87cd3c4 59 // Convert read bytes into samples
coreyharris 2:54182d6a168f 60 for(idx = 0; idx < readBytes; idx+=3) {
coreyharris 2:54182d6a168f 61 sample = (fifoData[idx]<<16) | (fifoData[idx+1]<<8) | (fifoData[idx+2]);
coreyharris 4:14d1b87cd3c4 62 opSample = sample >> 8; // Sign extends sample to 16-bit opSample
coreyharris 2:54182d6a168f 63 pc.printf("Op. Sample : %i\r\n", opSample); // Print results
coreyharris 0:0bd4103885bf 64 }
coreyharris 4:14d1b87cd3c4 65
coreyharris 2:54182d6a168f 66 pc.printf("\r\n\r\n");
coreyharris 2:54182d6a168f 67
coreyharris 0:0bd4103885bf 68 }
coreyharris 0:0bd4103885bf 69 }
coreyharris 0:0bd4103885bf 70 }
coreyharris 3:a580414c44ce 71
coreyharris 3:a580414c44ce 72 } else {
coreyharris 0:0bd4103885bf 73
coreyharris 4:14d1b87cd3c4 74 pc.printf("Something went wrong, "
coreyharris 4:14d1b87cd3c4 75 "check the I2C bus or power connections... \r\n");
coreyharris 0:0bd4103885bf 76 bLed = LED_OFF;
coreyharris 0:0bd4103885bf 77 gLed = LED_OFF;
coreyharris 0:0bd4103885bf 78
coreyharris 0:0bd4103885bf 79 while(1)
coreyharris 0:0bd4103885bf 80 {
coreyharris 0:0bd4103885bf 81 rLed = !rLed;
coreyharris 0:0bd4103885bf 82 wait(0.5);
coreyharris 3:a580414c44ce 83 }
coreyharris 3:a580414c44ce 84 }
coreyharris 0:0bd4103885bf 85
coreyharris 3:a580414c44ce 86 }
coreyharris 0:0bd4103885bf 87 }
coreyharris 0:0bd4103885bf 88
coreyharris 0:0bd4103885bf 89
coreyharris 0:0bd4103885bf 90 bool op_sensor_config(MAX30101 &op_sensor) {
coreyharris 0:0bd4103885bf 91
coreyharris 0:0bd4103885bf 92 //Reset Device
coreyharris 0:0bd4103885bf 93 MAX30101::ModeConfiguration_u modeConfig;
coreyharris 0:0bd4103885bf 94 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 95 modeConfig.bits.reset = 1;
coreyharris 0:0bd4103885bf 96 int32_t rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 97
coreyharris 0:0bd4103885bf 98
coreyharris 0:0bd4103885bf 99 //enable MAX30101 interrupts
coreyharris 0:0bd4103885bf 100 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 101 if(rc == 0)
coreyharris 0:0bd4103885bf 102 {
coreyharris 0:0bd4103885bf 103 ints.all = 0;
coreyharris 1:471e2b722d24 104 ints.bits.die_temp = 1; // Enable FIFO almost full interrupt
coreyharris 1:471e2b722d24 105 ints.bits.a_full = 1; // Enable internal die temp. interrupt
coreyharris 0:0bd4103885bf 106 rc = op_sensor.enableInterrupts(ints);
coreyharris 0:0bd4103885bf 107 }
coreyharris 0:0bd4103885bf 108
coreyharris 0:0bd4103885bf 109 //configure FIFO
coreyharris 0:0bd4103885bf 110 MAX30101::FIFO_Configuration_u fifoConfig;
coreyharris 0:0bd4103885bf 111 if(rc == 0)
coreyharris 0:0bd4103885bf 112 {
coreyharris 0:0bd4103885bf 113 fifoConfig.all = 0;
coreyharris 1:471e2b722d24 114 fifoConfig.bits.fifo_a_full = 15; // Max level of 15 samples
coreyharris 4:14d1b87cd3c4 115 fifoConfig.bits.sample_average = MAX30101::AveragedSamples_8; // Average 8 samples
coreyharris 0:0bd4103885bf 116 rc = op_sensor.setFIFOConfiguration(fifoConfig);
coreyharris 0:0bd4103885bf 117 }
coreyharris 0:0bd4103885bf 118
coreyharris 0:0bd4103885bf 119 MAX30101::SpO2Configuration_u spo2Config;
coreyharris 0:0bd4103885bf 120 if(rc == 0)
coreyharris 0:0bd4103885bf 121 {
coreyharris 1:471e2b722d24 122 spo2Config.all = 0;
coreyharris 1:471e2b722d24 123 spo2Config.bits.spo2_sr = MAX30101::SR_400_Hz; // SpO2 SR = 400Hz
coreyharris 2:54182d6a168f 124 spo2Config.bits.led_pw = 1;
coreyharris 0:0bd4103885bf 125 rc = op_sensor.setSpO2Configuration(spo2Config);
coreyharris 0:0bd4103885bf 126 }
coreyharris 0:0bd4103885bf 127
coreyharris 0:0bd4103885bf 128 //Set LED1 drive current
coreyharris 0:0bd4103885bf 129 if(rc == 0)
coreyharris 0:0bd4103885bf 130 {
coreyharris 0:0bd4103885bf 131 //Heart Rate only
coreyharris 2:54182d6a168f 132 rc = op_sensor.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x1F);
coreyharris 0:0bd4103885bf 133 }
coreyharris 0:0bd4103885bf 134
coreyharris 0:0bd4103885bf 135 //Set operating mode
coreyharris 0:0bd4103885bf 136 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 137 if(rc == 0)
coreyharris 0:0bd4103885bf 138 {
coreyharris 1:471e2b722d24 139 modeConfig.bits.mode = MAX30101::HeartRateMode; // Heart-rate only
coreyharris 0:0bd4103885bf 140 rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 141 }
coreyharris 0:0bd4103885bf 142
coreyharris 0:0bd4103885bf 143
coreyharris 0:0bd4103885bf 144 return rc;
coreyharris 0:0bd4103885bf 145 }