summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2021-09-18 21:58:27 -0600
committerCommit Bot <commit-bot@chromium.org>2021-09-21 02:41:41 +0000
commit26e3f0f8596300e1b7f64f426c0469f9bfee669f (patch)
tree39b2dba11ab9d9b73a73a66f07cd307c452e230c
parentdf6a8a118ba190cecd14d7e8b56e1945d739f390 (diff)
downloadchrome-ec-26e3f0f8596300e1b7f64f426c0469f9bfee669f.tar.gz
zephyr: test: lis2dw12 fail setting bdu on init
BRANCH=none BUG=b:200046770 TEST=zmake configure --test zephyr/projects/drivers Signed-off-by: Yuval Peress <peress@chromium.org> Change-Id: I5e109001cf19010c2a4edc68be9b043a14070480 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3170539 Reviewed-by: Jeremy Bettis <jbettis@chromium.org> Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Commit-Queue: Yuval Peress <peress@google.com> Tested-by: Yuval Peress <peress@google.com>
-rw-r--r--zephyr/emul/emul_lis2dw12.c23
-rw-r--r--zephyr/include/emul/emul_lis2dw12.h10
-rw-r--r--zephyr/test/drivers/src/lis2dw12.c30
3 files changed, 60 insertions, 3 deletions
diff --git a/zephyr/emul/emul_lis2dw12.c b/zephyr/emul/emul_lis2dw12.c
index fba6dbe758..6fa8b31cc9 100644
--- a/zephyr/emul/emul_lis2dw12.c
+++ b/zephyr/emul/emul_lis2dw12.c
@@ -31,6 +31,8 @@ struct lis2dw12_emul_data {
uint8_t who_am_i_reg;
/** Emulated ctrl2 register */
uint8_t ctrl2_reg;
+ /** Soft reset count */
+ uint32_t soft_reset_count;
};
struct lis2dw12_emul_cfg {
@@ -48,13 +50,17 @@ struct i2c_emul *lis2dw12_emul_to_i2c_emul(const struct emul *emul)
void lis2dw12_emul_reset(const struct emul *emul)
{
struct lis2dw12_emul_data *data = emul->data;
+ struct i2c_emul *i2c_emul = lis2dw12_emul_to_i2c_emul(emul);
- i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul),
+ i2c_common_emul_set_read_fail_reg(i2c_emul,
I2C_COMMON_EMUL_NO_FAIL_REG);
- i2c_common_emul_set_write_fail_reg(lis2dw12_emul_to_i2c_emul(emul),
+ i2c_common_emul_set_write_fail_reg(i2c_emul,
I2C_COMMON_EMUL_NO_FAIL_REG);
+ i2c_common_emul_set_read_func(i2c_emul, NULL, NULL);
+ i2c_common_emul_set_write_func(i2c_emul, NULL, NULL);
data->who_am_i_reg = LIS2DW12_WHO_AM_I;
data->ctrl2_reg = 0;
+ data->soft_reset_count = 0;
}
void lis2dw12_emul_set_who_am_i(const struct emul *emul, uint8_t who_am_i)
@@ -64,6 +70,13 @@ void lis2dw12_emul_set_who_am_i(const struct emul *emul, uint8_t who_am_i)
data->who_am_i_reg = who_am_i;
}
+uint32_t lis2dw12_emul_get_soft_reset_count(const struct emul *emul)
+{
+ struct lis2dw12_emul_data *data = emul->data;
+
+ return data->soft_reset_count;
+}
+
static int lis2dw12_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val,
int bytes)
{
@@ -95,7 +108,11 @@ static int lis2dw12_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val,
return -EINVAL;
case LIS2DW12_CTRL2_ADDR:
__ASSERT_NO_MSG(bytes == 1);
- data->ctrl2_reg = val;
+ if ((val & LIS2DW12_SOFT_RESET_MASK) != 0) {
+ /* Soft reset */
+ data->soft_reset_count++;
+ }
+ data->ctrl2_reg = val & ~LIS2DW12_SOFT_RESET_MASK;
break;
default:
return -EINVAL;
diff --git a/zephyr/include/emul/emul_lis2dw12.h b/zephyr/include/emul/emul_lis2dw12.h
index f2c5a5cdfc..b136e24f0a 100644
--- a/zephyr/include/emul/emul_lis2dw12.h
+++ b/zephyr/include/emul/emul_lis2dw12.h
@@ -35,4 +35,14 @@ void lis2dw12_emul_reset(const struct emul *emul);
*/
void lis2dw12_emul_set_who_am_i(const struct emul *emul, uint8_t who_am_i);
+/**
+ * @brief Check the number of times the chip was soft reset.
+ *
+ * This value is reset by a call to lis2dw12_emul_reset().
+ *
+ * @param emul The emulator to query
+ * @return The number of times that the chip was reset.
+ */
+uint32_t lis2dw12_emul_get_soft_reset_count(const struct emul *emul);
+
#endif /* ZEPHYR_INCLUDE_EMUL_EMUL_LIS2DW12_H_ */
diff --git a/zephyr/test/drivers/src/lis2dw12.c b/zephyr/test/drivers/src/lis2dw12.c
index c5b82a4e19..287430e65b 100644
--- a/zephyr/test/drivers/src/lis2dw12.c
+++ b/zephyr/test/drivers/src/lis2dw12.c
@@ -70,6 +70,33 @@ static void test_lis2dw12_init__timeout_read_soft_reset(void)
rv, EC_ERROR_TIMEOUT);
}
+static int lis2dw12_test_mock_write_fail_set_bdu(struct i2c_emul *emul, int reg,
+ uint8_t val, int bytes,
+ void *data)
+{
+ if (reg == LIS2DW12_BDU_ADDR && bytes == 1 &&
+ (val & LIS2DW12_BDU_MASK) != 0) {
+ return -EIO;
+ }
+ return 1;
+}
+
+static void test_lis2dw12_init__fail_set_bdu(void)
+{
+ const struct emul *emul = emul_get_binding(EMUL_LABEL);
+ struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID];
+ int rv;
+
+ i2c_common_emul_set_write_func(lis2dw12_emul_to_i2c_emul(emul),
+ lis2dw12_test_mock_write_fail_set_bdu,
+ NULL);
+ rv = ms->drv->init(ms);
+ zassert_equal(EC_ERROR_INVAL, rv, "init returned %d but expected %d",
+ rv, EC_ERROR_INVAL);
+ zassert_true(lis2dw12_emul_get_soft_reset_count(emul) > 0,
+ "expected at least one soft reset");
+}
+
void test_suite_lis2dw12(void)
{
ztest_test_suite(lis2dw12,
@@ -84,6 +111,9 @@ void test_suite_lis2dw12(void)
lis2dw12_setup, unit_test_noop),
ztest_unit_test_setup_teardown(
test_lis2dw12_init__timeout_read_soft_reset,
+ lis2dw12_setup, unit_test_noop),
+ ztest_unit_test_setup_teardown(
+ test_lis2dw12_init__fail_set_bdu,
lis2dw12_setup, unit_test_noop));
ztest_run_test_suite(lis2dw12);
}