From 812d8c2bb95afdce1cd072891d7b562df95a2178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Xose=20P=C3=A9rez?= Date: Sat, 3 Mar 2018 09:36:32 +0100 Subject: [PATCH] Moving address locking to init method in BMX280 sensor (#353) --- code/espurna/sensors/BMX280Sensor.h | 17 +++++++++++---- code/espurna/sensors/I2CSensor.h | 32 ++++++++++++++++------------- code/espurna/sensors/SI7021Sensor.h | 10 ++++++++- 3 files changed, 40 insertions(+), 19 deletions(-) diff --git a/code/espurna/sensors/BMX280Sensor.h b/code/espurna/sensors/BMX280Sensor.h index 1451ba48..26fe4cac 100644 --- a/code/espurna/sensors/BMX280Sensor.h +++ b/code/espurna/sensors/BMX280Sensor.h @@ -72,10 +72,6 @@ class BMX280Sensor : public I2CSensor { _dirty = false; _chip = 0; - // I2C auto-discover - _address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses); - if (_address == 0) return; - // Init _init(); @@ -194,13 +190,26 @@ class BMX280Sensor : public I2CSensor { // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up delay(10); + // I2C auto-discover + _address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses); + if (_address == 0) return; + // Check sensor correctly initialized _chip = i2c_read_uint8(_address, BMX280_REGISTER_CHIPID); if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) { + _chip = 0; i2cReleaseLock(_address); + _previous_address = 0; _error = SENSOR_ERROR_UNKNOWN_ID; + + // Setting _address to 0 forces auto-discover + // This might be necessary at this stage if there is a + // different sensor in the hardcoded address + _address = 0; + return; + } _count = 0; diff --git a/code/espurna/sensors/I2CSensor.h b/code/espurna/sensors/I2CSensor.h index 5a01971e..bec7d51a 100644 --- a/code/espurna/sensors/I2CSensor.h +++ b/code/espurna/sensors/I2CSensor.h @@ -40,28 +40,32 @@ class I2CSensor : public BaseSensor { // Specific for I2C sensors unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) { + // If we have already locked this address for this sensor quit + if ((address > 0) && (address == _previous_address)) { + return _previous_address; + } + // Check if we should release a previously locked address - if (_previous_address != address) { + if ((_previous_address > 0) && (_previous_address != address)) { i2cReleaseLock(_previous_address); + _previous_address = 0; } - // If we have already an address, check it is not locked - if (address && !i2cGetLock(address)) { - _error = SENSOR_ERROR_I2C; - - // If we don't have an address... - } else { - - // Trigger auto-discover - address = i2cFindAndLock(size, addresses); + // If requesting a specific address, try to ger a lock to it + if ((0 < address) && i2cGetLock(address)) { + _previous_address = address; + return _previous_address; + } - // If still nothing exit with error - if (address == 0) _error = SENSOR_ERROR_I2C; + // If everything else fails, perform an auto-discover + _previous_address = i2cFindAndLock(size, addresses); + // Flag error + if (0 == _previous_address) { + _error = SENSOR_ERROR_I2C; } - _previous_address = address; - return address; + return _previous_address; } diff --git a/code/espurna/sensors/SI7021Sensor.h b/code/espurna/sensors/SI7021Sensor.h index 971990fb..86192b01 100644 --- a/code/espurna/sensors/SI7021Sensor.h +++ b/code/espurna/sensors/SI7021Sensor.h @@ -121,9 +121,17 @@ class SI7021Sensor : public I2CSensor { _chip = i2c_read_uint8(_address); if ((_chip != SI7021_CHIP_SI7021) & (_chip != SI7021_CHIP_HTU21D)) { + + _count = 0; i2cReleaseLock(_address); + _previous_address = 0; _error = SENSOR_ERROR_UNKNOWN_ID; - _count = 0; + + // Setting _address to 0 forces auto-discover + // This might be necessary at this stage if there is a + // different sensor in the hardcoded address + _address = 0; + } else { _count = 2; }