diff options
author | Gwendal Grignou <gwendal@chromium.org> | 2016-09-07 15:21:12 -0700 |
---|---|---|
committer | chrome-bot <chrome-bot@chromium.org> | 2016-09-09 00:04:28 -0700 |
commit | 60fc54854cffea213933cccd223ad80770cb5fb3 (patch) | |
tree | 45de68a5d3317649aff414f23048e70edabeffca /driver/baro_bmp280.c | |
parent | bcd2872e786f43e6507c0d7bd17029cc7c1f429c (diff) | |
download | chrome-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>
Diffstat (limited to 'driver/baro_bmp280.c')
-rw-r--r-- | driver/baro_bmp280.c | 25 |
1 files changed, 24 insertions, 1 deletions
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, }; |