Driver, C++ source code, and library for the MAX30101 heart rate sensor chip. The MAX30101 IC includes integrated LEDs and photodetectors for the collection of raw data for Heart Rate/Pulse Rate monitoring and for SpO2 (blood oxygen saturation) levels.
Dependents: MAX30101_Heart_Rate_Sp02_Sensor_Chip MAX30101_Heart_Rate_Sp02_SENSOR_Hello_World
Revision 0:b0addee6d8d1, committed 2018-07-21
- Comitter:
- phonemacro
- Date:
- Sat Jul 21 07:30:15 2018 +0000
- Commit message:
- convert to library from folder
Changed in this revision
MAX30101.cpp | Show annotated file Show diff for this revision Revisions of this file |
MAX30101.h | Show annotated file Show diff for this revision Revisions of this file |
diff -r 000000000000 -r b0addee6d8d1 MAX30101.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MAX30101.cpp Sat Jul 21 07:30:15 2018 +0000 @@ -0,0 +1,721 @@ +/******************************************************************************* + * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + ******************************************************************************* + */ + +#include "mbed.h" +#include "MAX30101.h" + +MAX30101 *MAX30101::instance = NULL; + +//****************************************************************************** +MAX30101::MAX30101(PinName sda, PinName scl, int slaveAddress): + slaveAddress(slaveAddress) { + i2c = new I2C(sda, scl); + i2c_owner = true; + i2c->frequency(400000); + onInterruptCallback = NULL; + onDataAvailableCallback = NULL; + instance = this; +} + +//****************************************************************************** +MAX30101::MAX30101(I2C *_i2c, int slaveAddress) : + slaveAddress(slaveAddress) { + i2c = _i2c; + i2c_owner = false; + i2c->frequency(400000); + onInterruptCallback = NULL; + onDataAvailableCallback = NULL; + instance = this; +} + +//****************************************************************************** +MAX30101::~MAX30101(void) { + if (i2c_owner) { + delete i2c; + } +} + +//****************************************************************************** +int MAX30101::int_handler(void) { + uint16_t index, i; + uint16_t rx_bytes, second_rx_bytes; + char temp_int; + char temp_frac; + uint16_t num_active_led; + uint32_t sample; + int loop = 1; + static uint8_t cntr_int = 0; + + max30101_Interrupt_Status_1_t Interrupt_Status_1; + max30101_Interrupt_Status_2_t Interrupt_Status_2; + max30101_mode_configuration_t mode_configuration; + max30101_multiLED_mode_ctrl_1_t multiLED_mode_ctrl_1; + max30101_multiLED_mode_ctrl_2_t multiLED_mode_ctrl_2; + max30101_spo2_configuration_t spo2_configuration; + max30101_fifo_configuration_t fifo_configuration; + + cntr_int++; + + while (loop) { + if (i2c_reg_read(REG_INT_STAT_1, &Interrupt_Status_1.all) != 0) { ///< Read Interrupt flag bits + return -1; + } + + if (i2c_reg_read(REG_INT_STAT_2, &Interrupt_Status_2.all) != 0) { ///< Read Interrupt flag bits + return -1; + } + + /* Read all the relevant register bits */ + if (i2c_reg_read(REG_MODE_CFG, &mode_configuration.all) != 0) { + return -1; + } + + + if (i2c_reg_read(REG_SLT2_SLT1, &multiLED_mode_ctrl_1.all) != 0) { + return -1; + } + + + if (i2c_reg_read(REG_SLT4_SLT3, &multiLED_mode_ctrl_2.all) != 0) { + return -1; + } + + + if (i2c_reg_read(REG_SPO2_CFG, &spo2_configuration.all) != 0) { + return -1; + } + + + if (i2c_reg_read(REG_FIFO_CFG, &fifo_configuration.all) != 0) { + return -1; + } + + + + if (Interrupt_Status_1.bit.a_full) { + ///< Read the sample(s) + char reg = REG_FIFO_DATA; + + num_active_led = 0; + + if (mode_configuration.bit.mode == 0x02) {///< Heart Rate mode, i.e. 1 led + num_active_led = 1; + } else if (mode_configuration.bit.mode == 0x03) { ///< SpO2 mode, i.e. 2 led + num_active_led = 2; + } else if (mode_configuration.bit.mode == 0x07) { ///< Multi-LED mode, i.e. 1-4 led + if (multiLED_mode_ctrl_1.bit.slot1 != 0) { + num_active_led++; + } + + if (multiLED_mode_ctrl_1.bit.slot2 != 0) { + num_active_led++; + } + + if (multiLED_mode_ctrl_2.bit.slot3 != 0) { + num_active_led++; + } + + if (multiLED_mode_ctrl_2.bit.slot4 != 0) { + num_active_led++; + } + } + ///< 3bytes/LED x Number of Active LED x FIFO level selected + rx_bytes = 3 * num_active_led * (32-fifo_configuration.bit.fifo_a_full); + + second_rx_bytes = rx_bytes; + + /** + * @brief: + * The FIFO Size is determined by the Sample size. The number of bytes + * in a Sample is dictated by number of LED's + * + * #LED Selected Bytes in "1" sample + * 1 3 + * 2 6 + * 3 9 + * 4 12 + * + * The I2C API function limits the number of bytes to read, to 256 (i.e. + * char). Therefore, when set for Multiple LED's and the FIFO + * size is set to 32. It would mean there is more than 256 bytes. + * In that case two I2C reads have to be made. However It is important + * to note that each "Sample" must be read completely and reading only + * partial number of bytes from a sample will result in erroneous data. + * + * + * For example: + * Num of LED selected = 3 and FIFO size is set to 32 (i.e. 0 value in + * register), then the number of bytes will be + * 3bytes/Led * 3led's * 32 = 288 bytes in all. Since there are + * 3 LED's each sample will contain (3 * 3) 9bytes. + * Therefore Sample 1 = 9bytes, Sample 2 = 18,... Sample 28 = 252. + * Therefore the first I2C read should be 252 bytes and the second + * read should be 288-252 = 36. + * + * It turns out that this size issue comes up only when number of LED + * selected is 3 or 4 and choosing 252bytes + * for the first I2C read would work for both Number of LED selection. + */ + + if (rx_bytes <= CHUNK_SIZE) { + I2CM_Read(slaveAddress, ®, 1, &max30101_rawData[0], + (char)rx_bytes /*total_databytes_1*/); + } else { + I2CM_Read(slaveAddress, ®, 1, &max30101_rawData[0], CHUNK_SIZE); + + second_rx_bytes = second_rx_bytes - CHUNK_SIZE; + I2CM_Read(slaveAddress, ®, 1, &max30101_rawData[CHUNK_SIZE], + (char)second_rx_bytes); + } + + index = 0; + + for (i = 0; i < rx_bytes; i += 3) { + sample = ((uint32_t)(max30101_rawData[i] & 0x03) << 16) | (max30101_rawData[i + 1] << 8) | max30101_rawData[i + 2]; + + ///< Right shift the data based on the LED_PW setting + sample = sample >> (3 - spo2_configuration.bit.led_pw); // 0=shift 3, 1=shift 2, 2=shift 1, 3=no shift + + max30101_buffer[index++] = sample; + } + + onDataAvailableCallback(MAX30101_OXIMETER_DATA + num_active_led, max30101_buffer, index); + } + + + ///< This interrupt handles the temperature interrupt + if (Interrupt_Status_2.bit.die_temp_rdy) { + char reg; + + reg = REG_TINT; + if (I2CM_Read(slaveAddress, ®, 1, &temp_int, 1) != 0) { + return -1; + } + + reg = REG_TFRAC; + if (I2CM_Read(slaveAddress, ®, 1, &temp_frac, 1) != 0) { + return -1; + } + + max30101_final_temp = (int8_t)temp_int + 0.0625f * temp_frac; + + if (i2c_reg_write(REG_TEMP_EN, 0x00) != 0) { ///< Die Temperature Config, Temp disable... after one read... + return -1; + } + } + + if (i2c_reg_read(REG_INT_STAT_1, &Interrupt_Status_1.all) != 0) { ///< Read Interrupt flag bits + + return -1; + } + if (Interrupt_Status_1.bit.a_full != 1) { + loop = 0; + } + } + + interruptPostCallback(); + + + return 0; +} + +//****************************************************************************** +int MAX30101::SpO2mode_init(uint8_t fifo_waterlevel_mark, uint8_t sample_avg, + uint8_t sample_rate, uint8_t pulse_width, + uint8_t red_led_current, uint8_t ir_led_current) { + + char status; + + max30101_mode_configuration_t mode_configuration; + max30101_fifo_configuration_t fifo_configuration; + max30101_spo2_configuration_t spo2_configuration; + max30101_Interrupt_Enable_1_t Interrupt_Enable_1; + + mode_configuration.all = 0; + mode_configuration.bit.reset = 1; + if (i2c_reg_write(REG_MODE_CFG, mode_configuration.all) != 0) // Reset the device + { + return -1; + } + + ///< Give it some settle time (100ms) + wait(1.0 / 10.0); ///< Let things settle down a bit + + fifo_configuration.all = 0; + fifo_configuration.bit.smp_ave = sample_avg; ///< Sample averaging; + fifo_configuration.bit.fifo_roll_over_en = 1; ///< FIFO Roll over enabled + fifo_configuration.bit.fifo_a_full = fifo_waterlevel_mark; ///< Interrupt when certain level is filled + + if (i2c_reg_write(REG_FIFO_CFG, fifo_configuration.all) != 0) { + return -1; + } + + spo2_configuration.bit.spo2_adc_rge = 0x2; ///< ADC Range 8192 fullscale + spo2_configuration.bit.spo2_sr = sample_rate; ///< 100 Samp/sec. + spo2_configuration.bit.led_pw = pulse_width; ///< Pulse Width=411us and ADC Resolution=18 + if (i2c_reg_write(REG_SPO2_CFG, spo2_configuration.all) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED1_PA, red_led_current) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED2_PA, ir_led_current) != 0) { + return -1; + } + + /************/ + + if (i2c_reg_read(REG_INT_STAT_1, &status) != 0) ///< Clear INT1 by reading the status + { + return -1; + } + + if (i2c_reg_read(REG_INT_STAT_2, &status) != 0) ///< Clear INT2 by reading the status + { + return -1; + } + + if (i2c_reg_write(REG_FIFO_W_PTR, 0x00) != 0) ///< Clear FIFO ptr + { + return -1; + } + + if (i2c_reg_write(REG_FIFO_OVF_CNT, 0x00) != 0) ///< Clear FIFO ptr + { + return -1; + } + + if (i2c_reg_write(REG_FIFO_R_PTR, 0x00) != 0) ///< Clear FIFO ptr + { + return -1; + } + + Interrupt_Enable_1.all = 0; + Interrupt_Enable_1.bit.a_full_en = 1; ///< Enable FIFO almost full interrupt + if (i2c_reg_write(REG_INT_EN_1, Interrupt_Enable_1.all) != 0) { + return -1; + } + + mode_configuration.all = 0; + mode_configuration.bit.mode = 0x03; ///< SpO2 mode + if (i2c_reg_write(REG_MODE_CFG, mode_configuration.all) != 0) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30101::SpO2mode_stop(void) { + + max30101_Interrupt_Enable_1_t Interrupt_Enable_1; + max30101_mode_configuration_t mode_configuration; + uint8_t led1_pa; + uint8_t led2_pa; + + Interrupt_Enable_1.all = 0; + Interrupt_Enable_1.bit.a_full_en = 0; ///< Disable FIFO almost full interrupt + if (i2c_reg_write(REG_INT_EN_1, Interrupt_Enable_1.all) != 0) { + return -1; + } + + mode_configuration.all = 0; + mode_configuration.bit.mode = 0x00; ///< SpO2 mode off + if (i2c_reg_write(REG_MODE_CFG, mode_configuration.all) != 0) { + return -1; + } + + led1_pa = 0; ///< RED LED current, 0.0 + if (i2c_reg_write(REG_LED1_PA, led1_pa) != 0) { + return -1; + } + + led2_pa = 0; ///< IR LED current, 0.0 + if (i2c_reg_write(REG_LED2_PA, led2_pa) != 0) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30101::HRmode_init(uint8_t fifo_waterlevel_mark, uint8_t sample_avg, + uint8_t sample_rate, uint8_t pulse_width, + uint8_t red_led_current) { + + /*uint8_t*/ char status; + + max30101_mode_configuration_t mode_configuration; + max30101_fifo_configuration_t fifo_configuration; + max30101_spo2_configuration_t spo2_configuration; + max30101_Interrupt_Enable_1_t Interrupt_Enable_1; + + mode_configuration.all = 0; + mode_configuration.bit.reset = 1; + if (i2c_reg_write(REG_MODE_CFG, mode_configuration.all) != 0) ///< Reset the device, Mode = don't use... + { + return -1; + } + + ///< Give it some settle time (100ms) + wait(1.0 / 10.0); ///< Let things settle down a bit + + fifo_configuration.all = 0; + fifo_configuration.bit.smp_ave = sample_avg; ///< Sample averaging; + fifo_configuration.bit.fifo_roll_over_en = 1; ///< FIFO Roll over enabled + fifo_configuration.bit.fifo_a_full = fifo_waterlevel_mark; ///< Interrupt when certain level is filled + if (i2c_reg_write(REG_FIFO_CFG, fifo_configuration.all) != 0) { + return -1; + } + + spo2_configuration.bit.spo2_adc_rge = 0x2; ///< ADC Range 8192 fullscale + spo2_configuration.bit.spo2_sr = sample_rate; ///< 100 Samp/sec. + spo2_configuration.bit.led_pw = pulse_width; ///< Pulse Width=411us and ADC Resolution=18 + if (i2c_reg_write(REG_SPO2_CFG, spo2_configuration.all) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED1_PA, red_led_current) != 0) { + return -1; + } + + /************/ + + if (i2c_reg_read(REG_INT_STAT_1, &status) != 0) ///< Clear INT1 by reading the status + { + return -1; + } + + if (i2c_reg_read(REG_INT_STAT_2, &status) != 0) ///< Clear INT2 by reading the status + { + return -1; + } + + if (i2c_reg_write(REG_FIFO_W_PTR, 0x00) != 0) ///< Clear FIFO ptr + { + return -1; + } + + if (i2c_reg_write(REG_FIFO_OVF_CNT, 0x00) != 0) ///< Clear FIFO ptr + { + return -1; + } + + if (i2c_reg_write(REG_FIFO_R_PTR, 0x00) != 0) ///< Clear FIFO ptr + { + return -1; + } + + Interrupt_Enable_1.all = 0; + Interrupt_Enable_1.bit.a_full_en = 1; + + // Interrupt + if (i2c_reg_write(REG_INT_EN_1, Interrupt_Enable_1.all) != 0) { + return -1; + } + + mode_configuration.all = 0; + mode_configuration.bit.mode = 0x02; ///< HR mode + if (i2c_reg_write(REG_MODE_CFG, mode_configuration.all) != 0) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30101::HRmode_stop(void) { + + max30101_Interrupt_Enable_1_t Interrupt_Enable_1; + max30101_mode_configuration_t mode_configuration; + + Interrupt_Enable_1.all = 0; + Interrupt_Enable_1.bit.a_full_en = 0; ///< Disable FIFO almost full interrupt + if (i2c_reg_write(REG_INT_EN_1, Interrupt_Enable_1.all) != 0) { + return -1; + } + + mode_configuration.all = 0; + mode_configuration.bit.mode = 0x00; ///< HR mode off + if (i2c_reg_write(REG_MODE_CFG, mode_configuration.all) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED1_PA, 0) != 0) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30101::Multimode_init(uint8_t fifo_waterlevel_mark, uint8_t sample_avg, + uint8_t sample_rate, uint8_t pulse_width, + uint8_t red_led_current, uint8_t ir_led_current, + uint8_t green_led_current, uint8_t slot_1, + uint8_t slot_2, uint8_t slot_3, uint8_t slot_4) { + + char status; + max30101_mode_configuration_t mode_configuration; + max30101_fifo_configuration_t fifo_configuration; + max30101_spo2_configuration_t spo2_configuration; + max30101_multiLED_mode_ctrl_1_t multiLED_mode_ctrl_1; + max30101_multiLED_mode_ctrl_2_t multiLED_mode_ctrl_2; + max30101_Interrupt_Enable_1_t Interrupt_Enable_1; + + mode_configuration.all = 0; + mode_configuration.bit.reset = 1; + if (i2c_reg_write(REG_MODE_CFG, mode_configuration.all) != 0) ///< Reset the device, Mode = don't use... + { + return -1; + } + + /* Give it some settle time (100ms) */ ///< Let things settle down a bit + wait(1.0 / 10.0); + + fifo_configuration.all = 0; + fifo_configuration.bit.smp_ave = sample_avg; ///< Sample averaging; + fifo_configuration.bit.fifo_roll_over_en = 1; ///< FIFO Roll over enabled + fifo_configuration.bit.fifo_a_full = + fifo_waterlevel_mark; ///< Interrupt when certain level is filled + if (i2c_reg_write(REG_FIFO_CFG, fifo_configuration.all) != 0) { + return -1; + } + + spo2_configuration.bit.spo2_adc_rge = 0x2; ///< ADC Range 8192 fullscale + spo2_configuration.bit.spo2_sr = sample_rate; ///< 100 Samp/sec. + spo2_configuration.bit.led_pw = pulse_width; ///< Pulse Width=411us and ADC Resolution=18 + if (i2c_reg_write(REG_SPO2_CFG, spo2_configuration.all) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED1_PA, red_led_current) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED2_PA, ir_led_current) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED3_PA, green_led_current) != 0) { + return -1; + } + + ///< 0x01=Red(LED1), 0x02=IR(LED2), 0x03=Green(LED3) : Use LEDn_PA to adjust the intensity + ///< 0x05=Red , 0x06=IR , 0x07=Green : Use PILOT_PA to adjust the intensity DO NOT USE THIS ROW... + + multiLED_mode_ctrl_1.bit.slot1 = slot_1; + multiLED_mode_ctrl_1.bit.slot2 = slot_2; + if (i2c_reg_write(REG_SLT2_SLT1, multiLED_mode_ctrl_1.all)) { + return -1; + } + + multiLED_mode_ctrl_2.all = 0; + multiLED_mode_ctrl_2.bit.slot3 = slot_3; + multiLED_mode_ctrl_2.bit.slot4 = slot_4; + if (i2c_reg_write(REG_SLT4_SLT3, multiLED_mode_ctrl_2.all)) { + return -1; + } + + /************/ + + if (i2c_reg_read(REG_INT_STAT_1, &status) != 0) ///< Clear INT1 by reading the status + { + return -1; + } + + if (i2c_reg_read(REG_INT_STAT_2, &status) != 0) ///< Clear INT2 by reading the status + { + return -1; + } + + if (i2c_reg_write(REG_FIFO_W_PTR, 0x00) != 0) ///< Clear FIFO ptr + { + return -1; + } + + if (i2c_reg_write(REG_FIFO_OVF_CNT, 0x00) != 0) ///< Clear FIFO ptr + { + return -1; + } + + if (i2c_reg_write(REG_FIFO_R_PTR, 0x00) != 0) ///< Clear FIFO ptr + { + return -1; + } + + Interrupt_Enable_1.all = 0; + Interrupt_Enable_1.bit.a_full_en = 1; ///< Enable FIFO almost full interrupt + if (i2c_reg_write(REG_INT_EN_1, Interrupt_Enable_1.all) != 0) { + return -1; + } + + mode_configuration.all = 0; + mode_configuration.bit.mode = 0x07; ///< Multi-LED mode + if (i2c_reg_write(REG_MODE_CFG, mode_configuration.all) != 0) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30101::Multimode_stop(void) { + +max30101_Interrupt_Enable_1_t Interrupt_Enable_1; +max30101_mode_configuration_t mode_configuration; + + + Interrupt_Enable_1.all = 0; + Interrupt_Enable_1.bit.a_full_en = 0; ///< Disable FIFO almost full interrupt + if (i2c_reg_write(REG_INT_EN_1, Interrupt_Enable_1.all) != 0) { + return -1; + } + + mode_configuration.all = 0; + mode_configuration.bit.mode = 0x00; ///< Multi-LED mode off + if (i2c_reg_write(REG_MODE_CFG, mode_configuration.all) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED1_PA, 0) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED2_PA, 0) != 0) { + return -1; + } + + if (i2c_reg_write(REG_LED3_PA, 0) != 0) { + return -1; + } + return 0; +} + +//****************************************************************************** +int MAX30101::tempread(void) { + + if (i2c_reg_write(REG_INT_EN_2, 0x02) != 0) {///< Interrupt Enable 2, Temperature Interrupt + return -1; + } + + if (i2c_reg_write(REG_TEMP_EN, 0x01) != 0) {///< Die Temperature Config, Temp enable... + + return -1; + } + return 0; +} + +//****************************************************************************** +int MAX30101::i2c_reg_write(MAX30101_REG_map_t reg, char value) { + + char cmdData[2] = {reg, value}; + + if (I2CM_Write(slaveAddress, NULL, 0, cmdData, 2) != 0) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30101::i2c_reg_read(MAX30101_REG_map_t reg, char *value) { + if (I2CM_Read(slaveAddress, (char *)®, 1, value, 1) != 0 /*1*/) { + return -1; + } + + return 0; +} + +//****************************************************************************** +int MAX30101::I2CM_Read(int slaveAddress, char *writeData, char writeCount, + char *readData, char readCount) { + + if (writeData != NULL && writeCount != 0) { + i2c->write(slaveAddress, writeData, writeCount, true); + } + if (readData != NULL && readCount != 0) { + i2c->read(slaveAddress, readData, readCount); + } + return 0; +} + +//****************************************************************************** +int MAX30101::I2CM_Write(int slaveAddress, char *writeData1, char writeCount1, + char *writeData2, char writeCount2) { + + if (writeData1 != NULL && writeCount1 != 0) { + i2c->write(slaveAddress, writeData1, writeCount1); + } + if (writeData2 != NULL && writeCount2 != 0) { + i2c->write(slaveAddress, writeData2, writeCount2); + } + return 0; +} + +//****************************************************************************** +void MAX30101::onDataAvailable(DataCallbackFunction _onDataAvailable) { + + onDataAvailableCallback = _onDataAvailable; +} + +//****************************************************************************** +void MAX30101::dataAvailable(uint32_t id, uint32_t *buffer, uint32_t length) { + + if (onDataAvailableCallback != NULL) { + (*onDataAvailableCallback)(id, buffer, length); + } +} + +//****************************************************************************** +void MAX30101::onInterrupt(InterruptFunction _onInterrupt) { + + onInterruptCallback = _onInterrupt; +} + +//****************************************************************************** +void MAX30101::interruptPostCallback(void) { + + if (onInterruptCallback != NULL) { + + (*onInterruptCallback)(); + } +} + +//****************************************************************************** +void MAX30101::MidIntHandler(void) { + + MAX30101::instance->int_handler(); +}
diff -r 000000000000 -r b0addee6d8d1 MAX30101.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MAX30101.h Sat Jul 21 07:30:15 2018 +0000 @@ -0,0 +1,568 @@ +/******************************************************************************* +/ * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. + * + * Permission is hereby granted, free of charge, to any person obtaining a + * copy of this software and associated documentation files (the "Software"), + * to deal in the Software without restriction, including without limitation + * the rights to use, copy, modify, merge, publish, distribute, sublicense, + * and/or sell copies of the Software, and to permit persons to whom the + * Software is furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included + * in all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS + * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF + * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. + * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES + * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, + * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR + * OTHER DEALINGS IN THE SOFTWARE. + * + * Except as contained in this notice, the name of Maxim Integrated + * Products, Inc. shall not be used except as stated in the Maxim Integrated + * Products, Inc. Branding Policy. + * + * The mere transfer of this software does not imply any licenses + * of trade secrets, proprietary technology, copyrights, patents, + * trademarks, maskwork rights, or any other form of intellectual + * property whatsoever. Maxim Integrated Products, Inc. retains all + * ownership rights. + *******************************************************************************/ +/** + * Maxim Integrated MAX30101 Oximeter chip + * + * IMPORTANT: The code below will also need MAX14720.cpp and MAX14720.h + * + * @code + * #include "mbed.h" + * #include "MAX14720.h" + * #include "MAX30101.h" + * + * + * /// define the HVOUT Boost Voltage default for the MAX14720 PMIC + * #define HVOUT_VOLTAGE 4500 // set to 4500 mV + * + * /// define all I2C addresses + * #define MAX14720_I2C_SLAVE_ADDR (0x54) + * #define MAX30101_I2C_SLAVE_ADDR (0xAE) + * + * /// Settings for the HR initialization + * #define FIFO_WATERLEVEL_MARK 15 + * #define SAMPLE_AVG 2 + * #define SAMPLE_RATE 1 + * #define PULSE_WIDTH 2 + * #define RED_LED_CURRENT 0x1F + * + * /// Buffer size for streaming data out. + * #define BUFFER_LENGTH 50 + * + * + * /// I2C Master 2 + * I2C i2c2(I2C2_SDA, I2C2_SCL); // used by MAX14720, MAX30101, LIS2DH + * /// SPI Master 0 with SPI0_SS for use with MAX30001 + * SPI spi(SPI0_MOSI, SPI0_MISO, SPI0_SCK, SPI0_SS); // used by MAX30001 + * + * /// PMIC + * MAX14720 max14720(&i2c2, MAX14720_I2C_SLAVE_ADDR); + * /// Optical Oximeter + * MAX30101 max30101(&i2c2, MAX30101_I2C_SLAVE_ADDR); + * InterruptIn max30101_Interrupt(P4_0); + * + * //@brief Creating a buffer to hold the data + * uint32_t oxiBuffer[BUFFER_LENGTH]; + * int oxiIndex = 0; + * char data_trigger = 0; + * + * + * //@brief Creates a packet that will be streamed via USB Serial + * //@brief the packet created will be inserted into a fifo to be streamed at a later time + * //@param id Streaming ID + * //@param buffer Pointer to a uint32 array that contains the data to include in the packet + * //@param number Number of elements in the buffer + * // + * void StreamPacketUint32_ex(uint32_t id, uint32_t *buffer, uint32_t number) { + * int i; + * if (id == MAX30101_OXIMETER_DATA + 1) { + * + * for (i = 0; i < number; i++) { + * oxiBuffer[oxiIndex] = buffer[i]; + * oxiIndex++; + * + * if (oxiIndex > BUFFER_LENGTH) + * { + * data_trigger = 1; + * oxiIndex = 0; + * } + * } + * } + * } + * + * int main() { + * // hold results for returning functions + * int result; + * + * // initialize HVOUT on the MAX14720 PMIC + * result = max14720.init(); + * if (result == MAX14720_ERROR){ + * printf("Error initializing MAX14720"); + * } + * max14720.boostEn = MAX14720::BOOST_ENABLED; + * max14720.boostSetVoltage(HVOUT_VOLTAGE); + * + * // MAX30101 initialize interrupt + * max30101.onDataAvailable(&StreamPacketUint32_ex); + * max30101_Interrupt.fall(&MAX30101::MAX30101MidIntHandler); + * + * // This is the HR mode only (IR LED only) + * max30101.HRmode_init(FIFO_WATERLEVEL_MARK, SAMPLE_AVG, SAMPLE_RATE,PULSE_WIDTH, RED_LED_CURRENT); + * + * printf("Please wait for data to start streaming\n"); + * fflush(stdout); + * + * while (1) { + * if(data_trigger == 1) + * { + * printf("%ld ", oxiBuffer[oxiIndex]); // Print the ECG data on a serial port terminal software + * fflush(stdout); + * } + * } + * } + * @endcode + * + */ + +#ifndef _MAX30101_H_ +#define _MAX30101_H_ + +#include "mbed.h" + +#define MAX30101_RAW_DATA_SIZE 3 * 4 * 32 +#define MAX30101_PROC_DATA_SIZE 4 * 32 + +#define MAX30101_OXIMETER_DATA 0x10 + +#define CHUNK_SIZE 252 + +///< MAX30101 Register addresses + +#define MAX30101_INT_PORT 4 +#define MAX30101_INT_PIN 0 +#define MAX30101_MASTER_NUM 2 + +/** +* Maxim Integrated MAX30101 Oximeter chip +*/ +class MAX30101 { +public: + float max30101_final_temp; ///< Global declaration + uint32_t max30101_buffer[MAX30101_PROC_DATA_SIZE]; ///< final Processed data + char max30101_rawData[MAX30101_RAW_DATA_SIZE]; ///< raw data from the chip + + typedef enum { ///< MAX30101 Register addresses + + ///< Status + REG_INT_STAT_1 = 0x00, + REG_INT_STAT_2 = 0x01, + REG_INT_EN_1 = 0x02, + REG_INT_EN_2 = 0x03, + + REG_FIFO_W_PTR = 0x04, + REG_FIFO_OVF_CNT = 0x05, + REG_FIFO_R_PTR = 0x06, + REG_FIFO_DATA = 0x07, + ///< Configuration + REG_FIFO_CFG = 0x08, + REG_MODE_CFG = 0x09, + REG_SPO2_CFG = 0x0A, + REG_LED1_PA = 0x0C, + REG_LED2_PA = 0x0D, + REG_LED3_PA = 0x0E, + REG_PILOT_PA = 0x10, + REG_SLT2_SLT1 = 0x11, + REG_SLT4_SLT3 = 0x12, + ///< Die Temp + REG_TINT = 0x1F, + REG_TFRAC = 0x20, + REG_TEMP_EN = 0x21, + ///< Proximity Func + REG_PROX_INT_THR = 0x30, + /* Part ID */ + REG_REV_ID = 0xFE, + REG_ID = 0xFF, + } MAX30101_REG_map_t; + + /**********/ + /* STATUS */ + /**********/ + /// @brief STATUS1 (0x00) + typedef union max30101_Interrupt_Status_1_reg { + char all; + struct { + char pwr_rdy : 1; + char reserved : 3; + char prox_int : 1; + char alc_ovf : 1; + char ppg_rdy : 1; + char a_full : 1; + } bit; + } max30101_Interrupt_Status_1_t; + + /// @brief STATUS2 (0x01) + typedef union max30101_Interrupt_Status_2_reg { + char all; + struct { + char reserved1 : 1; + char die_temp_rdy : 1; + char reserved2 : 6; + } bit; + } max30101_Interrupt_Status_2_t; + + /// @brief INTERRUPT_ENABLE1 (0x02) + typedef union max30101_Interrupt_Enable_1_reg { + char all; + struct { + char reserved1 : 4; + char prox_int_en : 1; + char alc_ovf_en : 1; + char ppg_rdy_en : 1; + char a_full_en : 1; + } bit; + } max30101_Interrupt_Enable_1_t; + + /// @brief INTERRUPT_ENABLE2 (0x03) + typedef union max30101_Interrupt_Enable_2_reg { + char all; + struct { + char reserved1 : 1; + char die_temp_rdy_en : 1; + char reserved2 : 6; + } bit; + } max30101_Interrupt_Enable_2_t; + + /*********/ + /* FIFO */ + /*********/ + // 0x04 + /// @brief FIFO_WR_PTR (0x04) + typedef union max30101_fifo_wr_ptr_reg { + char all; + struct { + char fifo_wr_ptr : 5; + char reserved1 : 3; + } bit; + } max30101_fifo_wr_ptr_t; + + /// @brief OVF_COUNTER (0x05) + typedef union max30101_ovf_counter_reg { + char all; + struct { + char fifo_ovf_counter : 5; + char reserved1 : 3; + } bit; + } max30101_ovf_counter_reg_t; + + /// @brief FIFO_READ_PTR (0x06) + typedef union max30101_fifo_rd_ptr_reg { + char all; + struct { + char fifo_rd_ptr : 5; + char reserved1 : 3; + } bit; + } max30101_fifo_rd_ptr_t; + + /********************/ + /* Configuration */ + /********************/ + // 0x08 + /// @brief FIFO_CONFIGURATION (0x08) + typedef union max30101_fifo_configuration_reg { + char all; + struct { + char fifo_a_full : 4; + char fifo_roll_over_en : 1; + char smp_ave : 3; + } bit; + } max30101_fifo_configuration_t; + + /// @brief MODE_CONFIGURATION (0x09) + typedef union max30101_mode_configuration_reg { + char all; + struct { + char mode : 3; + char reserved1 : 3; + char reset : 1; + char shdn : 1; + } bit; + } max30101_mode_configuration_t; + + /// @brief SPO2_CONGIGURATION (0x0A) + typedef union max30101_spo2_configuration_reg { + char all; + struct { + char led_pw : 2; + char spo2_sr : 3; + char spo2_adc_rge : 2; + char reserved1 : 1; + } bit; + } max30101_spo2_configuration_t; + + typedef union max30101_multiLED_mode_ctrl_1_reg { + char all; + struct { + char slot1 : 3; + char reserved : 1; + char slot2 : 3; + char reserved1 : 1; + } bit; + } max30101_multiLED_mode_ctrl_1_t; + + typedef union max30101_multiLED_mode_ctrl_2_reg { + char all; + struct { + char slot3 : 3; + char reserved : 1; + char slot4 : 3; + char reserved1 : 1; + } bit; + } max30101_multiLED_mode_ctrl_2_t; + + /********************/ + /* Die Temperature */ + /********************/ + + char max30101_tinit; + + char max30101_tfrac; + + typedef union max30101_die_temp_config { + char all; + struct { + char temp_en : 1; + char reserved : 7; + } bit; + } max30101_die_temp_config_t; + + /***** Function Prototypes *****/ + + char max30101_prox_int_thresh; + + /** + * @brief MAX30101 constructor. + * @param sda mbed pin to use for SDA line of I2C interface. + * @param scl mbed pin to use for SCL line of I2C interface. + */ + MAX30101(PinName sda, PinName scl, int slaveAddress); + + /** + * @brief MAX30101 constructor. + * @param i2c I2C object to use. + */ + MAX30101(I2C *i2c, int slaveAddress); + + /** + * @brief MAX30101 destructor. + */ + ~MAX30101(void); + + /** + * @brief Allows reading from MAX30101 register + * @param reg: is the register address, to read from (look at max30101.h and the + * data sheet for details) + * @param value: is the pointer to the value read from the register + * @returns 0-if no error. A non-zero value indicates an error. + */ + int i2c_reg_read(MAX30101_REG_map_t reg, char *value); + + /** + * @brief Allows writing to MAX30101 register + * @param reg: is the register address, to read from (look at max30101.h and + * the data sheet for details) + * @param value: is the value to write to the register + * @returns 0-if if no error. A non-zero value indicates an error. + */ + int i2c_reg_write(MAX30101_REG_map_t reg, char value); + + /** + * @brief This function sets up for the SpO2 mode. The data is returned in + * the callback function + * @brief max30101_int_handler in global array: buffer[]. SP mode handles two LED (Red,IR) data. Hence it + * @brief can fill up the FIFO up to a maximum of 3bytes/sample x 32 x 2 = 192bytes. + * @param fifo_waterlevel_mark: corresponds to FIFO_A_FULL, In FIFO Configuration Register (0x08) + * @param sample_avg: corresponds to SMP_AVE, in FIFO Configuration Register (0x08) + * @param sample_rate: corresponds to SPO2_SR, IN SpO2 Configuration Register (0x0A) + * @param pulse_width: corresponds to LED_PW in SpO2 Configuration register(0x0A) + * @param red_led_current: corresponds to LED1_PA register (0x0C). Please see data sheet for values + * @param ir_led_current: corresponds to LED2_PA register (0x0D). Please see data sheet for values + * @returns 0-if everything is good. A non-zero value indicates an error. + */ + int SpO2mode_init(uint8_t fifo_waterlevel_mark, uint8_t sample_avg, + uint8_t sample_rate, uint8_t pulse_width, + uint8_t red_led_current, uint8_t ir_led_current); + + /** + * @brief This function will stop the SpO2 mode and turn off all operating LED�s. + * @return 0-if if no error. A non-zero value indicates an error. + */ + int SpO2mode_stop(void); + + /** + * @brief This function sets up for the HR mode. The data is returned in thecallback function + * @brief max30101_int_handler in global array: buffer[].HR mode handles one LED (Red) data. Hence it can fill + * @brief up the FIFO up to a maximum of 3bytes/sample x 32 = 96bytes. + * @brief fifo_waterlevel_mark: corresponds to FIFO_A_FULL, In FIFO Configuration Register (0x08) + * @param sample_avg: corresponds to SMP_AVE, in FIFO Configuration Register (0x08) + * @param sample_rate:corresponds to SPO2_SR, IN SpO2 Configuration Register (0x0A) + * @param pulse_width: corresponds to LED_PW in SpO2 Configuration Register(0x0A) + * @param red_led_current: corresponds to LED1_PA register (0x0C). Please see data sheet for values + * @returns 0-if if no error. A non-zero value indicates an error. + */ + int HRmode_init(uint8_t fifo_waterlevel_mark, uint8_t sample_avg, + uint8_t sample_rate, uint8_t pulse_width, + uint8_t red_led_current); + + /** + * @brief This function will stop the HR mode and turn off all operating + * LED’s. + * @return 0-if if no error. A non-zero value indicates an error. + */ + int HRmode_stop(void); + + /** + *@brief This function sets up for the Multi-mode. The data is returned in the callback function max30101_int_handler in global array: + *@brief buffer[]. Multi-LED mode can handle 1 to 4 LED combinations. Hence it can fill up the FIFO up to a maximum of + *@brief 3bytes/sample x 32 x 4 = 384bytes. + *@param fifo_waterlevel_mark: corresponds to FIFO_A_FULL, In FIFO Configuration Register (0x08) + *@param sample_avg: corresponds to SMP_AVE, in FIFO Configuration Register (0x08) + *@param sample_rate:corresponds to SPO2_SR, IN SpO2 Configuration Register (0x0A) + *@param pulse_width: corresponds to LED_PW in SpO2 Configuration register(0x0A) + *@param red_led_current: corresponds to LED1_PA register (0x0C). Please see data sheet for values + *@param ir_led_current: corresponds to LED2_PA register (0x0D). Please see data sheet for values + *@param green_led_current: corresponds to LED3_PA register (0x0E). Please see data sheet for values + *@param slot_1,…,slot_4: corresponds to Multi-LED Mode control Registers (0x11-0x12). + *@returns 0-if if no error. A non-zero value indicates an error. + */ + int Multimode_init(uint8_t fifo_waterlevel_mark, uint8_t sample_avg, + uint8_t sample_rate, uint8_t pulse_width, + uint8_t red_led_current, uint8_t ir_led_current, + uint8_t green_led_current, uint8_t slot_1, uint8_t slot_2, + uint8_t slot_3, uint8_t slot_4); + + /** + * @brief This function will stop the Multi-mode and turn off all operating LED’s. + * @returns 0-if if no error. A non-zero value indicates an error. + */ + int Multimode_stop(void); + + /** + * @brief This is a function that sets up for temperature read and should be called after one of the mode + * @brief has been setup. The data is returned in the callback function max30101_int_handler. This + * @brief function needs to be called every time temperature reading is required. + * @brief Call the temp function after one of the MODES have been started + * @brief Note that the temp is disabled after one read... also, it is not necessary + * @brief to read the temp frequently... + * @returns 0-if if no error. A non-zero value indicates an error. + */ + int tempread(void); + + /** + *@brief This is a callback function which collects the data from the FIFO of the MAX30101 in a 32-bit + *@brief unsigned global array called max30101_buffer[]. Upon every interrupt from the MAX30101, this + *@brief function is called to service the FIFO of the MAX30101. This callback function also services the + *@brief interrupt for the temp data. The temp data is collected in a floating point global variable + *@brief final_temp. + *@param max30101_buffer[], global uint32_t + *@returns 0-if everything is good. A non-zero value indicates an error. + */ + int int_handler(void); + + /** + * @brief encapsulates the int_handler above + */ + static void MidIntHandler(void); + + /** + * @brief type definition for data interrupt + */ + typedef void (*DataCallbackFunction)(uint32_t id, uint32_t *buffer, + uint32_t length); + /** + * @brief type definition for general interrupt + */ + typedef void (*InterruptFunction)(); + + /** + * @brief Used to connect a callback for when interrupt data is available + */ + void onInterrupt(InterruptFunction _onInterrupt); + + /** + * @brief Used to connect a callback for when interrupt data is available + */ + void onDataAvailable(DataCallbackFunction _onDataAvailable); + + static MAX30101 *instance; + +private: + /** + * @brief Used to notify an external function that interrupt data is available + * @param id type of data available + * @param buffer 32-bit buffer that points to the data + * @param length length of 32-bit elements available + */ + void dataAvailable(uint32_t id, uint32_t *buffer, uint32_t length); + + + /** + * @brief Executed on interrupt (callback function at the end of the interrupt) + * @param id type of data available + * @param buffer 32-bit buffer that points to the data + * @param length length of 32-bit elements available + */ + void interruptPostCallback(void); + + /// callback function when interrupt data is available + DataCallbackFunction onDataAvailableCallback; + + /// callback function when interrupt data is available + InterruptFunction onInterruptCallback; + + /** + * @brief Read from an I2C device (Read I2c wrapper method) + * @param slaveAddress slave address to use with transaction + * @param writeData pointer of data to write + * @param writeCount number of data to write + * @param readData pointer to buffer to read to + * @param readCount number of bytes to read + */ + + int I2CM_Read(int slaveAddress, char *writeData, char writeCount, char *readData, char readCount); + + /** + * @brief Write to an I2C device (I2C wrapper method) + * @param slaveAddress slave address to use with transaction + * @param writeData1 pointer of data to write + * @param writeCount1 number of data to write + * @param writeData2 pointer to buffer to read to + * @param writeCount2 number of bytes to read + */ + int I2CM_Write(int slaveAddress, char *writeData1, char writeCount1, char *writeData2, char writeCount2); + + /// @brief pointer to I2C object + I2C *i2c; + + /// @brief flag to track if this object is the owner (created) the I2C object + bool i2c_owner; + + /// @brief Device slave address + int slaveAddress; +}; + + /** + * @brief Resets the I2C block, when needed + */ +extern void I2CM_Init_Reset(uint8_t index, int speed); + + + +#endif /* _MAX30101_H_ */