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