SpO2 algorithm used in MAX30101 onboard MAX32620HSP. Algorithm is not perfect but it works

Dependencies:   mbed MAX14720 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX30102.cpp Source File

MAX30102.cpp

00001 /** \file max30102.cpp ******************************************************
00002 *
00003 * Project: MAXREFDES117#
00004 * Filename: max30102.cpp
00005 * Description: This module is an embedded controller driver for the MAX30102
00006 *
00007 *
00008 * --------------------------------------------------------------------
00009 *
00010 * This code follows the following naming conventions:
00011 *
00012 * char              ch_pmod_value
00013 * char (array)      s_pmod_s_string[16]
00014 * float             f_pmod_value
00015 * int32_t           n_pmod_value
00016 * int32_t (array)   an_pmod_value[16]
00017 * int16_t           w_pmod_value
00018 * int16_t (array)   aw_pmod_value[16]
00019 * uint16_t          uw_pmod_value
00020 * uint16_t (array)  auw_pmod_value[16]
00021 * uint8_t           uch_pmod_value
00022 * uint8_t (array)   auch_pmod_buffer[16]
00023 * uint32_t          un_pmod_value
00024 * int32_t *         pn_pmod_value
00025 *
00026 * ------------------------------------------------------------------------- */
00027 /*******************************************************************************
00028 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00029 *
00030 * Permission is hereby granted, free of charge, to any person obtaining a
00031 * copy of this software and associated documentation files (the "Software"),
00032 * to deal in the Software without restriction, including without limitation
00033 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00034 * and/or sell copies of the Software, and to permit persons to whom the
00035 * Software is furnished to do so, subject to the following conditions:
00036 *
00037 * The above copyright notice and this permission notice shall be included
00038 * in all copies or substantial portions of the Software.
00039 *
00040 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00041 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00042 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00043 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00044 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00045 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00046 * OTHER DEALINGS IN THE SOFTWARE.
00047 *
00048 * Except as contained in this notice, the name of Maxim Integrated
00049 * Products, Inc. shall not be used except as stated in the Maxim Integrated
00050 * Products, Inc. Branding Policy.
00051 *
00052 * The mere transfer of this software does not imply any licenses
00053 * of trade secrets, proprietary technology, copyrights, patents,
00054 * trademarks, maskwork rights, or any other form of intellectual
00055 * property whatsoever. Maxim Integrated Products, Inc. retains all
00056 * ownership rights.
00057 *******************************************************************************
00058 */
00059 #include "mbed.h"
00060 #include "MAX30102.h"
00061 
00062 #ifdef TARGET_MAX32600MBED
00063 I2C i2c(I2C1_SDA, I2C1_SCL);
00064 #else
00065 I2C i2c(I2C2_SDA, I2C2_SCL);
00066 #endif
00067 
00068 bool maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data)
00069 /**
00070 * \brief        Write a value to a MAX30102 register
00071 * \par          Details
00072 *               This function writes a value to a MAX30102 register
00073 *
00074 * \param[in]    uch_addr    - register address
00075 * \param[in]    uch_data    - register data
00076 *
00077 * \retval       true on success
00078 */
00079 {
00080   char ach_i2c_data[2];
00081   ach_i2c_data[0]=uch_addr;
00082   ach_i2c_data[1]=uch_data;
00083   
00084   if(i2c.write(I2C_WRITE_ADDR, ach_i2c_data, 2, false)==0)
00085     return true;
00086   else
00087     return false;
00088 }
00089 
00090 bool maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data)
00091 /**
00092 * \brief        Read a MAX30102 register
00093 * \par          Details
00094 *               This function reads a MAX30102 register
00095 *
00096 * \param[in]    uch_addr    - register address
00097 * \param[out]   puch_data    - pointer that stores the register data
00098 *
00099 * \retval       true on success
00100 */
00101 {
00102   char ch_i2c_data;
00103   ch_i2c_data=uch_addr;
00104   if(i2c.write(I2C_WRITE_ADDR, &ch_i2c_data, 1, true)!=0)
00105     return false;
00106   if(i2c.read(I2C_READ_ADDR, &ch_i2c_data, 1, false)==0)
00107   {
00108     *puch_data=(uint8_t) ch_i2c_data;
00109     return true;
00110   }
00111   else
00112     return false;
00113 }
00114 bool maxim_max30102_soft_init()
00115   /**
00116   * \brief        Initialize the MAX30102
00117   * \par          Details
00118   *               This function initializes the MAX30102
00119   *
00120   * \param        None
00121   *
00122   * \retval       true on success
00123   */
00124 {
00125   if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xC0)) // INTR setting
00126     return false;
00127   if(!maxim_max30102_write_reg(REG_INTR_ENABLE_2,0x00))
00128     return false;
00129   if(!maxim_max30102_write_reg(REG_FIFO_WR_PTR,0x00))  //FIFO_WR_PTR[4:0]
00130     return false;
00131   if(!maxim_max30102_write_reg(REG_OVF_COUNTER,0x00))  //OVF_COUNTER[4:0]
00132     return false;
00133   if(!maxim_max30102_write_reg(REG_FIFO_RD_PTR,0x00))  //FIFO_RD_PTR[4:0]
00134     return false;
00135   if(!maxim_max30102_write_reg(REG_FIFO_CONFIG,0x0f))  //sample avg = 1, fifo rollover=false, fifo almost full = 17
00136     return false;
00137   if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x03))   //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
00138     return false;
00139   if(!maxim_max30102_write_reg(REG_SPO2_CONFIG,0x47))  // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS)
00140     return false;
00141   if(!maxim_max30102_write_reg(REG_LED1_PA,0x00))   //Choose value for ~ 7mA for LED1
00142     return false;
00143   if(!maxim_max30102_write_reg(REG_LED2_PA,0x69))   // Choose value for ~ 7mA for LED2
00144     return false;
00145   if(!maxim_max30102_write_reg(REG_PILOT_PA,0x00))   // Choose value for ~ 25mA for Pilot LED
00146     return false;
00147   if(!maxim_max30102_write_reg(REG_PROX_INT_THRESH, 0x21)) 
00148     return false;     
00149   return true;
00150 }
00151 bool maxim_max30102_init()
00152 /**
00153 * \brief        Initialize the MAX30102
00154 * \par          Details
00155 *               This function initializes the MAX30102
00156 *
00157 * \param        None
00158 *
00159 * \retval       true on success
00160 */
00161 {
00162   if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xD0)) // INTR setting
00163     return false;
00164   if(!maxim_max30102_write_reg(REG_INTR_ENABLE_2,0x00))
00165     return false;
00166   if(!maxim_max30102_write_reg(REG_FIFO_WR_PTR,0x00))  //FIFO_WR_PTR[4:0]
00167     return false;
00168   if(!maxim_max30102_write_reg(REG_OVF_COUNTER,0x00))  //OVF_COUNTER[4:0]
00169     return false;
00170   if(!maxim_max30102_write_reg(REG_FIFO_RD_PTR,0x00))  //FIFO_RD_PTR[4:0]
00171     return false;
00172   if(!maxim_max30102_write_reg(REG_FIFO_CONFIG,0x0f))  //sample avg = 1, fifo rollover=false, fifo almost full = 17
00173     return false;
00174   if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x03))   //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
00175     return false;
00176   if(!maxim_max30102_write_reg(REG_SPO2_CONFIG,0x47))  // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS)
00177     return false;
00178   
00179   if(!maxim_max30102_write_reg(REG_LED1_PA,0x3C))   //Choose value for ~ 7mA for LED1
00180     return false;
00181   if(!maxim_max30102_write_reg(REG_LED2_PA,0x69))   // Choose value for ~ 7mA for LED2
00182     return false;
00183   if(!maxim_max30102_write_reg(REG_PILOT_PA,0x7f))   // Choose value for ~ 25mA for Pilot LED
00184     return false;
00185   if(!maxim_max30102_write_reg(REG_PROX_INT_THRESH, 0x21)) 
00186     return false;  
00187   return true;  
00188 }
00189 
00190 bool maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led)
00191 /**
00192 * \brief        Read a set of samples from the MAX30102 FIFO register
00193 * \par          Details
00194 *               This function reads a set of samples from the MAX30102 FIFO register
00195 *
00196 * \param[out]   *pun_red_led   - pointer that stores the red LED reading data
00197 * \param[out]   *pun_ir_led    - pointer that stores the IR LED reading data
00198 *
00199 * \retval       true on success
00200 */
00201 {
00202   uint32_t un_temp;
00203   unsigned char uch_temp;
00204   *pun_red_led=0;
00205   *pun_ir_led=0;
00206   char ach_i2c_data[6];
00207   
00208   //read and clear status register
00209   maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_temp);
00210   maxim_max30102_read_reg(REG_INTR_STATUS_2, &uch_temp);
00211   
00212   ach_i2c_data[0]=REG_FIFO_DATA;
00213   if(i2c.write(I2C_WRITE_ADDR, ach_i2c_data, 1, true)!=0)
00214     return false;
00215   if(i2c.read(I2C_READ_ADDR, ach_i2c_data, 6, false)!=0)
00216   {
00217     return false;
00218   }
00219   un_temp=(unsigned char) ach_i2c_data[0];
00220   un_temp<<=16;
00221   *pun_red_led+=un_temp;
00222   un_temp=(unsigned char) ach_i2c_data[1];
00223   un_temp<<=8;
00224   *pun_red_led+=un_temp;
00225   un_temp=(unsigned char) ach_i2c_data[2];
00226   *pun_red_led+=un_temp;
00227   
00228   un_temp=(unsigned char) ach_i2c_data[3];
00229   un_temp<<=16;
00230   *pun_ir_led+=un_temp;
00231   un_temp=(unsigned char) ach_i2c_data[4];
00232   un_temp<<=8;
00233   *pun_ir_led+=un_temp;
00234   un_temp=(unsigned char) ach_i2c_data[5];
00235   *pun_ir_led+=un_temp;
00236   *pun_red_led&=0x03FFFF;  //Mask MSB [23:18]
00237   *pun_ir_led&=0x03FFFF;  //Mask MSB [23:18]
00238   
00239   
00240   return true;
00241 }
00242 
00243 bool maxim_max30102_reset()
00244 /**
00245 * \brief        Reset the MAX30102
00246 * \par          Details
00247 *               This function resets the MAX30102
00248 *
00249 * \param        None
00250 *
00251 * \retval       true on success
00252 */
00253 {
00254     if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x40))
00255         return false;
00256     else
00257         return true;    
00258 }