34 SKIPPED, 0, 0, SLEEP_MODE, SKIPPED, SKIPPED, 0, FILTER_OFF, STB_TIME_0_5};
47 SKIPPED, 0, 0, FORCED_MODE, SKIPPED,
48 OVERSAMPLING_1, 0, FILTER_OFF, STB_TIME_0_5};
51 : spiSlave(spiSlave), config(config)
59 LOG_ERR(logger,
"Invalid WHO AM I");
66 miosix::Thread::sleep(3);
68 loadCompensationParameters();
72 miosix::Thread::sleep(
82 if (config.
bytes.ctrlHumidity != readBackConfig.bytes.ctrlHumidity ||
83 config.
bytes.ctrlPressureAndTemperature !=
84 readBackConfig.bytes.ctrlPressureAndTemperature ||
85 config.
bytes.config != readBackConfig.bytes.config)
87 LOG_ERR(logger,
"Device configuration incorrect, setup failed.");
98 config.
bits.mode = mode;
105 config.
bits.oversamplingHumidity = oversampling;
112 config.
bits.oversamplingPressure = oversampling;
119 config.
bits.oversamplingTemperature = oversampling;
126 config.
bits.filter = filterCoeff;
133 config.
bits.standbyTime = standbyTime;
147 int32_t adc_H = ((uint32_t)buffer[0] << 8);
152 data.
humidity = compensateHumidity(adc_H);
167 int32_t adc_P = ((uint32_t)buffer[0]) << 12;
168 adc_P |= ((uint32_t)buffer[1]) << 4;
169 adc_P |= (buffer[2] >> 4) & 0x0F;
173 data.
pressure = compensatePressure(adc_P);
188 int32_t adcTemperature = ((uint32_t)buffer[0]) << 12;
189 adcTemperature |= ((uint32_t)buffer[1]) << 4;
190 adcTemperature |= (buffer[2] >> 4) & 0x0F;
192 fineTemperature = computeFineTemperature(adcTemperature);
196 data.
temperature = compensateTemperature(fineTemperature);
204 return ceil(1.25 + (2.3 * config.
bits.oversamplingTemperature) +
205 (2.3 * config.
bits.oversamplingPressure + 0.575) +
206 (2.3 * config.
bits.oversamplingHumidity + 0.575));
229 int32_t adcTemperature = ((uint32_t)buffer[3]) << 12;
230 adcTemperature |= ((uint32_t)buffer[4]) << 4;
231 adcTemperature |= (buffer[5] >> 4) & 0x0F;
233 int32_t adc_P = ((uint32_t)buffer[0]) << 12;
234 adc_P |= ((uint32_t)buffer[1]) << 4;
235 adc_P |= (buffer[2] >> 4) & 0x0F;
237 int32_t adc_H = ((uint32_t)buffer[6] << 8);
241 fineTemperature = computeFineTemperature(adcTemperature);
243 data.
temperature = compensateTemperature(fineTemperature);
248 data.
pressure = compensatePressure(adc_P);
253 data.
humidity = compensateHumidity(adc_H);
263 transaction.writeRegister(REG_RESET, 0xB6);
266bool BME280::checkWhoAmI()
268 SPITransaction transaction(spiSlave);
270 uint8_t whoAmIValue = transaction.readRegister(REG_ID);
275void BME280::setConfiguration() { setConfiguration(config); }
277void BME280::setConfiguration(BME280Config config)
279 SPITransaction transaction(spiSlave);
281 transaction.writeRegister(REG_CONFIG, config.bytes.config);
282 transaction.writeRegister(REG_CTRL_HUM, config.bytes.ctrlHumidity);
283 transaction.writeRegister(REG_CTRL_MEAS,
284 config.bytes.ctrlPressureAndTemperature);
287BME280::BME280Config BME280::readConfiguration()
290 SPITransaction transaction(spiSlave);
292 transaction.readRegisters(REG_CTRL_HUM, (uint8_t*)&tmp, 4);
297void BME280::loadCompensationParameters()
301 SPITransaction transaction(spiSlave);
303 transaction.readRegisters(REG_CALIB_0, (uint8_t*)&compParams, 25);
308 SPITransaction transaction(spiSlave);
310 transaction.readRegisters(REG_CALIB_26,
311 (uint8_t*)&compParams.
bits.dig_H2, 7);
317 compParams.
bits.dig_H4 =
318 (compParams.
bits.dig_H4 << 4) | (compParams.
bits.dig_H4 >> 8);
321int32_t BME280::computeFineTemperature(int32_t adcTemperature)
324 var1 = ((((adcTemperature >> 3) - ((int32_t)compParams.
bits.dig_T1 << 1))) *
325 ((int32_t)compParams.
bits.dig_T2)) >>
327 var2 = (((((adcTemperature >> 4) - ((int32_t)compParams.
bits.dig_T1)) *
328 ((adcTemperature >> 4) - ((int32_t)compParams.
bits.dig_T1))) >>
330 ((int32_t)compParams.
bits.dig_T3)) >>
335int32_t BME280::compensateTemperature(int32_t fineTemperature)
337 return (fineTemperature * 5 + 128) >> 8;
340uint32_t BME280::compensatePressure(int32_t adc_P)
342 int64_t var1, var2, p;
343 var1 = ((int64_t)fineTemperature) - 128000;
344 var2 = var1 * var1 * (int64_t)compParams.
bits.dig_P6;
345 var2 = var2 + ((var1 * (int64_t)compParams.
bits.dig_P5) << 17);
346 var2 = var2 + (((int64_t)compParams.
bits.dig_P4) << 35);
347 var1 = ((var1 * var1 * (int64_t)compParams.
bits.dig_P3) >> 8) +
348 ((var1 * ((int64_t)compParams.
bits.dig_P2) << 12));
350 ((((int64_t)1) << 47) + var1) * ((int64_t)compParams.
bits.dig_P1) >> 33;
354 p = (((p << 31) - var2) * 3125) / var1;
355 var1 = (((int64_t)compParams.
bits.dig_P9) * (p >> 13) * (p >> 13)) >> 25;
356 var2 = (((int64_t)compParams.
bits.dig_P8) * p) >> 19;
357 p = ((p + var1 + var2) >> 8) + (((int64_t)compParams.
bits.dig_P7) << 4);
361uint32_t BME280::compensateHumidity(int32_t adc_H)
365 v_x1_u32r = (fineTemperature - ((int32_t)768000));
366 v_x1_u32r = (((((adc_H << 14) - (((int32_t)compParams.
bits.dig_H4) << 20) -
367 (((int32_t)compParams.
bits.dig_H5) * v_x1_u32r)) +
370 (((((((v_x1_u32r * ((int32_t)compParams.
bits.dig_H6)) >> 10) *
371 (((v_x1_u32r * ((int32_t)compParams.
bits.dig_H3)) >> 11) +
372 ((int32_t)32768))) >>
374 ((int32_t)2097152)) *
375 ((int32_t)compParams.
bits.dig_H2) +
378 v_x1_u32r = (v_x1_u32r - (((((v_x1_u32r >> 15) * (v_x1_u32r >> 15)) >> 7) *
379 ((int32_t)compParams.
bits.dig_H1)) >>
381 v_x1_u32r = (v_x1_u32r < 0 ? 0 : v_x1_u32r);
382 v_x1_u32r = (v_x1_u32r > 419430400 ? 419430400 : v_x1_u32r);
383 return (uint32_t)(v_x1_u32r >> 12);
#define LOG_ERR(logger,...)
static const BME280Config BME280_CONFIG_TEMP_SINGLE
void setHumidityOversampling(Oversampling oversampling)
Sets the oversampling for humidity readings, use SKIPPED to disable humidity sampling.
static const BME280Config BME280_CONFIG_ALL_ENABLED
Temperature enabled in forced mode.
void setTemperatureOversampling(Oversampling oversampling)
Sets the oversampling for temperature readings, use SKIPPED to disable temperature sampling.
bool init() override
Initialize the device with the specified configuration.
void setStandbyTime(StandbyTime standbyTime)
Sets the standby time between readings in normal mode.
void setSensorMode(Mode mode)
Sets the sensor mode.
BME280(SPISlave spiSlave, BME280Config config=BME280_CONFIG_ALL_ENABLED)
static const BME280Config BME280_DEFAULT_CONFIG
Datasheet values for indoor navigation.
unsigned int getMaxMeasurementTime()
bool selfTest() override
Reads the WHO AM I register.
static unsigned int calculateMaxMeasurementTime(BME280Config config)
Maximum measurement time formula from datasheet page 51.
HumidityData readHumidity()
Reads only the humidity, does not set the configuration.
PressureData readPressure()
Reads only the pressure, does not set the configuration.
void setPressureOversampling(Oversampling oversampling)
Sets the oversampling for pressure readings, use SKIPPED to disable pressure sampling.
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.
BME280Data sampleImpl() override
Read a data sample from the sensor. In case of errors, the method should return the last available co...
static constexpr uint8_t REG_ID_VAL
Who am I value.
Provides high-level access to the SPI Bus for a single transaction.
void readRegisters(uint8_t reg, uint8_t *data, size_t size)
Reads multiple bytes starting from the specified register.
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
Contains information about a single SPI slave device.
uint64_t temperatureTimestamp
struct Boardcore::BME280::BME280Config::@2 bytes