diff options
author | Ching-Kang Yen <chingkang@chromium.org> | 2020-01-16 16:11:40 +0800 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2020-06-23 23:03:37 +0000 |
commit | e6dde3dfd93b326fa1eef39031a49e267a1c23cc (patch) | |
tree | aa4969aa5392f9950353240415dd8117e5d181c0 /driver/accelgyro_bmi160.c | |
parent | 5dba972ba39d75bfcb20f2dd473be55c0a76d33e (diff) | |
download | chrome-ec-e6dde3dfd93b326fa1eef39031a49e267a1c23cc.tar.gz |
driver: bmi160: Fix rounding error in set_offset() and get_offset()
The original set_offset() and get_offset() codes in the
driver/accelgyro_bmi160 use simple divisions to write the data.
The more times the set_offset() and get_offset() is used, the
data will get closer to 0.
Fixing it by replacing simple division to round_divide(), division
that round to nearest, in the common/math_util.c.
BRANCH=octopus
BUG=b:146823505
TEST=Testing on octopus:ampton on branch [firmware-octopus-11297.B].
Checking the data did not rounding to 0.
Change-Id: Ide9df9e32fc501e63d6f952cb8254df7662afd23
Signed-off-by: Ching-Kang Yen <chingkang@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2002998
Reviewed-by: Gwendal Grignou <gwendal@chromium.org>
Commit-Queue: Gwendal Grignou <gwendal@chromium.org>
(cherry picked from commit 036e9f7b214b763524497550aae0ee94df8fd536)
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2249638
Reviewed-by: Inno.Park <ih.yoo.park@samsung.corp-partner.google.com>
Tested-by: Inno.Park <ih.yoo.park@samsung.corp-partner.google.com>
Commit-Queue: Bob Moragues <moragues@chromium.org>
Diffstat (limited to 'driver/accelgyro_bmi160.c')
-rw-r--r-- | driver/accelgyro_bmi160.c | 21 |
1 files changed, 13 insertions, 8 deletions
diff --git a/driver/accelgyro_bmi160.c b/driver/accelgyro_bmi160.c index d8a3cb438b..ac4bf3c68e 100644 --- a/driver/accelgyro_bmi160.c +++ b/driver/accelgyro_bmi160.c @@ -485,8 +485,10 @@ static int get_offset(const struct motion_sensor_t *s, &val); if (val > 0x7f) val = -256 + val; - v[i] = val * BMI160_OFFSET_ACC_MULTI_MG / - BMI160_OFFSET_ACC_DIV_MG; + v[i] = round_divide( + (int64_t)val * BMI160_OFFSET_ACC_MULTI_MG, + BMI160_OFFSET_ACC_DIV_MG); + } break; case MOTIONSENSE_TYPE_GYRO: @@ -504,8 +506,9 @@ static int get_offset(const struct motion_sensor_t *s, val |= ((val98 >> (2 * i)) & 0x3) << 8; if (val > 0x1ff) val = -1024 + val; - v[i] = val * BMI160_OFFSET_GYRO_MULTI_MDS / - BMI160_OFFSET_GYRO_DIV_MDS; + v[i] = round_divide( + (int64_t)val * BMI160_OFFSET_GYRO_MULTI_MDS, + BMI160_OFFSET_GYRO_DIV_MDS); } break; #ifdef CONFIG_MAG_BMI160_BMM150 @@ -542,8 +545,9 @@ static int set_offset(const struct motion_sensor_t *s, switch (s->type) { case MOTIONSENSE_TYPE_ACCEL: for (i = X; i <= Z; i++) { - val = v[i] * BMI160_OFFSET_ACC_DIV_MG / - BMI160_OFFSET_ACC_MULTI_MG; + val = round_divide( + (int64_t)v[i] * BMI160_OFFSET_ACC_DIV_MG, + BMI160_OFFSET_ACC_MULTI_MG); if (val > 127) val = 127; if (val < -128) @@ -558,8 +562,9 @@ static int set_offset(const struct motion_sensor_t *s, break; case MOTIONSENSE_TYPE_GYRO: for (i = X; i <= Z; i++) { - val = v[i] * BMI160_OFFSET_GYRO_DIV_MDS / - BMI160_OFFSET_GYRO_MULTI_MDS; + val = round_divide( + (int64_t)v[i] * BMI160_OFFSET_GYRO_DIV_MDS, + BMI160_OFFSET_GYRO_MULTI_MDS); if (val > 511) val = 511; if (val < -512) |