summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYuval Peress <peress@chromium.org>2021-09-20 13:31:51 -0600
committerCommit Bot <commit-bot@chromium.org>2021-09-21 02:41:48 +0000
commitf61e542f9871e9583e95b50599a9f41a9bf1024c (patch)
tree8e889ef8e474985c4321c6aa381d592d4e6f4f3b
parent5a12a3dbca6e07882d1760b115dcbf3b7ec5931c (diff)
downloadchrome-ec-f61e542f9871e9583e95b50599a9f41a9bf1024c.tar.gz
zephyr: test: Add test for st_raw_read_n
Enable mocking and add a test using the I2C mock emulator for the st_raw_read_n function. BRANCH=none BUG=b:200589041 TEST=zmake configure --test zephyr/test/drivers Signed-off-by: Yuval Peress <peress@chromium.org> Change-Id: I2554f1eab898942bc6d68860beba2e1eb02b5aa0 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3171695 Tested-by: Yuval Peress <peress@google.com> Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Reviewed-by: Jeremy Bettis <jbettis@chromium.org> Commit-Queue: Yuval Peress <peress@google.com>
-rw-r--r--zephyr/test/drivers/overlay.dts7
-rw-r--r--zephyr/test/drivers/prj.conf3
-rw-r--r--zephyr/test/drivers/src/main.c2
-rw-r--r--zephyr/test/drivers/src/stm_mems_common.c67
4 files changed, 79 insertions, 0 deletions
diff --git a/zephyr/test/drivers/overlay.dts b/zephyr/test/drivers/overlay.dts
index adf90fbc6c..90a9864df7 100644
--- a/zephyr/test/drivers/overlay.dts
+++ b/zephyr/test/drivers/overlay.dts
@@ -525,4 +525,11 @@
reg = <0x19>;
label = "LIS2DW12_EMUL";
};
+
+ i2c_mock: i2c_mock@84 {
+ compatible = "cros,i2c-mock";
+ status = "okay";
+ reg = <0x84>;
+ label = "I2C_MOCK";
+ };
};
diff --git a/zephyr/test/drivers/prj.conf b/zephyr/test/drivers/prj.conf
index 71920523c5..6f9cbaa076 100644
--- a/zephyr/test/drivers/prj.conf
+++ b/zephyr/test/drivers/prj.conf
@@ -7,6 +7,9 @@
# CONFIG_I2C_LOG_LEVEL_DBG=y
CONFIG_ZTEST=y
+CONFIG_ZTEST_ASSERT_VERBOSE=1
+CONFIG_ZTEST_MOCKING=y
+CONFIG_ZTEST_PARAMETER_COUNT=5
CONFIG_PLATFORM_EC=y
CONFIG_CROS_EC=y
CONFIG_SHIMMED_TASKS=y
diff --git a/zephyr/test/drivers/src/main.c b/zephyr/test/drivers/src/main.c
index dd5ad79d0a..8d40bc4373 100644
--- a/zephyr/test/drivers/src/main.c
+++ b/zephyr/test/drivers/src/main.c
@@ -22,6 +22,7 @@ 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);
+extern void test_suite_stm_mems_common(void);
void test_main(void)
{
@@ -45,4 +46,5 @@ void test_main(void)
test_suite_bb_retimer();
test_suite_ln9310();
test_suite_lis2dw12();
+ test_suite_stm_mems_common();
}
diff --git a/zephyr/test/drivers/src/stm_mems_common.c b/zephyr/test/drivers/src/stm_mems_common.c
new file mode 100644
index 0000000000..e69fe572fc
--- /dev/null
+++ b/zephyr/test/drivers/src/stm_mems_common.c
@@ -0,0 +1,67 @@
+/* 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 <device.h>
+#include <devicetree.h>
+#include <errno.h>
+
+#include "common.h"
+#include "driver/stm_mems_common.h"
+#include "emul/emul_common_i2c.h"
+#include "emul/i2c_mock.h"
+#include "i2c/i2c.h"
+
+#define MOCK_EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(i2c_mock)))
+
+static void setup(void)
+{
+ i2c_mock_reset(MOCK_EMUL);
+}
+
+static const struct device *get_i2c_dev(void)
+{
+ const struct device *i2c_dev =
+ device_get_binding(DT_LABEL(DT_NODELABEL(i2c0)));
+
+ zassert_not_null(i2c_dev, "Failed to find 'i2c0' node");
+ zassert_equal(i2c_dev, i2c_get_device_for_port(I2C_PORT_POWER),
+ "i2c0 node doesn't match I2C_PORT_POWER");
+ return i2c_dev;
+}
+
+static int mock_read_fn(struct i2c_emul *emul, int reg, uint8_t *val, int bytes,
+ void *data)
+{
+ ztest_check_expected_value(reg);
+ ztest_check_expected_value(bytes);
+ return ztest_get_return_value();
+}
+
+static void test_st_raw_read_n(void)
+{
+ const struct emul *emul = MOCK_EMUL;
+ struct i2c_emul *i2c_emul = i2c_mock_to_i2c_emul(emul);
+ int rv;
+
+ i2c_common_emul_set_read_func(i2c_emul, mock_read_fn, NULL);
+ ztest_expect_value(mock_read_fn, reg, 0x80);
+ ztest_expect_value(mock_read_fn, bytes, 0);
+ ztest_returns_value(mock_read_fn, -EIO);
+
+ rv = st_raw_read_n(I2C_PORT_POWER, i2c_mock_get_addr(emul), 0, NULL, 2);
+ /* The shim layer translates -EIO to EC_ERROR_INVAL. */
+ zassert_equal(rv, EC_ERROR_INVAL, "rv was %d but expected %d", rv,
+ EC_ERROR_INVAL);
+}
+
+void test_suite_stm_mems_common(void)
+{
+ ztest_test_suite(stm_mems_common,
+ ztest_unit_test_setup_teardown(
+ test_st_raw_read_n,
+ unit_test_noop, unit_test_noop));
+ ztest_run_test_suite(stm_mems_common);
+}