summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2021-12-01 16:16:00 -0700
committerCommit Bot <commit-bot@chromium.org>2021-12-08 19:43:24 +0000
commitd6f885ac401e06f035841889bbb66e0ba53c6ee1 (patch)
treeadc127937486410abb04aaac50c208bdd39b3266
parenta5b613c19c958454cd2365dd3bdd1169a60ce945 (diff)
downloadchrome-ec-d6f885ac401e06f035841889bbb66e0ba53c6ee1.tar.gz
zephyr: lis2dw12: Test set_data_rate()
Unit test for the LIS2DW12 set_data_rate function. Covers turning the device off by setting a rate of 0, and exercises the rounding and clamping functionality. BRANCH=None BUG=b:200046770 TEST=zmake -D configure --test test-drivers; make runhosttests Signed-off-by: Tristan Honscheid <honscheid@google.com> Change-Id: I1ba28a7b192b4921593085b922269adc172ba6ad Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3311183 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
-rw-r--r--zephyr/test/drivers/src/lis2dw12.c116
1 files changed, 116 insertions, 0 deletions
diff --git a/zephyr/test/drivers/src/lis2dw12.c b/zephyr/test/drivers/src/lis2dw12.c
index e37d962a9a..87babe8723 100644
--- a/zephyr/test/drivers/src/lis2dw12.c
+++ b/zephyr/test/drivers/src/lis2dw12.c
@@ -14,6 +14,12 @@
#define EMUL_LABEL DT_LABEL(DT_NODELABEL(lis2dw12_emul))
#include <stdio.h>
+
+enum lis2dw12_round_mode {
+ ROUND_DOWN,
+ ROUND_UP,
+};
+
static void lis2dw12_setup(void)
{
lis2dw12_emul_reset(emul_get_binding(EMUL_LABEL));
@@ -153,6 +159,113 @@ static void test_lis2dw12_set_range(void)
EC_ERROR_INVAL, rv);
}
+static void test_lis2dw12_set_rate(void)
+{
+ const struct emul *emul = emul_get_binding(EMUL_LABEL);
+ struct i2c_emul *i2c_emul = lis2dw12_emul_to_i2c_emul(emul);
+ struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID];
+ struct stprivate_data *drv_data = ms->drv_data;
+ int rv;
+
+ /* Part 1: Turn off sensor with rate=0 */
+ rv = ms->drv->set_data_rate(ms, 0, 0);
+
+ zassert_equal(lis2dw12_emul_peek_odr(i2c_emul),
+ LIS2DW12_ODR_POWER_OFF_VAL,
+ "Output data rate should be %d but got %d",
+ LIS2DW12_ODR_POWER_OFF_VAL,
+ lis2dw12_emul_peek_odr(i2c_emul));
+ zassert_equal(drv_data->base.odr, LIS2DW12_ODR_POWER_OFF_VAL,
+ "Output data rate should be %d but got %d",
+ LIS2DW12_ODR_POWER_OFF_VAL, drv_data->base.odr);
+ zassert_equal(rv, EC_SUCCESS, "Returned %d but expected %d", rv,
+ EC_SUCCESS);
+
+ /* Part 2: Set some output data rates. We will request a certain rate
+ * and make sure the closest supported rate is used.
+ */
+
+ static const struct {
+ int requested_rate; /* millihertz */
+ enum lis2dw12_round_mode round;
+ int expected_norm_rate; /* millihertz */
+ uint8_t expected_reg_val;
+ } test_params[] = {
+ { 1000, ROUND_DOWN, LIS2DW12_ODR_MIN_VAL,
+ LIS2DW12_ODR_12HZ_VAL },
+ { 12501, ROUND_DOWN, 12500, LIS2DW12_ODR_12HZ_VAL },
+ { 25001, ROUND_DOWN, 25000, LIS2DW12_ODR_25HZ_VAL },
+ { 50001, ROUND_DOWN, 50000, LIS2DW12_ODR_50HZ_VAL },
+ { 100001, ROUND_DOWN, 100000, LIS2DW12_ODR_100HZ_VAL },
+ { 200001, ROUND_DOWN, 200000, LIS2DW12_ODR_200HZ_VAL },
+ { 400001, ROUND_DOWN, 400000, LIS2DW12_ODR_400HZ_VAL },
+ { 800001, ROUND_DOWN, 800000, LIS2DW12_ODR_800HZ_VAL },
+ { 1600001, ROUND_DOWN, 1600000, LIS2DW12_ODR_1_6kHZ_VAL },
+ { 3200001, ROUND_DOWN, LIS2DW12_ODR_MAX_VAL,
+ LIS2DW12_ODR_1_6kHZ_VAL },
+
+ { 1000, ROUND_UP, LIS2DW12_ODR_MIN_VAL, LIS2DW12_ODR_12HZ_VAL },
+ { 12501, ROUND_UP, 25000, LIS2DW12_ODR_25HZ_VAL },
+ { 25001, ROUND_UP, 50000, LIS2DW12_ODR_50HZ_VAL },
+ { 50001, ROUND_UP, 100000, LIS2DW12_ODR_100HZ_VAL },
+ { 100001, ROUND_UP, 200000, LIS2DW12_ODR_200HZ_VAL },
+ { 200001, ROUND_UP, 400000, LIS2DW12_ODR_400HZ_VAL },
+ { 400001, ROUND_UP, 800000, LIS2DW12_ODR_800HZ_VAL },
+ { 800001, ROUND_UP, 1600000, LIS2DW12_ODR_1_6kHZ_VAL },
+ { 1600001, ROUND_UP, LIS2DW12_ODR_MAX_VAL,
+ LIS2DW12_ODR_1_6kHZ_VAL },
+ };
+
+ for (size_t i = 0; i < ARRAY_SIZE(test_params); i++) {
+ /* For each test vector in the above array */
+ drv_data->base.odr = -1;
+ rv = ms->drv->set_data_rate(ms, test_params[i].requested_rate,
+ test_params[i].round);
+
+ /* Check the normalized rate the driver chose */
+ zassert_equal(
+ drv_data->base.odr, test_params[i].expected_norm_rate,
+ "For requested rate %d, output data rate should be %d but got %d",
+ test_params[i].requested_rate,
+ test_params[i].expected_norm_rate, drv_data->base.odr);
+
+ /* Read ODR and mode bits back from CTRL1 register */
+ uint8_t odr_bits = lis2dw12_emul_peek_odr(i2c_emul);
+
+ zassert_equal(
+ odr_bits, test_params[i].expected_reg_val,
+ "For requested rate %d, ODR bits should be 0x%x but got 0x%x - %d",
+ test_params[i].requested_rate,
+ test_params[i].expected_reg_val, odr_bits,
+ LIS2DW12_ODR_MAX_VAL);
+
+ /* Check if high performance mode was enabled if rate >
+ * 200,000mHz
+ */
+
+ uint8_t mode_bits = lis2dw12_emul_peek_mode(i2c_emul);
+ uint8_t lpmode_bits = lis2dw12_emul_peek_lpmode(i2c_emul);
+
+ if (odr_bits > LIS2DW12_ODR_200HZ_VAL) {
+ /* High performance mode, LP mode immaterial */
+ zassert_equal(mode_bits, LIS2DW12_HIGH_PERF,
+ "MODE[1:0] should be 0x%x, but got 0x%x",
+ LIS2DW12_HIGH_PERF, mode_bits);
+
+ } else {
+ /* Low power mode, LP mode 2 */
+ zassert_equal(mode_bits, LIS2DW12_LOW_POWER,
+ "MODE[1:0] should be 0x%x, but got 0x%x",
+ LIS2DW12_LOW_POWER, mode_bits);
+
+ zassert_equal(
+ lpmode_bits, LIS2DW12_LOW_POWER_MODE_2,
+ "LPMODE[1:0] should be 0x%x, but got 0x%x",
+ LIS2DW12_LOW_POWER_MODE_2, lpmode_bits);
+ }
+ }
+}
+
void test_suite_lis2dw12(void)
{
ztest_test_suite(lis2dw12,
@@ -176,6 +289,9 @@ void test_suite_lis2dw12(void)
lis2dw12_setup, lis2dw12_setup),
ztest_unit_test_setup_teardown(
test_lis2dw12_set_range,
+ lis2dw12_setup, lis2dw12_setup),
+ ztest_unit_test_setup_teardown(
+ test_lis2dw12_set_rate,
lis2dw12_setup, lis2dw12_setup)
);
ztest_run_test_suite(lis2dw12);