From 5a12a3dbca6e07882d1760b115dcbf3b7ec5931c Mon Sep 17 00:00:00 2001 From: Yuval Peress Date: Mon, 20 Sep 2021 13:30:55 -0600 Subject: zephyr: test: add a generic I2C mock Add a simple mock with no state to test common i2c code. BRANCH=none BUG=b:200589041 TEST=zmake testall Signed-off-by: Yuval Peress Change-Id: I99730361a298708278106deb58890a0ed2c9febf Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3171694 Tested-by: Yuval Peress Reviewed-by: Jeremy Bettis Commit-Queue: Yuval Peress --- zephyr/dts/bindings/emul/cros,i2c-mock.yaml | 12 +++++ zephyr/emul/CMakeLists.txt | 1 + zephyr/emul/Kconfig | 1 + zephyr/emul/Kconfig.i2c_mock | 22 +++++++++ zephyr/emul/i2c_mock.c | 70 +++++++++++++++++++++++++++++ zephyr/include/emul/i2c_mock.h | 35 +++++++++++++++ 6 files changed, 141 insertions(+) create mode 100644 zephyr/dts/bindings/emul/cros,i2c-mock.yaml create mode 100644 zephyr/emul/Kconfig.i2c_mock create mode 100644 zephyr/emul/i2c_mock.c create mode 100644 zephyr/include/emul/i2c_mock.h diff --git a/zephyr/dts/bindings/emul/cros,i2c-mock.yaml b/zephyr/dts/bindings/emul/cros,i2c-mock.yaml new file mode 100644 index 0000000000..7da69028bd --- /dev/null +++ b/zephyr/dts/bindings/emul/cros,i2c-mock.yaml @@ -0,0 +1,12 @@ +# 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. + +description: A generic I2C mock + +compatible: "cros,i2c-mock" + +include: base.yaml +properties: + reg: + required: true diff --git a/zephyr/emul/CMakeLists.txt b/zephyr/emul/CMakeLists.txt index b46c51c8b6..02b176b942 100644 --- a/zephyr/emul/CMakeLists.txt +++ b/zephyr/emul/CMakeLists.txt @@ -14,3 +14,4 @@ zephyr_library_sources_ifdef(CONFIG_EMUL_TCS3400 emul_tcs3400.c) zephyr_library_sources_ifdef(CONFIG_EMUL_BB_RETIMER emul_bb_retimer.c) zephyr_library_sources_ifdef(CONFIG_EMUL_LN9310 emul_ln9310.c) zephyr_library_sources_ifdef(CONFIG_EMUL_LIS2DW12 emul_lis2dw12.c) +zephyr_library_sources_ifdef(CONFIG_I2C_MOCK i2c_mock.c) diff --git a/zephyr/emul/Kconfig b/zephyr/emul/Kconfig index c108caea4b..8d9c8e42ea 100644 --- a/zephyr/emul/Kconfig +++ b/zephyr/emul/Kconfig @@ -69,3 +69,4 @@ config EMUL_BB_RETIMER rsource "Kconfig.ln9310" rsource "Kconfig.lis2dw12" +rsource "Kconfig.i2c_mock" diff --git a/zephyr/emul/Kconfig.i2c_mock b/zephyr/emul/Kconfig.i2c_mock new file mode 100644 index 0000000000..6c98a32739 --- /dev/null +++ b/zephyr/emul/Kconfig.i2c_mock @@ -0,0 +1,22 @@ +# 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. + +DT_COMPAT_I2C_MOCK := cros,i2c-mock + +menuconfig I2C_MOCK + bool "Mock implementation of an I2C device" + default $(dt_compat_enabled,$(DT_COMPAT_I2C_MOCK)) + depends on I2C_EMUL + help + Enable the I2C mock. This driver is a pure mock and does nothing by + default. It is used to test common i2c code. Mock API is available in + zephyr/include/emul/i2c_mock.h + +if I2C_MOCK + +module = I2C_MOCK +module-str = i2c_mock +source "subsys/logging/Kconfig.template.log_config" + +endif # I2C_MOCK diff --git a/zephyr/emul/i2c_mock.c b/zephyr/emul/i2c_mock.c new file mode 100644 index 0000000000..7c3722ad2e --- /dev/null +++ b/zephyr/emul/i2c_mock.c @@ -0,0 +1,70 @@ +/* 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. + */ + +#define DT_DRV_COMPAT cros_i2c_mock + +#include +#include "emul/emul_common_i2c.h" + +#include +LOG_MODULE_REGISTER(i2c_mock, CONFIG_I2C_MOCK_LOG_LEVEL); + +struct i2c_emul *i2c_mock_to_i2c_emul(const struct emul *emul) +{ + struct i2c_common_emul_data *data = emul->data; + + return &(data->emul); +} + +void i2c_mock_reset(const struct emul *emul) +{ + struct i2c_emul *i2c_emul = i2c_mock_to_i2c_emul(emul); + + i2c_common_emul_set_read_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); +} + +uint16_t i2c_mock_get_addr(const struct emul *emul) +{ + const struct i2c_common_emul_cfg *cfg = emul->cfg; + + return cfg->addr; +} + +static const struct i2c_emul_api i2c_mock_api = { + .transfer = i2c_common_emul_transfer, +}; + +static int i2c_mock_init(const struct emul *emul, + const struct device *parent) +{ + const struct i2c_common_emul_cfg *cfg = emul->cfg; + struct i2c_common_emul_data *data = emul->data; + + data->emul.api = &i2c_mock_api; + data->emul.addr = cfg->addr; + data->emul.parent = emul; + data->i2c = parent; + data->cfg = cfg; + i2c_common_emul_init(data); + + return i2c_emul_register(parent, emul->dev_label, &data->emul); +} + +#define INIT_I2C_MOCK(n) \ + static const struct i2c_common_emul_cfg i2c_mock_cfg_##n = { \ + .i2c_label = DT_INST_BUS_LABEL(n), \ + .dev_label = DT_INST_LABEL(n), \ + .addr = DT_INST_REG_ADDR(n), \ + }; \ + static struct i2c_common_emul_data i2c_mock_data_##n; \ + EMUL_DEFINE(i2c_mock_init, DT_DRV_INST(n), &i2c_mock_cfg_##n, \ + &i2c_mock_data_##n) + +DT_INST_FOREACH_STATUS_OKAY(INIT_I2C_MOCK) diff --git a/zephyr/include/emul/i2c_mock.h b/zephyr/include/emul/i2c_mock.h new file mode 100644 index 0000000000..e9e8d97252 --- /dev/null +++ b/zephyr/include/emul/i2c_mock.h @@ -0,0 +1,35 @@ +/* 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. + */ + +#ifndef ZEPHYR_INCLUDE_EMUL_I2C_MOCK_H_ +#define ZEPHYR_INCLUDE_EMUL_I2C_MOCK_H_ + +#include +#include + +/** + * @brief reset the I2C mock. + * + * @param emul The mock device to reset. + */ +void i2c_mock_reset(const struct emul *emul); + +/** + * @brief Get the i2c emulator pointer from the top level mock. + * + * @param emul The mock device to query + * @return Pointer to the i2c emulator struct + */ +struct i2c_emul *i2c_mock_to_i2c_emul(const struct emul *emul); + +/** + * @brief Get the I2C address of the mock + * + * @param emul The mock device to query + * @return The address on the I2C bus + */ +uint16_t i2c_mock_get_addr(const struct emul *emul); + +#endif /* ZEPHYR_INCLUDE_EMUL_I2C_MOCK_H_ */ -- cgit v1.2.1