Maxim Integrated / Mbed OS MAX30208_Demo

Dependencies:   max32630fthr USBDevice

Committer:
tlyp
Date:
Wed Jul 15 15:06:32 2020 +0000
Revision:
0:d996cb30964c
Child:
3:27e027ab268b
Testing Program for MAX30208 Temp Sensor;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tlyp 0:d996cb30964c 1 /*******************************************************************************
tlyp 0:d996cb30964c 2 * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
tlyp 0:d996cb30964c 3 *
tlyp 0:d996cb30964c 4 * Permission is hereby granted, free of charge, to any person obtaining a
tlyp 0:d996cb30964c 5 * copy of this software and associated documentation files (the "Software"),
tlyp 0:d996cb30964c 6 * to deal in the Software without restriction, including without limitation
tlyp 0:d996cb30964c 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
tlyp 0:d996cb30964c 8 * and/or sell copies of the Software, and to permit persons to whom the
tlyp 0:d996cb30964c 9 * Software is furnished to do so, subject to the following conditions:
tlyp 0:d996cb30964c 10 *
tlyp 0:d996cb30964c 11 * The above copyright notice and this permission notice shall be included
tlyp 0:d996cb30964c 12 * in all copies or substantial portions of the Software.
tlyp 0:d996cb30964c 13 *
tlyp 0:d996cb30964c 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
tlyp 0:d996cb30964c 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tlyp 0:d996cb30964c 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tlyp 0:d996cb30964c 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
tlyp 0:d996cb30964c 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
tlyp 0:d996cb30964c 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
tlyp 0:d996cb30964c 20 * OTHER DEALINGS IN THE SOFTWARE.
tlyp 0:d996cb30964c 21 *
tlyp 0:d996cb30964c 22 * Except as contained in this notice, the name of Maxim Integrated
tlyp 0:d996cb30964c 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
tlyp 0:d996cb30964c 24 * Products, Inc. Branding Policy.
tlyp 0:d996cb30964c 25 *
tlyp 0:d996cb30964c 26 * The mere transfer of this software does not imply any licenses
tlyp 0:d996cb30964c 27 * of trade secrets, proprietary technology, copyrights, patents,
tlyp 0:d996cb30964c 28 * trademarks, maskwork rights, or any other form of intellectual
tlyp 0:d996cb30964c 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
tlyp 0:d996cb30964c 30 * ownership rights.
tlyp 0:d996cb30964c 31 *******************************************************************************
tlyp 0:d996cb30964c 32 */
tlyp 0:d996cb30964c 33 #ifndef __MAX30208_H_
tlyp 0:d996cb30964c 34 #define __MAX30208_H_
tlyp 0:d996cb30964c 35
tlyp 0:d996cb30964c 36 #include "mbed.h"
tlyp 0:d996cb30964c 37
tlyp 0:d996cb30964c 38 /**
tlyp 0:d996cb30964c 39 * @brief Library for the MAX30208\n
tlyp 0:d996cb30964c 40 *
tlyp 0:d996cb30964c 41 * @code
tlyp 0:d996cb30964c 42 * #include "mbed.h"
tlyp 0:d996cb30964c 43 * #include "max32630fthr.h"
tlyp 0:d996cb30964c 44 * #include "MAX30208.h"
tlyp 0:d996cb30964c 45 *
tlyp 0:d996cb30964c 46 * MAX32630FTHR pegasus(MAX32630FTHR::VIO_3V3);
tlyp 0:d996cb30964c 47 *
tlyp 0:d996cb30964c 48 * //Get I2C instance
tlyp 0:d996cb30964c 49 * I2C i2cBus(I2C1_SDA, I2C1_SCL);
tlyp 0:d996cb30964c 50 *
tlyp 0:d996cb30964c 51 * //Get temp sensor instance
tlyp 0:d996cb30964c 52 * MAX30208 bodyTempSensor(i2cBus, 0x50); //Constructor takes 7-bit slave adrs
tlyp 0:d996cb30964c 53 *
tlyp 0:d996cb30964c 54 * int main(void)
tlyp 0:d996cb30964c 55 * {
tlyp 0:d996cb30964c 56 * //use sensor
tlyp 0:d996cb30964c 57 * }
tlyp 0:d996cb30964c 58 * @endcode
tlyp 0:d996cb30964c 59 */
tlyp 0:d996cb30964c 60
tlyp 0:d996cb30964c 61 class MAX30208
tlyp 0:d996cb30964c 62 {
tlyp 0:d996cb30964c 63
tlyp 0:d996cb30964c 64 public:
tlyp 0:d996cb30964c 65 /// MAX30208 Register Addresses
tlyp 0:d996cb30964c 66 enum Registers_e {
tlyp 0:d996cb30964c 67 Status = 0x00,
tlyp 0:d996cb30964c 68 Interrupt_Enable = 0x01,
tlyp 0:d996cb30964c 69 FIFO_Write_Pointer = 0x04,
tlyp 0:d996cb30964c 70 FIFO_Read_Pointer = 0x05,
tlyp 0:d996cb30964c 71 FIFO_Overflow_Counter = 0x06,
tlyp 0:d996cb30964c 72 FIFO_Data_Counter = 0x07,
tlyp 0:d996cb30964c 73 FIFO_Data = 0x08,
tlyp 0:d996cb30964c 74 FIFO_Config1 = 0x09,
tlyp 0:d996cb30964c 75 FIFO_Config2 = 0x0A,
tlyp 0:d996cb30964c 76 System_Control = 0x0C,
tlyp 0:d996cb30964c 77 Alarm_High_MSB = 0x10,
tlyp 0:d996cb30964c 78 Alarm_Low_MSB = 0x12,
tlyp 0:d996cb30964c 79 Temp_Sensor_Setup = 0x14,
tlyp 0:d996cb30964c 80 GPIO_Setup = 0x20,
tlyp 0:d996cb30964c 81 GPIO_Control = 0x21
tlyp 0:d996cb30964c 82 };
tlyp 0:d996cb30964c 83
tlyp 0:d996cb30964c 84 union Configuration_InterruptEnable{
tlyp 0:d996cb30964c 85 uint8_t all;
tlyp 0:d996cb30964c 86 struct BitField_s{
tlyp 0:d996cb30964c 87 uint8_t TEMP_RDY_EN : 1;
tlyp 0:d996cb30964c 88 uint8_t TEMP_HI_EN : 1;
tlyp 0:d996cb30964c 89 uint8_t TEMP_LO_EN : 1;
tlyp 0:d996cb30964c 90 uint8_t : 4; //unused bits
tlyp 0:d996cb30964c 91 uint8_t A_FULL_EN : 1;
tlyp 0:d996cb30964c 92 }config;
tlyp 0:d996cb30964c 93 };
tlyp 0:d996cb30964c 94
tlyp 0:d996cb30964c 95 union Configuration_FIFOConfig2{
tlyp 0:d996cb30964c 96 uint8_t all;
tlyp 0:d996cb30964c 97 struct BitField_s{
tlyp 0:d996cb30964c 98 uint8_t : 1; //unused bit
tlyp 0:d996cb30964c 99 uint8_t FIFO_RO : 1;
tlyp 0:d996cb30964c 100 uint8_t A_FULL_TYPE : 1;
tlyp 0:d996cb30964c 101 uint8_t FIFO_STAT_CLR : 1;
tlyp 0:d996cb30964c 102 uint8_t FLUSH_FIFO : 1;
tlyp 0:d996cb30964c 103 uint8_t : 0; //unused bits
tlyp 0:d996cb30964c 104 }config;
tlyp 0:d996cb30964c 105 };
tlyp 0:d996cb30964c 106
tlyp 0:d996cb30964c 107 union Configuration_GPIOSetup{
tlyp 0:d996cb30964c 108 uint8_t all;
tlyp 0:d996cb30964c 109 struct BitField_s{
tlyp 0:d996cb30964c 110 uint8_t GPIO0_MODE : 2;
tlyp 0:d996cb30964c 111 uint8_t : 4; //unused bits
tlyp 0:d996cb30964c 112 uint8_t GPIO1_MODE : 2;
tlyp 0:d996cb30964c 113 }config;
tlyp 0:d996cb30964c 114 };
tlyp 0:d996cb30964c 115
tlyp 0:d996cb30964c 116 union Configuration_GPIOControl{
tlyp 0:d996cb30964c 117 uint8_t all;
tlyp 0:d996cb30964c 118 struct BitField_s{
tlyp 0:d996cb30964c 119 uint8_t GPIO0_LL : 1;
tlyp 0:d996cb30964c 120 uint8_t : 2; //unused bits
tlyp 0:d996cb30964c 121 uint8_t GPIO1_LL : 1;
tlyp 0:d996cb30964c 122 uint8_t : 0; //unused bits
tlyp 0:d996cb30964c 123 }config;
tlyp 0:d996cb30964c 124 };
tlyp 0:d996cb30964c 125
tlyp 0:d996cb30964c 126 /**
tlyp 0:d996cb30964c 127 * @brief Constructor using reference to I2C object
tlyp 0:d996cb30964c 128 * @param i2c - Reference to I2C object
tlyp 0:d996cb30964c 129 * @param slaveAddress - 7-bit I2C address
tlyp 0:d996cb30964c 130 */
tlyp 0:d996cb30964c 131 MAX30208(I2C &i2c, uint8_t slaveAddress);
tlyp 0:d996cb30964c 132
tlyp 0:d996cb30964c 133 /** @brief Destructor */
tlyp 0:d996cb30964c 134 ~MAX30208(void);
tlyp 0:d996cb30964c 135
tlyp 0:d996cb30964c 136 /**
tlyp 0:d996cb30964c 137 * @brief Write Interrupt Register
tlyp 0:d996cb30964c 138 * @param config - Reference to Configuration type, config.all is written upon succesful register write
tlyp 0:d996cb30964c 139 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 140 */
tlyp 0:d996cb30964c 141 int32_t writeInterruptRegister(Configuration_InterruptEnable config);
tlyp 0:d996cb30964c 142
tlyp 0:d996cb30964c 143 /**
tlyp 0:d996cb30964c 144 * @brief Read Interrupt Register Configuration
tlyp 0:d996cb30964c 145 * @param config - Reference to Configuration type, config.all is updated upon succesful register read
tlyp 0:d996cb30964c 146 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 147 */
tlyp 0:d996cb30964c 148 int32_t readInterruptRegister(Configuration_InterruptEnable &config);
tlyp 0:d996cb30964c 149
tlyp 0:d996cb30964c 150 /**
tlyp 0:d996cb30964c 151 * @brief Read Status Register
tlyp 0:d996cb30964c 152 * @param[out] value - Status Register Value on succesful read
tlyp 0:d996cb30964c 153 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 154 */
tlyp 0:d996cb30964c 155 int32_t readStatus(uint16_t &value);
tlyp 0:d996cb30964c 156
tlyp 0:d996cb30964c 157 /**
tlyp 0:d996cb30964c 158 * @brief Read FIFO Write Pointer Value
tlyp 0:d996cb30964c 159 * @param[out] value - FIFO Write Pointer value on succesful read
tlyp 0:d996cb30964c 160 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 161 */
tlyp 0:d996cb30964c 162 int32_t readWritePointer(uint16_t &value);
tlyp 0:d996cb30964c 163
tlyp 0:d996cb30964c 164 /**
tlyp 0:d996cb30964c 165 * @brief Read FIFO Read Pointer Value
tlyp 0:d996cb30964c 166 * @param[out] value - FIFO Read Pointer value on succesful read
tlyp 0:d996cb30964c 167 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 168 */
tlyp 0:d996cb30964c 169 int32_t readReadPointer(uint16_t &value);
tlyp 0:d996cb30964c 170
tlyp 0:d996cb30964c 171 /**
tlyp 0:d996cb30964c 172 * @brief Write FIFO Read Pointer Value
tlyp 0:d996cb30964c 173 * @param config - New FIFO Read Pointer value on succesful write
tlyp 0:d996cb30964c 174 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 175 */
tlyp 0:d996cb30964c 176 int32_t writeReadPointer(uint8_t config);
tlyp 0:d996cb30964c 177
tlyp 0:d996cb30964c 178 /**
tlyp 0:d996cb30964c 179 * @brief Read FIFO Overflow Register
tlyp 0:d996cb30964c 180 * @param[out] value - Overflow Counter value on succesful read
tlyp 0:d996cb30964c 181 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 182 */
tlyp 0:d996cb30964c 183 int32_t readOverflow(uint16_t &value);
tlyp 0:d996cb30964c 184
tlyp 0:d996cb30964c 185 /**
tlyp 0:d996cb30964c 186 * @brief Read Data Counter Register
tlyp 0:d996cb30964c 187 * @param[out] value - Data Count register value on succesful read
tlyp 0:d996cb30964c 188 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 189 */
tlyp 0:d996cb30964c 190 int32_t readDataCounter(uint16_t &value);
tlyp 0:d996cb30964c 191
tlyp 0:d996cb30964c 192 /**
tlyp 0:d996cb30964c 193 * @brief Read FIFO Data at FIFO Read Pointer
tlyp 0:d996cb30964c 194 * @param[out] value - Temperature value from FIFO data register on succesful read
tlyp 0:d996cb30964c 195 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 196 */
tlyp 0:d996cb30964c 197 int32_t readData(uint16_t &value);
tlyp 0:d996cb30964c 198
tlyp 0:d996cb30964c 199 /**
tlyp 0:d996cb30964c 200 * @brief Take a new temperature reading
tlyp 0:d996cb30964c 201 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 202 */
tlyp 0:d996cb30964c 203 int32_t takeDataMeasurment();
tlyp 0:d996cb30964c 204
tlyp 0:d996cb30964c 205 /**
tlyp 0:d996cb30964c 206 * @brief Read FIFO Config1 Register
tlyp 0:d996cb30964c 207 * @param[out] value - FIFO Config1 value on succesful read
tlyp 0:d996cb30964c 208 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 209 */
tlyp 0:d996cb30964c 210 int32_t readFIFOConfig1(uint16_t &value);
tlyp 0:d996cb30964c 211
tlyp 0:d996cb30964c 212 /**
tlyp 0:d996cb30964c 213 * @brief Write FIFO Config1 register
tlyp 0:d996cb30964c 214 * @param config - FIFO Config1 register data to write
tlyp 0:d996cb30964c 215 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 216 */
tlyp 0:d996cb30964c 217 int32_t writeFIFOConfig1(uint8_t config);
tlyp 0:d996cb30964c 218
tlyp 0:d996cb30964c 219 /**
tlyp 0:d996cb30964c 220 * @brief Read FIFO Config2 register
tlyp 0:d996cb30964c 221 * @param[out] config - Reference to Configuration type, config.all is updated upon succesful register read
tlyp 0:d996cb30964c 222 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 223 */
tlyp 0:d996cb30964c 224 int32_t readFIFOConfig2(Configuration_FIFOConfig2 &config);
tlyp 0:d996cb30964c 225
tlyp 0:d996cb30964c 226 /**
tlyp 0:d996cb30964c 227 * @brief Read FIFO Config2 register
tlyp 0:d996cb30964c 228 * @param config - Reference to Configuration type, config.all is written upon succesful register write
tlyp 0:d996cb30964c 229 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 230 */
tlyp 0:d996cb30964c 231 int32_t writeFIFOConfig2(Configuration_FIFOConfig2 config);
tlyp 0:d996cb30964c 232
tlyp 0:d996cb30964c 233 /**
tlyp 0:d996cb30964c 234 * @brief Reset Device to factory default
tlyp 0:d996cb30964c 235 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 236 */
tlyp 0:d996cb30964c 237 int32_t resetDevice(); //set bit 0 in system register to 1 to factory reset
tlyp 0:d996cb30964c 238
tlyp 0:d996cb30964c 239 /**
tlyp 0:d996cb30964c 240 * @brief Read High Temperature Alarm Value
tlyp 0:d996cb30964c 241 * @param[out] temp - High Temperature Alarm Value
tlyp 0:d996cb30964c 242 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 243 */
tlyp 0:d996cb30964c 244 int32_t readAlarmHigh(uint16_t &temp);
tlyp 0:d996cb30964c 245
tlyp 0:d996cb30964c 246 /**
tlyp 0:d996cb30964c 247 * @brief Write High Temperature Alarm Value
tlyp 0:d996cb30964c 248 * @param temp - 16-bit High Temperature Value to Write
tlyp 0:d996cb30964c 249 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 250 */
tlyp 0:d996cb30964c 251 int32_t writeAlarmHigh(uint16_t temp);
tlyp 0:d996cb30964c 252
tlyp 0:d996cb30964c 253 /**
tlyp 0:d996cb30964c 254 * @brief Read Low Temperature Alarm Value
tlyp 0:d996cb30964c 255 * @param[out] temp - Low Temperature Alarm Value
tlyp 0:d996cb30964c 256 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 257 */
tlyp 0:d996cb30964c 258 int32_t readAlarmLow(uint16_t &value);
tlyp 0:d996cb30964c 259
tlyp 0:d996cb30964c 260 /**
tlyp 0:d996cb30964c 261 * @brief Write Low Temperature Alarm Value
tlyp 0:d996cb30964c 262 * @param temp - 16-bit Low Temperature Value to Write
tlyp 0:d996cb30964c 263 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 264 */
tlyp 0:d996cb30964c 265 int32_t writeAlarmLow(uint16_t temp);
tlyp 0:d996cb30964c 266
tlyp 0:d996cb30964c 267 /**
tlyp 0:d996cb30964c 268 * @brief Read GPIO Setup register
tlyp 0:d996cb30964c 269 * @param config - Reference to Configuration type, config.all is updated upon succesful register read
tlyp 0:d996cb30964c 270 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 271 */
tlyp 0:d996cb30964c 272 int32_t readGPIOSetup(Configuration_GPIOSetup &config);
tlyp 0:d996cb30964c 273
tlyp 0:d996cb30964c 274 /**
tlyp 0:d996cb30964c 275 * @brief Write GPIO Setup register
tlyp 0:d996cb30964c 276 * @param config - Reference to Configuration type, config.all is written to register upon succesful register write
tlyp 0:d996cb30964c 277 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 278 */
tlyp 0:d996cb30964c 279 int32_t writeGPIOSetup(Configuration_GPIOSetup config);
tlyp 0:d996cb30964c 280
tlyp 0:d996cb30964c 281 /**
tlyp 0:d996cb30964c 282 * @brief Read GPIO Control register
tlyp 0:d996cb30964c 283 * @param config - Reference to Configuration type, config.all is updated upon succesful register read
tlyp 0:d996cb30964c 284 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 285 */
tlyp 0:d996cb30964c 286 int32_t readGPIOControl(Configuration_GPIOControl &config);
tlyp 0:d996cb30964c 287
tlyp 0:d996cb30964c 288 /**
tlyp 0:d996cb30964c 289 * @brief Write GPIO Control register
tlyp 0:d996cb30964c 290 * @param config - Reference to Configuration type, config.all is written to register upon succesful register write
tlyp 0:d996cb30964c 291 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 292 */
tlyp 0:d996cb30964c 293 int32_t writeGPIOControl(Configuration_GPIOControl config);
tlyp 0:d996cb30964c 294
tlyp 0:d996cb30964c 295 /**
tlyp 0:d996cb30964c 296 * @brief Convert Raw Sensor Data to degrees Celisus
tlyp 0:d996cb30964c 297 * @param rawTemp - 16 bit raw temperature data
tlyp 0:d996cb30964c 298 * @return Returns the converted Celsius Temperature
tlyp 0:d996cb30964c 299 */
tlyp 0:d996cb30964c 300 float toCelsius(uint16_t rawTemp);
tlyp 0:d996cb30964c 301
tlyp 0:d996cb30964c 302 /**
tlyp 0:d996cb30964c 303 * @brief Convert Celsius Temperature to Fahrenheit
tlyp 0:d996cb30964c 304 * @param temperatureC - Temperature in degrees Celsius that will be converted
tlyp 0:d996cb30964c 305 * @return Returns the converted Fahrenheit temperature
tlyp 0:d996cb30964c 306 */
tlyp 0:d996cb30964c 307 float toFahrenheit(float temperatureC);
tlyp 0:d996cb30964c 308
tlyp 0:d996cb30964c 309 protected:
tlyp 0:d996cb30964c 310
tlyp 0:d996cb30964c 311 /**
tlyp 0:d996cb30964c 312 * @brief Write register of device at slave address
tlyp 0:d996cb30964c 313 * @param reg - char array that contains address of register and write value
tlyp 0:d996cb30964c 314 * @param value - Data written to register on sucessful write
tlyp 0:d996cb30964c 315 * @param bytesWritten - Number of bytes to write
tlyp 0:d996cb30964c 316 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 317 */
tlyp 0:d996cb30964c 318 int32_t writeRegister(Registers_e reg, uint16_t value, int bytesWritten);
tlyp 0:d996cb30964c 319 /**
tlyp 0:d996cb30964c 320 * @brief Read register of device at slave address
tlyp 0:d996cb30964c 321 * @param reg - Register address
tlyp 0:d996cb30964c 322 * @param[out] value - Read data on successful read
tlyp 0:d996cb30964c 323 * @param bytesRead - Number of bytes to read
tlyp 0:d996cb30964c 324 * @return 0 on success, non-zero on failure
tlyp 0:d996cb30964c 325 */
tlyp 0:d996cb30964c 326 int32_t readRegister(Registers_e reg, uint16_t &value, int bytesRead);
tlyp 0:d996cb30964c 327
tlyp 0:d996cb30964c 328 private:
tlyp 0:d996cb30964c 329 /// I2C object
tlyp 0:d996cb30964c 330 I2C & m_i2c;
tlyp 0:d996cb30964c 331 /// Device slave addresses
tlyp 0:d996cb30964c 332 uint8_t m_writeAddress, m_readAddress;
tlyp 0:d996cb30964c 333 };
tlyp 0:d996cb30964c 334
tlyp 0:d996cb30964c 335 #endif /* __MAX30208_H_ */