Skip to content

Commit 0d2105c

Browse files
committed
drivers: sensors: bmp581: fix temperature scaling
This change fixes and issue where negative temperatures wrap and return 250C when the sensor gets below zero. The implementation is pulled from Boschs official BMP5_SensorAPI and has been tested to work down to -40. Signed-off-by: Maxmillion McLaughlin <[email protected]>
1 parent ca85e76 commit 0d2105c

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

drivers/sensor/bosch/bmp581/bmp581.c

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,11 +405,17 @@ static int bmp581_sample_fetch(const struct device *dev, enum sensor_channel cha
405405

406406
ret = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_TEMP_DATA_XLSB, data, 6);
407407
if (ret == BMP5_OK) {
408-
/* convert raw sensor data to sensor_value. Shift the decimal part by 1 decimal
409-
* place to compensate for the conversion in sensor_value_to_double()
408+
/* convert raw sensor data to sensor_value.
409+
* BMP581 temperature data is 24-bit signed with LSB = 1/65536 °C
410410
*/
411-
drv->last_sample.temperature.val1 = data[2];
412-
drv->last_sample.temperature.val2 = (data[1] << 8 | data[0]) * 10;
411+
int32_t raw_temp = ((int32_t)((uint32_t)(((uint32_t)data[2] << 16) |
412+
((uint16_t)data[1] << 8) | data[0])
413+
<< 8) >>
414+
8);
415+
416+
// Convert raw temperature: LSB = 1/65536 °C, val2 in millionths
417+
drv->last_sample.temperature.val1 = raw_temp / 65536;
418+
drv->last_sample.temperature.val2 = ((int64_t)(raw_temp % 65536) * 1000000) / 65536;
413419

414420
if (drv->osr_odr_press_config.press_en == BMP5_ENABLE) {
415421
/* convert raw sensor data to sensor_value. Shift the decimal part by

0 commit comments

Comments
 (0)