MAX14690 library

Fork of MAX14690 by Maxim Integrated

MAX14690.cpp

Committer:
switches
Date:
2016-11-10
Revision:
9:8fb54367ceb5
Parent:
MAX14720.cpp@ 8:2c3f2da51c5d
Child:
10:32c7e2ab67aa

File content as of revision 9:8fb54367ceb5:

/*******************************************************************************
 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included
 * in all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 * OTHER DEALINGS IN THE SOFTWARE.
 *
 * Except as contained in this notice, the name of Maxim Integrated
 * Products, Inc. shall not be used except as stated in the Maxim Integrated
 * Products, Inc. Branding Policy.
 *
 * The mere transfer of this software does not imply any licenses
 * of trade secrets, proprietary technology, copyrights, patents,
 * trademarks, maskwork rights, or any other form of intellectual
 * property whatsoever. Maxim Integrated Products, Inc. retains all
 * ownership rights.
 *******************************************************************************
 */

#include "MAX14690.h"

//******************************************************************************
MAX14690::MAX14690(PinName sda, PinName scl, int slaveAddress) :
    slaveAddress(slaveAddress)
{
    i2c = new I2C(sda,scl);
    isOwner = true;
    clkDivEn = false;
    clkDivSet = 0;
    boostISet = BOOST_ISET_100mA;
    boostMillivolts = 3300;
    boostEn = BOOST_DISABLED;
    boostEMI = false;
    boostInd = false;
    boostHysOff = false;
    boostPasDsc = false;
    boostActDsc = false;
    buckMd = BUCK_BURST;
    buckFst = false;
    buckISet = BUCK_ISET_300mA;
    buckCfg = false;
    buckInd = false;
    buckHysOff = true;
    buckMinOT = true;
    buckInteg = true;
    buckPasDsc = false;
    buckActDsc = false;
    buckFScl = false;
}
//******************************************************************************
MAX14690::MAX14690(I2C *i2c, int slaveAddress) :
    slaveAddress(slaveAddress)
{
    this->i2c = i2c;
    isOwner = false;
    clkDivEn = false;
    clkDivSet = 0;
    boostISet = BOOST_ISET_100mA;
    boostMillivolts = 3300;
    boostEn = BOOST_DISABLED;
    boostEMI = false;
    boostInd = false;
    boostHysOff = false;
    boostPasDsc = false;
    boostActDsc = false;
    buckMd = BUCK_BURST;
    buckFst = false;
    buckISet = BUCK_ISET_300mA;
    buckCfg = false;
    buckInd = false;
    buckHysOff = true;
    buckMinOT = true;
    buckInteg = true;
    buckPasDsc = false;
    buckActDsc = false;
    buckFScl = false;
}

//******************************************************************************
MAX14690::~MAX14690()
{
    if (isOwner == true) {
        delete i2c;
    }
}

//******************************************************************************
int MAX14690::boostSetMode(boostEn_t mode)
{
    char data;

    boostEn = mode;
    data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
    return writeReg(REG_BOOST_CFG, data);
}

//******************************************************************************
int MAX14690::boostSetVoltage(int mV)
{
    char data;

    if ((MAX14690_BOOST_MIN_MV <= mV) && (mV <= MAX14690_BOOST_MAX_MV)) {
        boostMillivolts = mV;
        data = (mV - MAX14690_BOOST_MIN_MV) / MAX14690_BOOST_STEP_MV;
    } else {
        return MAX14690_ERROR;
    }

    if (boostEn == BOOST_ENABLED) {
        if (writeReg(REG_BOOST_CFG, 0x00) != MAX14690_NO_ERROR) {
            return MAX14690_ERROR;
        }
    }

    if (writeReg(REG_BOOST_VSET, data) != MAX14690_NO_ERROR) {
        return MAX14690_ERROR;
    }

    if (boostEn == BOOST_ENABLED) {
        data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
        if (writeReg(REG_BOOST_CFG, data) != MAX14690_NO_ERROR) {
            return MAX14690_ERROR;
        }
    }

    return MAX14690_NO_ERROR;
}

//******************************************************************************
int MAX14690::init()
{
    char data;

    data = (clkDivEn << 7) | (clkDivSet);
    if (writeReg(REG_BOOST_CDIV, data) != MAX14690_NO_ERROR) {
        return MAX14690_ERROR;
    }

    data = (boostISet);
    if (writeReg(REG_BOOST_ISET, data) != MAX14690_NO_ERROR) {
        return MAX14690_ERROR;
    }

    if ((MAX14690_BOOST_MIN_MV <= boostMillivolts) &&
        (boostMillivolts <= MAX14690_BOOST_MAX_MV)) {
        data = (boostMillivolts - MAX14690_BOOST_MIN_MV) / MAX14690_BOOST_STEP_MV;
        if (writeReg(REG_BOOST_VSET, data) != MAX14690_NO_ERROR) {
            return MAX14690_ERROR;
        }
    } else {
        return MAX14690_ERROR;
    }

    data = (buckMd << 1) | (buckFst);
    if (writeReg(REG_BUCK_CFG, data) != MAX14690_NO_ERROR) {
        return MAX14690_ERROR;
    }

    data = (boostHysOff << 7) | (boostPasDsc << 6) | (boostActDsc << 5) |
           (buckPasDsc << 2) | (buckActDsc << 1) | (buckFScl);
    if (writeReg(REG_BBB_EXTRA, data) != MAX14690_NO_ERROR) {
        return MAX14690_ERROR;
    }

    // Write Boost Enable Register Last
    data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
    if (writeReg(REG_BOOST_CFG, data) != MAX14690_NO_ERROR) {
        return MAX14690_ERROR;
    }

    return MAX14690_NO_ERROR;
}

//******************************************************************************
int MAX14690::monSet(monCfg_t monCfg)
{
    return writeReg(REG_MON_CFG, monCfg);
}

//******************************************************************************
int MAX14690::shutdown()
{
    return writeReg(REG_PWR_OFF, 0xB2);
}


//******************************************************************************
int MAX14690::writeReg(registers_t reg,  char value)
{
    char cmdData[2] = { (char)reg, value };

    if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) {
        return MAX14690_ERROR;
    }

    return MAX14690_NO_ERROR;
}

//******************************************************************************
int MAX14690::readReg(registers_t reg, char *value)
{
    char cmdData[1] = { (char)reg };

    if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) {
        return MAX14690_ERROR;
    }

    if (i2c->read(slaveAddress, value, 1) != 0) {
        return MAX14690_ERROR;
    }

    return MAX14690_NO_ERROR;
}