summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2021-11-22 12:43:54 -0700
committerCommit Bot <commit-bot@chromium.org>2021-12-08 18:51:47 +0000
commitff99f9407aca97ca1c4e6055552f25817205598d (patch)
tree6b7188c8c76ba0b90193c25218acc5cc870b9ee5
parent60b21a3cd67324e967e1be4d18e8e7beaebf49a8 (diff)
downloadchrome-ec-ff99f9407aca97ca1c4e6055552f25817205598d.tar.gz
zephyr: lis2dw12: Add tests for set_power_mode()
* Export set_power_mode() in case of unit tests. We need direct access to this function in order to exercise all of this cases, because it is only ever called within the driver using fixed args that don't hit all cases. * Rename set_power_mode() to lis2dw12_set_power_mode to prevent naming collisions * Add a test BRANCH=None BUG=b:200046770 TEST=zmake -D configure --test test-drivers; make runhosttests Signed-off-by: Tristan Honscheid <honscheid@google.com> Change-Id: I96b102c5a1a4c2524b775a32587afe16675a3cd1 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3296683 Reviewed-by: Aaron Massey <aaronmassey@google.com> Tested-by: Tristan Honscheid <honscheid@google.com> Commit-Queue: Tristan Honscheid <honscheid@google.com>
-rw-r--r--driver/accel_lis2dw12.c17
-rw-r--r--driver/accel_lis2dw12.h6
-rw-r--r--zephyr/test/drivers/src/lis2dw12.c39
3 files changed, 49 insertions, 13 deletions
diff --git a/driver/accel_lis2dw12.c b/driver/accel_lis2dw12.c
index 1872e572f2..d1abd7c03b 100644
--- a/driver/accel_lis2dw12.c
+++ b/driver/accel_lis2dw12.c
@@ -286,7 +286,7 @@ static int lis2dw12_irq_handler(struct motion_sensor_t *s,
#endif
/**
- * set_power_mode - set sensor power mode
+ * lis2dw12_set_power_mode - set sensor power mode
* @s: Motion sensor pointer
* @mode: LIS2DW12_LOW_POWER, LIS2DW12_HIGH_PERF
* @lpmode: LIS2DW12_LOW_POWER_MODE_2,
@@ -296,9 +296,10 @@ static int lis2dw12_irq_handler(struct motion_sensor_t *s,
* TODO: LIS2DW12_LOW_POWER_MODE_1 not implemented because output differ
* in resolution
*/
-static int set_power_mode(const struct motion_sensor_t *s,
- enum lis2sw12_mode mode,
- enum lis2sw12_lpmode lpmode)
+STATIC_IF_NOT(CONFIG_ZTEST)
+int lis2dw12_set_power_mode(const struct motion_sensor_t *s,
+ enum lis2sw12_mode mode,
+ enum lis2sw12_lpmode lpmode)
{
int ret = EC_SUCCESS;
@@ -436,9 +437,9 @@ static int set_data_rate(const struct motion_sensor_t *s, int rate, int rnd)
*/
if (!IS_ENABLED(CONFIG_ACCEL_LIS2DWL)) {
if (reg_val > LIS2DW12_ODR_200HZ_VAL)
- ret = set_power_mode(s, LIS2DW12_HIGH_PERF, 0);
+ ret = lis2dw12_set_power_mode(s, LIS2DW12_HIGH_PERF, 0);
else
- ret = set_power_mode(s, LIS2DW12_LOW_POWER,
+ ret = lis2dw12_set_power_mode(s, LIS2DW12_LOW_POWER,
LIS2DW12_LOW_POWER_MODE_2);
}
@@ -569,10 +570,10 @@ static int init(struct motion_sensor_t *s)
* lis2dwl supports 14 bit resolution only
* at high performance mode
*/
- ret = set_power_mode(s, LIS2DW12_HIGH_PERF, 0);
+ ret = lis2dw12_set_power_mode(s, LIS2DW12_HIGH_PERF, 0);
else
/* Set default Mode and Low Power Mode. */
- ret = set_power_mode(s, LIS2DW12_LOW_POWER,
+ ret = lis2dw12_set_power_mode(s, LIS2DW12_LOW_POWER,
LIS2DW12_LOW_POWER_MODE_2);
if (ret != EC_SUCCESS)
goto err_unlock;
diff --git a/driver/accel_lis2dw12.h b/driver/accel_lis2dw12.h
index 8e1c97464c..71116cb13b 100644
--- a/driver/accel_lis2dw12.h
+++ b/driver/accel_lis2dw12.h
@@ -206,4 +206,10 @@ enum lis2dw12_fs {
*/
#define LIS2DW12_RESOLUTION 14
+#ifdef CONFIG_ZTEST
+int lis2dw12_set_power_mode(const struct motion_sensor_t *s,
+ enum lis2sw12_mode mode,
+ enum lis2sw12_lpmode lpmode);
+#endif
+
#endif /* __CROS_EC_ACCEL_LIS2DW12_H */
diff --git a/zephyr/test/drivers/src/lis2dw12.c b/zephyr/test/drivers/src/lis2dw12.c
index 287430e65b..c6117e2191 100644
--- a/zephyr/test/drivers/src/lis2dw12.c
+++ b/zephyr/test/drivers/src/lis2dw12.c
@@ -97,23 +97,52 @@ static void test_lis2dw12_init__fail_set_bdu(void)
"expected at least one soft reset");
}
+static void test_lis2dw12_set_power_mode(void)
+{
+ const struct emul *emul = emul_get_binding(EMUL_LABEL);
+ struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID];
+ int rv;
+
+ /* Part 1: happy path */
+ rv = lis2dw12_set_power_mode(ms, LIS2DW12_LOW_POWER,
+ LIS2DW12_LOW_POWER_MODE_2);
+ zassert_equal(rv, EC_SUCCESS, "Expected %d but got %d", EC_SUCCESS, rv);
+
+ /* Part 2: unimplemented modes */
+ rv = lis2dw12_set_power_mode(ms, LIS2DW12_LOW_POWER,
+ LIS2DW12_LOW_POWER_MODE_1);
+ zassert_equal(rv, EC_ERROR_UNIMPLEMENTED, "Expected %d but got %d",
+ EC_ERROR_UNIMPLEMENTED, rv);
+
+ /* Part 3: attempt to set mode but cannot modify reg. */
+ i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul),
+ LIS2DW12_ACC_MODE_ADDR);
+ rv = lis2dw12_set_power_mode(ms, LIS2DW12_LOW_POWER,
+ LIS2DW12_LOW_POWER_MODE_2);
+ zassert_equal(rv, EC_ERROR_INVAL, "Expected %d but got %d",
+ EC_ERROR_INVAL, rv);
+}
+
void test_suite_lis2dw12(void)
{
ztest_test_suite(lis2dw12,
ztest_unit_test_setup_teardown(
test_lis2dw12_init__fail_read_who_am_i,
- lis2dw12_setup, unit_test_noop),
+ lis2dw12_setup, lis2dw12_setup),
ztest_unit_test_setup_teardown(
test_lis2dw12_init__fail_who_am_i,
- lis2dw12_setup, unit_test_noop),
+ lis2dw12_setup, lis2dw12_setup),
ztest_unit_test_setup_teardown(
test_lis2dw12_init__fail_write_soft_reset,
- lis2dw12_setup, unit_test_noop),
+ lis2dw12_setup, lis2dw12_setup),
ztest_unit_test_setup_teardown(
test_lis2dw12_init__timeout_read_soft_reset,
- lis2dw12_setup, unit_test_noop),
+ lis2dw12_setup, lis2dw12_setup),
ztest_unit_test_setup_teardown(
test_lis2dw12_init__fail_set_bdu,
- lis2dw12_setup, unit_test_noop));
+ lis2dw12_setup, lis2dw12_setup),
+ ztest_unit_test_setup_teardown(
+ test_lis2dw12_set_power_mode,
+ lis2dw12_setup, lis2dw12_setup));
ztest_run_test_suite(lis2dw12);
}