Library for MAX30101 SpO2 and heart rate sensor

Dependents:   HeartRate HeartRate proj final_project_ee119 ... more

Committer:
j3
Date:
Thu Apr 13 23:23:36 2017 +0000
Revision:
0:2d0c91de9279
Child:
2:0db1f3bec727
init commit, basic outline

Who changed what in which revision?

UserRevisionLine numberNew contents of line
j3 0:2d0c91de9279 1 /*******************************************************************************
j3 0:2d0c91de9279 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
j3 0:2d0c91de9279 3 *
j3 0:2d0c91de9279 4 * Permission is hereby granted, free of charge, to any person obtaining a
j3 0:2d0c91de9279 5 * copy of this software and associated documentation files (the "Software"),
j3 0:2d0c91de9279 6 * to deal in the Software without restriction, including without limitation
j3 0:2d0c91de9279 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
j3 0:2d0c91de9279 8 * and/or sell copies of the Software, and to permit persons to whom the
j3 0:2d0c91de9279 9 * Software is furnished to do so, subject to the following conditions:
j3 0:2d0c91de9279 10 *
j3 0:2d0c91de9279 11 * The above copyright notice and this permission notice shall be included
j3 0:2d0c91de9279 12 * in all copies or substantial portions of the Software.
j3 0:2d0c91de9279 13 *
j3 0:2d0c91de9279 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
j3 0:2d0c91de9279 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
j3 0:2d0c91de9279 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
j3 0:2d0c91de9279 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
j3 0:2d0c91de9279 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
j3 0:2d0c91de9279 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
j3 0:2d0c91de9279 20 * OTHER DEALINGS IN THE SOFTWARE.
j3 0:2d0c91de9279 21 *
j3 0:2d0c91de9279 22 * Except as contained in this notice, the name of Maxim Integrated
j3 0:2d0c91de9279 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
j3 0:2d0c91de9279 24 * Products, Inc. Branding Policy.
j3 0:2d0c91de9279 25 *
j3 0:2d0c91de9279 26 * The mere transfer of this software does not imply any licenses
j3 0:2d0c91de9279 27 * of trade secrets, proprietary technology, copyrights, patents,
j3 0:2d0c91de9279 28 * trademarks, maskwork rights, or any other form of intellectual
j3 0:2d0c91de9279 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
j3 0:2d0c91de9279 30 * ownership rights.
j3 0:2d0c91de9279 31 *******************************************************************************
j3 0:2d0c91de9279 32 */
j3 0:2d0c91de9279 33 #ifndef __MAX30101_H_
j3 0:2d0c91de9279 34 #define __MAX30101_H_
j3 0:2d0c91de9279 35
j3 0:2d0c91de9279 36 #include "mbed.h"
j3 0:2d0c91de9279 37
j3 0:2d0c91de9279 38 /**
j3 0:2d0c91de9279 39 * Driver for the MAX30101
j3 0:2d0c91de9279 40 *
j3 0:2d0c91de9279 41 * @code
j3 0:2d0c91de9279 42 * #include "mbed.h"
j3 0:2d0c91de9279 43 * #include "max32630fthr.h"
j3 0:2d0c91de9279 44 * #include "MAX30101.h"
j3 0:2d0c91de9279 45 *
j3 0:2d0c91de9279 46 * MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
j3 0:2d0c91de9279 47 *
j3 0:2d0c91de9279 48 * //Get I2C instance
j3 0:2d0c91de9279 49 * I2C i2cBus(P3_4, P3_5);
j3 0:2d0c91de9279 50 *
j3 0:2d0c91de9279 51 * //Get temp sensor instance
j3 0:2d0c91de9279 52 * MAX30101 op_sensor(i2cBus); //Constructor takes 7-bit slave adrs
j3 0:2d0c91de9279 53 *
j3 0:2d0c91de9279 54 * int main(void)
j3 0:2d0c91de9279 55 * {
j3 0:2d0c91de9279 56 * //use sensor
j3 0:2d0c91de9279 57 * }
j3 0:2d0c91de9279 58 * @endcode
j3 0:2d0c91de9279 59 */
j3 0:2d0c91de9279 60
j3 0:2d0c91de9279 61 class MAX30101
j3 0:2d0c91de9279 62 {
j3 0:2d0c91de9279 63 public:
j3 0:2d0c91de9279 64
j3 0:2d0c91de9279 65 ///7-bit slave address
j3 0:2d0c91de9279 66 static const uint8_t I2C_ADRS = 0x57;
j3 0:2d0c91de9279 67 ///8-bit write address
j3 0:2d0c91de9279 68 static const uint8_t I2C_W_ADRS = 0xAE;
j3 0:2d0c91de9279 69 ///8-bit read address
j3 0:2d0c91de9279 70 static const uint8_t I2C_R_ADRS = 0xAF;
j3 0:2d0c91de9279 71
j3 0:2d0c91de9279 72 ///MAX30101 Register Map
j3 0:2d0c91de9279 73 enum Registers_e
j3 0:2d0c91de9279 74 {
j3 0:2d0c91de9279 75 InterruptStatus1 = 0x00,
j3 0:2d0c91de9279 76 InterruptStatus2 = 0x01,
j3 0:2d0c91de9279 77 InterruptEnable1 = 0x02,
j3 0:2d0c91de9279 78 InterruptEnable2 = 0x03,
j3 0:2d0c91de9279 79 FIFO_WritePointer = 0x04,
j3 0:2d0c91de9279 80 OverflowCounter = 0x05,
j3 0:2d0c91de9279 81 FIFO_ReadPointer = 0x06,
j3 0:2d0c91de9279 82 FIFO_DataRegister = 0x07,
j3 0:2d0c91de9279 83 FIFO_Configuration = 0x08,
j3 0:2d0c91de9279 84 ModeConfiguration = 0x09,
j3 0:2d0c91de9279 85 SpO2Configuration = 0x0A,
j3 0:2d0c91de9279 86 LED1_PA = 0x0C,
j3 0:2d0c91de9279 87 LED2_PA = 0x0D,
j3 0:2d0c91de9279 88 LED3_PA = 0x0E,
j3 0:2d0c91de9279 89 ProxModeLED_PA = 0x10,
j3 0:2d0c91de9279 90 ModeControlReg1 = 0x11,
j3 0:2d0c91de9279 91 ModeControlReg2 = 0x12,
j3 0:2d0c91de9279 92 DieTempInt = 0x1F,
j3 0:2d0c91de9279 93 DieTempFrac = 0x20,
j3 0:2d0c91de9279 94 DieTempConfig = 0x21,
j3 0:2d0c91de9279 95 ProxIntThreshold = 0x30,
j3 0:2d0c91de9279 96 RevID = 0xFE,
j3 0:2d0c91de9279 97 PartID = 0xFF
j3 0:2d0c91de9279 98 };
j3 0:2d0c91de9279 99
j3 0:2d0c91de9279 100 ///Interrupt Status/Enable BitField
j3 0:2d0c91de9279 101 union InterruptBitField_u
j3 0:2d0c91de9279 102 {
j3 0:2d0c91de9279 103 uint8_t all;
j3 0:2d0c91de9279 104
j3 0:2d0c91de9279 105 struct BitField_s
j3 0:2d0c91de9279 106 {
j3 0:2d0c91de9279 107 uint8_t pwr_rdy : 1; ///< Bit0
j3 0:2d0c91de9279 108 uint8_t die_temp : 1; ///< Bit1
j3 0:2d0c91de9279 109 uint8_t reserved : 2; ///< Bit3:2
j3 0:2d0c91de9279 110 uint8_t prox_int : 1; ///< Bit4
j3 0:2d0c91de9279 111 uint8_t alc_ovf : 1; ///< Bit5
j3 0:2d0c91de9279 112 uint8_t ppg_rdy : 1; ///< Bit6
j3 0:2d0c91de9279 113 uint8_t a_full : 1; ///< Bit7
j3 0:2d0c91de9279 114 }bits;
j3 0:2d0c91de9279 115 };
j3 0:2d0c91de9279 116
j3 0:2d0c91de9279 117 ///FIFO Configuration BitField
j3 0:2d0c91de9279 118 union FIFO_Configuration_u
j3 0:2d0c91de9279 119 {
j3 0:2d0c91de9279 120 uint8_t all;
j3 0:2d0c91de9279 121 struct BitField_s
j3 0:2d0c91de9279 122 {
j3 0:2d0c91de9279 123 uint8_t fifo_a_full : 4;
j3 0:2d0c91de9279 124 uint8_t fifo_roll_over_en : 1;
j3 0:2d0c91de9279 125 uint8_t sample_average : 3;
j3 0:2d0c91de9279 126 }bits;
j3 0:2d0c91de9279 127 };
j3 0:2d0c91de9279 128
j3 0:2d0c91de9279 129 ///Mode Configuration BitField
j3 0:2d0c91de9279 130 union ModeConfiguration_u
j3 0:2d0c91de9279 131 {
j3 0:2d0c91de9279 132 uint8_t all;
j3 0:2d0c91de9279 133 struct BitField_s
j3 0:2d0c91de9279 134 {
j3 0:2d0c91de9279 135 uint8_t mode : 3;
j3 0:2d0c91de9279 136 uint8_t reserved : 3;
j3 0:2d0c91de9279 137 uint8_t reset : 1;
j3 0:2d0c91de9279 138 uint8_t shdn : 1;
j3 0:2d0c91de9279 139 }bits;
j3 0:2d0c91de9279 140 };
j3 0:2d0c91de9279 141
j3 0:2d0c91de9279 142 ///SpO2 Configuration BitField
j3 0:2d0c91de9279 143 union SpO2Configuration_u
j3 0:2d0c91de9279 144 {
j3 0:2d0c91de9279 145 uint8_t all;
j3 0:2d0c91de9279 146 struct BitField_s
j3 0:2d0c91de9279 147 {
j3 0:2d0c91de9279 148 uint8_t led_pw : 2;
j3 0:2d0c91de9279 149 uint8_t spo2_sr : 3;
j3 0:2d0c91de9279 150 uint8_t spo2_adc_range : 2;
j3 0:2d0c91de9279 151 uint8_t reserved : 1;
j3 0:2d0c91de9279 152 }bits;
j3 0:2d0c91de9279 153 };
j3 0:2d0c91de9279 154
j3 0:2d0c91de9279 155 ///Multi-LED Mode Control Register BitField
j3 0:2d0c91de9279 156 union ModeControlReg_u
j3 0:2d0c91de9279 157 {
j3 0:2d0c91de9279 158 uint8_t all;
j3 0:2d0c91de9279 159 struct BitField_s
j3 0:2d0c91de9279 160 {
j3 0:2d0c91de9279 161 uint8_t lo_slot : 3;
j3 0:2d0c91de9279 162 uint8_t reserved1 : 1;
j3 0:2d0c91de9279 163 uint8_t hi_slot : 3;
j3 0:2d0c91de9279 164 uint8_t reserved2 : 1;
j3 0:2d0c91de9279 165 }bits;
j3 0:2d0c91de9279 166 };
j3 0:2d0c91de9279 167
j3 0:2d0c91de9279 168 /**
j3 0:2d0c91de9279 169 * @brief Constructor using I2C PinNames
j3 0:2d0c91de9279 170 * @param sda - Pinname for sda
j3 0:2d0c91de9279 171 * @param scl - Pinname for scl
j3 0:2d0c91de9279 172 */
j3 0:2d0c91de9279 173 MAX30101(PinName sda, PinName scl);
j3 0:2d0c91de9279 174
j3 0:2d0c91de9279 175 /**
j3 0:2d0c91de9279 176 * @brief Constructor using reference to I2C object
j3 0:2d0c91de9279 177 * @param i2c - Reference to I2C object
j3 0:2d0c91de9279 178 */
j3 0:2d0c91de9279 179 MAX30101(I2C &i2c);
j3 0:2d0c91de9279 180
j3 0:2d0c91de9279 181 /** @brief Destructor */
j3 0:2d0c91de9279 182 ~MAX30101();
j3 0:2d0c91de9279 183
j3 0:2d0c91de9279 184 /**
j3 0:2d0c91de9279 185 * @brief Writes appropriate bits to Interrupt Enable 1 and 2.
j3 0:2d0c91de9279 186 *
j3 0:2d0c91de9279 187 * @param data - Interrupts to enable
j3 0:2d0c91de9279 188 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 189 */
j3 0:2d0c91de9279 190 int32_t enableInterrupts(const InterruptBitField_u data);
j3 0:2d0c91de9279 191
j3 0:2d0c91de9279 192 /**
j3 0:2d0c91de9279 193 * @brief Reads interrupt status flags from Interrupt Status 1 and 2.
j3 0:2d0c91de9279 194 *
j3 0:2d0c91de9279 195 * @param[out] data - Contains interrupts status flags on success.
j3 0:2d0c91de9279 196 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 197 */
j3 0:2d0c91de9279 198 int32_t getInterruptStatus(InterruptBitField_u &data);
j3 0:2d0c91de9279 199
j3 0:2d0c91de9279 200 /**
j3 0:2d0c91de9279 201 * @brief Writes FIFO configuration register with given data
j3 0:2d0c91de9279 202 *
j3 0:2d0c91de9279 203 * @param config - FIFO Configuration
j3 0:2d0c91de9279 204 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 205 */
j3 0:2d0c91de9279 206 int32_t setFIFOConfiguration(const FIFO_Configuration_u config);
j3 0:2d0c91de9279 207
j3 0:2d0c91de9279 208 /**
j3 0:2d0c91de9279 209 * @brief Reads FIFO configuration register
j3 0:2d0c91de9279 210 *
j3 0:2d0c91de9279 211 * @param[out] config - FIFO Configuration on success
j3 0:2d0c91de9279 212 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 213 */
j3 0:2d0c91de9279 214 int32_t getFIFOConfiguration(FIFO_Configuration_u &config);
j3 0:2d0c91de9279 215
j3 0:2d0c91de9279 216 /**
j3 0:2d0c91de9279 217 * @brief Writes Mode configuration register with given data
j3 0:2d0c91de9279 218 *
j3 0:2d0c91de9279 219 * @param config - Mode Configuration
j3 0:2d0c91de9279 220 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 221 */
j3 0:2d0c91de9279 222 int32_t setModeConfiguration(const ModeConfiguration_u config);
j3 0:2d0c91de9279 223
j3 0:2d0c91de9279 224 /**
j3 0:2d0c91de9279 225 * @brief Reads Mode configuration register
j3 0:2d0c91de9279 226 *
j3 0:2d0c91de9279 227 * @param[out] config - Mode Configuration on success
j3 0:2d0c91de9279 228 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 229 */
j3 0:2d0c91de9279 230 int32_t getModeConfiguration(ModeConfiguration_u &config);
j3 0:2d0c91de9279 231
j3 0:2d0c91de9279 232 /**
j3 0:2d0c91de9279 233 * @brief Writes SpO2 configuration register with given data
j3 0:2d0c91de9279 234 *
j3 0:2d0c91de9279 235 * @param config - SpO2 Configuration
j3 0:2d0c91de9279 236 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 237 */
j3 0:2d0c91de9279 238 int32_t setSpO2Configuration(const SpO2Configuration_u config);
j3 0:2d0c91de9279 239
j3 0:2d0c91de9279 240 /**
j3 0:2d0c91de9279 241 * @brief Reads SpO2 configuration register
j3 0:2d0c91de9279 242 *
j3 0:2d0c91de9279 243 * @param[out] config - SpO2 Configuration on success
j3 0:2d0c91de9279 244 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 245 */
j3 0:2d0c91de9279 246 int32_t getSpO2Configuration(SpO2Configuration_u &config);
j3 0:2d0c91de9279 247
j3 0:2d0c91de9279 248 /**
j3 0:2d0c91de9279 249 * @brief Writes LEDx/Prox Pulse Amplitude register with given data
j3 0:2d0c91de9279 250 *
j3 0:2d0c91de9279 251 * @param reg - LEDx/Prox Pulse Amplitude register to write
j3 0:2d0c91de9279 252 * @param amp - LED pulse amplitude
j3 0:2d0c91de9279 253 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 254 */
j3 0:2d0c91de9279 255 int32_t setLEDPulseAmplitude(Registers_e reg, const uint8_t amp);
j3 0:2d0c91de9279 256
j3 0:2d0c91de9279 257 /**
j3 0:2d0c91de9279 258 * @brief Reads LEDx/Prox Pulse Amplitude register
j3 0:2d0c91de9279 259 *
j3 0:2d0c91de9279 260 * @param reg - LEDx/Prox Pulse Amplitude register to read
j3 0:2d0c91de9279 261 * @param[out] amp - LED pulse amplitude on success
j3 0:2d0c91de9279 262 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 263 */
j3 0:2d0c91de9279 264 int32_t getLEDPulseAmplitude(Registers_e reg, uint8_t &amp);
j3 0:2d0c91de9279 265
j3 0:2d0c91de9279 266 /**
j3 0:2d0c91de9279 267 * @brief Writes Multi-LED Mode Control Register
j3 0:2d0c91de9279 268 *
j3 0:2d0c91de9279 269 * @param reg - Multi-LED Mode Control register 1 or 2
j3 0:2d0c91de9279 270 * @param data - Data to write to register
j3 0:2d0c91de9279 271 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 272 */
j3 0:2d0c91de9279 273 int32_t setMultiLEDModeControl(Registers_e reg, const ModeControlReg_u data);
j3 0:2d0c91de9279 274
j3 0:2d0c91de9279 275 /**
j3 0:2d0c91de9279 276 * @brief Reads Multi-LED Mode Control Register
j3 0:2d0c91de9279 277 *
j3 0:2d0c91de9279 278 * @param reg - Multi-LED Mode Control register 1 or 2
j3 0:2d0c91de9279 279 * @param[out] data - Data read from register on success
j3 0:2d0c91de9279 280 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 281 */
j3 0:2d0c91de9279 282 int32_t getMultiLEDModeControl(Registers_e reg, ModeControlReg_u &data);
j3 0:2d0c91de9279 283
j3 0:2d0c91de9279 284 /**
j3 0:2d0c91de9279 285 * @brief Gets raw die temperature
j3 0:2d0c91de9279 286 *
j3 0:2d0c91de9279 287 * @param[out] data - Raw die temperature on success
j3 0:2d0c91de9279 288 *
j3 0:2d0c91de9279 289 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 290 */
j3 0:2d0c91de9279 291 int32_t getDieTemperature(uint16_t &data);
j3 0:2d0c91de9279 292
j3 0:2d0c91de9279 293 /**
j3 0:2d0c91de9279 294 * @brief Gets die temperature in celsius
j3 0:2d0c91de9279 295 *
j3 0:2d0c91de9279 296 * @param[out] data - Die temperature in celsius on success
j3 0:2d0c91de9279 297 *
j3 0:2d0c91de9279 298 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 299 */
j3 0:2d0c91de9279 300 int32_t getDieTemperatureC(float &data);
j3 0:2d0c91de9279 301
j3 0:2d0c91de9279 302 /**
j3 0:2d0c91de9279 303 * @brief Converts celsius to Fahrenheit
j3 0:2d0c91de9279 304 *
j3 0:2d0c91de9279 305 * @param c - Temperature in celsius
j3 0:2d0c91de9279 306 *
j3 0:2d0c91de9279 307 * @return Temperature in Fahrenheit
j3 0:2d0c91de9279 308 */
j3 0:2d0c91de9279 309 float celsius2fahrenheit(float c);
j3 0:2d0c91de9279 310
j3 0:2d0c91de9279 311 /**
j3 0:2d0c91de9279 312 * @brief Writes Proximity Interrupt Threshold Register
j3 0:2d0c91de9279 313 *
j3 0:2d0c91de9279 314 * @param data - Data to write to register
j3 0:2d0c91de9279 315 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 316 */
j3 0:2d0c91de9279 317 int32_t setProxIntThreshold(const uint8_t data);
j3 0:2d0c91de9279 318
j3 0:2d0c91de9279 319 /**
j3 0:2d0c91de9279 320 * @brief Reads Proximity Interrupt Threshold Register
j3 0:2d0c91de9279 321 *
j3 0:2d0c91de9279 322 * @param data - Data read on success
j3 0:2d0c91de9279 323 * @return 0 on success, non 0 otherwise
j3 0:2d0c91de9279 324 */
j3 0:2d0c91de9279 325 int32_t getProxIntThreshold(uint8_t &data);
j3 0:2d0c91de9279 326
j3 0:2d0c91de9279 327 protected:
j3 0:2d0c91de9279 328
j3 0:2d0c91de9279 329 /**
j3 0:2d0c91de9279 330 * @brief Write register of device
j3 0:2d0c91de9279 331 * @param reg - Register address
j3 0:2d0c91de9279 332 * @param value - Value to write
j3 0:2d0c91de9279 333 * @return 0 on success, non-zero on failure
j3 0:2d0c91de9279 334 */
j3 0:2d0c91de9279 335 int32_t writeRegister(Registers_e reg, uint8_t value);
j3 0:2d0c91de9279 336
j3 0:2d0c91de9279 337 /**
j3 0:2d0c91de9279 338 * @brief Read register of device
j3 0:2d0c91de9279 339 * @param reg - Register address
j3 0:2d0c91de9279 340 * @param[out] value - Read data on success
j3 0:2d0c91de9279 341 * @return 0 on success, non-zero on failure
j3 0:2d0c91de9279 342 */
j3 0:2d0c91de9279 343 int32_t readRegister(Registers_e reg, uint8_t &value);
j3 0:2d0c91de9279 344
j3 0:2d0c91de9279 345 private:
j3 0:2d0c91de9279 346
j3 0:2d0c91de9279 347 I2C m_i2cBus;
j3 0:2d0c91de9279 348
j3 0:2d0c91de9279 349 };
j3 0:2d0c91de9279 350
j3 0:2d0c91de9279 351 #endif /* __MAX30101_H_ */
j3 0:2d0c91de9279 352