34 SKIPPED, 0, 0, SLEEP_MODE, SKIPPED, SKIPPED, 0, FILTER_OFF, STB_TIME_0_5};
48 SKIPPED, 0, 0, FORCED_MODE, SKIPPED,
49 OVERSAMPLING_1, 0, FILTER_OFF, STB_TIME_0_5};
59 LOG_ERR(logger,
"Invalid WHO AM I");
67 miosix::Thread::sleep(3);
69 loadCompensationParameters();
73 miosix::Thread::sleep(
83 if (config.
bytes.ctrlHumidity != readBackConfig.bytes.ctrlHumidity ||
84 config.
bytes.ctrlPressureAndTemperature !=
85 readBackConfig.bytes.ctrlPressureAndTemperature ||
86 config.
bytes.config != readBackConfig.bytes.config)
88 LOG_ERR(logger,
"Device configuration incorrect, setup failed.");
99 config.
bits.mode = mode;
106 config.
bits.oversamplingHumidity = oversampling;
113 config.
bits.oversamplingPressure = oversampling;
120 config.
bits.oversamplingTemperature = oversampling;
127 config.
bits.filter = filterCoeff;
134 config.
bits.standbyTime = standbyTime;
144 int32_t adc_H = ((uint32_t)buffer[0] << 8);
149 data.
humidity = compensateHumidity(adc_H);
166 int32_t adc_P = ((uint32_t)buffer[0]) << 12;
167 adc_P |= ((uint32_t)buffer[1]) << 4;
168 adc_P |= (buffer[2] >> 4) & 0x0F;
172 data.
pressure = compensatePressure(adc_P);
189 int32_t adcTemperature = ((uint32_t)buffer[0]) << 12;
190 adcTemperature |= ((uint32_t)buffer[1]) << 4;
191 adcTemperature |= (buffer[2] >> 4) & 0x0F;
193 fineTemperature = computeFineTemperature(adcTemperature);
197 data.
temperature = compensateTemperature(fineTemperature);
211 return ceil(1.25 + (2.3 * config.
bits.oversamplingTemperature) +
212 (2.3 * config.
bits.oversamplingPressure + 0.575) +
213 (2.3 * config.
bits.oversamplingHumidity + 0.575));
232 int32_t adcTemperature = ((uint32_t)buffer[3]) << 12;
233 adcTemperature |= ((uint32_t)buffer[4]) << 4;
234 adcTemperature |= (buffer[5] >> 4) & 0x0F;
236 int32_t adc_P = ((uint32_t)buffer[0]) << 12;
237 adc_P |= ((uint32_t)buffer[1]) << 4;
238 adc_P |= (buffer[2] >> 4) & 0x0F;
240 int32_t adc_H = ((uint32_t)buffer[6] << 8);
244 fineTemperature = computeFineTemperature(adcTemperature);
246 data.
temperature = compensateTemperature(fineTemperature);
251 data.
pressure = compensatePressure(adc_P);
256 data.
humidity = compensateHumidity(adc_H);
268bool BME280I2C::reset()
279bool BME280I2C::checkWhoAmI()
294void BME280I2C::setConfiguration() { setConfiguration(config); }
296void BME280I2C::setConfiguration(BME280Config config)
298 if (!bus.
writeRegister(slaveConfig, REG_CONFIG, config.bytes.config))
300 LOG_ERR(logger,
"Error while writing to register REG_CONFIG");
305 config.bytes.ctrlHumidity))
307 LOG_ERR(logger,
"Error while writing to register REG_CTRL_HUM");
312 config.bytes.ctrlPressureAndTemperature))
314 LOG_ERR(logger,
"Error while writing to register REG_CTRL_MEAS");
319BME280I2C::BME280Config BME280I2C::readConfiguration()
334void BME280I2C::loadCompensationParameters()
346 (uint8_t*)&compParams.bits.dig_H2, 7))
353 compParams.bytesArray[29] =
354 (compParams.bytesArray[29] << 4) | (compParams.bytesArray[29] >> 4);
355 compParams.bits.dig_H4 =
356 (compParams.bits.dig_H4 << 4) | (compParams.bits.dig_H4 >> 8);
359int32_t BME280I2C::computeFineTemperature(int32_t adcTemperature)
362 var1 = ((((adcTemperature >> 3) - ((int32_t)compParams.bits.dig_T1 << 1))) *
363 ((int32_t)compParams.bits.dig_T2)) >>
365 var2 = (((((adcTemperature >> 4) - ((int32_t)compParams.bits.dig_T1)) *
366 ((adcTemperature >> 4) - ((int32_t)compParams.bits.dig_T1))) >>
368 ((int32_t)compParams.bits.dig_T3)) >>
373int32_t BME280I2C::compensateTemperature(int32_t fineTemperature)
375 return (fineTemperature * 5 + 128) >> 8;
378uint32_t BME280I2C::compensatePressure(int32_t adc_P)
380 int64_t var1, var2, p;
381 var1 = ((int64_t)fineTemperature) - 128000;
382 var2 = var1 * var1 * (int64_t)compParams.bits.dig_P6;
383 var2 = var2 + ((var1 * (int64_t)compParams.bits.dig_P5) << 17);
384 var2 = var2 + (((int64_t)compParams.bits.dig_P4) << 35);
385 var1 = ((var1 * var1 * (int64_t)compParams.bits.dig_P3) >> 8) +
386 ((var1 * ((int64_t)compParams.bits.dig_P2) << 12));
388 ((((int64_t)1) << 47) + var1) * ((int64_t)compParams.bits.dig_P1) >> 33;
392 p = (((p << 31) - var2) * 3125) / var1;
393 var1 = (((int64_t)compParams.bits.dig_P9) * (p >> 13) * (p >> 13)) >> 25;
394 var2 = (((int64_t)compParams.bits.dig_P8) * p) >> 19;
395 p = ((p + var1 + var2) >> 8) + (((int64_t)compParams.bits.dig_P7) << 4);
399uint32_t BME280I2C::compensateHumidity(int32_t adc_H)
403 v_x1_u32r = (fineTemperature - ((int32_t)768000));
404 v_x1_u32r = (((((adc_H << 14) - (((int32_t)compParams.bits.dig_H4) << 20) -
405 (((int32_t)compParams.bits.dig_H5) * v_x1_u32r)) +
408 (((((((v_x1_u32r * ((int32_t)compParams.bits.dig_H6)) >> 10) *
409 (((v_x1_u32r * ((int32_t)compParams.bits.dig_H3)) >> 11) +
410 ((int32_t)32768))) >>
412 ((int32_t)2097152)) *
413 ((int32_t)compParams.bits.dig_H2) +
416 v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
417 ((int32_t)compParams.bits.dig_H1)) >>
419 v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r);
420 v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r);
421 return (uint32_t)(v_x1_u32r >> 12);
#define LOG_ERR(logger,...)
bool init() override
Initialize the device with the specified configuration.
static const BME280Config BME280_CONFIG_TEMP_SINGLE
void setTemperatureOversampling(Oversampling oversampling)
Sets the oversampling for temperature readings, use SKIPPED to disable temperature sampling.
static constexpr uint8_t REG_ID_VAL
Who am I value.
void setHumidityOversampling(Oversampling oversampling)
Sets the oversampling for humidity readings, use SKIPPED to disable humidity sampling.
void setSensorMode(Mode mode)
Sets the sensor mode.
static unsigned int calculateMaxMeasurementTime(BME280Config config)
Maximum measurement time formula from datasheet page 51.
BME280Data sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
PressureData readPressure()
Reads only the pressure, does not set the configuration.
static const BME280Config BME280_DEFAULT_CONFIG
Datasheet values for indoor navigation.
static const BME280Config BME280_CONFIG_ALL_ENABLED
Temperature enabled in forced mode.
HumidityData readHumidity()
Reads only the humidity, does not set the configuration.
void setPressureOversampling(Oversampling oversampling)
Sets the oversampling for pressure readings, use SKIPPED to disable pressure sampling.
BME280I2C(I2C &bus, BME280Config config=BME280_CONFIG_ALL_ENABLED)
bool selfTest() override
Reads the WHO AM I register.
void setStandbyTime(StandbyTime standbyTime)
Sets the standby time between readings in normal mode.
unsigned int getMaxMeasurementTime()
void setFilterCoeff(FilterCoeff filterCoeff)
Sets the coefficient for the IIR filter (applied to temperature and pressure)
TemperatureData readTemperature()
Reads only the temperature, does not set the configuration.
High level driver for the I2C peripherals.
bool readFromRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, void *buffer, const size_t nBytes)
Non blocking operation to read n-bytes from register from a slave.
bool writeRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, const uint8_t registerContent)
Non blocking operation to write an 8-bit register from a slave.
bool readRegister(const I2CDriver::I2CSlaveConfig &slaveConfig, const uint8_t registerAddress, uint8_t ®isterContent)
Non blocking operation to read an 8-bit register from a slave.
uint64_t getTimestamp()
Returns the current timer value in microseconds.
This file includes all the types the logdecoder script will decode.
Structure to handle humidity data.
uint64_t humidityTimestamp
uint64_t pressureTimestamp
uint64_t temperatureTimestamp
struct Boardcore::BME280I2C::BME280Config::@3 bytes