1
0
mirror of https://github.com/DCC-EX/CommandStation-EX.git synced 2024-12-24 13:21:23 +01:00

IO_VL53L0X driver: improve I2C error checking and reporting.

This commit is contained in:
Neil McKechnie 2023-01-16 23:00:58 +00:00
parent 7c25f22939
commit abf62dfd85

View File

@ -115,6 +115,7 @@ private:
STATE_CHECKSTATUS = 5, STATE_CHECKSTATUS = 5,
STATE_GETRESULTS = 6, STATE_GETRESULTS = 6,
STATE_DECODERESULTS = 7, STATE_DECODERESULTS = 7,
STATE_FAILED = 8,
}; };
// Register addresses // Register addresses
@ -190,11 +191,12 @@ protected:
// Set 2.8V mode // Set 2.8V mode
write_reg(VL53L0X_CONFIG_PAD_SCL_SDA__EXTSUP_HV, write_reg(VL53L0X_CONFIG_PAD_SCL_SDA__EXTSUP_HV,
read_reg(VL53L0X_CONFIG_PAD_SCL_SDA__EXTSUP_HV) | 0x01); read_reg(VL53L0X_CONFIG_PAD_SCL_SDA__EXTSUP_HV) | 0x01);
_nextState = STATE_INITIATESCAN;
} else { } else {
DIAG(F("VL53L0X I2C:x%x device not responding"), _i2cAddress); DIAG(F("VL53L0X I2C:x%x device not responding"), _i2cAddress);
_deviceState = DEVSTATE_FAILED; _deviceState = DEVSTATE_FAILED;
_nextState = STATE_FAILED;
} }
_nextState = STATE_INITIATESCAN;
break; break;
case STATE_INITIATESCAN: case STATE_INITIATESCAN:
// Not scanning, so initiate a scan // Not scanning, so initiate a scan
@ -207,13 +209,11 @@ protected:
status = _rb.status; status = _rb.status;
if (status == I2C_STATUS_PENDING) return; // try next time if (status == I2C_STATUS_PENDING) return; // try next time
if (status != I2C_STATUS_OK) { if (status != I2C_STATUS_OK) {
DIAG(F("VL53L0X I2C:x%x Error:%d %S"), _i2cAddress, status, I2CManager.getErrorMessage(status)); reportError(status);
_deviceState = DEVSTATE_FAILED; _nextState = STATE_FAILED;
_value = false;
} else } else
_nextState = 2;
delayUntil(currentMicros + 95000); // wait for 95 ms before checking.
_nextState = STATE_GETRESULTS; _nextState = STATE_GETRESULTS;
delayUntil(currentMicros + 95000); // wait for 95 ms before checking.
break; break;
case STATE_GETRESULTS: case STATE_GETRESULTS:
// Ranging completed. Request results // Ranging completed. Request results
@ -240,15 +240,29 @@ protected:
else if (_distance > _offThreshold) else if (_distance > _offThreshold)
_value = false; _value = false;
} }
}
// Completed. Restart scan on next loop entry. // Completed. Restart scan on next loop entry.
_nextState = STATE_INITIATESCAN; _nextState = STATE_INITIATESCAN;
} else {
reportError(status);
_nextState = STATE_FAILED;
}
break;
case STATE_FAILED:
// Do nothing.
delayUntil(currentMicros+1000000UL);
break; break;
default: default:
break; break;
} }
} }
// Function to report a failed I2C operation. Put the device off-line.
void reportError(uint8_t status) {
DIAG(F("VL53L0X I2C:x%x Error:%d %S"), _i2cAddress, status, I2CManager.getErrorMessage(status));
_deviceState = DEVSTATE_FAILED;
_value = false;
}
// For analogue read, first pin returns distance, second pin is signal strength, and third is ambient level. // For analogue read, first pin returns distance, second pin is signal strength, and third is ambient level.
int _readAnalogue(VPIN vpin) override { int _readAnalogue(VPIN vpin) override {
int pin = vpin - _firstVpin; int pin = vpin - _firstVpin;