@@ -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
@@ -480,7 +521,7 @@ uint8_t SFE_MAX1704X::setThreshold(uint8_t percent)
480521 // It has an LSb weight of 1%, and can be programmed from 1% to 32%.
481522 // The value is (32 - ATHD)%, e.g.: 00000=32%, 00001=31%, 11111=1%.
482523 // Let's convert our percent to that first:
483- percent = constrain (percent, 0 , 32 );
524+ percent = ( uint8_t ) constrain (( float ) percent, 0.0 , 32.0 );
484525 percent = 32 - percent;
485526
486527 // Read config reg, so we don't modify any other values:
@@ -806,18 +847,39 @@ uint8_t SFE_MAX1704X::write16(uint16_t data, uint8_t address)
806847
807848uint16_t SFE_MAX1704X::read16 (uint8_t address)
808849{
809- uint8_t msb, lsb;
810- int16_t timeout = 1000 ;
850+ bool success = false ;
851+ uint8_t retries = 3 ;
852+ uint16_t result = 0 ;
811853
812- _i2cPort->beginTransmission (MAX1704x_ADDRESS);
813- _i2cPort->write (address);
814- _i2cPort->endTransmission (false );
854+ while ((success == false ) && (retries > 0 ))
855+ {
856+ _i2cPort->beginTransmission (MAX1704x_ADDRESS);
857+ _i2cPort->write (address);
858+ _i2cPort->endTransmission (false ); // Don't release the bus
859+
860+ if (_i2cPort->requestFrom (MAX1704x_ADDRESS, 2 ) == 2 )
861+ {
862+ uint8_t msb = _i2cPort->read ();
863+ uint8_t lsb = _i2cPort->read ();
864+ result = ((uint16_t )msb << 8 ) | lsb;
865+ success = true ;
866+ }
867+ else
868+ {
869+ retries--;
870+ if (_printDebug == true )
871+ {
872+ _debugPort->println (F (" SFE_MAX1704X::read16: retrying..." ));
873+ }
874+ delay (50 );
875+ }
876+ }
815877
816- _i2cPort-> requestFrom (MAX1704x_ADDRESS, 2 );
817- while ((_i2cPort-> available () < 2 ) && (timeout-- > 0 ))
818- delay ( 1 );
819- msb = _i2cPort-> read ( );
820- lsb = _i2cPort-> read ();
878+ if (_printDebug == true )
879+ {
880+ if (!success)
881+ _debugPort-> println ( F ( " SFE_MAX1704X::read16: failed to read data! " ) );
882+ }
821883
822- return (( uint16_t )msb << 8 ) | lsb ;
884+ return (result) ;
823885}
0 commit comments