summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGwendal Grignou <gwendal@chromium.org>2018-10-12 11:07:32 -0700
committerchrome-bot <chrome-bot@chromium.org>2018-12-14 22:40:39 -0800
commit5182b36569877f8bf13fb3a71863250ea86ff96d (patch)
tree59a240fa605f2c3ca20213d10c6b616f2f3ddadd
parent8be1f0f55cdc7fcd236886ffaccade08612e19a8 (diff)
downloadchrome-ec-5182b36569877f8bf13fb3a71863250ea86ff96d.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>
-rw-r--r--common/motion_sense.c28
-rw-r--r--include/accelgyro.h20
-rw-r--r--include/ec_commands.h52
-rw-r--r--util/ectool.c1
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 1dfa8a52b7..c05a1d495a 100644
--- a/include/ec_commands.h
+++ b/include/ec_commands.h
@@ -2366,6 +2366,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
};
@@ -2504,7 +2510,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
@@ -2597,6 +2606,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) */
@@ -2723,12 +2762,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);