John Greene / Mbed OS MAX30101_HR_SPO2

Dependencies:   MAX30101 max32630fthr

Fork of MAX30101_Demo_Plot_Algorithm by John Greene

Committer:
coreyharris
Date:
Tue Aug 29 20:32:39 2017 +0000
Revision:
6:238d2eec53de
Parent:
5:2f708191f1bd
Child:
7:6075af57668e
Updated comments

Who changed what in which revision?

UserRevisionLine numberNew contents of line
coreyharris 5:2f708191f1bd 1 /*******************************************************************************
coreyharris 5:2f708191f1bd 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
coreyharris 5:2f708191f1bd 3 *
coreyharris 5:2f708191f1bd 4 * Permission is hereby granted, free of charge, to any person obtaining a
coreyharris 5:2f708191f1bd 5 * copy of this software and associated documentation files (the "Software"),
coreyharris 5:2f708191f1bd 6 * to deal in the Software without restriction, including without limitation
coreyharris 5:2f708191f1bd 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
coreyharris 5:2f708191f1bd 8 * and/or sell copies of the Software, and to permit persons to whom the
coreyharris 5:2f708191f1bd 9 * Software is furnished to do so, subject to the following conditions:
coreyharris 5:2f708191f1bd 10 *
coreyharris 5:2f708191f1bd 11 * The above copyright notice and this permission notice shall be included
coreyharris 5:2f708191f1bd 12 * in all copies or substantial portions of the Software.
coreyharris 5:2f708191f1bd 13 *
coreyharris 5:2f708191f1bd 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
coreyharris 5:2f708191f1bd 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
coreyharris 5:2f708191f1bd 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
coreyharris 5:2f708191f1bd 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
coreyharris 5:2f708191f1bd 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
coreyharris 5:2f708191f1bd 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
coreyharris 5:2f708191f1bd 20 * OTHER DEALINGS IN THE SOFTWARE.
coreyharris 5:2f708191f1bd 21 *
coreyharris 5:2f708191f1bd 22 * Except as contained in this notice, the name of Maxim Integrated
coreyharris 5:2f708191f1bd 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
coreyharris 5:2f708191f1bd 24 * Products, Inc. Branding Policy.
coreyharris 5:2f708191f1bd 25 *
coreyharris 5:2f708191f1bd 26 * The mere transfer of this software does not imply any licenses
coreyharris 5:2f708191f1bd 27 * of trade secrets, proprietary technology, copyrights, patents,
coreyharris 5:2f708191f1bd 28 * trademarks, maskwork rights, or any other form of intellectual
coreyharris 5:2f708191f1bd 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
coreyharris 5:2f708191f1bd 30 * ownership rights.
coreyharris 5:2f708191f1bd 31 *******************************************************************************
coreyharris 5:2f708191f1bd 32 */
coreyharris 5:2f708191f1bd 33
coreyharris 5:2f708191f1bd 34
coreyharris 0:0bd4103885bf 35 #include "mbed.h"
coreyharris 0:0bd4103885bf 36 #include "max32630fthr.h"
coreyharris 0:0bd4103885bf 37 #include "MAX30101.h"
coreyharris 0:0bd4103885bf 38
coreyharris 0:0bd4103885bf 39 MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
coreyharris 0:0bd4103885bf 40
coreyharris 0:0bd4103885bf 41 bool op_sensor_config(MAX30101 &op_sensor);
coreyharris 0:0bd4103885bf 42
coreyharris 0:0bd4103885bf 43 /* Op Sensor FIFO nearly full callback */
coreyharris 1:471e2b722d24 44 volatile bool op_sensorIntFlag = 0;
coreyharris 0:0bd4103885bf 45 void op_sensor_callback()
coreyharris 0:0bd4103885bf 46 {
coreyharris 1:471e2b722d24 47 op_sensorIntFlag = 1;
coreyharris 0:0bd4103885bf 48 }
coreyharris 0:0bd4103885bf 49
coreyharris 0:0bd4103885bf 50 int main()
coreyharris 0:0bd4103885bf 51 {
coreyharris 1:471e2b722d24 52 Serial pc(USBTX, USBRX); // Use USB debug probe for serial link
coreyharris 1:471e2b722d24 53 pc.baud(115200); // Baud rate = 115200
coreyharris 0:0bd4103885bf 54
coreyharris 1:471e2b722d24 55 DigitalOut rLed(LED1, LED_OFF); // Debug LEDs
coreyharris 0:0bd4103885bf 56 DigitalOut gLed(LED2, LED_OFF);
coreyharris 0:0bd4103885bf 57 DigitalOut bLed(LED3, LED_OFF);
coreyharris 0:0bd4103885bf 58
coreyharris 1:471e2b722d24 59 InterruptIn op_sensor_int(P3_2); // Config P3_2 as int. in for
coreyharris 1:471e2b722d24 60 op_sensor_int.fall(&op_sensor_callback); // FIFO ready interrupt
coreyharris 0:0bd4103885bf 61
coreyharris 4:14d1b87cd3c4 62 I2C i2cBus(I2C1_SDA, I2C1_SCL); // I2C bus, P3_4 = SDA, P3_5 = SCL
coreyharris 0:0bd4103885bf 63
coreyharris 4:14d1b87cd3c4 64 MAX30101 op_sensor(i2cBus); // Create new MAX30101 on i2cBus
coreyharris 4:14d1b87cd3c4 65 int rc = op_sensor_config(op_sensor); // Config sensor, return 0 on success
coreyharris 0:0bd4103885bf 66
coreyharris 5:2f708191f1bd 67 MAX30101::InterruptBitField_u ints; // Read interrupt status to clear
coreyharris 5:2f708191f1bd 68 rc = op_sensor.getInterruptStatus(ints); // power on interrupt
coreyharris 5:2f708191f1bd 69
coreyharris 0:0bd4103885bf 70 uint8_t fifoData[MAX30101::MAX_FIFO_BYTES];
coreyharris 0:0bd4103885bf 71 uint16_t idx, readBytes;
coreyharris 2:54182d6a168f 72 int16_t opSample;
coreyharris 2:54182d6a168f 73 uint32_t sample;
coreyharris 0:0bd4103885bf 74 while(1) {
coreyharris 2:54182d6a168f 75
coreyharris 1:471e2b722d24 76 if( rc == 0 ) {
coreyharris 4:14d1b87cd3c4 77
coreyharris 4:14d1b87cd3c4 78 // Check if op_sensor interrupt asserted
coreyharris 1:471e2b722d24 79 if(op_sensorIntFlag) {
coreyharris 4:14d1b87cd3c4 80
coreyharris 1:471e2b722d24 81 op_sensorIntFlag = 0; // Lower interrupt flag
coreyharris 4:14d1b87cd3c4 82 rc = op_sensor.getInterruptStatus(ints); // Read interrupt status
coreyharris 0:0bd4103885bf 83
coreyharris 4:14d1b87cd3c4 84 // Check if FIFO almost full interrupt asserted
coreyharris 1:471e2b722d24 85 if((rc == 0) && (ints.bits.a_full)) {
coreyharris 4:14d1b87cd3c4 86
coreyharris 4:14d1b87cd3c4 87 // Read FIFO
coreyharris 4:14d1b87cd3c4 88 rc = op_sensor.readFIFO(MAX30101::OneLedChannel, fifoData, readBytes);
coreyharris 0:0bd4103885bf 89
coreyharris 1:471e2b722d24 90 if(rc == 0) {
coreyharris 4:14d1b87cd3c4 91
coreyharris 4:14d1b87cd3c4 92 // Convert read bytes into samples
coreyharris 2:54182d6a168f 93 for(idx = 0; idx < readBytes; idx+=3) {
coreyharris 2:54182d6a168f 94 sample = (fifoData[idx]<<16) | (fifoData[idx+1]<<8) | (fifoData[idx+2]);
coreyharris 6:238d2eec53de 95 opSample = sample >> 8; // Sign extends sample to 16-bit opSample
coreyharris 5:2f708191f1bd 96 pc.printf("%i\r\n", opSample); // Print results
coreyharris 0:0bd4103885bf 97 }
coreyharris 4:14d1b87cd3c4 98
coreyharris 0:0bd4103885bf 99 }
coreyharris 0:0bd4103885bf 100 }
coreyharris 0:0bd4103885bf 101 }
coreyharris 3:a580414c44ce 102
coreyharris 5:2f708191f1bd 103 // If rc != 0, a communication error has occurred
coreyharris 3:a580414c44ce 104 } else {
coreyharris 0:0bd4103885bf 105
coreyharris 4:14d1b87cd3c4 106 pc.printf("Something went wrong, "
coreyharris 4:14d1b87cd3c4 107 "check the I2C bus or power connections... \r\n");
coreyharris 0:0bd4103885bf 108 bLed = LED_OFF;
coreyharris 0:0bd4103885bf 109 gLed = LED_OFF;
coreyharris 0:0bd4103885bf 110
coreyharris 0:0bd4103885bf 111 while(1)
coreyharris 0:0bd4103885bf 112 {
coreyharris 0:0bd4103885bf 113 rLed = !rLed;
coreyharris 0:0bd4103885bf 114 wait(0.5);
coreyharris 3:a580414c44ce 115 }
coreyharris 3:a580414c44ce 116 }
coreyharris 0:0bd4103885bf 117
coreyharris 3:a580414c44ce 118 }
coreyharris 0:0bd4103885bf 119 }
coreyharris 0:0bd4103885bf 120
coreyharris 0:0bd4103885bf 121
coreyharris 0:0bd4103885bf 122 bool op_sensor_config(MAX30101 &op_sensor) {
coreyharris 0:0bd4103885bf 123
coreyharris 0:0bd4103885bf 124 //Reset Device
coreyharris 0:0bd4103885bf 125 MAX30101::ModeConfiguration_u modeConfig;
coreyharris 0:0bd4103885bf 126 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 127 modeConfig.bits.reset = 1;
coreyharris 0:0bd4103885bf 128 int32_t rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 129
coreyharris 0:0bd4103885bf 130
coreyharris 0:0bd4103885bf 131 //enable MAX30101 interrupts
coreyharris 0:0bd4103885bf 132 MAX30101::InterruptBitField_u ints;
coreyharris 0:0bd4103885bf 133 if(rc == 0)
coreyharris 0:0bd4103885bf 134 {
coreyharris 0:0bd4103885bf 135 ints.all = 0;
coreyharris 1:471e2b722d24 136 ints.bits.die_temp = 1; // Enable FIFO almost full interrupt
coreyharris 1:471e2b722d24 137 ints.bits.a_full = 1; // Enable internal die temp. interrupt
coreyharris 0:0bd4103885bf 138 rc = op_sensor.enableInterrupts(ints);
coreyharris 0:0bd4103885bf 139 }
coreyharris 0:0bd4103885bf 140
coreyharris 0:0bd4103885bf 141 //configure FIFO
coreyharris 0:0bd4103885bf 142 MAX30101::FIFO_Configuration_u fifoConfig;
coreyharris 0:0bd4103885bf 143 if(rc == 0)
coreyharris 0:0bd4103885bf 144 {
coreyharris 0:0bd4103885bf 145 fifoConfig.all = 0;
coreyharris 5:2f708191f1bd 146 fifoConfig.bits.fifo_a_full = 15; // Max level of 15 samples
coreyharris 5:2f708191f1bd 147 fifoConfig.bits.sample_average = MAX30101::AveragedSamples_8; // Average 8 samples
coreyharris 0:0bd4103885bf 148 rc = op_sensor.setFIFOConfiguration(fifoConfig);
coreyharris 0:0bd4103885bf 149 }
coreyharris 0:0bd4103885bf 150
coreyharris 0:0bd4103885bf 151 MAX30101::SpO2Configuration_u spo2Config;
coreyharris 0:0bd4103885bf 152 if(rc == 0)
coreyharris 0:0bd4103885bf 153 {
coreyharris 1:471e2b722d24 154 spo2Config.all = 0;
coreyharris 1:471e2b722d24 155 spo2Config.bits.spo2_sr = MAX30101::SR_400_Hz; // SpO2 SR = 400Hz
coreyharris 5:2f708191f1bd 156 spo2Config.bits.led_pw = 1; // 16-bit ADC resolution
coreyharris 0:0bd4103885bf 157 rc = op_sensor.setSpO2Configuration(spo2Config);
coreyharris 0:0bd4103885bf 158 }
coreyharris 0:0bd4103885bf 159
coreyharris 0:0bd4103885bf 160 //Set LED1 drive current
coreyharris 0:0bd4103885bf 161 if(rc == 0)
coreyharris 0:0bd4103885bf 162 {
coreyharris 5:2f708191f1bd 163 // Heart Rate only, 1 LED channel, Pulse amp. = 0x1F
coreyharris 2:54182d6a168f 164 rc = op_sensor.setLEDPulseAmplitude(MAX30101::LED1_PA, 0x1F);
coreyharris 0:0bd4103885bf 165 }
coreyharris 0:0bd4103885bf 166
coreyharris 0:0bd4103885bf 167 //Set operating mode
coreyharris 0:0bd4103885bf 168 modeConfig.all = 0;
coreyharris 0:0bd4103885bf 169 if(rc == 0)
coreyharris 0:0bd4103885bf 170 {
coreyharris 1:471e2b722d24 171 modeConfig.bits.mode = MAX30101::HeartRateMode; // Heart-rate only
coreyharris 0:0bd4103885bf 172 rc = op_sensor.setModeConfiguration(modeConfig);
coreyharris 0:0bd4103885bf 173 }
coreyharris 0:0bd4103885bf 174
coreyharris 0:0bd4103885bf 175
coreyharris 0:0bd4103885bf 176 return rc;
coreyharris 0:0bd4103885bf 177 }