Skip to content

Commit 50bfe62

Browse files
committed
Remove infinite loop when read fails
It looks like ST put in some fix for their boards. This caused an infinite loop in our sketches if a sensor was not connected. This fixes it but it may break ST board support (who cares?).
1 parent 79fd280 commit 50bfe62

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

src/vl53l1x_class.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1009,8 +1009,10 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CWrite(uint8_t DeviceAddr, uint16_t RegisterAddr
10091009
VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr, uint8_t *pBuffer, uint16_t NumByteToRead)
10101010
{
10111011
int status = 0;
1012+
10121013
//Loop until the port is transmitted correctly
1013-
do
1014+
uint8_t maxAttempts = 5;
1015+
for (uint8_t x = 0; x < maxAttempts; x++)
10141016
{
10151017
#ifdef DEBUG_MODE
10161018
Serial.print("Beginning transmission to ");
@@ -1026,6 +1028,10 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr,
10261028
buffer[1] = RegisterAddr & 0xFF;
10271029
dev_i2c->write(buffer, 2);
10281030
status = dev_i2c->endTransmission(false);
1031+
1032+
if (status == 0)
1033+
break;
1034+
10291035
//Fix for some STM32 boards
10301036
//Reinitialize th i2c bus with the default parameters
10311037
#ifdef ARDUINO_ARCH_STM32
@@ -1036,7 +1042,7 @@ VL53L1X_ERROR VL53L1X::VL53L1_I2CRead(uint8_t DeviceAddr, uint16_t RegisterAddr,
10361042
}
10371043
#endif
10381044
//End of fix
1039-
} while (status != 0);
1045+
}
10401046

10411047
dev_i2c->requestFrom(((uint8_t)(((DeviceAddr) >> 1) & 0x7F)), (byte)NumByteToRead);
10421048

0 commit comments

Comments
 (0)