Read data from MAX30102 sensor

Dependents:   sensomed

Committer:
shettypavank
Date:
Sun Dec 18 09:26:45 2016 +0000
Revision:
0:162320d6f658
A remote sensing device to measure standard vitals namely blood pressure, respiration rate, SpO2,?, body temperature and heart rate was developed which could log data to the cloud through a smart phone using the NEXPAQ platform

Who changed what in which revision?

UserRevisionLine numberNew contents of line
shettypavank 0:162320d6f658 1 /** \file max30102.cpp ******************************************************
shettypavank 0:162320d6f658 2 *
shettypavank 0:162320d6f658 3 * Project: MAXREFDES117#
shettypavank 0:162320d6f658 4 * Filename: max30102.cpp
shettypavank 0:162320d6f658 5 * Description: This module is an embedded controller driver for the MAX30102
shettypavank 0:162320d6f658 6 *
shettypavank 0:162320d6f658 7 *
shettypavank 0:162320d6f658 8 * --------------------------------------------------------------------
shettypavank 0:162320d6f658 9 *
shettypavank 0:162320d6f658 10 * This code follows the following naming conventions:
shettypavank 0:162320d6f658 11 *
shettypavank 0:162320d6f658 12 * char ch_pmod_value
shettypavank 0:162320d6f658 13 * char (array) s_pmod_s_string[16]
shettypavank 0:162320d6f658 14 * float f_pmod_value
shettypavank 0:162320d6f658 15 * int32_t n_pmod_value
shettypavank 0:162320d6f658 16 * int32_t (array) an_pmod_value[16]
shettypavank 0:162320d6f658 17 * int16_t w_pmod_value
shettypavank 0:162320d6f658 18 * int16_t (array) aw_pmod_value[16]
shettypavank 0:162320d6f658 19 * uint16_t uw_pmod_value
shettypavank 0:162320d6f658 20 * uint16_t (array) auw_pmod_value[16]
shettypavank 0:162320d6f658 21 * uint8_t uch_pmod_value
shettypavank 0:162320d6f658 22 * uint8_t (array) auch_pmod_buffer[16]
shettypavank 0:162320d6f658 23 * uint32_t un_pmod_value
shettypavank 0:162320d6f658 24 * int32_t * pn_pmod_value
shettypavank 0:162320d6f658 25 *
shettypavank 0:162320d6f658 26 * ------------------------------------------------------------------------- */
shettypavank 0:162320d6f658 27 /*******************************************************************************
shettypavank 0:162320d6f658 28 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
shettypavank 0:162320d6f658 29 *
shettypavank 0:162320d6f658 30 * Permission is hereby granted, free of charge, to any person obtaining a
shettypavank 0:162320d6f658 31 * copy of this software and associated documentation files (the "Software"),
shettypavank 0:162320d6f658 32 * to deal in the Software without restriction, including without limitation
shettypavank 0:162320d6f658 33 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
shettypavank 0:162320d6f658 34 * and/or sell copies of the Software, and to permit persons to whom the
shettypavank 0:162320d6f658 35 * Software is furnished to do so, subject to the following conditions:
shettypavank 0:162320d6f658 36 *
shettypavank 0:162320d6f658 37 * The above copyright notice and this permission notice shall be included
shettypavank 0:162320d6f658 38 * in all copies or substantial portions of the Software.
shettypavank 0:162320d6f658 39 *
shettypavank 0:162320d6f658 40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
shettypavank 0:162320d6f658 41 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
shettypavank 0:162320d6f658 42 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
shettypavank 0:162320d6f658 43 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
shettypavank 0:162320d6f658 44 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
shettypavank 0:162320d6f658 45 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
shettypavank 0:162320d6f658 46 * OTHER DEALINGS IN THE SOFTWARE.
shettypavank 0:162320d6f658 47 *
shettypavank 0:162320d6f658 48 * Except as contained in this notice, the name of Maxim Integrated
shettypavank 0:162320d6f658 49 * Products, Inc. shall not be used except as stated in the Maxim Integrated
shettypavank 0:162320d6f658 50 * Products, Inc. Branding Policy.
shettypavank 0:162320d6f658 51 *
shettypavank 0:162320d6f658 52 * The mere transfer of this software does not imply any licenses
shettypavank 0:162320d6f658 53 * of trade secrets, proprietary technology, copyrights, patents,
shettypavank 0:162320d6f658 54 * trademarks, maskwork rights, or any other form of intellectual
shettypavank 0:162320d6f658 55 * property whatsoever. Maxim Integrated Products, Inc. retains all
shettypavank 0:162320d6f658 56 * ownership rights.
shettypavank 0:162320d6f658 57 *******************************************************************************
shettypavank 0:162320d6f658 58 */
shettypavank 0:162320d6f658 59 #include "mbed.h"
shettypavank 0:162320d6f658 60 #include "MAX30102.h"
shettypavank 0:162320d6f658 61 #define I2C_SDA P1_6
shettypavank 0:162320d6f658 62 #define I2C_SCL P1_7
shettypavank 0:162320d6f658 63
shettypavank 0:162320d6f658 64 I2C i2c(I2C_SDA, I2C_SCL);
shettypavank 0:162320d6f658 65
shettypavank 0:162320d6f658 66
shettypavank 0:162320d6f658 67 bool maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data)
shettypavank 0:162320d6f658 68 /**
shettypavank 0:162320d6f658 69 * \brief Write a value to a MAX30102 register
shettypavank 0:162320d6f658 70 * \par Details
shettypavank 0:162320d6f658 71 * This function writes a value to a MAX30102 register
shettypavank 0:162320d6f658 72 *
shettypavank 0:162320d6f658 73 * \param[in] uch_addr - register address
shettypavank 0:162320d6f658 74 * \param[in] uch_data - register data
shettypavank 0:162320d6f658 75 *
shettypavank 0:162320d6f658 76 * \retval true on success
shettypavank 0:162320d6f658 77 */
shettypavank 0:162320d6f658 78 {
shettypavank 0:162320d6f658 79 char ach_i2c_data[2];
shettypavank 0:162320d6f658 80 ach_i2c_data[0]=uch_addr;
shettypavank 0:162320d6f658 81 ach_i2c_data[1]=uch_data;
shettypavank 0:162320d6f658 82
shettypavank 0:162320d6f658 83 if(i2c.write(I2C_WRITE_ADDR, ach_i2c_data, 2, false)==0)
shettypavank 0:162320d6f658 84 return true;
shettypavank 0:162320d6f658 85 else
shettypavank 0:162320d6f658 86 return false;
shettypavank 0:162320d6f658 87 }
shettypavank 0:162320d6f658 88
shettypavank 0:162320d6f658 89 bool maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data)
shettypavank 0:162320d6f658 90 /**
shettypavank 0:162320d6f658 91 * \brief Read a MAX30102 register
shettypavank 0:162320d6f658 92 * \par Details
shettypavank 0:162320d6f658 93 * This function reads a MAX30102 register
shettypavank 0:162320d6f658 94 *
shettypavank 0:162320d6f658 95 * \param[in] uch_addr - register address
shettypavank 0:162320d6f658 96 * \param[out] puch_data - pointer that stores the register data
shettypavank 0:162320d6f658 97 *
shettypavank 0:162320d6f658 98 * \retval true on success
shettypavank 0:162320d6f658 99 */
shettypavank 0:162320d6f658 100 {
shettypavank 0:162320d6f658 101 char ch_i2c_data;
shettypavank 0:162320d6f658 102 ch_i2c_data=uch_addr;
shettypavank 0:162320d6f658 103 if(i2c.write(I2C_WRITE_ADDR, &ch_i2c_data, 1, true)!=0)
shettypavank 0:162320d6f658 104 return false;
shettypavank 0:162320d6f658 105 if(i2c.read(I2C_READ_ADDR, &ch_i2c_data, 1, false)==0)
shettypavank 0:162320d6f658 106 {
shettypavank 0:162320d6f658 107 *puch_data=(uint8_t) ch_i2c_data;
shettypavank 0:162320d6f658 108 return true;
shettypavank 0:162320d6f658 109 }
shettypavank 0:162320d6f658 110 else
shettypavank 0:162320d6f658 111 return false;
shettypavank 0:162320d6f658 112 }
shettypavank 0:162320d6f658 113
shettypavank 0:162320d6f658 114 bool maxim_max30102_init()
shettypavank 0:162320d6f658 115 /**
shettypavank 0:162320d6f658 116 * \brief Initialize the MAX30102
shettypavank 0:162320d6f658 117 * \par Details
shettypavank 0:162320d6f658 118 * This function initializes the MAX30102
shettypavank 0:162320d6f658 119 *
shettypavank 0:162320d6f658 120 * \param None
shettypavank 0:162320d6f658 121 *
shettypavank 0:162320d6f658 122 * \retval true on success
shettypavank 0:162320d6f658 123 */
shettypavank 0:162320d6f658 124 {
shettypavank 0:162320d6f658 125 if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xc0)) // INTR setting
shettypavank 0:162320d6f658 126 return false;
shettypavank 0:162320d6f658 127 if(!maxim_max30102_write_reg(REG_INTR_ENABLE_2,0x00))
shettypavank 0:162320d6f658 128 return false;
shettypavank 0:162320d6f658 129 if(!maxim_max30102_write_reg(REG_FIFO_WR_PTR,0x00)) //FIFO_WR_PTR[4:0]
shettypavank 0:162320d6f658 130 return false;
shettypavank 0:162320d6f658 131 if(!maxim_max30102_write_reg(REG_OVF_COUNTER,0x00)) //OVF_COUNTER[4:0]
shettypavank 0:162320d6f658 132 return false;
shettypavank 0:162320d6f658 133 if(!maxim_max30102_write_reg(REG_FIFO_RD_PTR,0x00)) //FIFO_RD_PTR[4:0]
shettypavank 0:162320d6f658 134 return false;
shettypavank 0:162320d6f658 135 if(!maxim_max30102_write_reg(REG_FIFO_CONFIG,0x0f)) //sample avg = 1, fifo rollover=false, fifo almost full = 17
shettypavank 0:162320d6f658 136 return false;
shettypavank 0:162320d6f658 137 if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x03)) //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
shettypavank 0:162320d6f658 138 return false;
shettypavank 0:162320d6f658 139 if(!maxim_max30102_write_reg(REG_SPO2_CONFIG,0x27)) // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS)
shettypavank 0:162320d6f658 140 return false;
shettypavank 0:162320d6f658 141
shettypavank 0:162320d6f658 142 if(!maxim_max30102_write_reg(REG_LED1_PA,0x24)) //Choose value for ~ 7mA for LED1
shettypavank 0:162320d6f658 143 return false;
shettypavank 0:162320d6f658 144 if(!maxim_max30102_write_reg(REG_LED2_PA,0x24)) // Choose value for ~ 7mA for LED2
shettypavank 0:162320d6f658 145 return false;
shettypavank 0:162320d6f658 146 if(!maxim_max30102_write_reg(REG_PILOT_PA,0x7f)) // Choose value for ~ 25mA for Pilot LED
shettypavank 0:162320d6f658 147 return false;
shettypavank 0:162320d6f658 148 return true;
shettypavank 0:162320d6f658 149 }
shettypavank 0:162320d6f658 150
shettypavank 0:162320d6f658 151 bool maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led)
shettypavank 0:162320d6f658 152 /**
shettypavank 0:162320d6f658 153 * \brief Read a set of samples from the MAX30102 FIFO register
shettypavank 0:162320d6f658 154 * \par Details
shettypavank 0:162320d6f658 155 * This function reads a set of samples from the MAX30102 FIFO register
shettypavank 0:162320d6f658 156 *
shettypavank 0:162320d6f658 157 * \param[out] *pun_red_led - pointer that stores the red LED reading data
shettypavank 0:162320d6f658 158 * \param[out] *pun_ir_led - pointer that stores the IR LED reading data
shettypavank 0:162320d6f658 159 *
shettypavank 0:162320d6f658 160 * \retval true on success
shettypavank 0:162320d6f658 161 */
shettypavank 0:162320d6f658 162 {
shettypavank 0:162320d6f658 163 uint32_t un_temp;
shettypavank 0:162320d6f658 164 unsigned char uch_temp;
shettypavank 0:162320d6f658 165 *pun_red_led=0;
shettypavank 0:162320d6f658 166 *pun_ir_led=0;
shettypavank 0:162320d6f658 167 char ach_i2c_data[6];
shettypavank 0:162320d6f658 168
shettypavank 0:162320d6f658 169 //read and clear status register
shettypavank 0:162320d6f658 170 maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_temp);
shettypavank 0:162320d6f658 171 maxim_max30102_read_reg(REG_INTR_STATUS_2, &uch_temp);
shettypavank 0:162320d6f658 172
shettypavank 0:162320d6f658 173 ach_i2c_data[0]=REG_FIFO_DATA;
shettypavank 0:162320d6f658 174 if(i2c.write(I2C_WRITE_ADDR, ach_i2c_data, 1, true)!=0)
shettypavank 0:162320d6f658 175 return false;
shettypavank 0:162320d6f658 176 if(i2c.read(I2C_READ_ADDR, ach_i2c_data, 6, false)!=0)
shettypavank 0:162320d6f658 177 {
shettypavank 0:162320d6f658 178 return false;
shettypavank 0:162320d6f658 179 }
shettypavank 0:162320d6f658 180 un_temp=(unsigned char) ach_i2c_data[0];
shettypavank 0:162320d6f658 181 un_temp<<=16;
shettypavank 0:162320d6f658 182 *pun_red_led+=un_temp;
shettypavank 0:162320d6f658 183 un_temp=(unsigned char) ach_i2c_data[1];
shettypavank 0:162320d6f658 184 un_temp<<=8;
shettypavank 0:162320d6f658 185 *pun_red_led+=un_temp;
shettypavank 0:162320d6f658 186 un_temp=(unsigned char) ach_i2c_data[2];
shettypavank 0:162320d6f658 187 *pun_red_led+=un_temp;
shettypavank 0:162320d6f658 188
shettypavank 0:162320d6f658 189 un_temp=(unsigned char) ach_i2c_data[3];
shettypavank 0:162320d6f658 190 un_temp<<=16;
shettypavank 0:162320d6f658 191 *pun_ir_led+=un_temp;
shettypavank 0:162320d6f658 192 un_temp=(unsigned char) ach_i2c_data[4];
shettypavank 0:162320d6f658 193 un_temp<<=8;
shettypavank 0:162320d6f658 194 *pun_ir_led+=un_temp;
shettypavank 0:162320d6f658 195 un_temp=(unsigned char) ach_i2c_data[5];
shettypavank 0:162320d6f658 196 *pun_ir_led+=un_temp;
shettypavank 0:162320d6f658 197 *pun_red_led&=0x03FFFF; //Mask MSB [23:18]
shettypavank 0:162320d6f658 198 *pun_ir_led&=0x03FFFF; //Mask MSB [23:18]
shettypavank 0:162320d6f658 199
shettypavank 0:162320d6f658 200
shettypavank 0:162320d6f658 201 return true;
shettypavank 0:162320d6f658 202 }
shettypavank 0:162320d6f658 203
shettypavank 0:162320d6f658 204 bool maxim_max30102_reset()
shettypavank 0:162320d6f658 205 /**
shettypavank 0:162320d6f658 206 * \brief Reset the MAX30102
shettypavank 0:162320d6f658 207 * \par Details
shettypavank 0:162320d6f658 208 * This function resets the MAX30102
shettypavank 0:162320d6f658 209 *
shettypavank 0:162320d6f658 210 * \param None
shettypavank 0:162320d6f658 211 *
shettypavank 0:162320d6f658 212 * \retval true on success
shettypavank 0:162320d6f658 213 */
shettypavank 0:162320d6f658 214 {
shettypavank 0:162320d6f658 215 if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x40))
shettypavank 0:162320d6f658 216 return false;
shettypavank 0:162320d6f658 217 else
shettypavank 0:162320d6f658 218 return true;
shettypavank 0:162320d6f658 219 }