From 606acbd904635e08344c7050097232f2e50f4e52 Mon Sep 17 00:00:00 2001 From: Ching-Kang Yen Date: Thu, 16 Jan 2020 16:11:40 +0800 Subject: 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 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2002998 Reviewed-by: Gwendal Grignou Commit-Queue: Gwendal Grignou (cherry picked from commit 036e9f7b214b763524497550aae0ee94df8fd536) Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2247976 Reviewed-by: Inno.Park Tested-by: Inno.Park Commit-Queue: Bob Moragues --- common/math_util.c | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'common/math_util.c') diff --git a/common/math_util.c b/common/math_util.c index ad98aecdf5..813847344b 100644 --- a/common/math_util.c +++ b/common/math_util.c @@ -238,3 +238,11 @@ void rotate_inv(const vector_3_t v, const matrix_3x3_t R, vector_3_t res) res[1] = FP_TO_INT(fp_div(t[1], deter)); res[2] = FP_TO_INT(fp_div(t[2], deter)); } + +/* division that round to the nearest integer */ +int round_divide(int64_t dividend, int divisor) +{ + return (dividend > 0) ^ (divisor > 0) ? + (dividend - divisor / 2) / divisor : + (dividend + divisor / 2) / divisor; +} -- cgit v1.2.1