This program for MAX32620HSP board's ECG to recieve raw data on serial. You can delete everything in main() everything happens on StreamPacketUint32_ex

Dependencies:   mbed MAX14720 USBDevice

Dependents:   HSP_ECG_LeadOFF_Detection

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX30101.h Source File

MAX30101.h

00001 /*******************************************************************************
00002 / * Copyright (C) 2015 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  * max30101.h
00034  *
00035  *  Created on: Aug 26, 2015
00036  *      Author: faisal.tariq
00037  */
00038 
00039 #ifndef _MAX30101_H_
00040 #define _MAX30101_H_
00041 
00042 #include "mbed.h"
00043 
00044 #define MAX30101_RAW_DATA_SIZE 3 * 4 * 32
00045 #define MAX30101_PROC_DATA_SIZE 4 * 32
00046 
00047 #define MAX30101_OXIMETER_DATA 0x10
00048 
00049 #define CHUNK_SIZE 252
00050 
00051 // MAX30101 Register addresses
00052 
00053 #define MAX30101_INT_PORT 4
00054 #define MAX30101_INT_PIN 0
00055 #define MAX30101_MASTER_NUM 2
00056 
00057 /**
00058 * Maxim Integrated MAX30101 Oximeter chip
00059 */
00060 class MAX30101 {
00061 public:
00062   float max30101_final_temp;                         // Global declaration
00063   uint32_t max30101_buffer[MAX30101_PROC_DATA_SIZE]; // final Processed data
00064   char max30101_rawData[MAX30101_RAW_DATA_SIZE];     //  raw data from the chip
00065 
00066   typedef enum { // MAX30101 Register addresses
00067 
00068     /*Status */
00069     REG_INT_STAT_1 = 0x00,
00070     REG_INT_STAT_2 = 0x01,
00071     REG_INT_EN_1   = 0x02,
00072     REG_INT_EN_2   = 0x03,
00073 
00074     REG_FIFO_W_PTR   = 0x04,
00075     REG_FIFO_OVF_CNT = 0x05,
00076     REG_FIFO_R_PTR   = 0x06,
00077     REG_FIFO_DATA    = 0x07,
00078     /* Configuration */
00079     REG_FIFO_CFG  = 0x08,
00080     REG_MODE_CFG  = 0x09,
00081     REG_SPO2_CFG  = 0x0A,
00082     REG_LED1_PA   = 0x0C,
00083     REG_LED2_PA   = 0x0D,
00084     REG_LED3_PA   = 0x0E,
00085     REG_PILOT_PA  = 0x10,
00086     REG_SLT2_SLT1 = 0x11,
00087     REG_SLT4_SLT3 = 0x12,
00088     /* Die Temp    */
00089     REG_TINT    = 0x1F,
00090     REG_TFRAC   = 0x20,
00091     REG_TEMP_EN = 0x21,
00092     /* Proximity Func */
00093     REG_PROX_INT_THR = 0x30,
00094     /* Part ID        */
00095     REG_REV_ID = 0xFE,
00096     REG_ID     = 0xFF,
00097   } MAX30101_REG_map_t;
00098 
00099   /**********/
00100   /* STATUS */
00101   /**********/
00102   /// @brief STATUS1 (0x00)
00103   union max30101_Interrupt_Status_1_reg {
00104     char all;
00105     struct {
00106       char pwr_rdy  : 1;
00107       char reserved : 3;
00108       char prox_int : 1;
00109       char alc_ovf  : 1;
00110       char ppg_rdy  : 1;
00111       char a_full   : 1;
00112     } bit;
00113   } max30101_Interrupt_Status_1;
00114 
00115   /// @brief STATUS2 (0x01)
00116   union max30101_Interrupt_Status_2_reg {
00117     char all;
00118     struct {
00119       char reserved1    : 1;
00120       char die_temp_rdy : 1;
00121       char reserved2    : 6;
00122     } bit;
00123   } max30101_Interrupt_Status_2;
00124 
00125   /// @brief INTERRUPT_ENABLE1 (0x02)
00126   volatile union max30101_Interrupt_Enable_1_reg {
00127     uint8_t all;
00128     struct {
00129       uint8_t reserved1   : 4;
00130       uint8_t prox_int_en : 1;
00131       uint8_t alc_ovf_en  : 1;
00132       uint8_t ppg_rdy_en  : 1;
00133       uint8_t a_full_en   : 1;
00134     } bit;
00135   } max30101_Interrupt_Enable_1;
00136 
00137   /// @brief INTERRUPT_ENABLE2 (0x03)
00138   volatile union max30101_Interrupt_Enable_2_reg {
00139     uint8_t all;
00140     struct {
00141       uint8_t reserved1       : 1;
00142       uint8_t die_temp_rdy_en : 1;
00143       uint8_t reserved2       : 6;
00144     } bit;
00145   } max30101_Interrupt_Enable_2;
00146 
00147   /*********/
00148   /* FIFO  */
00149   /*********/
00150   // 0x04
00151   /// @brief FIFO_WR_PTR (0x04)
00152   volatile union max30101_fifo_wr_ptr_reg {
00153     uint8_t all;
00154     struct {
00155       uint8_t fifo_wr_ptr : 5;
00156       uint8_t reserved1   : 3;
00157     } bit;
00158   } max30101_fifo_wr_ptr;
00159 
00160   /// @brief OVF_COUNTER (0x05)
00161   volatile union max30101_ovf_counter_reg {
00162     uint8_t all;
00163     struct {
00164       uint8_t fifo_ovf_counter : 5;
00165       uint8_t reserved1        : 3;
00166     } bit;
00167   } max30101_ovf_counter_reg;
00168 
00169   /// @brief FIFO_READ_PTR (0x06)
00170   volatile union max30101_fifo_rd_ptr_reg {
00171     uint8_t all;
00172     struct {
00173       uint8_t fifo_rd_ptr : 5;
00174       uint8_t reserved1   : 3;
00175     } bit;
00176   } max30101_fifo_rd_ptr;
00177 
00178   // 0x07
00179   uint8_t max30101_fifo_data;
00180 
00181   /********************/
00182   /* Configuration    */
00183   /********************/
00184   // 0x08
00185   /// @brief FIFO_CONFIGURATION (0x08)
00186   volatile union max30101_fifo_configuration_reg {
00187     uint8_t all;
00188     struct {
00189       uint8_t fifo_a_full       : 4;
00190       uint8_t fifo_roll_over_en : 1;
00191       uint8_t smp_ave           : 3;
00192     } bit;
00193   } max30101_fifo_configuration;
00194 
00195   /// @brief MODE_CONFIGURATION (0x09)
00196   volatile union max30101_mode_configuration_reg {
00197     uint8_t all;
00198     struct {
00199       uint8_t mode      : 3;
00200       uint8_t reserved1 : 3;
00201       uint8_t reset     : 1;
00202       uint8_t shdn      : 1;
00203     } bit;
00204   } max30101_mode_configuration;
00205 
00206   /// @brief SPO2_CONGIGURATION (0x0A)
00207   volatile union max30101_spo2_configuration_reg {
00208     uint8_t all;
00209     struct {
00210       uint8_t led_pw       : 2;
00211       uint8_t spo2_sr      : 3;
00212       uint8_t spo2_adc_rge : 2;
00213       uint8_t reserved1    : 1;
00214     } bit;
00215   } max30101_spo2_configuration;
00216 
00217   /// @brief LED1_PA (0x0C)
00218   uint8_t max30101_led1_pa;
00219 
00220   /// @brief LED2_PA (0x0D)
00221   uint8_t max30101_led2_pa;
00222 
00223   /// @brief LED3_PA (0x0E)
00224   uint8_t max30101_led3_pa;
00225 
00226   /// @brief PILOT_PA (0x10)
00227   uint8_t max30101_pilot_pa;
00228 
00229   volatile union max30101_multiLED_mode_ctrl_1_reg {
00230     uint8_t all;
00231     struct {
00232       uint8_t slot1     : 3;
00233       uint8_t reserved  : 1;
00234       uint8_t slot2     : 3;
00235       uint8_t reserved1 : 1;
00236     } bit;
00237   } max30101_multiLED_mode_ctrl_1;
00238 
00239   volatile union max30101_multiLED_mode_ctrl_2_reg {
00240     uint8_t all;
00241     struct {
00242       uint8_t slot3     : 3;
00243       uint8_t reserved  : 1;
00244       uint8_t slot4     : 3;
00245       uint8_t reserved1 : 1;
00246     } bit;
00247   } max30101_multiLED_mode_ctrl_2;
00248 
00249   /********************/
00250   /* Die Temperature  */
00251   /********************/
00252 
00253   uint8_t max30101_tinit;
00254 
00255   uint8_t max30101_tfrac;
00256 
00257   volatile union max30101_die_temp_config {
00258     uint8_t all;
00259     struct {
00260       uint8_t temp_en  : 1;
00261       uint8_t reserved : 7;
00262     } bit;
00263   } max30101_die_temp_config;
00264   /*******************************/
00265   /***** Function Prototypes *****/
00266   /*******************************/
00267 
00268   uint8_t max30101_prox_int_thresh;
00269 
00270   /**
00271   * MAX30101 constructor.
00272   *
00273   * @param sda mbed pin to use for SDA line of I2C interface.
00274   * @param scl mbed pin to use for SCL line of I2C interface.
00275   */
00276   MAX30101(PinName sda, PinName scl, int slaveAddress);
00277 
00278   /**
00279   *  MAX30101 constructor.
00280   *
00281   * @param i2c I2C object to use.
00282   */
00283   MAX30101(I2C *i2c, int slaveAddress);
00284 
00285   /**
00286   * MAX30101 destructor.
00287   */
00288   ~MAX30101(void);
00289 
00290   /**
00291        * @brief Allows reading from MAX30101 register
00292    * @param reg: is the register address, to read from (look at max30101.h and the
00293    *             data sheet for details)
00294    * @param value: is the pointer to the value read from the register
00295    * @returns  0-if no error.  A non-zero value indicates an error.
00296    */
00297   int i2c_reg_read(MAX30101_REG_map_t reg, char *value);
00298 
00299   /**
00300    * @brief Allows writing to MAX30101 register
00301    * @param reg: is the register address, to read from (look at max30101.h and
00302    * the
00303    *        data sheet for details)
00304    * @param value: is the value to write to the register
00305    * @returns  0-if if no error.  A non-zero value indicates an error.
00306    */
00307   int i2c_reg_write(MAX30101_REG_map_t reg, char value);
00308 
00309   /**
00310    * @brief This function sets up for the SpO2 mode.  The data is returned in
00311    * the callback function
00312    * @brief max30101_int_handler in global array: buffer[].  SP mode handles two LED (Red,IR) data.  Hence it
00313    * @brief can fill up the FIFO up to a maximum of 3bytes/sample x 32 x 2 = 192bytes.
00314    * @param fifo_waterlevel_mark: corresponds to FIFO_A_FULL, In FIFO Configuration Register (0x08)
00315    * @param sample_avg: corresponds to SMP_AVE, in FIFO Configuration Register (0x08)
00316    * @param sample_rate: corresponds to SPO2_SR, IN SpO2 Configuration Register (0x0A)
00317    * @param pulse_width: corresponds to LED_PW in SpO2 Configuration register(0x0A)
00318    * @param red_led_current: corresponds to LED1_PA register (0x0C).  Please see data sheet for values
00319    * @param ir_led_current: corresponds to LED2_PA register (0x0D).  Please see data sheet for values
00320    * @returns 0-if everything is good.  A non-zero value indicates an error.
00321    */
00322   int SpO2mode_init(uint8_t fifo_waterlevel_mark, uint8_t sample_avg,
00323                     uint8_t sample_rate, uint8_t pulse_width,
00324                     uint8_t red_led_current, uint8_t ir_led_current);
00325 
00326   /**
00327    * @brief This function will stop the SpO2 mode and turn off all operating LED�s.
00328    * @return  0-if if no error.  A non-zero value indicates an error.
00329    */
00330   int SpO2mode_stop(void);
00331 
00332   /**
00333    * @brief This function sets up for the HR mode.  The data is returned in thecallback function
00334    * @brief max30101_int_handler in global array: buffer[].HR mode handles one LED (Red) data.  Hence it can fill
00335    * @brief up the FIFO up to a maximum of 3bytes/sample x 32 = 96bytes.
00336    * @brief fifo_waterlevel_mark: corresponds to FIFO_A_FULL, In FIFO Configuration Register (0x08)
00337    * @param sample_avg: corresponds to SMP_AVE, in FIFO Configuration Register (0x08)
00338    * @param sample_rate:corresponds to SPO2_SR, IN SpO2 Configuration Register (0x0A)
00339    * @param pulse_width: corresponds to LED_PW in SpO2 Configuration Register(0x0A)
00340    * @param red_led_current: corresponds to LED1_PA register (0x0C).  Please see data sheet for values
00341    * @returns  0-if if no error.  A non-zero value indicates an error.
00342    */
00343   int HRmode_init(uint8_t fifo_waterlevel_mark, uint8_t sample_avg,
00344                   uint8_t sample_rate, uint8_t pulse_width,
00345                   uint8_t red_led_current);
00346 
00347   /**
00348    * @brief This function will stop the HR mode and turn off all operating
00349    * LED’s.
00350    * @return  0-if if no error.  A non-zero value indicates an error.
00351    */
00352   int HRmode_stop(void);
00353 
00354   /**
00355    *@brief This function sets up for the Multi-mode.  The data is returned in the callback function max30101_int_handler in global array:
00356    *@brief buffer[].  Multi-LED mode can handle 1 to 4 LED combinations.  Hence it can fill up the FIFO up to a maximum of
00357    *@brief 3bytes/sample x 32 x 4 = 384bytes.
00358    *@param fifo_waterlevel_mark: corresponds to FIFO_A_FULL, In FIFO Configuration Register (0x08)
00359    *@param sample_avg: corresponds to SMP_AVE, in FIFO Configuration Register (0x08)
00360    *@param sample_rate:corresponds to SPO2_SR, IN SpO2 Configuration Register (0x0A)
00361    *@param pulse_width: corresponds to LED_PW in SpO2 Configuration register(0x0A)
00362    *@param red_led_current: corresponds to LED1_PA register (0x0C).  Please see data sheet for values
00363    *@param ir_led_current: corresponds to LED2_PA register (0x0D).  Please see data sheet for values
00364    *@param green_led_current: corresponds to LED3_PA register (0x0E).  Please see data sheet for values
00365    *@param slot_1,…,slot_4: corresponds to Multi-LED Mode control Registers (0x11-0x12).
00366    *@returns  0-if if no error.  A non-zero value indicates an error.
00367    */
00368   int Multimode_init(uint8_t fifo_waterlevel_mark, uint8_t sample_avg,
00369                      uint8_t sample_rate, uint8_t pulse_width,
00370                      uint8_t red_led_current, uint8_t ir_led_current,
00371                      uint8_t green_led_current, uint8_t slot_1, uint8_t slot_2,
00372                      uint8_t slot_3, uint8_t slot_4);
00373 
00374   /**
00375    * @brief This function will stop the Multi-mode and turn off all operating LED’s.
00376    * @returns  0-if if no error.  A non-zero value indicates an error.
00377    */
00378   int Multimode_stop(void);
00379 
00380   /**
00381    * @brief This is a function that sets up for temperature read and should be called after one of the mode
00382    * @brief has been setup.  The data is returned in the callback function max30101_int_handler.  This
00383    * @brief function needs to be called every time temperature reading is required.
00384    * @brief Call the temp function after one of the MODES have been started
00385    * @brief Note that the temp is disabled after one read... also, it is not necessary
00386    * @brief to read the temp frequently...
00387    * @returns  0-if if no error.  A non-zero value indicates an error.
00388    */
00389   int tempread(void);
00390 
00391   /**
00392    *@brief This is a callback function which collects the data from the FIFO of the MAX30101 in a 32-bit
00393    *@brief unsigned global array called max30101_buffer[].  Upon every interrupt from the MAX30101, this
00394    *@brief function is called to service the FIFO of the MAX30101.  This callback function also services the
00395    *@brief interrupt for the temp data.  The temp data is collected in a floating point global variable
00396    *@brief final_temp.
00397    *@param max30101_buffer[], global uint32_t
00398    *@returns  0-if everything is good.  A non-zero value indicates an error.
00399    */
00400   int int_handler(void);
00401   /**
00402   * @brief type definition for data interrupt
00403   */
00404   typedef void (*DataCallbackFunction)(uint32_t id, uint32_t *buffer,
00405                                        uint32_t length);
00406   /**
00407   * @brief type definition for general interrupt
00408   */
00409   typedef void (*InterruptFunction)();
00410 
00411   /**
00412   * @brief Used to connect a callback for when interrupt data is available
00413   */
00414   void onInterrupt(InterruptFunction _onInterrupt);
00415 
00416   /**
00417   * @brief Used to connect a callback for when interrupt data is available
00418   */
00419   void onDataAvailable(DataCallbackFunction _onDataAvailable);
00420 
00421   /**
00422   * @brief Interrupt callback
00423   */
00424   void MAX30101_OnInterrupt(void);
00425   
00426   static MAX30101 *instance;
00427 
00428 private:
00429   /// called when interrupt data is available
00430   void dataAvailable(uint32_t id, uint32_t *buffer, uint32_t length);
00431   /// callback function at the end of the interrupt
00432   void interruptPostCallback(void);
00433   /// callback function when interrupt data is available
00434   DataCallbackFunction onDataAvailableCallback;
00435   /// callback function when interrupt data is available
00436   InterruptFunction onInterruptCallback;
00437   /// Read I2c wrapper method
00438   int I2CM_Read(int slaveAddress, char *writeData, char writeCount, char *readData, char readCount);
00439   /// Write I2c wrapper method
00440   int I2CM_Write(int slaveAddress, char *writeData1, char writeCount1, char *writeData2, char writeCount2);
00441   /// pointer to I2C object
00442   I2C *i2c;
00443   /// flag to track if this object is the owner (created) the I2C object
00444   bool i2c_owner;
00445   /// Device slave address
00446   int slaveAddress;
00447 };
00448 
00449 /**
00450 *  @brief Resets the I2C block, when needed
00451 */
00452 extern void I2CM_Init_Reset(uint8_t index, int speed);
00453 
00454  /**
00455   * @brief Used for debugging, if needed
00456   */
00457 void MAX30101MidIntHandler(void);
00458 
00459 #endif /* _MAX30101_H_ */