Skip to content

Commit 6c765b3

Browse files
committed
Resolve issue #4
1 parent 9f38aed commit 6c765b3

File tree

3 files changed

+50
-9
lines changed

3 files changed

+50
-9
lines changed

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=SparkFun MAX1704x Fuel Gauge Arduino Library
2-
version=1.0.1
2+
version=1.0.2
33
author=SparkFun Electronics <[email protected]>
44
maintainer=SparkFun Electronics <sparkfun.com>
55
sentence=Arduino library for the MAX17043/44/48/49 fuel gauges

src/SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.cpp

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -61,19 +61,60 @@ boolean SFE_MAX1704X::begin(TwoWire &wirePort)
6161
return (true);
6262
}
6363

64-
//Returns true if device answers on _deviceAddress
64+
//Returns true if device is present
6565
boolean SFE_MAX1704X::isConnected(void)
6666
{
67-
_i2cPort->beginTransmission((uint8_t)MAX1704x_ADDRESS);
68-
if (_i2cPort->endTransmission() == 0)
67+
//Updated to resolve issue #4 Dec 27th 2021
68+
//Also avoid using the standard "if device answers on _deviceAddress" test
69+
//(https://github.com/sparkfun/Arduino_Apollo3/issues/400#issuecomment-992631994)
70+
bool success = false;
71+
uint8_t retries = 3;
72+
uint16_t version = 0;
73+
74+
while ((success == false) && (retries > 0))
75+
{
76+
_i2cPort->beginTransmission(MAX1704x_ADDRESS);
77+
_i2cPort->write(MAX17043_VERSION); // Attempt to read the version register
78+
_i2cPort->endTransmission(false); // Don't release the bus
79+
80+
if (_i2cPort->requestFrom(MAX1704x_ADDRESS, 2) == 2) // Attempt to read the version (2 bytes)
81+
{
82+
uint8_t msb = _i2cPort->read();
83+
uint8_t lsb = _i2cPort->read();
84+
version = ((uint16_t)msb << 8) | lsb;
85+
success = true;
86+
}
87+
else
88+
{
89+
retries--;
90+
if (_printDebug == true)
91+
{
92+
_debugPort->println(F("SFE_MAX1704X::isConnected: retrying..."));
93+
}
94+
delay(50);
95+
}
96+
}
97+
98+
if (!success) // Return now if the version could not be read
99+
{
100+
if (_printDebug == true)
101+
{
102+
_debugPort->println(F("SFE_MAX1704X::isConnected: failed to detect IC!"));
103+
}
104+
return (success);
105+
}
106+
107+
//Extra test - but only for MAX17048/9 - see issue #4
108+
if (_device >= MAX1704X_MAX17048)
69109
{
70110
//Get version should return 0x001_
71111
//Not a great test but something
72-
//Supported on 43/44/48/49
73-
if (getVersion() & (1 << 4))
74-
return true;
112+
//Supported on 48/49
113+
if ((version & (1 << 4)) == 0)
114+
success = false;
75115
}
76-
return false;
116+
117+
return (success);
77118
}
78119

79120
//Enable or disable the printing of debug messages

src/SparkFun_MAX1704x_Fuel_Gauge_Arduino_Library.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ class SFE_MAX1704X
123123
// begin() - Initializes the MAX17043.
124124
boolean begin(TwoWire &wirePort = Wire); //Returns true if module is detected
125125

126-
//Returns true if device answers on MAX1704x_ADDRESS
126+
//Returns true if device is present
127127
boolean isConnected(void);
128128

129129
// Debug

0 commit comments

Comments
 (0)