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