Fork of MAX14690 library
Fork of MAX14690 by
Diff: MAX14720.cpp
- Revision:
- 8:2c3f2da51c5d
- Parent:
- 7:2e9f6e70b34f
--- a/MAX14720.cpp Thu Oct 06 21:33:25 2016 +0000 +++ b/MAX14720.cpp Wed Oct 19 22:32:56 2016 +0000 @@ -89,6 +89,7 @@ buckActDsc = false; buckFScl = false; } + //****************************************************************************** MAX14720::~MAX14720() { @@ -100,116 +101,127 @@ //****************************************************************************** int MAX14720::boostSetMode(boostEn_t mode) { - int result; char data; + boostEn = mode; data = (boostEn << 3) | (boostEMI << 1) | (boostInd); - result = writeReg(REG_BOOST_CFG, data); - if (result == MAX14720_ERROR) return result; - return 0; + return writeReg(REG_BOOST_CFG, data); } //****************************************************************************** int MAX14720::boostSetVoltage(int mV) { - int result; char data; - if ((MAX14720_BOOST_MIN_MV <= mV)&&(mV <= MAX14720_BOOST_MAX_MV)) { + + if ((MAX14720_BOOST_MIN_MV <= mV) && (mV <= MAX14720_BOOST_MAX_MV)) { boostMillivolts = mV; data = (mV - MAX14720_BOOST_MIN_MV) / MAX14720_BOOST_STEP_MV; } else { return MAX14720_ERROR; } + if (boostEn == BOOST_ENABLED) { - result = writeReg(REG_BOOST_CFG, 0x00); - if (result == MAX14720_ERROR) return result; + if (writeReg(REG_BOOST_CFG, 0x00) != MAX14720_NO_ERROR) { + return MAX14720_ERROR; + } } - result = writeReg(REG_BOOST_VSET, data); - if (result == MAX14720_ERROR) return result; + + if (writeReg(REG_BOOST_VSET, data) != MAX14720_NO_ERROR) { + return MAX14720_ERROR; + } + if (boostEn == BOOST_ENABLED) { data = (boostEn << 3) | (boostEMI << 1) | (boostInd); - result = writeReg(REG_BOOST_CFG, data); - if (result == MAX14720_ERROR) return result; + if (writeReg(REG_BOOST_CFG, data) != MAX14720_NO_ERROR) { + return MAX14720_ERROR; + } } - return 0; + + return MAX14720_NO_ERROR; } //****************************************************************************** int MAX14720::init() { - int result; char data; + data = (clkDivEn << 7) | (clkDivSet); - result = writeReg(REG_BOOST_CDIV, data); - if (result == MAX14720_ERROR) return result; + if (writeReg(REG_BOOST_CDIV, data) != MAX14720_NO_ERROR) { + return MAX14720_ERROR; + } + data = (boostISet); - result = writeReg(REG_BOOST_ISET, data); - if (result == MAX14720_ERROR) return result; - if ((MAX14720_BOOST_MIN_MV <= boostMillivolts)&&(boostMillivolts <= MAX14720_BOOST_MAX_MV)) { + if (writeReg(REG_BOOST_ISET, data) != MAX14720_NO_ERROR) { + return MAX14720_ERROR; + } + + if ((MAX14720_BOOST_MIN_MV <= boostMillivolts) && + (boostMillivolts <= MAX14720_BOOST_MAX_MV)) { data = (boostMillivolts - MAX14720_BOOST_MIN_MV) / MAX14720_BOOST_STEP_MV; + if (writeReg(REG_BOOST_VSET, data) != MAX14720_NO_ERROR) { + return MAX14720_ERROR; + } } else { return MAX14720_ERROR; } - result = writeReg(REG_BOOST_VSET, data); - if (result == MAX14720_ERROR) return result; + data = (buckMd << 1) | (buckFst); - result = writeReg(REG_BUCK_CFG, data); - if (result == MAX14720_ERROR) return result; - data = (boostHysOff << 7) | (boostPasDsc << 6) | (boostActDsc << 5) | (buckPasDsc << 2) | (buckActDsc << 1) | (buckFScl); - result = writeReg(REG_BBB_EXTRA, data); - if (result == MAX14720_ERROR) return result; + if (writeReg(REG_BUCK_CFG, data) != MAX14720_NO_ERROR) { + return MAX14720_ERROR; + } + + data = (boostHysOff << 7) | (boostPasDsc << 6) | (boostActDsc << 5) | + (buckPasDsc << 2) | (buckActDsc << 1) | (buckFScl); + if (writeReg(REG_BBB_EXTRA, data) != MAX14720_NO_ERROR) { + return MAX14720_ERROR; + } + // Write Boost Enable Register Last data = (boostEn << 3) | (boostEMI << 1) | (boostInd); - result = writeReg(REG_BOOST_CFG, data); - if (result == MAX14720_ERROR) return result; - return 0; + if (writeReg(REG_BOOST_CFG, data) != MAX14720_NO_ERROR) { + return MAX14720_ERROR; + } + + return MAX14720_NO_ERROR; } //****************************************************************************** int MAX14720::monSet(monCfg_t monCfg) { - int result; - result = writeReg(REG_MON_CFG, monCfg); - if (result == MAX14720_ERROR) return result; - return 0; + return writeReg(REG_MON_CFG, monCfg); } //****************************************************************************** int MAX14720::shutdown() { - int result; - result = writeReg(REG_PWR_OFF, 0xB2); - if (result == MAX14720_ERROR) return result; - return 0; + return writeReg(REG_PWR_OFF, 0xB2); } //****************************************************************************** int MAX14720::writeReg(registers_t reg, char value) { - int result; char cmdData[2] = { (char)reg, value }; - result = i2c->write(slaveAddress, cmdData, 2); - if (result != 0) return MAX14720_ERROR; + + if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) { + return MAX14720_ERROR; + } + return MAX14720_NO_ERROR; } //****************************************************************************** int MAX14720::readReg(registers_t reg, char *value) { - int result; char cmdData[1] = { (char)reg }; - result = i2c->write(slaveAddress, cmdData, 1); - if (result != 0) return MAX14720_ERROR; - result = i2c->read(slaveAddress, value, 1); - if (result != 0) return MAX14720_ERROR; + if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) { + return MAX14720_ERROR; + } + + if (i2c->read(slaveAddress, value, 1) != 0) { + return MAX14720_ERROR; + } + return MAX14720_NO_ERROR; } - - - - - - -