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.
MAX30102.cpp
00001 00002 00003 00004 /** \file max30102.cpp ****************************************************** 00005 * 00006 * Project: MAXREFDES117# 00007 * Filename: max30102.cpp 00008 * Description: This module is an embedded controller driver for the MAX30102 00009 * 00010 * 00011 * -------------------------------------------------------------------- 00012 * 00013 * This code follows the following naming conventions: 00014 * 00015 * char ch_pmod_value 00016 * char (array) s_pmod_s_string[16] 00017 * float f_pmod_value 00018 * int32_t n_pmod_value 00019 * int32_t (array) an_pmod_value[16] 00020 * int16_t w_pmod_value 00021 * int16_t (array) aw_pmod_value[16] 00022 * uint16_t uw_pmod_value 00023 * uint16_t (array) auw_pmod_value[16] 00024 * uint8_t uch_pmod_value 00025 * uint8_t (array) auch_pmod_buffer[16] 00026 * uint32_t un_pmod_value 00027 * int32_t * pn_pmod_value 00028 * 00029 * ------------------------------------------------------------------------- */ 00030 /******************************************************************************* 00031 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved. 00032 * 00033 * Permission is hereby granted, free of charge, to any person obtaining a 00034 * copy of this software and associated documentation files (the "Software"), 00035 * to deal in the Software without restriction, including without limitation 00036 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 00037 * and/or sell copies of the Software, and to permit persons to whom the 00038 * Software is furnished to do so, subject to the following conditions: 00039 * 00040 * The above copyright notice and this permission notice shall be included 00041 * in all copies or substantial portions of the Software. 00042 * 00043 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 00044 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 00045 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 00046 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES 00047 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 00048 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 00049 * OTHER DEALINGS IN THE SOFTWARE. 00050 * 00051 * Except as contained in this notice, the name of Maxim Integrated 00052 * Products, Inc. shall not be used except as stated in the Maxim Integrated 00053 * Products, Inc. Branding Policy. 00054 * 00055 * The mere transfer of this software does not imply any licenses 00056 * of trade secrets, proprietary technology, copyrights, patents, 00057 * trademarks, maskwork rights, or any other form of intellectual 00058 * property whatsoever. Maxim Integrated Products, Inc. retains all 00059 * ownership rights. 00060 ******************************************************************************* 00061 */ 00062 #include "mbed.h" 00063 #include "MAX30102.h" 00064 00065 #ifdef TARGET_MAX32600MBED 00066 I2C i2c(I2C1_SDA, I2C1_SCL); 00067 #else 00068 I2C i2c(I2C_SDA, I2C_SCL); 00069 #endif 00070 00071 bool maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data) 00072 /** 00073 * \brief Write a value to a MAX30102 register 00074 * \par Details 00075 * This function writes a value to a MAX30102 register 00076 * 00077 * \param[in] uch_addr - register address 00078 * \param[in] uch_data - register data 00079 * 00080 * \retval true on success 00081 */ 00082 { 00083 char ach_i2c_data[2]; 00084 ach_i2c_data[0]=uch_addr; 00085 ach_i2c_data[1]=uch_data; 00086 00087 if(i2c.write(I2C_WRITE_ADDR, ach_i2c_data, 2, false)==0) 00088 return true; 00089 else 00090 return false; 00091 } 00092 00093 bool maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data) 00094 /** 00095 * \brief Read a MAX30102 register 00096 * \par Details 00097 * This function reads a MAX30102 register 00098 * 00099 * \param[in] uch_addr - register address 00100 * \param[out] puch_data - pointer that stores the register data 00101 * 00102 * \retval true on success 00103 */ 00104 { 00105 char ch_i2c_data; 00106 ch_i2c_data=uch_addr; 00107 if(i2c.write(I2C_WRITE_ADDR, &ch_i2c_data, 1, true)!=0) 00108 return false; 00109 if(i2c.read(I2C_READ_ADDR, &ch_i2c_data, 1, false)==0) 00110 { 00111 *puch_data=(uint8_t) ch_i2c_data; 00112 return true; 00113 } 00114 else 00115 return false; 00116 } 00117 00118 bool maxim_max30102_init() 00119 /** 00120 * \brief Initialize the MAX30102 00121 * \par Details 00122 * This function initializes the MAX30102 00123 * 00124 * \param None 00125 * 00126 * \retval true on success 00127 */ 00128 { 00129 if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xc0)) // INTR setting 00130 return false; 00131 if(!maxim_max30102_write_reg(REG_INTR_ENABLE_2,0x00)) 00132 return false; 00133 if(!maxim_max30102_write_reg(REG_FIFO_WR_PTR,0x00)) //FIFO_WR_PTR[4:0] 00134 return false; 00135 if(!maxim_max30102_write_reg(REG_OVF_COUNTER,0x00)) //OVF_COUNTER[4:0] 00136 return false; 00137 if(!maxim_max30102_write_reg(REG_FIFO_RD_PTR,0x00)) //FIFO_RD_PTR[4:0] 00138 return false; 00139 if(!maxim_max30102_write_reg(REG_FIFO_CONFIG,0x0f)) //sample avg = 1, fifo rollover=false, fifo almost full = 17 00140 return false; 00141 if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x03)) //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED 00142 return false; 00143 if(!maxim_max30102_write_reg(REG_SPO2_CONFIG,0x27)) // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS) 00144 return false; 00145 00146 if(!maxim_max30102_write_reg(REG_LED1_PA,0x24)) //Choose value for ~ 7mA for LED1 00147 return false; 00148 if(!maxim_max30102_write_reg(REG_LED2_PA,0x24)) // Choose value for ~ 7mA for LED2 00149 return false; 00150 if(!maxim_max30102_write_reg(REG_PILOT_PA,0x7f)) // Choose value for ~ 25mA for Pilot LED 00151 return false; 00152 return true; 00153 } 00154 00155 bool maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led) 00156 /** 00157 * \brief Read a set of samples from the MAX30102 FIFO register 00158 * \par Details 00159 * This function reads a set of samples from the MAX30102 FIFO register 00160 * 00161 * \param[out] *pun_red_led - pointer that stores the red LED reading data 00162 * \param[out] *pun_ir_led - pointer that stores the IR LED reading data 00163 * 00164 * \retval true on success 00165 */ 00166 { 00167 uint32_t un_temp; 00168 unsigned char uch_temp; 00169 *pun_red_led=0; 00170 *pun_ir_led=0; 00171 char ach_i2c_data[6]; 00172 00173 //read and clear status register 00174 maxim_max30102_read_reg(REG_INTR_STATUS_1, &uch_temp); 00175 maxim_max30102_read_reg(REG_INTR_STATUS_2, &uch_temp); 00176 00177 ach_i2c_data[0]=REG_FIFO_DATA; 00178 if(i2c.write(I2C_WRITE_ADDR, ach_i2c_data, 1, true)!=0) 00179 return false; 00180 if(i2c.read(I2C_READ_ADDR, ach_i2c_data, 6, false)!=0) 00181 { 00182 return false; 00183 } 00184 un_temp=(unsigned char) ach_i2c_data[0]; 00185 un_temp<<=16; 00186 *pun_red_led+=un_temp; 00187 un_temp=(unsigned char) ach_i2c_data[1]; 00188 un_temp<<=8; 00189 *pun_red_led+=un_temp; 00190 un_temp=(unsigned char) ach_i2c_data[2]; 00191 *pun_red_led+=un_temp; 00192 00193 un_temp=(unsigned char) ach_i2c_data[3]; 00194 un_temp<<=16; 00195 *pun_ir_led+=un_temp; 00196 un_temp=(unsigned char) ach_i2c_data[4]; 00197 un_temp<<=8; 00198 *pun_ir_led+=un_temp; 00199 un_temp=(unsigned char) ach_i2c_data[5]; 00200 *pun_ir_led+=un_temp; 00201 *pun_red_led&=0x03FFFF; //Mask MSB [23:18] 00202 *pun_ir_led&=0x03FFFF; //Mask MSB [23:18] 00203 00204 00205 return true; 00206 } 00207 00208 bool maxim_max30102_reset() 00209 /** 00210 * \brief Reset the MAX30102 00211 * \par Details 00212 * This function resets the MAX30102 00213 * 00214 * \param None 00215 * 00216 * \retval true on success 00217 */ 00218 { 00219 if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x40)) 00220 return false; 00221 else 00222 return true; 00223 } 00224 00225 00226 00227 00228 00229 00230 00231 00232 00233 00234 00235 00236 00237 00238 00239 00240 00241 00242 00243 00244 00245 uint8_t MAX30102_begin(uint8_t reg) 00246 { 00247 char data; 00248 char temp[1] = {reg}; 00249 i2c.write(I2C_WRITE_ADDR, temp , 1); 00250 temp[1] = 0x00; 00251 i2c.write(I2C_WRITE_ADDR, temp, 1); 00252 int a = i2c.read(I2C_WRITE_ADDR, &data, 1); 00253 return data; 00254 00255 } 00256 00257 int MAX30102_writeRegValue(uint8_t reg, char value) 00258 { 00259 char cmdData[2] = { (char)reg, value }; 00260 00261 if (i2c.write(I2C_WRITE_ADDR, cmdData, sizeof(cmdData)) != 0) { 00262 return MAX30102_ERROR; 00263 } 00264 00265 return MAX30102_NO_ERROR; 00266 }
Generated on Fri Jul 29 2022 02:39:04 by
1.7.2