MAXREFDES117# Code Documentation  V01.00
Heart Rate / SpO2 Monitor
 All Files Functions Variables Macros Pages
MAX30102.cpp
Go to the documentation of this file.
1 
27 /*******************************************************************************
28 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
29 *
30 * Permission is hereby granted, free of charge, to any person obtaining a
31 * copy of this software and associated documentation files (the "Software"),
32 * to deal in the Software without restriction, including without limitation
33 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
34 * and/or sell copies of the Software, and to permit persons to whom the
35 * Software is furnished to do so, subject to the following conditions:
36 *
37 * The above copyright notice and this permission notice shall be included
38 * in all copies or substantial portions of the Software.
39 *
40 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
41 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
42 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
43 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
44 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
45 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
46 * OTHER DEALINGS IN THE SOFTWARE.
47 *
48 * Except as contained in this notice, the name of Maxim Integrated
49 * Products, Inc. shall not be used except as stated in the Maxim Integrated
50 * Products, Inc. Branding Policy.
51 *
52 * The mere transfer of this software does not imply any licenses
53 * of trade secrets, proprietary technology, copyrights, patents,
54 * trademarks, maskwork rights, or any other form of intellectual
55 * property whatsoever. Maxim Integrated Products, Inc. retains all
56 * ownership rights.
57 *******************************************************************************
58 */
59 #include "mbed.h"
60 #include "MAX30102.h"
61 
62 #ifdef TARGET_MAX32600MBED
63 I2C i2c(I2C1_SDA, I2C1_SCL);
64 #else
65 I2C i2c(I2C_SDA, I2C_SCL);
66 #endif
67 
68 bool maxim_max30102_write_reg(uint8_t uch_addr, uint8_t uch_data)
79 {
80  char ach_i2c_data[2];
81  ach_i2c_data[0]=uch_addr;
82  ach_i2c_data[1]=uch_data;
83 
84  if(i2c.write(I2C_WRITE_ADDR, ach_i2c_data, 2, false)==0)
85  return true;
86  else
87  return false;
88 }
89 
90 bool maxim_max30102_read_reg(uint8_t uch_addr, uint8_t *puch_data)
101 {
102  char ch_i2c_data;
103  ch_i2c_data=uch_addr;
104  if(i2c.write(I2C_WRITE_ADDR, &ch_i2c_data, 1, true)!=0)
105  return false;
106  if(i2c.read(I2C_READ_ADDR, &ch_i2c_data, 1, false)==0)
107  {
108  *puch_data=(uint8_t) ch_i2c_data;
109  return true;
110  }
111  else
112  return false;
113 }
114 
125 {
126  if(!maxim_max30102_write_reg(REG_INTR_ENABLE_1,0xc0)) // INTR setting
127  return false;
129  return false;
130  if(!maxim_max30102_write_reg(REG_FIFO_WR_PTR,0x00)) //FIFO_WR_PTR[4:0]
131  return false;
132  if(!maxim_max30102_write_reg(REG_OVF_COUNTER,0x00)) //OVF_COUNTER[4:0]
133  return false;
134  if(!maxim_max30102_write_reg(REG_FIFO_RD_PTR,0x00)) //FIFO_RD_PTR[4:0]
135  return false;
136  if(!maxim_max30102_write_reg(REG_FIFO_CONFIG,0x0f)) //sample avg = 1, fifo rollover=false, fifo almost full = 17
137  return false;
138  if(!maxim_max30102_write_reg(REG_MODE_CONFIG,0x03)) //0x02 for Red only, 0x03 for SpO2 mode 0x07 multimode LED
139  return false;
140  if(!maxim_max30102_write_reg(REG_SPO2_CONFIG,0x27)) // SPO2_ADC range = 4096nA, SPO2 sample rate (100 Hz), LED pulseWidth (400uS)
141  return false;
142 
143  if(!maxim_max30102_write_reg(REG_LED1_PA,0x24)) //Choose value for ~ 7mA for LED1
144  return false;
145  if(!maxim_max30102_write_reg(REG_LED2_PA,0x24)) // Choose value for ~ 7mA for LED2
146  return false;
147  if(!maxim_max30102_write_reg(REG_PILOT_PA,0x7f)) // Choose value for ~ 25mA for Pilot LED
148  return false;
149  return true;
150 }
151 
152 bool maxim_max30102_read_fifo(uint32_t *pun_red_led, uint32_t *pun_ir_led)
163 {
164  uint32_t un_temp;
165  unsigned char uch_temp;
166  *pun_red_led=0;
167  *pun_ir_led=0;
168  char ach_i2c_data[6];
169 
170  //read and clear status register
173 
174  ach_i2c_data[0]=REG_FIFO_DATA;
175  if(i2c.write(I2C_WRITE_ADDR, ach_i2c_data, 1, true)!=0)
176  return false;
177  if(i2c.read(I2C_READ_ADDR, ach_i2c_data, 6, false)!=0)
178  {
179  return false;
180  }
181  un_temp=(unsigned char) ach_i2c_data[0];
182  un_temp<<=16;
183  *pun_red_led+=un_temp;
184  un_temp=(unsigned char) ach_i2c_data[1];
185  un_temp<<=8;
186  *pun_red_led+=un_temp;
187  un_temp=(unsigned char) ach_i2c_data[2];
188  *pun_red_led+=un_temp;
189 
190  un_temp=(unsigned char) ach_i2c_data[3];
191  un_temp<<=16;
192  *pun_ir_led+=un_temp;
193  un_temp=(unsigned char) ach_i2c_data[4];
194  un_temp<<=8;
195  *pun_ir_led+=un_temp;
196  un_temp=(unsigned char) ach_i2c_data[5];
197  *pun_ir_led+=un_temp;
198  *pun_red_led&=0x03FFFF; //Mask MSB [23:18]
199  *pun_ir_led&=0x03FFFF; //Mask MSB [23:18]
200 
201 
202  return true;
203 }
204 
215 {
217  return false;
218  else
219  return true;
220 }