summaryrefslogtreecommitdiff
path: root/zephyr
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2021-09-17 00:02:34 -0600
committerCommit Bot <commit-bot@chromium.org>2021-09-17 21:00:15 +0000
commit78d947267857710daa7338fdcffcddb2f21c6d54 (patch)
tree1b4d550cfe4c6880fe3b588fff444de7a765fb87 /zephyr
parent256b26922dc7446260a4ec22538fc5574e8503f3 (diff)
downloadchrome-ec-78d947267857710daa7338fdcffcddb2f21c6d54.tar.gz
zephyr: test: Add tests for lis2dw12 driver
Add a single test for the lis2dw12 driver that checks the 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: I8dba62f941d74cca025645b81512b4e0c9e04908 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3168035 Tested-by: Yuval Peress <peress@google.com> Reviewed-by: Jeremy Bettis <jbettis@chromium.org> Commit-Queue: Yuval Peress <peress@google.com>
Diffstat (limited to 'zephyr')
-rw-r--r--zephyr/emul/emul_lis2dw12.c52
-rw-r--r--zephyr/include/emul/emul_lis2dw12.h20
-rw-r--r--zephyr/test/drivers/src/lis2dw12.c41
-rw-r--r--zephyr/test/drivers/src/main.c2
4 files changed, 115 insertions, 0 deletions
diff --git a/zephyr/emul/emul_lis2dw12.c b/zephyr/emul/emul_lis2dw12.c
index e775bdbb20..07d405bfc5 100644
--- a/zephyr/emul/emul_lis2dw12.c
+++ b/zephyr/emul/emul_lis2dw12.c
@@ -20,9 +20,15 @@
#include <logging/log.h>
LOG_MODULE_REGISTER(lis2dw12_emul, CONFIG_LIS2DW12_EMUL_LOG_LEVEL);
+#define LIS2DW12_DATA_FROM_I2C_EMUL(_emul) \
+ CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \
+ struct lis2dw12_emul_data, common)
+
struct lis2dw12_emul_data {
/** Common I2C data */
struct i2c_common_emul_data common;
+ /** Emulated who-am-i register */
+ uint8_t who_am_i_reg;
};
struct lis2dw12_emul_cfg {
@@ -30,6 +36,50 @@ struct lis2dw12_emul_cfg {
struct i2c_common_emul_cfg common;
};
+void lis2dw12_emul_reset(const struct emul *emul)
+{
+ struct lis2dw12_emul_data *data = emul->data;
+
+ data->who_am_i_reg = LIS2DW12_WHO_AM_I;
+}
+
+void lis2dw12_emul_set_who_am_i(const struct emul *emul, uint8_t who_am_i)
+{
+ struct lis2dw12_emul_data *data = emul->data;
+
+ data->who_am_i_reg = who_am_i;
+}
+
+static int lis2dw12_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val,
+ int bytes)
+{
+ 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);
+ *val = data->who_am_i_reg;
+ break;
+ default:
+ return -EINVAL;
+ }
+ return 0;
+}
+
+static int lis2dw12_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val,
+ int bytes)
+{
+ switch (reg) {
+ case LIS2DW12_WHO_AM_I_REG:
+ LOG_ERR("Can't write to who-am-i register");
+ return -EINVAL;
+ default:
+ return -EINVAL;
+ }
+ return 0;
+}
+
static struct i2c_emul_api lis2dw12_emul_api_i2c = {
.transfer = i2c_common_emul_transfer,
};
@@ -54,6 +104,8 @@ static int emul_lis2dw12_init(const struct emul *emul,
#define INIT_LIS2DW12(n) \
static struct lis2dw12_emul_data lis2dw12_emul_data_##n = { \
.common = { \
+ .write_byte = lis2dw12_emul_write_byte, \
+ .read_byte = lis2dw12_emul_read_byte, \
}, \
}; \
static const struct lis2dw12_emul_cfg lis2dw12_emul_cfg_##n = { \
diff --git a/zephyr/include/emul/emul_lis2dw12.h b/zephyr/include/emul/emul_lis2dw12.h
index ce34887da8..7433d27bfd 100644
--- a/zephyr/include/emul/emul_lis2dw12.h
+++ b/zephyr/include/emul/emul_lis2dw12.h
@@ -6,4 +6,24 @@
#ifndef ZEPHYR_INCLUDE_EMUL_EMUL_LIS2DW12_H_
#define ZEPHYR_INCLUDE_EMUL_EMUL_LIS2DW12_H_
+#include <emul.h>
+
+/**
+ * @brief Reset the state of the lis2dw12 emulator.
+ *
+ * @param emul The emulator to reset.
+ */
+void lis2dw12_emul_reset(const struct emul *emul);
+
+/**
+ * @brief Set the who-am-i register value.
+ *
+ * By default the who-am-i register holds LIS2DW12_WHO_AM_I, this function
+ * enables overriding that value in order to drive testing.
+ *
+ * @param emul The emulator to modify.
+ * @param who_am_i The new who-am-i register value.
+ */
+void lis2dw12_emul_set_who_am_i(const struct emul *emul, uint8_t who_am_i);
+
#endif /* ZEPHYR_INCLUDE_EMUL_EMUL_LIS2DW12_H_ */
diff --git a/zephyr/test/drivers/src/lis2dw12.c b/zephyr/test/drivers/src/lis2dw12.c
new file mode 100644
index 0000000000..204c259bdc
--- /dev/null
+++ b/zephyr/test/drivers/src/lis2dw12.c
@@ -0,0 +1,41 @@
+/* Copyright 2021 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 <ztest.h>
+#include <drivers/emul.h>
+#include "driver/accel_lis2dw12.h"
+#include "emul/emul_lis2dw12.h"
+
+#define LIS2DW12_NODELABEL DT_NODELABEL(ms_lis2dw12_accel)
+#define LIS2DW12_SENSOR_ID SENSOR_ID(LIS2DW12_NODELABEL)
+#define EMUL_LABEL DT_LABEL(DT_NODELABEL(lis2dw12_emul))
+
+#include <stdio.h>
+static void lis2dw12_setup(void)
+{
+ lis2dw12_emul_reset(emul_get_binding(EMUL_LABEL));
+}
+
+static void test_lis2dw12_init__fail_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;
+
+ lis2dw12_emul_set_who_am_i(emul, ~LIS2DW12_WHO_AM_I);
+
+ rv = ms->drv->init(ms);
+ zassert_equal(EC_ERROR_ACCESS_DENIED, rv,
+ "init returned %d but was expecting %d", rv,
+ EC_ERROR_ACCESS_DENIED);
+}
+
+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_run_test_suite(lis2dw12);
+}
diff --git a/zephyr/test/drivers/src/main.c b/zephyr/test/drivers/src/main.c
index 5237f74c2f..dd5ad79d0a 100644
--- a/zephyr/test/drivers/src/main.c
+++ b/zephyr/test/drivers/src/main.c
@@ -21,6 +21,7 @@ extern void test_suite_tcs3400(void);
extern void test_suite_espi(void);
extern void test_suite_bb_retimer(void);
extern void test_suite_ln9310(void);
+extern void test_suite_lis2dw12(void);
void test_main(void)
{
@@ -43,4 +44,5 @@ void test_main(void)
test_suite_espi();
test_suite_bb_retimer();
test_suite_ln9310();
+ test_suite_lis2dw12();
}