Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions drivers/sensor/bosch/bmp581/bmp581.c
Original file line number Diff line number Diff line change
Expand Up @@ -405,11 +405,17 @@ static int bmp581_sample_fetch(const struct device *dev, enum sensor_channel cha

ret = bmp581_reg_read_rtio(&conf->bus, BMP5_REG_TEMP_DATA_XLSB, data, 6);
if (ret == BMP5_OK) {
/* convert raw sensor data to sensor_value. Shift the decimal part by 1 decimal
* place to compensate for the conversion in sensor_value_to_double()
/* convert raw sensor data to sensor_value.
* BMP581 temperature data is 24-bit signed with LSB = 1/65536 °C
*/
drv->last_sample.temperature.val1 = data[2];
drv->last_sample.temperature.val2 = (data[1] << 8 | data[0]) * 10;
int32_t raw_temp = ((int32_t)((uint32_t)(((uint32_t)data[2] << 16) |
((uint16_t)data[1] << 8) | data[0])
<< 8) >>
8);
Comment on lines +408 to +414
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of shifting 8 up and 8 down, I suggest using sign_extend(), similar to how it's done in the decoder:

case SENSOR_CHAN_AMBIENT_TEMP: {
/* Temperature is in data[2:0], data[2] is integer part */
uint32_t raw_temp = ((uint32_t)frame[*fit].payload[2] << 16) |
((uint16_t)frame[*fit].payload[1] << 8) |
frame[*fit].payload[0];
int32_t raw_temp_signed = sign_extend(raw_temp, 23);


/* Convert raw temperature: LSB = 1/65536 °C, val2 in millionths */
drv->last_sample.temperature.val1 = raw_temp / 65536;
drv->last_sample.temperature.val2 = ((int64_t)(raw_temp % 65536) * 1000000) / 65536;

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