MAXREFDES#117

Dependencies:   mbed USBDevice

Committer:
MaximGordon
Date:
Thu May 19 22:04:06 2016 +0000
Revision:
4:5273ab1085ab
Parent:
3:7c0fb55eb3ff
Child:
5:e7b8e61b1fe1
improved coding style

Who changed what in which revision?

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