diff options
author | Gwendal Grignou <gwendal@chromium.org> | 2018-10-12 11:07:32 -0700 |
---|---|---|
committer | ChromeOS Commit Bot <chromeos-commit-bot@chromium.org> | 2018-12-18 21:27:42 +0000 |
commit | 8904b5a6cf3b33ba81ab2e11d9050687e3897c4a (patch) | |
tree | 08b7b6b76c3b24d319e2871047d7891375de392b | |
parent | 805c77f69f5d17d3140f230f38103467068a967e (diff) | |
download | chrome-ec-8904b5a6cf3b33ba81ab2e11d9050687e3897c4a.tar.gz |
common: sensor: Add API to set sensor scale
Add option to scale the sensor data. Each axis can be scaled
differently.
If the sensor does not support setting scale, return an error.
BUG=b:112957338
BRANCH=nocturne
TEST=Compile, load, check setting calibscale returns an error.
Change-Id: Ib6aac39f22ddcbff5f3e45830f8029811a4ed1ad
Signed-off-by: Gwendal Grignou <gwendal@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/1279185
Commit-Ready: ChromeOS CL Exonerator Bot <chromiumos-cl-exonerator@appspot.gserviceaccount.com>
Reviewed-by: Aseda Aboagye <aaboagye@chromium.org>
Reviewed-on: https://chromium-review.googlesource.com/c/1383312
Reviewed-by: Jett Rink <jettrink@chromium.org>
Commit-Queue: Jett Rink <jettrink@chromium.org>
Tested-by: Jett Rink <jettrink@chromium.org>
Trybot-Ready: Jett Rink <jettrink@chromium.org>
-rw-r--r-- | common/motion_sense.c | 28 | ||||
-rw-r--r-- | include/accelgyro.h | 20 | ||||
-rw-r--r-- | include/ec_commands.h | 52 | ||||
-rw-r--r-- | util/ectool.c | 1 |
4 files changed, 96 insertions, 5 deletions
diff --git a/common/motion_sense.c b/common/motion_sense.c index 9f4892445a..f5bfc8426c 100644 --- a/common/motion_sense.c +++ b/common/motion_sense.c @@ -1282,6 +1282,34 @@ static int host_cmd_motion_sense(struct host_cmd_handler_args *args) args->response_size = sizeof(out->sensor_offset); break; + case MOTIONSENSE_CMD_SENSOR_SCALE: + /* Verify sensor number is valid. */ + sensor = host_sensor_id_to_real_sensor( + in->sensor_scale.sensor_num); + if (sensor == NULL) + return EC_RES_INVALID_PARAM; + /* Set new range if the data arg has a value. */ + if (in->sensor_scale.flags & MOTION_SENSE_SET_OFFSET) { + if (!sensor->drv->set_scale) + return EC_RES_INVALID_COMMAND; + + ret = sensor->drv->set_scale(sensor, + in->sensor_scale.scale, + in->sensor_scale.temp); + if (ret != EC_SUCCESS) + return ret; + } + + if (!sensor->drv->get_scale) + return EC_RES_INVALID_COMMAND; + + ret = sensor->drv->get_scale(sensor, out->sensor_scale.scale, + &out->sensor_scale.temp); + if (ret != EC_SUCCESS) + return ret; + args->response_size = sizeof(out->sensor_scale); + break; + case MOTIONSENSE_CMD_PERFORM_CALIB: /* Verify sensor number is valid. */ sensor = host_sensor_id_to_real_sensor( diff --git a/include/accelgyro.h b/include/accelgyro.h index b2ea962ba2..cc731cf794 100644 --- a/include/accelgyro.h +++ b/include/accelgyro.h @@ -10,9 +10,6 @@ /* Header file for accelerometer / gyro drivers. */ -/* Number of counts from accelerometer that represents 1G acceleration. */ -#define ACCEL_G 1024 - struct accelgyro_drv { /** * Initialize accelerometers. @@ -88,6 +85,19 @@ struct accelgyro_drv { int (*get_offset)(const struct motion_sensor_t *s, int16_t *offset, int16_t *temp); + /** + * Setter and getter methods for the sensor scale. + * @s Pointer to sensor data. + * @scale: scale to apply to raw data. + * @temp: temperature when calibration was done. + * @return EC_SUCCESS if successful, non-zero if error. + */ + int (*set_scale)(const struct motion_sensor_t *s, + const uint16_t *scale, + int16_t temp); + int (*get_scale)(const struct motion_sensor_t *s, + uint16_t *scale, + int16_t *temp); int (*perform_calib)(const struct motion_sensor_t *s); #ifdef CONFIG_ACCEL_INTERRUPTS /** @@ -131,6 +141,10 @@ struct accelgyro_drv { struct accelgyro_saved_data_t { int odr; int range; + uint16_t scale[3]; }; +#define SENSOR_APPLY_SCALE(_input, _scale) \ + (((_input) * (_scale)) / MOTION_SENSE_DEFAULT_SCALE) + #endif /* __CROS_EC_ACCELGYRO_H */ diff --git a/include/ec_commands.h b/include/ec_commands.h index 5340c3d470..3f50a2628a 100644 --- a/include/ec_commands.h +++ b/include/ec_commands.h @@ -2368,6 +2368,12 @@ enum motionsense_command { /* Set lid angle for tablet mode detection. */ MOTIONSENSE_CMD_TABLET_MODE_LID_ANGLE = 17, + /* + * Sensor Scale command is a setter/getter command for the calibration + * scale. + */ + MOTIONSENSE_CMD_SENSOR_SCALE = 18, + /* Number of motionsense sub-commands. */ MOTIONSENSE_NUM_CMDS }; @@ -2506,7 +2512,10 @@ struct ec_motion_sense_activity { /* MOTIONSENSE_CMD_SENSOR_OFFSET subcommand flag */ /* Set Calibration information */ -#define MOTION_SENSE_SET_OFFSET 1 +#define MOTION_SENSE_SET_OFFSET (1 << 0) + +/* Default Scale value, factor 1. */ +#define MOTION_SENSE_DEFAULT_SCALE (1 << 15) #define LID_ANGLE_UNRELIABLE 500 @@ -2599,6 +2608,36 @@ struct ec_params_motion_sense { int16_t offset[3]; } sensor_offset; + /* Used for MOTIONSENSE_CMD_SENSOR_SCALE */ + struct __ec_todo_packed { + uint8_t sensor_num; + + /* + * bit 0: If set (MOTION_SENSE_SET_OFFSET), set + * the calibration information in the EC. + * If unset, just retrieve calibration information. + */ + uint16_t flags; + + /* + * Temperature at calibration, in units of 0.01 C + * 0x8000: invalid / unknown. + * 0x0: 0C + * 0x7fff: +327.67C + */ + int16_t temp; + + /* + * Scale for calibration: + * By default scale is 1, it is encoded on 16bits: + * 1 = 1 << 15 + * ~2 = 0xFFFF + * ~0 = 0. + */ + uint16_t scale[3]; + } sensor_scale; + + /* Used for MOTIONSENSE_CMD_FIFO_INFO */ /* (no params) */ @@ -2725,12 +2764,21 @@ struct ec_response_motion_sense { } ec_rate, sensor_odr, sensor_range, kb_wake_angle, fifo_int_enable, spoof; - /* Used for MOTIONSENSE_CMD_SENSOR_OFFSET */ + /* + * Used for MOTIONSENSE_CMD_SENSOR_OFFSET, + * PERFORM_CALIB. + */ struct __ec_todo_unpacked { int16_t temp; int16_t offset[3]; } sensor_offset, perform_calib; + /* Used for MOTIONSENSE_CMD_SENSOR_SCALE */ + struct __ec_todo_unpacked { + int16_t temp; + uint16_t scale[3]; + } sensor_scale; + struct ec_response_motion_sense_fifo_info fifo_info, fifo_flush; struct ec_response_motion_sense_fifo_data fifo_read; diff --git a/util/ectool.c b/util/ectool.c index f918c0b7eb..51cfd37d7b 100644 --- a/util/ectool.c +++ b/util/ectool.c @@ -4101,6 +4101,7 @@ static const struct { ST_BOTH_SIZES(fifo_int_enable), ST_BOTH_SIZES(spoof), ST_BOTH_SIZES(tablet_mode_threshold), + ST_BOTH_SIZES(sensor_scale), }; BUILD_ASSERT(ARRAY_SIZE(ms_command_sizes) == MOTIONSENSE_NUM_CMDS); |