Maxim Integrated / MAX14720

Dependents:   HSP_PMIC_Demo HSP_SpO2 HSP_ECG HSP_ECG_LeadOFF_Detection

Fork of MAX14720 by Greg Steiert

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX14720.cpp Source File

MAX14720.cpp

00001 /*******************************************************************************
00002  * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
00003  *
00004  * Permission is hereby granted, free of charge, to any person obtaining a
00005  * copy of this software and associated documentation files (the "Software"),
00006  * to deal in the Software without restriction, including without limitation
00007  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
00008  * and/or sell copies of the Software, and to permit persons to whom the
00009  * Software is furnished to do so, subject to the following conditions:
00010  *
00011  * The above copyright notice and this permission notice shall be included
00012  * in all copies or substantial portions of the Software.
00013  *
00014  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00015  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00016  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
00017  * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
00018  * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
00019  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
00020  * OTHER DEALINGS IN THE SOFTWARE.
00021  *
00022  * Except as contained in this notice, the name of Maxim Integrated
00023  * Products, Inc. shall not be used except as stated in the Maxim Integrated
00024  * Products, Inc. Branding Policy.
00025  *
00026  * The mere transfer of this software does not imply any licenses
00027  * of trade secrets, proprietary technology, copyrights, patents,
00028  * trademarks, maskwork rights, or any other form of intellectual
00029  * property whatsoever. Maxim Integrated Products, Inc. retains all
00030  * ownership rights.
00031  *******************************************************************************
00032  */
00033 
00034 #include "MAX14720.h"
00035 
00036 //******************************************************************************
00037 MAX14720::MAX14720(PinName sda, PinName scl, int slaveAddress) :
00038     slaveAddress(slaveAddress)
00039 {
00040     i2c = new I2C(sda,scl);
00041     isOwner = true;
00042     clkDivEn = false;
00043     clkDivSet = 0;
00044     boostISet = BOOST_ISET_100mA;
00045     boostMillivolts = 3300;
00046     boostEn = BOOST_DISABLED;
00047     boostEMI = false;
00048     boostInd = false;
00049     boostHysOff = false;
00050     boostPasDsc = false;
00051     boostActDsc = false;
00052     buckMd = BUCK_BURST;
00053     buckFst = false;
00054     buckISet = BUCK_ISET_300mA;
00055     buckCfg = false;
00056     buckInd = false;
00057     buckHysOff = true;
00058     buckMinOT = true;
00059     buckInteg = true;
00060     buckPasDsc = false;
00061     buckActDsc = false;
00062     buckFScl = false;
00063 }
00064 //******************************************************************************
00065 MAX14720::MAX14720(I2C *i2c, int slaveAddress) :
00066     slaveAddress(slaveAddress)
00067 {
00068     this->i2c = i2c;
00069     isOwner = false;
00070     clkDivEn = false;
00071     clkDivSet = 0;
00072     boostISet = BOOST_ISET_100mA;
00073     boostMillivolts = 3300;
00074     boostEn = BOOST_DISABLED;
00075     boostEMI = false;
00076     boostInd = false;
00077     boostHysOff = false;
00078     boostPasDsc = false;
00079     boostActDsc = false;
00080     buckMd = BUCK_BURST;
00081     buckFst = false;
00082     buckISet = BUCK_ISET_300mA;
00083     buckCfg = false;
00084     buckInd = false;
00085     buckHysOff = true;
00086     buckMinOT = true;
00087     buckInteg = true;
00088     buckPasDsc = false;
00089     buckActDsc = false;
00090     buckFScl = false;
00091 }
00092 
00093 //******************************************************************************
00094 MAX14720::~MAX14720()
00095 {
00096     if (isOwner == true) {
00097         delete i2c;
00098     }
00099 }
00100 
00101 //******************************************************************************
00102 int MAX14720::boostSetMode(boostEn_t mode)
00103 {
00104     char data;
00105 
00106     boostEn = mode;
00107     data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
00108     return writeReg(REG_BOOST_CFG, data);
00109 }
00110 
00111 //******************************************************************************
00112 int MAX14720::boostSetVoltage(int mV)
00113 {
00114     char data;
00115 
00116     if ((MAX14720_BOOST_MIN_MV <= mV) && (mV <= MAX14720_BOOST_MAX_MV)) {
00117         boostMillivolts = mV;
00118         data = (mV - MAX14720_BOOST_MIN_MV) / MAX14720_BOOST_STEP_MV;
00119     } else {
00120         return MAX14720_ERROR;
00121     }
00122 
00123     if (boostEn == BOOST_ENABLED) {
00124         if (writeReg(REG_BOOST_CFG, 0x00) != MAX14720_NO_ERROR) {
00125             return MAX14720_ERROR;
00126         }
00127     }
00128 
00129     if (writeReg(REG_BOOST_VSET, data) != MAX14720_NO_ERROR) {
00130         return MAX14720_ERROR;
00131     }
00132 
00133     if (boostEn == BOOST_ENABLED) {
00134         data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
00135         if (writeReg(REG_BOOST_CFG, data) != MAX14720_NO_ERROR) {
00136             return MAX14720_ERROR;
00137         }
00138     }
00139 
00140     return MAX14720_NO_ERROR;
00141 }
00142 
00143 //******************************************************************************
00144 int MAX14720::init()
00145 {
00146     char data;
00147 
00148     data = (clkDivEn << 7) | (clkDivSet);
00149     if (writeReg(REG_BOOST_CDIV, data) != MAX14720_NO_ERROR) {
00150         return MAX14720_ERROR;
00151     }
00152 
00153     data = (boostISet);
00154     if (writeReg(REG_BOOST_ISET, data) != MAX14720_NO_ERROR) {
00155         return MAX14720_ERROR;
00156     }
00157 
00158     if ((MAX14720_BOOST_MIN_MV <= boostMillivolts) &&
00159         (boostMillivolts <= MAX14720_BOOST_MAX_MV)) {
00160         data = (boostMillivolts - MAX14720_BOOST_MIN_MV) / MAX14720_BOOST_STEP_MV;
00161         if (writeReg(REG_BOOST_VSET, data) != MAX14720_NO_ERROR) {
00162             return MAX14720_ERROR;
00163         }
00164     } else {
00165         return MAX14720_ERROR;
00166     }
00167 
00168     data = (buckMd << 1) | (buckFst);
00169     if (writeReg(REG_BUCK_CFG, data) != MAX14720_NO_ERROR) {
00170         return MAX14720_ERROR;
00171     }
00172 
00173     data = (boostHysOff << 7) | (boostPasDsc << 6) | (boostActDsc << 5) |
00174            (buckPasDsc << 2) | (buckActDsc << 1) | (buckFScl);
00175     if (writeReg(REG_BBB_EXTRA, data) != MAX14720_NO_ERROR) {
00176         return MAX14720_ERROR;
00177     }
00178 
00179     // Write Boost Enable Register Last
00180     data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
00181     if (writeReg(REG_BOOST_CFG, data) != MAX14720_NO_ERROR) {
00182         return MAX14720_ERROR;
00183     }
00184 
00185     return MAX14720_NO_ERROR;
00186 }
00187 
00188 //******************************************************************************
00189 int MAX14720::monSet(monCfg_t monCfg)
00190 {
00191     return writeReg(REG_MON_CFG, monCfg);
00192 }
00193 
00194 //******************************************************************************
00195 int MAX14720::shutdown()
00196 {
00197     return writeReg(REG_PWR_OFF, 0xB2);
00198 }
00199 
00200 
00201 //******************************************************************************
00202 int MAX14720::writeReg(registers_t reg,  char value)
00203 {
00204     char cmdData[2] = { (char)reg, value };
00205 
00206     if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) {
00207         return MAX14720_ERROR;
00208     }
00209 
00210     return MAX14720_NO_ERROR;
00211 }
00212 
00213 //******************************************************************************
00214 int MAX14720::readReg(registers_t reg, char *value)
00215 {
00216     char cmdData[1] = { (char)reg };
00217 
00218     if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) {
00219         return MAX14720_ERROR;
00220     }
00221 
00222     if (i2c->read(slaveAddress, value, 1) != 0) {
00223         return MAX14720_ERROR;
00224     }
00225 
00226     return MAX14720_NO_ERROR;
00227 }