diff options
author | Tomasz Michalec <tm@semihalf.com> | 2021-07-14 00:44:44 +0200 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-07-14 21:24:55 +0000 |
commit | 0773847d80c8821da2b277c6e007b6948eb350aa (patch) | |
tree | 995136cd3f73c4de6cc94f68af70cfda97fa7de7 | |
parent | 1369994e674688cb1d4f045b48b1b4b1836ae01c (diff) | |
download | chrome-ec-0773847d80c8821da2b277c6e007b6948eb350aa.tar.gz |
driver: bmi160: bmi260: Minor driver fixes
Fix three issues with BMI160 and BMI260 drivers:
- get/set acclerometer/gyroscope offset will return error on failed read
of offset register
- BMI160 calibration function returns error when setting range fail
- Invalid temperature is properly recognized by driver
BUG=none
BRANCH=none
TEST=run zmake drivers test
Signed-off-by: Tomasz Michalec <tm@semihalf.com>
Change-Id: I545c0a931227ef7efc000ec97c1f6297a48e6d1a
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3027039
Reviewed-by: Simon Glass <sjg@chromium.org>
Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
Reviewed-by: Keith Short <keithshort@chromium.org>
-rw-r--r-- | driver/accelgyro_bmi160.c | 14 | ||||
-rw-r--r-- | driver/accelgyro_bmi260.c | 10 | ||||
-rw-r--r-- | driver/accelgyro_bmi_common.c | 72 | ||||
-rw-r--r-- | include/driver/accelgyro_bmi_common.h | 8 |
4 files changed, 72 insertions, 32 deletions
diff --git a/driver/accelgyro_bmi160.c b/driver/accelgyro_bmi160.c index daa47965d8..66dffc0db5 100644 --- a/driver/accelgyro_bmi160.c +++ b/driver/accelgyro_bmi160.c @@ -211,13 +211,19 @@ static int set_offset(const struct motion_sensor_t *s, switch (s->type) { case MOTIONSENSE_TYPE_ACCEL: - bmi_set_accel_offset(s, v); + ret = bmi_set_accel_offset(s, v); + if (ret != EC_SUCCESS) + return ret; + ret = bmi_write8(s->port, s->i2c_spi_addr_flags, BMI160_OFFSET_EN_GYR98, val98 | BMI160_OFFSET_ACC_EN); break; case MOTIONSENSE_TYPE_GYRO: - bmi_set_gyro_offset(s, v, &val98); + ret = bmi_set_gyro_offset(s, v, &val98); + if (ret != EC_SUCCESS) + return ret; + ret = bmi_write8(s->port, s->i2c_spi_addr_flags, BMI160_OFFSET_EN_GYR98, val98 | BMI160_OFFSET_GYRO_EN); @@ -246,7 +252,9 @@ static int perform_calib(struct motion_sensor_t *s, int enable) * Temporary set frequency to 100Hz to get enough data in a short * period of time. */ - set_data_rate(s, 100000, 0); + ret = set_data_rate(s, 100000, 0); + if (ret != EC_SUCCESS) + goto end_perform_calib; switch (s->type) { case MOTIONSENSE_TYPE_ACCEL: diff --git a/driver/accelgyro_bmi260.c b/driver/accelgyro_bmi260.c index 21d783fe84..7fbbb073cd 100644 --- a/driver/accelgyro_bmi260.c +++ b/driver/accelgyro_bmi260.c @@ -161,13 +161,19 @@ static int set_offset(const struct motion_sensor_t *s, switch (s->type) { case MOTIONSENSE_TYPE_ACCEL: - bmi_set_accel_offset(s, v); + ret = bmi_set_accel_offset(s, v); + if (ret != EC_SUCCESS) + return ret; + ret = bmi_write8(s->port, s->i2c_spi_addr_flags, BMI260_NV_CONF, val_nv_conf | BMI260_ACC_OFFSET_EN); break; case MOTIONSENSE_TYPE_GYRO: - bmi_set_gyro_offset(s, v, &val98); + ret = bmi_set_gyro_offset(s, v, &val98); + if (ret != EC_SUCCESS) + return ret; + ret = bmi_write8(s->port, s->i2c_spi_addr_flags, BMI260_OFFSET_EN_GYR98, val98 | BMI260_OFFSET_GYRO_EN); diff --git a/driver/accelgyro_bmi_common.c b/driver/accelgyro_bmi_common.c index 7dff59fc5b..5bd7cef42c 100644 --- a/driver/accelgyro_bmi_common.c +++ b/driver/accelgyro_bmi_common.c @@ -570,7 +570,7 @@ int bmi_get_data_rate(const struct motion_sensor_t *s) int bmi_get_offset(const struct motion_sensor_t *s, int16_t *offset, int16_t *temp) { - int i; + int i, ret = EC_SUCCESS; intv3_t v; switch (s->type) { @@ -580,7 +580,7 @@ int bmi_get_offset(const struct motion_sensor_t *s, int16_t *offset, * two-complement number in units of 3.9 mg independent of the * range selected for the accelerometer. */ - bmi_accel_get_offset(s, v); + ret = bmi_accel_get_offset(s, v); break; case MOTIONSENSE_TYPE_GYRO: /* @@ -589,17 +589,21 @@ int bmi_get_offset(const struct motion_sensor_t *s, int16_t *offset, * Therefore a maximum range that can be compensated is * -31.25 °/s to +31.25 °/s */ - bmi_gyro_get_offset(s, v); + ret = bmi_gyro_get_offset(s, v); break; #ifdef CONFIG_MAG_BMI_BMM150 case MOTIONSENSE_TYPE_MAG: - bmm150_get_offset(s, v); + ret = bmm150_get_offset(s, v); break; #endif /* defined(CONFIG_MAG_BMI_BMM150) */ default: for (i = X; i <= Z; i++) v[i] = 0; } + + if (ret != EC_SUCCESS) + return ret; + rotate(v, *s->rot_standard_ref, v); offset[X] = v[X]; offset[Y] = v[Y]; @@ -732,7 +736,7 @@ int bmi_get_sensor_temp(int idx, int *temp_ptr) BMI_TEMPERATURE_0(V(s)), (uint8_t *)&temp, sizeof(temp)); - if (ret || temp == BMI_INVALID_TEMP) + if (ret || temp == (int16_t)BMI_INVALID_TEMP) return EC_ERROR_NOT_POWERED; *temp_ptr = C_TO_K(23 + ((temp + 256) >> 9)); @@ -775,41 +779,54 @@ int bmi_get_normalized_rate(const struct motion_sensor_t *s, int rate, int rnd, return EC_SUCCESS; } -void bmi_accel_get_offset(const struct motion_sensor_t *accel, intv3_t v) +int bmi_accel_get_offset(const struct motion_sensor_t *accel, intv3_t v) { - int i, val; + int i, val, ret; for (i = X; i <= Z; i++) { - bmi_read8(accel->port, accel->i2c_spi_addr_flags, - BMI_OFFSET_ACC70(V(accel)) + i, &val); + ret = bmi_read8(accel->port, accel->i2c_spi_addr_flags, + BMI_OFFSET_ACC70(V(accel)) + i, &val); + if (ret != EC_SUCCESS) + return ret; + if (val > 0x7f) val = -256 + val; v[i] = round_divide((int64_t)val * BMI_OFFSET_ACC_MULTI_MG, BMI_OFFSET_ACC_DIV_MG); } + + return EC_SUCCESS; } -void bmi_gyro_get_offset(const struct motion_sensor_t *gyro, intv3_t v) +int bmi_gyro_get_offset(const struct motion_sensor_t *gyro, intv3_t v) { - int i, val, val98; + int i, val, val98, ret; /* Read the MSB first */ - bmi_read8(gyro->port, gyro->i2c_spi_addr_flags, - BMI_OFFSET_EN_GYR98(V(gyro)), &val98); + ret = bmi_read8(gyro->port, gyro->i2c_spi_addr_flags, + BMI_OFFSET_EN_GYR98(V(gyro)), &val98); + if (ret != EC_SUCCESS) + return ret; + for (i = X; i <= Z; i++) { - bmi_read8(gyro->port, gyro->i2c_spi_addr_flags, - BMI_OFFSET_GYR70(V(gyro)) + i, &val); + ret = bmi_read8(gyro->port, gyro->i2c_spi_addr_flags, + BMI_OFFSET_GYR70(V(gyro)) + i, &val); + if (ret != EC_SUCCESS) + return ret; + val |= ((val98 >> (2 * i)) & 0x3) << 8; if (val > 0x1ff) val = -1024 + val; v[i] = round_divide((int64_t)val * BMI_OFFSET_GYRO_MULTI_MDS, BMI_OFFSET_GYRO_DIV_MDS); } + + return EC_SUCCESS; } -void bmi_set_accel_offset(const struct motion_sensor_t *accel, intv3_t v) +int bmi_set_accel_offset(const struct motion_sensor_t *accel, intv3_t v) { - int i, val; + int i, val, ret; for (i = X; i <= Z; ++i) { val = round_divide((int64_t)v[i] * BMI_OFFSET_ACC_DIV_MG, @@ -820,15 +837,19 @@ void bmi_set_accel_offset(const struct motion_sensor_t *accel, intv3_t v) val = -128; if (val < 0) val = 256 + val; - bmi_write8(accel->port, accel->i2c_spi_addr_flags, - BMI_OFFSET_ACC70(V(accel)) + i, val); + ret = bmi_write8(accel->port, accel->i2c_spi_addr_flags, + BMI_OFFSET_ACC70(V(accel)) + i, val); + if (ret != EC_SUCCESS) + return ret; } + + return EC_SUCCESS; } -void bmi_set_gyro_offset(const struct motion_sensor_t *gyro, intv3_t v, +int bmi_set_gyro_offset(const struct motion_sensor_t *gyro, intv3_t v, int *val98_ptr) { - int i, val; + int i, val, ret; for (i = X; i <= Z; i++) { val = round_divide((int64_t)v[i] * BMI_OFFSET_GYRO_DIV_MDS, @@ -839,11 +860,16 @@ void bmi_set_gyro_offset(const struct motion_sensor_t *gyro, intv3_t v, val = -512; if (val < 0) val = 1024 + val; - bmi_write8(gyro->port, gyro->i2c_spi_addr_flags, - BMI_OFFSET_GYR70(V(gyro)) + i, val & 0xFF); + ret = bmi_write8(gyro->port, gyro->i2c_spi_addr_flags, + BMI_OFFSET_GYR70(V(gyro)) + i, val & 0xFF); + if (ret != EC_SUCCESS) + return ret; + *val98_ptr &= ~(0x3 << (2 * i)); *val98_ptr |= (val >> 8) << (2 * i); } + + return EC_SUCCESS; } #ifdef CONFIG_BMI_ORIENTATION_SENSOR diff --git a/include/driver/accelgyro_bmi_common.h b/include/driver/accelgyro_bmi_common.h index 398f04dc42..c403999c5a 100644 --- a/include/driver/accelgyro_bmi_common.h +++ b/include/driver/accelgyro_bmi_common.h @@ -301,16 +301,16 @@ int bmi_get_normalized_rate(const struct motion_sensor_t *s, int rate, int rnd, int *normalized_rate_ptr, uint8_t *reg_val_ptr); /* Get the accelerometer offset */ -void bmi_accel_get_offset(const struct motion_sensor_t *accel, intv3_t v); +int bmi_accel_get_offset(const struct motion_sensor_t *accel, intv3_t v); /* Get the gyroscope offset */ -void bmi_gyro_get_offset(const struct motion_sensor_t *gyro, intv3_t v); +int bmi_gyro_get_offset(const struct motion_sensor_t *gyro, intv3_t v); /* Set the accelerometer offset */ -void bmi_set_accel_offset(const struct motion_sensor_t *accel, intv3_t v); +int bmi_set_accel_offset(const struct motion_sensor_t *accel, intv3_t v); /* Set the gyroscope offset */ -void bmi_set_gyro_offset(const struct motion_sensor_t *gyro, intv3_t v, +int bmi_set_gyro_offset(const struct motion_sensor_t *gyro, intv3_t v, int *val98_ptr); #endif /* __CROS_EC_ACCELGYRO_BMI_COMMON_H */ |