Library for MAX30101 SpO2 and heart rate sensor

Dependents:   HeartRate HeartRate proj final_project_ee119 ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX30101.h Source File

MAX30101.h

00001 /*******************************************************************************
00002  * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  *******************************************************************************
00032  */
00033 #ifndef __MAX30101_H_
00034 #define __MAX30101_H_
00035 
00036 #include "mbed.h"
00037 
00038 /**
00039  * @brief Library for the MAX30101\n
00040  * The MAX30101 is an integrated pulse oximetry and heart-rate monitor module. 
00041  * It includes internal LEDs, photodetectors, optical elements, and low-noise 
00042  * electronics with ambient light rejection. The MAX30101 provides a complete 
00043  * system solution to ease the design-in process for mobile and wearable devices.
00044  *
00045  * @code
00046  * #include "mbed.h"
00047  * #include "max32630fthr.h"
00048  * #include "MAX30101.h"
00049  * 
00050  * MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
00051  *
00052  * //Get I2C instance
00053  * I2C i2cBus(P3_4, P3_5);
00054  *
00055  * //Get temp sensor instance
00056  * MAX30101 op_sensor(i2cBus); //Constructor takes 7-bit slave adrs
00057  *
00058  * int main(void) 
00059  * {
00060  *     //use sensor
00061  * }
00062  * @endcode
00063  */
00064  
00065 class MAX30101
00066 {
00067 public:
00068 
00069     ///7-bit slave address
00070     static const uint8_t I2C_ADRS   = 0x57; 
00071     ///8-bit write address
00072     static const uint8_t I2C_W_ADRS = 0xAE;
00073     ///8-bit read address
00074     static const uint8_t I2C_R_ADRS = 0xAF;
00075     ///Max # Bytes in FIFO
00076     static const uint16_t MAX_FIFO_BYTES = 288;
00077     ///# of bytes per LED channel
00078     static const uint8_t BYTES_PER_CH = 3;
00079     
00080     ///MAX30101 Register Map
00081     enum Registers_e
00082     {
00083         InterruptStatus1   = 0x00,
00084         InterruptStatus2   = 0x01,
00085         InterruptEnable1   = 0x02,
00086         InterruptEnable2   = 0x03,
00087         FIFO_WritePointer  = 0x04,
00088         OverflowCounter    = 0x05,
00089         FIFO_ReadPointer   = 0x06,
00090         FIFO_DataRegister  = 0x07,
00091         FIFO_Configuration = 0x08,
00092         ModeConfiguration  = 0x09,
00093         SpO2Configuration  = 0x0A,
00094         LED1_PA            = 0x0C,
00095         LED2_PA            = 0x0D,
00096         LED3_PA            = 0x0E,
00097         ProxModeLED_PA     = 0x10,
00098         ModeControlReg1    = 0x11,
00099         ModeControlReg2    = 0x12,
00100         DieTempInt         = 0x1F, 
00101         DieTempFrac        = 0x20,
00102         DieTempConfig      = 0x21,
00103         ProxIntThreshold   = 0x30,
00104         RevID              = 0xFE,
00105         PartID             = 0xFF
00106     };
00107     
00108     ///MAX30101 Operational Modes
00109     enum OpModes_e
00110     {
00111         HeartRateMode = 2,
00112         SpO2Mode      = 3,
00113         MultiLedMode  = 7
00114     };
00115     
00116     ///Number of LED channels used
00117     enum LedChannels_e
00118     {
00119         OneLedChannel    = 1,
00120         TwoLedChannels   = 2,
00121         ThreeLedChannels = 3
00122     };
00123     
00124     ///Number of samples averaged per FIFO sample, set in FIFO config
00125     enum NumSamplesAveraged_e
00126     {
00127         AveragedSamples_0  = 0,
00128         AveragedSamples_2  = 1,
00129         AveragedSamples_4  = 2,
00130         AveragedSamples_8  = 3,
00131         AveragedSamples_16 = 4,
00132         AveragedSamples_32 = 5
00133     };
00134     
00135     ///ADC Range, set in SpO2 config
00136     enum ADCRange_e
00137     {
00138         ADC_Range_0 = 0,
00139         ADC_Range_1 = 1,
00140         ADC_Range_2 = 2,
00141         ADC_Range_3 = 3
00142     };
00143     
00144     //LED PulseWidth, set in SpO2 config
00145     enum LEDPulseWidth
00146     {
00147         PW_0 = 0,
00148         PW_1 = 1,
00149         PW_2 = 2,
00150         PW_3 = 3
00151     };
00152         
00153     
00154     ///Sample rate, set in SpO2 config
00155     enum SampleRate_e
00156     {
00157         SR_50_Hz   = 0,
00158         SR_100_Hz  = 1,
00159         SR_200_Hz  = 2,
00160         SR_400_Hz  = 3,
00161         SR_800_Hz  = 4,
00162         SR_1000_Hz = 5,
00163         SR_1600_Hz = 6,
00164         SR_3200_Hz = 7
00165     };
00166         
00167     ///Interrupt Status/Enable BitField
00168     union InterruptBitField_u
00169     {
00170         uint8_t all;
00171         
00172         struct BitField_s 
00173         {
00174             uint8_t pwr_rdy  : 1; ///< Bit0
00175             uint8_t die_temp : 1; ///< Bit1
00176             uint8_t reserved : 2; ///< Bit3:2
00177             uint8_t prox_int : 1; ///< Bit4
00178             uint8_t alc_ovf  : 1; ///< Bit5
00179             uint8_t ppg_rdy  : 1; ///< Bit6
00180             uint8_t a_full   : 1; ///< Bit7
00181         }bits;
00182     };
00183     
00184     ///FIFO Configuration BitField
00185     union FIFO_Configuration_u
00186     {
00187         uint8_t all;
00188         struct BitField_s
00189         {
00190             uint8_t fifo_a_full       : 4;
00191             uint8_t fifo_roll_over_en : 1;
00192             uint8_t sample_average    : 3;
00193         }bits;
00194     };
00195     
00196     ///Mode Configuration BitField
00197     union ModeConfiguration_u
00198     {
00199         uint8_t all;
00200         struct BitField_s
00201         {
00202             uint8_t mode     : 3;
00203             uint8_t reserved : 3;
00204             uint8_t reset    : 1;
00205             uint8_t shdn     : 1;
00206         }bits;
00207     };
00208     
00209     ///SpO2 Configuration BitField
00210     union SpO2Configuration_u
00211     {
00212         uint8_t all;
00213         struct BitField_s
00214         {
00215             uint8_t led_pw         : 2;
00216             uint8_t spo2_sr        : 3;
00217             uint8_t spo2_adc_range : 2;
00218             uint8_t reserved       : 1;
00219         }bits;
00220     };
00221     
00222     ///Multi-LED Mode Control Register BitField
00223     union ModeControlReg_u
00224     {
00225         uint8_t all;
00226         struct BitField_s
00227         {
00228             uint8_t lo_slot     : 3;
00229             uint8_t reserved1 : 1;
00230             uint8_t hi_slot     : 3;
00231             uint8_t reserved2 : 1;
00232         }bits;  
00233     };
00234     
00235     /**
00236     * @brief  Constructor using reference to I2C object
00237     * @param i2c - Reference to I2C object
00238     */
00239     MAX30101(I2C &i2c);
00240 
00241     /** @brief Destructor */
00242     ~MAX30101();
00243     
00244     /**
00245     * @brief Writes appropriate bits to Interrupt Enable 1 and 2.
00246     *
00247     * @param data - Interrupts to enable
00248     * @return 0 on success, non 0 otherwise
00249     */
00250     int32_t enableInterrupts(const InterruptBitField_u data);
00251     
00252     /**
00253     * @brief Reads interrupt status flags from Interrupt Status 1 and 2.
00254     *
00255     * @param[out] data - Contains interrupts status flags on success.
00256     * @return 0 on success, non 0 otherwise
00257     */
00258     int32_t getInterruptStatus(InterruptBitField_u &data);
00259     
00260     /**
00261     * @brief Writes FIFO configuration register with given data
00262     *
00263     * @param config - FIFO Configuration
00264     * @return 0 on success, non 0 otherwise
00265     */
00266     int32_t setFIFOConfiguration(const FIFO_Configuration_u config);
00267     
00268     /**
00269     * @brief Reads FIFO configuration register
00270     *
00271     * @param[out] config - FIFO Configuration on success
00272     * @return 0 on success, non 0 otherwise
00273     */
00274     int32_t getFIFOConfiguration(FIFO_Configuration_u &config);
00275     
00276     /**
00277     * @brief Writes Mode configuration register with given data
00278     *
00279     * @param config - Mode Configuration
00280     * @return 0 on success, non 0 otherwise
00281     */
00282     int32_t setModeConfiguration(const ModeConfiguration_u config);
00283     
00284     /**
00285     * @brief Reads Mode configuration register
00286     *
00287     * @param[out] config - Mode Configuration on success
00288     * @return 0 on success, non 0 otherwise
00289     */
00290     int32_t getModeConfiguration(ModeConfiguration_u &config);
00291     
00292     /**
00293     * @brief Writes SpO2 configuration register with given data
00294     *
00295     * @param config - SpO2 Configuration
00296     * @return 0 on success, non 0 otherwise
00297     */
00298     int32_t setSpO2Configuration(const SpO2Configuration_u config);
00299     
00300     /**
00301     * @brief Reads SpO2 configuration register
00302     *
00303     * @param[out] config - SpO2 Configuration on success
00304     * @return 0 on success, non 0 otherwise
00305     */
00306     int32_t getSpO2Configuration(SpO2Configuration_u &config);
00307     
00308     /**
00309     * @brief Writes LEDx/Prox Pulse Amplitude register with given data
00310     *
00311     * @param reg - LEDx/Prox Pulse Amplitude register to write
00312     * @param amp - LED pulse amplitude
00313     * @return 0 on success, non 0 otherwise
00314     */
00315     int32_t setLEDPulseAmplitude(Registers_e reg, const uint8_t amp);
00316     
00317     /**
00318     * @brief Reads LEDx/Prox Pulse Amplitude register
00319     *
00320     * @param reg - LEDx/Prox Pulse Amplitude register to read
00321     * @param[out] amp - LED pulse amplitude on success
00322     * @return 0 on success, non 0 otherwise
00323     */
00324     int32_t getLEDPulseAmplitude(Registers_e reg, uint8_t &amp);
00325     
00326     /**
00327     * @brief Writes Multi-LED Mode Control Register 
00328     *
00329     * @param reg - Multi-LED Mode Control register 1 or 2
00330     * @param data - Data to write to register
00331     * @return 0 on success, non 0 otherwise
00332     */
00333     int32_t setMultiLEDModeControl(Registers_e reg, const ModeControlReg_u data);
00334     
00335     /**
00336     * @brief Reads Multi-LED Mode Control Register 
00337     *
00338     * @param reg - Multi-LED Mode Control register 1 or 2
00339     * @param[out] data - Data read from register on success
00340     * @return 0 on success, non 0 otherwise
00341     */
00342     int32_t getMultiLEDModeControl(Registers_e reg, ModeControlReg_u &data);
00343     
00344     /**
00345     * @brief Gets raw die temperature, interrupt must be enabled
00346     *
00347     * @param[out] data - Raw die temperature on success
00348     *
00349     * @return 0 on success, non 0 otherwise
00350     */
00351     int32_t getDieTemperature(uint16_t &data);
00352     
00353     /**
00354     * @brief Gets die temperature in celsius, interrupt must be enabled
00355     *
00356     * @param[out] data - Die temperature in celsius on success
00357     *
00358     * @return 0 on success, non 0 otherwise
00359     */
00360     int32_t getDieTemperatureC(float &data);
00361     
00362     /**
00363     * @brief Converts celsius to Fahrenheit
00364     *
00365     * @param c - Temperature in celsius
00366     *
00367     * @return Temperature in Fahrenheit
00368     */
00369     float celsius2fahrenheit(float c); 
00370     
00371     /**
00372     * @brief Writes Proximity Interrupt Threshold Register 
00373     *
00374     * @param data - Data to write to register
00375     * @return 0 on success, non 0 otherwise
00376     */
00377     int32_t setProxIntThreshold(const uint8_t data);
00378     
00379     /**
00380     * @brief Reads Proximity Interrupt Threshold Register 
00381     *
00382     * @param data - Data read on success
00383     * @return 0 on success, non 0 otherwise
00384     */
00385     int32_t getProxIntThreshold(uint8_t &data);
00386     
00387     /** 
00388     * @brief Attempts to read FIFO
00389     *
00390     * @param numLeds - Number of LED channels used; 0 < numLeds < 4
00391     * @param data - pointer to buffer for holding read data
00392     * @param[out] readBytes - number of bytes read from fifo
00393     *
00394     * @return 0 on success, non 0 otherwise
00395     */
00396     int32_t readFIFO(LedChannels_e numLeds, uint8_t *data, uint16_t &readBytes);
00397 
00398 protected:
00399 
00400     /** 
00401     * @brief Write register of device
00402     * @param reg - Register address
00403     * @param value - Value to write
00404     * @return 0 on success, non-zero on failure
00405     */
00406     int32_t writeRegister(Registers_e reg, uint8_t value);
00407 
00408     /**
00409     * @brief  Read register of device
00410     * @param reg - Register address
00411     * @param[out] value - Read data on success
00412     * @return 0 on success, non-zero on failure
00413     */
00414     int32_t readRegister(Registers_e reg, uint8_t &value);
00415 
00416 private:
00417 
00418     I2C & m_i2cBus;
00419     uint8_t m_fifoReadPtr, m_fifoWritePtr, m_fifoNumBytes;
00420     
00421 
00422 };
00423 
00424 #endif /* __MAX30101_H_ */
00425