diff options
Diffstat (limited to 'zephyr/test/drivers')
27 files changed, 1373 insertions, 983 deletions
diff --git a/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h b/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h index a291e8162c..6ace7a9e15 100644 --- a/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h +++ b/zephyr/test/drivers/common/include/test/drivers/tcpci_test_common.h @@ -42,7 +42,9 @@ void check_tcpci_reg_with_mask_f(const struct emul *emul, int reg, * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_init(const struct emul *emul, enum usbc_port port); +void test_tcpci_init(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI release callback @@ -51,7 +53,9 @@ void test_tcpci_init(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_release(const struct emul *emul, enum usbc_port port); +void test_tcpci_release(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI get cc callback @@ -60,7 +64,9 @@ void test_tcpci_release(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_get_cc(const struct emul *emul, enum usbc_port port); +void test_tcpci_get_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set cc callback @@ -69,7 +75,9 @@ void test_tcpci_get_cc(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_cc(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set polarity callback @@ -78,7 +86,9 @@ void test_tcpci_set_cc(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_polarity(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_polarity(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set vconn callback @@ -87,7 +97,9 @@ void test_tcpci_set_polarity(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_vconn(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_vconn(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set msg header callback @@ -96,7 +108,9 @@ void test_tcpci_set_vconn(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_msg_header(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_msg_header(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI rx and sop prime enable callback @@ -105,7 +119,9 @@ void test_tcpci_set_msg_header(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_rx_detect(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI get raw message from TCPC callback @@ -115,6 +131,7 @@ void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port); * tcpc_config */ void test_tcpci_get_rx_message_raw(const struct emul *emul, + struct i2c_common_emul_data *common_data, enum usbc_port port); /** @@ -124,7 +141,9 @@ void test_tcpci_get_rx_message_raw(const struct emul *emul, * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_transmit(const struct emul *emul, enum usbc_port port); +void test_tcpci_transmit(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI alert callback @@ -133,7 +152,9 @@ void test_tcpci_transmit(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_alert(const struct emul *emul, enum usbc_port port); +void test_tcpci_alert(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI alert RX message callback @@ -142,7 +163,9 @@ void test_tcpci_alert(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port); +void test_tcpci_alert_rx_message(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI auto discharge on disconnect callback @@ -151,7 +174,9 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_auto_discharge(const struct emul *emul, enum usbc_port port); +void test_tcpci_auto_discharge(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI drp toggle callback @@ -160,7 +185,9 @@ void test_tcpci_auto_discharge(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_drp_toggle(const struct emul *emul, enum usbc_port port); +void test_tcpci_drp_toggle(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI get chip info callback @@ -169,7 +196,9 @@ void test_tcpci_drp_toggle(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_get_chip_info(const struct emul *emul, enum usbc_port port); +void test_tcpci_get_chip_info(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI enter low power mode callback @@ -178,7 +207,9 @@ void test_tcpci_get_chip_info(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_low_power_mode(const struct emul *emul, enum usbc_port port); +void test_tcpci_low_power_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); /** * @brief Test TCPCI set bist test mode callback @@ -187,6 +218,8 @@ void test_tcpci_low_power_mode(const struct emul *emul, enum usbc_port port); * @param port Select USBC port that will be used to obtain tcpm_drv from * tcpc_config */ -void test_tcpci_set_bist_mode(const struct emul *emul, enum usbc_port port); +void test_tcpci_set_bist_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port); #endif /* __TCPCI_TEST_COMMON_H */ diff --git a/zephyr/test/drivers/common/include/test/drivers/utils.h b/zephyr/test/drivers/common/include/test/drivers/utils.h index b7fd6ebb86..d656b8fe02 100644 --- a/zephyr/test/drivers/common/include/test/drivers/utils.h +++ b/zephyr/test/drivers/common/include/test/drivers/utils.h @@ -8,6 +8,7 @@ #include <zephyr/drivers/emul.h> #include <zephyr/drivers/gpio/gpio_emul.h> +#include <zephyr/ztest.h> #include <stddef.h> #include <string.h> diff --git a/zephyr/test/drivers/common/src/utils.c b/zephyr/test/drivers/common/src/utils.c index ba27eb7095..a5d9cd0733 100644 --- a/zephyr/test/drivers/common/src/utils.c +++ b/zephyr/test/drivers/common/src/utils.c @@ -13,6 +13,7 @@ #include "charge_state.h" #include "emul/emul_isl923x.h" #include "emul/emul_smart_battery.h" +#include "emul/emul_stub_device.h" #include "emul/tcpc/emul_tcpci_partner_src.h" #include "hooks.h" #include "power.h" @@ -27,12 +28,12 @@ void test_set_chipset_to_s0(void) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); const struct device *battery_gpio_dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); printk("%s: Forcing power on\n", __func__); - emul = sbat_emul_get_ptr(BATTERY_ORD); + bat = sbat_emul_get_bat_data(emul); /* @@ -496,3 +497,14 @@ void test_set_chipset_to_g3_then_transition_to_s5(void) */ k_sleep(K_SECONDS(1)); } + +int emul_init_stub(const struct device *dev) +{ + ARG_UNUSED(dev); + + return 0; +} + +/* These 2 lines are needed because we don't define an espi host driver */ +#define DT_DRV_COMPAT zephyr_espi_emul_espi_host +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); diff --git a/zephyr/test/drivers/default/src/bb_retimer.c b/zephyr/test/drivers/default/src/bb_retimer.c index a9db2eb052..1eb428ad84 100644 --- a/zephyr/test/drivers/default/src/bb_retimer.c +++ b/zephyr/test/drivers/default/src/bb_retimer.c @@ -44,15 +44,15 @@ ZTEST_USER(bb_retimer_no_tasks, test_bb_set_state) { struct pd_discovery *disc; uint32_t conn, exp_conn; - struct i2c_emul *emul; + const struct emul *emul = bb_emul_get(BB_RETIMER_ORD); + struct i2c_common_emul_data *common_data = + emul_bb_retimer_get_i2c_common_data(emul); bool ack_required; - emul = bb_emul_get(BB_RETIMER_ORD); - set_test_runner_tid(); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, + i2c_common_emul_set_write_fail_reg(common_data, BB_RETIMER_REG_CONNECTION_STATE); /* Test fail on reset register write */ @@ -63,7 +63,8 @@ ZTEST_USER(bb_retimer_no_tasks, test_bb_set_state) zassert_false(ack_required, "ACK is never required for BB retimer"); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set UFP role for whole test */ tc_set_data_role(USBC_PORT_C1, PD_ROLE_UFP); @@ -201,7 +202,7 @@ ZTEST_USER(bb_retimer_no_tasks, test_bb_set_dfp_state) union tbt_mode_resp_cable cable_resp; struct pd_discovery *disc, *dev_disc; uint32_t conn, exp_conn; - struct i2c_emul *emul; + const struct emul *emul = bb_emul_get(BB_RETIMER_ORD); bool ack_required; emul = bb_emul_get(BB_RETIMER_ORD); @@ -463,7 +464,9 @@ ZTEST_USER(bb_retimer, test_bb_init) { const struct device *gpio_dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_USB_C1_LS_EN_PATH, gpios)); - struct i2c_emul *emul; + const struct emul *emul = bb_emul_get(BB_RETIMER_ORD); + struct i2c_common_emul_data *common_data = + emul_bb_retimer_get_i2c_common_data(emul); zassert_not_null(gpio_dev, "Cannot get GPIO device"); @@ -473,7 +476,8 @@ ZTEST_USER(bb_retimer, test_bb_init) test_set_chipset_to_s0(); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BB_RETIMER_REG_VENDOR_ID); + i2c_common_emul_set_read_fail_reg(common_data, + BB_RETIMER_REG_VENDOR_ID); /* Test fail on vendor ID read */ zassert_equal(EC_ERROR_INVAL, bb_usb_retimer.init(&usb_muxes[USBC_PORT_C1]), NULL); @@ -485,7 +489,8 @@ ZTEST_USER(bb_retimer, test_bb_init) NULL); /* Setup wrong vendor ID */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bb_emul_set_reg(emul, BB_RETIMER_REG_VENDOR_ID, 0x12144678); /* Test fail on wrong vendor ID */ zassert_equal(EC_ERROR_INVAL, @@ -497,7 +502,8 @@ ZTEST_USER(bb_retimer, test_bb_init) NULL); /* Setup emulator fail on device ID read */ - i2c_common_emul_set_read_fail_reg(emul, BB_RETIMER_REG_DEVICE_ID); + i2c_common_emul_set_read_fail_reg(common_data, + BB_RETIMER_REG_DEVICE_ID); bb_emul_set_reg(emul, BB_RETIMER_REG_VENDOR_ID, BB_RETIMER_VENDOR_ID_1); /* Test fail on device ID read */ zassert_equal(EC_ERROR_INVAL, @@ -509,7 +515,8 @@ ZTEST_USER(bb_retimer, test_bb_init) NULL); /* Setup wrong device ID */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bb_emul_set_reg(emul, BB_RETIMER_REG_DEVICE_ID, 0x12144678); /* Test fail on wrong device ID */ zassert_equal(EC_ERROR_INVAL, diff --git a/zephyr/test/drivers/default/src/bc12.c b/zephyr/test/drivers/default/src/bc12.c index ac1c5692d1..76268e9172 100644 --- a/zephyr/test/drivers/default/src/bc12.c +++ b/zephyr/test/drivers/default/src/bc12.c @@ -98,7 +98,7 @@ static const struct bc12_status bc12_chg_limits[] = { static void test_bc12_pi3usb9201_host_mode(void) { - struct i2c_emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD); + const struct emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD); uint8_t a, b; /* @@ -145,7 +145,7 @@ test_bc12_pi3usb9201_client_mode(enum pi3usb9201_client_sts detect_result, enum charge_supplier supplier, int current_limit) { - struct i2c_emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD); + const struct emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD); uint8_t a, b; int port, voltage; @@ -234,7 +234,7 @@ ZTEST_USER(bc12, test_bc12_pi3usb9201) DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_BATT_PRES_ODL_PATH, gpios)); const struct device *acok_dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_ACOK_OD_PATH, gpios)); - struct i2c_emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD); + const struct emul *emul = pi3usb9201_emul_get(PI3USB9201_ORD); uint8_t a, b; /* Pretend we have battery and AC so charging works normally. */ diff --git a/zephyr/test/drivers/default/src/bma2x2.c b/zephyr/test/drivers/default/src/bma2x2.c index 7f8b5d4777..becacf5266 100644 --- a/zephyr/test/drivers/default/src/bma2x2.c +++ b/zephyr/test/drivers/default/src/bma2x2.c @@ -19,9 +19,9 @@ /** How accurate comparision of vectors should be. */ #define V_EPS 8 -#define EMUL_LABEL DT_NODELABEL(bma_emul) +#define EMUL_NODE DT_NODELABEL(bma_emul) -#define BMA_ORD DT_DEP_ORD(EMUL_LABEL) +#define BMA_ORD DT_DEP_ORD(EMUL_NODE) /** Mutex for test motion sensor */ static mutex_t sensor_mutex; @@ -52,13 +52,13 @@ static struct motion_sensor_t ms = { .mutex = &sensor_mutex, .drv_data = &acc_data, .port = I2C_PORT_NODELABEL(i2c0), - .i2c_spi_addr_flags = DT_REG_ADDR(EMUL_LABEL), + .i2c_spi_addr_flags = DT_REG_ADDR(EMUL_NODE), .rot_standard_ref = NULL, .current_range = 0, }; /** Set emulator offset values to vector of three int16_t */ -static void set_emul_offset(struct i2c_emul *emul, int16_t *offset) +static void set_emul_offset(const struct emul *emul, int16_t *offset) { bma_emul_set_off(emul, BMA_EMUL_AXIS_X, offset[0]); bma_emul_set_off(emul, BMA_EMUL_AXIS_Y, offset[1]); @@ -66,7 +66,7 @@ static void set_emul_offset(struct i2c_emul *emul, int16_t *offset) } /** Save emulator offset values to vector of three int16_t */ -static void get_emul_offset(struct i2c_emul *emul, int16_t *offset) +static void get_emul_offset(const struct emul *emul, int16_t *offset) { offset[0] = bma_emul_get_off(emul, BMA_EMUL_AXIS_X); offset[1] = bma_emul_get_off(emul, BMA_EMUL_AXIS_Y); @@ -74,7 +74,7 @@ static void get_emul_offset(struct i2c_emul *emul, int16_t *offset) } /** Set emulator accelerometer values to vector of three int16_t */ -static void set_emul_acc(struct i2c_emul *emul, int16_t *acc) +static void set_emul_acc(const struct emul *emul, int16_t *acc) { bma_emul_set_acc(emul, BMA_EMUL_AXIS_X, acc[0]); bma_emul_set_acc(emul, BMA_EMUL_AXIS_Y, acc[1]); @@ -121,7 +121,7 @@ struct reset_func_data { * accessing register data.ok_before_fail times. Error is returned during next * data.fail_attempts times. */ -static int emul_read_reset(struct i2c_emul *emul, int reg, uint8_t *buf, +static int emul_read_reset(const struct emul *emul, int reg, uint8_t *buf, int bytes, void *data) { struct reset_func_data *d = data; @@ -156,7 +156,9 @@ static int emul_read_reset(struct i2c_emul *emul, int reg, uint8_t *buf, */ ZTEST_USER(bma2x2, test_bma_get_offset) { - struct i2c_emul *emul; + const struct emul *emul = bma_emul_get(BMA_ORD); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int16_t ret_offset[3]; int16_t exp_offset[3]; int16_t temp; @@ -164,18 +166,22 @@ ZTEST_USER(bma2x2, test_bma_get_offset) emul = bma_emul_get(BMA_ORD); /* Test fail on each axis */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_OFFSET_X_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->get_offset(&ms, ret_offset, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_OFFSET_Y_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->get_offset(&ms, ret_offset, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_OFFSET_Z_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->get_offset(&ms, ret_offset, &temp), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set emulator offset */ exp_offset[0] = BMA_EMUL_1G / 10; @@ -207,7 +213,9 @@ ZTEST_USER(bma2x2, test_bma_get_offset) */ ZTEST_USER(bma2x2, test_bma_set_offset) { - struct i2c_emul *emul; + const struct emul *emul = bma_emul_get(BMA_ORD); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int16_t ret_offset[3]; int16_t exp_offset[3]; int16_t temp = 0; @@ -215,18 +223,22 @@ ZTEST_USER(bma2x2, test_bma_set_offset) emul = bma_emul_get(BMA_ORD); /* Test fail on each axis */ - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, + BMA2x2_OFFSET_X_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->set_offset(&ms, exp_offset, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, + BMA2x2_OFFSET_Y_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->set_offset(&ms, exp_offset, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, + BMA2x2_OFFSET_Z_AXIS_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->set_offset(&ms, exp_offset, temp), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_offset[0] = BMA_EMUL_1G / 10; @@ -259,7 +271,7 @@ ZTEST_USER(bma2x2, test_bma_set_offset) * Try to set range and check if expected range was set in driver and in * emulator. */ -static void check_set_range_f(struct i2c_emul *emul, int range, int rnd, +static void check_set_range_f(const struct emul *emul, int range, int rnd, int exp_range, int line) { uint8_t exp_range_reg; @@ -304,7 +316,9 @@ static void check_set_range_f(struct i2c_emul *emul, int range, int rnd, /** Test set range with and without I2C errors. */ ZTEST_USER(bma2x2, test_bma_set_range) { - struct i2c_emul *emul; + const struct emul *emul = bma_emul_get(BMA_ORD); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int start_range; emul = bma_emul_get(BMA_ORD); @@ -314,7 +328,8 @@ ZTEST_USER(bma2x2, test_bma_set_range) ms.current_range = start_range; bma_emul_set_reg(emul, BMA2x2_RANGE_SELECT_ADDR, BMA2x2_RANGE_2G); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_RANGE_SELECT_ADDR); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms.drv->set_range(&ms, 12, 0), NULL); @@ -327,10 +342,12 @@ ZTEST_USER(bma2x2, test_bma_set_range) bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, + BMA2x2_RANGE_SELECT_ADDR); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms.drv->set_range(&ms, 12, 0), NULL); @@ -343,7 +360,8 @@ ZTEST_USER(bma2x2, test_bma_set_range) bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_range(emul, 1, 0, 2); @@ -378,47 +396,53 @@ ZTEST_USER(bma2x2, test_bma_set_range) ZTEST_USER(bma2x2, test_bma_init) { struct reset_func_data reset_func_data; - struct i2c_emul *emul; + const struct emul *emul = bma_emul_get(BMA_ORD); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); emul = bma_emul_get(BMA_ORD); /* Setup emulator fail read function */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_CHIP_ID_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_CHIP_ID_ADDR); /* Test fail on chip id read */ zassert_equal(EC_ERROR_UNKNOWN, ms.drv->init(&ms), NULL); /* Disable failing on chip id read, but set wrong value */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bma_emul_set_reg(emul, BMA2x2_CHIP_ID_ADDR, 23); /* Test wrong chip id */ zassert_equal(EC_ERROR_ACCESS_DENIED, ms.drv->init(&ms), NULL); /* Set correct chip id, but fail on reset reg read */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RST_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_RST_ADDR); bma_emul_set_reg(emul, BMA2x2_CHIP_ID_ADDR, BMA255_CHIP_ID_MAJOR); /* Test fail on reset register read */ zassert_equal(EC_ERROR_INVAL, ms.drv->init(&ms), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_RST_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, BMA2x2_RST_ADDR); /* Test fail on reset register write */ zassert_equal(EC_ERROR_INVAL, ms.drv->init(&ms), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail reset read function */ reset_func_data.ok_before_fail = 1; reset_func_data.fail_attempts = 100; reset_func_data.reset_value = 0; - i2c_common_emul_set_read_func(emul, emul_read_reset, &reset_func_data); + i2c_common_emul_set_read_func(common_data, emul_read_reset, + &reset_func_data); /* Test fail on too many reset read errors */ zassert_equal(EC_ERROR_TIMEOUT, ms.drv->init(&ms), NULL); @@ -443,14 +467,14 @@ ZTEST_USER(bma2x2, test_bma_init) zassert_equal(EC_RES_SUCCESS, ms.drv->init(&ms), NULL); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /* * Try to set data rate and check if expected rate was set in driver and in * emulator. */ -static void check_set_rate_f(struct i2c_emul *emul, int rate, int rnd, +static void check_set_rate_f(const struct emul *emul, int rate, int rnd, int exp_rate, int line) { uint8_t exp_rate_reg; @@ -508,7 +532,9 @@ static void check_set_rate_f(struct i2c_emul *emul, int rate, int rnd, /** Test set and get rate with and without I2C errors. */ ZTEST_USER(bma2x2, test_bma_rate) { - struct i2c_emul *emul; + const struct emul *emul = bma_emul_get(BMA_ORD); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); uint8_t reg_rate; int drv_rate; @@ -577,7 +603,7 @@ ZTEST_USER(bma2x2, test_bma_rate) reg_rate = bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_BW_SELECT_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_BW_SELECT_ADDR); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms.drv->set_data_rate(&ms, 15625, 0), @@ -592,10 +618,11 @@ ZTEST_USER(bma2x2, test_bma_rate) NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMA2x2_BW_SELECT_ADDR); + i2c_common_emul_set_write_fail_reg(common_data, BMA2x2_BW_SELECT_ADDR); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms.drv->set_data_rate(&ms, 15625, 0), @@ -610,13 +637,16 @@ ZTEST_USER(bma2x2, test_bma_rate) NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); } /** Test read with and without I2C errors. */ ZTEST_USER(bma2x2, test_bma_read) { - struct i2c_emul *emul; + const struct emul *emul = bma_emul_get(BMA_ORD); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int16_t ret_acc[3]; int16_t exp_acc[3]; intv3_t ret_acc_v; @@ -629,21 +659,22 @@ ZTEST_USER(bma2x2, test_bma_read) bma_emul_set_off(emul, BMA_EMUL_AXIS_Z, 0); /* Test fail on each axis */ - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_LSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_X_AXIS_LSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_MSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_X_AXIS_MSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_LSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_Y_AXIS_LSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_MSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_Y_AXIS_MSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_LSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_Z_AXIS_LSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_MSB_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, BMA2x2_Z_AXIS_MSB_ADDR); zassert_equal(EC_ERROR_INVAL, ms.drv->read(&ms, ret_acc_v), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input accelerometer values */ exp_acc[0] = BMA_EMUL_1G / 10; @@ -705,7 +736,7 @@ struct calib_func_data { * error when offset control register is accessed when cal ready bit is not set * and data.read_fail is not zero. */ -static int emul_read_calib_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int emul_read_calib_func(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct calib_func_data *d = data; @@ -737,7 +768,7 @@ static int emul_read_calib_func(struct i2c_emul *emul, int reg, uint8_t *val, * calib_start field in data with time when offset compensation process was * triggerd. */ -static int emul_write_calib_func(struct i2c_emul *emul, int reg, uint8_t val, +static int emul_write_calib_func(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { struct calib_func_data *d = data; @@ -758,7 +789,9 @@ static int emul_write_calib_func(struct i2c_emul *emul, int reg, uint8_t val, ZTEST_USER(bma2x2, test_bma_perform_calib) { struct calib_func_data func_data; - struct i2c_emul *emul; + const struct emul *emul = bma_emul_get(BMA_ORD); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int16_t start_off[3]; int16_t exp_off[3]; int16_t ret_off[3]; @@ -768,8 +801,6 @@ ZTEST_USER(bma2x2, test_bma_perform_calib) { 0, FLOAT_TO_FP(1), 0 }, { 0, 0, FLOAT_TO_FP(-1) } }; - emul = bma_emul_get(BMA_ORD); - /* Range and rate cannot change after calibration */ range = 4; rate = 125000; @@ -797,8 +828,10 @@ ZTEST_USER(bma2x2, test_bma_perform_calib) exp_off[2] = BMA_EMUL_1G - exp_off[2]; /* Setup emulator calibration functions */ - i2c_common_emul_set_read_func(emul, emul_read_calib_func, &func_data); - i2c_common_emul_set_write_func(emul, emul_write_calib_func, &func_data); + i2c_common_emul_set_read_func(common_data, emul_read_calib_func, + &func_data); + i2c_common_emul_set_write_func(common_data, emul_write_calib_func, + &func_data); /* Setup emulator to fail on first access to offset control register */ func_data.calib_start = k_uptime_get_32(); @@ -896,8 +929,8 @@ ZTEST_USER(bma2x2, test_bma_perform_calib) compare_int3v(exp_off, ret_off); /* Remove custom emulator functions */ - i2c_common_emul_set_read_func(emul, NULL, NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } /** Test get resolution. */ diff --git a/zephyr/test/drivers/default/src/bmi160.c b/zephyr/test/drivers/default/src/bmi160.c index 39144bd9e6..fc6f2c824e 100644 --- a/zephyr/test/drivers/default/src/bmi160.c +++ b/zephyr/test/drivers/default/src/bmi160.c @@ -50,7 +50,7 @@ static void rotate_int3v_by_test_rotation(intv3_t v) } /** Set emulator accelerometer offset values to intv3_t vector */ -static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) +static void set_emul_acc_offset(const struct emul *emul, intv3_t offset) { bmi_emul_set_off(emul, BMI_EMUL_ACC_X, offset[0]); bmi_emul_set_off(emul, BMI_EMUL_ACC_Y, offset[1]); @@ -58,7 +58,7 @@ static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) } /** Save emulator accelerometer offset values to intv3_t vector */ -static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) +static void get_emul_acc_offset(const struct emul *emul, intv3_t offset) { offset[0] = bmi_emul_get_off(emul, BMI_EMUL_ACC_X); offset[1] = bmi_emul_get_off(emul, BMI_EMUL_ACC_Y); @@ -66,7 +66,7 @@ static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) } /** Set emulator accelerometer values to intv3_t vector */ -static void set_emul_acc(struct i2c_emul *emul, intv3_t acc) +static void set_emul_acc(const struct emul *emul, intv3_t acc) { bmi_emul_set_value(emul, BMI_EMUL_ACC_X, acc[0]); bmi_emul_set_value(emul, BMI_EMUL_ACC_Y, acc[1]); @@ -74,7 +74,7 @@ static void set_emul_acc(struct i2c_emul *emul, intv3_t acc) } /** Set emulator gyroscope offset values to intv3_t vector */ -static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) +static void set_emul_gyr_offset(const struct emul *emul, intv3_t offset) { bmi_emul_set_off(emul, BMI_EMUL_GYR_X, offset[0]); bmi_emul_set_off(emul, BMI_EMUL_GYR_Y, offset[1]); @@ -82,7 +82,7 @@ static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) } /** Save emulator gyroscope offset values to intv3_t vector */ -static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) +static void get_emul_gyr_offset(const struct emul *emul, intv3_t offset) { offset[0] = bmi_emul_get_off(emul, BMI_EMUL_GYR_X); offset[1] = bmi_emul_get_off(emul, BMI_EMUL_GYR_Y); @@ -90,7 +90,7 @@ static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) } /** Set emulator gyroscope values to vector of three int16_t */ -static void set_emul_gyr(struct i2c_emul *emul, intv3_t gyr) +static void set_emul_gyr(const struct emul *emul, intv3_t gyr) { bmi_emul_set_value(emul, BMI_EMUL_GYR_X, gyr[0]); bmi_emul_set_value(emul, BMI_EMUL_GYR_Y, gyr[1]); @@ -137,7 +137,9 @@ static void compare_int3v_f(intv3_t exp_v, intv3_t v, int eps, int line) ZTEST_USER(bmi160, test_bmi_acc_get_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int16_t ret[3]; intv3_t ret_v; intv3_t exp_v; @@ -157,16 +159,17 @@ ZTEST_USER(bmi160, test_bmi_acc_get_offset) exp_v[2] = -1000 / 30; /* Test fail on offset read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; @@ -192,7 +195,9 @@ ZTEST_USER(bmi160, test_bmi_acc_get_offset) ZTEST_USER(bmi160, test_bmi_gyr_get_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int16_t ret[3]; intv3_t ret_v; intv3_t exp_v; @@ -202,7 +207,8 @@ ZTEST_USER(bmi160, test_bmi_gyr_get_offset) ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set emulator offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -215,19 +221,20 @@ ZTEST_USER(bmi160, test_bmi_gyr_get_offset) exp_v[2] = -125000 / 300; /* Test fail on offset read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; @@ -256,7 +263,9 @@ ZTEST_USER(bmi160, test_bmi_gyr_get_offset) ZTEST_USER(bmi160, test_bmi_acc_set_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int16_t input_v[3]; int16_t temp = 0; intv3_t ret_v; @@ -266,26 +275,31 @@ ZTEST_USER(bmi160, test_bmi_acc_set_offset) ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Test fail on OFFSET EN GYR98 register read and write */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_ACC70); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_ACC70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_ACC70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_v[0] = BMI_EMUL_1G / 10; @@ -334,7 +348,9 @@ ZTEST_USER(bmi160, test_bmi_acc_set_offset) ZTEST_USER(bmi160, test_bmi_gyr_set_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int16_t input_v[3]; int16_t temp = 0; intv3_t ret_v; @@ -344,26 +360,31 @@ ZTEST_USER(bmi160, test_bmi_gyr_set_offset) ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Test fail on OFFSET EN GYR98 register read and write */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_GYR70); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_GYR70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_GYR70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -404,7 +425,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_set_offset) * Try to set accelerometer range and check if expected range was set * in driver and in emulator. */ -static void check_set_acc_range_f(struct i2c_emul *emul, +static void check_set_acc_range_f(const struct emul *emul, struct motion_sensor_t *ms, int range, int rnd, int exp_range, int line) { @@ -450,7 +471,9 @@ static void check_set_acc_range_f(struct i2c_emul *emul, ZTEST_USER(bmi160, test_bmi_acc_set_range) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int start_range; emul = bmi_emul_get(BMI_ORD); @@ -461,7 +484,7 @@ ZTEST_USER(bmi160, test_bmi_acc_set_range) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI160_ACC_RANGE, BMI160_GSEL_2G); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_ACC_RANGE); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_ACC_RANGE); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 0), NULL); @@ -474,7 +497,8 @@ ZTEST_USER(bmi160, test_bmi_acc_set_range) NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_acc_range(emul, ms, 1, 0, 2); @@ -509,7 +533,7 @@ ZTEST_USER(bmi160, test_bmi_acc_set_range) * Try to set gyroscope range and check if expected range was set in driver and * in emulator. */ -static void check_set_gyr_range_f(struct i2c_emul *emul, +static void check_set_gyr_range_f(const struct emul *emul, struct motion_sensor_t *ms, int range, int rnd, int exp_range, int line) { @@ -558,7 +582,9 @@ static void check_set_gyr_range_f(struct i2c_emul *emul, ZTEST_USER(bmi160, test_bmi_gyr_set_range) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int start_range; emul = bmi_emul_get(BMI_ORD); @@ -569,7 +595,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_set_range) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI160_GYR_RANGE, BMI160_DPS_SEL_250); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_GYR_RANGE); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_GYR_RANGE); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 125, 0), NULL); @@ -582,7 +608,8 @@ ZTEST_USER(bmi160, test_bmi_gyr_set_range) bmi_emul_get_reg(emul, BMI160_GYR_RANGE), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_gyr_range(emul, ms, 1, 0, 125); @@ -643,7 +670,7 @@ ZTEST_USER(bmi160, test_bmi_get_resolution) * Try to set accelerometer data rate and check if expected rate was set * in driver and in emulator. */ -static void check_set_acc_rate_f(struct i2c_emul *emul, +static void check_set_acc_rate_f(const struct emul *emul, struct motion_sensor_t *ms, int rate, int rnd, int exp_rate, int line) { @@ -703,7 +730,9 @@ static void check_set_acc_rate_f(struct i2c_emul *emul, ZTEST_USER(bmi160, test_bmi_acc_rate) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); uint8_t reg_rate; int pmu_status; int drv_rate; @@ -776,7 +805,7 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) reg_rate = bmi_emul_get_reg(emul, BMI160_ACC_CONF); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_CONF); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -789,10 +818,11 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_ACC_CONF); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_ACC_CONF); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -805,7 +835,8 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET; @@ -832,7 +863,7 @@ ZTEST_USER(bmi160, test_bmi_acc_rate) * Try to set gyroscope data rate and check if expected rate was set * in driver and in emulator. */ -static void check_set_gyr_rate_f(struct i2c_emul *emul, +static void check_set_gyr_rate_f(const struct emul *emul, struct motion_sensor_t *ms, int rate, int rnd, int exp_rate, int line) { @@ -892,7 +923,9 @@ static void check_set_gyr_rate_f(struct i2c_emul *emul, ZTEST_USER(bmi160, test_bmi_gyr_rate) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); uint8_t reg_rate; int pmu_status; int drv_rate; @@ -959,7 +992,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_rate) reg_rate = bmi_emul_get_reg(emul, BMI160_GYR_CONF); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_CONF); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -972,10 +1005,11 @@ ZTEST_USER(bmi160, test_bmi_gyr_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_GYR_CONF); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_GYR_CONF); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -988,7 +1022,8 @@ ZTEST_USER(bmi160, test_bmi_gyr_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET; @@ -1049,7 +1084,9 @@ ZTEST_USER(bmi160, test_bmi_scale) ZTEST_USER(bmi160, test_bmi_read_temp) { struct motion_sensor_t *ms_acc, *ms_gyr; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int ret_temp; int exp_temp; @@ -1058,18 +1095,19 @@ ZTEST_USER(bmi160, test_bmi_read_temp) ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_TEMPERATURE_0); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_TEMPERATURE_0); zassert_equal(EC_ERROR_NOT_POWERED, ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL); zassert_equal(EC_ERROR_NOT_POWERED, ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_TEMPERATURE_1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_TEMPERATURE_1); zassert_equal(EC_ERROR_NOT_POWERED, ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL); zassert_equal(EC_ERROR_NOT_POWERED, ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Fail on invalid temperature */ bmi_emul_set_reg(emul, BMI160_TEMPERATURE_0, 0x00); @@ -1128,7 +1166,9 @@ ZTEST_USER(bmi160, test_bmi_read_temp) ZTEST_USER(bmi160, test_bmi_acc_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); intv3_t ret_v; intv3_t exp_v; int16_t scale[3] = { MOTION_SENSE_DEFAULT_SCALE, @@ -1144,10 +1184,11 @@ ZTEST_USER(bmi160, test_bmi_acc_read) bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, 0); /* Fail on read status */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1215,20 +1256,21 @@ ZTEST_USER(bmi160, test_bmi_acc_read) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_X_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_X_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Y_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_Y_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Y_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_Y_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Z_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_Z_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Z_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_Z_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1236,7 +1278,9 @@ ZTEST_USER(bmi160, test_bmi_acc_read) ZTEST_USER(bmi160, test_bmi_gyr_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); intv3_t ret_v; intv3_t exp_v; int16_t scale[3] = { MOTION_SENSE_DEFAULT_SCALE, @@ -1252,10 +1296,11 @@ ZTEST_USER(bmi160, test_bmi_gyr_read) bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, 0); /* Fail on read status */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1323,20 +1368,21 @@ ZTEST_USER(bmi160, test_bmi_gyr_read) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_X_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_X_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Y_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_Y_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Y_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_Y_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Z_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_Z_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Z_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_Z_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1344,7 +1390,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_read) * Custom emulatro read function which always return not ready STATUS register. * Used in calibration test. */ -static int emul_nrdy(struct i2c_emul *emul, int reg, uint8_t *val, int byte, +static int emul_nrdy(const struct emul *emul, int reg, uint8_t *val, int byte, void *data) { if (reg == BMI160_STATUS) { @@ -1361,7 +1407,9 @@ static int emul_nrdy(struct i2c_emul *emul, int reg, uint8_t *val, int byte, ZTEST_USER(bmi160, test_bmi_acc_perform_calib) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); uint8_t pmu_status; intv3_t start_off; intv3_t exp_off; @@ -1407,13 +1455,13 @@ ZTEST_USER(bmi160, test_bmi_acc_perform_calib) exp_off[2] = BMI_EMUL_1G - exp_off[2]; /* Test fail on rate set */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_ACC_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); @@ -1421,13 +1469,14 @@ ZTEST_USER(bmi160, test_bmi_acc_perform_calib) bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); /* Test fail on data not ready */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_read_func(emul, emul_nrdy, NULL); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_func(common_data, emul_nrdy, NULL); zassert_equal(EC_RES_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); /* Stop fast offset compensation before next test */ bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); @@ -1489,7 +1538,9 @@ ZTEST_USER(bmi160, test_bmi_acc_perform_calib) ZTEST_USER(bmi160, test_bmi_gyr_perform_calib) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); uint8_t pmu_status; intv3_t start_off; intv3_t exp_off; @@ -1534,13 +1585,13 @@ ZTEST_USER(bmi160, test_bmi_gyr_perform_calib) zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on rate set */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_GYR_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); @@ -1548,13 +1599,14 @@ ZTEST_USER(bmi160, test_bmi_gyr_perform_calib) bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); /* Test fail on data not ready */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_read_func(emul, emul_nrdy, NULL); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_func(common_data, emul_nrdy, NULL); zassert_equal(EC_RES_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); /* Stop fast offset compensation before next test */ bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); @@ -1578,7 +1630,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_perform_calib) ZTEST_USER(bmi160, test_bmi_init) { struct motion_sensor_t *ms_acc, *ms_gyr; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); emul = bmi_emul_get(BMI_ORD); ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; @@ -1600,7 +1652,7 @@ struct fifo_func_data { * to value passed as additional data. It sets interrupt registers to 0 after * access. */ -static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int emul_fifo_func(const struct emul *emul, int reg, uint8_t *val, int byte, void *data) { struct fifo_func_data *d = data; @@ -1715,7 +1767,9 @@ ZTEST_USER(bmi160, test_bmi_acc_fifo) struct motion_sensor_t *ms, *ms_gyr; struct fifo_func_data func_data; struct bmi_emul_frame f[3]; - struct i2c_emul *emul; + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int gyr_range = 125; int acc_range = 2; int event; @@ -1739,11 +1793,12 @@ ZTEST_USER(bmi160, test_bmi_acc_fifo) event = BMI_INT_EVENT; /* Test fail to read interrupt status registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_0); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_INT_STATUS_0); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_INT_STATUS_1); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test no interrupt */ bmi_emul_set_reg(emul, BMI160_INT_STATUS_0, 0); @@ -1756,7 +1811,7 @@ ZTEST_USER(bmi160, test_bmi_acc_fifo) check_fifo(ms, ms_gyr, NULL, acc_range, gyr_range); /* Set custom function for FIFO test */ - i2c_common_emul_set_read_func(emul, emul_fifo_func, &func_data); + i2c_common_emul_set_read_func(common_data, emul_fifo_func, &func_data); /* Set range */ zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, acc_range, 0), NULL); zassert_equal(EC_SUCCESS, ms_gyr->drv->set_range(ms_gyr, gyr_range, 0), @@ -1839,7 +1894,7 @@ ZTEST_USER(bmi160, test_bmi_acc_fifo) check_fifo(ms, ms_gyr, f, acc_range, gyr_range); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /** Test irq handler of gyroscope sensor */ @@ -1860,8 +1915,7 @@ ZTEST_USER(bmi160, test_bmi_gyr_fifo) ZTEST_USER(bmi160, test_bmi_sec_raw_read8) { struct motion_sensor_t *ms = &motion_sensors[BMI_ACC_SENSOR_ID]; - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); - + const struct emul *emul = bmi_emul_get(BMI_ORD); uint8_t expected_read_value = 0xAA; uint8_t requested_reg_addr = 0x55; uint8_t actual_reg_addr; @@ -1893,8 +1947,7 @@ ZTEST_USER(bmi160, test_bmi_sec_raw_read8) ZTEST_USER(bmi160, test_bmi_sec_raw_write8) { struct motion_sensor_t *ms = &motion_sensors[BMI_ACC_SENSOR_ID]; - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); - + const struct emul *emul = bmi_emul_get(BMI_ORD); uint8_t expected_write_value = 0xAB; uint8_t requested_reg_addr = 0x56; uint8_t actual_reg_addr; @@ -1927,7 +1980,6 @@ ZTEST_USER(bmi160, test_bmi_set_offset_invalid_type) { struct motion_sensor_t ms_fake; int ret; - int16_t unused_offset; int16_t temp = 0; @@ -1962,7 +2014,7 @@ ZTEST_USER(bmi160, test_bmi_perform_calib_invalid_type) /** Test reading the onboard temperature sensor */ ZTEST_USER(bmi160, test_bmi_temp_sensor) { - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = bmi_emul_get(BMI_ORD); int ret; /* Part 1: @@ -2023,24 +2075,27 @@ ZTEST_USER(bmi160, test_bmi_interrupt_handler) /* Make an I2C emulator mock wrapped in FFF for use with test_bmi_init_chip_id() */ -FAKE_VALUE_FUNC(int, bmi_init_chip_id_mock_write_fn, struct i2c_emul *, int, +FAKE_VALUE_FUNC(int, bmi_init_chip_id_mock_write_fn, const struct emul *, int, uint8_t, int, void *); /** Test handling of invalid or unreadable chip IDs in init() */ ZTEST_USER(bmi160, test_bmi_init_chip_id) { struct motion_sensor_t *ms = &motion_sensors[BMI_ACC_SENSOR_ID]; - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); int ret; /* Part 1: Cannot read the Chip ID register */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_CHIP_ID); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_CHIP_ID); ret = ms->drv->init(ms); zassert_equal(ret, EC_ERROR_UNKNOWN, "Expected %d but got %d", EC_ERROR_UNKNOWN, ret); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Part 2: Incorrect chip ID - this triggers a series of writes in an * attempt to 'unlock' the chip. @@ -2053,8 +2108,8 @@ ZTEST_USER(bmi160, test_bmi_init_chip_id) RESET_FAKE(bmi_init_chip_id_mock_write_fn); bmi_init_chip_id_mock_write_fn_fake.return_val = 1; - i2c_common_emul_set_write_func(emul, bmi_init_chip_id_mock_write_fn, - NULL); + i2c_common_emul_set_write_func(common_data, + bmi_init_chip_id_mock_write_fn, NULL); /* Return a phony chip ID */ bmi_emul_set_reg(emul, BMI160_CHIP_ID, 0xFF); @@ -2077,20 +2132,23 @@ ZTEST_USER(bmi160, test_bmi_init_chip_id) MOCK_ASSERT_I2C_WRITE(bmi_init_chip_id_mock_write_fn, 4, BMI160_CMD_EXT_MODE_ADDR, 0); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } static void bmi160_before(void *fixture) { ARG_UNUSED(fixture); - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); struct motion_sensor_t *acc_ms; struct motion_sensor_t *gyr_ms; acc_ms = &motion_sensors[BMI_ACC_SENSOR_ID]; gyr_ms = &motion_sensors[BMI_GYR_SENSOR_ID]; - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bmi_emul_set_reg(emul, BMI160_CHIP_ID, 0xd1); /* Disable rotation */ diff --git a/zephyr/test/drivers/default/src/bmi260.c b/zephyr/test/drivers/default/src/bmi260.c index a4c713a7fb..059c0fc51a 100644 --- a/zephyr/test/drivers/default/src/bmi260.c +++ b/zephyr/test/drivers/default/src/bmi260.c @@ -51,7 +51,7 @@ static void rotate_int3v_by_test_rotation(intv3_t v) } /** Set emulator accelerometer offset values to intv3_t vector */ -static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) +static void set_emul_acc_offset(const struct emul *emul, intv3_t offset) { bmi_emul_set_off(emul, BMI_EMUL_ACC_X, offset[0]); bmi_emul_set_off(emul, BMI_EMUL_ACC_Y, offset[1]); @@ -59,7 +59,7 @@ static void set_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) } /** Save emulator accelerometer offset values to intv3_t vector */ -static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) +static void get_emul_acc_offset(const struct emul *emul, intv3_t offset) { offset[0] = bmi_emul_get_off(emul, BMI_EMUL_ACC_X); offset[1] = bmi_emul_get_off(emul, BMI_EMUL_ACC_Y); @@ -67,7 +67,7 @@ static void get_emul_acc_offset(struct i2c_emul *emul, intv3_t offset) } /** Set emulator accelerometer values to intv3_t vector */ -static void set_emul_acc(struct i2c_emul *emul, intv3_t acc) +static void set_emul_acc(const struct emul *emul, intv3_t acc) { bmi_emul_set_value(emul, BMI_EMUL_ACC_X, acc[0]); bmi_emul_set_value(emul, BMI_EMUL_ACC_Y, acc[1]); @@ -75,7 +75,7 @@ static void set_emul_acc(struct i2c_emul *emul, intv3_t acc) } /** Set emulator gyroscope offset values to intv3_t vector */ -static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) +static void set_emul_gyr_offset(const struct emul *emul, intv3_t offset) { bmi_emul_set_off(emul, BMI_EMUL_GYR_X, offset[0]); bmi_emul_set_off(emul, BMI_EMUL_GYR_Y, offset[1]); @@ -83,7 +83,7 @@ static void set_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) } /** Save emulator gyroscope offset values to intv3_t vector */ -static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) +static void get_emul_gyr_offset(const struct emul *emul, intv3_t offset) { offset[0] = bmi_emul_get_off(emul, BMI_EMUL_GYR_X); offset[1] = bmi_emul_get_off(emul, BMI_EMUL_GYR_Y); @@ -91,7 +91,7 @@ static void get_emul_gyr_offset(struct i2c_emul *emul, intv3_t offset) } /** Set emulator gyroscope values to vector of three int16_t */ -static void set_emul_gyr(struct i2c_emul *emul, intv3_t gyr) +static void set_emul_gyr(const struct emul *emul, intv3_t gyr) { bmi_emul_set_value(emul, BMI_EMUL_GYR_X, gyr[0]); bmi_emul_set_value(emul, BMI_EMUL_GYR_Y, gyr[1]); @@ -138,8 +138,8 @@ static void compare_int3v_f(intv3_t exp_v, intv3_t v, int eps, int line) * Custom emulator read function which always return INIT OK status in * INTERNAL STATUS register. Used in init test. */ -static int emul_init_ok(struct i2c_emul *emul, int reg, uint8_t *val, int byte, - void *data) +static int emul_init_ok(const struct emul *emul, int reg, uint8_t *val, + int byte, void *data) { bmi_emul_set_reg(emul, BMI260_INTERNAL_STATUS, BMI260_INIT_OK); @@ -151,10 +151,12 @@ static void bmi_init_emul(void) { struct motion_sensor_t *ms_acc; struct motion_sensor_t *ms_gyr; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; int ret; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; @@ -163,7 +165,7 @@ static void bmi_init_emul(void) * BMI260_INTERNAL_STATUS register, because init function triggers reset * which clears value set in this register before test. */ - i2c_common_emul_set_read_func(emul, emul_init_ok, NULL); + i2c_common_emul_set_read_func(common_data, emul_init_ok, NULL); ret = ms_acc->drv->init(ms_acc); zassert_equal(EC_RES_SUCCESS, ret, "Got accel init error %d", ret); @@ -172,20 +174,22 @@ static void bmi_init_emul(void) zassert_equal(EC_RES_SUCCESS, ret, "Got gyro init error %d", ret); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /** Test get accelerometer offset with and without rotation */ ZTEST_USER(bmi260, test_bmi_acc_get_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; int16_t ret[3]; intv3_t ret_v; intv3_t exp_v; int16_t temp; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Set emulator offset */ @@ -199,16 +203,17 @@ ZTEST_USER(bmi260, test_bmi_acc_get_offset) exp_v[2] = -1000 / 30; /* Test fail on offset read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_ACC70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; @@ -234,17 +239,20 @@ ZTEST_USER(bmi260, test_bmi_acc_get_offset) ZTEST_USER(bmi260, test_bmi_gyr_get_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; int16_t ret[3]; intv3_t ret_v; intv3_t exp_v; int16_t temp; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set emulator offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -257,19 +265,20 @@ ZTEST_USER(bmi260, test_bmi_gyr_get_offset) exp_v[2] = -125000 / 300; /* Test fail on offset read */ - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_GYR70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI160_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->get_offset(ms, ret, &temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; @@ -298,7 +307,8 @@ ZTEST_USER(bmi260, test_bmi_gyr_get_offset) ZTEST_USER(bmi260, test_bmi_acc_set_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; int16_t input_v[3]; int16_t temp = 0; intv3_t ret_v; @@ -306,29 +316,35 @@ ZTEST_USER(bmi260, test_bmi_acc_set_offset) uint8_t nv_c; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Test fail on NV CONF register read and write */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_NV_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_NV_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, BMI260_NV_CONF); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_NV_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_write_fail_reg(common_data, BMI160_OFFSET_ACC70); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_ACC70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_write_fail_reg(common_data, + BMI160_OFFSET_ACC70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup NV_CONF register value */ bmi_emul_set_reg(emul, BMI260_NV_CONF, 0x7); @@ -382,36 +398,43 @@ ZTEST_USER(bmi260, test_bmi_acc_set_offset) ZTEST_USER(bmi260, test_bmi_gyr_set_offset) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; int16_t input_v[3]; int16_t temp = 0; intv3_t ret_v; intv3_t exp_v; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Test fail on OFFSET EN GYR98 register read and write */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_OFFSET_GYR70); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 1); + i2c_common_emul_set_write_fail_reg(common_data, + BMI260_OFFSET_GYR70 + 1); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 2); + i2c_common_emul_set_write_fail_reg(common_data, + BMI260_OFFSET_GYR70 + 2); zassert_equal(EC_ERROR_INVAL, ms->drv->set_offset(ms, input_v, temp), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -456,7 +479,7 @@ ZTEST_USER(bmi260, test_bmi_gyr_set_offset) * Try to set accelerometer range and check if expected range was set * in driver and in emulator. */ -static void check_set_acc_range_f(struct i2c_emul *emul, +static void check_set_acc_range_f(const struct emul *emul, struct motion_sensor_t *ms, int range, int rnd, int exp_range, int line) { @@ -502,10 +525,12 @@ static void check_set_acc_range_f(struct i2c_emul *emul, ZTEST_USER(bmi260, test_bmi_acc_set_range) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; int start_range; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Setup starting range, shouldn't be changed on error */ @@ -513,7 +538,7 @@ ZTEST_USER(bmi260, test_bmi_acc_set_range) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI260_ACC_RANGE, BMI260_GSEL_2G); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_ACC_RANGE); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_ACC_RANGE); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 12, 0), NULL); @@ -526,7 +551,8 @@ ZTEST_USER(bmi260, test_bmi_acc_set_range) NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_acc_range(emul, ms, 1, 0, 2); @@ -561,7 +587,7 @@ ZTEST_USER(bmi260, test_bmi_acc_set_range) * Try to set gyroscope range and check if expected range was set in driver and * in emulator. */ -static void check_set_gyr_range_f(struct i2c_emul *emul, +static void check_set_gyr_range_f(const struct emul *emul, struct motion_sensor_t *ms, int range, int rnd, int exp_range, int line) { @@ -610,10 +636,12 @@ static void check_set_gyr_range_f(struct i2c_emul *emul, ZTEST_USER(bmi260, test_bmi_gyr_set_range) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; int start_range; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Setup starting range, shouldn't be changed on error */ @@ -621,7 +649,7 @@ ZTEST_USER(bmi260, test_bmi_gyr_set_range) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI260_GYR_RANGE, BMI260_DPS_SEL_250); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_GYR_RANGE); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_GYR_RANGE); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_range(ms, 125, 0), NULL); @@ -634,7 +662,8 @@ ZTEST_USER(bmi260, test_bmi_gyr_set_range) bmi_emul_get_reg(emul, BMI260_GYR_RANGE), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_gyr_range(emul, ms, 1, 0, 125); @@ -695,7 +724,7 @@ ZTEST_USER(bmi260, test_bmi_get_resolution) * Try to set accelerometer data rate and check if expected rate was set * in driver and in emulator. */ -static void check_set_acc_rate_f(struct i2c_emul *emul, +static void check_set_acc_rate_f(const struct emul *emul, struct motion_sensor_t *ms, int rate, int rnd, int exp_rate, int line) { @@ -755,12 +784,14 @@ static void check_set_acc_rate_f(struct i2c_emul *emul, ZTEST_USER(bmi260, test_bmi_acc_rate) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; uint8_t reg_rate; uint8_t pwr_ctrl; int drv_rate; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Test setting rate with rounding down */ @@ -828,7 +859,7 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) reg_rate = bmi_emul_get_reg(emul, BMI260_ACC_CONF); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_CONF); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -841,10 +872,11 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_ACC_CONF); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_ACC_CONF); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -857,7 +889,8 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ bmi_emul_set_reg(emul, BMI260_PWR_CTRL, @@ -881,25 +914,27 @@ ZTEST_USER(bmi260, test_bmi_acc_rate) zassert_true(reg_rate & BMI260_FILTER_PERF, NULL); /* Test disabling sensor (by setting rate to 0) but failing. */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_PWR_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_PWR_CTRL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 0, 0), "Did not properly handle failed power down."); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test enabling sensor but failing. (after first disabling it) */ ms->drv->set_data_rate(ms, 0, 0); - i2c_common_emul_set_write_fail_reg(emul, BMI260_PWR_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_PWR_CTRL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), "Did not properly handle failed power up."); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); } /** * Try to set gyroscope data rate and check if expected rate was set * in driver and in emulator. */ -static void check_set_gyr_rate_f(struct i2c_emul *emul, +static void check_set_gyr_rate_f(const struct emul *emul, struct motion_sensor_t *ms, int rate, int rnd, int exp_rate, int line) { @@ -959,12 +994,14 @@ static void check_set_gyr_rate_f(struct i2c_emul *emul, ZTEST_USER(bmi260, test_bmi_gyr_rate) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; uint8_t reg_rate; uint8_t pwr_ctrl; int drv_rate; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Test setting rate with rounding down */ @@ -1026,7 +1063,7 @@ ZTEST_USER(bmi260, test_bmi_gyr_rate) reg_rate = bmi_emul_get_reg(emul, BMI260_GYR_CONF); /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_CONF); /* Test fail on read */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -1039,10 +1076,11 @@ ZTEST_USER(bmi260, test_bmi_gyr_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_GYR_CONF); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_GYR_CONF); /* Test fail on write */ zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 50000, 0), @@ -1055,7 +1093,8 @@ ZTEST_USER(bmi260, test_bmi_gyr_rate) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL); /* Do not fail on write */ - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ bmi_emul_set_reg(emul, BMI260_PWR_CTRL, @@ -1120,27 +1159,30 @@ ZTEST_USER(bmi260, test_bmi_scale) ZTEST_USER(bmi260, test_bmi_read_temp) { struct motion_sensor_t *ms_acc, *ms_gyr; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; int ret_temp; int exp_temp; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Setup emulator fail on read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_TEMPERATURE_0); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_TEMPERATURE_0); zassert_equal(EC_ERROR_NOT_POWERED, ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL); zassert_equal(EC_ERROR_NOT_POWERED, ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_TEMPERATURE_1); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_TEMPERATURE_1); zassert_equal(EC_ERROR_NOT_POWERED, ms_acc->drv->read_temp(ms_acc, &ret_temp), NULL); zassert_equal(EC_ERROR_NOT_POWERED, ms_gyr->drv->read_temp(ms_gyr, &ret_temp), NULL); /* Do not fail on read */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Fail on invalid temperature */ bmi_emul_set_reg(emul, BMI260_TEMPERATURE_0, 0x00); @@ -1199,7 +1241,8 @@ ZTEST_USER(bmi260, test_bmi_read_temp) ZTEST_USER(bmi260, test_bmi_acc_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; intv3_t ret_v; intv3_t exp_v; int16_t scale[3] = { MOTION_SENSE_DEFAULT_SCALE, @@ -1207,6 +1250,7 @@ ZTEST_USER(bmi260, test_bmi_acc_read) MOTION_SENSE_DEFAULT_SCALE }; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Set offset 0 to simplify test */ @@ -1215,10 +1259,11 @@ ZTEST_USER(bmi260, test_bmi_acc_read) bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, 0); /* Fail on read status */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1286,20 +1331,21 @@ ZTEST_USER(bmi260, test_bmi_acc_read) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_X_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Y_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_Y_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Y_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_Y_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Z_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_Z_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Z_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_Z_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1307,7 +1353,8 @@ ZTEST_USER(bmi260, test_bmi_acc_read) ZTEST_USER(bmi260, test_bmi_gyr_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; intv3_t ret_v; intv3_t exp_v; int16_t scale[3] = { MOTION_SENSE_DEFAULT_SCALE, @@ -1315,6 +1362,7 @@ ZTEST_USER(bmi260, test_bmi_gyr_read) MOTION_SENSE_DEFAULT_SCALE }; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Set offset 0 to simplify test */ @@ -1323,10 +1371,11 @@ ZTEST_USER(bmi260, test_bmi_gyr_read) bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, 0); /* Fail on read status */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1394,20 +1443,21 @@ ZTEST_USER(bmi260, test_bmi_gyr_read) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_X_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Y_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_Y_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Y_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_Y_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Z_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_Z_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Z_H_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_Z_H_G); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, ret_v), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1415,7 +1465,8 @@ ZTEST_USER(bmi260, test_bmi_gyr_read) ZTEST_USER(bmi260, test_bmi_acc_perform_calib) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; intv3_t start_off; intv3_t exp_off; intv3_t ret_off; @@ -1423,6 +1474,7 @@ ZTEST_USER(bmi260, test_bmi_acc_perform_calib) int rate; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; bmi_init_emul(); @@ -1459,20 +1511,22 @@ ZTEST_USER(bmi260, test_bmi_acc_perform_calib) zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on rate read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on data not ready */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bmi_emul_set_reg(emul, BMI260_STATUS, 0); zassert_equal(EC_ERROR_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); @@ -1482,18 +1536,19 @@ ZTEST_USER(bmi260, test_bmi_acc_perform_calib) bmi_emul_set_reg(emul, BMI260_STATUS, BMI260_DRDY_ACC); /* Test fail on data read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_ACC_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on setting offset */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_NV_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_NV_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful offset compenastion */ zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL); @@ -1511,7 +1566,8 @@ ZTEST_USER(bmi260, test_bmi_acc_perform_calib) ZTEST_USER(bmi260, test_bmi_gyr_perform_calib) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; intv3_t start_off; intv3_t exp_off; intv3_t ret_off; @@ -1519,6 +1575,7 @@ ZTEST_USER(bmi260, test_bmi_gyr_perform_calib) int rate; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_GYR_SENSOR_ID]; bmi_init_emul(); @@ -1552,20 +1609,22 @@ ZTEST_USER(bmi260, test_bmi_gyr_perform_calib) zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on rate read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_CONF); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_CONF); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on data not ready */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); bmi_emul_set_reg(emul, BMI260_STATUS, 0); zassert_equal(EC_ERROR_TIMEOUT, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); @@ -1579,18 +1638,19 @@ ZTEST_USER(bmi260, test_bmi_gyr_perform_calib) BMI260_DRDY_ACC | BMI260_DRDY_GYR); /* Test fail on data read */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_GYR_X_L_G); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on setting offset */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_OFFSET_EN_GYR98); zassert_equal(EC_ERROR_INVAL, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful offset compenastion */ zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL); @@ -1617,9 +1677,11 @@ static const void *init_rom_map_addr_passthru(const void *addr, int size) ZTEST_USER(bmi260, test_bmi_init) { struct motion_sensor_t *ms_acc, *ms_gyr; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; @@ -1640,7 +1702,7 @@ struct fifo_func_data { * to value passed as additional data. It sets interrupt registers to 0 after * access. */ -static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int emul_fifo_func(const struct emul *emul, int reg, uint8_t *val, int byte, void *data) { struct fifo_func_data *d = data; @@ -1755,12 +1817,14 @@ ZTEST_USER(bmi260, test_bmi_acc_fifo) struct motion_sensor_t *ms, *ms_gyr; struct fifo_func_data func_data; struct bmi_emul_frame f[3]; - struct i2c_emul *emul; + const struct emul *emul; + struct i2c_common_emul_data *common_data; int gyr_range = 125; int acc_range = 2; int event; emul = bmi_emul_get(BMI_ORD); + common_data = emul_bmi_get_i2c_common_data(emul); ms = &motion_sensors[BMI_ACC_SENSOR_ID]; ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; @@ -1777,11 +1841,12 @@ ZTEST_USER(bmi260, test_bmi_acc_fifo) event = BMI_INT_EVENT; /* Test fail to read interrupt status registers */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_0); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_INT_STATUS_0); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_1); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_INT_STATUS_1); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test no interrupt */ bmi_emul_set_reg(emul, BMI260_INT_STATUS_0, 0); @@ -1794,7 +1859,7 @@ ZTEST_USER(bmi260, test_bmi_acc_fifo) check_fifo(ms, ms_gyr, NULL, acc_range, gyr_range); /* Set custom function for FIFO test */ - i2c_common_emul_set_read_func(emul, emul_fifo_func, &func_data); + i2c_common_emul_set_read_func(common_data, emul_fifo_func, &func_data); /* Set range */ zassert_equal(EC_SUCCESS, ms->drv->set_range(ms, acc_range, 0), NULL); zassert_equal(EC_SUCCESS, ms_gyr->drv->set_range(ms_gyr, gyr_range, 0), @@ -1877,7 +1942,7 @@ ZTEST_USER(bmi260, test_bmi_acc_fifo) check_fifo(ms, ms_gyr, f, acc_range, gyr_range); /* Remove custom emulator read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /** Test irq handler of gyroscope sensor */ @@ -1958,19 +2023,22 @@ ZTEST_USER(bmi260, test_interrupt_handler) ZTEST_USER(bmi260, test_bmi_init_chip_id) { - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data = + emul_bmi_get_i2c_common_data(emul); struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Part 1: * Error occurs while reading the chip ID */ - i2c_common_emul_set_read_fail_reg(emul, BMI260_CHIP_ID); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_CHIP_ID); int ret = ms_acc->drv->init(ms_acc); zassert_equal(ret, EC_ERROR_UNKNOWN, "Expected %d (EC_ERROR_UNKNOWN) but got %d", EC_ERROR_UNKNOWN, ret); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Part 2: * Test cases where the returned chip ID does not match what is @@ -2012,9 +2080,10 @@ ZTEST_USER(bmi260, test_bmi_init_chip_id) /* 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 *); + const struct emul *, int, uint8_t *, int, void *); +struct i2c_common_emul_data *common_data; 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) + const struct 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 @@ -2034,10 +2103,13 @@ ZTEST_USER(bmi260, test_bmi_config_load_no_mapped_flash) * `bmi_config_load()` returns NULL) */ - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data; struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; int ret, num_status_reg_reads; + common_data = emul_bmi_get_i2c_common_data(emul); + /* Force bmi_config_load() to have to manually copy from memory */ RESET_FAKE(init_rom_map); init_rom_map_fake.return_val = NULL; @@ -2051,7 +2123,8 @@ ZTEST_USER(bmi260, test_bmi_config_load_no_mapped_flash) */ 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); + common_data, 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; @@ -2071,13 +2144,14 @@ ZTEST_USER(bmi260, test_bmi_config_load_no_mapped_flash) num_status_reg_reads, 1); /* Part 2: write to `BMI260_INIT_ADDR_0` fails */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_INIT_ADDR_0); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_INIT_ADDR_0); ret = ms_acc->drv->init(ms_acc); zassert_equal(ret, EC_ERROR_INVALID_CONFIG, "Got %d but expected %d", ret, EC_ERROR_INVALID_CONFIG); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Part 3: init_rom_copy() fails w/ a non-zero return code of 255. */ init_rom_copy_fake.return_val = 255; @@ -2089,16 +2163,17 @@ ZTEST_USER(bmi260, test_bmi_config_load_no_mapped_flash) init_rom_copy_fake.return_val = 0; /* Part 4: write to `BMI260_INIT_DATA` fails */ - i2c_common_emul_set_write_fail_reg(emul, BMI260_INIT_DATA); + i2c_common_emul_set_write_fail_reg(common_data, BMI260_INIT_DATA); ret = ms_acc->drv->init(ms_acc); zassert_equal(ret, EC_ERROR_INVALID_CONFIG, "Got %d but expected %d", ret, EC_ERROR_INVALID_CONFIG); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Cleanup */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } ZTEST_USER(bmi260, test_bmi_config_unsupported_chip) @@ -2113,9 +2188,12 @@ ZTEST_USER(bmi260, test_bmi_config_unsupported_chip) "CONFIG_ACCELGYRO_BMI220 defined." #endif - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data; struct motion_sensor_t ms_fake; + common_data = emul_bmi_get_i2c_common_data(emul); + /* Set up struct and emaulator to be a BMI220 chip, which * `bmi_config_load()` does not support in the current configuration */ @@ -2136,13 +2214,16 @@ ZTEST_USER(bmi260, test_init_config_read_failure) * BMI260_INTERNAL_STATUS. */ - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data; struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; int ret; + common_data = emul_bmi_get_i2c_common_data(emul); + /* 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); + i2c_common_emul_set_read_fail_reg(common_data, BMI260_INTERNAL_STATUS); RESET_FAKE(init_rom_map); init_rom_map_fake.custom_fake = init_rom_map_addr_passthru; @@ -2156,7 +2237,7 @@ ZTEST_USER(bmi260, test_init_config_read_failure) * 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, +static int status_timeout_mock_read_fn(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { if (reg == BMI260_INTERNAL_STATUS && val) { @@ -2177,14 +2258,18 @@ ZTEST_USER(bmi260, test_init_config_status_timeout) * before the timeout. */ - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data; struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; int ret; + common_data = emul_bmi_get_i2c_common_data(emul); + /* 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); + i2c_common_emul_set_read_func(common_data, status_timeout_mock_read_fn, + NULL); RESET_FAKE(init_rom_map); init_rom_map_fake.custom_fake = init_rom_map_addr_passthru; @@ -2206,15 +2291,20 @@ static void bmi260_test_before(void *arg) { ARG_UNUSED(arg); - struct i2c_emul *emul = bmi_emul_get(BMI_ORD); + const struct emul *emul = bmi_emul_get(BMI_ORD); + struct i2c_common_emul_data *common_data; struct motion_sensor_t *ms_acc = &motion_sensors[BMI_ACC_SENSOR_ID]; struct motion_sensor_t *ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; + common_data = emul_bmi_get_i2c_common_data(emul); + /* Reset I2C */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_read_func(emul, NULL, NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_func(common_data, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); /* Reset local fakes(s) */ RESET_FAKE(bmi_config_load_no_mapped_flash_mock_read_fn); diff --git a/zephyr/test/drivers/default/src/console_cmd/accelrange.c b/zephyr/test/drivers/default/src/console_cmd/accelrange.c index b78702e486..5aef4d7937 100644 --- a/zephyr/test/drivers/default/src/console_cmd/accelrange.c +++ b/zephyr/test/drivers/default/src/console_cmd/accelrange.c @@ -16,17 +16,20 @@ #include "motion_sense.h" #include "test/drivers/test_state.h" -#define EMUL_LABEL DT_NODELABEL(bma_emul) +#define EMUL_NODE DT_NODELABEL(bma_emul) #define BMA_ORD DT_DEP_ORD(EMUL_LABEL) static void console_cmd_accelrange_after(void *fixture) { - struct i2c_emul *emul = bma_emul_get(BMA_ORD); + const struct emul *emul = emul_get_binding(DEVICE_DT_NAME(EMUL_NODE)); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); ARG_UNUSED(fixture); shell_execute_cmd(get_ec_shell(), "accelrange 0 2"); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_SUITE(console_cmd_accelrange, drivers_predicate_post_main, NULL, NULL, @@ -101,10 +104,13 @@ ZTEST_USER(console_cmd_accelrange, test_set_range_round_down) ZTEST_USER(console_cmd_accelrange, test_i2c_error) { - struct i2c_emul *emul = bma_emul_get(BMA_ORD); + const struct emul *emul = emul_get_binding(DEVICE_DT_NAME(EMUL_NODE)); + struct i2c_common_emul_data *common_data = + emul_bma_get_i2c_common_data(emul); int rv; - i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, + BMA2x2_RANGE_SELECT_ADDR); rv = shell_execute_cmd(get_ec_shell(), "accelrange 0 3"); zassert_equal(rv, EC_ERROR_PARAM2, "Expected %d, but got %d", diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb.c b/zephyr/test/drivers/default/src/integration/usbc/usb.c index 4b8cd2fe11..672ba6fb39 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb.c @@ -47,7 +47,7 @@ static void integration_usb_before(void *state) /* Reset vbus to 0mV */ /* TODO(b/217610871): Remove redundant test state cleanup */ isl923x_emul_set_adc_vbus(charger_emul, 0); - struct i2c_emul *i2c_emul; + const struct emul *battery_emul = sbat_emul_get_ptr(BATTERY_ORD); struct sbat_emul_bat_data *bat; const struct device *gpio_dev = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_AC_OK_PATH, gpios)); @@ -70,8 +70,8 @@ static void integration_usb_before(void *state) zassert_ok(tcpci_emul_disconnect_partner(tcpci_emul2), NULL); /* Battery defaults to charging, so reset to not charging. */ - i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); - bat = sbat_emul_get_bat_data(i2c_emul); + battery_emul = sbat_emul_get_ptr(BATTERY_ORD); + bat = sbat_emul_get_bat_data(battery_emul); bat->cur = -5; /* diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c b/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c index afd6e5aebc..ba6da0bbf9 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_20v_3a_pd_charger.c @@ -87,10 +87,10 @@ ZTEST_SUITE(usb_attach_20v_3a_pd_charger, drivers_predicate_post_main, ZTEST(usb_attach_20v_3a_pd_charger, test_battery_is_charging) { - struct i2c_emul *i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); uint16_t battery_status; - zassume_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + zassume_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, 0, @@ -155,11 +155,11 @@ ZTEST(usb_attach_20v_3a_pd_charger, test_power_info) ZTEST_F(usb_attach_20v_3a_pd_charger, test_disconnect_battery_not_charging) { - struct i2c_emul *i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); uint16_t battery_status; disconnect_charger_from_port(fixture); - zassert_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + zassert_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, STATUS_DISCHARGING, diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c index 278a6508da..1a00e086c0 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_sink.c @@ -115,11 +115,11 @@ ZTEST_F(usb_attach_5v_3a_pd_sink, test_partner_pd_completed) ZTEST(usb_attach_5v_3a_pd_sink, test_battery_is_discharging) { - struct i2c_emul *i2c_emul = + const struct emul *emul = sbat_emul_get_ptr(DT_DEP_ORD(DT_NODELABEL(battery))); uint16_t battery_status; - zassume_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + zassume_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, STATUS_DISCHARGING, @@ -171,12 +171,12 @@ ZTEST(usb_attach_5v_3a_pd_sink, test_power_info) ZTEST_F(usb_attach_5v_3a_pd_sink, test_disconnect_battery_discharging) { - struct i2c_emul *i2c_emul = + const struct emul *emul = sbat_emul_get_ptr(DT_DEP_ORD(DT_NODELABEL(battery))); uint16_t battery_status; disconnect_sink_from_port(fixture); - zassert_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + zassert_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, STATUS_DISCHARGING, diff --git a/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c index 2139c75af1..d6beedeb1d 100644 --- a/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c +++ b/zephyr/test/drivers/default/src/integration/usbc/usb_5v_3a_pd_source.c @@ -63,10 +63,10 @@ ZTEST_SUITE(usb_attach_5v_3a_pd_source, drivers_predicate_post_main, ZTEST(usb_attach_5v_3a_pd_source, test_battery_is_charging) { - struct i2c_emul *i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); uint16_t battery_status; - zassume_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + zassume_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, 0, @@ -131,11 +131,11 @@ ZTEST(usb_attach_5v_3a_pd_source, test_power_info) ZTEST_F(usb_attach_5v_3a_pd_source, test_disconnect_battery_not_charging) { - struct i2c_emul *i2c_emul = sbat_emul_get_ptr(BATTERY_ORD); + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); uint16_t battery_status; disconnect_source_from_port(fixture->tcpci_emul, fixture->charger_emul); - zassert_ok(sbat_emul_get_word_val(i2c_emul, SB_BATTERY_STATUS, + zassert_ok(sbat_emul_get_word_val(emul, SB_BATTERY_STATUS, &battery_status), NULL); zassert_equal(battery_status & STATUS_DISCHARGING, STATUS_DISCHARGING, diff --git a/zephyr/test/drivers/default/src/isl923x.c b/zephyr/test/drivers/default/src/isl923x.c index 321845ea04..adefc01b97 100644 --- a/zephyr/test/drivers/default/src/isl923x.c +++ b/zephyr/test/drivers/default/src/isl923x.c @@ -46,8 +46,9 @@ BUILD_ASSERT(IS_ENABLED(CONFIG_CHARGER_ISL9238), #define CHARGER_NUM get_charger_num(&isl923x_drv) #define ISL923X_EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(isl923x_emul))) +#define COMMON_DATA emul_isl923x_get_i2c_common_data(ISL923X_EMUL) -static int mock_write_fn_always_fail(struct i2c_emul *emul, int reg, +static int mock_write_fn_always_fail(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { ztest_test_fail(); @@ -56,8 +57,6 @@ static int mock_write_fn_always_fail(struct i2c_emul *emul, int reg, ZTEST(isl923x, test_isl923x_set_current) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int expected_current_milli_amps[] = { EXPECTED_CURRENT_MA(0), EXPECTED_CURRENT_MA(4), EXPECTED_CURRENT_MA(8), EXPECTED_CURRENT_MA(16), @@ -69,13 +68,13 @@ ZTEST(isl923x, test_isl923x_set_current) int current_milli_amps; /* Test I2C failure when reading charge current */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CHG_CURRENT); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CHG_CURRENT); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_current(CHARGER_NUM, ¤t_milli_amps), NULL); /* Reset fail register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); for (int i = 0; i < ARRAY_SIZE(expected_current_milli_amps); ++i) { @@ -129,8 +128,6 @@ ZTEST(isl923x, test_isl923x_set_voltage) ZTEST(isl923x, test_isl923x_set_input_current_limit) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int expected_current_milli_amps[] = { EXPECTED_INPUT_CURRENT_MA(0), EXPECTED_INPUT_CURRENT_MA(4), EXPECTED_INPUT_CURRENT_MA(8), @@ -146,25 +143,25 @@ ZTEST(isl923x, test_isl923x_set_input_current_limit) int current_milli_amps; /* Test failing to write to current limit 1 reg */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_ADAPTER_CURRENT_LIMIT1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_input_current_limit(CHARGER_NUM, 0), NULL); /* Test failing to write to current limit 2 reg */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_ADAPTER_CURRENT_LIMIT2); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_input_current_limit(CHARGER_NUM, 0), NULL); /* Reset fail register */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test failing to read current limit 1 reg */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_ADAPTER_CURRENT_LIMIT1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current_limit(CHARGER_NUM, @@ -172,7 +169,7 @@ ZTEST(isl923x, test_isl923x_set_input_current_limit) NULL); /* Reset fail register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test normal code path */ @@ -200,7 +197,6 @@ ZTEST(isl923x, test_isl923x_psys) ZTEST(isl923x, test_manufacturer_id) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int id; isl923x_emul_set_manufacturer_id(isl923x_emul, 0x1234); @@ -208,20 +204,19 @@ ZTEST(isl923x, test_manufacturer_id) zassert_equal(0x1234, id, NULL); /* Test read error */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_MANUFACTURER_ID); zassert_equal(EC_ERROR_INVAL, isl923x_drv.manufacturer_id(CHARGER_NUM, &id), NULL); /* Reset fail register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST(isl923x, test_device_id) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int id; isl923x_emul_set_device_id(isl923x_emul, 0x5678); @@ -229,47 +224,45 @@ ZTEST(isl923x, test_device_id) zassert_equal(0x5678, id, NULL); /* Test read error */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_DEVICE_ID); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_DEVICE_ID); zassert_equal(EC_ERROR_INVAL, isl923x_drv.device_id(CHARGER_NUM, &id), NULL); /* Reset fail register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST(isl923x, test_options) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint32_t option; /* Test failed control 0 read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_option(CHARGER_NUM, &option), NULL); /* Test failed control 1 read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_option(CHARGER_NUM, &option), NULL); /* Reset failed read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test failed control 0 write */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_option(CHARGER_NUM, option), NULL); /* Test failed control 1 write */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_option(CHARGER_NUM, option), NULL); /* Reset failed write */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test normal write/read, note that bits 23 and 0 are always 0 */ @@ -333,7 +326,6 @@ ZTEST(isl923x, test_set_ac_prochot) { const struct emul *isl923x_emul = ISL923X_EMUL; const struct device *i2c_dev = isl923x_emul_get_parent(isl923x_emul); - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t expected_current_milli_amps[] = { EXPECTED_INPUT_CURRENT_MA(0), EXPECTED_INPUT_CURRENT_MA(128), @@ -352,12 +344,12 @@ ZTEST(isl923x, test_set_ac_prochot) NULL); /* Test failed I2C write to prochot register */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_PROCHOT_AC); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_PROCHOT_AC); zassert_equal(EC_ERROR_INVAL, isl923x_set_ac_prochot(CHARGER_NUM, 0), NULL); /* Clear write fail reg */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); for (int i = 0; i < ARRAY_SIZE(expected_current_milli_amps); ++i) { @@ -377,8 +369,9 @@ ZTEST(isl923x, test_set_ac_prochot) CHARGER_NUM, expected_current_milli_amps[i]), "Failed to set AC prochot to %dmA", expected_current_milli_amps[i]); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ¤t_milli_amps, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), + ¤t_milli_amps, sizeof(current_milli_amps)), "Failed to read AC prochot register"); zassert_equal(EXPECTED_INPUT_CURRENT_REG( @@ -394,7 +387,6 @@ ZTEST(isl923x, test_set_dc_prochot) { const struct emul *isl923x_emul = ISL923X_EMUL; const struct device *i2c_dev = isl923x_emul_get_parent(isl923x_emul); - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t expected_current_milli_amps[] = { EXPECTED_CURRENT_MA(256), EXPECTED_CURRENT_MA(512), EXPECTED_CURRENT_MA(1024), EXPECTED_CURRENT_MA(2048), @@ -409,12 +401,12 @@ ZTEST(isl923x, test_set_dc_prochot) NULL); /* Test failed I2C write to prochot register */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_PROCHOT_DC); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_PROCHOT_DC); zassert_equal(EC_ERROR_INVAL, isl923x_set_dc_prochot(CHARGER_NUM, 0), NULL); /* Clear write fail reg */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); for (int i = 0; i < ARRAY_SIZE(expected_current_milli_amps); ++i) { @@ -433,8 +425,9 @@ ZTEST(isl923x, test_set_dc_prochot) CHARGER_NUM, expected_current_milli_amps[i]), "Failed to set DC prochot to %dmA", expected_current_milli_amps[i]); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ¤t_milli_amps, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), + ¤t_milli_amps, sizeof(current_milli_amps)), "Failed to read DC prochot register"); zassert_equal( @@ -450,44 +443,44 @@ ZTEST(isl923x, test_comparator_inversion) { const struct emul *isl923x_emul = ISL923X_EMUL; const struct device *i2c_dev = isl923x_emul_get_parent(isl923x_emul); - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint8_t reg_addr = ISL923X_REG_CONTROL2; uint16_t reg_value; uint8_t tx_buf[] = { reg_addr, 0, 0 }; /* Test failed read, should not write */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); - i2c_common_emul_set_write_func(i2c_emul, mock_write_fn_always_fail, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL2); + i2c_common_emul_set_write_func(COMMON_DATA, mock_write_fn_always_fail, NULL); zassert_equal(EC_ERROR_INVAL, isl923x_set_comparator_inversion(CHARGER_NUM, false), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_write_func(COMMON_DATA, NULL, NULL); /* Test failed write */ - zassert_ok(i2c_write(i2c_dev, tx_buf, sizeof(tx_buf), i2c_emul->addr), + zassert_ok(i2c_write(i2c_dev, tx_buf, sizeof(tx_buf), + isl923x_emul->bus.i2c->addr), "Failed to clear CTRL2 register"); - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL2); zassert_equal(EC_ERROR_INVAL, isl923x_set_comparator_inversion(CHARGER_NUM, true), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test enable comparator inversion */ zassert_ok(isl923x_set_comparator_inversion(CHARGER_NUM, true), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), "Failed to read CTRL 2 register"); zassert_true((reg_value & ISL923X_C2_INVERT_CMOUT) != 0, NULL); /* Test disable comparator inversion */ zassert_ok(isl923x_set_comparator_inversion(CHARGER_NUM, false), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), "Failed to read CTRL 2 register"); zassert_true((reg_value & ISL923X_C2_INVERT_CMOUT) == 0, NULL); @@ -503,10 +496,10 @@ ZTEST(isl923x, test_discharge_on_ac) uint16_t reg_value; /* Test failure to read CTRL1 register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.discharge_on_ac(CHARGER_NUM, true), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set CTRL1 register to 0 */ @@ -514,22 +507,22 @@ ZTEST(isl923x, test_discharge_on_ac) NULL); /* Test failure to write CTRL1 register */ - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.discharge_on_ac(CHARGER_NUM, true), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), NULL); zassert_equal(0, reg_value, NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test enabling discharge on AC */ zassert_ok(isl923x_drv.discharge_on_ac(CHARGER_NUM, true), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), NULL); zassert_true((reg_value & ISL923X_C1_LEARN_MODE_ENABLE) != 0, NULL); @@ -537,8 +530,8 @@ ZTEST(isl923x, test_discharge_on_ac) /* Test disabling discharge on AC */ zassert_ok(isl923x_drv.discharge_on_ac(CHARGER_NUM, false), NULL); - zassert_ok(i2c_write_read(i2c_dev, i2c_emul->addr, ®_addr, - sizeof(reg_addr), ®_value, + zassert_ok(i2c_write_read(i2c_dev, isl923x_emul->bus.i2c->addr, + ®_addr, sizeof(reg_addr), ®_value, sizeof(reg_value)), NULL); zassert_true((reg_value & ISL923X_C1_LEARN_MODE_ENABLE) == 0, NULL); @@ -547,17 +540,16 @@ ZTEST(isl923x, test_discharge_on_ac) ZTEST(isl923x, test_get_vbus_voltage) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); /* Standard fixed-power PD source voltages. */ int test_voltage_mv[] = { 5000, 9000, 15000, 20000 }; int voltage; /* Test fail to read the ADC vbus register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, RAA489000_REG_ADC_VBUS); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, RAA489000_REG_ADC_VBUS); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_vbus_voltage(CHARGER_NUM, 0, &voltage), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); for (int i = 0; i < ARRAY_SIZE(test_voltage_mv); ++i) { @@ -582,14 +574,13 @@ ZTEST(isl923x, test_get_vbus_voltage) ZTEST(isl923x, test_init) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int input_current; /* Test failed CTRL2 register read (prochot debounce) */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL2); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -599,9 +590,9 @@ ZTEST(isl923x, test_init) /* Test failed CTRL2 register write */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL2); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -611,9 +602,9 @@ ZTEST(isl923x, test_init) /* Test failed CTRL 0 read */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -624,9 +615,9 @@ ZTEST(isl923x, test_init) /* Test failed CTRL 0 write */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -637,9 +628,9 @@ ZTEST(isl923x, test_init) /* Test failed CTRL 3 read */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL9238_REG_CONTROL3); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -649,9 +640,9 @@ ZTEST(isl923x, test_init) /* Test failed CTRL 3 write */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL9238_REG_CONTROL3); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -661,10 +652,10 @@ ZTEST(isl923x, test_init) /* Test failed write adapter current limit */ isl923x_emul_reset_registers(isl923x_emul); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, ISL923X_REG_ADAPTER_CURRENT_LIMIT1); isl923x_drv.init(CHARGER_NUM); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, &input_current), @@ -698,7 +689,6 @@ ZTEST(isl923x, test_init_late_jump) ZTEST(isl923x, test_isl923x_is_acok) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); enum ec_error_list rv; bool acok; @@ -708,13 +698,13 @@ ZTEST(isl923x, test_isl923x_is_acok) "Invalid charger num, but AC OK check succeeded"); /* Part 2: error accessing register */ - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_INFO2); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL9238_REG_INFO2); rv = raa489000_is_acok(CHARGER_NUM, &acok); zassert_equal(EC_ERROR_INVAL, rv, "Register read failure, but AC OK check succeeded"); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); /* Part 3: successful path - ACOK is true */ @@ -735,7 +725,6 @@ ZTEST(isl923x, test_isl923x_is_acok) ZTEST(isl923x, test_isl923x_enable_asgate) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); int rv; /* Part 1: Try enabling the ASGATE */ @@ -744,7 +733,7 @@ ZTEST(isl923x, test_isl923x_enable_asgate) zassert_equal(EC_SUCCESS, rv, "Expected return code of %d but got %d", EC_SUCCESS, rv); zassert_true( - isl923x_emul_peek_reg(i2c_emul, RAA489000_REG_CONTROL8) & + isl923x_emul_peek_reg(isl923x_emul, RAA489000_REG_CONTROL8) & RAA489000_C8_ASGATE_ON_READY, "RAA489000_C8_ASGATE_ON_READY bit not set in Control Reg 8"); @@ -753,15 +742,16 @@ ZTEST(isl923x, test_isl923x_enable_asgate) zassert_equal(EC_SUCCESS, rv, "Expected return code of %d but got %d", EC_SUCCESS, rv); - zassert_false(isl923x_emul_peek_reg(i2c_emul, RAA489000_REG_CONTROL8) & + zassert_false(isl923x_emul_peek_reg(isl923x_emul, + RAA489000_REG_CONTROL8) & RAA489000_C8_ASGATE_ON_READY, "RAA489000_C8_ASGATE_ON_READY bit set in Control Reg 8"); } /* Mock read and write functions to use in the hibernation test */ -FAKE_VALUE_FUNC(int, hibernate_mock_read_fn, struct i2c_emul *, int, uint8_t *, - int, void *); -FAKE_VALUE_FUNC(int, hibernate_mock_write_fn, struct i2c_emul *, int, uint8_t, +FAKE_VALUE_FUNC(int, hibernate_mock_read_fn, const struct emul *, int, + uint8_t *, int, void *); +FAKE_VALUE_FUNC(int, hibernate_mock_write_fn, const struct emul *, int, uint8_t, int, void *); /** @@ -769,8 +759,6 @@ FAKE_VALUE_FUNC(int, hibernate_mock_write_fn, struct i2c_emul *, int, uint8_t, */ static void isl923x_hibernate_before(void *state) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); ARG_UNUSED(state); /* Reset mocks and make the read/write mocks pass all data through */ @@ -779,13 +767,15 @@ static void isl923x_hibernate_before(void *state) hibernate_mock_read_fn_fake.return_val = 1; hibernate_mock_write_fn_fake.return_val = 1; - i2c_common_emul_set_read_func(i2c_emul, hibernate_mock_read_fn, NULL); - i2c_common_emul_set_write_func(i2c_emul, hibernate_mock_write_fn, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, hibernate_mock_read_fn, + NULL); + i2c_common_emul_set_write_func(COMMON_DATA, hibernate_mock_write_fn, + NULL); /* Don't fail on any register access */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); } @@ -794,31 +784,28 @@ static void isl923x_hibernate_before(void *state) */ static void isl923x_hibernate_after(void *state) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); ARG_UNUSED(state); /* Clear the mock read/write functions */ - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, NULL, NULL); + i2c_common_emul_set_write_func(COMMON_DATA, NULL, NULL); /* Don't fail on any register access */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST(isl923x_hibernate, test_isl923x_hibernate__happy_path) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t actual; raa489000_hibernate(CHARGER_NUM, false); /* Check ISL923X_REG_CONTROL0 */ - actual = isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL0); + actual = isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL0); zassert_false(actual & RAA489000_C0_EN_CHG_PUMPS_TO_100PCT, "RAA489000_C0_EN_CHG_PUMPS_TO_100PCT should not be set"); @@ -826,7 +813,7 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__happy_path) "RAA489000_C0_BGATE_FORCE_ON should not be set"); /* Check ISL923X_REG_CONTROL1 */ - actual = isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1); + actual = isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1); zassert_false( actual & RAA489000_C1_ENABLE_SUPP_SUPPORT_MODE, @@ -839,13 +826,13 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__happy_path) "ISL923X_C1_DISABLE_MON should be set"); /* Check ISL9238_REG_CONTROL3 (disable_adc = false) */ - actual = isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3); + actual = isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3); zassert_true(actual & RAA489000_ENABLE_ADC, "RAA489000_ENABLE_ADC should be set"); /* Check ISL9238_REG_CONTROL4 */ - actual = isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL4); + actual = isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL4); zassert_true(actual & RAA489000_C4_DISABLE_GP_CMP, "RAA489000_C4_DISABLE_GP_CMP should be set"); @@ -886,10 +873,7 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__invalid_charger_number) ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL923X_REG_CONTROL0) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); - - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL0); raa489000_hibernate(CHARGER_NUM, false); @@ -904,10 +888,7 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL923X_REG_CONTROL0) ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL923X_REG_CONTROL1) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); - - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL923X_REG_CONTROL1); raa489000_hibernate(CHARGER_NUM, false); @@ -928,10 +909,7 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL923X_REG_CONTROL1) ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL9238_REG_CONTROL3) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); - - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL9238_REG_CONTROL3); raa489000_hibernate(CHARGER_NUM, false); @@ -952,10 +930,7 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL9238_REG_CONTROL3) ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL9238_REG_CONTROL4) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); - - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL4); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, ISL9238_REG_CONTROL4); raa489000_hibernate(CHARGER_NUM, false); @@ -974,13 +949,12 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__fail_at_ISL9238_REG_CONTROL4) ZTEST(isl923x_hibernate, test_isl923x_hibernate__adc_disable) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t expected; raa489000_hibernate(CHARGER_NUM, true); /* Check ISL9238_REG_CONTROL3 (disable_adc = true) */ - expected = isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3); + expected = isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3); expected &= ~RAA489000_ENABLE_ADC; MOCK_ASSERT_I2C_READ(hibernate_mock_read_fn, 4, ISL9238_REG_CONTROL3); @@ -994,40 +968,39 @@ ZTEST(isl923x_hibernate, test_isl923x_hibernate__adc_disable) ZTEST(isl923x_hibernate, test_isl9238c_hibernate) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t control1_expected, control2_expected, control3_expected; int rv; /* Part 1: Happy path */ control1_expected = - (isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1) & + (isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1) & ~ISL923X_C1_ENABLE_PSYS) | ISL923X_C1_DISABLE_MON; control2_expected = - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2) | + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2) | ISL923X_C2_COMPARATOR; control3_expected = - isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3) | + isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3) | ISL9238_C3_BGATE_OFF; rv = isl9238c_hibernate(CHARGER_NUM); zassert_equal(EC_SUCCESS, rv, "Expected return code %d but got %d", EC_SUCCESS, rv); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1), control1_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1), + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1), control1_expected); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2), control2_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2), + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2), control2_expected); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3), control3_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3), + isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3), control3_expected); /* Part 2: Fail reading each register and check for error code */ @@ -1035,7 +1008,7 @@ ZTEST(isl923x_hibernate, test_isl9238c_hibernate) ISL9238_REG_CONTROL3 }; for (int i = 0; i < ARRAY_SIZE(registers); i++) { - i2c_common_emul_set_read_fail_reg(i2c_emul, registers[i]); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, registers[i]); rv = isl9238c_hibernate(CHARGER_NUM); @@ -1048,39 +1021,38 @@ ZTEST(isl923x_hibernate, test_isl9238c_hibernate) ZTEST(isl923x_hibernate, test_isl9238c_resume) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); uint16_t control1_expected, control2_expected, control3_expected; int rv; /* Part 1: Happy path */ control1_expected = - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1) | + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1) | ISL923X_C1_ENABLE_PSYS; control2_expected = - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2) & + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2) & ~ISL923X_C2_COMPARATOR; control3_expected = - isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3) & + isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3) & ~ISL9238_C3_BGATE_OFF; rv = isl9238c_resume(CHARGER_NUM); zassert_equal(EC_SUCCESS, rv, "Expected return code %d but got %d", EC_SUCCESS, rv); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1), control1_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL1), + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL1), control1_expected); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2), control2_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL923X_REG_CONTROL2), + isl923x_emul_peek_reg(isl923x_emul, ISL923X_REG_CONTROL2), control2_expected); - zassert_equal(isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3), + zassert_equal(isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3), control3_expected, "Unexpected register value 0x%02x. Should be 0x%02x", - isl923x_emul_peek_reg(i2c_emul, ISL9238_REG_CONTROL3), + isl923x_emul_peek_reg(isl923x_emul, ISL9238_REG_CONTROL3), control3_expected); /* Part 2: Fail reading each register and check for error code */ @@ -1088,7 +1060,7 @@ ZTEST(isl923x_hibernate, test_isl9238c_resume) ISL9238_REG_CONTROL3 }; for (int i = 0; i < ARRAY_SIZE(registers); i++) { - i2c_common_emul_set_read_fail_reg(i2c_emul, registers[i]); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, registers[i]); rv = isl9238c_resume(CHARGER_NUM); diff --git a/zephyr/test/drivers/default/src/lis2dw12.c b/zephyr/test/drivers/default/src/lis2dw12.c index 5def699088..0996b96181 100644 --- a/zephyr/test/drivers/default/src/lis2dw12.c +++ b/zephyr/test/drivers/default/src/lis2dw12.c @@ -1,4 +1,4 @@ -/* Copyright 2021 The Chromium OS Authors. All rights reserved. +/* Copyright 2022 The ChromiumOS Authors. * Use of this source code is governed by a BSD-style license that can be * found in the LICENSE file. */ @@ -60,11 +60,12 @@ static void lis2dw12_after(void *state) ZTEST(lis2dw12, test_lis2dw12_init__fail_read_who_am_i) { const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), - LIS2DW12_WHO_AM_I_REG); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_WHO_AM_I_REG); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, NULL); } @@ -86,10 +87,12 @@ ZTEST(lis2dw12, test_lis2dw12_init__fail_who_am_i) ZTEST(lis2dw12, test_lis2dw12_init__fail_write_soft_reset) { const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_write_fail_reg(lis2dw12_emul_to_i2c_emul(emul), + i2c_common_emul_set_write_fail_reg(common_data, LIS2DW12_SOFT_RESET_ADDR); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, NULL); @@ -98,19 +101,21 @@ ZTEST(lis2dw12, test_lis2dw12_init__fail_write_soft_reset) ZTEST(lis2dw12, test_lis2dw12_init__timeout_read_soft_reset) { const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_SOFT_RESET_ADDR); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_TIMEOUT, rv, "init returned %d but expected %d", rv, EC_ERROR_TIMEOUT); } -static int lis2dw12_test_mock_write_fail_set_bdu(struct i2c_emul *emul, int reg, - uint8_t val, int bytes, - void *data) +static int lis2dw12_test_mock_write_fail_set_bdu(const struct emul *emul, + int reg, uint8_t val, + int bytes, void *data) { if (reg == LIS2DW12_BDU_ADDR && bytes == 1 && (val & LIS2DW12_BDU_MASK) != 0) { @@ -122,12 +127,13 @@ static int lis2dw12_test_mock_write_fail_set_bdu(struct i2c_emul *emul, int reg, ZTEST(lis2dw12, test_lis2dw12_init__fail_set_bdu) { const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_write_func(lis2dw12_emul_to_i2c_emul(emul), - lis2dw12_test_mock_write_fail_set_bdu, - NULL); + i2c_common_emul_set_write_func( + common_data, lis2dw12_test_mock_write_fail_set_bdu, NULL); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, "init returned %d but expected %d", rv, EC_ERROR_INVAL); @@ -138,11 +144,12 @@ ZTEST(lis2dw12, test_lis2dw12_init__fail_set_bdu) ZTEST(lis2dw12, test_lis2dw12_init__fail_set_lir) { const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), - LIS2DW12_LIR_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_LIR_ADDR); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, "init returned %d but expected %d", @@ -151,7 +158,7 @@ ZTEST(lis2dw12, test_lis2dw12_init__fail_set_lir) "expected at least one soft reset"); } -static int lis2dw12_test_mock_write_fail_set_power_mode(struct i2c_emul *emul, +static int lis2dw12_test_mock_write_fail_set_power_mode(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { @@ -166,12 +173,14 @@ static int lis2dw12_test_mock_write_fail_set_power_mode(struct i2c_emul *emul, ZTEST(lis2dw12, test_lis2dw12_init__fail_set_power_mode) { const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; i2c_common_emul_set_write_func( - lis2dw12_emul_to_i2c_emul(emul), - lis2dw12_test_mock_write_fail_set_power_mode, NULL); + common_data, lis2dw12_test_mock_write_fail_set_power_mode, + NULL); rv = ms->drv->init(ms); zassert_equal(EC_ERROR_INVAL, rv, "init returned %d but expected %d", @@ -201,6 +210,8 @@ ZTEST(lis2dw12, test_lis2dw12_init__success) ZTEST(lis2dw12, test_lis2dw12_set_power_mode) { const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; @@ -216,8 +227,7 @@ ZTEST(lis2dw12, test_lis2dw12_set_power_mode) EC_ERROR_UNIMPLEMENTED, rv); /* Part 3: attempt to set mode but cannot modify reg. */ - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), - LIS2DW12_ACC_MODE_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_ACC_MODE_ADDR); rv = lis2dw12_set_power_mode(ms, LIS2DW12_LOW_POWER, LIS2DW12_LOW_POWER_MODE_2); zassert_equal(rv, EC_ERROR_INVAL, "Expected %d but got %d", @@ -227,6 +237,8 @@ ZTEST(lis2dw12, test_lis2dw12_set_power_mode) ZTEST(lis2dw12, test_lis2dw12_set_range) { const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; int rv; @@ -241,8 +253,7 @@ ZTEST(lis2dw12, test_lis2dw12_set_range) ms->current_range); /* Part 2: Error accessing register */ - i2c_common_emul_set_read_fail_reg(lis2dw12_emul_to_i2c_emul(emul), - LIS2DW12_FS_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_FS_ADDR); rv = ms->drv->set_range(ms, LIS2DW12_ACCEL_FS_MAX_VAL, 0); zassert_equal(rv, EC_ERROR_INVAL, "Expected %d but got %d", EC_ERROR_INVAL, rv); @@ -251,7 +262,6 @@ ZTEST(lis2dw12, test_lis2dw12_set_range) ZTEST(lis2dw12, test_lis2dw12_set_rate) { const struct emul *emul = emul_get_binding(EMUL_LABEL); - struct i2c_emul *i2c_emul = lis2dw12_emul_to_i2c_emul(emul); struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; struct stprivate_data *drv_data = ms->drv_data; int rv; @@ -259,11 +269,9 @@ ZTEST(lis2dw12, test_lis2dw12_set_rate) /* Part 1: Turn off sensor with rate=0 */ rv = ms->drv->set_data_rate(ms, 0, 0); - zassert_equal(lis2dw12_emul_peek_odr(i2c_emul), - LIS2DW12_ODR_POWER_OFF_VAL, + zassert_equal(lis2dw12_emul_peek_odr(emul), LIS2DW12_ODR_POWER_OFF_VAL, "Output data rate should be %d but got %d", - LIS2DW12_ODR_POWER_OFF_VAL, - lis2dw12_emul_peek_odr(i2c_emul)); + LIS2DW12_ODR_POWER_OFF_VAL, lis2dw12_emul_peek_odr(emul)); zassert_equal(drv_data->base.odr, LIS2DW12_ODR_POWER_OFF_VAL, "Output data rate should be %d but got %d", LIS2DW12_ODR_POWER_OFF_VAL, drv_data->base.odr); @@ -319,7 +327,7 @@ ZTEST(lis2dw12, test_lis2dw12_set_rate) test_params[i].expected_norm_rate, drv_data->base.odr); /* Read ODR and mode bits back from CTRL1 register */ - uint8_t odr_bits = lis2dw12_emul_peek_odr(i2c_emul); + uint8_t odr_bits = lis2dw12_emul_peek_odr(emul); zassert_equal( odr_bits, test_params[i].expected_reg_val, @@ -332,8 +340,8 @@ ZTEST(lis2dw12, test_lis2dw12_set_rate) * 200,000mHz */ - uint8_t mode_bits = lis2dw12_emul_peek_mode(i2c_emul); - uint8_t lpmode_bits = lis2dw12_emul_peek_lpmode(i2c_emul); + uint8_t mode_bits = lis2dw12_emul_peek_mode(emul); + uint8_t lpmode_bits = lis2dw12_emul_peek_lpmode(emul); if (odr_bits > LIS2DW12_ODR_200HZ_VAL) { /* High performance mode, LP mode immaterial */ @@ -358,7 +366,9 @@ ZTEST(lis2dw12, test_lis2dw12_set_rate) ZTEST(lis2dw12, test_lis2dw12_read) { const struct emul *emul = emul_get_binding(EMUL_LABEL); - struct i2c_emul *i2c_emul = lis2dw12_emul_to_i2c_emul(emul); + struct i2c_common_emul_data *common_data = + emul_lis2dw12_get_i2c_common_data(emul); + struct motion_sensor_t *ms = &motion_sensors[LIS2DW12_SENSOR_ID]; struct stprivate_data *drvdata = ms->drv_data; intv3_t sample = { 0, 0, 0 }; @@ -376,7 +386,7 @@ ZTEST(lis2dw12, test_lis2dw12_read) * ready bit */ - i2c_common_emul_set_read_fail_reg(i2c_emul, LIS2DW12_STATUS_REG); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_STATUS_REG); rv = ms->drv->read(ms, sample); @@ -388,7 +398,7 @@ ZTEST(lis2dw12, test_lis2dw12_read) * case, the driver should return the reading in from `ms->raw_xyz` */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); lis2dw12_emul_clear_accel_reading(emul); ms->raw_xyz[X] = 123; @@ -406,7 +416,7 @@ ZTEST(lis2dw12, test_lis2dw12_read) */ intv3_t fake_sample = { 100, 200, 300 }; - i2c_common_emul_set_read_fail_reg(i2c_emul, LIS2DW12_OUT_X_L_ADDR); + i2c_common_emul_set_read_fail_reg(common_data, LIS2DW12_OUT_X_L_ADDR); lis2dw12_emul_set_accel_reading(emul, fake_sample); rv = ms->drv->read(ms, sample); @@ -429,7 +439,7 @@ ZTEST(lis2dw12, test_lis2dw12_read) fake_sample[i] * (1 << (16 - LIS2DW12_RESOLUTION)); } - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); lis2dw12_emul_set_accel_reading(emul, fake_sample); diff --git a/zephyr/test/drivers/default/src/ln9310.c b/zephyr/test/drivers/default/src/ln9310.c index e83e2df0a5..634e537e3d 100644 --- a/zephyr/test/drivers/default/src/ln9310.c +++ b/zephyr/test/drivers/default/src/ln9310.c @@ -32,10 +32,10 @@ ZTEST(ln9310, test_ln9310_read_chip_fails) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -43,7 +43,7 @@ ZTEST(ln9310, test_ln9310_read_chip_fails) ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_2S); ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED); - i2c_common_emul_set_read_fail_reg(i2c_emul, LN9310_REG_BC_STS_C); + i2c_common_emul_set_read_fail_reg(common_data, LN9310_REG_BC_STS_C); zassert_true(ln9310_init() != 0, NULL); zassert_false(ln9310_emul_is_init(emulator), NULL); @@ -52,7 +52,7 @@ ZTEST(ln9310, test_ln9310_read_chip_fails) k_msleep(TEST_DELAY_MS); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } @@ -111,7 +111,7 @@ struct startup_workaround_data { bool startup_workaround_should_fail; }; -static int mock_write_fn_intercept_startup_workaround(struct i2c_emul *emul, +static int mock_write_fn_intercept_startup_workaround(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { @@ -136,8 +136,8 @@ ZTEST(ln9310, test_ln9310_2s_cfly_precharge_startup) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - - struct i2c_emul *emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct startup_workaround_data test_data = { .startup_workaround_attempted = false, @@ -160,7 +160,8 @@ ZTEST(ln9310, test_ln9310_2s_cfly_precharge_startup) zassert_false(ln9310_power_good(), NULL); i2c_common_emul_set_write_func( - emul, mock_write_fn_intercept_startup_workaround, &test_data); + common_data, mock_write_fn_intercept_startup_workaround, + &test_data); ln9310_software_enable(true); zassert_true(test_data.startup_workaround_attempted, NULL); @@ -175,14 +176,15 @@ ZTEST(ln9310, test_ln9310_2s_cfly_precharge_startup) k_msleep(TEST_DELAY_MS); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_3s_cfly_precharge_startup) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct startup_workaround_data test_data = { .startup_workaround_attempted = false, @@ -205,7 +207,8 @@ ZTEST(ln9310, test_ln9310_3s_cfly_precharge_startup) zassert_false(ln9310_power_good(), NULL); i2c_common_emul_set_write_func( - emul, mock_write_fn_intercept_startup_workaround, &test_data); + common_data, mock_write_fn_intercept_startup_workaround, + &test_data); ln9310_software_enable(true); zassert_true(test_data.startup_workaround_attempted, NULL); @@ -220,15 +223,15 @@ ZTEST(ln9310, test_ln9310_3s_cfly_precharge_startup) k_msleep(TEST_DELAY_MS); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_cfly_precharge_exceeds_retries) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - - struct i2c_emul *emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct startup_workaround_data test_data = { .startup_workaround_attempted = false, @@ -255,7 +258,8 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_exceeds_retries) zassert_false(ln9310_power_good(), NULL); i2c_common_emul_set_write_func( - emul, mock_write_fn_intercept_startup_workaround, &test_data); + common_data, mock_write_fn_intercept_startup_workaround, + &test_data); ln9310_software_enable(true); zassert_true(test_data.startup_workaround_attempted, NULL); @@ -264,7 +268,7 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_exceeds_retries) k_msleep(TEST_DELAY_MS); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_battery_unknown) @@ -301,10 +305,10 @@ ZTEST(ln9310, test_ln9310_2s_battery_read_fails) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -312,7 +316,7 @@ ZTEST(ln9310, test_ln9310_2s_battery_read_fails) ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_2S); ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED); - i2c_common_emul_set_read_fail_reg(i2c_emul, LN9310_REG_BC_STS_B); + i2c_common_emul_set_read_fail_reg(common_data, LN9310_REG_BC_STS_B); zassert_true(ln9310_init() != 0, NULL); zassert_false(ln9310_emul_is_init(emulator), NULL); @@ -326,10 +330,10 @@ ZTEST(ln9310, test_ln9310_2s_battery_read_fails) ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_2S); ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED); - i2c_common_emul_set_read_fail_reg(i2c_emul, LN9310_REG_TRACK_CTRL); + i2c_common_emul_set_read_fail_reg(common_data, LN9310_REG_TRACK_CTRL); zassert_false(ln9310_init() == 0, NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } @@ -337,10 +341,10 @@ ZTEST(ln9310, test_ln9310_lion_ctrl_reg_fails) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -348,7 +352,7 @@ ZTEST(ln9310, test_ln9310_lion_ctrl_reg_fails) ln9310_emul_set_battery_cell_type(emulator, BATTERY_CELL_TYPE_2S); ln9310_emul_set_version(emulator, LN9310_BC_STS_C_CHIP_REV_FIXED); - i2c_common_emul_set_read_fail_reg(i2c_emul, LN9310_REG_LION_CTRL); + i2c_common_emul_set_read_fail_reg(common_data, LN9310_REG_LION_CTRL); zassert_true(ln9310_init() != 0, NULL); zassert_false(ln9310_emul_is_init(emulator), NULL); @@ -362,7 +366,7 @@ ZTEST(ln9310, test_ln9310_lion_ctrl_reg_fails) ln9310_software_enable(true); zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } @@ -371,7 +375,7 @@ struct precharge_timeout_data { bool handled_clearing_standby_en_bit_timeout; }; -static int mock_intercept_startup_ctrl_reg(struct i2c_emul *emul, int reg, +static int mock_intercept_startup_ctrl_reg(const struct emul *emulator, int reg, uint8_t val, int bytes, void *data) { struct precharge_timeout_data *test_data = data; @@ -398,7 +402,8 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_timesout) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct precharge_timeout_data test_data = { .time_to_mock = { .val = -1, @@ -411,7 +416,6 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_timesout) }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -428,7 +432,7 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_timesout) zassert_false(ln9310_power_good(), NULL); i2c_common_emul_set_write_func( - i2c_emul, mock_intercept_startup_ctrl_reg, &test_data); + common_data, mock_intercept_startup_ctrl_reg, &test_data); ln9310_software_enable(true); /* TODO(b/201420132) */ @@ -437,7 +441,7 @@ ZTEST(ln9310, test_ln9310_cfly_precharge_timesout) /* It only times out on one attempt, it should subsequently startup */ zassert_true(ln9310_power_good(), NULL); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); } struct reg_to_fail_data { @@ -445,7 +449,7 @@ struct reg_to_fail_data { int reg_access_fail_countdown; }; -static int mock_read_intercept_reg_to_fail(struct i2c_emul *emul, int reg, +static int mock_read_intercept_reg_to_fail(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct reg_to_fail_data *test_data = data; @@ -462,14 +466,14 @@ ZTEST(ln9310, test_ln9310_interrupt_reg_fail) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_fail_data test_data = { .reg_access_to_fail = 0, .reg_access_fail_countdown = 0, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -480,8 +484,8 @@ ZTEST(ln9310, test_ln9310_interrupt_reg_fail) zassert_ok(ln9310_init(), NULL); zassert_true(ln9310_emul_is_init(emulator), NULL); - i2c_common_emul_set_read_func(i2c_emul, mock_read_intercept_reg_to_fail, - &test_data); + i2c_common_emul_set_read_func( + common_data, mock_read_intercept_reg_to_fail, &test_data); /* Fail in beginning of software enable */ test_data.reg_access_to_fail = LN9310_REG_INT1; @@ -503,21 +507,21 @@ ZTEST(ln9310, test_ln9310_interrupt_reg_fail) zassert_false(ln9310_power_good(), NULL); zassert_true(test_data.reg_access_fail_countdown <= 0, NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_sys_sts_reg_fail) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_fail_data test_data = { .reg_access_to_fail = 0, .reg_access_fail_countdown = 0, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -529,7 +533,7 @@ ZTEST(ln9310, test_ln9310_sys_sts_reg_fail) zassert_true(ln9310_emul_is_init(emulator), NULL); i2c_common_emul_set_read_func( - i2c_emul, &mock_read_intercept_reg_to_fail, &test_data); + common_data, &mock_read_intercept_reg_to_fail, &test_data); /* Register only read once and in the interrupt handler */ test_data.reg_access_to_fail = LN9310_REG_SYS_STS; @@ -543,7 +547,7 @@ ZTEST(ln9310, test_ln9310_sys_sts_reg_fail) zassert_false(ln9310_power_good(), NULL); zassert_true(test_data.reg_access_fail_countdown <= 0, NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } struct reg_to_intercept { @@ -551,7 +555,7 @@ struct reg_to_intercept { uint8_t replace_val; }; -static int mock_read_interceptor(struct i2c_emul *emul, int reg, uint8_t *val, +static int mock_read_interceptor(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct reg_to_intercept *test_data = data; @@ -568,14 +572,14 @@ ZTEST(ln9310, test_ln9310_reset_explicit_detected_startup) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_intercept test_data = { .reg = LN9310_REG_LION_CTRL, .replace_val = 0, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -586,7 +590,7 @@ ZTEST(ln9310, test_ln9310_reset_explicit_detected_startup) zassert_ok(ln9310_init(), NULL); zassert_true(ln9310_emul_is_init(emulator), NULL); - i2c_common_emul_set_read_func(i2c_emul, &mock_read_interceptor, + i2c_common_emul_set_read_func(common_data, &mock_read_interceptor, &test_data); ln9310_software_enable(true); @@ -596,21 +600,21 @@ ZTEST(ln9310, test_ln9310_reset_explicit_detected_startup) zassert_true(ln9310_power_good(), NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_update_startup_seq_fails) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_fail_data test_data = { .reg_access_to_fail = LN9310_REG_CFG_4, .reg_access_fail_countdown = 1, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -621,7 +625,7 @@ ZTEST(ln9310, test_ln9310_update_startup_seq_fails) REQUIRES_CFLY_PRECHARGE_STARTUP_CHIP_REV); i2c_common_emul_set_read_func( - i2c_emul, &mock_read_intercept_reg_to_fail, &test_data); + common_data, &mock_read_intercept_reg_to_fail, &test_data); zassert_false(ln9310_init() == 0, NULL); zassert_false(ln9310_emul_is_init(emulator), NULL); @@ -634,21 +638,21 @@ ZTEST(ln9310, test_ln9310_update_startup_seq_fails) zassert_false(ln9310_power_good(), NULL); zassert_true(test_data.reg_access_fail_countdown <= 0, NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } ZTEST(ln9310, test_ln9310_state_change_only_on_mode_change_interrupt) { const struct emul *emulator = emul_get_binding(DT_LABEL(DT_NODELABEL(ln9310))); - struct i2c_emul *i2c_emul = ln9310_emul_get_i2c_emul(emulator); + struct i2c_common_emul_data *common_data = + emul_ln9310_get_i2c_common_data(emulator); struct reg_to_intercept test_data = { .reg = LN9310_REG_INT1, .replace_val = 0, }; zassert_not_null(emulator, NULL); - zassert_not_null(i2c_emul, NULL); ln9310_emul_set_context(emulator); ln9310_emul_reset(emulator); @@ -659,7 +663,7 @@ ZTEST(ln9310, test_ln9310_state_change_only_on_mode_change_interrupt) zassert_ok(ln9310_init(), NULL); zassert_true(ln9310_emul_is_init(emulator), NULL); - i2c_common_emul_set_read_func(i2c_emul, &mock_read_interceptor, + i2c_common_emul_set_read_func(common_data, &mock_read_interceptor, &test_data); ln9310_software_enable(true); @@ -669,7 +673,7 @@ ZTEST(ln9310, test_ln9310_state_change_only_on_mode_change_interrupt) zassert_false(ln9310_power_good(), NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } static inline void reset_ln9310_state(void) diff --git a/zephyr/test/drivers/default/src/power_common.c b/zephyr/test/drivers/default/src/power_common.c index 2d61ecf333..869aef8898 100644 --- a/zephyr/test/drivers/default/src/power_common.c +++ b/zephyr/test/drivers/default/src/power_common.c @@ -296,7 +296,9 @@ ZTEST(power_common, test_power_hc_smart_discharge) struct ec_params_smart_discharge params; struct host_cmd_handler_args args = BUILD_HOST_COMMAND(EC_CMD_SMART_DISCHARGE, 0, response, params); - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int hours_to_zero; int hibern_drate; int cutoff_drate; @@ -309,9 +311,10 @@ ZTEST(power_common, test_power_hc_smart_discharge) params.flags = EC_SMART_DISCHARGE_FLAGS_SET; /* Test fail when battery capacity is not available */ - i2c_common_emul_set_read_fail_reg(emul, SB_FULL_CHARGE_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_FULL_CHARGE_CAPACITY); zassert_equal(EC_RES_UNAVAILABLE, host_command_process(&args), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup discharge rates */ params.drate.hibern = 10; @@ -391,7 +394,9 @@ ZTEST(power_common, test_power_board_system_is_idle) struct host_cmd_handler_args args = BUILD_HOST_COMMAND(EC_CMD_SMART_DISCHARGE, 0, response, params); struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); uint64_t last_shutdown_time = 0; uint64_t target; uint64_t now; @@ -421,11 +426,12 @@ ZTEST(power_common, test_power_board_system_is_idle) * Test hibernation is requested when battery remaining capacity * is not available */ - i2c_common_emul_set_read_fail_reg(emul, SB_REMAINING_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_REMAINING_CAPACITY); zassert_equal(CRITICAL_SHUTDOWN_HIBERNATE, board_system_is_idle(last_shutdown_time, &target, now), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup remaining capacity to trigger cutoff */ bat->cap = response.dzone.cutoff - 5; @@ -458,7 +464,7 @@ static void setup_hibernation_delay(void *state) struct host_cmd_handler_args args = BUILD_HOST_COMMAND(EC_CMD_SMART_DISCHARGE, 0, response, params); struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); ARG_UNUSED(state); emul = sbat_emul_get_ptr(BATTERY_ORD); diff --git a/zephyr/test/drivers/default/src/ppc_sn5s330.c b/zephyr/test/drivers/default/src/ppc_sn5s330.c index bc81ed3c5b..ba41c5c07a 100644 --- a/zephyr/test/drivers/default/src/ppc_sn5s330.c +++ b/zephyr/test/drivers/default/src/ppc_sn5s330.c @@ -21,6 +21,7 @@ /** This must match the index of the sn5s330 in ppc_chips[] */ #define SN5S330_PORT 0 #define EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(sn5s330_emul))) +#define COMMON_DATA emul_sn5s330_get_i2c_common_data(EMUL) #define FUNC_SET1_ILIMPP1_MSK 0x1F #define SN5S330_INTERRUPT_DELAYMS 15 @@ -42,7 +43,7 @@ struct intercept_read_data { uint8_t replacement_val; }; -static int intercept_read_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int intercept_read_func(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct intercept_read_data *test_data = data; @@ -53,7 +54,7 @@ static int intercept_read_func(struct i2c_emul *emul, int reg, uint8_t *val, return EC_SUCCESS; } -static int intercept_write_func(struct i2c_emul *emul, int reg, uint8_t val, +static int intercept_write_func(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { struct intercept_write_data *test_data = data; @@ -64,7 +65,7 @@ static int intercept_write_func(struct i2c_emul *emul, int reg, uint8_t val, return 1; } -static int fail_until_write_func(struct i2c_emul *emul, int reg, uint8_t val, +static int fail_until_write_func(const struct emul *emul, int reg, uint8_t val, int bytes, void *data) { uint32_t *count = data; @@ -79,23 +80,21 @@ static int fail_until_write_func(struct i2c_emul *emul, int reg, uint8_t val, ZTEST(ppc_sn5s330, test_fail_once_func_set1) { const struct emul *emul = EMUL; - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(emul); uint32_t count = 1; uint8_t func_set1_value; - i2c_common_emul_set_write_func(i2c_emul, fail_until_write_func, &count); + i2c_common_emul_set_write_func(COMMON_DATA, fail_until_write_func, + &count); zassert_ok(sn5s330_drv.init(SN5S330_PORT), NULL); zassert_equal(count, 0, NULL); sn5s330_emul_peek_reg(emul, SN5S330_FUNC_SET1, &func_set1_value); zassert_true((func_set1_value & SN5S330_ILIM_1_62) != 0, NULL); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_write_func(COMMON_DATA, NULL, NULL); } ZTEST(ppc_sn5s330, test_dead_battery_boot_force_pp2_fets_set) { - const struct emul *emul = EMUL; - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(emul); struct intercept_write_data test_write_data = { .reg_to_intercept = SN5S330_FUNC_SET3, .val_intercepted = 0, @@ -106,9 +105,9 @@ ZTEST(ppc_sn5s330, test_dead_battery_boot_force_pp2_fets_set) .replacement_val = SN5S330_DB_BOOT, }; - i2c_common_emul_set_write_func(i2c_emul, intercept_write_func, + i2c_common_emul_set_write_func(COMMON_DATA, intercept_write_func, &test_write_data); - i2c_common_emul_set_read_func(i2c_emul, intercept_read_func, + i2c_common_emul_set_read_func(COMMON_DATA, intercept_read_func, &test_read_data); zassert_ok(sn5s330_drv.init(SN5S330_PORT), NULL); @@ -384,18 +383,17 @@ ZTEST(ppc_sn5s330, test_sn5s330_set_vconn_fet) } /* Make an I2C emulator mock read func wrapped in FFF */ -FAKE_VALUE_FUNC(int, dump_read_fn, struct i2c_emul *, int, uint8_t *, int, +FAKE_VALUE_FUNC(int, dump_read_fn, const struct emul *, int, uint8_t *, int, void *); ZTEST(ppc_sn5s330, test_dump) { int ret; - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); /* Set up our fake read function to pass through to the real emul */ RESET_FAKE(dump_read_fn); dump_read_fn_fake.return_val = 1; - i2c_common_emul_set_read_func(i2c_emul, dump_read_fn, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, dump_read_fn, NULL); ret = sn5s330_drv.reg_dump(SN5S330_PORT); @@ -437,16 +435,18 @@ enum i2c_operation { I2C_READ, }; -#define INIT_I2C_FAIL_HELPER(EMUL, RW, REG) \ +#define INIT_I2C_FAIL_HELPER(COMMON_DATA, RW, REG) \ do { \ if ((RW) == I2C_READ) { \ - i2c_common_emul_set_read_fail_reg((EMUL), (REG)); \ + i2c_common_emul_set_read_fail_reg((COMMON_DATA), \ + (REG)); \ i2c_common_emul_set_write_fail_reg( \ - (EMUL), I2C_COMMON_EMUL_NO_FAIL_REG); \ + (COMMON_DATA), I2C_COMMON_EMUL_NO_FAIL_REG); \ } else if ((RW) == I2C_WRITE) { \ i2c_common_emul_set_read_fail_reg( \ - (EMUL), I2C_COMMON_EMUL_NO_FAIL_REG); \ - i2c_common_emul_set_write_fail_reg((EMUL), (REG)); \ + (COMMON_DATA), I2C_COMMON_EMUL_NO_FAIL_REG); \ + i2c_common_emul_set_write_fail_reg((COMMON_DATA), \ + (REG)); \ } else { \ zassert_true(false, "Invalid I2C operation"); \ } \ @@ -459,51 +459,61 @@ enum i2c_operation { ZTEST(ppc_sn5s330, test_init_reg_fails) { - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); - /* Fail on each of the I2C operations the init function does to ensure * we get the correct return value. This includes operations made by * clr_flags() and set_flags(). */ - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET5); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET6); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET6); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET9); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET11); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET8); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET8); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET4); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET4); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET3); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET3); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_FUNC_SET10); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_FUNC_SET10); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_STATUS_REG4); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_RISE_REG1); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_FALL_REG1); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_RISE_REG2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_FALL_REG2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_RISE_REG3); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_MASK_FALL_REG3); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_READ, SN5S330_INT_STATUS_REG4); - - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_RISE_REG1); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_RISE_REG2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_RISE_REG3); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_FALL_REG1); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_FALL_REG2); - INIT_I2C_FAIL_HELPER(i2c_emul, I2C_WRITE, SN5S330_INT_TRIP_FALL_REG3); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET5); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET6); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET6); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET9); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET11); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET8); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET8); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET4); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET4); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET3); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET3); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_FUNC_SET10); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_FUNC_SET10); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, SN5S330_INT_STATUS_REG4); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_RISE_REG1); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_FALL_REG1); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_RISE_REG2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_FALL_REG2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_RISE_REG3); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_MASK_FALL_REG3); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_READ, SN5S330_INT_STATUS_REG4); + + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_RISE_REG1); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_RISE_REG2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_RISE_REG3); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_FALL_REG1); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_FALL_REG2); + INIT_I2C_FAIL_HELPER(COMMON_DATA, I2C_WRITE, + SN5S330_INT_TRIP_FALL_REG3); } -static int pp_fet_test_mock_read_fn(struct i2c_emul *emul, int reg, +static int pp_fet_test_mock_read_fn(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { int *counter = data; @@ -535,11 +545,10 @@ ZTEST(ppc_sn5s330, test_pp_fet_enable_fail) * battery mode, which we take care of in the mock read function. */ - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int counter; int ret; - i2c_common_emul_set_read_func(i2c_emul, pp_fet_test_mock_read_fn, + i2c_common_emul_set_read_func(COMMON_DATA, pp_fet_test_mock_read_fn, &counter); /* Allow only the first access to the reg to succeed. This tests the @@ -592,10 +601,9 @@ ZTEST(ppc_sn5s330, test_set_polarity) ZTEST(ppc_sn5s330, test_set_vbus_source_current_limit_fail) { - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int ret; - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET1); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET1); ret = sn5s330_drv.set_vbus_source_current_limit(SN5S330_PORT, TYPEC_RP_3A0); @@ -605,10 +613,9 @@ ZTEST(ppc_sn5s330, test_set_vbus_source_current_limit_fail) ZTEST(ppc_sn5s330, test_sn5s330_discharge_vbus_fail) { - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int ret; - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET3); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET3); ret = sn5s330_drv.discharge_vbus(SN5S330_PORT, false); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", @@ -619,25 +626,24 @@ ZTEST(ppc_sn5s330, test_low_power_mode_fail) { /* Test failed I2C operations in the enter low power mode function */ - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int ret; - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET3); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET3); ret = sn5s330_drv.enter_low_power_mode(SN5S330_PORT); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET4); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET4); ret = sn5s330_drv.enter_low_power_mode(SN5S330_PORT); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET2); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET2); ret = sn5s330_drv.enter_low_power_mode(SN5S330_PORT); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET9); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET9); ret = sn5s330_drv.enter_low_power_mode(SN5S330_PORT); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); @@ -647,10 +653,9 @@ ZTEST(ppc_sn5s330, test_sn5s330_set_vconn_fail) { /* Test failed I2C operations in the set Vconn function */ - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); int ret; - i2c_common_emul_set_read_fail_reg(i2c_emul, SN5S330_FUNC_SET4); + i2c_common_emul_set_read_fail_reg(COMMON_DATA, SN5S330_FUNC_SET4); ret = sn5s330_drv.set_vconn(SN5S330_PORT, 0); zassert_equal(EC_ERROR_INVAL, ret, "Expected EC_ERROR_INVAL but got %d", ret); @@ -658,13 +663,11 @@ ZTEST(ppc_sn5s330, test_sn5s330_set_vconn_fail) static inline void reset_sn5s330_state(void) { - struct i2c_emul *i2c_emul = sn5s330_emul_to_i2c_emul(EMUL); - - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_func(COMMON_DATA, NULL, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, NULL, NULL); + i2c_common_emul_set_write_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(COMMON_DATA, I2C_COMMON_EMUL_NO_FAIL_REG); sn5s330_emul_reset(EMUL); RESET_FAKE(sn5s330_emul_interrupt_set_stub); diff --git a/zephyr/test/drivers/default/src/ppc_syv682x.c b/zephyr/test/drivers/default/src/ppc_syv682x.c index 86f97bbb21..6f67d245fc 100644 --- a/zephyr/test/drivers/default/src/ppc_syv682x.c +++ b/zephyr/test/drivers/default/src/ppc_syv682x.c @@ -25,7 +25,8 @@ #define GPIO_USB_C1_FRS_EN_PATH DT_PATH(named_gpios, usb_c1_frs_en) struct ppc_syv682x_fixture { - struct i2c_emul *ppc_emul; + const struct emul *ppc_emul; + struct i2c_common_emul_data *common_data; const struct device *frs_en_gpio_port; int frs_en_gpio_pin; }; @@ -43,6 +44,8 @@ static void *syv682x_test_setup(void) static struct ppc_syv682x_fixture fixture; fixture.ppc_emul = syv682x_emul_get(SYV682X_ORD); + fixture.common_data = + emul_syv682x_get_i2c_common_data(fixture.ppc_emul); zassume_not_null(fixture.ppc_emul, NULL); fixture.frs_en_gpio_port = DEVICE_DT_GET(DT_GPIO_CTLR(GPIO_USB_C1_FRS_EN_PATH, gpios)); @@ -55,7 +58,8 @@ static void *syv682x_test_setup(void) static void syv682x_test_after(void *data) { struct ppc_syv682x_fixture *fixture = data; - struct i2c_emul *emul = fixture->ppc_emul; + const struct emul *emul = fixture->ppc_emul; + struct i2c_common_emul_data *common_data = fixture->common_data; /* Disable the power path and clear interrupt conditions. */ zassume_ok(syv682x_emul_set_reg(emul, SYV682X_CONTROL_1_REG, @@ -65,12 +69,14 @@ static void syv682x_test_after(void *data) SYV682X_CONTROL_4_NONE); /* Clear the mock read/write functions */ - i2c_common_emul_set_read_func(emul, NULL, NULL); - i2c_common_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); + i2c_common_emul_set_write_func(common_data, NULL, NULL); /* Don't fail on any register access */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_SUITE(ppc_syv682x, drivers_predicate_post_main, syv682x_test_setup, NULL, @@ -658,7 +664,7 @@ ZTEST(ppc_syv682x, test_syv682x_ppc_dump) * reg_access_to_fail on read number N, where N is the initial value of * reg_access_fail_countdown. */ -static int mock_read_intercept_reg_fail(struct i2c_emul *emul, int reg, +static int mock_read_intercept_reg_fail(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct reg_to_fail_data *test_data = data; @@ -674,11 +680,11 @@ static int mock_read_intercept_reg_fail(struct i2c_emul *emul, int reg, ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_status) { /* Failed STATUS read should cause init to fail. */ - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, SYV682X_STATUS_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "STATUS read error, but init succeeded"); - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } @@ -691,7 +697,7 @@ ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_1) }; /* Failed CONTROL_1 read */ - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, SYV682X_CONTROL_1_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_1 read error, but init succeeded"); @@ -709,23 +715,23 @@ ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_1) "succeeded"); zassert_ok(drv->reg_dump(syv682x_port), "CONTROL_1 read error, and ppc_dump failed"); - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Init reads CONTROL_1 several times. The 3rd read happens while * setting the source current limit. Check that init fails when that * read fails. */ - i2c_common_emul_set_read_func(fixture->ppc_emul, + i2c_common_emul_set_read_func(fixture->common_data, &mock_read_intercept_reg_fail, ®_fail); reg_fail.reg_access_to_fail = SYV682X_CONTROL_1_REG; reg_fail.reg_access_fail_countdown = 3; zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_1 read error, but init succeeded"); - i2c_common_emul_set_read_func(fixture->ppc_emul, NULL, NULL); + i2c_common_emul_set_read_func(fixture->common_data, NULL, NULL); /* Failed CONTROL_1 write */ - i2c_common_emul_set_write_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, SYV682X_CONTROL_1_REG); /* During init, the driver will write CONTROL_1 either to disable all @@ -745,65 +751,65 @@ ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_1) EC_SUCCESS, "CONTROL_1 write error, but VBUS source " "enable succeeded"); - i2c_common_emul_set_write_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_2) { /* Failed CONTROL_2 read */ - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, SYV682X_CONTROL_2_REG); zassert_not_equal(ppc_discharge_vbus(syv682x_port, true), EC_SUCCESS, "CONTROL_2 read error, but VBUS discharge succeeded"); - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Failed CONTROL_2 write */ - i2c_common_emul_set_write_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, SYV682X_CONTROL_2_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_2 write error, but init succeeded"); - i2c_common_emul_set_write_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_3) { /* Failed CONTROL_3 read */ - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, SYV682X_CONTROL_3_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_3 read error, but VBUS discharge succeeded"); - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Failed CONTROL_3 write */ - i2c_common_emul_set_write_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, SYV682X_CONTROL_3_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_3 write error, but init succeeded"); - i2c_common_emul_set_write_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } ZTEST_F(ppc_syv682x, test_syv682x_i2c_error_control_4) { /* Failed CONTROL_4 read */ - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, SYV682X_CONTROL_4_REG); zassert_not_equal(ppc_set_vconn(syv682x_port, true), EC_SUCCESS, "CONTROL_2 read error, but VCONN set succeeded"); - i2c_common_emul_set_read_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_read_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Failed CONTROL_4 write */ - i2c_common_emul_set_write_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, SYV682X_CONTROL_4_REG); zassert_not_equal(ppc_init(syv682x_port), EC_SUCCESS, "CONTROL_4 write error, but init succeeded"); zassert_not_equal(ppc_set_vconn(syv682x_port, true), EC_SUCCESS, "CONTROL_4 write error, but VCONN set succeeded"); - i2c_common_emul_set_write_fail_reg(fixture->ppc_emul, + i2c_common_emul_set_write_fail_reg(fixture->common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } diff --git a/zephyr/test/drivers/default/src/ps8xxx.c b/zephyr/test/drivers/default/src/ps8xxx.c index 7a37539c8e..5e15ed728d 100644 --- a/zephyr/test/drivers/default/src/ps8xxx.c +++ b/zephyr/test/drivers/default/src/ps8xxx.c @@ -26,13 +26,14 @@ static void test_ps8xxx_init_fail(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *ps8xxx_i2c_emul = tcpci_emul_get_i2c_emul(ps8xxx_emul); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); /* Test fail on FW reg read */ - i2c_common_emul_set_read_fail_reg(ps8xxx_i2c_emul, PS8XXX_REG_FW_REV); + i2c_common_emul_set_read_fail_reg(common_data, PS8XXX_REG_FW_REV); zassert_equal(EC_ERROR_TIMEOUT, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); - i2c_common_emul_set_read_fail_reg(ps8xxx_i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on FW reg set to 0 */ @@ -67,9 +68,11 @@ ZTEST(ps8815, test_init_fail) ZTEST(ps8805, test_ps8805_init) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *p1_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_1); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(ps8xxx_emul); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + struct i2c_common_emul_data *p1_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_1); /* Set arbitrary FW reg value != 0 for this test */ tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x31); @@ -77,17 +80,17 @@ ZTEST(ps8805, test_ps8805_init) tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_POWER_STATUS, 0x0); /* Test fail on read I2C debug reg */ - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, PS8XXX_REG_I2C_DEBUGGING_ENABLE); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on read DCI reg */ - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, PS8XXX_P1_REG_MUX_USB_DCI_CFG); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful init */ @@ -104,8 +107,9 @@ ZTEST(ps8805, test_ps8805_init) ZTEST(ps8815, test_ps8815_init) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *p1_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_1); + struct i2c_common_emul_data *p1_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_1); /* Set arbitrary FW reg value != 0 for this test */ tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, 0x31); @@ -113,10 +117,10 @@ ZTEST(ps8815, test_ps8815_init) tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_POWER_STATUS, 0x0); /* Test fail on reading HW revision register */ - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, PS8815_P1_REG_HW_REVISION); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.init(USBC_PORT_C1), NULL); - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful init */ @@ -127,7 +131,9 @@ ZTEST(ps8815, test_ps8815_init) static void test_ps8xxx_release(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(ps8xxx_emul); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + uint64_t start_ms; /* Test successful release with correct FW reg read */ @@ -137,7 +143,7 @@ static void test_ps8xxx_release(void) "release on correct FW reg read shouldn't wait for chip"); /* Test delay on FW reg read fail */ - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, PS8XXX_REG_FW_REV); + i2c_common_emul_set_read_fail_reg(common_data, PS8XXX_REG_FW_REV); start_ms = k_uptime_get(); zassert_equal(EC_SUCCESS, ps8xxx_tcpm_drv.release(USBC_PORT_C1), NULL); zassert_true(k_uptime_get() - start_ms >= 10, @@ -318,7 +324,9 @@ ZTEST(ps8815, test_set_vconn) static void test_ps8xxx_transmit(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(ps8xxx_emul); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + struct tcpci_emul_msg *msg; uint64_t exp_cnt, cnt; uint16_t reg_val; @@ -326,12 +334,12 @@ static void test_ps8xxx_transmit(void) msg = tcpci_emul_get_tx_msg(ps8xxx_emul); /* Test fail on transmitting BIST MODE 2 message */ - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, TCPC_REG_TRANSMIT); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TRANSMIT); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.transmit( USBC_PORT_C1, TCPCI_MSG_TX_BIST_MODE_2, 0, NULL), NULL); - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test sending BIST MODE 2 message */ @@ -378,28 +386,30 @@ ZTEST(ps8815, test_transmit) static void test_ps88x5_drp_toggle(bool delay_expected) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(ps8xxx_emul); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + uint16_t exp_role_ctrl; int64_t start_time; int64_t delay; /* Test fail on command write */ - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, TCPC_REG_COMMAND); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_COMMAND); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.drp_toggle(USBC_PORT_C1), NULL); /* Test fail on role control write */ - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, TCPC_REG_ROLE_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ROLE_CTRL); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.drp_toggle(USBC_PORT_C1), NULL); - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on CC status read */ - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, TCPC_REG_CC_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_CC_STATUS); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.drp_toggle(USBC_PORT_C1), NULL); - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set CC status as snk, CC lines set arbitrary */ @@ -481,7 +491,9 @@ ZTEST(ps8805, test_drp_toggle) static void test_ps8xxx_get_chip_info(uint16_t current_product_id) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(ps8xxx_emul); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + struct ec_response_pd_chip_info_v1 info; uint16_t vendor, product, device_id, fw_rev; @@ -499,11 +511,11 @@ static void test_ps8xxx_get_chip_info(uint16_t current_product_id) tcpci_emul_set_reg(ps8xxx_emul, PS8XXX_REG_FW_REV, fw_rev); /* Test fail on reading FW revision */ - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, PS8XXX_REG_FW_REV); + i2c_common_emul_set_read_fail_reg(common_data, PS8XXX_REG_FW_REV); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, &info), NULL); - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test reading chip info */ @@ -608,8 +620,9 @@ ZTEST(ps8815, test_ps8815_get_chip_info) ZTEST(ps8805, test_ps8805_get_chip_info_fix_dev_id) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *p0_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_0); + struct i2c_common_emul_data *p0_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_0); struct ec_response_pd_chip_info_v1 info; uint16_t vendor, product, device_id, fw_rev; uint16_t chip_rev; @@ -649,12 +662,12 @@ ZTEST(ps8805, test_ps8805_get_chip_info_fix_dev_id) tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_BCD_DEV, device_id); /* Test error on fixing device id because of fail chip revision read */ - i2c_common_emul_set_read_fail_reg(p0_i2c_emul, + i2c_common_emul_set_read_fail_reg(p0_i2c_common_data, PS8805_P0_REG_CHIP_REVISION); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, &info), NULL); - i2c_common_emul_set_read_fail_reg(p0_i2c_emul, + i2c_common_emul_set_read_fail_reg(p0_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set wrong chip revision */ @@ -701,8 +714,9 @@ ZTEST(ps8805, test_ps8805_get_chip_info_fix_dev_id) ZTEST(ps8815, test_ps8815_get_chip_info_fix_dev_id) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *p1_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_1); + struct i2c_common_emul_data *p1_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_1); struct ec_response_pd_chip_info_v1 info; uint16_t vendor, product, device_id, fw_rev; uint16_t hw_rev; @@ -742,12 +756,12 @@ ZTEST(ps8815, test_ps8815_get_chip_info_fix_dev_id) tcpci_emul_set_reg(ps8xxx_emul, TCPC_REG_BCD_DEV, device_id); /* Test error on fixing device id because of fail hw revision read */ - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, PS8815_P1_REG_HW_REVISION); zassert_equal(EC_ERROR_INVAL, ps8xxx_tcpm_drv.get_chip_info(USBC_PORT_C1, 1, &info), NULL); - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set wrong hw revision */ @@ -794,8 +808,9 @@ ZTEST(ps8815, test_ps8815_get_chip_info_fix_dev_id) ZTEST(ps8805, test_ps8805_gpio) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *gpio_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_GPIO); + struct i2c_common_emul_data *gpio_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_GPIO); uint8_t exp_ctrl, gpio_ctrl; int level; @@ -867,7 +882,7 @@ ZTEST(ps8805, test_ps8805_gpio) NULL); /* Setup fail on gpio control reg read */ - i2c_common_emul_set_read_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_read_fail_reg(gpio_i2c_common_data, PS8805_REG_GPIO_CONTROL); /* Test fail on reading gpio control reg */ @@ -880,16 +895,16 @@ ZTEST(ps8805, test_ps8805_gpio) NULL); /* Do not fail on gpio control reg read */ - i2c_common_emul_set_read_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_read_fail_reg(gpio_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on writing gpio control reg */ - i2c_common_emul_set_write_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_write_fail_reg(gpio_i2c_common_data, PS8805_REG_GPIO_CONTROL); zassert_equal(EC_ERROR_INVAL, ps8805_gpio_set_level(USBC_PORT_C1, PS8805_GPIO_0, 1), NULL); - i2c_common_emul_set_write_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_write_fail_reg(gpio_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Clear gpio control reg */ @@ -934,8 +949,10 @@ ZTEST(ps8805, test_ps8805_gpio) static void test_ps8xxx_tcpci_init(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_init(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_init(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_init) @@ -952,8 +969,10 @@ ZTEST(ps8815, test_tcpci_init) static void test_ps8xxx_tcpci_release(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_release(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_release(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_release) @@ -970,8 +989,10 @@ ZTEST(ps8815, test_tcpci_release) static void test_ps8xxx_tcpci_get_cc(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_get_cc(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_get_cc(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_get_cc) @@ -988,8 +1009,10 @@ ZTEST(ps8815, test_tcpci_get_cc) static void test_ps8xxx_tcpci_set_cc(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_cc(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_set_cc(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_cc) @@ -1006,8 +1029,10 @@ ZTEST(ps8815, test_tcpci_set_cc) static void test_ps8xxx_tcpci_set_polarity(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_polarity(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_set_polarity(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_polarity) @@ -1024,8 +1049,10 @@ ZTEST(ps8815, test_tcpci_set_polarity) static void test_ps8xxx_tcpci_set_vconn(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_vconn(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_set_vconn(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_vconn) @@ -1042,8 +1069,10 @@ ZTEST(ps8815, test_tcpci_set_vconn) static void test_ps8xxx_tcpci_set_msg_header(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_msg_header(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_set_msg_header(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_msg_header) @@ -1060,8 +1089,10 @@ ZTEST(ps8815, test_tcpci_set_msg_header) static void test_ps8xxx_tcpci_get_rx_message_raw(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_get_rx_message_raw(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_get_rx_message_raw(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_get_rx_message_raw) @@ -1078,8 +1109,10 @@ ZTEST(ps8815, test_tcpci_get_rx_message_raw) static void test_ps8xxx_tcpci_transmit(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_transmit(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_transmit(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_transmit) @@ -1096,8 +1129,10 @@ ZTEST(ps8815, test_tcpci_transmit) static void test_ps8xxx_tcpci_alert(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_alert(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_alert(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_alert) @@ -1114,8 +1149,10 @@ ZTEST(ps8815, test_tcpci_alert) static void test_ps8xxx_tcpci_alert_rx_message(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_alert_rx_message(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_alert_rx_message(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_alert_rx_message) @@ -1132,6 +1169,8 @@ ZTEST(ps8815, test_tcpci_alert_rx_message) static void test_ps8xxx_tcpci_low_power_mode(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); /* * PS8751/PS8815 has the auto sleep function that enters * low power mode on its own in ~2 seconds. Other chips @@ -1140,7 +1179,7 @@ static void test_ps8xxx_tcpci_low_power_mode(void) if (board_get_ps8xxx_product_id(USBC_PORT_C1) == PS8751_PRODUCT_ID || board_get_ps8xxx_product_id(USBC_PORT_C1) == PS8815_PRODUCT_ID) return; - test_tcpci_low_power_mode(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_low_power_mode(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_low_power_mode) @@ -1157,8 +1196,10 @@ ZTEST(ps8815, test_tcpci_low_power_mode) static void test_ps8xxx_tcpci_set_bist_mode(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); - test_tcpci_set_bist_mode(ps8xxx_emul, USBC_PORT_C1); + test_tcpci_set_bist_mode(ps8xxx_emul, common_data, USBC_PORT_C1); } ZTEST(ps8805, test_tcpci_set_bist_mode) @@ -1175,37 +1216,42 @@ ZTEST(ps8815, test_tcpci_set_bist_mode) static void setup_no_fail_all(void) { const struct emul *ps8xxx_emul = emul_get_binding(PS8XXX_EMUL_LABEL); - struct i2c_emul *tcpci_i2c_emul = tcpci_emul_get_i2c_emul(ps8xxx_emul); - struct i2c_emul *p0_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_0); - struct i2c_emul *p1_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_1); - struct i2c_emul *gpio_i2c_emul = - ps8xxx_emul_get_i2c_emul(ps8xxx_emul, PS8XXX_EMUL_PORT_GPIO); - - i2c_common_emul_set_read_fail_reg(tcpci_i2c_emul, + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(ps8xxx_emul); + + struct i2c_common_emul_data *p0_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_0); + struct i2c_common_emul_data *p1_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_1); + struct i2c_common_emul_data *gpio_i2c_common_data = + ps8xxx_emul_get_i2c_common_data(ps8xxx_emul, + PS8XXX_EMUL_PORT_GPIO); + + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(tcpci_i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - if (p0_i2c_emul != NULL) { - i2c_common_emul_set_read_fail_reg(p0_i2c_emul, + if (p0_i2c_common_data != NULL) { + i2c_common_emul_set_read_fail_reg(p0_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(p0_i2c_emul, + i2c_common_emul_set_write_fail_reg(p0_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } - if (p1_i2c_emul != NULL) { - i2c_common_emul_set_read_fail_reg(p1_i2c_emul, + if (p1_i2c_common_data != NULL) { + i2c_common_emul_set_read_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(p1_i2c_emul, + i2c_common_emul_set_write_fail_reg(p1_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } - if (gpio_i2c_emul != NULL) { - i2c_common_emul_set_read_fail_reg(gpio_i2c_emul, + if (gpio_i2c_common_data != NULL) { + i2c_common_emul_set_read_fail_reg(gpio_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(gpio_i2c_emul, + i2c_common_emul_set_write_fail_reg(gpio_i2c_common_data, I2C_COMMON_EMUL_NO_FAIL_REG); } } diff --git a/zephyr/test/drivers/default/src/smart.c b/zephyr/test/drivers/default/src/smart.c index 6de42efe9c..7756621a9f 100644 --- a/zephyr/test/drivers/default/src/smart.c +++ b/zephyr/test/drivers/default/src/smart.c @@ -24,12 +24,11 @@ ZTEST_USER(smart_battery, test_battery_getters) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); char block[32]; int expected; int word; - emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); zassert_equal(EC_SUCCESS, battery_get_mode(&word), NULL); @@ -81,19 +80,22 @@ ZTEST_USER(smart_battery, test_battery_getters) ZTEST_USER(smart_battery, test_battery_get_capacity) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int word; emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Test fail when checking battery mode */ - i2c_common_emul_set_read_fail_reg(emul, SB_BATTERY_MODE); + i2c_common_emul_set_read_fail_reg(common_data, SB_BATTERY_MODE); zassert_equal(EC_ERROR_INVAL, battery_remaining_capacity(&word), NULL); zassert_equal(EC_ERROR_INVAL, battery_full_charge_capacity(&word), NULL); zassert_equal(EC_ERROR_INVAL, battery_design_capacity(&word), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test getting remaining capacity and if mAh mode is forced */ bat->mode |= MODE_CAPACITY; @@ -118,7 +120,7 @@ ZTEST_USER(smart_battery, test_battery_get_capacity) ZTEST_USER(smart_battery, test_battery_status) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); int expected; int status; @@ -143,16 +145,20 @@ ZTEST_USER(smart_battery, test_battery_status) /** Test wait for stable function */ ZTEST_USER(smart_battery, test_battery_wait_for_stable) { - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); emul = sbat_emul_get_ptr(BATTERY_ORD); /* Should fail when read function always fail */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(EC_ERROR_NOT_POWERED, battery_wait_for_stable(), NULL); /* Should be ok with default handler */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(EC_SUCCESS, battery_wait_for_stable(), NULL); } @@ -160,7 +166,7 @@ ZTEST_USER(smart_battery, test_battery_wait_for_stable) ZTEST_USER(smart_battery, test_battery_manufacture_date) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); int day, month, year; int exp_month = 5; int exp_year = 2018; @@ -184,7 +190,9 @@ ZTEST_USER(smart_battery, test_battery_manufacture_date) ZTEST_USER(smart_battery, test_battery_time_at_rate) { struct sbat_emul_bat_data *bat; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int expect_time; int minutes; int rate; @@ -202,16 +210,18 @@ ZTEST_USER(smart_battery, test_battery_time_at_rate) rate = -6000; /* Test fail on writing at rate register */ - i2c_common_emul_set_write_fail_reg(emul, SB_AT_RATE); + i2c_common_emul_set_write_fail_reg(common_data, SB_AT_RATE); zassert_equal(EC_ERROR_INVAL, battery_time_at_rate(rate, &minutes), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on reading at rate ok register */ - i2c_common_emul_set_read_fail_reg(emul, SB_AT_RATE_OK); + i2c_common_emul_set_read_fail_reg(common_data, SB_AT_RATE_OK); zassert_equal(EC_ERROR_INVAL, battery_time_at_rate(rate, &minutes), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* * Expected discharging rate is less then 10s, @@ -243,87 +253,92 @@ ZTEST_USER(smart_battery, test_battery_get_params) { struct sbat_emul_bat_data *bat; struct batt_params batt; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int flags; emul = sbat_emul_get_ptr(BATTERY_ORD); bat = sbat_emul_get_bat_data(emul); /* Fail temperature read */ - i2c_common_emul_set_read_fail_reg(emul, SB_TEMPERATURE); + i2c_common_emul_set_read_fail_reg(common_data, SB_TEMPERATURE); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_TEMPERATURE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail state of charge read; want charge cannot be set */ - i2c_common_emul_set_read_fail_reg(emul, SB_RELATIVE_STATE_OF_CHARGE); + i2c_common_emul_set_read_fail_reg(common_data, + SB_RELATIVE_STATE_OF_CHARGE); flags = BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_STATE_OF_CHARGE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail voltage read */ - i2c_common_emul_set_read_fail_reg(emul, SB_VOLTAGE); + i2c_common_emul_set_read_fail_reg(common_data, SB_VOLTAGE); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_VOLTAGE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail current read */ - i2c_common_emul_set_read_fail_reg(emul, SB_CURRENT); + i2c_common_emul_set_read_fail_reg(common_data, SB_CURRENT); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_CURRENT; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail average current read */ - i2c_common_emul_set_read_fail_reg(emul, SB_AVERAGE_CURRENT); + i2c_common_emul_set_read_fail_reg(common_data, SB_AVERAGE_CURRENT); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_AVERAGE_CURRENT; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail charging voltage read; want charge cannot be set */ - i2c_common_emul_set_read_fail_reg(emul, SB_CHARGING_VOLTAGE); + i2c_common_emul_set_read_fail_reg(common_data, SB_CHARGING_VOLTAGE); flags = BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_DESIRED_VOLTAGE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail charging voltage read; want charge cannot be set */ - i2c_common_emul_set_read_fail_reg(emul, SB_CHARGING_CURRENT); + i2c_common_emul_set_read_fail_reg(common_data, SB_CHARGING_CURRENT); flags = BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_DESIRED_CURRENT; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail remaining capacity read */ - i2c_common_emul_set_read_fail_reg(emul, SB_REMAINING_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_REMAINING_CAPACITY); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_REMAINING_CAPACITY; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail full capacity read */ - i2c_common_emul_set_read_fail_reg(emul, SB_FULL_CHARGE_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_FULL_CHARGE_CAPACITY); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_FULL_CAPACITY; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail status read */ - i2c_common_emul_set_read_fail_reg(emul, SB_BATTERY_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, SB_BATTERY_STATUS); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_STATUS; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Fail all */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_FAIL_ALL_REG); flags = BATT_FLAG_BAD_ANY; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); /* Use default handler, everything should be ok */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE; battery_get_params(&batt); zassert_equal(flags, batt.flags, "0x%x != 0x%x", flags, batt.flags); @@ -335,7 +350,7 @@ struct mfgacc_data { int len; }; -static int mfgacc_read_func(struct i2c_emul *emul, int reg, uint8_t *val, +static int mfgacc_read_func(const struct emul *emul, int reg, uint8_t *val, int bytes, void *data) { struct mfgacc_data *conf = data; @@ -352,7 +367,9 @@ ZTEST_USER(smart_battery, test_battery_mfacc) { struct sbat_emul_bat_data *bat; struct mfgacc_data mfacc_conf; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); uint8_t recv_buf[10]; uint8_t mf_data[10]; uint16_t cmd; @@ -375,12 +392,13 @@ ZTEST_USER(smart_battery, test_battery_mfacc) len = 10; /* Test fail on writing SB_MANUFACTURER_ACCESS register */ - i2c_common_emul_set_write_fail_reg(emul, SB_MANUFACTURER_ACCESS); + i2c_common_emul_set_write_fail_reg(common_data, SB_MANUFACTURER_ACCESS); zassert_equal(EC_ERROR_INVAL, sb_read_mfgacc(cmd, SB_ALT_MANUFACTURER_ACCESS, recv_buf, len), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on reading manufacturer data (custom handler is not set) */ zassert_equal(EC_ERROR_INVAL, @@ -399,7 +417,8 @@ ZTEST_USER(smart_battery, test_battery_mfacc) mfacc_conf.reg = SB_ALT_MANUFACTURER_ACCESS; mfacc_conf.len = len; mfacc_conf.buf = mf_data; - i2c_common_emul_set_read_func(emul, mfgacc_read_func, &mfacc_conf); + i2c_common_emul_set_read_func(common_data, mfgacc_read_func, + &mfacc_conf); /* Test error when mf_data doesn't start with command */ zassert_equal(EC_ERROR_UNKNOWN, @@ -420,7 +439,7 @@ ZTEST_USER(smart_battery, test_battery_mfacc) zassert_mem_equal(mf_data + 1, recv_buf, len - 1, NULL); /* Disable custom read function */ - i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(common_data, NULL, NULL); } /** Test battery fake charge level set and read */ @@ -428,7 +447,9 @@ ZTEST_USER(smart_battery, test_battery_fake_charge) { struct sbat_emul_bat_data *bat; struct batt_params batt; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); + struct i2c_common_emul_data *common_data = + emul_smart_battery_get_i2c_common_data(emul); int remaining_cap; int fake_charge; int charge; @@ -469,7 +490,7 @@ ZTEST_USER(smart_battery, test_battery_fake_charge) remaining_cap, batt.remaining_capacity); /* Test fake remaining capacity when full capacity is not available */ - i2c_common_emul_set_read_fail_reg(emul, SB_FULL_CHARGE_CAPACITY); + i2c_common_emul_set_read_fail_reg(common_data, SB_FULL_CHARGE_CAPACITY); flags = BATT_FLAG_WANT_CHARGE | BATT_FLAG_RESPONSIVE | BATT_FLAG_BAD_FULL_CAPACITY; battery_get_params(&batt); @@ -479,7 +500,8 @@ ZTEST_USER(smart_battery, test_battery_fake_charge) remaining_cap = bat->design_cap * fake_charge / 100; zassert_equal(remaining_cap, batt.remaining_capacity, "%d != %d", remaining_cap, batt.remaining_capacity); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable fake charge level */ zassert_equal(EC_SUCCESS, @@ -501,7 +523,7 @@ ZTEST_USER(smart_battery, test_battery_fake_temperature) { struct sbat_emul_bat_data *bat; struct batt_params batt; - struct i2c_emul *emul; + const struct emul *emul = sbat_emul_get_ptr(BATTERY_ORD); int fake_temp; int flags; diff --git a/zephyr/test/drivers/default/src/stm_mems_common.c b/zephyr/test/drivers/default/src/stm_mems_common.c index 26c00feaef..ab24a0eefc 100644 --- a/zephyr/test/drivers/default/src/stm_mems_common.c +++ b/zephyr/test/drivers/default/src/stm_mems_common.c @@ -16,14 +16,15 @@ #include "test/drivers/test_state.h" #define MOCK_EMUL emul_get_binding(DT_LABEL(DT_NODELABEL(i2c_mock))) +#define COMMON_DATA emul_i2c_mock_get_i2c_common_data(MOCK_EMUL) struct mock_properties { /* Incremented by the mock function every time it is called */ int call_count; }; -static int mock_read_fn(struct i2c_emul *emul, int reg, uint8_t *val, int bytes, - void *data) +static int mock_read_fn(const struct emul *emul, int reg, uint8_t *val, + int bytes, void *data) { ztest_check_expected_value(reg); ztest_check_expected_value(bytes); @@ -34,8 +35,8 @@ static int mock_read_fn(struct i2c_emul *emul, int reg, uint8_t *val, int bytes, return ztest_get_return_value(); } -static int mock_write_fn(struct i2c_emul *emul, int reg, uint8_t val, int bytes, - void *data) +static int mock_write_fn(const struct emul *emul, int reg, uint8_t val, + int bytes, void *data) { struct mock_properties *props = (struct mock_properties *)data; @@ -51,10 +52,10 @@ static int mock_write_fn(struct i2c_emul *emul, int reg, uint8_t val, int bytes, ZTEST(stm_mems_common, test_st_raw_read_n) { 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); + i2c_common_emul_set_read_func(COMMON_DATA, mock_read_fn, NULL); /* * Ensure the MSb (auto-increment bit) in the register address gets * set, but also return an error condition @@ -72,10 +73,10 @@ ZTEST(stm_mems_common, test_st_raw_read_n) ZTEST(stm_mems_common, test_st_raw_read_n_noinc) { 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); + i2c_common_emul_set_read_func(COMMON_DATA, mock_read_fn, NULL); /* * Unlike `st_raw_read_n`, the MSb (auto-increment bit) in the register * address should NOT be automatically set. Also return an error. @@ -94,7 +95,7 @@ ZTEST(stm_mems_common, test_st_raw_read_n_noinc) ZTEST(stm_mems_common, test_st_write_data_with_mask) { const struct emul *emul = MOCK_EMUL; - struct i2c_emul *i2c_emul = i2c_mock_to_i2c_emul(emul); + int rv; const struct motion_sensor_t sensor = { @@ -111,7 +112,7 @@ ZTEST(stm_mems_common, test_st_write_data_with_mask) (test_data & test_mask); /* Part 1: error occurs when reading initial value from sensor */ - i2c_common_emul_set_read_func(i2c_emul, mock_read_fn, NULL); + i2c_common_emul_set_read_func(COMMON_DATA, mock_read_fn, NULL); ztest_expect_value(mock_read_fn, reg, test_addr); ztest_expect_value(mock_read_fn, bytes, 0); /* Value is immaterial but ztest has no way to explicitly ignore it */ @@ -136,7 +137,7 @@ ZTEST(stm_mems_common, test_st_write_data_with_mask) .call_count = 0, }; - i2c_common_emul_set_write_func(i2c_emul, mock_write_fn, + i2c_common_emul_set_write_func(COMMON_DATA, mock_write_fn, &write_fn_props); rv = st_write_data_with_mask(&sensor, test_addr, test_mask, test_data); diff --git a/zephyr/test/drivers/default/src/tcpci.c b/zephyr/test/drivers/default/src/tcpci.c index 18a3341c27..cdaac61039 100644 --- a/zephyr/test/drivers/default/src/tcpci.c +++ b/zephyr/test/drivers/default/src/tcpci.c @@ -20,172 +20,210 @@ #include "tcpm/tcpci.h" #include "test/drivers/test_state.h" -#define EMUL_LABEL DT_NODELABEL(tcpci_emul) +#define EMUL_LABEL "TCPCI_EMUL" /** Test TCPCI init and vbus level */ ZTEST(tcpci, test_generic_tcpci_init) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_init(emul, USBC_PORT_C0); + test_tcpci_init(emul, common_data, USBC_PORT_C0); } /** Test TCPCI release */ ZTEST(tcpci, test_generic_tcpci_release) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_release(emul, USBC_PORT_C0); + test_tcpci_release(emul, common_data, USBC_PORT_C0); } /** Test TCPCI get cc */ ZTEST(tcpci, test_generic_tcpci_get_cc) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_get_cc(emul, USBC_PORT_C0); + test_tcpci_get_cc(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set cc */ ZTEST(tcpci, test_generic_tcpci_set_cc) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_cc(emul, USBC_PORT_C0); + test_tcpci_set_cc(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set polarity */ ZTEST(tcpci, test_generic_tcpci_set_polarity) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_polarity(emul, USBC_PORT_C0); + test_tcpci_set_polarity(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set vconn */ ZTEST(tcpci, test_generic_tcpci_set_vconn) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_vconn(emul, USBC_PORT_C0); + test_tcpci_set_vconn(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set msg header */ ZTEST(tcpci, test_generic_tcpci_set_msg_header) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_msg_header(emul, USBC_PORT_C0); + test_tcpci_set_msg_header(emul, common_data, USBC_PORT_C0); } /** Test TCPCI rx and sop prime enable */ ZTEST(tcpci, test_generic_tcpci_set_rx_detect) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_rx_detect(emul, USBC_PORT_C0); + test_tcpci_set_rx_detect(emul, common_data, USBC_PORT_C0); } /** Test TCPCI get raw message from TCPC revision 2.0 */ ZTEST(tcpci, test_generic_tcpci_get_rx_message_raw_rev2) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); /* Revision 2.0 is set by default in test_rules */ - test_tcpci_get_rx_message_raw(emul, USBC_PORT_C0); + test_tcpci_get_rx_message_raw(emul, common_data, USBC_PORT_C0); } /** Test TCPCI get raw message from TCPC revision 1.0 */ ZTEST(tcpci, test_generic_tcpci_get_rx_message_raw_rev1) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); tcpc_config[USBC_PORT_C0].flags = 0; tcpci_emul_set_rev(emul, TCPCI_EMUL_REV1_0_VER1_0); - test_tcpci_get_rx_message_raw(emul, USBC_PORT_C0); + test_tcpci_get_rx_message_raw(emul, common_data, USBC_PORT_C0); } /** Test TCPCI transmitting message from TCPC revision 2.0 */ ZTEST(tcpci, test_generic_tcpci_transmit_rev2) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); /* Revision 2.0 is set by default in test_rules */ - test_tcpci_transmit(emul, USBC_PORT_C0); + test_tcpci_transmit(emul, common_data, USBC_PORT_C0); } /** Test TCPCI transmitting message from TCPC revision 1.0 */ ZTEST(tcpci, test_generic_tcpci_transmit_rev1) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); tcpc_config[USBC_PORT_C0].flags = 0; tcpci_emul_set_rev(emul, TCPCI_EMUL_REV1_0_VER1_0); - test_tcpci_transmit(emul, USBC_PORT_C0); + test_tcpci_transmit(emul, common_data, USBC_PORT_C0); } /** Test TCPCI alert */ ZTEST(tcpci, test_generic_tcpci_alert) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_alert(emul, USBC_PORT_C0); + test_tcpci_alert(emul, common_data, USBC_PORT_C0); } /** Test TCPCI alert RX message */ ZTEST(tcpci, test_generic_tcpci_alert_rx_message) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_alert_rx_message(emul, USBC_PORT_C0); + test_tcpci_alert_rx_message(emul, common_data, USBC_PORT_C0); } /** Test TCPCI auto discharge on disconnect */ ZTEST(tcpci, test_generic_tcpci_auto_discharge) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_auto_discharge(emul, USBC_PORT_C0); + test_tcpci_auto_discharge(emul, common_data, USBC_PORT_C0); } /** Test TCPCI drp toggle */ ZTEST(tcpci, test_generic_tcpci_drp_toggle) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_drp_toggle(emul, USBC_PORT_C0); + test_tcpci_drp_toggle(emul, common_data, USBC_PORT_C0); } /** Test TCPCI get chip info */ ZTEST(tcpci, test_generic_tcpci_get_chip_info) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_get_chip_info(emul, USBC_PORT_C0); + test_tcpci_get_chip_info(emul, common_data, USBC_PORT_C0); } /** Test TCPCI enter low power mode */ ZTEST(tcpci, test_generic_tcpci_low_power_mode) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_low_power_mode(emul, USBC_PORT_C0); + test_tcpci_low_power_mode(emul, common_data, USBC_PORT_C0); } /** Test TCPCI set bist test mode */ ZTEST(tcpci, test_generic_tcpci_set_bist_mode) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); - test_tcpci_set_bist_mode(emul, USBC_PORT_C0); + test_tcpci_set_bist_mode(emul, common_data, USBC_PORT_C0); } /** Test TCPCI discharge vbus */ ZTEST(tcpci, test_generic_tcpci_discharge_vbus) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); uint8_t exp_ctrl, initial_ctrl; /* Set initial value for POWER ctrl register. Chosen arbitrary. */ @@ -207,7 +245,7 @@ ZTEST(tcpci, test_generic_tcpci_discharge_vbus) /** Test TCPC xfer */ ZTEST(tcpci, test_tcpc_xfer) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); uint16_t val, exp_val; uint8_t reg; @@ -226,7 +264,7 @@ ZTEST(tcpci, test_tcpc_xfer) /** Test TCPCI debug accessory enable/disable */ ZTEST(tcpci, test_generic_tcpci_debug_accessory) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); + const struct emul *emul = emul_get_binding(EMUL_LABEL); uint8_t exp_val, initial_val; /* Set initial value for STD output register. Chosen arbitrary. */ @@ -262,15 +300,16 @@ static void set_usb_mux_tcpc(void) /** Test TCPCI mux init */ ZTEST(tcpci, test_generic_tcpci_mux_init) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); struct usb_mux *tcpci_usb_mux = &usb_muxes[USBC_PORT_C0]; /* Set as usb mux with TCPC for first init call */ set_usb_mux_tcpc(); /* Make sure that TCPC is not accessed */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(EC_SUCCESS, tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); @@ -278,9 +317,9 @@ ZTEST(tcpci, test_generic_tcpci_mux_init) set_usb_mux_not_tcpc(); /* Test fail on power status read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_POWER_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_POWER_STATUS); zassert_equal(EC_ERROR_INVAL, tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on uninitialised bit set */ @@ -294,15 +333,15 @@ ZTEST(tcpci, test_generic_tcpci_mux_init) TCPC_REG_POWER_STATUS_VBUS_DET); /* Test fail on alert mask write fail */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ALERT_MASK); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ALERT_MASK); zassert_equal(EC_ERROR_UNKNOWN, tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); /* Test fail on alert write fail */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ALERT); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ALERT); zassert_equal(EC_ERROR_UNKNOWN, tcpci_tcpm_mux_init(tcpci_usb_mux), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set arbitrary value to alert and alert mask registers */ @@ -318,15 +357,16 @@ ZTEST(tcpci, test_generic_tcpci_mux_init) /** Test TCPCI mux enter low power mode */ ZTEST(tcpci, test_generic_tcpci_mux_enter_low_power) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); struct usb_mux *tcpci_usb_mux = &usb_muxes[USBC_PORT_C0]; /* Set as usb mux with TCPC for first enter_low_power call */ set_usb_mux_tcpc(); /* Make sure that TCPC is not accessed */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(EC_SUCCESS, tcpci_tcpm_mux_enter_low_power(tcpci_usb_mux), NULL); @@ -335,10 +375,10 @@ ZTEST(tcpci, test_generic_tcpci_mux_enter_low_power) set_usb_mux_not_tcpc(); /* Test error on failed command set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_COMMAND); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_COMMAND); zassert_equal(EC_ERROR_INVAL, tcpci_tcpm_mux_enter_low_power(tcpci_usb_mux), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test correct command is issued */ @@ -350,8 +390,9 @@ ZTEST(tcpci, test_generic_tcpci_mux_enter_low_power) /** Test TCPCI mux set and get */ static void test_generic_tcpci_mux_set_get(void) { - const struct emul *emul = emul_get_binding(DT_LABEL(EMUL_LABEL)); - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); + const struct emul *emul = emul_get_binding(EMUL_LABEL); + struct i2c_common_emul_data *common_data = + emul_tcpci_generic_get_i2c_common_data(emul); struct usb_mux *tcpci_usb_mux = &usb_muxes[USBC_PORT_C0]; mux_state_t mux_state, mux_state_get; uint16_t exp_val, initial_val; @@ -360,20 +401,21 @@ static void test_generic_tcpci_mux_set_get(void) mux_state = USB_PD_MUX_NONE; /* Test fail on standard output config register read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_CONFIG_STD_OUTPUT); + i2c_common_emul_set_read_fail_reg(common_data, + TCPC_REG_CONFIG_STD_OUTPUT); zassert_equal(EC_ERROR_INVAL, tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), NULL); zassert_equal(EC_ERROR_INVAL, tcpci_tcpm_mux_get(tcpci_usb_mux, &mux_state_get), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on standard output config register write */ - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_CONFIG_STD_OUTPUT); zassert_equal(EC_ERROR_INVAL, tcpci_tcpm_mux_set(tcpci_usb_mux, mux_state, &ack), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set initial value for STD output register. Chosen arbitrary. */ diff --git a/zephyr/test/drivers/default/src/tcpci_test_common.c b/zephyr/test/drivers/default/src/tcpci_test_common.c index df2f14e3c4..934bdd1369 100644 --- a/zephyr/test/drivers/default/src/tcpci_test_common.c +++ b/zephyr/test/drivers/default/src/tcpci_test_common.c @@ -39,18 +39,19 @@ void check_tcpci_reg_with_mask_f(const struct emul *emul, int reg, } /** Test TCPCI init and vbus level */ -void test_tcpci_init(const struct emul *emul, enum usbc_port port) +void test_tcpci_init(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint16_t exp_mask; tcpc_config[port].flags |= TCPC_FLAGS_TCPCI_REV2_0_NO_VSAFE0V; /* Test fail on power status read */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_POWER_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_POWER_STATUS); zassert_equal(EC_ERROR_INVAL, drv->init(port), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on uninitialised bit set */ @@ -134,7 +135,9 @@ void test_tcpci_init(const struct emul *emul, enum usbc_port port) } /** Test TCPCI release */ -void test_tcpci_release(const struct emul *emul, enum usbc_port port) +void test_tcpci_release(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; @@ -147,7 +150,9 @@ void test_tcpci_release(const struct emul *emul, enum usbc_port port) } /** Test TCPCI get cc */ -void test_tcpci_get_cc(const struct emul *emul, enum usbc_port port) +void test_tcpci_get_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; enum tcpc_cc_voltage_status cc1, cc2; @@ -247,10 +252,11 @@ void test_tcpci_get_cc(const struct emul *emul, enum usbc_port port) } /** Test TCPCI set cc */ -void test_tcpci_set_cc(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_cc(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); enum tcpc_rp_value rp; enum tcpc_cc_pull cc; @@ -263,9 +269,9 @@ void test_tcpci_set_cc(const struct emul *emul, enum usbc_port port) TCPC_REG_ROLE_CTRL_SET(TYPEC_NO_DRP, rp, cc, cc)); /* Test error on failed role ctrl set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ROLE_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ROLE_CTRL); zassert_equal(EC_ERROR_INVAL, drv->set_cc(port, TYPEC_CC_OPEN), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting 1.5 RP and cc RD */ @@ -295,10 +301,11 @@ void test_tcpci_set_cc(const struct emul *emul, enum usbc_port port) } /** Test TCPCI set polarity */ -void test_tcpci_set_polarity(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_polarity(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint8_t initial_ctrl; uint8_t exp_ctrl; @@ -309,10 +316,10 @@ void test_tcpci_set_polarity(const struct emul *emul, enum usbc_port port) /* Test error on failed polarity set */ exp_ctrl = initial_ctrl; - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TCPC_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TCPC_CTRL); zassert_equal(EC_ERROR_INVAL, drv->set_polarity(port, POLARITY_CC2), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); check_tcpci_reg(emul, TCPC_REG_TCPC_CTRL, exp_ctrl); @@ -340,10 +347,11 @@ void test_tcpci_set_polarity(const struct emul *emul, enum usbc_port port) } /** Test TCPCI set vconn */ -void test_tcpci_set_vconn(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_vconn(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint8_t initial_ctrl; uint8_t exp_ctrl; @@ -354,9 +362,9 @@ void test_tcpci_set_vconn(const struct emul *emul, enum usbc_port port) /* Test error on failed vconn set */ exp_ctrl = initial_ctrl; - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_POWER_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_POWER_CTRL); zassert_equal(EC_ERROR_INVAL, drv->set_vconn(port, 1), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); check_tcpci_reg(emul, TCPC_REG_POWER_CTRL, exp_ctrl); @@ -372,17 +380,18 @@ void test_tcpci_set_vconn(const struct emul *emul, enum usbc_port port) } /** Test TCPCI set msg header */ -void test_tcpci_set_msg_header(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_msg_header(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); /* Test error on failed header set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_MSG_HDR_INFO); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_MSG_HDR_INFO); zassert_equal(EC_ERROR_INVAL, drv->set_msg_header(port, PD_ROLE_SINK, PD_ROLE_UFP), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting sink UFP */ @@ -415,15 +424,16 @@ void test_tcpci_set_msg_header(const struct emul *emul, enum usbc_port port) } /** Test TCPCI rx and sop prime enable */ -void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_rx_detect(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); /* Test error from rx_enable on rx detect set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_RX_DETECT); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_RX_DETECT); zassert_equal(EC_ERROR_INVAL, drv->set_rx_enable(port, 1), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test rx disable */ @@ -440,9 +450,9 @@ void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port) TCPC_REG_RX_DETECT_SOP_SOPP_SOPPP_HRST_MASK); /* Test error from sop_prime on rx detect set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_RX_DETECT); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_RX_DETECT); zassert_equal(EC_ERROR_INVAL, drv->sop_prime_enable(port, 0), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sop prime with rx enabled does change RX_DETECT */ @@ -459,10 +469,11 @@ void test_tcpci_set_rx_detect(const struct emul *emul, enum usbc_port port) } /** Test TCPCI get raw message from TCPC */ -void test_tcpci_get_rx_message_raw(const struct emul *emul, enum usbc_port port) +void test_tcpci_get_rx_message_raw(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); struct tcpci_emul_msg msg; uint32_t payload[7]; uint16_t rx_mask; @@ -488,10 +499,10 @@ void test_tcpci_get_rx_message_raw(const struct emul *emul, enum usbc_port port) "Failed to setup emulator message"); /* Test fail on reading byte count */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_RX_BUFFER); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_RX_BUFFER); zassert_equal(EC_ERROR_UNKNOWN, drv->get_message_raw(port, payload, &head), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Get raw message should always clean RX alerts */ rx_mask = TCPC_REG_ALERT_RX_BUF_OVF | TCPC_REG_ALERT_RX_STATUS; @@ -537,10 +548,11 @@ void test_tcpci_get_rx_message_raw(const struct emul *emul, enum usbc_port port) } /** Test TCPCI transmitting message from TCPC */ -void test_tcpci_transmit(const struct emul *emul, enum usbc_port port) +void test_tcpci_transmit(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); struct tcpci_emul_msg *msg; uint32_t data[6]; uint16_t header; @@ -554,11 +566,11 @@ void test_tcpci_transmit(const struct emul *emul, enum usbc_port port) } /* Test transmit hard reset fail */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TRANSMIT); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TRANSMIT); zassert_equal(EC_ERROR_INVAL, drv->transmit(port, TCPCI_MSG_TX_HARD_RESET, 0, NULL), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test transmit cabel reset */ @@ -574,10 +586,10 @@ void test_tcpci_transmit(const struct emul *emul, enum usbc_port port) zassert_equal(TCPCI_MSG_TX_HARD_RESET, msg->type, NULL); /* Test transmit fail on rx buffer */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TX_BUFFER); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TX_BUFFER); zassert_equal(EC_ERROR_INVAL, drv->transmit(port, TCPCI_MSG_SOP_PRIME, 0, data), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test transmit only header */ @@ -605,15 +617,16 @@ void test_tcpci_transmit(const struct emul *emul, enum usbc_port port) } /** Test TCPCI alert */ -void test_tcpci_alert(const struct emul *emul, enum usbc_port port) +void test_tcpci_alert(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); /* Test alert read fail */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_ALERT); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_ALERT); drv->tcpc_alert(port); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Handle overcurrent */ @@ -649,7 +662,9 @@ void test_tcpci_alert(const struct emul *emul, enum usbc_port port) } /** Test TCPCI alert RX message */ -void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) +void test_tcpci_alert_rx_message(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; struct tcpci_emul_msg msg1, msg2; @@ -795,7 +810,9 @@ void test_tcpci_alert_rx_message(const struct emul *emul, enum usbc_port port) } /** Test TCPCI auto discharge on disconnect */ -void test_tcpci_auto_discharge(const struct emul *emul, enum usbc_port port) +void test_tcpci_auto_discharge(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; uint8_t initial_ctrl; @@ -819,24 +836,25 @@ void test_tcpci_auto_discharge(const struct emul *emul, enum usbc_port port) } /** Test TCPCI drp toggle */ -void test_tcpci_drp_toggle(const struct emul *emul, enum usbc_port port) +void test_tcpci_drp_toggle(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint8_t exp_tcpc_ctrl, exp_role_ctrl, initial_tcpc_ctrl; /* Test error on failed role CTRL set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ROLE_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ROLE_CTRL); zassert_equal(EC_ERROR_INVAL, drv->drp_toggle(port), NULL); /* Test error on failed TCPC CTRL set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TCPC_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TCPC_CTRL); zassert_equal(EC_ERROR_INVAL, drv->drp_toggle(port), NULL); /* Test error on failed command set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_COMMAND); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_COMMAND); zassert_equal(EC_ERROR_INVAL, drv->drp_toggle(port), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set initial value for TCPC ctrl register. Chosen arbitrary. */ @@ -884,25 +902,26 @@ void test_tcpci_drp_toggle(const struct emul *emul, enum usbc_port port) } /** Test TCPCI get chip info */ -void test_tcpci_get_chip_info(const struct emul *emul, enum usbc_port port) +void test_tcpci_get_chip_info(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); struct ec_response_pd_chip_info_v1 info; uint16_t vendor, product, bcd; /* Test error on failed vendor id get */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_VENDOR_ID); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_VENDOR_ID); zassert_equal(EC_ERROR_INVAL, drv->get_chip_info(port, 1, &info), NULL); /* Test error on failed product id get */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_PRODUCT_ID); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_PRODUCT_ID); zassert_equal(EC_ERROR_INVAL, drv->get_chip_info(port, 1, &info), NULL); /* Test error on failed BCD get */ - i2c_common_emul_set_read_fail_reg(i2c_emul, TCPC_REG_VENDOR_ID); + i2c_common_emul_set_read_fail_reg(common_data, TCPC_REG_VENDOR_ID); zassert_equal(EC_ERROR_INVAL, drv->get_chip_info(port, 1, &info), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test reading chip info. Values chosen arbitrary. */ @@ -922,10 +941,10 @@ void test_tcpci_get_chip_info(const struct emul *emul, enum usbc_port port) info.product_id = 0; info.device_id = 0; /* Make sure, that TCPC is not accessed */ - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(EC_SUCCESS, drv->get_chip_info(port, 0, &info), NULL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(vendor, info.vendor_id, NULL); zassert_equal(product, info.product_id, NULL); @@ -933,15 +952,16 @@ void test_tcpci_get_chip_info(const struct emul *emul, enum usbc_port port) } /** Test TCPCI enter low power mode */ -void test_tcpci_low_power_mode(const struct emul *emul, enum usbc_port port) +void test_tcpci_low_power_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); /* Test error on failed command set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_COMMAND); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_COMMAND); zassert_equal(EC_ERROR_INVAL, drv->enter_low_power_mode(port), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test correct command is issued */ @@ -950,21 +970,22 @@ void test_tcpci_low_power_mode(const struct emul *emul, enum usbc_port port) } /** Test TCPCI set bist test mode */ -void test_tcpci_set_bist_mode(const struct emul *emul, enum usbc_port port) +void test_tcpci_set_bist_mode(const struct emul *emul, + struct i2c_common_emul_data *common_data, + enum usbc_port port) { const struct tcpm_drv *drv = tcpc_config[port].drv; - struct i2c_emul *i2c_emul = tcpci_emul_get_i2c_emul(emul); uint16_t exp_mask, initial_mask; uint8_t exp_ctrl, initial_ctrl; /* Test error on TCPC CTRL set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_TCPC_CTRL); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_TCPC_CTRL); zassert_equal(EC_ERROR_INVAL, drv->set_bist_test_mode(port, 1), NULL); /* Test error on alert mask set */ - i2c_common_emul_set_write_fail_reg(i2c_emul, TCPC_REG_ALERT_MASK); + i2c_common_emul_set_write_fail_reg(common_data, TCPC_REG_ALERT_MASK); zassert_equal(EC_ERROR_INVAL, drv->set_bist_test_mode(port, 1), NULL); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(common_data, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set initial value for alert mask register. Chosen arbitrary. */ diff --git a/zephyr/test/drivers/default/src/tcs3400.c b/zephyr/test/drivers/default/src/tcs3400.c index d7fcface36..6ef1c20dab 100644 --- a/zephyr/test/drivers/default/src/tcs3400.c +++ b/zephyr/test/drivers/default/src/tcs3400.c @@ -29,9 +29,10 @@ ZTEST_USER(tcs3400, test_tcs_init) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = tcs_emul_get(TCS_ORD); + struct i2c_common_emul_data *common_data = + emul_tcs3400_get_i2c_common_data(emul); - emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; @@ -39,9 +40,11 @@ ZTEST_USER(tcs3400, test_tcs_init) zassert_equal(EC_SUCCESS, ms_rgb->drv->init(ms_rgb), NULL); /* Fail init on communication errors */ - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(EC_ERROR_INVAL, ms->drv->init(ms), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Fail on bad ID */ tcs_emul_set_reg(emul, TCS_I2C_ID, 0); @@ -62,7 +65,9 @@ ZTEST_USER(tcs3400, test_tcs_init) ZTEST_USER(tcs3400, test_tcs_read) { struct motion_sensor_t *ms; - struct i2c_emul *emul; + const struct emul *emul = tcs_emul_get(TCS_ORD); + struct i2c_common_emul_data *common_data = + emul_tcs3400_get_i2c_common_data(emul); uint8_t enable; intv3_t v; @@ -70,13 +75,14 @@ ZTEST_USER(tcs3400, test_tcs_read) ms = &motion_sensors[TCS_CLR_SENSOR_ID]; /* Test error on writing registers */ - i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ATIME); + i2c_common_emul_set_write_fail_reg(common_data, TCS_I2C_ATIME); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, v), NULL); - i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_CONTROL); + i2c_common_emul_set_write_fail_reg(common_data, TCS_I2C_CONTROL); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, v), NULL); - i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE); + i2c_common_emul_set_write_fail_reg(common_data, TCS_I2C_ENABLE); zassert_equal(EC_ERROR_INVAL, ms->drv->read(ms, v), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test starting read with calibration */ tcs_emul_set_reg(emul, TCS_I2C_ATIME, 0); @@ -141,7 +147,9 @@ static void check_fifo_empty_f(struct motion_sensor_t *ms, ZTEST_USER(tcs3400, test_tcs_irq_handler_fail) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = tcs_emul_get(TCS_ORD); + struct i2c_common_emul_data *common_data = + emul_tcs3400_get_i2c_common_data(emul); uint32_t event; emul = tcs_emul_get(TCS_ORD); @@ -156,15 +164,17 @@ ZTEST_USER(tcs3400, test_tcs_irq_handler_fail) event = TCS_INT_EVENT; /* Test error on reading status */ - i2c_common_emul_set_read_fail_reg(emul, TCS_I2C_STATUS); + i2c_common_emul_set_read_fail_reg(common_data, TCS_I2C_STATUS); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); check_fifo_empty(ms, ms_rgb); /* Test fail on changing device power state */ - i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE); + i2c_common_emul_set_write_fail_reg(common_data, TCS_I2C_ENABLE); zassert_equal(EC_ERROR_INVAL, ms->drv->irq_handler(ms, &event), NULL); - i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); check_fifo_empty(ms, ms_rgb); /* Test that no data is committed when status is 0 */ @@ -230,7 +240,7 @@ static void check_fifo_f(struct motion_sensor_t *ms, ZTEST_USER(tcs3400, test_tcs_read_calibration) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = tcs_emul_get(TCS_ORD); uint32_t event = TCS_INT_EVENT; int emul_v[4]; int exp_v[4]; @@ -304,7 +314,7 @@ ZTEST_USER(tcs3400, test_tcs_read_calibration) * First element of expected vector is updated by this function. */ static void set_emul_val_from_exp(int *exp_v, uint16_t *scale, - struct i2c_emul *emul) + const struct emul *emul) { int emul_v[4]; int ir; @@ -342,7 +352,7 @@ static void set_emul_val_from_exp(int *exp_v, uint16_t *scale, ZTEST_USER(tcs3400, test_tcs_read_xyz) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = tcs_emul_get(TCS_ORD); uint32_t event = TCS_INT_EVENT; /* Expected data to test: IR, R, G, B */ int exp_v[][4] = { @@ -414,7 +424,7 @@ ZTEST_USER(tcs3400, test_tcs_read_xyz) ZTEST_USER(tcs3400, test_tcs_scale) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = tcs_emul_get(TCS_ORD); uint32_t event = TCS_INT_EVENT; /* Expected data to test: IR, R, G, B */ int exp_v[][4] = { @@ -524,7 +534,9 @@ ZTEST_USER(tcs3400, test_tcs_scale) ZTEST_USER(tcs3400, test_tcs_data_rate) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = tcs_emul_get(TCS_ORD); + struct i2c_common_emul_data *common_data = + emul_tcs3400_get_i2c_common_data(emul); uint8_t enable; emul = tcs_emul_get(TCS_ORD); @@ -533,12 +545,13 @@ ZTEST_USER(tcs3400, test_tcs_data_rate) ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; /* Test fail on reading device power state */ - i2c_common_emul_set_read_fail_reg(emul, TCS_I2C_ENABLE); + i2c_common_emul_set_read_fail_reg(common_data, TCS_I2C_ENABLE); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 0, 0), NULL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 0, 1), NULL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 100, 0), NULL); zassert_equal(EC_ERROR_INVAL, ms->drv->set_data_rate(ms, 100, 1), NULL); - i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(common_data, + I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting 0 rate disables device */ zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 0, 0), NULL); @@ -585,7 +598,7 @@ ZTEST_USER(tcs3400, test_tcs_data_rate) ZTEST_USER(tcs3400, test_tcs_set_range) { struct motion_sensor_t *ms, *ms_rgb; - struct i2c_emul *emul; + const struct emul *emul = tcs_emul_get(TCS_ORD); emul = tcs_emul_get(TCS_ORD); ms = &motion_sensors[TCS_CLR_SENSOR_ID]; diff --git a/zephyr/test/drivers/isl923x/src/charge_ramp_hw.c b/zephyr/test/drivers/isl923x/src/charge_ramp_hw.c index fcb1b73018..298f71d5b7 100644 --- a/zephyr/test/drivers/isl923x/src/charge_ramp_hw.c +++ b/zephyr/test/drivers/isl923x/src/charge_ramp_hw.c @@ -32,20 +32,20 @@ ZTEST(charge_ramp_hw, test_charge_ramp_hw_ramp) ZTEST(charge_ramp_hw, test_charge_ramp_hw_ramp_read_fail_reg0) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(ISL923X_EMUL); - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + i2c_common_emul_set_read_fail_reg(common_data, ISL923X_REG_CONTROL0); zassert_equal(EC_ERROR_INVAL, isl923x_drv.set_hw_ramp(CHARGER_NUM, 1), NULL); } ZTEST(charge_ramp_hw, test_charge_ramp_hw_ramp_read_fail_acl1) { - const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(ISL923X_EMUL); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(common_data, ISL923X_REG_ADAPTER_CURRENT_LIMIT1); zassert_equal(0, isl923x_drv.ramp_get_current_limit(CHARGER_NUM), NULL); } diff --git a/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c b/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c index a7cdf48419..3825f4bc36 100644 --- a/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c +++ b/zephyr/test/drivers/isl923x/src/console_cmd_amon_bmon.c @@ -63,10 +63,11 @@ ZTEST(console_cmd_amon_bmon, test_isl923x_amonbmon_get_input_current_read_fail_req1) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(isl923x_emul); int current_milli_amps; - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_read_fail_reg(common_data, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current(CHARGER_NUM, ¤t_milli_amps), @@ -79,10 +80,11 @@ ZTEST(console_cmd_amon_bmon, test_isl923x_amonbmon_get_input_current_read_fail_req3) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(isl923x_emul); int current_milli_amps; - i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_read_fail_reg(common_data, ISL9238_REG_CONTROL3); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current(CHARGER_NUM, ¤t_milli_amps), @@ -93,10 +95,11 @@ ZTEST(console_cmd_amon_bmon, test_isl923x_amonbmon_get_input_current_write_fail_req1) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(isl923x_emul); int current_milli_amps; - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL1); + i2c_common_emul_set_write_fail_reg(common_data, ISL923X_REG_CONTROL1); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current(CHARGER_NUM, ¤t_milli_amps), @@ -107,10 +110,11 @@ ZTEST(console_cmd_amon_bmon, test_isl923x_amonbmon_get_input_current_write_fail_req3) { const struct emul *isl923x_emul = ISL923X_EMUL; - struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + struct i2c_common_emul_data *common_data = + emul_isl923x_get_i2c_common_data(isl923x_emul); int current_milli_amps; - i2c_common_emul_set_write_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + i2c_common_emul_set_write_fail_reg(common_data, ISL9238_REG_CONTROL3); zassert_equal(EC_ERROR_INVAL, isl923x_drv.get_input_current(CHARGER_NUM, ¤t_milli_amps), |