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 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       0x00
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;
00080  //int32_t SpO_Raw;
00081  uint32_t index = 0;
00082  DigitalOut led(LED1); 
00083  
00084  //@brief Creates a packet that will be streamed via USB Serial
00085  //@brief the packet created will be inserted into a fifo to be streamed at a later time
00086  //@param id Streaming ID
00087  //@param buffer Pointer to a uint32 array that contains the data to include in the packet
00088  //@param number Number of elements in the buffer
00089  //
00090  void StreamPacketUint32_ex(uint32_t id, uint32_t *buffer, uint32_t number) {
00091     int k;
00092     if(id == MAX30001_DATA_ECG)
00093     {
00094         for (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  }
00104  /*void StreamPacketUint32_ex(uint32_t id, uint32_t *buffer, uint32_t number) {
00105     int i;
00106     if(id == MAX30101_OXIMETER_DATA + 2)
00107     {
00108         for (i = 0; i < number; i++) 
00109         {
00110             SpO_Raw = (int32_t)(buffer[i]);
00111         }
00112         usbSerial.printf("%d\r\n", SpO_Raw);
00113     } 
00114  }*/
00115  //******************************************************************************
00116  /*void MAX30101_OnInterrupt(void){
00117     I2CM_Init_Reset(2, 1);
00118  }*/
00119  //*******************************************************************************
00120  int main() {
00121     // hold results for returning functions
00122     int result;
00123     
00124     // initialize HVOUT on the MAX14720 PMIC
00125     result = max14720.init();
00126     if (result == MAX14720_ERROR){
00127         printf("Error initializing MAX14720");
00128     }
00129     max14720.boostEn = MAX14720::BOOST_ENABLED;
00130     max14720.boostSetVoltage(HVOUT_VOLTAGE);
00131     
00132     
00133     // This is the SpO2 mode (IR&Red LED)
00134     /*max30101.SpO2mode_init(FIFO_WATERLEVEL_MARK, SAMPLE_AVG, SAMPLE_RATE,PULSE_WIDTH, RED_LED_CURRENT,IR_LED_CURRENT);
00135         
00136     // MAX30101 initialize interrupt  
00137     max30101.onInterrupt(&MAX30101_OnInterrupt);
00138     max30101.onDataAvailable(&StreamPacketUint32_ex);
00139     max30101_Interrupt.fall(&MAX30101MidIntHandler);*/
00140     
00141     // Interrupt priority    
00142     NVIC_SetPriority(GPIO_P0_IRQn, 5);
00143     NVIC_SetPriority(GPIO_P1_IRQn, 5);
00144     NVIC_SetPriority(GPIO_P2_IRQn, 5);
00145     NVIC_SetPriority(GPIO_P3_IRQn, 5);
00146     NVIC_SetPriority(GPIO_P4_IRQn, 5);
00147     NVIC_SetPriority(GPIO_P5_IRQn, 5);
00148     NVIC_SetPriority(GPIO_P6_IRQn, 5);
00149     // used by the MAX30001
00150     NVIC_SetPriority(SPI1_IRQn, 0);
00151     
00152     /* ECG Initialize */
00153     max30001_InterruptB.disable_irq();
00154     max30001_Interrupt2B.disable_irq();
00155     max30001_InterruptB.mode(PullUp);
00156     max30001_InterruptB.fall(&MAX30001Mid_IntB_Handler);
00157     max30001_Interrupt2B.mode(PullUp);
00158     max30001_Interrupt2B.fall(&MAX30001Mid_Int2B_Handler);
00159     max30001_InterruptB.enable_irq();
00160     max30001_Interrupt2B.enable_irq();
00161     MAX30001_AllowInterrupts(1);    
00162     // Configuring the FCLK for the ECG, set to 32.768KHZ
00163     pwmout.period_us(31);
00164     pwmout.write(0.5);          // 0-1 is 0-100%, 0.5 = 50% duty cycle.
00165     //max30001.max30001_RtoR_InitStart(En_rtor, Wndw, Gain1, Pavg, Ptsf, Hoff, Ravg, Rhsf, Clr_rrint);
00166     max30001.max30001_sw_rst(); // Do a software reset of the MAX30001
00167     max30001.max30001_INT_assignment(
00168         MAX30001::MAX30001_INT_B,  // en_enint_loc
00169         MAX30001::MAX30001_NO_INT, // en_eovf_loc
00170         MAX30001::MAX30001_NO_INT, // en_fstint_loc
00171 
00172         MAX30001::MAX30001_INT_2B, // en_dcloffint_loc
00173         MAX30001::MAX30001_INT_B, // en_bint_loc
00174         MAX30001::MAX30001_NO_INT, // en_bovf_loc
00175 
00176         MAX30001::MAX30001_INT_2B, // en_bover_loc
00177         MAX30001::MAX30001_INT_2B, // en_bundr_loc
00178         MAX30001::MAX30001_NO_INT, // en_bcgmon_loc
00179 
00180         MAX30001::MAX30001_INT_B,  // en_pint_loc
00181         MAX30001::MAX30001_NO_INT, // en_povf_loc,
00182         MAX30001::MAX30001_NO_INT, // en_pedge_loc
00183 
00184         MAX30001::MAX30001_INT_2B, // en_lonint_loc
00185         MAX30001::MAX30001_INT_B,  // en_rrint_loc
00186         MAX30001::MAX30001_NO_INT, //  en_samp_loc
00187 
00188         MAX30001::MAX30001_INT_ODNR,  // intb_Type
00189         MAX30001::MAX30001_INT_ODNR); // int2b_Type
00190         
00191     // MAX30001 initialize interrupt
00192     max30001.max30001_ECG_InitStart(En_ecg, Openp, Openn, Pol, Calp_sel, Caln_sel, E_fit, Rate, Gain, Dhpf, Dlpf);
00193     //max30001.max30001_Enable_DcLeadOFF_Init(En_dcloff, Ipol, Imag, Vth);
00194     //max30001.max30001_FIFO_LeadONOff_Read();
00195     max30001.max30001_synch();
00196     max30001.onDataAvailable(&StreamPacketUint32_ex);
00197     int a;
00198     while (1) 
00199     {
00200         a = usbSerial._getc();
00201         if (a == 65)
00202         {
00203             max30001.max30001_Stop_ECG();
00204             index = 0;
00205         }
00206         if (a == 97)
00207         {
00208             //max30001.max30001_sw_rst(); 
00209             max30001.max30001_ECG_InitStart(En_ecg, Openp, Openn, Pol, Calp_sel, Caln_sel, E_fit, Rate, Gain, Dhpf, Dlpf);
00210             //max30001.max30001_synch();
00211             //max30001.onDataAvailable(&StreamPacketUint32_ex);            
00212         }
00213     }
00214 }
00215