summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTristan Honscheid <honscheid@google.com>2021-10-25 14:47:55 -0600
committerCommit Bot <commit-bot@chromium.org>2021-11-02 22:58:25 +0000
commit82be0a6d7c78ef3ea10cb5f9269fc337f9fc3409 (patch)
tree3d00f2b63f13b1402554b09a61602bc153b32872
parent5aa338628bcf5d30cfa3cd7136b7f8afae71f05e (diff)
downloadchrome-ec-82be0a6d7c78ef3ea10cb5f9269fc337f9fc3409.tar.gz
zephyr: Test accelgyro_bmi260.c init when init_rom_map returns NULL
Tests the situation where we load BMI config data when flash memory is not mapped (basically what occurs when `init_rom_map()` in `bmi_config_load()` returns NULL) Add a macro that helps inspect the argument history for an FFF fake. BRANCH=None BUG=b:184856157 TEST=zmake configure --test zephyr/test/drivers Signed-off-by: Tristan Honscheid <honscheid@google.com> Change-Id: If5e5b7cf7864f86ba3a40d06f4bea517cc854b18 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3242206 Reviewed-by: Yuval Peress <peress@google.com>
-rw-r--r--zephyr/test/drivers/include/test_mocks.h29
-rw-r--r--zephyr/test/drivers/src/bmi260.c77
2 files changed, 105 insertions, 1 deletions
diff --git a/zephyr/test/drivers/include/test_mocks.h b/zephyr/test/drivers/include/test_mocks.h
index 8ce47014cf..602db9174a 100644
--- a/zephyr/test/drivers/include/test_mocks.h
+++ b/zephyr/test/drivers/include/test_mocks.h
@@ -5,6 +5,35 @@
#include <fff.h>
+/*
+ * Convenience macros
+ */
+
+/**
+ * @brief Helper macro for inspecting the argument history of a given
+ * fake. Counts number of times the fake was called with a given
+ * argument.
+ * @param FAKE - FFF-provided fake structure (no pointers).
+ * @param ARG_NUM - Zero-based index of the argument to compare.
+ * @param VAL - Expression the argument must equal.
+ * @return Returns the number of times a call was made to the fake
+ * where the argument `ARG_NUM` equals `VAL`.
+ */
+#define MOCK_COUNT_CALLS_WITH_ARG_VALUE(FAKE, ARG_NUM, VAL) \
+ ({ \
+ int count = 0; \
+ for (int i = 0; i < (FAKE).call_count; i++) { \
+ if ((FAKE).arg##ARG_NUM##_history[i] == (VAL)) { \
+ count++; \
+ } \
+ } \
+ count; \
+ })
+
+/*
+ * Mock declarations
+ */
+
/* Mocks for common/init_rom.c */
DECLARE_FAKE_VALUE_FUNC(const void *, init_rom_map, const void *, int);
DECLARE_FAKE_VOID_FUNC(init_rom_unmap, const void *, int);
diff --git a/zephyr/test/drivers/src/bmi260.c b/zephyr/test/drivers/src/bmi260.c
index d5f6a55100..38e090785b 100644
--- a/zephyr/test/drivers/src/bmi260.c
+++ b/zephyr/test/drivers/src/bmi260.c
@@ -3,6 +3,7 @@
* found in the LICENSE file.
*/
+#include <fff.h>
#include <zephyr.h>
#include <ztest.h>
@@ -14,6 +15,7 @@
#include "motion_sense_fifo.h"
#include "driver/accelgyro_bmi260.h"
#include "driver/accelgyro_bmi_common.h"
+#include "test_mocks.h"
#define BMI_ORD DT_DEP_ORD(DT_NODELABEL(accel_bmi260))
#define BMI_ACC_SENSOR_ID SENSOR_ID(DT_NODELABEL(ms_bmi260_accel))
@@ -1567,6 +1569,15 @@ static int emul_init_ok(struct i2c_emul *emul, int reg, uint8_t *val, int byte,
return 1;
}
+/**
+ * A custom fake to use with the `init_rom_map` mock that returns the
+ * value of `addr`
+ */
+static const void *init_rom_map_addr_passthru(const void *addr, int size)
+{
+ return addr;
+}
+
/** Test init function of BMI260 accelerometer and gyroscope sensors */
static void test_bmi_init(void)
{
@@ -1577,6 +1588,10 @@ static void test_bmi_init(void)
ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID];
ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID];
+ /* The mock should return whatever is passed in to its addr param */
+ RESET_FAKE(init_rom_map);
+ init_rom_map_fake.custom_fake = init_rom_map_addr_passthru;
+
/*
* Test successful init. It is needed custom function to set value of
* BMI260_INTERNAL_STATUS register, because init function triggers reset
@@ -1969,6 +1984,64 @@ void test_bmi_init_chip_id(void)
EC_ERROR_ACCESS_DENIED, ret);
}
+/* Make an I2C emulator mock wrapped in FFF */
+FAKE_VALUE_FUNC(int, bmi_config_load_no_mapped_flash_mock_read_fn,
+ struct i2c_emul *, int, uint8_t *, int, void *);
+static int bmi_config_load_no_mapped_flash_mock_read_fn_helper(
+ 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 status of 'initialized' when this
+ * is read.
+ */
+ *val = BMI260_INIT_OK;
+ return 0;
+ }
+ /* For other registers, go through the normal emulator route */
+ return 1;
+}
+
+void test_bmi_config_load_no_mapped_flash(void)
+{
+ /* Tests the situation where we load BMI config data when flash memory
+ * is not mapped (basically what occurs when `init_rom_map()` in
+ * `bmi_config_load()` returns NULL)
+ */
+
+ struct i2c_emul *emul = bmi_emul_get(BMI_ORD);
+ struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID];
+ int ret, num_status_reg_reads;
+
+ /* Force bmi_config_load() to have to manually copy from memory */
+ RESET_FAKE(init_rom_map)
+ init_rom_map_fake.return_val = NULL;
+
+ /* Set proper chip ID and raise the INIT_OK flag to signal that config
+ * succeeded.
+ */
+ bmi_emul_set_reg(emul, BMI260_CHIP_ID, BMI260_CHIP_ID_MAJOR);
+ i2c_common_emul_set_read_func(
+ emul, bmi_config_load_no_mapped_flash_mock_read_fn, NULL);
+ RESET_FAKE(bmi_config_load_no_mapped_flash_mock_read_fn);
+ bmi_config_load_no_mapped_flash_mock_read_fn_fake.custom_fake =
+ bmi_config_load_no_mapped_flash_mock_read_fn_helper;
+
+ ret = ms_acc->drv->init(ms_acc);
+
+ zassert_equal(ret, EC_RES_SUCCESS, "Got %d but expected %d", ret,
+ EC_RES_SUCCESS);
+
+ /* Check the number of times we accessed BMI260_INTERNAL_STATUS */
+ num_status_reg_reads = MOCK_COUNT_CALLS_WITH_ARG_VALUE(
+ bmi_config_load_no_mapped_flash_mock_read_fn_fake, 1,
+ BMI260_INTERNAL_STATUS);
+ zassert_equal(1, num_status_reg_reads,
+ "Accessed status reg %d times but expected %d.",
+ num_status_reg_reads, 1);
+
+ i2c_common_emul_set_read_func(emul, NULL, NULL);
+}
+
void test_suite_bmi260(void)
{
ztest_test_suite(bmi260,
@@ -1992,6 +2065,8 @@ void test_suite_bmi260(void)
ztest_user_unit_test(test_bmi_gyr_fifo),
ztest_user_unit_test(test_unsupported_configs),
ztest_user_unit_test(test_interrupt_handler),
- ztest_user_unit_test(test_bmi_init_chip_id));
+ ztest_user_unit_test(test_bmi_init_chip_id),
+ ztest_user_unit_test(
+ test_bmi_config_load_no_mapped_flash));
ztest_run_test_suite(bmi260);
}