Skip to content

Commit

Permalink
bmx280: suspend and resume
Browse files Browse the repository at this point in the history
separate branches for i2c search and init
replace optimistic waits with a status check
  • Loading branch information
mcspr committed Mar 15, 2023
1 parent 4f72c60 commit 83c9df9
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 20 deletions.
77 changes: 62 additions & 15 deletions code/espurna/sensors/BMX280Sensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
#define BMX280_REGISTER_CAL26 0xE1

#define BMX280_REGISTER_CONTROLHUMID 0xF2
#define BMX280_REGISTER_STATUS 0xF3
#define BMX280_REGISTER_CONTROL 0xF4
#define BMX280_REGISTER_CONFIG 0xF5
#define BMX280_REGISTER_PRESSUREDATA 0xF7
Expand Down Expand Up @@ -145,25 +146,28 @@ class BMX280Sensor : public I2CSensor<> {

// Pre-read hook (usually to populate registers with up-to-date data)
void pre() override {

if (_run_init) {
i2cClearBus();
_init();
_init(lockedAddress());
}

if (_chip == 0) {
return;
}

_error = SENSOR_ERROR_OK;

const auto address = lockedAddress();

#if BMX280_MODE == 1
_forceRead(address);
#endif

_error = _read(address);
if (!_wait(address)) {
_error = SENSOR_ERROR_NOT_READY;
return;
}

_error = _read(address);
if (_error != SENSOR_ERROR_OK) {
_run_init = true;
}
Expand Down Expand Up @@ -192,27 +196,33 @@ class BMX280Sensor : public I2CSensor<> {
// Initialization method, must be idempotent
void begin() override {
if (!_dirty) return;
_init();
if (!_find()) return;
_init(lockedAddress());
_dirty = !_ready;
}

protected:
void suspend() override {
if (_chip != 0) {
i2c_write_uint8(lockedAddress(), BMX280_REGISTER_CONTROL, 0);
_ready = false;
}
}

void _init() {
void resume() override {
_run_init = true;
}

// Make sure sensor had enough time to turn on. BMX280 requires 2ms to start up
espurna::time::blockingDelay(espurna::duration::Milliseconds(10));
protected:

// No chip ID by default
bool _find() {
_chip = 0;
_count = 0;
_magnitudes = nullptr;

// I2C auto-discover
static constexpr uint8_t addresses[] {0x76, 0x77};
auto address = findAndLock(addresses);
if (address == 0) {
return;
return false;
}

// Check sensor correctly initialized
Expand All @@ -231,12 +241,25 @@ class BMX280Sensor : public I2CSensor<> {
if (!_magnitudes || !_count) {
resetUnknown();
_chip = 0;
}

return _chip != 0;
}

void _init(uint8_t address) {
if (_chip == 0) {
return;
}

i2c_write_uint8(address, BMX280_REGISTER_SOFTRESET, 0xB6);
if (!_wait(address)) {
_error = SENSOR_ERROR_NOT_READY;
return;
}

_readCoefficients(address);

unsigned char data = 0;
uint8_t data = 0;
i2c_write_uint8(address, BMX280_REGISTER_CONTROL, data);

data = (BMX280_STANDBY << 0x5) & 0xE0;
Expand All @@ -252,11 +275,33 @@ class BMX280Sensor : public I2CSensor<> {
i2c_write_uint8(address, BMX280_REGISTER_CONTROL, data);

_measurement_delay = _measurementTime();

_run_init = false;
_ready = true;

}

static bool _measurementsReady(uint8_t status) {
constexpr auto Measuring = uint8_t{ 1 << 3 };
constexpr auto InternalMemoryUpdate = uint8_t{ 1 };

return (status & (Measuring | InternalMemoryUpdate)) == 0;
}

static bool _wait(uint8_t address) {
uint8_t status = 0;

espurna::time::blockingDelay(
espurna::duration::Milliseconds{ 100 },
StatusDelay,
[&]() {
status = i2c_read_uint8(address, BMX280_REGISTER_STATUS);
return !_measurementsReady(status);
});

return _measurementsReady(status);
}

void _readCoefficients(unsigned char address) {
_bmx280_calib = bmx280_calib_t{
.dig_T1 = i2c_read_uint16_le(address, BMX280_REGISTER_DIG_T1),
Expand Down Expand Up @@ -307,15 +352,13 @@ class BMX280Sensor : public I2CSensor<> {
}

void _forceRead(unsigned char address) {

// We set the sensor in "forced mode" to force a reading.
// After the reading the sensor will go back to sleep mode.
uint8_t value = i2c_read_uint8(address, BMX280_REGISTER_CONTROL);
value = (value & 0xFC) + 0x01;
i2c_write_uint8(address, BMX280_REGISTER_CONTROL, value);

espurna::time::blockingDelay(_measurement_delay);

}

unsigned char _read(unsigned char address) {
Expand Down Expand Up @@ -406,6 +449,9 @@ class BMX280Sensor : public I2CSensor<> {

// ---------------------------------------------------------------------

// Make sure sensor had enough time to turn on. BMX280 requires at least 2ms to start up
static constexpr auto StatusDelay = espurna::duration::Milliseconds{ 2 };

espurna::duration::Milliseconds _measurement_delay;

bool _run_init = false;
Expand Down Expand Up @@ -448,6 +494,7 @@ class BMX280Sensor : public I2CSensor<> {
#if __cplusplus < 201703L
constexpr BaseSensor::Magnitude BMX280Sensor::Bmp280Magnitudes[];
constexpr BaseSensor::Magnitude BMX280Sensor::Bme280Magnitudes[];
constexpr espurna::duration::Milliseconds BMX280Sensor::StatusDelay;
#endif

#endif // SENSOR_SUPPORT && BMX280_SUPPORT
10 changes: 5 additions & 5 deletions code/espurna/sensors/BaseSensor.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,16 +160,16 @@ class BaseSensor {
virtual void calibrate() {
}

// Kind of sensor
virtual ClassKind kind() const {
return Kind;
}

// Number of decimals for a unit (or -1 for default)
virtual signed char decimals(espurna::sensor::Unit) const {
return -1;
}

// Kind of sensor
virtual ClassKind kind() const {
return Kind;
}

// Sensor ID, must be unique
virtual unsigned char id() const = 0;

Expand Down

0 comments on commit 83c9df9

Please sign in to comment.