@@ -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
6565boolean 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
0 commit comments