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