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.
Dependencies: max32630fthr Adafruit_FeatherOLED USBDevice
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 int 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 int 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__, ret); 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__, ret); 00121 return MAX20303_ERROR; 00122 } 00123 00124 value = data; 00125 printf("MAX20303 read reg[0x%X]=0x%X, ret=%d\r\n", (unsigned int)reg, (unsigned int)value, ret); 00126 return MAX20303_NO_ERROR; 00127 } 00128 00129 //****************************************************************************** 00130 int MAX20303::readRegMulti(registers_t reg, uint8_t *value, uint8_t len){ 00131 int 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__, ret); 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__, ret); 00143 return MAX20303_ERROR; 00144 } 00145 00146 printf("MAX20303 read reg[0x%X]=0x%X, ret=%d\r\n", (unsigned int)reg, (unsigned int)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 //uint8_t value; 00291 //printf("m_battery_is_connected is:%d\r\n", m_battery_is_connected); 00292 if(!m_battery_is_connected) { 00293 *batterylevel = 0; 00294 return 0; 00295 } 00296 00297 data[0] = 0x04; 00298 ret = m_i2c->write(MAX20303_I2C_ADDR_FUEL_GAUGE, data, 1); 00299 if(ret != 0){ 00300 printf("Max20303_FuelGauge has failed\r\n"); 00301 } 00302 00303 ret = m_i2c->read(MAX20303_I2C_ADDR_FUEL_GAUGE | 1, data, 2); 00304 if(ret != 0){ 00305 printf("Max20303_FuelGauge has failed\r\n"); 00306 } 00307 //printf("battery level is:%d\r\n", data[0]); 00308 *batterylevel = data[0]; 00309 00310 return 0; 00311 } 00312 00313 //****************************************************************************** 00314 char MAX20303::Max20303_IsBattery_Connected(){ 00315 AnalogIn ain(AIN_5); 00316 float adc_value; 00317 int ret; 00318 char result; 00319 // config the mux for the monitor pin 00320 appcmdoutvalue_ = 0x50; 00321 appdatainoutbuffer_[0] = 0x80; 00322 AppWrite(1); 00323 // disable the charger 00324 appcmdoutvalue_ = 0x1A; 00325 appdatainoutbuffer_[0] = 0x02; 00326 AppWrite(1); 00327 wait_ms(250); 00328 // adc measure launch 00329 appcmdoutvalue_ = 0x53; 00330 appdatainoutbuffer_[0] = 0x09; 00331 AppWrite(1); 00332 wait_ms(10); 00333 ret = readRegMulti(MAX20303::REG_AP_RESPONSE, i2cbuffer_, 5); 00334 adc_value = ain.read(); 00335 if(ret != 0){ 00336 result = false; 00337 } else { 00338 if((i2cbuffer_[4] < 0x40) | (adc_value < 0.3)) { 00339 result = false; 00340 } else { 00341 result = true; 00342 } 00343 } 00344 // enable the charger 00345 appcmdoutvalue_ = 0x1A; 00346 appdatainoutbuffer_[0] = 0x03; 00347 AppWrite(1); 00348 // for(int i = 1; i < 5; ++i){ 00349 // printf("reg value at:%d is:%02X\r\n", i, i2cbuffer_[i]); 00350 // } 00351 m_battery_is_connected = result; 00352 return result; 00353 } 00354 00355 00356 //****************************************************************************** 00357 int MAX20303::led0on(char enable) { 00358 00359 if(enable) 00360 return writeReg(REG_LED0_DIRECT, 0x21); 00361 else 00362 return writeReg(REG_LED0_DIRECT, 0x01); 00363 } 00364 00365 //****************************************************************************** 00366 int MAX20303::led1on(char enable) { 00367 if(enable) 00368 return writeReg(REG_LED1_DIRECT, 0x21); 00369 else 00370 return writeReg(REG_LED1_DIRECT, 0x01); 00371 } 00372 00373 //****************************************************************************** 00374 int MAX20303::led2on(char enable) { 00375 if(enable) 00376 return writeReg(REG_LED2_DIRECT, 0x21); 00377 else 00378 return writeReg(REG_LED2_DIRECT, 0x01); 00379 } 00380 00381 00382 //****************************************************************************** 00383 int MAX20303::BoostEnable(void) { 00384 writeReg(REG_AP_DATOUT3, 0x00); // 00 : 5V 00385 writeReg(REG_AP_DATOUT0, 0x01); // Boost Enabled 00386 writeReg(REG_AP_CMDOUT, 0x30); 00387 return MAX20303_NO_ERROR; 00388 } 00389 00390 //****************************************************************************** 00391 int MAX20303::BuckBoostEnable(void) 00392 { 00393 int ret = 0; 00394 00395 ret |= writeReg( REG_AP_DATOUT0, 0x00); // Reserved = 0x00 00396 ret |= writeReg( REG_AP_DATOUT1, 0x04); // BBstlSet = 0b'100 Buck Boost Peak current Limit = 200mA 00397 ret |= writeReg( REG_AP_DATOUT2, 0x19); // BBstVSet = 0b'11001 Buck Boost Output Voltage = 5V 00398 ret |= writeReg( REG_AP_DATOUT3, 0x01); // BBstRipRed = 1 Ripple Reduction 00399 // BBstAct = 1 Actively discharged in Hard-Reset or Enable Low 00400 // BBstPas = 1 Passively discharged in Hard-Reset or Enable Low 00401 // BBstMd = 1 Damping Enabled 00402 // BBstInd = 0 Inductance is 4.7uH 00403 // BBstEn = 0b'01 Enabled 00404 ret |= writeReg( REG_AP_CMDOUT, 0x70); 00405 if (ret != 0) 00406 return MAX20303_ERROR; 00407 00408 return MAX20303_NO_ERROR; 00409 }
Generated on Tue Jul 12 2022 20:09:28 by
