MAXREFDES117# Code Documentation  V01.00
Heart Rate / SpO2 Monitor
 All Data Structures Files Functions Variables Typedefs Macros Pages
max30102.cpp
Go to the documentation of this file.
1 
30 /*******************************************************************************
31 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
32 *
33 * Permission is hereby granted, free of charge, to any person obtaining a
34 * copy of this software and associated documentation files (the "Software"),
35 * to deal in the Software without restriction, including without limitation
36 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
37 * and/or sell copies of the Software, and to permit persons to whom the
38 * Software is furnished to do so, subject to the following conditions:
39 *
40 * The above copyright notice and this permission notice shall be included
41 * in all copies or substantial portions of the Software.
42 *
43 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
44 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
45 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
46 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
47 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
48 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
49 * OTHER DEALINGS IN THE SOFTWARE.
50 *
51 * Except as contained in this notice, the name of Maxim Integrated
52 * Products, Inc. shall not be used except as stated in the Maxim Integrated
53 * Products, Inc. Branding Policy.
54 *
55 * The mere transfer of this software does not imply any licenses
56 * of trade secrets, proprietary technology, copyrights, patents,
57 * trademarks, maskwork rights, or any other form of intellectual
58 * property whatsoever. Maxim Integrated Products, Inc. retains all
59 * ownership rights.
60 *******************************************************************************
61 */
62 #include "max30102.h"
63 #include "SoftI2CMaster.h"
64 #include "algorithm.h"
65 
66 bool maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data)
77 {
79  return false;
80  if(!i2c_write(uch_addr))
81  return false;
82  if(!i2c_write(uch_data))
83  return false;
84  i2c_stop();
85  return true;
86 }
87 
88 bool maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data)
99 {
101  return false;
102  if(!i2c_write(uch_addr))
103  return false;
105  return false;
106  *puch_data=i2c_read(true);
107  i2c_stop();
108  return true;
109 }
110 
121 {
122  i2c_init();
123  if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xc0)) // INTR setting
124  return false;
126  return false;
127  if(!maxim_max30102_write_reg(REG_FIFO_WR_PTR,0x00)) //FIFO_WR_PTR[4:0]
128  return false;
129  if(!maxim_max30102_write_reg(REG_OVF_COUNTER,0x00)) //OVF_COUNTER[4:0]
130  return false;
131  if(!maxim_max30102_write_reg(REG_FIFO_RD_PTR,0x00)) //FIFO_RD_PTR[4:0]
132  return false;
133  if(!maxim_max30102_write_reg(REG_FIFO_CONFIG,0x4f)) //sample avg = 4, fifo rollover=false, fifo almost full = 17
134  return false;
135  if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x03)) //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
136  return false;
137  if(!maxim_max30102_write_reg(REG_SPO2_CONFIG,0x27)) // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (411uS)
138  return false;
139 
140  if(!maxim_max30102_write_reg(REG_LED1_PA,0x24)) //Choose value for ~ 7mA for LED1
141  return false;
142  if(!maxim_max30102_write_reg(REG_LED2_PA,0x24)) // Choose value for ~ 7mA for LED2
143  return false;
144  if(!maxim_max30102_write_reg(REG_PILOT_PA,0x7f)) // Choose value for ~ 25mA for Pilot LED
145  return false;
146  return true;
147 }
148 
149 #if defined(ARDUINO_AVR_UNO)
150 //Arduino Uno doesn't have enough SRAM to store 100 samples of IR led data and red led data in 32-bit format
151 //To solve this problem, 16-bit MSB of the sampled data will be truncated. Samples become 16-bit data.
152 bool maxim_max30102_read_fifo(uint16_t *pun_red_led, uint16_t *pun_ir_led)
153 #else
154 bool maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led)
155 #endif
156 
166 {
167  uint32_t un_temp;
168  uint8_t uch_temp;
169  *pun_ir_led=0;
170  *pun_red_led=0;
174  return false;
176  return false;
178  return false;
179  un_temp=i2c_read(false);
180  un_temp<<=16;
181  *pun_red_led+=un_temp;
182  un_temp=i2c_read(false);
183  un_temp<<=8;
184  *pun_red_led+=un_temp;
185  un_temp=i2c_read(false);
186  *pun_red_led+=un_temp;
187 
188  un_temp=i2c_read(false);
189  un_temp<<=16;
190  *pun_ir_led+=un_temp;
191  un_temp=i2c_read(false);
192  un_temp<<=8;
193  *pun_ir_led+=un_temp;
194  un_temp=i2c_read(true);
195  *pun_ir_led+=un_temp;
196  i2c_stop();
197  *pun_red_led&=0x03FFFF; //Mask MSB [23:18]
198  *pun_ir_led&=0x03FFFF; //Mask MSB [23:18]
199  return true;
200 }
201 
212 {
214  return false;
215  else
216  return true;
217 }
218