Browse Source

Moving address locking to init method in BMX280 sensor (#353)

rfm69
Xose Pérez 6 years ago
parent
commit
812d8c2bb9
3 changed files with 40 additions and 19 deletions
  1. +13
    -4
      code/espurna/sensors/BMX280Sensor.h
  2. +18
    -14
      code/espurna/sensors/I2CSensor.h
  3. +9
    -1
      code/espurna/sensors/SI7021Sensor.h

+ 13
- 4
code/espurna/sensors/BMX280Sensor.h View File

@ -72,10 +72,6 @@ class BMX280Sensor : public I2CSensor {
_dirty = false; _dirty = false;
_chip = 0; _chip = 0;
// I2C auto-discover
_address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
if (_address == 0) return;
// Init // Init
_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 // Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
delay(10); delay(10);
// I2C auto-discover
_address = _begin_i2c(_address, sizeof(BMX280Sensor::addresses), BMX280Sensor::addresses);
if (_address == 0) return;
// Check sensor correctly initialized // Check sensor correctly initialized
_chip = i2c_read_uint8(_address, BMX280_REGISTER_CHIPID); _chip = i2c_read_uint8(_address, BMX280_REGISTER_CHIPID);
if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) { if ((_chip != BMX280_CHIP_BME280) && (_chip != BMX280_CHIP_BMP280)) {
_chip = 0; _chip = 0;
i2cReleaseLock(_address); i2cReleaseLock(_address);
_previous_address = 0;
_error = SENSOR_ERROR_UNKNOWN_ID; _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; return;
} }
_count = 0; _count = 0;


+ 18
- 14
code/espurna/sensors/I2CSensor.h View File

@ -40,28 +40,32 @@ class I2CSensor : public BaseSensor {
// Specific for I2C sensors // Specific for I2C sensors
unsigned char _begin_i2c(unsigned char address, size_t size, unsigned char * addresses) { 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 // Check if we should release a previously locked address
if (_previous_address != address) {
if ((_previous_address > 0) && (_previous_address != address)) {
i2cReleaseLock(_previous_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;
} }


+ 9
- 1
code/espurna/sensors/SI7021Sensor.h View File

@ -121,9 +121,17 @@ class SI7021Sensor : public I2CSensor {
_chip = i2c_read_uint8(_address); _chip = i2c_read_uint8(_address);
if ((_chip != SI7021_CHIP_SI7021) & (_chip != SI7021_CHIP_HTU21D)) { if ((_chip != SI7021_CHIP_SI7021) & (_chip != SI7021_CHIP_HTU21D)) {
_count = 0;
i2cReleaseLock(_address); i2cReleaseLock(_address);
_previous_address = 0;
_error = SENSOR_ERROR_UNKNOWN_ID; _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 { } else {
_count = 2; _count = 2;
} }


Loading…
Cancel
Save