summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGwendal Grignou <gwendal@chromium.org>2016-09-07 15:21:12 -0700
committerchrome-bot <chrome-bot@chromium.org>2016-09-09 00:04:28 -0700
commit60fc54854cffea213933cccd223ad80770cb5fb3 (patch)
tree45de68a5d3317649aff414f23048e70edabeffca
parentbcd2872e786f43e6507c0d7bd17029cc7c1f429c (diff)
downloadchrome-ec-60fc54854cffea213933cccd223ad80770cb5fb3.tar.gz
driver: bmp280: Add range
Data from the sensor (in Pa) does not fit in 16 bits. Add set_range/get_range to allow the AP to set the precision. For pressure around ~1000 hPa, we need to right shift by 2 bits. BUG=chrome-os-partner:57117 BRANCH=reef TEST=Check data is not truncated anymore: > accelrange 4 Range for sensor 4: 262144 (Pa ~= 2621 hPa) > accelread 4 Current data 4: 24030 0 0 Last calib. data 4: 24030 0 0 (x4 = 961.2 hPa) Change-Id: I3f7280336e5120d903116612c9c830f4150d2ed7 Signed-off-by: Gwendal Grignou <gwendal@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/382323 Reviewed-by: Aseda Aboagye <aaboagye@chromium.org>
-rw-r--r--board/reef/board.c2
-rw-r--r--driver/baro_bmp280.c25
-rw-r--r--driver/baro_bmp280.h5
3 files changed, 28 insertions, 4 deletions
diff --git a/board/reef/board.c b/board/reef/board.c
index 64ca396e94..e3e0872ab7 100644
--- a/board/reef/board.c
+++ b/board/reef/board.c
@@ -875,7 +875,7 @@ struct motion_sensor_t motion_sensors[] = {
.drv_data = &bmp280_drv_data,
.port = I2C_PORT_BARO,
.addr = BMP280_I2C_ADDRESS1,
- .rot_standard_ref = NULL, /* Identity Matrix. */
+ .default_range = 1 << 18, /* 1bit = 4 Pa, 16bit ~= 2600 hPa */
.config = {
/* AP: by default shutdown all sensors */
[SENSOR_CONFIG_AP] = {
diff --git a/driver/baro_bmp280.c b/driver/baro_bmp280.c
index b568407265..d2fb25d378 100644
--- a/driver/baro_bmp280.c
+++ b/driver/baro_bmp280.c
@@ -276,6 +276,26 @@ static int bmp280_set_power_mode(const struct motion_sensor_t *s,
return raw_write8(s->port, s->addr, BMP280_CTRL_MEAS_REG, val);
}
+static int bmp280_set_range(const struct motion_sensor_t *s,
+ int range,
+ int rnd)
+{
+ struct bmp280_drv_data_t *data = BMP280_GET_DATA(s);
+ /*
+ * ->range contains the number of bit to right shift in order for the
+ * measurment to fit into 16 bits (or less if the AP wants to).
+ */
+ data->range = 15 - __builtin_clz(range);
+ return EC_SUCCESS;
+}
+
+static int bmp280_get_range(const struct motion_sensor_t *s)
+{
+ struct bmp280_drv_data_t *data = BMP280_GET_DATA(s);
+
+ return 1 << (16 + data->range);
+}
+
/*
* bmp280_init() - Used to initialize barometer with default config
*
@@ -304,6 +324,7 @@ static int bmp280_init(const struct motion_sensor_t *s)
if (ret)
return ret;
+ bmp280_set_range(s, s->default_range, 0);
/* Read bmp280 calibration parameter */
return bmp280_get_calib_param(s);
}
@@ -322,7 +343,7 @@ static int bmp280_read(const struct motion_sensor_t *s, vector_3_t v)
if (ret)
return ret;
- v[0] = bmp280_compensate_pressure(s, pres);
+ v[0] = bmp280_compensate_pressure(s, pres) >> data->range;
v[1] = v[2] = 0;
return EC_SUCCESS;
@@ -378,6 +399,8 @@ struct bmp280_drv_data_t bmp280_drv_data;
const struct accelgyro_drv bmp280_drv = {
.init = bmp280_init,
.read = bmp280_read,
+ .set_range = bmp280_set_range,
+ .get_range = bmp280_get_range,
.set_data_rate = bmp280_set_data_rate,
.get_data_rate = bmp280_get_data_rate,
};
diff --git a/driver/baro_bmp280.h b/driver/baro_bmp280.h
index 586ed6677b..7a420655ab 100644
--- a/driver/baro_bmp280.h
+++ b/driver/baro_bmp280.h
@@ -196,8 +196,9 @@ struct bmp280_calib_param_t {
*/
struct bmp280_drv_data_t {
- struct bmp280_calib_param_t calib_param;
- int rate;
+ struct bmp280_calib_param_t calib_param;
+ uint16_t rate;
+ uint16_t range;
};
extern const struct accelgyro_drv bmp280_drv;