MAX32620HSP ECG with lead off detection

Dependencies:   mbed HSP_ECG MAX14720 USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001  #include "mbed.h"
00002  #include "MAX14720.h"
00003  #include "MAX30001.h"
00004  #include "MAX30101.h"
00005  #include "System.h"
00006  #include "USBSerial.h"
00007 
00008  /************************************************************************************************************************************/
00009  /// define the HVOUT Boost Voltage default for the MAX14720 PMIC
00010  #define HVOUT_VOLTAGE 4500 // set to 4500 mV
00011  
00012  /// define all I2C addresses
00013  #define MAX14720_I2C_SLAVE_ADDR (0x54)
00014  #define MAX30101_I2C_SLAVE_ADDR (0xAE)
00015  
00016  // Settings for ECG Initialization
00017  #define En_ecg     0x01
00018  #define Openp      0x00
00019  #define Openn      0x00
00020  #define Pol        0x00
00021  #define Calp_sel   0x00
00022  #define Caln_sel   0x00
00023  #define E_fit      0x0F
00024  #define Rate       0x02
00025  #define Gain       0x00
00026  #define Dhpf       0x01
00027  #define Dlpf       0x01
00028  
00029  // Settings for ECG RtoR
00030  #define En_rtor    0x01
00031  #define Wndw       0x03
00032  #define Gain1      0x0f
00033  #define Pavg       0x02
00034  #define Ptsf       0x03
00035  #define Hoff       0x20
00036  #define Ravg       0x02
00037  #define Rhsf       0x04
00038  #define Clr_rrint  0x01
00039  
00040  // Settings for Lead Detection
00041  #define En_dcloff  0x01
00042  #define Ipol       0x00
00043  #define Imag       0x01
00044  #define Vth        0x00
00045  
00046  // Settings for the HR initialization
00047  #define FIFO_WATERLEVEL_MARK 15
00048  #define SAMPLE_AVG           2
00049  #define SAMPLE_RATE          1
00050  #define PULSE_WIDTH          2
00051  #define RED_LED_CURRENT      0x1F
00052  #define IR_LED_CURRENT       0X1F
00053  
00054  /// Define with Maxim VID and a Maxim assigned PID, set to version 0x0001 and non-blocking
00055  USBSerial usbSerial(0x0b6a, 0x0100, 0x0001, false);
00056  
00057  // I2C Master 2
00058  I2C i2c2(I2C2_SDA, I2C2_SCL); // used by MAX14720, MAX30101, LIS2DH
00059  
00060  // SPI Master 0 with SPI0_SS for use with MAX30001
00061  SPI spi(SPI0_MOSI, SPI0_MISO, SPI0_SCK, SPI0_SS); // used by MAX30001
00062  
00063  // PMIC
00064  MAX14720 max14720(&i2c2, MAX14720_I2C_SLAVE_ADDR);
00065  
00066  // Optical Oximeter
00067  //MAX30101 max30101(&i2c2, MAX30101_I2C_SLAVE_ADDR);
00068  //InterruptIn max30101_Interrupt(P4_0);
00069 
00070  // ECG device
00071  MAX30001 max30001(&spi);
00072  InterruptIn max30001_InterruptB(P3_6);
00073  InterruptIn max30001_Interrupt2B(P4_5);
00074  
00075  /// PWM used as fclk for the MAX30001
00076  PwmOut pwmout(P1_7);
00077   
00078  // Data of sensors
00079  int32_t ECG_Raw, RtoR_data;
00080  uint32_t index = 0;
00081  
00082  //int32_t SpO_Raw;
00083  DigitalOut led(LED1); 
00084  
00085  //@brief Creates a packet that will be streamed via USB Serial
00086  //@brief the packet created will be inserted into a fifo to be streamed at a later time
00087  //@param id Streaming ID
00088  //@param buffer Pointer to a uint32 array that contains the data to include in the packet
00089  //@param number Number of elements in the buffer
00090  //
00091  void StreamPacketUint32_ex(uint32_t id, uint32_t *buffer, uint32_t number) {
00092     if(id == MAX30001_DATA_ECG)
00093     {
00094         for (int k = 0; k < number; k++) 
00095         {
00096             ECG_Raw = (int32_t)(buffer[k] << 8);
00097             ECG_Raw = ECG_Raw >> 14;
00098             usbSerial.printf("%d|" , index);
00099             usbSerial.printf("%d\r\n", ECG_Raw);
00100             index++;
00101         }
00102     }
00103     /*if(id == MAX30001_DATA_RTOR)
00104     {
00105            for(int i = 0; i < number; i++)
00106            {
00107                RtoR_data = (buffer[i]);
00108                //usbSerial.printf("HR: %d\r\n", (60000/(RtoR_data << 3)));
00109            }     
00110     }*/
00111     if(id == MAX30001_DATA_LEADOFF_DC)
00112     {
00113         usbSerial.printf("LOFF\r\n");
00114     }
00115  }
00116  int main() {
00117     // hold results for returning functions
00118     int result;
00119     
00120     // initialize HVOUT on the MAX14720 PMIC
00121     result = max14720.init();
00122     if (result == MAX14720_ERROR){
00123         printf("Error initializing MAX14720");
00124     }
00125     max14720.boostEn = MAX14720::BOOST_ENABLED;
00126     max14720.boostSetVoltage(HVOUT_VOLTAGE);
00127     
00128     // Interrupt priority    
00129     NVIC_SetPriority(GPIO_P0_IRQn, 5);
00130     NVIC_SetPriority(GPIO_P1_IRQn, 5);
00131     NVIC_SetPriority(GPIO_P2_IRQn, 5);
00132     NVIC_SetPriority(GPIO_P3_IRQn, 5);
00133     NVIC_SetPriority(GPIO_P4_IRQn, 5);
00134     NVIC_SetPriority(GPIO_P5_IRQn, 5);
00135     NVIC_SetPriority(GPIO_P6_IRQn, 5);
00136     // used by the MAX30001
00137     NVIC_SetPriority(SPI1_IRQn, 0);
00138     
00139     /* ECG Initialize */
00140     max30001_InterruptB.disable_irq();
00141     max30001_Interrupt2B.disable_irq();
00142     max30001_InterruptB.mode(PullUp);
00143     max30001_InterruptB.fall(&MAX30001Mid_IntB_Handler);
00144     max30001_Interrupt2B.mode(PullUp);
00145     max30001_Interrupt2B.fall(&MAX30001Mid_Int2B_Handler);
00146     max30001_InterruptB.enable_irq();
00147     max30001_Interrupt2B.enable_irq();
00148     MAX30001_AllowInterrupts(1);    
00149     // Configuring the FCLK for the ECG, set to 32.768KHZ
00150     pwmout.period_us(31);
00151     pwmout.write(0.5);          // 0-1 is 0-100%, 0.5 = 50% duty cycle.
00152     max30001.max30001_sw_rst(); // Do a software reset of the MAX30001
00153     max30001.max30001_INT_assignment(
00154         MAX30001::MAX30001_INT_B,  // en_enint_loc
00155         MAX30001::MAX30001_NO_INT, // en_eovf_loc
00156         MAX30001::MAX30001_NO_INT, // en_fstint_loc
00157 
00158         MAX30001::MAX30001_INT_2B, // en_dcloffint_loc
00159         MAX30001::MAX30001_INT_B, // en_bint_loc
00160         MAX30001::MAX30001_NO_INT, // en_bovf_loc
00161 
00162         MAX30001::MAX30001_INT_2B, // en_bover_loc
00163         MAX30001::MAX30001_INT_2B, // en_bundr_loc
00164         MAX30001::MAX30001_NO_INT, // en_bcgmon_loc
00165 
00166         MAX30001::MAX30001_INT_B,  // en_pint_loc
00167         MAX30001::MAX30001_NO_INT, // en_povf_loc,
00168         MAX30001::MAX30001_NO_INT, // en_pedge_loc
00169 
00170         MAX30001::MAX30001_INT_2B, // en_lonint_loc
00171         MAX30001::MAX30001_INT_B,  // en_rrint_loc
00172         MAX30001::MAX30001_NO_INT, //  en_samp_loc
00173 
00174         MAX30001::MAX30001_INT_ODNR,  // intb_Type
00175         MAX30001::MAX30001_INT_ODNR); // int2b_Type
00176         
00177     // MAX30001 initialize interrupt
00178     max30001.max30001_ECG_InitStart(En_ecg, Openp, Openn, Pol, Calp_sel, Caln_sel, E_fit, Rate, Gain, Dhpf, Dlpf);
00179     //max30001.max30001_RtoR_InitStart(En_rtor, Wndw, Gain1, Pavg, Ptsf, Hoff, Ravg, Rhsf, Clr_rrint);
00180     max30001.max30001_Enable_DcLeadOFF_Init(En_dcloff, Ipol, Imag, Vth);
00181     //max30001.max30001_FIFO_LeadONOff_Read();
00182     max30001.max30001_synch();
00183     max30001.onDataAvailable(&StreamPacketUint32_ex);
00184     int a;
00185     while (1) 
00186     {
00187         
00188         a = usbSerial._getc();
00189         if (a == 65)
00190         {
00191             max30001.max30001_Stop_ECG();
00192             max30001.max30001_Disable_DcLeadOFF();
00193             index = 0;
00194         }
00195         if (a == 97)
00196         {
00197             //max30001.max30001_sw_rst(); 
00198             max30001.max30001_ECG_InitStart(En_ecg, Openp, Openn, Pol, Calp_sel, Caln_sel, E_fit, Rate, Gain, Dhpf, Dlpf);
00199             max30001.max30001_Enable_DcLeadOFF_Init(En_dcloff, Ipol, Imag, Vth);
00200             //max30001.max30001_synch();
00201             //max30001.onDataAvailable(&StreamPacketUint32_ex);            
00202         }
00203     }
00204 }
00205