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