summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2021-10-28 11:57:27 -0600
committerCommit Bot <commit-bot@chromium.org>2021-11-02 22:58:34 +0000
commita58b4c52c3bcafa5c1b498380e9333377784e2e1 (patch)
treee609fe90d52951a35e795202cc74d83ce693bcaa
parent50fd26402af45541fc7b216b6e7ed29f0547de35 (diff)
downloadchrome-ec-stabilize-14321.B-main.tar.gz
zephyr: Finish accelgyro_bmi260.c unit testsstabilize-14321.B-main
Add remaining coverage to file. Test the timeout when waiting for the BMI260 chip to initialize and also handle a failed status read duirng that process BRANCH=None BUG=b:184856157 TEST=zmake configure --test zephyr/test/drivers Signed-off-by: Tristan Honscheid <honscheid@google.com> Change-Id: I6f5174e51a46070edd0ddb0267d9edf34d0d25e9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3251920 Reviewed-by: Abe Levkoy <alevkoy@chromium.org>
-rw-r--r--zephyr/test/drivers/src/bmi260.c72
1 files changed, 71 insertions, 1 deletions
diff --git a/zephyr/test/drivers/src/bmi260.c b/zephyr/test/drivers/src/bmi260.c
index 9c89245e8c..fb00288367 100644
--- a/zephyr/test/drivers/src/bmi260.c
+++ b/zephyr/test/drivers/src/bmi260.c
@@ -2104,6 +2104,73 @@ void test_bmi_config_unsupported_chip(void)
EC_ERROR_INVALID_CONFIG, ret);
}
+void test_init_config_read_failure(void)
+{
+ /* Test proper response to a failed read from the register
+ * BMI260_INTERNAL_STATUS.
+ */
+
+ struct i2c_emul *emul = bmi_emul_get(BMI_ORD);
+ struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID];
+ int ret;
+
+ /* Set up i2c emulator and mocks */
+ bmi_emul_set_reg(emul, BMI260_CHIP_ID, BMI260_CHIP_ID_MAJOR);
+ i2c_common_emul_set_read_fail_reg(emul, BMI260_INTERNAL_STATUS);
+ RESET_FAKE(init_rom_map);
+ init_rom_map_fake.custom_fake = init_rom_map_addr_passthru;
+
+ ret = ms_acc->drv->init(ms_acc);
+
+ zassert_equal(ret, EC_ERROR_INVALID_CONFIG, "Expected %d but got %d",
+ EC_ERROR_INVALID_CONFIG, ret);
+}
+
+/* Mock read function and counter used to test the timeout when
+ * waiting for the chip to initialize
+ */
+static int timeout_test_status_reg_access_count;
+static int status_timeout_mock_read_fn(struct i2c_emul *emul, int reg,
+ uint8_t *val, int bytes, void *data)
+{
+ if (reg == BMI260_INTERNAL_STATUS && val) {
+ /* We want to force-return a non-OK status each time */
+ timeout_test_status_reg_access_count++;
+ *val = BMI260_INIT_ERR;
+ return 0;
+ } else {
+ return 1;
+ }
+}
+
+void test_init_config_status_timeout(void)
+{
+ /* We allow up to 15 tries to get a successful BMI260_INIT_OK
+ * value from the BMI260_INTERNAL_STATUS register. Make sure
+ * we properly handle the case where the chip is not initialized
+ * before the timeout.
+ */
+
+ struct i2c_emul *emul = bmi_emul_get(BMI_ORD);
+ struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID];
+ int ret;
+
+ /* Set up i2c emulator and mocks */
+ bmi_emul_set_reg(emul, BMI260_CHIP_ID, BMI260_CHIP_ID_MAJOR);
+ timeout_test_status_reg_access_count = 0;
+ i2c_common_emul_set_read_func(emul, status_timeout_mock_read_fn, NULL);
+ RESET_FAKE(init_rom_map);
+ init_rom_map_fake.custom_fake = init_rom_map_addr_passthru;
+
+ ret = ms_acc->drv->init(ms_acc);
+
+ zassert_equal(timeout_test_status_reg_access_count, 15,
+ "Expected %d attempts but counted %d", 15,
+ timeout_test_status_reg_access_count);
+ zassert_equal(ret, EC_ERROR_INVALID_CONFIG, "Expected %d but got %d",
+ EC_ERROR_INVALID_CONFIG, ret);
+}
+
void test_suite_bmi260(void)
{
ztest_test_suite(bmi260,
@@ -2131,6 +2198,9 @@ void test_suite_bmi260(void)
ztest_user_unit_test(
test_bmi_config_load_no_mapped_flash),
ztest_user_unit_test(
- test_bmi_config_unsupported_chip));
+ test_bmi_config_unsupported_chip),
+ ztest_user_unit_test(
+ test_init_config_read_failure),
+ ztest_user_unit_test(test_init_config_status_timeout));
ztest_run_test_suite(bmi260);
}