Skip to content

Commit 5750012

Browse files
committed
Updated .begin to avoid problems when calling isConnected twice
1 parent ff99f98 commit 5750012

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/SparkFun_MCP9600.cpp

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,27 @@ bool MCP9600::begin(uint8_t address, TwoWire &wirePort)
3333
_deviceAddress = address; //grab the address that the sensor is on
3434
_i2cPort = &wirePort; //grab the port that the user wants to use
3535

36+
// This is the old .begin code. See below for the reason we don't use it any more...
37+
//
3638
//return true if the device is connected and the device ID is what we expect
37-
bool success = isConnected();
38-
success &= checkDeviceID();
39+
//bool success = isConnected();
40+
//success &= checkDeviceID();
41+
42+
// The MCP9600 is a fussy device. If we call isConnected twice in succession, the second call fails
43+
// as the MCP9600 does not ACK on the second call. Only on the first.
44+
//
45+
// The OpenLog Artemis routinely uses the same isConnected code twice: once to detect _anything_ that is on the bus;
46+
// and then again to detect an MCP9600. The second time fails.
47+
//
48+
// If there is a device with a lower address on the OLA Qwiic bus, that device responds first and satisfies
49+
// isConnected. Then the OLA code uses isConnected again to detect an MCP9600. That call is successful, as this time it is the first call.
50+
// However, the OLA then goes on to call this function to begin the sensor. Previously this was failing as isConnected
51+
// (12 lines above) was then being called for the second time...
52+
//
53+
// Long story short, we should not call isConnected here. We should only call checkDeviceID.
54+
55+
bool success = checkDeviceID();
56+
3957
return success;
4058
}
4159

@@ -115,7 +133,7 @@ float MCP9600::getTempDelta(bool units)
115133

116134
signed long MCP9600::getRawADC()
117135
{
118-
for (byte attempts; attempts <= retryAttempts; attempts++)
136+
for (byte attempts = 0; attempts <= retryAttempts; attempts++)
119137
{
120138
_i2cPort->beginTransmission(_deviceAddress);
121139
_i2cPort->write(RAW_ADC);
@@ -562,7 +580,7 @@ uint8_t MCP9600::readSingleRegister(MCP9600_Register reg)
562580
//Attempt to read the register until we exit with no error code
563581
//This attempts to fix the bug where clock stretching sometimes failes, as
564582
//described in the MCP9600 eratta
565-
for (uint8_t attempts; attempts <= retryAttempts; attempts++)
583+
for (uint8_t attempts = 0; attempts <= retryAttempts; attempts++)
566584
{
567585
_i2cPort->beginTransmission(_deviceAddress);
568586
_i2cPort->write(reg);
@@ -580,7 +598,7 @@ uint16_t MCP9600::readDoubleRegister(MCP9600_Register reg)
580598
//Attempt to read the register until we exit with no error code
581599
//This attempts to fix the bug where clock stretching sometimes failes, as
582600
//described in the MCP9600 eratta
583-
for (byte attempts; attempts <= retryAttempts; attempts++)
601+
for (byte attempts = 0; attempts <= retryAttempts; attempts++)
584602
{
585603
_i2cPort->beginTransmission(_deviceAddress);
586604
_i2cPort->write(reg);
@@ -611,4 +629,4 @@ bool MCP9600::writeDoubleRegister(MCP9600_Register reg, uint16_t data)
611629
_i2cPort->write(highByte(data));
612630
_i2cPort->write(lowByte(data));
613631
return (_i2cPort->endTransmission() != 0);
614-
}
632+
}

0 commit comments

Comments
 (0)