summaryrefslogtreecommitdiff
path: root/test
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2020-10-23 12:05:48 -0600
committerCommit Bot <commit-bot@chromium.org>2020-12-08 19:06:07 +0000
commit6f0894886e4d15c60430b380001156b5292438cb (patch)
tree7b26a03738d1640fd610506fbfbb40004297d9f4 /test
parent9760abbefee06a7ffa214dd477665b8c9476061e (diff)
downloadchrome-ec-6f0894886e4d15c60430b380001156b5292438cb.tar.gz
common:online_calibration: Spoof sensor data
This change allows the `accelspoof` ec command to also override the calibration data (bypassing the calibration module). This config flag should be used when testing layers above the EC (kernel and up)'s handling of the calibration values. BRANCH=none BUG=none TEST=Built on eve and added unit tests. TEST=make run-online_calibration_spoof -j Signed-off-by: Yuval Peress <peress@chromium.org> Change-Id: I4735b9613c152af5559661a91565b05635d6495e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2494986
Diffstat (limited to 'test')
-rw-r--r--test/build.mk4
-rw-r--r--test/gyro_cal.c102
-rw-r--r--test/gyro_cal_init_for_test.c114
-rw-r--r--test/gyro_cal_init_for_test.h39
-rw-r--r--test/online_calibration_spoof.c196
-rw-r--r--test/online_calibration_spoof.tasklist10
-rw-r--r--test/test_config.h8
7 files changed, 371 insertions, 102 deletions
diff --git a/test/build.mk b/test/build.mk
index e994695ab0..fa0e3500cf 100644
--- a/test/build.mk
+++ b/test/build.mk
@@ -60,6 +60,7 @@ test-list-host += motion_sense_fifo
test-list-host += mutex
test-list-host += newton_fit
test-list-host += online_calibration
+test-list-host += online_calibration_spoof
test-list-host += pingpong
test-list-host += power_button
test-list-host += printf
@@ -145,7 +146,7 @@ flash_write_protect-y=flash_write_protect.o
fpsensor-y=fpsensor.o
fpsensor_crypto-y=fpsensor_crypto.o
fpsensor_state-y=fpsensor_state.o
-gyro_cal-y=gyro_cal.o
+gyro_cal-y=gyro_cal.o gyro_cal_init_for_test.o
hooks-y=hooks.o
host_command-y=host_command.o
i2c_bitbang-y=i2c_bitbang.o
@@ -165,6 +166,7 @@ motion_angle_tablet-y=motion_angle_tablet.o motion_angle_data_literals_tablet.o
motion_lid-y=motion_lid.o
motion_sense_fifo-y=motion_sense_fifo.o
online_calibration-y=online_calibration.o
+online_calibration_spoof-y=online_calibration_spoof.o gyro_cal_init_for_test.o
kasa-y=kasa.o
mpu-y=mpu.o
mutex-y=mutex.o
diff --git a/test/gyro_cal.c b/test/gyro_cal.c
index 85d53789ef..10d48ca18e 100644
--- a/test/gyro_cal.c
+++ b/test/gyro_cal.c
@@ -6,6 +6,7 @@
#include "common.h"
#include "gyro_cal.h"
#include "gyro_still_det.h"
+#include "gyro_cal_init_for_test.h"
#include "motion_sense.h"
#include "test_util.h"
#include <string.h>
@@ -70,107 +71,6 @@ static float normal_random2(float mean, float stddev)
}
/**
- *
- * @param det Pointer to the stillness detector
- * @param var_threshold The variance threshold in units^2
- * @param confidence_delta The confidence delta in units^2
- */
-static void gyro_still_det_initialization_for_test(struct gyro_still_det *det,
- float var_threshold,
- float confidence_delta)
-{
- /* Clear all data structure variables to 0. */
- memset(det, 0, sizeof(struct gyro_still_det));
-
- /*
- * Set the delta about the variance threshold for calculation
- * of the stillness confidence score.
- */
- if (confidence_delta < var_threshold)
- det->confidence_delta = confidence_delta;
- else
- det->confidence_delta = var_threshold;
-
- /*
- * Set the variance threshold parameter for the stillness
- * confidence score.
- */
- det->var_threshold = var_threshold;
-
- /* Signal to start capture of next stillness data window. */
- det->start_new_window = true;
-}
-
-static void gyro_cal_initialization_for_test(struct gyro_cal *gyro_cal)
-{
- /* GyroCal initialization. */
- memset(gyro_cal, 0, sizeof(struct gyro_cal));
-
- /*
- * Initialize the stillness detectors.
- * Gyro parameter input units are [rad/sec].
- * Accel parameter input units are [m/sec^2].
- * Magnetometer parameter input units are [uT].
- */
- gyro_still_det_initialization_for_test(&gyro_cal->gyro_stillness_detect,
- /* var_threshold */ 5e-5f,
- /* confidence_delta */ 1e-5f);
- gyro_still_det_initialization_for_test(
- &gyro_cal->accel_stillness_detect,
- /* var_threshold */ 8e-3f,
- /* confidence_delta */ 1.6e-3f);
- gyro_still_det_initialization_for_test(&gyro_cal->mag_stillness_detect,
- /* var_threshold */ 1.4f,
- /* confidence_delta */ 0.25f);
-
- /* Reset stillness flag and start timestamp. */
- gyro_cal->prev_still = false;
- gyro_cal->start_still_time_us = 0;
-
- /* Set the min and max window stillness duration. */
- gyro_cal->min_still_duration_us = 5 * SECOND;
- gyro_cal->max_still_duration_us = 6 * SECOND;
-
- /* Sets the duration of the stillness processing windows. */
- gyro_cal->window_time_duration_us = 1500000;
-
- /* Set the window timeout duration. */
- gyro_cal->gyro_window_timeout_duration_us = 5 * SECOND;
-
- /* Load the last valid cal from system memory. */
- gyro_cal->bias_x = 0.0f; /* [rad/sec] */
- gyro_cal->bias_y = 0.0f; /* [rad/sec] */
- gyro_cal->bias_z = 0.0f; /* [rad/sec] */
- gyro_cal->calibration_time_us = 0;
-
- /* Set the stillness threshold required for gyro bias calibration. */
- gyro_cal->stillness_threshold = 0.95f;
-
- /*
- * Current window end-time used to assist in keeping sensor data
- * collection in sync. Setting this to zero signals that sensor data
- * will be dropped until a valid end-time is set from the first gyro
- * timestamp received.
- */
- gyro_cal->stillness_win_endtime_us = 0;
-
- /* Gyro calibrations will be applied (see, gyro_cal_remove_bias()). */
- gyro_cal->gyro_calibration_enable = true;
-
- /*
- * Sets the stability limit for the stillness window mean acceptable
- * delta.
- */
- gyro_cal->stillness_mean_delta_limit = 50.0f * MDEG_TO_RAD;
-
- /* Sets the min/max temperature delta limit for the stillness period. */
- gyro_cal->temperature_delta_limit_kelvin = 1.5f;
-
- /* Ensures that the data tracking functionality is reset. */
- init_gyro_cal(gyro_cal);
-}
-
-/**
* Tests that a calibration is updated after a period where the IMU device is
* stationary. Accelerometer and gyroscope measurements are simulated with data
* sheet specs for the BMI160 at their respective noise floors. A magnetometer
diff --git a/test/gyro_cal_init_for_test.c b/test/gyro_cal_init_for_test.c
new file mode 100644
index 0000000000..3963e5a207
--- /dev/null
+++ b/test/gyro_cal_init_for_test.c
@@ -0,0 +1,114 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "common.h"
+#include "timer.h"
+#include "gyro_cal_init_for_test.h"
+#include <string.h>
+
+#define NANO_PI (3.14159265359f)
+/** Unit conversion: milli-degrees to radians. */
+#define MDEG_TO_RAD (NANO_PI / 180.0e3f)
+
+/**
+ *
+ * @param det Pointer to the stillness detector
+ * @param var_threshold The variance threshold in units^2
+ * @param confidence_delta The confidence delta in units^2
+ */
+static void gyro_still_det_initialization_for_test(struct gyro_still_det *det,
+ float var_threshold,
+ float confidence_delta)
+{
+ /* Clear all data structure variables to 0. */
+ memset(det, 0, sizeof(struct gyro_still_det));
+
+ /*
+ * Set the delta about the variance threshold for calculation
+ * of the stillness confidence score.
+ */
+ if (confidence_delta < var_threshold)
+ det->confidence_delta = confidence_delta;
+ else
+ det->confidence_delta = var_threshold;
+
+ /*
+ * Set the variance threshold parameter for the stillness
+ * confidence score.
+ */
+ det->var_threshold = var_threshold;
+
+ /* Signal to start capture of next stillness data window. */
+ det->start_new_window = true;
+}
+
+void gyro_cal_initialization_for_test(struct gyro_cal *gyro_cal)
+{
+ /* GyroCal initialization. */
+ memset(gyro_cal, 0, sizeof(struct gyro_cal));
+
+ /*
+ * Initialize the stillness detectors.
+ * Gyro parameter input units are [rad/sec].
+ * Accel parameter input units are [m/sec^2].
+ * Magnetometer parameter input units are [uT].
+ */
+ gyro_still_det_initialization_for_test(&gyro_cal->gyro_stillness_detect,
+ /* var_threshold */ 5e-5f,
+ /* confidence_delta */ 1e-5f);
+ gyro_still_det_initialization_for_test(
+ &gyro_cal->accel_stillness_detect,
+ /* var_threshold */ 8e-3f,
+ /* confidence_delta */ 1.6e-3f);
+ gyro_still_det_initialization_for_test(&gyro_cal->mag_stillness_detect,
+ /* var_threshold */ 1.4f,
+ /* confidence_delta */ 0.25f);
+
+ /* Reset stillness flag and start timestamp. */
+ gyro_cal->prev_still = false;
+ gyro_cal->start_still_time_us = 0;
+
+ /* Set the min and max window stillness duration. */
+ gyro_cal->min_still_duration_us = 5 * SECOND;
+ gyro_cal->max_still_duration_us = 6 * SECOND;
+
+ /* Sets the duration of the stillness processing windows. */
+ gyro_cal->window_time_duration_us = 1500000;
+
+ /* Set the window timeout duration. */
+ gyro_cal->gyro_window_timeout_duration_us = 5 * SECOND;
+
+ /* Load the last valid cal from system memory. */
+ gyro_cal->bias_x = 0.0f; /* [rad/sec] */
+ gyro_cal->bias_y = 0.0f; /* [rad/sec] */
+ gyro_cal->bias_z = 0.0f; /* [rad/sec] */
+ gyro_cal->calibration_time_us = 0;
+
+ /* Set the stillness threshold required for gyro bias calibration. */
+ gyro_cal->stillness_threshold = 0.95f;
+
+ /*
+ * Current window end-time used to assist in keeping sensor data
+ * collection in sync. Setting this to zero signals that sensor data
+ * will be dropped until a valid end-time is set from the first gyro
+ * timestamp received.
+ */
+ gyro_cal->stillness_win_endtime_us = 0;
+
+ /* Gyro calibrations will be applied (see, gyro_cal_remove_bias()). */
+ gyro_cal->gyro_calibration_enable = true;
+
+ /*
+ * Sets the stability limit for the stillness window mean acceptable
+ * delta.
+ */
+ gyro_cal->stillness_mean_delta_limit = 50.0f * MDEG_TO_RAD;
+
+ /* Sets the min/max temperature delta limit for the stillness period. */
+ gyro_cal->temperature_delta_limit_kelvin = 1.5f;
+
+ /* Ensures that the data tracking functionality is reset. */
+ init_gyro_cal(gyro_cal);
+}
diff --git a/test/gyro_cal_init_for_test.h b/test/gyro_cal_init_for_test.h
new file mode 100644
index 0000000000..e32040bab9
--- /dev/null
+++ b/test/gyro_cal_init_for_test.h
@@ -0,0 +1,39 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#ifndef __CROS_EC_GYRO_CAL_INIT_FOR_TEST
+#define __CROS_EC_GYRO_CAL_INIT_FOR_TEST
+
+#include "gyro_cal.h"
+#include "gyro_still_det.h"
+
+/**
+ * Initialization function used for testing the gyroscope calibration.
+ * This function will initialize to the following values:
+ * - Gyrscope stillness detector
+ * - variance threshold: 5e-5
+ * - confidence delta: 1e-5
+ * - Accelerometer stillness detector
+ * - variance threshold: 8e-3
+ * - confidence delta: 1.6e-3
+ * - Magnetometer stillness detector
+ * - variance threshold: 1.4
+ * - confidence delta: 2.5e-1
+ * - Minimum stillness duration: 5 seconds
+ * - Maximum stillness duration: 6 seconds
+ * - Window duration: 1.5 seconds
+ * - Window timeout duration: 5 seconds
+ * - Stillness threshold: 0.95
+ * - Stillness mean delta limit: 50 millidegrees
+ * - Temperature delta limit: 1.5K
+ *
+ * Once all the values are set, this function will call init_gyro_cal()
+ * to finish initializing/resetting the struct data.
+ *
+ * @param gyro_cal Pointer to the calibration data structure to initialize.
+ */
+void gyro_cal_initialization_for_test(struct gyro_cal *gyro_cal);
+
+#endif /* __CROS_EC_GYRO_CAL_INIT_FOR_TEST */
diff --git a/test/online_calibration_spoof.c b/test/online_calibration_spoof.c
new file mode 100644
index 0000000000..66ef5d01de
--- /dev/null
+++ b/test/online_calibration_spoof.c
@@ -0,0 +1,196 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+#include "accel_cal.h"
+#include "accelgyro.h"
+#include "hwtimer.h"
+#include "mag_cal.h"
+#include "online_calibration.h"
+#include "test_util.h"
+#include "gyro_cal_init_for_test.h"
+#include "gyro_cal.h"
+#include "timer.h"
+#include <stdio.h>
+
+int mkbp_send_event(uint8_t event_type)
+{
+ return 1;
+}
+
+/*
+ * Mocked driver (can be re-used for all sensors).
+ */
+
+static int mock_read_temp(const struct motion_sensor_t *s, int *temp)
+{
+ if (temp)
+ *temp = 200;
+ return EC_SUCCESS;
+}
+
+static struct accelgyro_drv mock_sensor_driver = {
+ .read_temp = mock_read_temp,
+};
+
+/*
+ * Accelerometer, magnetometer, and gyroscope data structs.
+ */
+
+static struct accel_cal_algo accel_cal_algos[] = { {
+ .newton_fit = NEWTON_FIT(4, 15, FLOAT_TO_FP(0.01f), FLOAT_TO_FP(0.25f),
+ FLOAT_TO_FP(1.0e-8f), 100),
+} };
+
+static struct accel_cal accel_cal_data = {
+ .still_det =
+ STILL_DET(FLOAT_TO_FP(0.00025f), 800 * MSEC, 1200 * MSEC, 5),
+ .algos = accel_cal_algos,
+ .num_temp_windows = ARRAY_SIZE(accel_cal_algos),
+};
+
+static struct mag_cal_t mag_cal_data;
+
+static struct gyro_cal gyro_cal_data;
+
+/*
+ * Motion sensor array and count.
+ */
+
+struct motion_sensor_t motion_sensors[] = {
+ {
+ .type = MOTIONSENSE_TYPE_ACCEL,
+ .default_range = 4,
+ .drv = &mock_sensor_driver,
+ .online_calib_data[0] = {
+ .type_specific_data = &accel_cal_data,
+ },
+ },
+ {
+ .type = MOTIONSENSE_TYPE_MAG,
+ .default_range = 4,
+ .drv = &mock_sensor_driver,
+ .online_calib_data[0] = {
+ .type_specific_data = &mag_cal_data,
+ },
+ },
+ {
+ .type = MOTIONSENSE_TYPE_GYRO,
+ .default_range = 4,
+ .drv = &mock_sensor_driver,
+ .online_calib_data[0] = {
+ .type_specific_data = &gyro_cal_data,
+ },
+ },
+};
+
+const unsigned int motion_sensor_count = ARRAY_SIZE(motion_sensors);
+
+static void spoof_sensor_data(struct motion_sensor_t *s, int x, int y, int z)
+{
+ struct ec_response_motion_sensor_data data;
+ uint32_t timestamp = 0;
+
+ /* Set the data and flags. */
+ data.data[X] = x;
+ data.data[Y] = y;
+ data.data[Z] = z;
+ s->flags |= MOTIONSENSE_FLAG_IN_SPOOF_MODE;
+
+ /* Pass the data to online_calibdation. */
+ online_calibration_process_data(&data, s, timestamp);
+}
+
+/*
+ * Begin testing.
+ */
+
+static int test_accel_calibration_on_spoof(void)
+{
+ struct ec_response_online_calibration_data out;
+
+ /* Send spoof data (1, 2, 3). */
+ spoof_sensor_data(&motion_sensors[0], 1, 2, 3);
+
+ /* Check that we have new values. */
+ TEST_ASSERT(online_calibration_has_new_values());
+
+ /* Get the new values for sensor 0. */
+ TEST_ASSERT(online_calibration_read(&motion_sensors[0], &out));
+
+ /* Validate the new values. */
+ TEST_EQ(out.data[X], 1, "%d");
+ TEST_EQ(out.data[Y], 2, "%d");
+ TEST_EQ(out.data[Z], 3, "%d");
+
+ /* Validate that no other sensors have data. */
+ TEST_ASSERT(!online_calibration_has_new_values());
+
+ return EC_SUCCESS;
+}
+
+static int test_mag_calibration_on_spoof(void)
+{
+ struct ec_response_online_calibration_data out;
+
+ /* Send spoof data (4, 5, 6). */
+ spoof_sensor_data(&motion_sensors[1], 4, 5, 6);
+
+ /* Check that we have new values. */
+ TEST_ASSERT(online_calibration_has_new_values());
+
+ /* Get the new values for sensor 0. */
+ TEST_ASSERT(online_calibration_read(&motion_sensors[1], &out));
+
+ /* Validate the new values. */
+ TEST_EQ(out.data[X], 4, "%d");
+ TEST_EQ(out.data[Y], 5, "%d");
+ TEST_EQ(out.data[Z], 6, "%d");
+
+ /* Validate that no other sensors have data. */
+ TEST_ASSERT(!online_calibration_has_new_values());
+
+ return EC_SUCCESS;
+}
+
+static int test_gyro_calibration_on_spoof(void)
+{
+ struct ec_response_online_calibration_data out;
+
+ /* Send spoof data (7, 8, 9). */
+ spoof_sensor_data(&motion_sensors[2], 7, 8, 9);
+
+ /* Check that we have new values. */
+ TEST_ASSERT(online_calibration_has_new_values());
+
+ /* Get the new values for sensor 0. */
+ TEST_ASSERT(online_calibration_read(&motion_sensors[2], &out));
+
+ /* Validate the new values. */
+ TEST_EQ(out.data[X], 7, "%d");
+ TEST_EQ(out.data[Y], 8, "%d");
+ TEST_EQ(out.data[Z], 9, "%d");
+
+ /* Validate that no other sensors have data. */
+ TEST_ASSERT(!online_calibration_has_new_values());
+
+ return EC_SUCCESS;
+}
+
+void before_test(void)
+{
+ online_calibration_init();
+ gyro_cal_initialization_for_test(&gyro_cal_data);
+}
+
+void run_test(int argc, char **argv)
+{
+ test_reset();
+
+ RUN_TEST(test_accel_calibration_on_spoof);
+ RUN_TEST(test_mag_calibration_on_spoof);
+ RUN_TEST(test_gyro_calibration_on_spoof);
+
+ test_print_result();
+}
diff --git a/test/online_calibration_spoof.tasklist b/test/online_calibration_spoof.tasklist
new file mode 100644
index 0000000000..7d28eb5b64
--- /dev/null
+++ b/test/online_calibration_spoof.tasklist
@@ -0,0 +1,10 @@
+/* Copyright 2020 The Chromium OS Authors. All rights reserved.
+ * Use of this source code is governed by a BSD-style license that can be
+ * found in the LICENSE file.
+ */
+
+/**
+ * See CONFIG_TASK_LIST in config.h for details.
+ */
+#define CONFIG_TEST_TASK_LIST \
+ TASK_TEST(MOTIONSENSE, motion_sense_task, NULL, TASK_STACK_SIZE)
diff --git a/test/test_config.h b/test/test_config.h
index 51c2e5603d..4e9024f6c1 100644
--- a/test/test_config.h
+++ b/test/test_config.h
@@ -142,6 +142,14 @@
#define CONFIG_MKBP_USE_GPIO
#endif
+#ifdef TEST_ONLINE_CALIBRATION_SPOOF
+#define CONFIG_FPU
+#define CONFIG_ONLINE_CALIB
+#define CONFIG_MKBP_EVENT
+#define CONFIG_MKBP_USE_GPIO
+#define CONFIG_ONLINE_CALIB_SPOOF_MODE
+#endif /* TEST_ONLINE_CALIBRATION_SPOOF */
+
#ifdef TEST_GYRO_CAL
#define CONFIG_FPU
#define CONFIG_ONLINE_CALIB