Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
MAX20303.cpp
00001 /******************************************************************************* 00002 * Copyright (C) 2018 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 00035 #include "MAX20303.h" 00036 00037 00038 00039 //****************************************************************************** 00040 MAX20303::MAX20303(I2C *i2c): 00041 m_i2c(i2c), m_writeAddress(MAX20303_SLAVE_WR_ADDR), 00042 m_readAddress(MAX20303_SLAVE_RD_ADDR) 00043 { 00044 } 00045 00046 00047 //****************************************************************************** 00048 MAX20303::~MAX20303(void) 00049 { 00050 //empty block 00051 } 00052 00053 00054 //****************************************************************************** 00055 int MAX20303::LDO1Config() 00056 { 00057 int32_t ret = 0; 00058 uint8_t val; 00059 // ret |= writeReg(MAX20303::REG_AP_CMDOUT, 0x40); 00060 // ret |= writeReg(MAX20303::REG_AP_DATOUT0, 0x05); 00061 // ret |= writeReg(MAX20303::REG_AP_DATOUT1, 0x34); 00062 // 00063 // readReg(MAX20303::REG_AP_CMDOUT, val); 00064 // readReg(MAX20303::REG_AP_DATOUT0, val); 00065 // readReg(MAX20303::REG_AP_DATOUT1, val); 00066 appcmdoutvalue_ = 0x40; 00067 appdatainoutbuffer_[0] = 0x05; 00068 appdatainoutbuffer_[1] = 0x34; 00069 AppWrite(2); 00070 00071 return ret; 00072 } 00073 00074 //****************************************************************************** 00075 int MAX20303::LDO2Config() 00076 { 00077 int32_t ret = 0; 00078 uint8_t val; 00079 appcmdoutvalue_ = 0x42; 00080 appdatainoutbuffer_[0] = 0x01; 00081 appdatainoutbuffer_[1] = 0x15; // 0.9V + (0.1V * number) = 3V 00082 AppWrite(2); 00083 00084 return ret; 00085 } 00086 00087 00088 //****************************************************************************** 00089 int MAX20303::writeReg(registers_t reg, uint8_t value) 00090 { 00091 int32_t ret; 00092 00093 char cmdData[2] = {reg, value}; 00094 00095 ret = m_i2c->write(m_writeAddress, cmdData, sizeof(cmdData)); 00096 //printf("MAX20303 write reg[0x%X]=0x%X, ret=%d\r\n", (uint32_t)reg, value, ret) 00097 00098 if (ret != 0) 00099 return MAX20303_ERROR; 00100 00101 return MAX20303_NO_ERROR; 00102 } 00103 00104 00105 //****************************************************************************** 00106 int MAX20303::readReg(registers_t reg, uint8_t &value) 00107 { 00108 int32_t ret; 00109 00110 char data = reg; 00111 00112 ret = m_i2c->write(m_writeAddress, &data, sizeof(data)); 00113 if (ret != 0) { 00114 printf("%s - failed - ret: %d\n", __func__); 00115 return MAX20303_ERROR; 00116 } 00117 00118 ret = m_i2c->read(m_readAddress, &data, sizeof(data)); 00119 if (ret != 0) { 00120 printf("%s - failed - ret: %d\n", __func__); 00121 return MAX20303_ERROR; 00122 } 00123 00124 value = data; 00125 printf("MAX20303 read reg[0x%X]=0x%X, ret=%d\r\n", (uint32_t)reg, value, ret); 00126 return MAX20303_NO_ERROR; 00127 } 00128 00129 //****************************************************************************** 00130 int MAX20303::readRegMulti(registers_t reg, uint8_t *value, uint8_t len){ 00131 int32_t ret; 00132 char data = reg; 00133 00134 ret = m_i2c->write(m_writeAddress, &data, sizeof(data)); 00135 if (ret != 0) { 00136 printf("%s - failed - ret: %d\n", __func__); 00137 return MAX20303_ERROR; 00138 } 00139 00140 ret = m_i2c->read(m_readAddress, (char *)value, len); 00141 if (ret != 0) { 00142 printf("%s - failed - ret: %d\n", __func__); 00143 return MAX20303_ERROR; 00144 } 00145 00146 printf("MAX20303 read reg[0x%X]=0x%X, ret=%d\r\n", (uint32_t)reg, value, ret); 00147 return MAX20303_NO_ERROR; 00148 } 00149 00150 //****************************************************************************** 00151 int MAX20303::writeRegMulti(registers_t reg, uint8_t *value, uint8_t len){ 00152 int32_t ret; 00153 i2cbuffer_[0] = reg; 00154 memcpy(&i2cbuffer_[1], value, len); 00155 00156 ret = m_i2c->write(m_writeAddress, (char *)i2cbuffer_, (len+1)); 00157 //printf("MAX20303 write reg[0x%X]=0x%X, ret=%d\r\n", (uint32_t)reg, value, ret) 00158 00159 if (ret != 0) 00160 return MAX20303_ERROR; 00161 00162 return MAX20303_NO_ERROR; 00163 } 00164 //****************************************************************************** 00165 int MAX20303::mv2bits(int mV) 00166 { 00167 int regBits; 00168 00169 if (( MAX20303_LDO_MIN_MV <= mV) && (mV <= MAX20303_LDO_MAX_MV)) { 00170 regBits = (mV - MAX20303_LDO_MIN_MV) / MAX20303_LDO_STEP_MV; 00171 } else { 00172 return -1; 00173 } 00174 00175 return regBits; 00176 } 00177 //****************************************************************************** 00178 int MAX20303::PowerOffthePMIC(){ 00179 int ret; 00180 appdatainoutbuffer_[0] = 0xB2; 00181 appcmdoutvalue_ = 0x80; 00182 ret = AppWrite(1); 00183 00184 if(appcmdoutvalue_ != 0x80){ 00185 ret |= MAX20303_ERROR; 00186 } 00187 00188 return ret; 00189 } 00190 //****************************************************************************** 00191 int MAX20303::PowerOffDelaythePMIC(){ 00192 int ret; 00193 appdatainoutbuffer_[0] = 0xB2; 00194 appcmdoutvalue_ = 0x84; 00195 ret = AppWrite(1); 00196 00197 if(appcmdoutvalue_ != 0x80){ 00198 ret |= MAX20303_ERROR; 00199 } 00200 00201 return ret; 00202 } 00203 00204 //****************************************************************************** 00205 int MAX20303::SoftResetthePMIC(){ 00206 int ret; 00207 appdatainoutbuffer_[0] = 0xB3; 00208 appcmdoutvalue_ = 0x81; 00209 ret = AppWrite(1); 00210 00211 if(appcmdoutvalue_ != 0x81){ 00212 ret |= MAX20303_ERROR; 00213 } 00214 00215 return ret; 00216 } 00217 //****************************************************************************** 00218 int MAX20303::HardResetthePMIC(){ 00219 int ret; 00220 appdatainoutbuffer_[0] = 0xB4; 00221 appcmdoutvalue_ = 0x82; 00222 ret = AppWrite(1); 00223 00224 if(appcmdoutvalue_ != 0x82){ 00225 ret |= MAX20303_ERROR; 00226 } 00227 00228 return ret; 00229 } 00230 00231 //****************************************************************************** 00232 int MAX20303::AppWrite(uint8_t dataoutlen){ 00233 int ret; 00234 00235 ret = writeRegMulti(MAX20303::REG_AP_DATOUT0, appdatainoutbuffer_, dataoutlen); 00236 ret |= writeReg(MAX20303::REG_AP_CMDOUT, appcmdoutvalue_); 00237 wait_ms(10); 00238 ret |= readReg(MAX20303::REG_AP_RESPONSE, appcmdoutvalue_); 00239 00240 if(ret != 0) 00241 return MAX20303_ERROR; 00242 00243 return MAX20303_NO_ERROR; 00244 } 00245 00246 00247 //****************************************************************************** 00248 int MAX20303::AppRead(uint8_t datainlen){ 00249 int ret; 00250 00251 ret = writeReg(MAX20303::REG_AP_CMDOUT, appcmdoutvalue_); 00252 wait_ms(10); 00253 ret |= readRegMulti(MAX20303::REG_AP_RESPONSE, i2cbuffer_, datainlen); 00254 if(ret != 0) 00255 return MAX20303_ERROR; 00256 00257 return MAX20303_NO_ERROR; 00258 } 00259 00260 //****************************************************************************** 00261 char MAX20303::CheckPMICHWID(){ 00262 int ret; 00263 uint8_t value = 0x00; 00264 00265 ret = readReg(MAX20303::REG_HARDWARE_ID, value); 00266 if(ret != MAX20303_NO_ERROR) 00267 return false; 00268 00269 if(value == 0x02) 00270 return true; 00271 else 00272 return false; 00273 } 00274 00275 //****************************************************************************** 00276 int MAX20303::CheckPMICStatusRegisters(unsigned char buf_results[5]){ 00277 int ret; 00278 ret = readReg(MAX20303::REG_STATUS0, buf_results[0]); 00279 ret |= readReg(MAX20303::REG_STATUS1, buf_results[1]); 00280 ret |= readReg(MAX20303::REG_STATUS2, buf_results[2]); 00281 ret |= readReg(MAX20303::REG_STATUS3, buf_results[3]); 00282 ret |= readReg(MAX20303::REG_SYSTEM_ERROR, buf_results[4]); 00283 return ret; 00284 } 00285 00286 //****************************************************************************** 00287 int MAX20303::Max20303_BatteryGauge(unsigned char *batterylevel){ 00288 int ret; 00289 char data[2]; 00290 00291 data[0] = 0x04; 00292 ret = m_i2c->write(MAX20303_I2C_ADDR_FUEL_GAUGE, data, 1); 00293 if(ret != 0){ 00294 printf("Max20303_FuelGauge has failed\r\n"); 00295 } 00296 00297 ret = m_i2c->read(MAX20303_I2C_ADDR_FUEL_GAUGE | 1, data, 2); 00298 if(ret != 0){ 00299 printf("Max20303_FuelGauge has failed\r\n"); 00300 } 00301 00302 // if the level is more than 100 assume the battery is not connected 00303 if(data[0] > 100){ 00304 *batterylevel = 0; 00305 } else{ 00306 00307 *batterylevel = data[0]; 00308 } 00309 return 0; 00310 } 00311 00312 00313 //****************************************************************************** 00314 int MAX20303::led0on(char enable) { 00315 00316 if(enable) 00317 return writeReg(REG_LED0_DIRECT, 0x21); 00318 else 00319 return writeReg(REG_LED0_DIRECT, 0x01); 00320 } 00321 00322 //****************************************************************************** 00323 int MAX20303::led1on(char enable) { 00324 if(enable) 00325 return writeReg(REG_LED1_DIRECT, 0x21); 00326 else 00327 return writeReg(REG_LED1_DIRECT, 0x01); 00328 } 00329 00330 //****************************************************************************** 00331 int MAX20303::led2on(char enable) { 00332 if(enable) 00333 return writeReg(REG_LED2_DIRECT, 0x21); 00334 else 00335 return writeReg(REG_LED2_DIRECT, 0x01); 00336 } 00337 00338 00339 //****************************************************************************** 00340 int MAX20303::BoostEnable(void) { 00341 writeReg(REG_AP_DATOUT3, 0x00); // 00 : 5V 00342 writeReg(REG_AP_DATOUT0, 0x01); // Boost Enabled 00343 writeReg(REG_AP_CMDOUT, 0x30); 00344 return MAX20303_NO_ERROR; 00345 } 00346 00347 //****************************************************************************** 00348 int MAX20303::BuckBoostEnable(void) 00349 { 00350 int ret = 0; 00351 00352 ret |= writeReg( REG_AP_DATOUT0, 0x00); // Reserved = 0x00 00353 ret |= writeReg( REG_AP_DATOUT1, 0x04); // BBstlSet = 0b'100 Buck Boost Peak current Limit = 200mA 00354 ret |= writeReg( REG_AP_DATOUT2, 0x19); // BBstVSet = 0b'11001 Buck Boost Output Voltage = 5V 00355 ret |= writeReg( REG_AP_DATOUT3, 0x01); // BBstRipRed = 1 Ripple Reduction 00356 // BBstAct = 1 Actively discharged in Hard-Reset or Enable Low 00357 // BBstPas = 1 Passively discharged in Hard-Reset or Enable Low 00358 // BBstMd = 1 Damping Enabled 00359 // BBstInd = 0 Inductance is 4.7uH 00360 // BBstEn = 0b'01 Enabled 00361 ret |= writeReg( REG_AP_CMDOUT, 0x70); 00362 if (ret != 0) 00363 return MAX20303_ERROR; 00364 00365 return MAX20303_NO_ERROR; 00366 }
Generated on Thu Jul 14 2022 10:55:39 by
1.7.2