diff options
author | Gwendal Grignou <gwendal@chromium.org> | 2017-02-10 10:36:15 -0800 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2017-02-13 21:15:16 -0800 |
commit | d910997f6a87cd63e196c501922f1c323a274592 (patch) | |
tree | 890cec529b7b295e51d52894e3e19a971e215ecf | |
parent | 1a736ed9542eb4c94ab0a49dba7696cda7faf864 (diff) | |
download | chrome-ec-d910997f6a87cd63e196c501922f1c323a274592.tar.gz |
lis2dh: code cleanup
Make FIFO a stack variable to save static memory,
Remove auto_inc argument, always set
Remove constant for rate 0.
Force board to declare sensor private data.
Avoid name collision in stm_mems_common
Include stm_mems_common.h in accel_lis2dh.h, caller only need to
include accel_lis2dh.h.
BUG=none
BRANCH=none
TEST=Compile with discovery_stmems board.
Change-Id: Id52b54dd4ec3cf217247c5511ad5a506067ad293
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/441144
Tested-by: mario tesi <mario.tesi@st.com>
Reviewed-by: Aseda Aboagye <aaboagye@chromium.org>
Reviewed-by: mario tesi <mario.tesi@st.com>
-rw-r--r-- | driver/accel_lis2dh.c | 60 | ||||
-rw-r--r-- | driver/accel_lis2dh.h | 9 | ||||
-rw-r--r-- | driver/stm_mems_common.c | 34 | ||||
-rw-r--r-- | driver/stm_mems_common.h | 37 | ||||
-rw-r--r-- | include/config.h | 7 |
5 files changed, 72 insertions, 75 deletions
diff --git a/driver/accel_lis2dh.c b/driver/accel_lis2dh.c index 04b5b2f590..eadc66cf71 100644 --- a/driver/accel_lis2dh.c +++ b/driver/accel_lis2dh.c @@ -15,13 +15,7 @@ #include "math_util.h" #include "task.h" #include "util.h" -#include "driver/stm_mems_common.h" - -/* Internal data structure */ -struct stprivate_data lis2dh_a_data; -#ifdef CONFIG_ACCEL_FIFO -static uint8_t fifo[FIFO_READ_LEN]; -#endif /* CONFIG_ACCEL_FIFO */ +#include "driver/accel_lis2dh.h" #ifdef CONFIG_ACCEL_FIFO /** @@ -34,13 +28,13 @@ static int enable_fifo(const struct motion_sensor_t *s, int mode, int en_dis) { int ret; - ret = write_data_with_mask(s, LIS2DH_FIFO_CTRL_REG, LIS2DH_FIFO_MODE_MASK, - mode); + ret = st_write_data_with_mask(s, LIS2DH_FIFO_CTRL_REG, + LIS2DH_FIFO_MODE_MASK, mode); if (ret != EC_SUCCESS) return ret; - ret = write_data_with_mask(s, LIS2DH_CTRL5_ADDR, LIS2DH_FIFO_EN_MASK, - en_dis); + ret = st_write_data_with_mask(s, LIS2DH_CTRL5_ADDR, LIS2DH_FIFO_EN_MASK, + en_dis); return ret; } @@ -78,7 +72,8 @@ static int set_range(const struct motion_sensor_t *s, int range, int rnd) /* Lock accel resource to prevent another task from attempting * to write accel parameters until we are done */ mutex_lock(s->mutex); - err = write_data_with_mask(s, LIS2DH_CTRL4_ADDR, LIS2DH_FS_MASK, val); + err = st_write_data_with_mask(s, LIS2DH_CTRL4_ADDR, LIS2DH_FS_MASK, + val); /* Save Gain in range for speed up data path */ if (err == EC_SUCCESS) @@ -110,10 +105,11 @@ static int set_data_rate(const struct motion_sensor_t *s, int rate, int rnd) goto unlock_rate; #endif /* CONFIG_ACCEL_FIFO */ - if (rate == ODR_POWER_OFF_VAL) { + if (rate == 0) { /* Power Off device */ - ret = write_data_with_mask(s, LIS2DH_CTRL1_ADDR, - LIS2DH_ACC_ODR_MASK, ODR_POWER_OFF_VAL); + ret = st_write_data_with_mask( + s, LIS2DH_CTRL1_ADDR, + LIS2DH_ACC_ODR_MASK, LIS2DH_ODR_0HZ_VAL); goto unlock_rate; } @@ -138,8 +134,8 @@ static int set_data_rate(const struct motion_sensor_t *s, int rate, int rnd) * Lock accel resource to prevent another task from attempting * to write accel parameters until we are done */ - ret = write_data_with_mask(s, LIS2DH_CTRL1_ADDR, LIS2DH_ACC_ODR_MASK, - reg_val); + ret = st_write_data_with_mask(s, LIS2DH_CTRL1_ADDR, LIS2DH_ACC_ODR_MASK, + reg_val); if (ret == EC_SUCCESS) data->base.odr = normalized_rate; @@ -163,6 +159,7 @@ static int load_fifo(struct motion_sensor_t *s) struct ec_response_motion_sensor_data vect; int done = 0; int *axis = s->raw_xyz; + uint8_t fifo[FIFO_READ_LEN]; /* Try to Empty FIFO */ do { @@ -184,14 +181,14 @@ static int load_fifo(struct motion_sensor_t *s) else done = 1; - ret = raw_read_n(s->port, s->addr, LIS2DH_OUT_X_L_ADDR, fifo, - nsamples, AUTO_INC); + ret = st_raw_read_n(s->port, s->addr, LIS2DH_OUT_X_L_ADDR, fifo, + nsamples); if (ret != EC_SUCCESS) return ret; for (i = 0; i < nsamples; i += OUT_XYZ_SIZE) { /* Apply precision, sensitivity and rotation vector */ - normalize(s, axis, &fifo[i]); + st_normalize(s, axis, &fifo[i]); /* Fill vector array */ vect.data[0] = axis[0]; @@ -214,12 +211,13 @@ static int config_interrupt(const struct motion_sensor_t *s) #ifdef CONFIG_ACCEL_FIFO_THRES /* configure FIFO watermark level */ - ret = write_data_with_mask(s, LIS2DH_FIFO_CTRL_REG, - LIS2DH_FIFO_THR_MASK, CONFIG_ACCEL_FIFO_THRES); + ret = st_write_data_with_mask(s, LIS2DH_FIFO_CTRL_REG, + LIS2DH_FIFO_THR_MASK, + CONFIG_ACCEL_FIFO_THRES); if (ret != EC_SUCCESS) return ret; /* enable interrupt on FIFO watermask and route to int1 */ - ret = write_data_with_mask(s, LIS2DH_CTRL3_ADDR, + ret = st_write_data_with_mask(s, LIS2DH_CTRL3_ADDR, LIS2DH_FIFO_WTM_INT_MASK, 1); #endif /* CONFIG_ACCEL_FIFO */ @@ -301,8 +299,8 @@ static int read(const struct motion_sensor_t *s, vector_3_t v) } /* Read output data bytes starting at LIS2DH_OUT_X_L_ADDR */ - ret = raw_read_n(s->port, s->addr, LIS2DH_OUT_X_L_ADDR, raw, - OUT_XYZ_SIZE, AUTO_INC); + ret = st_raw_read_n(s->port, s->addr, LIS2DH_OUT_X_L_ADDR, raw, + OUT_XYZ_SIZE); if (ret != EC_SUCCESS) { CPRINTF("[%T %s type:0x%X RD XYZ Error]", s->name, s->type); @@ -310,7 +308,7 @@ static int read(const struct motion_sensor_t *s, vector_3_t v) } /* Transform from LSB to real data with rotation and gain */ - normalize(s, v, raw); + st_normalize(s, v, raw); /* apply offset in the device coordinates */ for (i = X; i <= Z; i++) @@ -396,12 +394,12 @@ const struct accelgyro_drv lis2dh_drv = { .read = read, .set_range = set_range, .get_range = get_range, - .set_resolution = set_resolution, - .get_resolution = get_resolution, + .set_resolution = st_set_resolution, + .get_resolution = st_get_resolution, .set_data_rate = set_data_rate, - .get_data_rate = get_data_rate, - .set_offset = set_offset, - .get_offset = get_offset, + .get_data_rate = st_get_data_rate, + .set_offset = st_set_offset, + .get_offset = st_get_offset, .perform_calib = NULL, #ifdef CONFIG_ACCEL_FIFO .load_fifo = load_fifo, diff --git a/driver/accel_lis2dh.h b/driver/accel_lis2dh.h index eac16d5e20..9872ccabe4 100644 --- a/driver/accel_lis2dh.h +++ b/driver/accel_lis2dh.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2016 The Chromium OS Authors. All rights reserved. +/* Copyright 2016 The Chromium OS Authors. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -8,6 +8,8 @@ #ifndef __CROS_EC_ACCEL_LIS2DH_H #define __CROS_EC_ACCEL_LIS2DH_H +#include "driver/stm_mems_common.h" + #define LIS2DH_I2C_ADDR(__x) (__x << 1) /* 7-bit address is 000110Xb. Where 'X' is determined @@ -88,7 +90,8 @@ enum lis2dh_fifo_modes { /* Acc data rate */ enum lis2dh_odr { - LIS2DH_ODR_1HZ_VAL = 0x01, + LIS2DH_ODR_0HZ_VAL = 0, + LIS2DH_ODR_1HZ_VAL, LIS2DH_ODR_10HZ_VAL, LIS2DH_ODR_25HZ_VAL, LIS2DH_ODR_50HZ_VAL, @@ -158,4 +161,6 @@ enum lis2dh_fs { extern const struct accelgyro_drv lis2dh_drv; +void lis2dh_interrupt(enum gpio_signal signal); + #endif /* __CROS_EC_ACCEL_LIS2DH_H */ diff --git a/driver/stm_mems_common.c b/driver/stm_mems_common.c index 9e482583b3..4655171b23 100644 --- a/driver/stm_mems_common.c +++ b/driver/stm_mems_common.c @@ -1,4 +1,4 @@ -/* Copyright (c) 2016 The Chromium OS Authors. All rights reserved. +/* Copyright 2016 The Chromium OS Authors. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -11,8 +11,7 @@ /** * Read single register */ -inline int raw_read8(const int port, const int addr, const int reg, - int *data_ptr) +int raw_read8(const int port, const int addr, const int reg, int *data_ptr) { /* TODO: Implement SPI interface support */ return i2c_read8(port, addr, reg, data_ptr); @@ -21,8 +20,7 @@ inline int raw_read8(const int port, const int addr, const int reg, /** * Write single register */ -inline int raw_write8(const int port, const int addr, const int reg, - int data) +int raw_write8(const int port, const int addr, const int reg, int data) { /* TODO: Implement SPI interface support */ return i2c_write8(port, addr, reg, data); @@ -34,14 +32,11 @@ inline int raw_write8(const int port, const int addr, const int reg, * MSB must be set for autoincrement in multi read when auto_inc * is set */ -int raw_read_n(const int port, const int addr, const uint8_t reg, - uint8_t *data_ptr, const int len, int auto_inc) +int st_raw_read_n(const int port, const int addr, const uint8_t reg, + uint8_t *data_ptr, const int len) { int rv = -EC_ERROR_PARAM1; - uint8_t reg_a = reg; - - if (auto_inc) - reg_a |= AUTO_INC; + uint8_t reg_a = reg | 0x80; /* TODO: Implement SPI interface support */ i2c_lock(port, 1); @@ -58,7 +53,7 @@ int raw_read_n(const int port, const int addr, const uint8_t reg, * @mask: The mask to search * @data: Data pointer */ -int write_data_with_mask(const struct motion_sensor_t *s, int reg, +int st_write_data_with_mask(const struct motion_sensor_t *s, int reg, uint8_t mask, uint8_t data) { int err; @@ -68,7 +63,8 @@ int write_data_with_mask(const struct motion_sensor_t *s, int reg, if (err != EC_SUCCESS) return err; - new_data = ((old_data & (~mask)) | ((data << __builtin_ctz(mask)) & mask)); + new_data = ((old_data & (~mask)) | + ((data << __builtin_ctz(mask)) & mask)); if (new_data == old_data) return EC_SUCCESS; @@ -84,7 +80,7 @@ int write_data_with_mask(const struct motion_sensor_t *s, int reg, * * TODO: must support multiple resolution */ -int set_resolution(const struct motion_sensor_t *s, int res, int rnd) +int st_set_resolution(const struct motion_sensor_t *s, int res, int rnd) { return EC_SUCCESS; } @@ -95,7 +91,7 @@ int set_resolution(const struct motion_sensor_t *s, int res, int rnd) * * TODO: must support multiple resolution */ -int get_resolution(const struct motion_sensor_t *s) +int st_get_resolution(const struct motion_sensor_t *s) { struct stprivate_data *data = s->drv_data; @@ -108,7 +104,7 @@ int get_resolution(const struct motion_sensor_t *s) * @offset: offset vector * @temp: Temp */ -int set_offset(const struct motion_sensor_t *s, +int st_set_offset(const struct motion_sensor_t *s, const int16_t *offset, int16_t temp) { struct stprivate_data *data = s->drv_data; @@ -125,7 +121,7 @@ int set_offset(const struct motion_sensor_t *s, * @offset: offset vector * @temp: Temp */ -int get_offset(const struct motion_sensor_t *s, +int st_get_offset(const struct motion_sensor_t *s, int16_t *offset, int16_t *temp) { struct stprivate_data *data = s->drv_data; @@ -141,7 +137,7 @@ int get_offset(const struct motion_sensor_t *s, * get_data_rate - Get data rate (ODR) * @s: Motion sensor pointer */ -int get_data_rate(const struct motion_sensor_t *s) +int st_get_data_rate(const struct motion_sensor_t *s) { struct stprivate_data *data = s->drv_data; @@ -154,7 +150,7 @@ int get_data_rate(const struct motion_sensor_t *s) * @v: output vector * @data: LSB raw data */ -void normalize(const struct motion_sensor_t *s, vector_3_t v, uint8_t *data) +void st_normalize(const struct motion_sensor_t *s, vector_3_t v, uint8_t *data) { int i; struct stprivate_data *drvdata = s->drv_data; diff --git a/driver/stm_mems_common.h b/driver/stm_mems_common.h index ca08de95de..5fa16936b7 100644 --- a/driver/stm_mems_common.h +++ b/driver/stm_mems_common.h @@ -1,4 +1,4 @@ -/* Copyright (c) 2016 The Chromium OS Authors. All rights reserved. +/* Copyright 2016 The Chromium OS Authors. All rights reserved. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -13,20 +13,13 @@ #include "accelgyro.h" #include "console.h" #include "i2c.h" -#include "driver/accel_lis2dh.h" /* Common debug funcions */ #define CPRINTF(format, args...) cprintf(CC_ACCEL, format "\n", ## args) -/* Set auto increment subaddress on multiple access */ -#define AUTO_INC 0x80 - /* X, Y, Z axis data len */ #define OUT_XYZ_SIZE 6 -/* Common define for poswer off ODR */ -#define ODR_POWER_OFF_VAL 0x00 - #ifdef CONFIG_ACCEL_FIFO #define FIFO_BUFFER_NUM_PATTERN 16 /* Define number of data to be read from FIFO each time @@ -42,23 +35,20 @@ /** * Read single register */ -inline int raw_read8(const int port, const int addr, const int reg, +extern inline int raw_read8(const int port, const int addr, const int reg, int *data_ptr); /** * Write single register */ -inline int raw_write8(const int port, const int addr, const int reg, +extern inline int raw_write8(const int port, const int addr, const int reg, int data); /** * Read n bytes for read - * NOTE: Some chip use MSB for auto-increments in SUB address - * MSB must be set for autoincrement in multi read when auto_inc - * is set */ -int raw_read_n(const int port, const int addr, const uint8_t reg, - uint8_t *data_ptr, const int len, int auto_inc); +int st_raw_read_n(const int port, const int addr, const uint8_t reg, + uint8_t *data_ptr, const int len); /** * write_data_with_mask - Write register with mask @@ -67,7 +57,7 @@ int raw_read_n(const int port, const int addr, const uint8_t reg, * @mask: The mask to search * @data: Data pointer */ -int write_data_with_mask(const struct motion_sensor_t *s, int reg, +int st_write_data_with_mask(const struct motion_sensor_t *s, int reg, uint8_t mask, uint8_t data); /** @@ -76,7 +66,7 @@ int write_data_with_mask(const struct motion_sensor_t *s, int reg, * @res: Bit resolution * @rnd: Round bit */ -int set_resolution(const struct motion_sensor_t *s, int res, int rnd); +int st_set_resolution(const struct motion_sensor_t *s, int res, int rnd); /** * get_resolution - Get bit resolution @@ -84,7 +74,7 @@ int set_resolution(const struct motion_sensor_t *s, int res, int rnd); * * TODO: must support multiple resolution */ -int get_resolution(const struct motion_sensor_t *s); +int st_get_resolution(const struct motion_sensor_t *s); /** * set_offset - Set data offset @@ -92,8 +82,8 @@ int get_resolution(const struct motion_sensor_t *s); * @offset: offset vector * @temp: Temp */ -int set_offset(const struct motion_sensor_t *s, - const int16_t *offset, int16_t temp); +int st_set_offset(const struct motion_sensor_t *s, + const int16_t *offset, int16_t temp); /** * get_offset - Get data offset @@ -101,13 +91,14 @@ int set_offset(const struct motion_sensor_t *s, * @offset: offset vector * @temp: Temp */ -int get_offset(const struct motion_sensor_t *s, int16_t *offset, int16_t *temp); +int st_get_offset(const struct motion_sensor_t *s, + int16_t *offset, int16_t *temp); /** * get_data_rate - Get data rate (ODR) * @s: Motion sensor pointer */ -int get_data_rate(const struct motion_sensor_t *s); +int st_get_data_rate(const struct motion_sensor_t *s); /** * normalize - Apply to LSB data sensitivity and rotation @@ -115,7 +106,7 @@ int get_data_rate(const struct motion_sensor_t *s); * @v: vector * @data: LSB raw data */ -void normalize(const struct motion_sensor_t *s, vector_3_t v, uint8_t *data); +void st_normalize(const struct motion_sensor_t *s, vector_3_t v, uint8_t *data); /* Internal data structure for sensors */ struct stprivate_data { diff --git a/include/config.h b/include/config.h index d13757f222..8550865556 100644 --- a/include/config.h +++ b/include/config.h @@ -64,6 +64,7 @@ #undef CONFIG_ACCEL_BMA255 #undef CONFIG_ACCEL_KXCJ9 #undef CONFIG_ACCEL_KX022 +#undef CONFIG_ACCEL_LIS2DH #undef CONFIG_ACCELGYRO_LSM6DS0 #undef CONFIG_ACCELGYRO_BMI160 #undef CONFIG_ACCELGYRO_LSM6DSM @@ -106,6 +107,12 @@ /* Specify type of Gyrometers attached. */ #undef CONFIG_GYRO_L3GD20H +/* + * Define the event to raise when LIS2DH interrupt. + * Must be within TASK_EVENT_MOTION_INTERRUPT_MASK. + */ +#undef CONFIG_ACCEL_LIS2DH_INT_EVENT + /* Compile chip support for analog-to-digital convertor */ #undef CONFIG_ADC |