mirror of
https://github.com/DCC-EX/CommandStation-EX.git
synced 2024-12-23 21:01:25 +01:00
IO_VL53L0X driver: improve I2C error checking and reporting.
This commit is contained in:
parent
7c25f22939
commit
abf62dfd85
30
IO_VL53L0X.h
30
IO_VL53L0X.h
@ -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;
|
_nextState = STATE_GETRESULTS;
|
||||||
delayUntil(currentMicros + 95000); // wait for 95 ms before checking.
|
delayUntil(currentMicros + 95000); // wait for 95 ms before checking.
|
||||||
_nextState = STATE_GETRESULTS;
|
|
||||||
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.
|
||||||
|
_nextState = STATE_INITIATESCAN;
|
||||||
|
} else {
|
||||||
|
reportError(status);
|
||||||
|
_nextState = STATE_FAILED;
|
||||||
}
|
}
|
||||||
// Completed. Restart scan on next loop entry.
|
break;
|
||||||
_nextState = STATE_INITIATESCAN;
|
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;
|
||||||
|
Loading…
Reference in New Issue
Block a user