From e013aaa016fd883a6ac545c851ecb1f5b1e60874 Mon Sep 17 00:00:00 2001 From: Paul Ma Date: Fri, 22 Jun 2018 18:11:04 +0800 Subject: phaser: fix lis2de resolution and sensor value output lis2de only support 8-bit resolution. This is what is different from lis2dh. This patch add two defines (CONFIG_ACCEL_LIS2DH and CONFIG_ACCEL_LIS2DE) to defferientiate them. For calculate_lid_angle() reliable calculation, code asks for 16 bits sensor value output, this patch fix that as well. Signed-off-by: Paul Ma BRANCH=none BUG=b:110604678 TEST=boot phaser360 board, use 'accelinfo on' to watch lid output and angle output, lid axis value should be 16 bit range and angle is correct. Change-Id: If77954ecf477736b9168b3a3b6805396a3201a57 Reviewed-on: https://chromium-review.googlesource.com/1111775 Commit-Ready: Paul Ma Tested-by: Paul Ma Reviewed-by: Jett Rink --- board/phaser/board.h | 2 +- driver/accel_lis2dh.h | 10 +++++++--- driver/build.mk | 2 +- driver/stm_mems_common.c | 7 +++++-- include/config.h | 20 ++++++++++++++++++++ 5 files changed, 34 insertions(+), 7 deletions(-) diff --git a/board/phaser/board.h b/board/phaser/board.h index 4c5a41059a..c8c866b703 100644 --- a/board/phaser/board.h +++ b/board/phaser/board.h @@ -30,7 +30,7 @@ #define CONFIG_CMD_ACCEL_INFO /* Sensors */ -#define CONFIG_ACCEL_LIS2DH /* Lid accel */ +#define CONFIG_ACCEL_LIS2DE /* Lid accel */ #define CONFIG_ACCELGYRO_LSM6DSM /* Base accel */ /* Sensors without hardware FIFO are in forced mode */ #define CONFIG_ACCEL_FORCE_MODE_MASK (1 << LID_ACCEL) diff --git a/driver/accel_lis2dh.h b/driver/accel_lis2dh.h index 0421f79503..fe8d17641e 100644 --- a/driver/accel_lis2dh.h +++ b/driver/accel_lis2dh.h @@ -102,11 +102,15 @@ enum lis2dh_odr { #define LIS2DH_FS_TO_REG(_fs) (__fls(_fs) - 1) /* Sensor resolution in number of bits - * This sensor has variable precision (8/10/12 bits) depending Power Mode - * selected - * TODO: Only Normal Power mode supported (10 bits) + * lis2dh has variable precision (8/10/12 bits) depending Power Mode + * selected, here Only Normal Power mode supported (10 bits). + * But for lis2de, it has only one 8bit resolution. */ +#ifdef CONFIG_ACCEL_LIS2DE +#define LIS2DH_RESOLUTION 8 +#elif defined(CONFIG_ACCEL_LIS2DH) #define LIS2DH_RESOLUTION 10 +#endif extern const struct accelgyro_drv lis2dh_drv; diff --git a/driver/build.mk b/driver/build.mk index 68b14f836e..6d0990bcec 100644 --- a/driver/build.mk +++ b/driver/build.mk @@ -14,7 +14,7 @@ driver-$(CONFIG_ACCELGYRO_LSM6DS0)+=accelgyro_lsm6ds0.o driver-$(CONFIG_ACCELGYRO_BMI160)+=accelgyro_bmi160.o driver-$(CONFIG_MAG_BMI160_BMM150)+=mag_bmm150.o driver-$(CONFIG_ACCELGYRO_LSM6DSM)+=accelgyro_lsm6dsm.o stm_mems_common.o -driver-$(CONFIG_ACCEL_LIS2DH)+=accel_lis2dh.o stm_mems_common.o +driver-$(CONFIG_ACCEL_LIS2D_COMMON)+=accel_lis2dh.o stm_mems_common.o driver-$(CONFIG_SYNC)+=sync.o # BC1.2 Charger Detection Devices diff --git a/driver/stm_mems_common.c b/driver/stm_mems_common.c index c7b84b5b98..427fc8091f 100644 --- a/driver/stm_mems_common.c +++ b/driver/stm_mems_common.c @@ -134,10 +134,13 @@ void st_normalize(const struct motion_sensor_t *s, vector_3_t v, uint8_t *data) { int i, range; struct stprivate_data *drvdata = s->drv_data; + /* data is left-aligned and the bottom bits need to be + * cleared because they may contain trash data. + */ + uint16_t mask = ~((1 << (16 - drvdata->resol)) - 1); for (i = X; i <= Z; i++) { - v[i] = (int16_t)((data[i * 2 + 1] << 8) | - data[i * 2]) >> (16 - drvdata->resol); + v[i] = ((data[i * 2 + 1] << 8) | data[i * 2]) & mask; } rotate(v, *s->rot_standard_ref, v); diff --git a/include/config.h b/include/config.h index 42cbe178ec..ad23544891 100644 --- a/include/config.h +++ b/include/config.h @@ -64,7 +64,17 @@ #undef CONFIG_ACCEL_BMA255 #undef CONFIG_ACCEL_KXCJ9 #undef CONFIG_ACCEL_KX022 +/* + * lis2dh and lis2de have the same register interface but different + * supported resolution. In normal mode, lis2dh works in 10-bit resolution, + * but lis2de only supports 8bit resolution. + * define CONFIG_ACCEL_LIS2DH if using lis2dh chip on the board and define + * CONFIG_ACCEL_LIS2DE if using lis2de chip. CONFIG_ACCEL_LIS2D_COMMON get + * automatically defined if either of them get defined. + */ #undef CONFIG_ACCEL_LIS2DH +#undef CONFIG_ACCEL_LIS2DE +#undef CONFIG_ACCEL_LIS2D_COMMON #undef CONFIG_ACCELGYRO_LSM6DS0 #undef CONFIG_ACCELGYRO_BMI160 #undef CONFIG_ACCELGYRO_LSM6DSM @@ -3766,6 +3776,15 @@ #endif /*****************************************************************************/ + +/* + * Automatically define CONFIG_ACCEL_LIS2D_COMMON if either child option is + * defined. + */ +#if defined(CONFIG_ACCEL_LIS2DH) || defined(CONFIG_ACCEL_LIS2DE) +#define CONFIG_ACCEL_LIS2D_COMMON +#endif + /* * Apply test config overrides last, since tests need to override some of the * config flags in non-standard ways to mock only parts of the system. @@ -3794,3 +3813,4 @@ #define CONFIG_BMI160_SEC_I2C #endif #endif /* __CROS_EC_CONFIG_H */ + -- cgit v1.2.1