MAX14690 Library

Dependents:   MAX32630HSP3_IMU_HelloWorld MAX32630HSP3_IMU_HelloWorld max32630fthr max4146x_comp ... more

Fork of MAX14690 by Greg Steiert

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MAX14690.cpp Source File

MAX14690.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 "MAX14690.h"
00035 
00036 //******************************************************************************
00037 MAX14690::MAX14690(I2C *i2c): _i2c(i2c)
00038 {
00039     resetToDefaults();
00040 }
00041 
00042 //******************************************************************************
00043 MAX14690::~MAX14690()
00044 {
00045 }
00046 
00047 //******************************************************************************
00048 void MAX14690::resetToDefaults()
00049 {
00050     intEnThermStatus = false;
00051     intEnChgStatus = false;
00052     intEnILim = false;
00053     intEnUSBOVP = false;
00054     intEnUSBOK = false;
00055     intEnChgThmSD = false;
00056     intEnThermReg = false;
00057     intEnChgTimeOut = false;
00058     intEnThermBuck1 = false;
00059     intEnThermBuck2 = false;
00060     intEnThermLDO1 = false;
00061     intEnThermLDO2 = false;
00062     intEnThermLDO3 = false;
00063     iLimCntl = ILIM_500mA;
00064     chgAutoStp = true;
00065     chgAutoReSta = true;
00066     batReChg = BAT_RECHG_120mV;
00067     batReg = BAT_REG_4200mV;
00068     chgEn = true;
00069     vPChg = VPCHG_3000mV;
00070     iPChg = IPCHG_10;
00071     chgDone = CHGDONE_10;
00072     mtChgTmr = MTCHGTMR_0min;
00073     fChgTmr = FCHGTMR_300min;
00074     pChgTmr = PCHGTMR_60min;
00075     buck1Md = BUCK_BURST;
00076     buck1Ind = 0;
00077     buck2Md = BUCK_BURST;
00078     buck2Ind = 0;
00079     ldo2Mode = LDO_DISABLED;
00080     ldo2Millivolts = 3200;
00081     ldo3Mode = LDO_DISABLED;
00082     ldo3Millivolts = 3000;
00083     thrmCfg = THRM_ENABLED;
00084     monRatio = MON_DIV4;
00085     monCfg = MON_PULLDOWN;
00086     buck2ActDsc = false;
00087     buck2FFET = false;
00088     buck1ActDsc = false;
00089     buck1FFET = false;
00090     pfnResEna = true;
00091     stayOn = true;
00092 }
00093 
00094 //******************************************************************************
00095 int MAX14690::ldo2SetMode(ldoMode_t mode)
00096 {
00097     ldo2Mode = mode;
00098     return writeReg(REG_LDO2_CFG, mode);
00099 }
00100 
00101 //******************************************************************************
00102 int MAX14690::ldo2SetVoltage(int mV)
00103 {
00104     int regBits = mv2bits(mV);
00105     char data;
00106 
00107     if (regBits < 0) {
00108         return MAX14690_ERROR;
00109     } else {
00110         data = regBits;
00111     }
00112 
00113     if (ldo2Mode == LDO_ENABLED) {
00114         if (writeReg(REG_LDO2_CFG, LDO_DISABLED) != MAX14690_NO_ERROR) {
00115             return MAX14690_ERROR;
00116         }
00117     }
00118 
00119     if (writeReg(REG_LDO2_VSET, data) != MAX14690_NO_ERROR) {
00120         return MAX14690_ERROR;
00121     }
00122 
00123     if (ldo2Mode == LDO_ENABLED) {
00124         if (writeReg(REG_LDO2_CFG, LDO_ENABLED) != MAX14690_NO_ERROR) {
00125             return MAX14690_ERROR;
00126         }
00127     }
00128 
00129     return MAX14690_NO_ERROR;
00130 }
00131 
00132 //******************************************************************************
00133 int MAX14690::ldo3SetMode(ldoMode_t mode)
00134 {
00135     ldo3Mode = mode;
00136     return writeReg(REG_LDO3_CFG, mode);
00137 }
00138 
00139 //******************************************************************************
00140 int MAX14690::ldo3SetVoltage(int mV)
00141 {
00142     int regBits = mv2bits(mV);
00143     char data;
00144 
00145     if (regBits < 0) {
00146         return MAX14690_ERROR;
00147     } else {
00148         data = regBits;
00149     }
00150 
00151     if (ldo3Mode == LDO_ENABLED) {
00152         if (writeReg(REG_LDO3_CFG, LDO_DISABLED) != MAX14690_NO_ERROR) {
00153             return MAX14690_ERROR;
00154         }
00155     }
00156 
00157     if (writeReg(REG_LDO3_VSET, data) != MAX14690_NO_ERROR) {
00158         return MAX14690_ERROR;
00159     }
00160 
00161     if (ldo3Mode == LDO_ENABLED) {
00162         if (writeReg(REG_LDO3_CFG, LDO_ENABLED) != MAX14690_NO_ERROR) {
00163             return MAX14690_ERROR;
00164         }
00165     }
00166 
00167     return MAX14690_NO_ERROR;
00168 }
00169 
00170 //******************************************************************************
00171 int MAX14690::init()
00172 {
00173     int regBits;
00174     char data;
00175 
00176     // Configure buck regulators
00177     data = (buck1Md << 1) |
00178            (buck1Ind);
00179     if (writeReg(REG_BUCK1_CFG, data) != MAX14690_NO_ERROR) {
00180         return MAX14690_ERROR;
00181     }
00182     data = (buck2Md << 1) |
00183            (buck2Ind);
00184     if (writeReg(REG_BUCK2_CFG, data) != MAX14690_NO_ERROR) {
00185         return MAX14690_ERROR;
00186     }
00187     data = (buck2ActDsc << 5) |
00188            (buck2FFET << 4) |
00189            (buck1ActDsc << 1) |
00190            (buck1FFET);
00191     if (writeReg(REG_BUCK_EXTRA, data) != MAX14690_NO_ERROR) {
00192         return MAX14690_ERROR;
00193     }
00194 
00195     // Configure Charger
00196     data = (iLimCntl);
00197     if (writeReg(REG_I_LIM_CNTL, data) != MAX14690_NO_ERROR) {
00198         return MAX14690_ERROR;
00199     }
00200     data = (vPChg << 4) |
00201            (iPChg << 2) |
00202            (chgDone);
00203     if (writeReg(REG_CHG_CNTL_B, data) != MAX14690_NO_ERROR) {
00204         return MAX14690_ERROR;
00205     }
00206     data = (mtChgTmr << 4) |
00207            (fChgTmr << 2) |
00208            (pChgTmr);
00209     if (writeReg(REG_CHG_TMR, data) != MAX14690_NO_ERROR) {
00210         return MAX14690_ERROR;
00211     }
00212     data = (thrmCfg);
00213     if (writeReg(REG_THRM_CFG, data) != MAX14690_NO_ERROR) {
00214         return MAX14690_ERROR;
00215     }
00216     // Set enable bit after setting other charger bits
00217     data = (chgAutoStp << 7) |
00218            (chgAutoReSta << 6) |
00219            (batReChg << 4) |
00220            (batReg << 1) |
00221            (chgEn);
00222     if (writeReg(REG_CHG_CNTL_A, data) != MAX14690_NO_ERROR) {
00223         return MAX14690_ERROR;
00224     }
00225 
00226     // Configure monitor multiplexer
00227     data = (monRatio << 4) |
00228            (monCfg);
00229     if (writeReg(REG_MON_CFG, data) != MAX14690_NO_ERROR) {
00230         return MAX14690_ERROR;
00231     }
00232 
00233     // Configure and enable LDOs
00234     regBits = mv2bits(ldo2Millivolts);
00235     if (regBits < 0) {
00236         return MAX14690_ERROR;
00237     } else {
00238         data = regBits;
00239     }
00240     if (writeReg(REG_LDO2_VSET, data) != MAX14690_NO_ERROR) {
00241         return MAX14690_ERROR;
00242     }
00243     data = (ldo2Mode);
00244     if (writeReg(REG_LDO2_CFG, data) != MAX14690_NO_ERROR) {
00245         return MAX14690_ERROR;
00246     }
00247     regBits = mv2bits(ldo3Millivolts);
00248     if (regBits < 0) {
00249         return MAX14690_ERROR;
00250     } else {
00251         data = regBits;
00252     }
00253     if (writeReg(REG_LDO3_VSET, data) != MAX14690_NO_ERROR) {
00254         return MAX14690_ERROR;
00255     }
00256     data = (ldo3Mode);
00257     if (writeReg(REG_LDO3_CFG, data) != MAX14690_NO_ERROR) {
00258         return MAX14690_ERROR;
00259     }
00260     
00261     // Set stayOn bit after other registers to confirm successful boot.
00262     data = (pfnResEna << 7) |
00263            (stayOn);
00264     if (writeReg(REG_PWR_CFG, data) != MAX14690_NO_ERROR) {
00265         return MAX14690_ERROR;
00266     }
00267 
00268     // Unmask Interrupts Last
00269     data = (intEnThermStatus << 7) |
00270            (intEnChgStatus << 6) |
00271            (intEnILim << 5) |
00272            (intEnUSBOVP << 4) |
00273            (intEnUSBOK << 3) |
00274            (intEnChgThmSD << 2) |
00275            (intEnThermReg << 1) |
00276            (intEnChgTimeOut);
00277     if (writeReg(REG_INT_MASK_A, data) != MAX14690_NO_ERROR) {
00278         return MAX14690_ERROR;
00279     }
00280     data = (intEnThermBuck1 << 4) |
00281            (intEnThermBuck2 << 3) |
00282            (intEnThermLDO1 << 2) |
00283            (intEnThermLDO2 << 1) |
00284            (intEnThermLDO3);
00285     if (writeReg(REG_INT_MASK_B, data) != MAX14690_NO_ERROR) {
00286         return MAX14690_ERROR;
00287     }
00288     
00289     return MAX14690_NO_ERROR;
00290 }
00291 
00292 //******************************************************************************
00293 int MAX14690::monSet(monCfg_t newMonCfg, monRatio_t newMonRatio)
00294 {
00295     char data = (newMonRatio << 4) | (newMonCfg);
00296     monCfg = newMonCfg;
00297     monRatio = newMonRatio;
00298     return writeReg(REG_MON_CFG, data);
00299 }
00300 
00301 //******************************************************************************
00302 int MAX14690::shutdown()
00303 {
00304     return writeReg(REG_PWR_OFF, 0xB2);
00305 }
00306 
00307 
00308 //******************************************************************************
00309 int MAX14690::writeReg(registers_t reg,  char value)
00310 {
00311     char cmdData[2] = { (char)reg, value };
00312 
00313     if ((*_i2c).write(MAX14690_I2C_ADDR, cmdData, sizeof(cmdData)) != 0) {
00314         return MAX14690_ERROR;
00315     }
00316 
00317     return MAX14690_NO_ERROR;
00318 }
00319 
00320 //******************************************************************************
00321 int MAX14690::readReg(registers_t reg, char *value)
00322 {
00323     char cmdData[1] = { (char)reg };
00324 
00325     if ((*_i2c).write(MAX14690_I2C_ADDR, cmdData, sizeof(cmdData)) != 0) {
00326         return MAX14690_ERROR;
00327     }
00328 
00329     if ((*_i2c).read(MAX14690_I2C_ADDR, value, 1) != 0) {
00330         return MAX14690_ERROR;
00331     }
00332 
00333     return MAX14690_NO_ERROR;
00334 }
00335 
00336 //******************************************************************************
00337 int MAX14690::mv2bits(int mV)
00338 {
00339     int regBits;
00340 
00341     if ((MAX14690_LDO_MIN_MV <= mV) && (mV <= MAX14690_LDO_MAX_MV)) {
00342         regBits = (mV - MAX14690_LDO_MIN_MV) / MAX14690_LDO_STEP_MV;
00343     } else {
00344         return -1;
00345     }
00346 
00347     return regBits;
00348 }