summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2021-09-18 10:31:28 -0600
committerCommit Bot <commit-bot@chromium.org>2021-09-21 02:41:32 +0000
commit558b809aae30c8e8e72492bc11831ce0f9aa9d24 (patch)
tree856edb3daa8f9cca75c6170b12b2fd98c4e7897d
parenteca8b44a324c5a89285dbfb6e4258b7036662d72 (diff)
downloadchrome-ec-558b809aae30c8e8e72492bc11831ce0f9aa9d24.tar.gz
zephyr: test: Test failed read on who-am-i register (lis2dw12)
Test the init code path that fails reading who-am-i register. BRANCH=none BUG=b:200046770 TEST=zmake configure --test zephyr/test/drivers Signed-off-by: Yuval Peress <peress@chromium.org> Change-Id: I66c17d1d0f2e889c22ccecca8ecedd467174b764 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3170534 Commit-Queue: Yuval Peress <peress@google.com> Tested-by: Yuval Peress <peress@google.com> Reviewed-by: Keith Short <keithshort@chromium.org>
-rw-r--r--zephyr/emul/emul_lis2dw12.c10
-rw-r--r--zephyr/include/emul/emul_lis2dw12.h9
-rw-r--r--zephyr/test/drivers/src/lis2dw12.c23
3 files changed, 38 insertions, 4 deletions
diff --git a/zephyr/emul/emul_lis2dw12.c b/zephyr/emul/emul_lis2dw12.c
index 07d405bfc5..e0ca762a8e 100644
--- a/zephyr/emul/emul_lis2dw12.c
+++ b/zephyr/emul/emul_lis2dw12.c
@@ -36,10 +36,19 @@ struct lis2dw12_emul_cfg {
struct i2c_common_emul_cfg common;
};
+struct i2c_emul *lis2dw12_emul_to_i2c_emul(const struct emul *emul)
+{
+ struct lis2dw12_emul_data *data = emul->data;
+
+ return &(data->common.emul);
+}
+
void lis2dw12_emul_reset(const struct emul *emul)
{
struct lis2dw12_emul_data *data = emul->data;
+ i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul),
+ I2C_COMMON_EMUL_NO_FAIL_REG);
data->who_am_i_reg = LIS2DW12_WHO_AM_I;
}
@@ -55,7 +64,6 @@ static int lis2dw12_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val,
{
struct lis2dw12_emul_data *data = LIS2DW12_DATA_FROM_I2C_EMUL(emul);
- LOG_ERR("read_byte(reg=%d)", reg);
switch (reg) {
case LIS2DW12_WHO_AM_I_REG:
__ASSERT_NO_MSG(bytes == 0);
diff --git a/zephyr/include/emul/emul_lis2dw12.h b/zephyr/include/emul/emul_lis2dw12.h
index 7433d27bfd..f2c5a5cdfc 100644
--- a/zephyr/include/emul/emul_lis2dw12.h
+++ b/zephyr/include/emul/emul_lis2dw12.h
@@ -7,6 +7,15 @@
#define ZEPHYR_INCLUDE_EMUL_EMUL_LIS2DW12_H_
#include <emul.h>
+#include <drivers/i2c_emul.h>
+
+/**
+ * @brief The the i2c emulator pointer from the top level emul.
+ *
+ * @param emul The emulator to query
+ * @return Pointer to the i2c emulator struct
+ */
+struct i2c_emul *lis2dw12_emul_to_i2c_emul(const struct emul *emul);
/**
* @brief Reset the state of the lis2dw12 emulator.
diff --git a/zephyr/test/drivers/src/lis2dw12.c b/zephyr/test/drivers/src/lis2dw12.c
index 204c259bdc..06009f95a0 100644
--- a/zephyr/test/drivers/src/lis2dw12.c
+++ b/zephyr/test/drivers/src/lis2dw12.c
@@ -6,6 +6,7 @@
#include <ztest.h>
#include <drivers/emul.h>
#include "driver/accel_lis2dw12.h"
+#include "emul/emul_common_i2c.h"
#include "emul/emul_lis2dw12.h"
#define LIS2DW12_NODELABEL DT_NODELABEL(ms_lis2dw12_accel)
@@ -18,6 +19,18 @@ static void lis2dw12_setup(void)
lis2dw12_emul_reset(emul_get_binding(EMUL_LABEL));
}
+static void test_lis2dw12_init__fail_read_who_am_i(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_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul),
+ LIS2DW12_WHO_AM_I_REG);
+ rv = ms->drv->init(ms);
+ zassert_equal(EC_ERROR_INVAL, rv, NULL);
+}
+
static void test_lis2dw12_init__fail_who_am_i(void)
{
const struct emul *emul = emul_get_binding(EMUL_LABEL);
@@ -34,8 +47,12 @@ static void test_lis2dw12_init__fail_who_am_i(void)
void test_suite_lis2dw12(void)
{
- ztest_test_suite(lis2dw12, ztest_unit_test_setup_teardown(
- test_lis2dw12_init__fail_who_am_i,
- lis2dw12_setup, unit_test_noop));
+ ztest_test_suite(lis2dw12,
+ ztest_unit_test_setup_teardown(
+ test_lis2dw12_init__fail_read_who_am_i,
+ lis2dw12_setup, unit_test_noop),
+ ztest_unit_test_setup_teardown(
+ test_lis2dw12_init__fail_who_am_i,
+ lis2dw12_setup, unit_test_noop));
ztest_run_test_suite(lis2dw12);
}