Vincent Neo / Mbed 2 deprecated CS3237_Project

Dependencies:   mbed

Committer:
tenvinc
Date:
Mon Sep 30 12:51:34 2019 +0000
Revision:
0:864a1ca8699e
initial commit;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
tenvinc 0:864a1ca8699e 1 /*******************************************************************************
tenvinc 0:864a1ca8699e 2 * Copyright (C) 2016 Maxim Integrated Products, Inc., All Rights Reserved.
tenvinc 0:864a1ca8699e 3 *
tenvinc 0:864a1ca8699e 4 * Permission is hereby granted, free of charge, to any person obtaining a
tenvinc 0:864a1ca8699e 5 * copy of this software and associated documentation files (the "Software"),
tenvinc 0:864a1ca8699e 6 * to deal in the Software without restriction, including without limitation
tenvinc 0:864a1ca8699e 7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
tenvinc 0:864a1ca8699e 8 * and/or sell copies of the Software, and to permit persons to whom the
tenvinc 0:864a1ca8699e 9 * Software is furnished to do so, subject to the following conditions:
tenvinc 0:864a1ca8699e 10 *
tenvinc 0:864a1ca8699e 11 * The above copyright notice and this permission notice shall be included
tenvinc 0:864a1ca8699e 12 * in all copies or substantial portions of the Software.
tenvinc 0:864a1ca8699e 13 *
tenvinc 0:864a1ca8699e 14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
tenvinc 0:864a1ca8699e 15 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tenvinc 0:864a1ca8699e 16 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tenvinc 0:864a1ca8699e 17 * IN NO EVENT SHALL MAXIM INTEGRATED BE LIABLE FOR ANY CLAIM, DAMAGES
tenvinc 0:864a1ca8699e 18 * OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
tenvinc 0:864a1ca8699e 19 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
tenvinc 0:864a1ca8699e 20 * OTHER DEALINGS IN THE SOFTWARE.
tenvinc 0:864a1ca8699e 21 *
tenvinc 0:864a1ca8699e 22 * Except as contained in this notice, the name of Maxim Integrated
tenvinc 0:864a1ca8699e 23 * Products, Inc. shall not be used except as stated in the Maxim Integrated
tenvinc 0:864a1ca8699e 24 * Products, Inc. Branding Policy.
tenvinc 0:864a1ca8699e 25 *
tenvinc 0:864a1ca8699e 26 * The mere transfer of this software does not imply any licenses
tenvinc 0:864a1ca8699e 27 * of trade secrets, proprietary technology, copyrights, patents,
tenvinc 0:864a1ca8699e 28 * trademarks, maskwork rights, or any other form of intellectual
tenvinc 0:864a1ca8699e 29 * property whatsoever. Maxim Integrated Products, Inc. retains all
tenvinc 0:864a1ca8699e 30 * ownership rights.
tenvinc 0:864a1ca8699e 31 *******************************************************************************
tenvinc 0:864a1ca8699e 32 */
tenvinc 0:864a1ca8699e 33
tenvinc 0:864a1ca8699e 34 #include "MAX14720.h"
tenvinc 0:864a1ca8699e 35
tenvinc 0:864a1ca8699e 36 //******************************************************************************
tenvinc 0:864a1ca8699e 37 MAX14720::MAX14720(PinName sda, PinName scl, int slaveAddress) :
tenvinc 0:864a1ca8699e 38 slaveAddress(slaveAddress)
tenvinc 0:864a1ca8699e 39 {
tenvinc 0:864a1ca8699e 40 i2c = new I2C(sda,scl);
tenvinc 0:864a1ca8699e 41 isOwner = true;
tenvinc 0:864a1ca8699e 42 clkDivEn = false;
tenvinc 0:864a1ca8699e 43 clkDivSet = 0;
tenvinc 0:864a1ca8699e 44 boostISet = BOOST_ISET_100mA;
tenvinc 0:864a1ca8699e 45 boostMillivolts = 3300;
tenvinc 0:864a1ca8699e 46 boostEn = BOOST_DISABLED;
tenvinc 0:864a1ca8699e 47 boostEMI = false;
tenvinc 0:864a1ca8699e 48 boostInd = false;
tenvinc 0:864a1ca8699e 49 boostHysOff = false;
tenvinc 0:864a1ca8699e 50 boostPasDsc = false;
tenvinc 0:864a1ca8699e 51 boostActDsc = false;
tenvinc 0:864a1ca8699e 52 buckMd = BUCK_BURST;
tenvinc 0:864a1ca8699e 53 buckFst = false;
tenvinc 0:864a1ca8699e 54 buckISet = BUCK_ISET_300mA;
tenvinc 0:864a1ca8699e 55 buckCfg = false;
tenvinc 0:864a1ca8699e 56 buckInd = false;
tenvinc 0:864a1ca8699e 57 buckHysOff = true;
tenvinc 0:864a1ca8699e 58 buckMinOT = true;
tenvinc 0:864a1ca8699e 59 buckInteg = true;
tenvinc 0:864a1ca8699e 60 buckPasDsc = false;
tenvinc 0:864a1ca8699e 61 buckActDsc = false;
tenvinc 0:864a1ca8699e 62 buckFScl = false;
tenvinc 0:864a1ca8699e 63 }
tenvinc 0:864a1ca8699e 64 //******************************************************************************
tenvinc 0:864a1ca8699e 65 MAX14720::MAX14720(I2C *i2c, int slaveAddress) :
tenvinc 0:864a1ca8699e 66 slaveAddress(slaveAddress)
tenvinc 0:864a1ca8699e 67 {
tenvinc 0:864a1ca8699e 68 this->i2c = i2c;
tenvinc 0:864a1ca8699e 69 isOwner = false;
tenvinc 0:864a1ca8699e 70 clkDivEn = false;
tenvinc 0:864a1ca8699e 71 clkDivSet = 0;
tenvinc 0:864a1ca8699e 72 boostISet = BOOST_ISET_100mA;
tenvinc 0:864a1ca8699e 73 boostMillivolts = 3300;
tenvinc 0:864a1ca8699e 74 boostEn = BOOST_DISABLED;
tenvinc 0:864a1ca8699e 75 boostEMI = false;
tenvinc 0:864a1ca8699e 76 boostInd = false;
tenvinc 0:864a1ca8699e 77 boostHysOff = false;
tenvinc 0:864a1ca8699e 78 boostPasDsc = false;
tenvinc 0:864a1ca8699e 79 boostActDsc = false;
tenvinc 0:864a1ca8699e 80 buckMd = BUCK_BURST;
tenvinc 0:864a1ca8699e 81 buckFst = false;
tenvinc 0:864a1ca8699e 82 buckISet = BUCK_ISET_300mA;
tenvinc 0:864a1ca8699e 83 buckCfg = false;
tenvinc 0:864a1ca8699e 84 buckInd = false;
tenvinc 0:864a1ca8699e 85 buckHysOff = true;
tenvinc 0:864a1ca8699e 86 buckMinOT = true;
tenvinc 0:864a1ca8699e 87 buckInteg = true;
tenvinc 0:864a1ca8699e 88 buckPasDsc = false;
tenvinc 0:864a1ca8699e 89 buckActDsc = false;
tenvinc 0:864a1ca8699e 90 buckFScl = false;
tenvinc 0:864a1ca8699e 91 }
tenvinc 0:864a1ca8699e 92
tenvinc 0:864a1ca8699e 93 //******************************************************************************
tenvinc 0:864a1ca8699e 94 MAX14720::~MAX14720()
tenvinc 0:864a1ca8699e 95 {
tenvinc 0:864a1ca8699e 96 if (isOwner == true) {
tenvinc 0:864a1ca8699e 97 delete i2c;
tenvinc 0:864a1ca8699e 98 }
tenvinc 0:864a1ca8699e 99 }
tenvinc 0:864a1ca8699e 100
tenvinc 0:864a1ca8699e 101 //******************************************************************************
tenvinc 0:864a1ca8699e 102 int MAX14720::boostSetMode(boostEn_t mode)
tenvinc 0:864a1ca8699e 103 {
tenvinc 0:864a1ca8699e 104 char data;
tenvinc 0:864a1ca8699e 105
tenvinc 0:864a1ca8699e 106 boostEn = mode;
tenvinc 0:864a1ca8699e 107 data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
tenvinc 0:864a1ca8699e 108 return writeReg(REG_BOOST_CFG, data);
tenvinc 0:864a1ca8699e 109 }
tenvinc 0:864a1ca8699e 110
tenvinc 0:864a1ca8699e 111 //******************************************************************************
tenvinc 0:864a1ca8699e 112 int MAX14720::boostSetVoltage(int mV)
tenvinc 0:864a1ca8699e 113 {
tenvinc 0:864a1ca8699e 114 char data;
tenvinc 0:864a1ca8699e 115
tenvinc 0:864a1ca8699e 116 if ((MAX14720_BOOST_MIN_MV <= mV) && (mV <= MAX14720_BOOST_MAX_MV)) {
tenvinc 0:864a1ca8699e 117 boostMillivolts = mV;
tenvinc 0:864a1ca8699e 118 data = (mV - MAX14720_BOOST_MIN_MV) / MAX14720_BOOST_STEP_MV;
tenvinc 0:864a1ca8699e 119 } else {
tenvinc 0:864a1ca8699e 120 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 121 }
tenvinc 0:864a1ca8699e 122
tenvinc 0:864a1ca8699e 123 if (boostEn == BOOST_ENABLED) {
tenvinc 0:864a1ca8699e 124 if (writeReg(REG_BOOST_CFG, 0x00) != MAX14720_NO_ERROR) {
tenvinc 0:864a1ca8699e 125 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 126 }
tenvinc 0:864a1ca8699e 127 }
tenvinc 0:864a1ca8699e 128
tenvinc 0:864a1ca8699e 129 if (writeReg(REG_BOOST_VSET, data) != MAX14720_NO_ERROR) {
tenvinc 0:864a1ca8699e 130 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 131 }
tenvinc 0:864a1ca8699e 132
tenvinc 0:864a1ca8699e 133 if (boostEn == BOOST_ENABLED) {
tenvinc 0:864a1ca8699e 134 data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
tenvinc 0:864a1ca8699e 135 if (writeReg(REG_BOOST_CFG, data) != MAX14720_NO_ERROR) {
tenvinc 0:864a1ca8699e 136 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 137 }
tenvinc 0:864a1ca8699e 138 }
tenvinc 0:864a1ca8699e 139
tenvinc 0:864a1ca8699e 140 return MAX14720_NO_ERROR;
tenvinc 0:864a1ca8699e 141 }
tenvinc 0:864a1ca8699e 142
tenvinc 0:864a1ca8699e 143 //******************************************************************************
tenvinc 0:864a1ca8699e 144 int MAX14720::init()
tenvinc 0:864a1ca8699e 145 {
tenvinc 0:864a1ca8699e 146 char data;
tenvinc 0:864a1ca8699e 147
tenvinc 0:864a1ca8699e 148 data = (clkDivEn << 7) | (clkDivSet);
tenvinc 0:864a1ca8699e 149 if (writeReg(REG_BOOST_CDIV, data) != MAX14720_NO_ERROR) {
tenvinc 0:864a1ca8699e 150 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 151 }
tenvinc 0:864a1ca8699e 152
tenvinc 0:864a1ca8699e 153 data = (boostISet);
tenvinc 0:864a1ca8699e 154 if (writeReg(REG_BOOST_ISET, data) != MAX14720_NO_ERROR) {
tenvinc 0:864a1ca8699e 155 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 156 }
tenvinc 0:864a1ca8699e 157
tenvinc 0:864a1ca8699e 158 if ((MAX14720_BOOST_MIN_MV <= boostMillivolts) &&
tenvinc 0:864a1ca8699e 159 (boostMillivolts <= MAX14720_BOOST_MAX_MV)) {
tenvinc 0:864a1ca8699e 160 data = (boostMillivolts - MAX14720_BOOST_MIN_MV) / MAX14720_BOOST_STEP_MV;
tenvinc 0:864a1ca8699e 161 if (writeReg(REG_BOOST_VSET, data) != MAX14720_NO_ERROR) {
tenvinc 0:864a1ca8699e 162 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 163 }
tenvinc 0:864a1ca8699e 164 } else {
tenvinc 0:864a1ca8699e 165 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 166 }
tenvinc 0:864a1ca8699e 167
tenvinc 0:864a1ca8699e 168 data = (buckMd << 1) | (buckFst);
tenvinc 0:864a1ca8699e 169 if (writeReg(REG_BUCK_CFG, data) != MAX14720_NO_ERROR) {
tenvinc 0:864a1ca8699e 170 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 171 }
tenvinc 0:864a1ca8699e 172
tenvinc 0:864a1ca8699e 173 data = (boostHysOff << 7) | (boostPasDsc << 6) | (boostActDsc << 5) |
tenvinc 0:864a1ca8699e 174 (buckPasDsc << 2) | (buckActDsc << 1) | (buckFScl);
tenvinc 0:864a1ca8699e 175 if (writeReg(REG_BBB_EXTRA, data) != MAX14720_NO_ERROR) {
tenvinc 0:864a1ca8699e 176 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 177 }
tenvinc 0:864a1ca8699e 178
tenvinc 0:864a1ca8699e 179 // Write Boost Enable Register Last
tenvinc 0:864a1ca8699e 180 data = (boostEn << 3) | (boostEMI << 1) | (boostInd);
tenvinc 0:864a1ca8699e 181 if (writeReg(REG_BOOST_CFG, data) != MAX14720_NO_ERROR) {
tenvinc 0:864a1ca8699e 182 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 183 }
tenvinc 0:864a1ca8699e 184
tenvinc 0:864a1ca8699e 185 return MAX14720_NO_ERROR;
tenvinc 0:864a1ca8699e 186 }
tenvinc 0:864a1ca8699e 187
tenvinc 0:864a1ca8699e 188 //******************************************************************************
tenvinc 0:864a1ca8699e 189 int MAX14720::monSet(monCfg_t monCfg)
tenvinc 0:864a1ca8699e 190 {
tenvinc 0:864a1ca8699e 191 return writeReg(REG_MON_CFG, monCfg);
tenvinc 0:864a1ca8699e 192 }
tenvinc 0:864a1ca8699e 193
tenvinc 0:864a1ca8699e 194 //******************************************************************************
tenvinc 0:864a1ca8699e 195 int MAX14720::shutdown()
tenvinc 0:864a1ca8699e 196 {
tenvinc 0:864a1ca8699e 197 return writeReg(REG_PWR_OFF, 0xB2);
tenvinc 0:864a1ca8699e 198 }
tenvinc 0:864a1ca8699e 199
tenvinc 0:864a1ca8699e 200
tenvinc 0:864a1ca8699e 201 //******************************************************************************
tenvinc 0:864a1ca8699e 202 int MAX14720::writeReg(registers_t reg, char value)
tenvinc 0:864a1ca8699e 203 {
tenvinc 0:864a1ca8699e 204 char cmdData[2] = { (char)reg, value };
tenvinc 0:864a1ca8699e 205
tenvinc 0:864a1ca8699e 206 if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) {
tenvinc 0:864a1ca8699e 207 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 208 }
tenvinc 0:864a1ca8699e 209
tenvinc 0:864a1ca8699e 210 return MAX14720_NO_ERROR;
tenvinc 0:864a1ca8699e 211 }
tenvinc 0:864a1ca8699e 212
tenvinc 0:864a1ca8699e 213 //******************************************************************************
tenvinc 0:864a1ca8699e 214 int MAX14720::readReg(registers_t reg, char *value)
tenvinc 0:864a1ca8699e 215 {
tenvinc 0:864a1ca8699e 216 char cmdData[1] = { (char)reg };
tenvinc 0:864a1ca8699e 217
tenvinc 0:864a1ca8699e 218 if (i2c->write(slaveAddress, cmdData, sizeof(cmdData)) != 0) {
tenvinc 0:864a1ca8699e 219 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 220 }
tenvinc 0:864a1ca8699e 221
tenvinc 0:864a1ca8699e 222 if (i2c->read(slaveAddress, value, 1) != 0) {
tenvinc 0:864a1ca8699e 223 return MAX14720_ERROR;
tenvinc 0:864a1ca8699e 224 }
tenvinc 0:864a1ca8699e 225
tenvinc 0:864a1ca8699e 226 return MAX14720_NO_ERROR;
tenvinc 0:864a1ca8699e 227 }