#include "mbed.h"
#include "max32630fthr.h"
#define false 0
#define true 1
MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
Serial pc(USBTX, USBRX);//USB TO SERIAL
DigitalIn INT(P3_2); //pin P3.2 connects to the interrupt output pin of the MAX30102
I2C i2cm1(P3_4, P3_5);
#define I2C_WRITE_ADDR 0xAE
#define I2C_READ_ADDR 0xAF
//register addresses
#define REG_INTR_STATUS_1 0x00
#define REG_INTR_STATUS_2 0x01
#define REG_INTR_ENABLE_1 0x02
#define REG_INTR_ENABLE_2 0x03
#define REG_FIFO_WR_PTR 0x04
#define REG_OVF_COUNTER 0x05
#define REG_FIFO_RD_PTR 0x06
#define REG_FIFO_DATA 0x07
#define REG_FIFO_CONFIG 0x08
#define REG_MODE_CONFIG 0x09
#define REG_SPO2_CONFIG 0x0A
#define REG_LED1_PA 0x0C
#define REG_LED2_PA 0x0D
#define REG_PILOT_PA 0x10
#define REG_MULTI_LED_CTRL1 0x11
#define REG_MULTI_LED_CTRL2 0x12
#define REG_TEMP_INTR 0x1F
#define REG_TEMP_FRAC 0x20
#define REG_TEMP_CONFIG 0x21
#define REG_PROX_INT_THRESH 0x30
#define REG_REV_ID 0xFE
#define REG_PART_ID 0xFF
bool maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data)
/**
* \brief Write a value to a MAX30102 register
* \par Details
* This function writes a value to a MAX30102 register
*
* \param[in] uch_addr - register address
* \param[in] uch_data - register data
*
* \retval true on success
*/
{
char ach_i2c_data[2];
ach_i2c_data[0]=uch_addr;
ach_i2c_data[1]=uch_data;
if(i2cm1.write(I2C_WRITE_ADDR, ach_i2c_data, 2, false)==0)
return true;
else
return false;
}
bool maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data)
/**
* \brief Read a MAX30102 register
* \par Details
* This function reads a MAX30102 register
*
* \param[in] uch_addr - register address
* \param[out] puch_data - pointer that stores the register data
*
* \retval true on success
*/
{
char ch_i2c_data;
ch_i2c_data=uch_addr;
if(i2cm1.write(I2C_WRITE_ADDR, &ch_i2c_data, 1, true)!=0)
return false;
if(i2cm1.read(I2C_READ_ADDR, &ch_i2c_data, 1, false)==0)
{
*puch_data=(uint8_t) ch_i2c_data;
return true;
}
else
return false;
}
bool maxim_max30102_reset()
/**
* \brief Reset the MAX30102
* \par Details
* This function resets the MAX30102
*
* \param None
*
* \retval true on success
*/
{
if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x40))
return false;
else
return true;
}
bool maxim_max30102_init()
/**
* \brief Initialize the MAX30102
* \par Details
* This function initializes the MAX30102
*
* \param None
*
* \retval true on success
*/
{
if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xc0)) // INTR setting
return false;
if(!maxim_max30102_write_reg(REG_INTR_ENABLE_2,0x02))
return false;
if(!maxim_max30102_write_reg(REG_FIFO_WR_PTR,0x00)) //FIFO_WR_PTR[4:0]
return false;
if(!maxim_max30102_write_reg(REG_OVF_COUNTER,0x00)) //OVF_COUNTER[4:0]
return false;
if(!maxim_max30102_write_reg(REG_FIFO_RD_PTR,0x00)) //FIFO_RD_PTR[4:0]
return false;
if(!maxim_max30102_write_reg(REG_FIFO_CONFIG,0x0f)) //sample avg = 1, fifo rollover=false, fifo almost full = 17
return false;
if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x03)) //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
return false;
if(!maxim_max30102_write_reg(REG_SPO2_CONFIG,0x27)) // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS)
return false;
if(!maxim_max30102_write_reg(REG_LED1_PA,0x24)) //Choose value for ~ 7mA for LED1
return false;
if(!maxim_max30102_write_reg(REG_LED2_PA,0x24)) // Choose value for ~ 7mA for LED2
return false;
if(!maxim_max30102_write_reg(REG_PILOT_PA,0x7f)) // Choose value for ~ 25mA for Pilot LED
return false;
if(!maxim_max30102_write_reg(REG_TEMP_CONFIG,0x01)) // En temp
return false;
return true;
}
int main()
{
uint8_t temp_inter,temp_fra;
float temp_value;
maxim_max30102_reset(); //resets the MAX30102
// initialize serial communication at 9600 bits per second:
pc.baud(9600);
pc.format(8,SerialBase::None,1);
pc.printf("ready to go!");
wait(1);
//read and clear status register
maxim_max30102_read_reg(0,&temp_inter);
maxim_max30102_init();
while(1)
{
while(INT.read()==1);
maxim_max30102_read_reg(REG_TEMP_INTR,&temp_inter);
maxim_max30102_read_reg(REG_TEMP_FRAC,&temp_fra);
temp_value=temp_inter+temp_fra*0.0625;
pc.printf("temperature is %6.4f \n\r", temp_value);
}
}