Skip to content

Commit a490c90

Browse files
ttwardskartben
authored andcommitted
drivers: sensor: bmi08x: fix interfaceand trigger
1. Temperature Interface According to BMI08x datasheet, temperature reading requires both MSB and LSB bytes to be read and processed correctly. Temp data processing should follow the formula: Temp in °C = (temp_msb * 8) + (temp_lsb / 32) This patch implements the correct reading sequence and calculation method as specified in the datasheet. 2. Trigger Setting Previously we set handler and then trigger struct. However under some situation, as long as we set the handler, we get into ISR immediately and never set trigger struct. I simply changed the sequence. Testing: - Verified temperature readings match datasheet - Tested on stm32f407igh board with BMI08x sensor Fixes: #82375 Signed-off-by: Wenxi Xu <[email protected]>
1 parent d36334a commit a490c90

File tree

3 files changed

+30
-5
lines changed

3 files changed

+30
-5
lines changed

drivers/sensor/bosch/bmi08x/bmi08x_accel.c

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -454,16 +454,41 @@ static int bmi08x_temp_channel_get(const struct device *dev, struct sensor_value
454454
{
455455
uint16_t temp_raw = 0U;
456456
int32_t temp_micro = 0;
457+
int16_t temp_int11 = 0;
457458
int ret;
458459

459460
ret = bmi08x_accel_word_read(dev, BMI08X_REG_TEMP_MSB, &temp_raw);
460-
if (ret < 0) {
461+
if (!ret) {
462+
temp_int11 = (temp_raw & 0xFF) << 3;
463+
} else {
464+
LOG_ERR("Error reading BMI08X_REG_TEMP_MSB. (err %d)", ret);
461465
return ret;
462466
}
463467

464-
/* the scale is 1/2^5/LSB = 31250 micro degrees */
465-
temp_micro = BMI08X_TEMP_OFFSET * 1000000ULL + temp_raw * 31250ULL;
468+
if (temp_raw == 0x80) {
469+
/* temperature invalid */
470+
LOG_ERR("BMI08X returned invalid temperature.");
471+
return -ENODATA;
472+
}
466473

474+
ret = bmi08x_accel_word_read(dev, BMI08X_REG_TEMP_LSB, &temp_raw);
475+
if (!ret) {
476+
temp_int11 |= (temp_raw & 0xE0) >> 5;
477+
} else {
478+
LOG_ERR("Error reading BMI08X_REG_TEMP_LSB. (err %d)", ret);
479+
return ret;
480+
}
481+
/*
482+
* int11 type ranges in [-1024, 1023]
483+
* the 11st bit declares +/-
484+
* if larger than 1023, it is negative.
485+
*/
486+
if (temp_int11 > 1023) {
487+
temp_int11 -= 2048;
488+
}
489+
/* the value ranges in [-504, 496] */
490+
/* the scale is 0.125°C/LSB = 125 micro degrees */
491+
temp_micro = temp_int11 * 125 + 23 * 1000000;
467492
val->val1 = temp_micro / 1000000ULL;
468493
val->val2 = temp_micro % 1000000ULL;
469494

drivers/sensor/bosch/bmi08x/bmi08x_accel_trigger.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ int bmi08x_trigger_set_acc(const struct device *dev, const struct sensor_trigger
8989
struct bmi08x_accel_data *data = dev->data;
9090

9191
if ((trig->chan == SENSOR_CHAN_ACCEL_XYZ) && (trig->type == SENSOR_TRIG_DATA_READY)) {
92-
data->handler_drdy_acc = handler;
9392
data->drdy_trig_acc = trig;
93+
data->handler_drdy_acc = handler;
9494
return 0;
9595
}
9696

drivers/sensor/bosch/bmi08x/bmi08x_gyro_trigger.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ int bmi08x_trigger_set_gyr(const struct device *dev, const struct sensor_trigger
8989
struct bmi08x_gyro_data *data = dev->data;
9090

9191
if ((trig->chan == SENSOR_CHAN_GYRO_XYZ) && (trig->type == SENSOR_TRIG_DATA_READY)) {
92-
data->handler_drdy_gyr = handler;
9392
data->drdy_trig_gyr = trig;
93+
data->handler_drdy_gyr = handler;
9494
return 0;
9595
}
9696

0 commit comments

Comments
 (0)