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>
This commit is contained in:
Gwendal Grignou
2016-09-07 15:21:12 -07:00
committed by chrome-bot
parent bcd2872e78
commit 60fc54854c
3 changed files with 28 additions and 4 deletions

View File

@@ -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] = {

View File

@@ -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,
};

View File

@@ -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;