Vincent Neo / Mbed 2 deprecated HSP_CS3237

Dependencies:   mbed

Committer:
tenvinc
Date:
Sun Sep 15 09:00:05 2019 +0000
Revision:
3:7932917dea9d
init

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tenvinc 3:7932917dea9d 1 ///*******************************************************************************
tenvinc 3:7932917dea9d 2 // * Copyright (C) 2017 Maxim Integrated Products, Inc., All Rights Reserved.
tenvinc 3:7932917dea9d 3 // *
tenvinc 3:7932917dea9d 4 // * Permission is hereby granted, free of charge, to any person obtaining a
tenvinc 3:7932917dea9d 5 // * copy of this software and associated documentation files (the "Software"),
tenvinc 3:7932917dea9d 6 // * to deal in the Software without restriction, including without limitation
tenvinc 3:7932917dea9d 7 // * the rights to use, copy, modify, merge, publish, distribute, sublicense,
tenvinc 3:7932917dea9d 8 // * and/or sell copies of the Software, and to permit persons to whom the
tenvinc 3:7932917dea9d 9 // * Software is furnished to do so, subject to the following conditions:
tenvinc 3:7932917dea9d 10 // *
tenvinc 3:7932917dea9d 11 // * The above copyright notice and this permission notice shall be included
tenvinc 3:7932917dea9d 12 // * in all copies or substantial portions of the Software.
tenvinc 3:7932917dea9d 13 // *
tenvinc 3:7932917dea9d 14 // * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
tenvinc 3:7932917dea9d 15 // * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tenvinc 3:7932917dea9d 16 // * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tenvinc 3:7932917dea9d 17 // * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
tenvinc 3:7932917dea9d 18 // * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
tenvinc 3:7932917dea9d 19 // * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
tenvinc 3:7932917dea9d 20 // * OTHER DEALINGS IN THE SOFTWARE.
tenvinc 3:7932917dea9d 21 // *
tenvinc 3:7932917dea9d 22 // * Except as contained in this notice, the name of Maxim Integrated
tenvinc 3:7932917dea9d 23 // * Products, Inc. shall not be used except as stated in the Maxim Integrated
tenvinc 3:7932917dea9d 24 // * Products, Inc. Branding Policy.
tenvinc 3:7932917dea9d 25 // *
tenvinc 3:7932917dea9d 26 // * The mere transfer of this software does not imply any licenses
tenvinc 3:7932917dea9d 27 // * of trade secrets, proprietary technology, copyrights, patents,
tenvinc 3:7932917dea9d 28 // * trademarks, maskwork rights, or any other form of intellectual
tenvinc 3:7932917dea9d 29 // * property whatsoever. Maxim Integrated Products, Inc. retains all
tenvinc 3:7932917dea9d 30 // * ownership rights.
tenvinc 3:7932917dea9d 31 // *******************************************************************************
tenvinc 3:7932917dea9d 32 // */
tenvinc 3:7932917dea9d 33 //
tenvinc 3:7932917dea9d 34 //
tenvinc 3:7932917dea9d 35 //#include "MAX30205.h"
tenvinc 3:7932917dea9d 36 //
tenvinc 3:7932917dea9d 37 //
tenvinc 3:7932917dea9d 38 ////******************************************************************************
tenvinc 3:7932917dea9d 39 //MAX30205::MAX30205(I2C &i2c, uint8_t slaveAddress):
tenvinc 3:7932917dea9d 40 //m_i2c(i2c), m_writeAddress(slaveAddress << 1),
tenvinc 3:7932917dea9d 41 //m_readAddress((slaveAddress << 1) | 1)
tenvinc 3:7932917dea9d 42 //{
tenvinc 3:7932917dea9d 43 //}
tenvinc 3:7932917dea9d 44 //
tenvinc 3:7932917dea9d 45 //
tenvinc 3:7932917dea9d 46 ////******************************************************************************
tenvinc 3:7932917dea9d 47 //MAX30205::~MAX30205(void)
tenvinc 3:7932917dea9d 48 //{
tenvinc 3:7932917dea9d 49 // //empty block
tenvinc 3:7932917dea9d 50 //}
tenvinc 3:7932917dea9d 51 //
tenvinc 3:7932917dea9d 52 //
tenvinc 3:7932917dea9d 53 ////******************************************************************************
tenvinc 3:7932917dea9d 54 //int32_t MAX30205::readTemperature(uint16_t &value)
tenvinc 3:7932917dea9d 55 //{
tenvinc 3:7932917dea9d 56 // return readRegister(MAX30205::Temperature, value);
tenvinc 3:7932917dea9d 57 //
tenvinc 3:7932917dea9d 58 //}
tenvinc 3:7932917dea9d 59 //
tenvinc 3:7932917dea9d 60 //
tenvinc 3:7932917dea9d 61 ////******************************************************************************
tenvinc 3:7932917dea9d 62 //int32_t MAX30205::readConfiguration(Configuration_u &config)
tenvinc 3:7932917dea9d 63 //{
tenvinc 3:7932917dea9d 64 // uint16_t data;
tenvinc 3:7932917dea9d 65 //
tenvinc 3:7932917dea9d 66 // int32_t result = readRegister(MAX30205::Configuration, data);
tenvinc 3:7932917dea9d 67 // if(result == 0)
tenvinc 3:7932917dea9d 68 // {
tenvinc 3:7932917dea9d 69 // config.all = (0x00FF & data);
tenvinc 3:7932917dea9d 70 // }
tenvinc 3:7932917dea9d 71 //
tenvinc 3:7932917dea9d 72 // return result;
tenvinc 3:7932917dea9d 73 //
tenvinc 3:7932917dea9d 74 //}
tenvinc 3:7932917dea9d 75 //
tenvinc 3:7932917dea9d 76 //
tenvinc 3:7932917dea9d 77 ////******************************************************************************
tenvinc 3:7932917dea9d 78 //int32_t MAX30205::writeConfiguration(const Configuration_u config)
tenvinc 3:7932917dea9d 79 //{
tenvinc 3:7932917dea9d 80 // uint16_t local_config = (0x00FF & config.all);
tenvinc 3:7932917dea9d 81 //
tenvinc 3:7932917dea9d 82 // return writeRegister(MAX30205::Configuration, local_config);
tenvinc 3:7932917dea9d 83 //}
tenvinc 3:7932917dea9d 84 //
tenvinc 3:7932917dea9d 85 //
tenvinc 3:7932917dea9d 86 ////******************************************************************************
tenvinc 3:7932917dea9d 87 //int32_t MAX30205::readTHYST(uint16_t &value)
tenvinc 3:7932917dea9d 88 //{
tenvinc 3:7932917dea9d 89 // return readRegister(MAX30205::THYST, value);
tenvinc 3:7932917dea9d 90 //}
tenvinc 3:7932917dea9d 91 //
tenvinc 3:7932917dea9d 92 //
tenvinc 3:7932917dea9d 93 ////******************************************************************************
tenvinc 3:7932917dea9d 94 //int32_t MAX30205::writeTHYST(uint16_t value)
tenvinc 3:7932917dea9d 95 //{
tenvinc 3:7932917dea9d 96 // return writeRegister(MAX30205::THYST, value);
tenvinc 3:7932917dea9d 97 //}
tenvinc 3:7932917dea9d 98 //
tenvinc 3:7932917dea9d 99 //
tenvinc 3:7932917dea9d 100 ////******************************************************************************
tenvinc 3:7932917dea9d 101 //int32_t MAX30205::readTOS(uint16_t &value)
tenvinc 3:7932917dea9d 102 //{
tenvinc 3:7932917dea9d 103 // return readRegister(MAX30205::TOS, value);
tenvinc 3:7932917dea9d 104 //}
tenvinc 3:7932917dea9d 105 //
tenvinc 3:7932917dea9d 106 //
tenvinc 3:7932917dea9d 107 ////******************************************************************************
tenvinc 3:7932917dea9d 108 //int32_t MAX30205::writeTOS(const uint16_t value)
tenvinc 3:7932917dea9d 109 //{
tenvinc 3:7932917dea9d 110 // return writeRegister(MAX30205::TOS, value);
tenvinc 3:7932917dea9d 111 //}
tenvinc 3:7932917dea9d 112 //
tenvinc 3:7932917dea9d 113 //
tenvinc 3:7932917dea9d 114 ////******************************************************************************
tenvinc 3:7932917dea9d 115 //float MAX30205::toCelsius(uint32_t rawTemp)
tenvinc 3:7932917dea9d 116 //{
tenvinc 3:7932917dea9d 117 // uint8_t val1, val2;
tenvinc 3:7932917dea9d 118 // float result;
tenvinc 3:7932917dea9d 119 //
tenvinc 3:7932917dea9d 120 // val1 = (rawTemp >> 8);
tenvinc 3:7932917dea9d 121 // val2 = (rawTemp & 0xFF);
tenvinc 3:7932917dea9d 122 //
tenvinc 3:7932917dea9d 123 // result = static_cast<float>(val1 + (val2/ 256.0F));
tenvinc 3:7932917dea9d 124 //
tenvinc 3:7932917dea9d 125 // return result;
tenvinc 3:7932917dea9d 126 //}
tenvinc 3:7932917dea9d 127 //
tenvinc 3:7932917dea9d 128 //
tenvinc 3:7932917dea9d 129 ////******************************************************************************
tenvinc 3:7932917dea9d 130 //float MAX30205::toFahrenheit(float temperatureC)
tenvinc 3:7932917dea9d 131 //{
tenvinc 3:7932917dea9d 132 // return((temperatureC * 1.8F) + 32.0f);
tenvinc 3:7932917dea9d 133 //}
tenvinc 3:7932917dea9d 134 //
tenvinc 3:7932917dea9d 135 //
tenvinc 3:7932917dea9d 136 ////******************************************************************************
tenvinc 3:7932917dea9d 137 //int32_t MAX30205::writeRegister(Registers_e reg, uint16_t value)
tenvinc 3:7932917dea9d 138 //{
tenvinc 3:7932917dea9d 139 // int32_t result;
tenvinc 3:7932917dea9d 140 //
tenvinc 3:7932917dea9d 141 // uint8_t hi = ((value >> 8) & 0xFF);
tenvinc 3:7932917dea9d 142 // uint8_t lo = (value & 0xFF);
tenvinc 3:7932917dea9d 143 // char cmdData[3] = {reg, hi, lo};
tenvinc 3:7932917dea9d 144 //
tenvinc 3:7932917dea9d 145 // result = m_i2c.write(m_writeAddress, cmdData, 3);
tenvinc 3:7932917dea9d 146 //
tenvinc 3:7932917dea9d 147 // return result;
tenvinc 3:7932917dea9d 148 //}
tenvinc 3:7932917dea9d 149 //
tenvinc 3:7932917dea9d 150 //
tenvinc 3:7932917dea9d 151 ////******************************************************************************
tenvinc 3:7932917dea9d 152 //int32_t MAX30205::readRegister(Registers_e reg, uint16_t &value)
tenvinc 3:7932917dea9d 153 //{
tenvinc 3:7932917dea9d 154 // int32_t result;
tenvinc 3:7932917dea9d 155 //
tenvinc 3:7932917dea9d 156 // char data[2];
tenvinc 3:7932917dea9d 157 // char cmdData[1] = {reg};
tenvinc 3:7932917dea9d 158 //
tenvinc 3:7932917dea9d 159 // result = m_i2c.write(m_writeAddress, cmdData, 1);
tenvinc 3:7932917dea9d 160 // if(result == 0)
tenvinc 3:7932917dea9d 161 // {
tenvinc 3:7932917dea9d 162 // result = m_i2c.read(m_readAddress, data, 2);
tenvinc 3:7932917dea9d 163 // if (result == 0)
tenvinc 3:7932917dea9d 164 // {
tenvinc 3:7932917dea9d 165 // value = (data[0] << 8) + data[1];
tenvinc 3:7932917dea9d 166 // }
tenvinc 3:7932917dea9d 167 // }
tenvinc 3:7932917dea9d 168 //
tenvinc 3:7932917dea9d 169 // return result;
tenvinc 3:7932917dea9d 170 //}
tenvinc 3:7932917dea9d 171
tenvinc 3:7932917dea9d 172
tenvinc 3:7932917dea9d 173 /*******************************************************************************
tenvinc 3:7932917dea9d 174 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
tenvinc 3:7932917dea9d 175 *
tenvinc 3:7932917dea9d 176 * Permission is hereby granted, free of charge, to any person obtaining a
tenvinc 3:7932917dea9d 177 * copy of this software and associated documentation files (the "Software"),
tenvinc 3:7932917dea9d 178 * to deal in the Software without restriction, including without limitation
tenvinc 3:7932917dea9d 179 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
tenvinc 3:7932917dea9d 180 * and/or sell copies of the Software, and to permit persons to whom the
tenvinc 3:7932917dea9d 181 * Software is furnished to do so, subject to the following conditions:
tenvinc 3:7932917dea9d 182 *
tenvinc 3:7932917dea9d 183 * The above copyright notice and this permission notice shall be included
tenvinc 3:7932917dea9d 184 * in all copies or substantial portions of the Software.
tenvinc 3:7932917dea9d 185 *
tenvinc 3:7932917dea9d 186 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
tenvinc 3:7932917dea9d 187 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tenvinc 3:7932917dea9d 188 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tenvinc 3:7932917dea9d 189 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
tenvinc 3:7932917dea9d 190 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
tenvinc 3:7932917dea9d 191 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
tenvinc 3:7932917dea9d 192 * OTHER DEALINGS IN THE SOFTWARE.
tenvinc 3:7932917dea9d 193 *
tenvinc 3:7932917dea9d 194 * Except as contained in this notice, the name of Maxim Integrated
tenvinc 3:7932917dea9d 195 * Products, Inc. shall not be used except as stated in the Maxim Integrated
tenvinc 3:7932917dea9d 196 * Products, Inc. Branding Policy.
tenvinc 3:7932917dea9d 197 *
tenvinc 3:7932917dea9d 198 * The mere transfer of this software does not imply any licenses
tenvinc 3:7932917dea9d 199 * of trade secrets, proprietary technology, copyrights, patents,
tenvinc 3:7932917dea9d 200 * trademarks, maskwork rights, or any other form of intellectual
tenvinc 3:7932917dea9d 201 * property whatsoever. Maxim Integrated Products, Inc. retains all
tenvinc 3:7932917dea9d 202 * ownership rights.
tenvinc 3:7932917dea9d 203 *******************************************************************************
tenvinc 3:7932917dea9d 204 */
tenvinc 3:7932917dea9d 205 #include "MAX30205.h"
tenvinc 3:7932917dea9d 206
tenvinc 3:7932917dea9d 207 //******************************************************************************
tenvinc 3:7932917dea9d 208 MAX30205::MAX30205(PinName sda, PinName scl, int slaveAddress) :
tenvinc 3:7932917dea9d 209 slaveAddress(slaveAddress) {
tenvinc 3:7932917dea9d 210 i2c = new I2C(sda, scl);
tenvinc 3:7932917dea9d 211 isOwner = true;
tenvinc 3:7932917dea9d 212 i2c->frequency(100000);
tenvinc 3:7932917dea9d 213 }
tenvinc 3:7932917dea9d 214
tenvinc 3:7932917dea9d 215 //******************************************************************************
tenvinc 3:7932917dea9d 216 MAX30205::MAX30205(I2C *i2c, int slaveAddress) : slaveAddress(slaveAddress) {
tenvinc 3:7932917dea9d 217 this->i2c = i2c;
tenvinc 3:7932917dea9d 218 i2c->frequency(100000);
tenvinc 3:7932917dea9d 219 isOwner = false;
tenvinc 3:7932917dea9d 220 }
tenvinc 3:7932917dea9d 221
tenvinc 3:7932917dea9d 222 //******************************************************************************
tenvinc 3:7932917dea9d 223 MAX30205::~MAX30205(void) {
tenvinc 3:7932917dea9d 224 if (isOwner == true) {
tenvinc 3:7932917dea9d 225 delete i2c;
tenvinc 3:7932917dea9d 226 }
tenvinc 3:7932917dea9d 227 }
tenvinc 3:7932917dea9d 228
tenvinc 3:7932917dea9d 229 //******************************************************************************
tenvinc 3:7932917dea9d 230 int MAX30205::reg_write(char reg, char value) {
tenvinc 3:7932917dea9d 231 int result;
tenvinc 3:7932917dea9d 232 char cmdData[2] = {(char)reg, value};
tenvinc 3:7932917dea9d 233 result = i2c->write(slaveAddress, cmdData, 2);
tenvinc 3:7932917dea9d 234 if (result != 0){
tenvinc 3:7932917dea9d 235 return -1;
tenvinc 3:7932917dea9d 236 }
tenvinc 3:7932917dea9d 237 return 0;
tenvinc 3:7932917dea9d 238 }
tenvinc 3:7932917dea9d 239
tenvinc 3:7932917dea9d 240 //******************************************************************************
tenvinc 3:7932917dea9d 241 int MAX30205::reg_write16(char reg, uint16_t value) {
tenvinc 3:7932917dea9d 242 int result;
tenvinc 3:7932917dea9d 243 char hi = (value >> 8) & 0xFF;
tenvinc 3:7932917dea9d 244 char lo = value & 0xFF;
tenvinc 3:7932917dea9d 245 char cmdData[3] = {reg, hi, lo};
tenvinc 3:7932917dea9d 246 result = i2c->write(slaveAddress, cmdData, 3);
tenvinc 3:7932917dea9d 247 if (result != 0) {
tenvinc 3:7932917dea9d 248 return -1;
tenvinc 3:7932917dea9d 249 }
tenvinc 3:7932917dea9d 250 return 0;
tenvinc 3:7932917dea9d 251 }
tenvinc 3:7932917dea9d 252
tenvinc 3:7932917dea9d 253 //******************************************************************************
tenvinc 3:7932917dea9d 254 int MAX30205::reg_read(char reg, char *value) {
tenvinc 3:7932917dea9d 255 int result;
tenvinc 3:7932917dea9d 256 char cmdData[1] = {reg};
tenvinc 3:7932917dea9d 257
tenvinc 3:7932917dea9d 258 result = i2c->write(slaveAddress, cmdData, 1);
tenvinc 3:7932917dea9d 259 if (result != 0) {
tenvinc 3:7932917dea9d 260 return -1;
tenvinc 3:7932917dea9d 261 }
tenvinc 3:7932917dea9d 262 result = i2c->read(slaveAddress, value, 1);
tenvinc 3:7932917dea9d 263 if (result != 0){
tenvinc 3:7932917dea9d 264 return -1;
tenvinc 3:7932917dea9d 265 }
tenvinc 3:7932917dea9d 266 return 0;
tenvinc 3:7932917dea9d 267 }
tenvinc 3:7932917dea9d 268
tenvinc 3:7932917dea9d 269 //******************************************************************************
tenvinc 3:7932917dea9d 270 int MAX30205::reg_read16(char reg, uint16_t *value) {
tenvinc 3:7932917dea9d 271 int result;
tenvinc 3:7932917dea9d 272 char data[2];
tenvinc 3:7932917dea9d 273 char cmdData[1] = {reg};
tenvinc 3:7932917dea9d 274 result = i2c->write(slaveAddress, cmdData, 1);
tenvinc 3:7932917dea9d 275 if (result != 0) {
tenvinc 3:7932917dea9d 276 return -1;
tenvinc 3:7932917dea9d 277 }
tenvinc 3:7932917dea9d 278 result = i2c->read(slaveAddress, data, 2);
tenvinc 3:7932917dea9d 279 if (result != 0) {
tenvinc 3:7932917dea9d 280 return -1;
tenvinc 3:7932917dea9d 281 }
tenvinc 3:7932917dea9d 282 *value = (data[0] << 8) + data[1];
tenvinc 3:7932917dea9d 283 return 0;
tenvinc 3:7932917dea9d 284 }
tenvinc 3:7932917dea9d 285
tenvinc 3:7932917dea9d 286 //******************************************************************************
tenvinc 3:7932917dea9d 287 int MAX30205::readTemperature(uint16_t *value) {
tenvinc 3:7932917dea9d 288 uint8_t data[2];
tenvinc 3:7932917dea9d 289 int status;
tenvinc 3:7932917dea9d 290 status = reg_read16(MAX30205_Temperature, (uint16_t *)&data);
tenvinc 3:7932917dea9d 291 *value = (data[0] << 8) + data[1];
tenvinc 3:7932917dea9d 292 return status;
tenvinc 3:7932917dea9d 293 }
tenvinc 3:7932917dea9d 294
tenvinc 3:7932917dea9d 295 //******************************************************************************
tenvinc 3:7932917dea9d 296 float MAX30205::toCelsius(unsigned int rawTemp) {
tenvinc 3:7932917dea9d 297 float val;
tenvinc 3:7932917dea9d 298 float val1, val2;
tenvinc 3:7932917dea9d 299 val1 = (float)(rawTemp >> 8);
tenvinc 3:7932917dea9d 300 val2 = (float)(rawTemp & 0xFF);
tenvinc 3:7932917dea9d 301 val = val2 + (val1 / 256.0f);
tenvinc 3:7932917dea9d 302 return val;
tenvinc 3:7932917dea9d 303 }
tenvinc 3:7932917dea9d 304
tenvinc 3:7932917dea9d 305 //******************************************************************************
tenvinc 3:7932917dea9d 306 float MAX30205::toFahrenheit(float temperatureC) {
tenvinc 3:7932917dea9d 307 return temperatureC * 9.0f / 5.0f + 32.0f;
tenvinc 3:7932917dea9d 308 }
tenvinc 3:7932917dea9d 309
tenvinc 3:7932917dea9d 310 //******************************************************************************
tenvinc 3:7932917dea9d 311 int MAX30205::reg_THYST_Read(uint16_t *value) {
tenvinc 3:7932917dea9d 312 return reg_read16(MAX30205_THYST, value);
tenvinc 3:7932917dea9d 313 }
tenvinc 3:7932917dea9d 314
tenvinc 3:7932917dea9d 315 //******************************************************************************
tenvinc 3:7932917dea9d 316 int MAX30205::reg_THYST_Write(uint16_t value) {
tenvinc 3:7932917dea9d 317 return reg_write16(MAX30205_THYST, value);
tenvinc 3:7932917dea9d 318 }