MAX17055 driver
Dependents: MAX32620FTHR_FuelGauge MAX32620FTHR_GPS_Tracker
MAX17055.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 #include "MAX17055.h" 00035 00036 //****************************************************************************** 00037 MAX17055::MAX17055(I2C &i2c, int address) : 00038 i2c(i2c), 00039 addr(address) 00040 { 00041 } 00042 00043 //****************************************************************************** 00044 MAX17055::~MAX17055() 00045 { 00046 } 00047 00048 //****************************************************************************** 00049 void MAX17055::init(float r_sense) 00050 { 00051 this->r_sense = r_sense; 00052 i_lsb = MAX17055_I_LSB_UV / r_sense; 00053 i_min_max_lsb = MAX17055_I_MAX_MIN_LSB_MV / r_sense; 00054 } 00055 00056 //****************************************************************************** 00057 int MAX17055::status(int *value) 00058 { 00059 char buf[2]; 00060 00061 if (readReg(MAX17055::STATUS, buf, 2)) { 00062 return MAX17055_ERROR; 00063 } 00064 00065 *value = (unsigned int)((buf[1] << 8) | buf[0]); 00066 00067 return MAX17055_NO_ERROR; 00068 } 00069 00070 //****************************************************************************** 00071 int MAX17055::v_cell(int *value) 00072 { 00073 char buf[2]; 00074 00075 if (readReg(MAX17055::V_CELL, buf, 2)) { 00076 return MAX17055_ERROR; 00077 } 00078 00079 *value = (unsigned int)((buf[1] << 8) | buf[0]); 00080 00081 return MAX17055_NO_ERROR; 00082 } 00083 00084 //****************************************************************************** 00085 int MAX17055::v_cell(float *value) 00086 { 00087 int v; 00088 00089 if (v_cell(&v)) { 00090 return MAX17055_ERROR; 00091 } 00092 00093 *value = v * MAX17055_V_LSB_MV; 00094 00095 return MAX17055_NO_ERROR; 00096 } 00097 00098 //****************************************************************************** 00099 int MAX17055::avg_v_cell(int *value) 00100 { 00101 char buf[2]; 00102 00103 if (readReg(MAX17055::AVG_V_CELL, buf, 2)) { 00104 return MAX17055_ERROR; 00105 } 00106 00107 *value = (unsigned int)((buf[1] << 8) | buf[0]); 00108 00109 return MAX17055_NO_ERROR; 00110 } 00111 00112 //****************************************************************************** 00113 int MAX17055::avg_v_cell(float *value) 00114 { 00115 int avc; 00116 00117 if (avg_v_cell(&avc)) { 00118 return MAX17055_ERROR; 00119 } 00120 00121 *value = avc * MAX17055_V_LSB_MV; 00122 00123 return MAX17055_NO_ERROR; 00124 } 00125 00126 //****************************************************************************** 00127 int MAX17055::max_min_volt(int *max, int *min) 00128 { 00129 char buf[2]; 00130 00131 if (readReg(MAX17055::MAX_MIN_VOLT, buf, 2)) { 00132 return MAX17055_ERROR; 00133 } 00134 00135 *max = (unsigned char)buf[1]; 00136 *min = (unsigned char)buf[0]; 00137 00138 return MAX17055_NO_ERROR; 00139 } 00140 00141 //****************************************************************************** 00142 int MAX17055::max_min_volt(float *max, float *min) 00143 { 00144 int v_max, v_min; 00145 00146 if (max_min_volt(&v_max, &v_min)) { 00147 return MAX17055_ERROR; 00148 } 00149 00150 *max = v_max * MAX17055_V_MAX_MIN_LSB_MV; 00151 *min = v_min * MAX17055_V_MAX_MIN_LSB_MV; 00152 00153 return MAX17055_NO_ERROR; 00154 } 00155 00156 //****************************************************************************** 00157 int MAX17055::current(int *value) 00158 { 00159 char buf[2]; 00160 00161 if (readReg(MAX17055::CURRENT, buf, 2)) { 00162 return MAX17055_ERROR; 00163 } 00164 00165 *value = (short int)((buf[1] << 8) | buf[0]); 00166 00167 return MAX17055_NO_ERROR; 00168 } 00169 00170 //****************************************************************************** 00171 int MAX17055::current(float *value) 00172 { 00173 int i; 00174 00175 if (current(&i)) { 00176 return MAX17055_ERROR; 00177 } 00178 00179 *value = i * i_lsb; 00180 00181 return MAX17055_NO_ERROR; 00182 } 00183 00184 //****************************************************************************** 00185 int MAX17055::avg_current(int *value) 00186 { 00187 char buf[2]; 00188 00189 if (readReg(MAX17055::AVG_CURRENT, buf, 2)) { 00190 return MAX17055_ERROR; 00191 } 00192 00193 *value = (short int)((buf[1] << 8) | buf[0]); 00194 00195 return MAX17055_NO_ERROR; 00196 } 00197 00198 //****************************************************************************** 00199 int MAX17055::avg_current(float *value) 00200 { 00201 int i_a; 00202 00203 if (avg_current(&i_a)) { 00204 return MAX17055_ERROR; 00205 } 00206 00207 *value = i_a * i_lsb; 00208 00209 return MAX17055_NO_ERROR; 00210 } 00211 00212 //****************************************************************************** 00213 int MAX17055::max_min_curr(int *max, int *min) 00214 { 00215 char buf[2]; 00216 00217 if (readReg(MAX17055::MAX_MIN_CURR, buf, 2)) { 00218 return MAX17055_ERROR; 00219 } 00220 00221 *max = (signed char)buf[1]; 00222 *min = (signed char)buf[0]; 00223 00224 return MAX17055_NO_ERROR; 00225 } 00226 00227 //****************************************************************************** 00228 int MAX17055::max_min_curr(float *max, float *min) 00229 { 00230 int i_max, i_min; 00231 00232 if (max_min_curr(&i_max, &i_min)) { 00233 return MAX17055_ERROR; 00234 } 00235 00236 *max = i_max * i_min_max_lsb; 00237 *min = i_min * i_min_max_lsb; 00238 00239 return MAX17055_NO_ERROR; 00240 } 00241 00242 //****************************************************************************** 00243 int MAX17055::temp(int *value) 00244 { 00245 char buf[2]; 00246 00247 if (readReg(MAX17055::TEMP, buf, 2)) { 00248 return MAX17055_ERROR; 00249 } 00250 00251 *value = (short int)((buf[1] << 8) | buf[0]); 00252 00253 return MAX17055_NO_ERROR; 00254 } 00255 00256 //****************************************************************************** 00257 int MAX17055::temp(float *value) 00258 { 00259 int t; 00260 00261 if (temp(&t)) { 00262 return MAX17055_ERROR; 00263 } 00264 00265 *value = t * (1.0f / 256); 00266 00267 return MAX17055_NO_ERROR; 00268 } 00269 00270 00271 //****************************************************************************** 00272 int MAX17055::avg_ta(int *value) 00273 { 00274 char buf[2]; 00275 00276 if (readReg(MAX17055::AVG_TA, buf, 2)) { 00277 return MAX17055_ERROR; 00278 } 00279 00280 *value = (short int)((buf[1] << 8) | buf[0]); 00281 00282 return MAX17055_NO_ERROR; 00283 } 00284 00285 //****************************************************************************** 00286 int MAX17055::avg_ta(float *value) 00287 { 00288 int ta; 00289 00290 if (avg_ta(&ta)) { 00291 return MAX17055_ERROR; 00292 } 00293 00294 *value = ta * (1.0f / 256); 00295 00296 return MAX17055_NO_ERROR; 00297 } 00298 00299 //****************************************************************************** 00300 int MAX17055::max_min_temp(int *max, int *min) 00301 { 00302 char buf[2]; 00303 00304 if (readReg(MAX17055::MAX_MIN_TEMP, buf, 2)) { 00305 return MAX17055_ERROR; 00306 } 00307 00308 *max = (signed char)buf[1]; 00309 *min = (signed char)buf[0]; 00310 00311 return MAX17055_NO_ERROR; 00312 } 00313 00314 //****************************************************************************** 00315 int MAX17055::max_min_temp(float *max, float *min) 00316 { 00317 int t_max, t_min; 00318 00319 if (max_min_temp(&t_max, &t_min)) { 00320 return MAX17055_ERROR; 00321 } 00322 00323 *max = t_max; 00324 *min = t_min; 00325 00326 return MAX17055_NO_ERROR; 00327 } 00328 00329 //****************************************************************************** 00330 int MAX17055::writeReg(reg_t reg, char value, bool verify) 00331 { 00332 char buf[] = { (char)reg, value }; 00333 00334 if (i2c.write(addr, buf, sizeof(buf))) { 00335 return MAX17055_ERROR; 00336 } 00337 00338 return MAX17055_NO_ERROR; 00339 } 00340 00341 //****************************************************************************** 00342 int MAX17055::writeReg(reg_t reg, uint16_t value, bool verify) 00343 { 00344 char wbuf[3]; 00345 char rbuf[2]; 00346 int attempt; 00347 00348 wbuf[0] = reg; 00349 wbuf[1] = value; 00350 wbuf[2] = value >> 8; 00351 00352 attempt = 0; 00353 00354 do { 00355 if (i2c.write(addr, wbuf, 3)) return MAX17055_ERROR; 00356 00357 if (!verify) break; 00358 00359 Thread::wait(1); 00360 00361 if (i2c.read(addr, rbuf, 2)) return MAX17055_ERROR; 00362 00363 } while ((((rbuf[1] << 8) | rbuf[0]) != value) && (attempt++ < 3)); 00364 00365 if (attempt == 3) return MAX17055_ERROR; 00366 00367 return MAX17055_NO_ERROR; 00368 } 00369 00370 //****************************************************************************** 00371 int MAX17055::readReg(reg_t reg, char *value) 00372 { 00373 char buf[] = { (char)reg }; 00374 00375 if (i2c.write(addr, buf, sizeof(buf))) { 00376 return MAX17055_ERROR; 00377 } 00378 00379 if (i2c.read(addr, value, 1)) { 00380 return MAX17055_ERROR; 00381 } 00382 00383 return MAX17055_NO_ERROR; 00384 } 00385 00386 //****************************************************************************** 00387 int MAX17055::readReg(reg_t reg, char *buf, int len) 00388 { 00389 *buf = (char)reg; 00390 00391 if (i2c.write(addr, buf, 1)) { 00392 return MAX17055_ERROR; 00393 } 00394 00395 if (i2c.read(addr, buf, len)) { 00396 return MAX17055_ERROR; 00397 } 00398 00399 return MAX17055_NO_ERROR; 00400 }
Generated on Thu Jul 14 2022 04:26:30 by 1.7.2