diff options
author | Aaron Massey <aaronmassey@google.com> | 2022-07-21 17:55:22 -0600 |
---|---|---|
committer | Chromeos LUCI <chromeos-scoped@luci-project-accounts.iam.gserviceaccount.com> | 2022-08-01 20:35:36 +0000 |
commit | 86db909517f1493b4bde2ad939c0ee248921995c (patch) | |
tree | d167fd043fe4dba265d387704f907e8500c8f01a | |
parent | b719d38f1a02fbe3f003b5f2b840166496b3326c (diff) | |
download | chrome-ec-86db909517f1493b4bde2ad939c0ee248921995c.tar.gz |
zephyr: test: Migrate to upstream emulator API
Migrate the existing emulators and tests to use the new upstream
emulator API. Majority of these changes was the removal of struct
i2c_emul usages in favor of direct usages of struct emul.
BRANCH=none
BUG=b:240291860
TEST=zmake test -a
Signed-off-by: Aaron Massey <aaronmassey@google.com>
Cq-Depend: chromium:3776856
Change-Id: I09c509ffc34ac3b28fbd4dece0acebcc36cc0932
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3781964
Reviewed-by: Yuval Peress <peress@google.com>
Tested-by: Yuval Peress <peress@google.com>
62 files changed, 2398 insertions, 1760 deletions
diff --git a/zephyr/emul/emul_bb_retimer.c b/zephyr/emul/emul_bb_retimer.c index 54402555bb..7587dc656f 100644 --- a/zephyr/emul/emul_bb_retimer.c +++ b/zephyr/emul/emul_bb_retimer.c @@ -16,13 +16,10 @@ LOG_MODULE_REGISTER(emul_bb_retimer); #include "emul/emul_common_i2c.h" #include "emul/emul_bb_retimer.h" +#include "emul/emul_stub_device.h" #include "driver/retimer/bb_retimer.h" -#define BB_DATA_FROM_I2C_EMUL(_emul) \ - CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \ - struct bb_emul_data, common) - /** Run-time data used by the emulator */ struct bb_emul_data { /** Common I2C data */ @@ -44,7 +41,7 @@ struct bb_emul_data { }; /** Check description in emul_bb_retimer.h */ -void bb_emul_set_reg(struct i2c_emul *emul, int reg, uint32_t val) +void bb_emul_set_reg(const struct emul *emul, int reg, uint32_t val) { struct bb_emul_data *data; @@ -52,12 +49,12 @@ void bb_emul_set_reg(struct i2c_emul *emul, int reg, uint32_t val) return; } - data = BB_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->reg[reg] = val; } /** Check description in emul_bb_retimer.h */ -uint32_t bb_emul_get_reg(struct i2c_emul *emul, int reg) +uint32_t bb_emul_get_reg(const struct emul *emul, int reg) { struct bb_emul_data *data; @@ -65,26 +62,26 @@ uint32_t bb_emul_get_reg(struct i2c_emul *emul, int reg) return 0; } - data = BB_DATA_FROM_I2C_EMUL(emul); + data = emul->data; return data->reg[reg]; } /** Check description in emul_bb_retimer.h */ -void bb_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set) +void bb_emul_set_err_on_ro_write(const struct emul *emul, bool set) { struct bb_emul_data *data; - data = BB_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_ro_write = set; } /** Check description in emul_bb_retimer.h */ -void bb_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set) +void bb_emul_set_err_on_rsvd_write(const struct emul *emul, bool set) { struct bb_emul_data *data; - data = BB_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_rsvd_write = set; } @@ -105,11 +102,11 @@ static const uint32_t bb_emul_rsvd_mask[] = { * * @param emul Pointer to BB retimer emulator */ -static void bb_emul_reset(struct i2c_emul *emul) +static void bb_emul_reset(const struct emul *emul) { struct bb_emul_data *data; - data = BB_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->reg[BB_RETIMER_REG_VENDOR_ID] = data->vendor_id; data->reg[BB_RETIMER_REG_DEVICE_ID] = BB_RETIMER_DEVICE_ID; @@ -133,12 +130,12 @@ static void bb_emul_reset(struct i2c_emul *emul) * @return 0 on success * @return -EIO on error */ -static int bb_emul_handle_write(struct i2c_emul *emul, int reg, int msg_len) +static int bb_emul_handle_write(const struct emul *emul, int reg, int msg_len) { struct bb_emul_data *data; uint32_t val; - data = BB_DATA_FROM_I2C_EMUL(emul); + data = emul->data; /* This write only selected register for I2C read message */ if (msg_len < 2) { @@ -190,11 +187,11 @@ static int bb_emul_handle_write(struct i2c_emul *emul, int reg, int msg_len) * @return 0 on success * @return -EIO on error */ -static int bb_emul_handle_read(struct i2c_emul *emul, int reg) +static int bb_emul_handle_read(const struct emul *emul, int reg) { struct bb_emul_data *data; - data = BB_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (reg >= BB_RETIMER_REG_COUNT) { LOG_ERR("Read unknown register 0x%x", reg); @@ -218,12 +215,12 @@ static int bb_emul_handle_read(struct i2c_emul *emul, int reg) * * @return 0 on success */ -static int bb_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, +static int bb_emul_write_byte(const struct emul *emul, int reg, uint8_t val, int bytes) { struct bb_emul_data *data; - data = BB_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (bytes == 1) { data->data_dword = 0; @@ -248,12 +245,12 @@ static int bb_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, * * @return 0 on success */ -static int bb_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, +static int bb_emul_read_byte(const struct emul *emul, int reg, uint8_t *val, int bytes) { struct bb_emul_data *data; - data = BB_DATA_FROM_I2C_EMUL(emul); + data = emul->data; /* First byte of read message is read size which is always 4 */ if (bytes == 0) { @@ -278,7 +275,7 @@ static int bb_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, * * @return Currently accessed register */ -static int bb_emul_access_reg(struct i2c_emul *emul, int reg, int bytes, +static int bb_emul_access_reg(const struct emul *emul, int reg, int bytes, bool read) { return reg; @@ -300,20 +297,18 @@ static int bb_emul_access_reg(struct i2c_emul *emul, int reg, int bytes, static int bb_emul_init(const struct emul *emul, const struct device *parent) { const struct i2c_common_emul_cfg *cfg = emul->cfg; - struct i2c_common_emul_data *data = cfg->data; - int ret; + struct bb_emul_data *data = emul->data; - data->emul.api = &i2c_common_emul_api; - data->emul.addr = cfg->addr; - data->i2c = parent; - data->cfg = cfg; - i2c_common_emul_init(data); + data->common.emul.addr = cfg->addr; + data->common.emul.target = emul; + data->common.i2c = parent; + data->common.cfg = cfg; - ret = i2c_emul_register(parent, emul->dev_label, &data->emul); + i2c_common_emul_init(&data->common); - bb_emul_reset(&data->emul); + bb_emul_reset(emul); - return ret; + return 0; } #define BB_RETIMER_EMUL(n) \ @@ -340,16 +335,16 @@ static int bb_emul_init(const struct emul *emul, const struct device *parent) .addr = DT_INST_REG_ADDR(n), \ }; \ EMUL_DEFINE(bb_emul_init, DT_DRV_INST(n), &bb_emul_cfg_##n, \ - &bb_emul_data_##n) + &bb_emul_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(BB_RETIMER_EMUL) #define BB_RETIMER_EMUL_CASE(n) \ case DT_INST_DEP_ORD(n): \ - return &bb_emul_data_##n.common.emul; + return bb_emul_data_##n.common.emul.target; /** Check description in emul_bb_emulator.h */ -struct i2c_emul *bb_emul_get(int ord) +const struct emul *bb_emul_get(int ord) { switch (ord) { DT_INST_FOREACH_STATUS_OKAY(BB_RETIMER_EMUL_CASE) @@ -358,3 +353,11 @@ struct i2c_emul *bb_emul_get(int ord) return NULL; } } + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_bb_retimer_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/emul_bma255.c b/zephyr/emul/emul_bma255.c index ca75442156..70b120c167 100644 --- a/zephyr/emul/emul_bma255.c +++ b/zephyr/emul/emul_bma255.c @@ -18,10 +18,7 @@ LOG_MODULE_REGISTER(emul_bma255); #include "emul/emul_bma255.h" #include "driver/accel_bma2x2.h" - -#define BMA_DATA_FROM_I2C_EMUL(_emul) \ - CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \ - struct bma_emul_data, common) +#include "emul/emul_stub_device.h" /** Run-time data used by the emulator */ struct bma_emul_data { @@ -74,7 +71,7 @@ struct bma_emul_data { }; /** Check description in emul_bma255.h */ -void bma_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val) +void bma_emul_set_reg(const struct emul *emul, int reg, uint8_t val) { struct bma_emul_data *data; @@ -82,12 +79,12 @@ void bma_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val) return; } - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->reg[reg] = val; } /** Check description in emul_bma255.h */ -uint8_t bma_emul_get_reg(struct i2c_emul *emul, int reg) +uint8_t bma_emul_get_reg(const struct emul *emul, int reg) { struct bma_emul_data *data; @@ -95,7 +92,7 @@ uint8_t bma_emul_get_reg(struct i2c_emul *emul, int reg) return 0; } - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; return data->reg[reg]; } @@ -182,11 +179,11 @@ static uint8_t bma_emul_off_to_nvm(int16_t off) } /** Check description in emul_bma255.h */ -int16_t bma_emul_get_off(struct i2c_emul *emul, int axis) +int16_t bma_emul_get_off(const struct emul *emul, int axis) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case BMA_EMUL_AXIS_X: @@ -201,11 +198,11 @@ int16_t bma_emul_get_off(struct i2c_emul *emul, int axis) } /** Check description in emul_bma255.h */ -void bma_emul_set_off(struct i2c_emul *emul, int axis, int16_t val) +void bma_emul_set_off(const struct emul *emul, int axis, int16_t val) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case BMA_EMUL_AXIS_X: @@ -227,11 +224,11 @@ void bma_emul_set_off(struct i2c_emul *emul, int axis, int16_t val) } /** Check description in emul_bma255.h */ -int16_t bma_emul_get_acc(struct i2c_emul *emul, int axis) +int16_t bma_emul_get_acc(const struct emul *emul, int axis) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case BMA_EMUL_AXIS_X: @@ -246,11 +243,11 @@ int16_t bma_emul_get_acc(struct i2c_emul *emul, int axis) } /** Check description in emul_bma255.h */ -void bma_emul_set_acc(struct i2c_emul *emul, int axis, int16_t val) +void bma_emul_set_acc(const struct emul *emul, int axis, int16_t val) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case BMA_EMUL_AXIS_X: @@ -266,47 +263,47 @@ void bma_emul_set_acc(struct i2c_emul *emul, int axis, int16_t val) } /** Check description in emul_bma255.h */ -void bma_emul_set_err_on_cal_nrdy(struct i2c_emul *emul, bool set) +void bma_emul_set_err_on_cal_nrdy(const struct emul *emul, bool set) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_cal_trg_nrdy = set; } /** Check description in emul_bma255.h */ -void bma_emul_set_err_on_cal_bad_range(struct i2c_emul *emul, bool set) +void bma_emul_set_err_on_cal_bad_range(const struct emul *emul, bool set) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_cal_trg_bad_range = set; } /** Check description in emul_bma255.h */ -void bma_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set) +void bma_emul_set_err_on_ro_write(const struct emul *emul, bool set) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_ro_write = set; } /** Check description in emul_bma255.h */ -void bma_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set) +void bma_emul_set_err_on_rsvd_write(const struct emul *emul, bool set) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_rsvd_write = set; } /** Check description in emul_bma255.h */ -void bma_emul_set_err_on_msb_first(struct i2c_emul *emul, bool set) +void bma_emul_set_err_on_msb_first(const struct emul *emul, bool set) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_msb_first = set; } @@ -384,11 +381,11 @@ static const uint8_t bma_emul_rsvd_mask[] = { * * @param emul Pointer to BMA255 emulator */ -static void bma_emul_restore_nvm(struct i2c_emul *emul) +static void bma_emul_restore_nvm(const struct emul *emul) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; /* Restore registers values */ data->reg[BMA2x2_OFFSET_X_AXIS_ADDR] = data->nvm_x; @@ -408,11 +405,11 @@ static void bma_emul_restore_nvm(struct i2c_emul *emul) * * @param emul Pointer to BMA255 emulator */ -static void bma_emul_reset(struct i2c_emul *emul) +static void bma_emul_reset(const struct emul *emul) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->reg[BMA2x2_CHIP_ID_ADDR] = 0xfa; data->reg[0x01] = 0x00; /* Reserved */ @@ -514,12 +511,12 @@ static int bma_emul_range_to_shift(uint8_t range) * * @return 0 on success */ -static int bma_emul_handle_nvm_write(struct i2c_emul *emul, uint8_t val) +static int bma_emul_handle_nvm_write(const struct emul *emul, uint8_t val) { struct bma_emul_data *data; uint8_t writes_rem; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; /* NVM not ready, ignore write/load requests */ if (!(data->reg[BMA2x2_EEPROM_CTRL_ADDR] & BMA2x2_EEPROM_RDY)) { @@ -558,11 +555,11 @@ static int bma_emul_handle_nvm_write(struct i2c_emul *emul, uint8_t val) * * @param emul Pointer to BMA255 emulator */ -static void bma_emul_clear_int(struct i2c_emul *emul) +static void bma_emul_clear_int(const struct emul *emul) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->reg[BMA2x2_STAT1_ADDR] = 0x00; data->reg[BMA2x2_STAT2_ADDR] = 0x00; @@ -579,12 +576,12 @@ static void bma_emul_clear_int(struct i2c_emul *emul) * * @return target Value to which offset compensation should be calculated */ -static int16_t bma_emul_get_target(struct i2c_emul *emul, int axis) +static int16_t bma_emul_get_target(const struct emul *emul, int axis) { struct bma_emul_data *data; uint8_t target; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; target = data->reg[BMA2x2_OFC_SETTING_ADDR] >> BMA2x2_OFC_TARGET_AXIS(axis); @@ -614,13 +611,13 @@ static int16_t bma_emul_get_target(struct i2c_emul *emul, int axis) * @return 0 on success * @return -EIO when trying to start fast compensation in wrong emulator state */ -static int bma_emul_handle_off_comp(struct i2c_emul *emul, uint8_t val) +static int bma_emul_handle_off_comp(const struct emul *emul, uint8_t val) { struct bma_emul_data *data; uint8_t trigger; int16_t target; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (val & BMA2x2_OFFSET_RESET) { data->off_x = 0; @@ -680,13 +677,13 @@ static int bma_emul_handle_off_comp(struct i2c_emul *emul, uint8_t val) * @return 0 on success * @return -EIO on error */ -static int bma_emul_handle_write(struct i2c_emul *emul, int reg, int bytes) +static int bma_emul_handle_write(const struct emul *emul, int reg, int bytes) { struct bma_emul_data *data; uint8_t val; int ret; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; val = data->write_byte; @@ -791,7 +788,7 @@ static int bma_emul_handle_write(struct i2c_emul *emul, int reg, int bytes) * @return 0 on success * @return -EIO when accessing MSB before LSB with enabled shadowing */ -static int bma_emul_get_acc_val(struct i2c_emul *emul, int lsb_reg, +static int bma_emul_get_acc_val(const struct emul *emul, int lsb_reg, bool *lsb_read, bool lsb, int16_t val) { struct bma_emul_data *data; @@ -800,7 +797,7 @@ static int bma_emul_get_acc_val(struct i2c_emul *emul, int lsb_reg, int msb_reg; int shift; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (lsb) { *lsb_read = 1; @@ -835,7 +832,7 @@ static int bma_emul_get_acc_val(struct i2c_emul *emul, int lsb_reg, } /** Check description in emul_bma255.h */ -int bma_emul_access_reg(struct i2c_emul *emul, int reg, int bytes, bool read) +int bma_emul_access_reg(const struct emul *emul, int reg, int bytes, bool read) { /* * Exclude first byte (select register) from total number of bytes @@ -866,13 +863,13 @@ int bma_emul_access_reg(struct i2c_emul *emul, int reg, int bytes, bool read) * @return 0 on success * @return -EIO on error */ -static int bma_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *val, +static int bma_emul_handle_read(const struct emul *emul, int reg, uint8_t *val, int bytes) { struct bma_emul_data *data; int ret; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; reg = bma_emul_access_reg(emul, reg, bytes, true /* = read */); @@ -934,12 +931,12 @@ static int bma_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *val, * @return 0 on success * @return -EIO on error */ -static int bma_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, +static int bma_emul_write_byte(const struct emul *emul, int reg, uint8_t val, int bytes) { struct bma_emul_data *data; - data = BMA_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->write_byte = val; @@ -962,20 +959,18 @@ static int bma_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, static int bma_emul_init(const struct emul *emul, const struct device *parent) { const struct i2c_common_emul_cfg *cfg = emul->cfg; - struct i2c_common_emul_data *data = cfg->data; - int ret; + struct bma_emul_data *data = emul->data; - data->emul.api = &i2c_common_emul_api; - data->emul.addr = cfg->addr; - data->i2c = parent; - data->cfg = cfg; - i2c_common_emul_init(data); + data->common.emul.addr = cfg->addr; + data->common.emul.target = emul; + data->common.i2c = parent; + data->common.cfg = cfg; - ret = i2c_emul_register(parent, emul->dev_label, &data->emul); + i2c_common_emul_init(&data->common); - bma_emul_reset(&data->emul); + bma_emul_reset(emul); - return ret; + return 0; } #define BMA255_EMUL(n) \ @@ -1016,16 +1011,16 @@ static int bma_emul_init(const struct emul *emul, const struct device *parent) .addr = DT_INST_REG_ADDR(n), \ }; \ EMUL_DEFINE(bma_emul_init, DT_DRV_INST(n), &bma_emul_cfg_##n, \ - &bma_emul_data_##n) + &bma_emul_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(BMA255_EMUL) #define BMA255_EMUL_CASE(n) \ case DT_INST_DEP_ORD(n): \ - return &bma_emul_data_##n.common.emul; + return bma_emul_data_##n.common.emul.target; /** Check description in emul_bma255.h */ -struct i2c_emul *bma_emul_get(int ord) +const struct emul *bma_emul_get(int ord) { switch (ord) { DT_INST_FOREACH_STATUS_OKAY(BMA255_EMUL_CASE) @@ -1034,3 +1029,11 @@ struct i2c_emul *bma_emul_get(int ord) return NULL; } } + +struct i2c_common_emul_data * +emul_bma_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); diff --git a/zephyr/emul/emul_bmi.c b/zephyr/emul/emul_bmi.c index 23f0bfddeb..b72c729b80 100644 --- a/zephyr/emul/emul_bmi.c +++ b/zephyr/emul/emul_bmi.c @@ -16,15 +16,12 @@ LOG_MODULE_REGISTER(emul_bmi); #include "emul/emul_common_i2c.h" #include "emul/emul_bmi.h" +#include "emul/emul_stub_device.h" #include "driver/accelgyro_bmi160.h" #include "driver/accelgyro_bmi260.h" #include "driver/accelgyro_bmi_common.h" -#define BMI_DATA_FROM_I2C_EMUL(_emul) \ - CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \ - struct bmi_emul_data, common) - /** Run-time data used by the emulator */ struct bmi_emul_data { /** Common I2C data */ @@ -87,7 +84,7 @@ struct bmi_emul_data { }; /** Check description in emul_bmi.h */ -void bmi_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val) +void bmi_emul_set_reg(const struct emul *emul, int reg, uint8_t val) { struct bmi_emul_data *data; @@ -95,12 +92,12 @@ void bmi_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val) return; } - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->reg[reg] = val; } /** Check description in emul_bmi.h */ -uint8_t bmi_emul_get_reg(struct i2c_emul *emul, int reg) +uint8_t bmi_emul_get_reg(const struct emul *emul, int reg) { struct bmi_emul_data *data; @@ -108,7 +105,7 @@ uint8_t bmi_emul_get_reg(struct i2c_emul *emul, int reg) return 0; } - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; return data->reg[reg]; } @@ -258,11 +255,11 @@ static uint16_t bmi_emul_gyr_off_to_nvm(int16_t off) } /** Check description in emul_bmi.h */ -int16_t bmi_emul_get_off(struct i2c_emul *emul, enum bmi_emul_axis axis) +int16_t bmi_emul_get_off(const struct emul *emul, enum bmi_emul_axis axis) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case BMI_EMUL_ACC_X: @@ -283,14 +280,14 @@ int16_t bmi_emul_get_off(struct i2c_emul *emul, enum bmi_emul_axis axis) } /** Check description in emul_bmi.h */ -void bmi_emul_set_off(struct i2c_emul *emul, enum bmi_emul_axis axis, +void bmi_emul_set_off(const struct emul *emul, enum bmi_emul_axis axis, int16_t val) { struct bmi_emul_data *data; uint16_t gyr_off; uint8_t gyr98_shift; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case BMI_EMUL_ACC_X: @@ -342,11 +339,11 @@ void bmi_emul_set_off(struct i2c_emul *emul, enum bmi_emul_axis axis, } /** Check description in emul_bmi.h */ -int32_t bmi_emul_get_value(struct i2c_emul *emul, enum bmi_emul_axis axis) +int32_t bmi_emul_get_value(const struct emul *emul, enum bmi_emul_axis axis) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case BMI_EMUL_ACC_X: @@ -367,12 +364,12 @@ int32_t bmi_emul_get_value(struct i2c_emul *emul, enum bmi_emul_axis axis) } /** Check description in emul_bmi.h */ -void bmi_emul_set_value(struct i2c_emul *emul, enum bmi_emul_axis axis, +void bmi_emul_set_value(const struct emul *emul, enum bmi_emul_axis axis, int32_t val) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case BMI_EMUL_ACC_X: @@ -397,47 +394,47 @@ void bmi_emul_set_value(struct i2c_emul *emul, enum bmi_emul_axis axis, } /** Check description in emul_bmi.h */ -void bmi_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set) +void bmi_emul_set_err_on_ro_write(const struct emul *emul, bool set) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_ro_write = set; } /** Check description in emul_bmi.h */ -void bmi_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set) +void bmi_emul_set_err_on_rsvd_write(const struct emul *emul, bool set) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_rsvd_write = set; } /** Check description in emul_bmi.h */ -void bmi_emul_set_err_on_wo_read(struct i2c_emul *emul, bool set) +void bmi_emul_set_err_on_wo_read(const struct emul *emul, bool set) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_wo_read = set; } /** Check description in emul_bmi.h */ -void bmi_emul_simulate_cmd_exec_time(struct i2c_emul *emul, bool set) +void bmi_emul_simulate_cmd_exec_time(const struct emul *emul, bool set) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->simulate_command_exec_time = set; } /** Check description in emul_bmi.h */ -void bmi_emul_set_skipped_frames(struct i2c_emul *emul, uint8_t skip) +void bmi_emul_set_skipped_frames(const struct emul *emul, uint8_t skip) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->fifo_skip = skip; } @@ -460,13 +457,13 @@ static int64_t bmi_emul_get_sensortime(void) * @param reg Pointer to 3 byte array, where current sensor time should be * stored */ -static void bmi_emul_set_sensortime_reg(struct i2c_emul *emul, uint8_t *reg) +static void bmi_emul_set_sensortime_reg(const struct emul *emul, uint8_t *reg) { struct bmi_emul_data *data; uint32_t twos_comp_val; int64_t time; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; time = bmi_emul_get_sensortime(); @@ -487,13 +484,13 @@ static void bmi_emul_set_sensortime_reg(struct i2c_emul *emul, uint8_t *reg) * @param reg Pointer to 2 byte array, where sensor value should be stored * @param shift How many bits should be shift to the right */ -static void bmi_emul_set_data_reg(struct i2c_emul *emul, int32_t val, +static void bmi_emul_set_data_reg(const struct emul *emul, int32_t val, uint8_t *reg, int shift) { struct bmi_emul_data *data; uint32_t twos_comp_val; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; twos_comp_val = bmi_emul_val_to_twos_comp(val); @@ -515,14 +512,14 @@ static void bmi_emul_set_data_reg(struct i2c_emul *emul, int32_t val, * * @return length of frame */ -static uint8_t bmi_emul_get_frame_len(struct i2c_emul *emul, +static uint8_t bmi_emul_get_frame_len(const struct emul *emul, struct bmi_emul_frame *frame, bool tag_time, bool header) { struct bmi_emul_data *data; int len; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; /* Empty FIFO frame */ if (frame == NULL) { @@ -584,7 +581,7 @@ static uint8_t bmi_emul_get_frame_len(struct i2c_emul *emul, * data * @param gyr_shift How many bits should be right shifted from gyroscope data */ -static void bmi_emul_set_current_frame(struct i2c_emul *emul, +static void bmi_emul_set_current_frame(const struct emul *emul, struct bmi_emul_frame *frame, bool tag_time, bool header, int acc_shift, int gyr_shift) @@ -592,7 +589,7 @@ static void bmi_emul_set_current_frame(struct i2c_emul *emul, struct bmi_emul_data *data; int i = 0; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->fifo_frame_byte = 0; data->fifo_frame_len = @@ -682,13 +679,13 @@ static void bmi_emul_set_current_frame(struct i2c_emul *emul, * * @param emul Pointer to BMI emulator */ -static void bmi_emul_updata_int_off(struct i2c_emul *emul) +static void bmi_emul_updata_int_off(const struct emul *emul) { struct bmi_emul_data *data; uint16_t gyr_nvm; uint8_t gyr98; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->off_acc_x = bmi_emul_acc_nvm_to_off( data->reg[data->type_data->acc_off_reg]); @@ -716,12 +713,12 @@ static void bmi_emul_updata_int_off(struct i2c_emul *emul) * * @param emul Pointer to BMI emulator */ -static void bmi_emul_restore_nvm(struct i2c_emul *emul) +static void bmi_emul_restore_nvm(const struct emul *emul) { struct bmi_emul_data *data; int i; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; ASSERT(data->type_data->nvm_len <= BMI_EMUL_MAX_NVM_REGS); @@ -734,11 +731,11 @@ static void bmi_emul_restore_nvm(struct i2c_emul *emul) } /** Check description in emul_bmi.h */ -void bmi_emul_flush_fifo(struct i2c_emul *emul, bool tag_time, bool header) +void bmi_emul_flush_fifo(const struct emul *emul, bool tag_time, bool header) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->fifo_skip = 0; data->fifo_frame = NULL; @@ -750,11 +747,11 @@ void bmi_emul_flush_fifo(struct i2c_emul *emul, bool tag_time, bool header) } /** Check description in emul_bmi.h */ -void bmi_emul_reset_common(struct i2c_emul *emul, bool tag_time, bool header) +void bmi_emul_reset_common(const struct emul *emul, bool tag_time, bool header) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; /* Restore registers backed in NVM */ bmi_emul_restore_nvm(emul); @@ -767,21 +764,21 @@ void bmi_emul_reset_common(struct i2c_emul *emul, bool tag_time, bool header) } /** Check description in emul_bmi.h */ -void bmi_emul_set_cmd_end_time(struct i2c_emul *emul, int time) +void bmi_emul_set_cmd_end_time(const struct emul *emul, int time) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->cmd_end_time = k_uptime_get_32() + time; } /** Check description in emul_bmi.h */ -bool bmi_emul_is_cmd_end(struct i2c_emul *emul) +bool bmi_emul_is_cmd_end(const struct emul *emul) { struct bmi_emul_data *data; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; /* We are simulating command execution time and it doesn't expired */ if (data->simulate_command_exec_time && @@ -806,14 +803,14 @@ bool bmi_emul_is_cmd_end(struct i2c_emul *emul) * @return 0 on success * @return -EIO on error */ -static int bmi_emul_handle_write(struct i2c_emul *emul, int reg, uint8_t val, +static int bmi_emul_handle_write(const struct emul *emul, int reg, uint8_t val, int byte) { struct bmi_emul_data *data; uint8_t rsvd_mask; int ret; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; ret = data->type_data->handle_write(data->reg, emul, reg, byte, val); reg = data->type_data->access_reg(emul, reg, byte, false /* = read */); @@ -858,15 +855,15 @@ static int bmi_emul_handle_write(struct i2c_emul *emul, int reg, uint8_t val, } /** Check description in emul_bmi.h */ -void bmi_emul_state_to_reg(struct i2c_emul *emul, int acc_shift, int gyr_shift, - int acc_reg, int gyr_reg, int sensortime_reg, - bool acc_off_en, bool gyr_off_en) +void bmi_emul_state_to_reg(const struct emul *emul, int acc_shift, + int gyr_shift, int acc_reg, int gyr_reg, + int sensortime_reg, bool acc_off_en, bool gyr_off_en) { struct bmi_emul_data *data; int32_t val[3]; int i; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (gyr_off_en) { val[0] = data->gyr_x - data->off_gyr_x; @@ -902,12 +899,13 @@ void bmi_emul_state_to_reg(struct i2c_emul *emul, int acc_shift, int gyr_shift, } /** Check description in emul_bmi.h */ -void bmi_emul_append_frame(struct i2c_emul *emul, struct bmi_emul_frame *frame) +void bmi_emul_append_frame(const struct emul *emul, + struct bmi_emul_frame *frame) { struct bmi_emul_data *data; struct bmi_emul_frame *tmp_frame; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (data->fifo_frame == NULL) { data->fifo_frame = frame; @@ -921,13 +919,13 @@ void bmi_emul_append_frame(struct i2c_emul *emul, struct bmi_emul_frame *frame) } /** Check description in emul_bmi.h */ -uint16_t bmi_emul_fifo_len(struct i2c_emul *emul, bool tag_time, bool header) +uint16_t bmi_emul_fifo_len(const struct emul *emul, bool tag_time, bool header) { struct bmi_emul_frame *frame; struct bmi_emul_data *data; uint16_t len = 0; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (data->fifo_skip != 0 && header) { len += 2; @@ -947,13 +945,13 @@ uint16_t bmi_emul_fifo_len(struct i2c_emul *emul, bool tag_time, bool header) } /** Check description in emul_bmi.h */ -uint8_t bmi_emul_get_fifo_data(struct i2c_emul *emul, int byte, bool tag_time, +uint8_t bmi_emul_get_fifo_data(const struct emul *emul, int byte, bool tag_time, bool header, int acc_shift, int gyr_shift) { struct bmi_emul_data *data; int ret; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (byte == 0) { /* Repeat uncompleated read of frame */ @@ -968,6 +966,7 @@ uint8_t bmi_emul_get_fifo_data(struct i2c_emul *emul, int byte, bool tag_time, if (data->fifo_skip != 0 && byte == 1 && header) { /* Return number of skipped frames */ + ret = data->fifo_skip; data->fifo_skip = 0; @@ -1000,13 +999,13 @@ uint8_t bmi_emul_get_fifo_data(struct i2c_emul *emul, int byte, bool tag_time, * @return 0 on success * @return -EIO on error */ -static int bmi_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *buf, +static int bmi_emul_handle_read(const struct emul *emul, int reg, uint8_t *buf, int byte) { struct bmi_emul_data *data; int ret; - data = BMI_DATA_FROM_I2C_EMUL(emul); + data = emul->data; ret = data->type_data->handle_read(data->reg, emul, reg, byte, buf); reg = data->type_data->access_reg(emul, reg, byte, true /* = read */); @@ -1035,35 +1034,29 @@ static int bmi_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *buf, static int bmi_emul_init(const struct emul *emul, const struct device *parent) { const struct i2c_common_emul_cfg *cfg = emul->cfg; - struct i2c_common_emul_data *data = cfg->data; - struct bmi_emul_data *bmi_data; - int ret; + struct bmi_emul_data *data = emul->data; - data->emul.api = &i2c_common_emul_api; - data->emul.addr = cfg->addr; - data->i2c = parent; - data->cfg = cfg; - i2c_common_emul_init(data); + data->common.emul.addr = cfg->addr; + data->common.emul.target = emul; + data->common.i2c = parent; + data->common.cfg = cfg; + i2c_common_emul_init(&data->common); - bmi_data = CONTAINER_OF(data, struct bmi_emul_data, common); - - switch (bmi_data->type) { + switch (data->type) { case BMI_EMUL_160: - bmi_data->type_data = get_bmi160_emul_type_data(); + data->type_data = get_bmi160_emul_type_data(); break; case BMI_EMUL_260: - bmi_data->type_data = get_bmi260_emul_type_data(); + data->type_data = get_bmi260_emul_type_data(); break; } /* Set callback access_reg to type specific function */ - data->access_reg = bmi_data->type_data->access_reg; - - ret = i2c_emul_register(parent, emul->dev_label, &data->emul); + data->common.access_reg = data->type_data->access_reg; - bmi_data->type_data->reset(bmi_data->reg, &data->emul); + data->type_data->reset(data->reg, emul); - return ret; + return 0; } #define BMI_EMUL(n) \ @@ -1093,16 +1086,16 @@ static int bmi_emul_init(const struct emul *emul, const struct device *parent) .addr = DT_INST_REG_ADDR(n), \ }; \ EMUL_DEFINE(bmi_emul_init, DT_DRV_INST(n), &bmi_emul_cfg_##n, \ - &bmi_emul_data_##n) + &bmi_emul_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(BMI_EMUL) #define BMI_EMUL_CASE(n) \ case DT_INST_DEP_ORD(n): \ - return &bmi_emul_data_##n.common.emul; + return bmi_emul_data_##n.common.emul.target; /** Check description in emul_bmi.h */ -struct i2c_emul *bmi_emul_get(int ord) +const struct emul *bmi_emul_get(int ord) { switch (ord) { DT_INST_FOREACH_STATUS_OKAY(BMI_EMUL_CASE) @@ -1111,3 +1104,11 @@ struct i2c_emul *bmi_emul_get(int ord) return NULL; } } + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_bmi_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/emul_bmi160.c b/zephyr/emul/emul_bmi160.c index cc761fce6e..1912cc42bb 100644 --- a/zephyr/emul/emul_bmi160.c +++ b/zephyr/emul/emul_bmi160.c @@ -182,7 +182,7 @@ static int bmi160_emul_gyr_range_to_shift(uint8_t range) * @param regs Pointer to array of emulator's registers * @param emul Pointer to BMI emulator */ -static void bmi160_emul_reset(uint8_t *regs, struct i2c_emul *emul) +static void bmi160_emul_reset(uint8_t *regs, const struct emul *emul) { bool tag_time; bool header; @@ -349,7 +349,7 @@ static int16_t bmi160_emul_get_acc_target_off(int32_t acc, uint8_t target) * @param regs Pointer to array of emulator's registers * @param emul Pointer to BMI emulator */ -static void bmi160_emul_handle_off_comp(uint8_t *regs, struct i2c_emul *emul) +static void bmi160_emul_handle_off_comp(uint8_t *regs, const struct emul *emul) { uint8_t target; int16_t off; @@ -361,6 +361,7 @@ static void bmi160_emul_handle_off_comp(uint8_t *regs, struct i2c_emul *emul) bmi_emul_set_off(emul, BMI_EMUL_GYR_X, off); val = bmi_emul_get_value(emul, BMI_EMUL_GYR_Y); off = bmi160_emul_get_gyr_target_off(val); + bmi_emul_set_off(emul, BMI_EMUL_GYR_Y, off); val = bmi_emul_get_value(emul, BMI_EMUL_GYR_Z); off = bmi160_emul_get_gyr_target_off(val); @@ -401,7 +402,8 @@ static void bmi160_emul_handle_off_comp(uint8_t *regs, struct i2c_emul *emul) * @return 0 on success * @return -EIO on failure */ -static int bmi160_emul_start_cmd(uint8_t *regs, struct i2c_emul *emul, int cmd) +static int bmi160_emul_start_cmd(uint8_t *regs, const struct emul *emul, + int cmd) { int time; @@ -470,7 +472,7 @@ static int bmi160_emul_start_cmd(uint8_t *regs, struct i2c_emul *emul, int cmd) * @param regs Pointer to array of emulator's registers * @param emul Pointer to BMI emulator */ -static void bmi160_emul_end_cmd(uint8_t *regs, struct i2c_emul *emul) +static void bmi160_emul_end_cmd(uint8_t *regs, const struct emul *emul) { uint8_t pmu_status; bool tag_time; @@ -560,7 +562,7 @@ static void bmi160_emul_end_cmd(uint8_t *regs, struct i2c_emul *emul) * @return BMI_EMUL_ACCESS_E on RO register access * @return -EIO on error */ -static int bmi160_emul_handle_write(uint8_t *regs, struct i2c_emul *emul, +static int bmi160_emul_handle_write(uint8_t *regs, const struct emul *emul, int reg, int byte, uint8_t val) { bool tag_time; @@ -618,7 +620,7 @@ static int bmi160_emul_handle_write(uint8_t *regs, struct i2c_emul *emul, * * @return Currently accessed register */ -static int bmi160_emul_access_reg(struct i2c_emul *emul, int reg, int byte, +static int bmi160_emul_access_reg(const struct emul *emul, int reg, int byte, bool read) { if (!read) { @@ -654,7 +656,7 @@ static int bmi160_emul_access_reg(struct i2c_emul *emul, int reg, int byte, * @return BMI_EMUL_ACCESS_E on WO register access * @return -EIO on other error */ -static int bmi160_emul_handle_read(uint8_t *regs, struct i2c_emul *emul, +static int bmi160_emul_handle_read(uint8_t *regs, const struct emul *emul, int reg, int byte, char *buf) { uint16_t fifo_len; diff --git a/zephyr/emul/emul_bmi260.c b/zephyr/emul/emul_bmi260.c index fe7c048728..9f1158edf6 100644 --- a/zephyr/emul/emul_bmi260.c +++ b/zephyr/emul/emul_bmi260.c @@ -123,7 +123,7 @@ static const uint8_t bmi_emul_260_rsvd_mask[] = { * @param regs Pointer to array of emulator's registers * @param emul Pointer to BMI emulator */ -static void bmi260_emul_reset(uint8_t *regs, struct i2c_emul *emul) +static void bmi260_emul_reset(uint8_t *regs, const struct emul *emul) { bool tag_time; bool header; @@ -278,7 +278,8 @@ static int bmi260_emul_gyr_range_to_shift(uint8_t range) * @return 0 on success * @return -EIO on failure */ -static int bmi260_emul_start_cmd(uint8_t *regs, struct i2c_emul *emul, int cmd) +static int bmi260_emul_start_cmd(uint8_t *regs, const struct emul *emul, + int cmd) { int time; @@ -306,7 +307,7 @@ static int bmi260_emul_start_cmd(uint8_t *regs, struct i2c_emul *emul, int cmd) * @param regs Pointer to array of emulator's registers * @param emul Pointer to BMI emulator */ -static void bmi260_emul_end_cmd(uint8_t *regs, struct i2c_emul *emul) +static void bmi260_emul_end_cmd(uint8_t *regs, const struct emul *emul) { bool tag_time; bool header; @@ -339,7 +340,7 @@ static void bmi260_emul_end_cmd(uint8_t *regs, struct i2c_emul *emul) * * @return Currently accessed register */ -static int bmi260_emul_access_reg(struct i2c_emul *emul, int reg, int byte, +static int bmi260_emul_access_reg(const struct emul *emul, int reg, int byte, bool read) { /* Ignore first byte which sets starting register */ @@ -380,7 +381,7 @@ static int bmi260_emul_access_reg(struct i2c_emul *emul, int reg, int byte, * @return BMI_EMUL_ACCESS_E on RO register access * @return -EIO on error */ -static int bmi260_emul_handle_write(uint8_t *regs, struct i2c_emul *emul, +static int bmi260_emul_handle_write(uint8_t *regs, const struct emul *emul, int reg, int byte, uint8_t val) { uint8_t mask; @@ -452,7 +453,7 @@ static int bmi260_emul_handle_write(uint8_t *regs, struct i2c_emul *emul, * @return BMI_EMUL_ACCESS_E on WO register access * @return -EIO on other error */ -static int bmi260_emul_handle_read(uint8_t *regs, struct i2c_emul *emul, +static int bmi260_emul_handle_read(uint8_t *regs, const struct emul *emul, int reg, int byte, char *buf) { uint16_t fifo_len; diff --git a/zephyr/emul/emul_common_i2c.c b/zephyr/emul/emul_common_i2c.c index 01353310a2..f246a97835 100644 --- a/zephyr/emul/emul_common_i2c.c +++ b/zephyr/emul/emul_common_i2c.c @@ -15,63 +15,46 @@ LOG_MODULE_REGISTER(emul_common_i2c); #include "emul/emul_common_i2c.h" /** Check description in emul_common_i2c.h */ -int i2c_common_emul_lock_data(struct i2c_emul *emul, k_timeout_t timeout) +int i2c_common_emul_lock_data(struct i2c_common_emul_data *common_data, + k_timeout_t timeout) { - struct i2c_common_emul_data *data; - - data = CONTAINER_OF(emul, struct i2c_common_emul_data, emul); - - return k_mutex_lock(&data->data_mtx, timeout); + return k_mutex_lock(&common_data->data_mtx, timeout); } /** Check description in emul_common_i2c.h */ -int i2c_common_emul_unlock_data(struct i2c_emul *emul) +int i2c_common_emul_unlock_data(struct i2c_common_emul_data *common_data) { - struct i2c_common_emul_data *data; - - data = CONTAINER_OF(emul, struct i2c_common_emul_data, emul); - - return k_mutex_unlock(&data->data_mtx); + return k_mutex_unlock(&common_data->data_mtx); } /** Check description in emul_common_i2c.h */ -void i2c_common_emul_set_write_func(struct i2c_emul *emul, +void i2c_common_emul_set_write_func(struct i2c_common_emul_data *common_data, i2c_common_emul_write_func func, void *data) { - struct i2c_common_emul_data *emul_data; - - emul_data = CONTAINER_OF(emul, struct i2c_common_emul_data, emul); - emul_data->write_func = func; - emul_data->write_func_data = data; + common_data->write_func = func; + common_data->write_func_data = data; } /** Check description in emul_common_i2c.h */ -void i2c_common_emul_set_read_func(struct i2c_emul *emul, +void i2c_common_emul_set_read_func(struct i2c_common_emul_data *common_data, i2c_common_emul_read_func func, void *data) { - struct i2c_common_emul_data *emul_data; - - emul_data = CONTAINER_OF(emul, struct i2c_common_emul_data, emul); - emul_data->read_func = func; - emul_data->read_func_data = data; + common_data->read_func = func; + common_data->read_func_data = data; } /** Check description in emul_common_i2c.h */ -void i2c_common_emul_set_read_fail_reg(struct i2c_emul *emul, int reg) +void i2c_common_emul_set_read_fail_reg(struct i2c_common_emul_data *common_data, + int reg) { - struct i2c_common_emul_data *data; - - data = CONTAINER_OF(emul, struct i2c_common_emul_data, emul); - data->read_fail_reg = reg; + common_data->read_fail_reg = reg; } /** Check description in emul_common_i2c.h */ -void i2c_common_emul_set_write_fail_reg(struct i2c_emul *emul, int reg) +void i2c_common_emul_set_write_fail_reg( + struct i2c_common_emul_data *common_data, int reg) { - struct i2c_common_emul_data *data; - - data = CONTAINER_OF(emul, struct i2c_common_emul_data, emul); - data->write_fail_reg = reg; + common_data->write_fail_reg = reg; } /** @@ -83,7 +66,7 @@ void i2c_common_emul_set_write_fail_reg(struct i2c_emul *emul, int reg) * * @retval start_write emulator callback return code */ -static int i2c_common_emul_start_write(struct i2c_emul *emul, +static int i2c_common_emul_start_write(const struct emul *target, struct i2c_common_emul_data *data) { int ret = 0; @@ -92,7 +75,7 @@ static int i2c_common_emul_start_write(struct i2c_emul *emul, if (data->start_write) { k_mutex_lock(&data->data_mtx, K_FOREVER); - ret = data->start_write(emul, data->cur_reg); + ret = data->start_write(target, data->cur_reg); k_mutex_unlock(&data->data_mtx); } @@ -108,14 +91,14 @@ static int i2c_common_emul_start_write(struct i2c_emul *emul, * * @retval finish_write emulator callback return code */ -static int i2c_common_emul_finish_write(struct i2c_emul *emul, +static int i2c_common_emul_finish_write(const struct emul *target, struct i2c_common_emul_data *data) { int ret = 0; if (data->finish_write) { k_mutex_lock(&data->data_mtx, K_FOREVER); - ret = data->finish_write(emul, data->cur_reg, data->msg_byte); + ret = data->finish_write(target, data->cur_reg, data->msg_byte); k_mutex_unlock(&data->data_mtx); } @@ -131,7 +114,7 @@ static int i2c_common_emul_finish_write(struct i2c_emul *emul, * * @retval start_read emulator callback return code */ -static int i2c_common_emul_start_read(struct i2c_emul *emul, +static int i2c_common_emul_start_read(const struct emul *target, struct i2c_common_emul_data *data) { int ret = 0; @@ -140,7 +123,7 @@ static int i2c_common_emul_start_read(struct i2c_emul *emul, if (data->start_read) { k_mutex_lock(&data->data_mtx, K_FOREVER); - ret = data->start_read(emul, data->cur_reg); + ret = data->start_read(target, data->cur_reg); k_mutex_unlock(&data->data_mtx); } @@ -156,14 +139,14 @@ static int i2c_common_emul_start_read(struct i2c_emul *emul, * * @retval finish_read emulator callback return code */ -static int i2c_common_emul_finish_read(struct i2c_emul *emul, +static int i2c_common_emul_finish_read(const struct emul *target, struct i2c_common_emul_data *data) { int ret = 0; if (data->finish_read) { k_mutex_lock(&data->data_mtx, K_FOREVER); - ret = data->finish_read(emul, data->cur_reg, data->msg_byte); + ret = data->finish_read(target, data->cur_reg, data->msg_byte); k_mutex_unlock(&data->data_mtx); } @@ -183,7 +166,7 @@ static int i2c_common_emul_finish_read(struct i2c_emul *emul, * @retval 0 If successful * @retval -EIO General input / output error */ -static int i2c_common_emul_write_byte(struct i2c_emul *emul, +static int i2c_common_emul_write_byte(const struct emul *target, struct i2c_common_emul_data *data, uint8_t val) { @@ -191,8 +174,8 @@ static int i2c_common_emul_write_byte(struct i2c_emul *emul, /* Custom user handler */ if (data->write_func) { - ret = data->write_func(emul, data->cur_reg, val, data->msg_byte, - data->write_func_data); + ret = data->write_func(target, data->cur_reg, val, + data->msg_byte, data->write_func_data); if (ret < 0) { return -EIO; } else if (ret == 0) { @@ -201,7 +184,7 @@ static int i2c_common_emul_write_byte(struct i2c_emul *emul, } /* Check if user wants to fail on accessed register */ if (data->access_reg) { - reg = data->access_reg(emul, data->cur_reg, data->msg_byte, + reg = data->access_reg(target, data->cur_reg, data->msg_byte, false /* = read */); } else { /* Ignore first (register address) byte */ @@ -215,7 +198,7 @@ static int i2c_common_emul_write_byte(struct i2c_emul *emul, /* Emulator handler */ if (data->write_byte) { k_mutex_lock(&data->data_mtx, K_FOREVER); - ret = data->write_byte(emul, data->cur_reg, val, + ret = data->write_byte(target, data->cur_reg, val, data->msg_byte); k_mutex_unlock(&data->data_mtx); if (ret) { @@ -239,7 +222,7 @@ static int i2c_common_emul_write_byte(struct i2c_emul *emul, * @retval 0 If successful * @retval -EIO General input / output error */ -static int i2c_common_emul_read_byte(struct i2c_emul *emul, +static int i2c_common_emul_read_byte(const struct emul *target, struct i2c_common_emul_data *data, uint8_t *val) { @@ -247,8 +230,8 @@ static int i2c_common_emul_read_byte(struct i2c_emul *emul, /* Custom user handler */ if (data->read_func) { - ret = data->read_func(emul, data->cur_reg, val, data->msg_byte, - data->read_func_data); + ret = data->read_func(target, data->cur_reg, val, + data->msg_byte, data->read_func_data); if (ret < 0) { return -EIO; } else if (ret == 0) { @@ -257,7 +240,7 @@ static int i2c_common_emul_read_byte(struct i2c_emul *emul, } /* Check if user wants to fail on accessed register */ if (data->access_reg) { - reg = data->access_reg(emul, data->cur_reg, data->msg_byte, + reg = data->access_reg(target, data->cur_reg, data->msg_byte, true /* = read */); } else { reg = data->cur_reg + data->msg_byte; @@ -270,7 +253,8 @@ static int i2c_common_emul_read_byte(struct i2c_emul *emul, /* Emulator handler */ if (data->read_byte) { k_mutex_lock(&data->data_mtx, K_FOREVER); - ret = data->read_byte(emul, data->cur_reg, val, data->msg_byte); + ret = data->read_byte(target, data->cur_reg, val, + data->msg_byte); k_mutex_unlock(&data->data_mtx); if (ret) { return -EIO; @@ -281,17 +265,15 @@ static int i2c_common_emul_read_byte(struct i2c_emul *emul, } /** Check description in emul_common_i2c.h */ -int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, - int num_msgs, int addr) +int i2c_common_emul_transfer_workhorse(const struct emul *target, + struct i2c_common_emul_data *data, + const struct i2c_common_emul_cfg *cfg, + struct i2c_msg *msgs, int num_msgs, + int addr) { - const struct i2c_common_emul_cfg *cfg; - struct i2c_common_emul_data *data; bool read, stop; int ret, i; - data = CONTAINER_OF(emul, struct i2c_common_emul_data, emul); - cfg = data->cfg; - if (cfg->addr != addr) { LOG_ERR("Address mismatch, expected %02x, got %02x", cfg->addr, addr); @@ -308,11 +290,12 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, case I2C_COMMON_EMUL_IN_WRITE: if (read) { data->msg_state = I2C_COMMON_EMUL_NONE_MSG; - ret = i2c_common_emul_finish_write(emul, data); + ret = i2c_common_emul_finish_write(target, + data); if (ret) { return ret; } - ret = i2c_common_emul_start_read(emul, data); + ret = i2c_common_emul_start_read(target, data); if (ret) { return ret; } @@ -321,7 +304,7 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, case I2C_COMMON_EMUL_IN_READ: if (!read) { data->msg_state = I2C_COMMON_EMUL_NONE_MSG; - ret = i2c_common_emul_finish_read(emul, data); + ret = i2c_common_emul_finish_read(target, data); if (ret) { return ret; } @@ -331,7 +314,7 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, } /* Dispatch command/register address */ data->cur_reg = msgs->buf[0]; - ret = i2c_common_emul_start_write(emul, data); + ret = i2c_common_emul_start_write(target, data); if (ret) { return ret; } @@ -339,7 +322,7 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, break; case I2C_COMMON_EMUL_NONE_MSG: if (read) { - ret = i2c_common_emul_start_read(emul, data); + ret = i2c_common_emul_start_read(target, data); if (ret) { return ret; } @@ -350,7 +333,7 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, } /* Dispatch command/register address */ data->cur_reg = msgs->buf[0]; - ret = i2c_common_emul_start_write(emul, data); + ret = i2c_common_emul_start_write(target, data); if (ret) { return ret; } @@ -379,7 +362,7 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, } /* Dispatch write command */ for (; i < msgs->len; i++, data->msg_byte++) { - ret = i2c_common_emul_write_byte(emul, data, + ret = i2c_common_emul_write_byte(target, data, msgs->buf[i]); if (ret) { return ret; @@ -387,7 +370,8 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, } /* Finish write command */ if (stop) { - ret = i2c_common_emul_finish_write(emul, data); + ret = i2c_common_emul_finish_write(target, + data); if (ret) { return ret; } @@ -396,7 +380,7 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, /* Dispatch read command */ for (i = 0; i < msgs->len; i++, data->msg_byte++) { ret = i2c_common_emul_read_byte( - emul, data, &(msgs->buf[i])); + target, data, &(msgs->buf[i])); if (ret) { return ret; } @@ -404,7 +388,7 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, /* Finish read command */ if (stop) { - ret = i2c_common_emul_finish_read(emul, data); + ret = i2c_common_emul_finish_read(target, data); if (ret) { return ret; } @@ -416,6 +400,20 @@ int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, } /** Check description in emul_common_i2c.h */ +int i2c_common_emul_transfer(const struct emul *target, struct i2c_msg *msgs, + int num_msgs, int addr) +{ + const struct i2c_common_emul_cfg *cfg; + struct i2c_common_emul_data *data; + + data = target->data; + cfg = target->cfg; + + return i2c_common_emul_transfer_workhorse(target, data, cfg, msgs, + num_msgs, addr); +} + +/** Check description in emul_common_i2c.h */ void i2c_common_emul_init(struct i2c_common_emul_data *data) { data->msg_state = I2C_COMMON_EMUL_NONE_MSG; @@ -426,7 +424,6 @@ void i2c_common_emul_init(struct i2c_common_emul_data *data) data->read_func = NULL; data->write_fail_reg = I2C_COMMON_EMUL_NO_FAIL_REG; - data->read_fail_reg = I2C_COMMON_EMUL_NO_FAIL_REG; k_mutex_init(&data->data_mtx); } @@ -434,3 +431,14 @@ void i2c_common_emul_init(struct i2c_common_emul_data *data) struct i2c_emul_api i2c_common_emul_api = { .transfer = i2c_common_emul_transfer, }; + +static int i2c_common_emul_transfer_noop(const struct emul *target, + struct i2c_msg *msgs, int num_msgs, + int addr) +{ + return 0; +} + +struct i2c_emul_api i2c_common_emul_noop = { + .transfer = i2c_common_emul_transfer_noop, +}; diff --git a/zephyr/emul/emul_isl923x.c b/zephyr/emul/emul_isl923x.c index 8a49f569dc..2787c08c5c 100644 --- a/zephyr/emul/emul_isl923x.c +++ b/zephyr/emul/emul_isl923x.c @@ -19,14 +19,11 @@ #include "emul/emul_isl923x.h" #include "emul/emul_smart_battery.h" #include "i2c.h" +#include "emul/emul_stub_device.h" #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(isl923x_emul, CONFIG_ISL923X_EMUL_LOG_LEVEL); -#define ISL923X_DATA_FROM_I2C_EMUL(_emul) \ - CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \ - struct isl923x_emul_data, common) - /** Mask used for the charge current register */ #define REG_CHG_CURRENT_MASK GENMASK(12, 2) @@ -214,10 +211,10 @@ void raa489000_emul_set_acok_pin(const struct emul *emulator, uint16_t value) break; \ } while (0) -static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, - int bytes) +static int isl923x_emul_read_byte(const struct emul *emul, int reg, + uint8_t *val, int bytes) { - struct isl923x_emul_data *data = ISL923X_DATA_FROM_I2C_EMUL(emul); + struct isl923x_emul_data *data = emul->data; switch (reg) { case ISL923X_REG_CHG_CURRENT: @@ -282,12 +279,12 @@ static int isl923x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, return 0; } -uint16_t isl923x_emul_peek_reg(struct i2c_emul *i2c_emul, int reg) +uint16_t isl923x_emul_peek_reg(const struct emul *emul, int reg) { uint8_t bytes[2]; - isl923x_emul_read_byte(i2c_emul, reg, &bytes[0], 0); - isl923x_emul_read_byte(i2c_emul, reg, &bytes[1], 1); + isl923x_emul_read_byte(emul, reg, &bytes[0], 0); + isl923x_emul_read_byte(emul, reg, &bytes[1], 1); return bytes[1] << 8 | bytes[0]; } @@ -302,10 +299,10 @@ uint16_t isl923x_emul_peek_reg(struct i2c_emul *i2c_emul, int reg) (REG) |= ((VAL) << 8) & (MASK); \ } while (0) -static int isl923x_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, - int bytes) +static int isl923x_emul_write_byte(const struct emul *emul, int reg, + uint8_t val, int bytes) { - struct isl923x_emul_data *data = ISL923X_DATA_FROM_I2C_EMUL(emul); + struct isl923x_emul_data *data = emul->data; switch (reg) { case ISL923X_REG_CHG_CURRENT: @@ -372,13 +369,14 @@ static int isl923x_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, reg); return -EINVAL; } + return 0; } -static int isl923x_emul_finish_write(struct i2c_emul *emul, int reg, int bytes) +static int isl923x_emul_finish_write(const struct emul *emul, int reg, + int bytes) { - struct isl923x_emul_data *data = ISL923X_DATA_FROM_I2C_EMUL(emul); - struct i2c_emul *battery_i2c_emul; + struct isl923x_emul_data *data = emul->data; struct sbat_emul_bat_data *bat; int16_t current; @@ -386,9 +384,10 @@ static int isl923x_emul_finish_write(struct i2c_emul *emul, int reg, int bytes) case ISL923X_REG_CHG_CURRENT: /* Write current to battery. */ if (data->battery_ord >= 0) { - battery_i2c_emul = sbat_emul_get_ptr(data->battery_ord); - if (battery_i2c_emul != NULL) { - bat = sbat_emul_get_bat_data(battery_i2c_emul); + const struct emul *battery_emul = + sbat_emul_get_ptr(data->battery_ord); + if (battery_emul != NULL) { + bat = sbat_emul_get_bat_data(battery_emul); if (bat != NULL) { current = REG_TO_CURRENT( data->current_limit_reg); @@ -412,12 +411,12 @@ static int emul_isl923x_init(const struct emul *emul, data->common.emul.api = &i2c_common_emul_api; data->common.emul.addr = cfg->common.addr; - data->common.emul.parent = emul; + data->common.emul.target = emul; data->common.i2c = parent; data->common.cfg = &cfg->common; i2c_common_emul_init(&data->common); - return i2c_emul_register(parent, emul->dev_label, &data->common.emul); + return 0; } #define INIT_ISL923X(n) \ @@ -440,7 +439,7 @@ static int emul_isl923x_init(const struct emul *emul, }, \ }; \ EMUL_DEFINE(emul_isl923x_init, DT_DRV_INST(n), &isl923x_emul_cfg_##n, \ - &isl923x_emul_data_##n) + &isl923x_emul_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(INIT_ISL923X) @@ -459,3 +458,11 @@ static void emul_isl923x_reset_before(const struct ztest_unit_test *test, } ZTEST_RULE(emul_isl923x_reset, emul_isl923x_reset_before, NULL); #endif /* CONFIG_ZTEST_NEW_API */ + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_isl923x_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/emul_lis2dw12.c b/zephyr/emul/emul_lis2dw12.c index b0e85f50b5..8ada7845bf 100644 --- a/zephyr/emul/emul_lis2dw12.c +++ b/zephyr/emul/emul_lis2dw12.c @@ -16,14 +16,11 @@ #include "emul/emul_common_i2c.h" #include "emul/emul_lis2dw12.h" #include "i2c.h" +#include "emul/emul_stub_device.h" #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(lis2dw12_emul, CONFIG_LIS2DW12_EMUL_LOG_LEVEL); -#define LIS2DW12_DATA_FROM_I2C_EMUL(_emul) \ - CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \ - struct lis2dw12_emul_data, common) - struct lis2dw12_emul_data { /** Common I2C data */ struct i2c_common_emul_data common; @@ -50,24 +47,16 @@ struct lis2dw12_emul_cfg { struct i2c_common_emul_cfg common; }; -struct i2c_emul *lis2dw12_emul_to_i2c_emul(const struct emul *emul) -{ - struct lis2dw12_emul_data *data = emul->data; - - return &(data->common.emul); -} - void lis2dw12_emul_reset(const struct emul *emul) { struct lis2dw12_emul_data *data = emul->data; - struct i2c_emul *i2c_emul = lis2dw12_emul_to_i2c_emul(emul); - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(&data->common, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(&data->common, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(&data->common, NULL, NULL); + i2c_common_emul_set_write_func(&data->common, NULL, NULL); data->who_am_i_reg = LIS2DW12_WHO_AM_I; data->ctrl1_reg = 0; data->ctrl2_reg = 0; @@ -93,10 +82,10 @@ uint32_t lis2dw12_emul_get_soft_reset_count(const struct emul *emul) return data->soft_reset_count; } -static int lis2dw12_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, - int bytes) +static int lis2dw12_emul_read_byte(const struct emul *emul, int reg, + uint8_t *val, int bytes) { - struct lis2dw12_emul_data *data = LIS2DW12_DATA_FROM_I2C_EMUL(emul); + struct lis2dw12_emul_data *data = emul->data; switch (reg) { case LIS2DW12_WHO_AM_I_REG: @@ -158,7 +147,7 @@ static int lis2dw12_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, return 0; } -uint8_t lis2dw12_emul_peek_reg(struct i2c_emul *emul, int reg) +uint8_t lis2dw12_emul_peek_reg(const struct emul *emul, int reg) { __ASSERT(emul, "emul is NULL"); @@ -171,7 +160,7 @@ uint8_t lis2dw12_emul_peek_reg(struct i2c_emul *emul, int reg) return val; } -uint8_t lis2dw12_emul_peek_odr(struct i2c_emul *emul) +uint8_t lis2dw12_emul_peek_odr(const struct emul *emul) { __ASSERT(emul, "emul is NULL"); @@ -181,7 +170,7 @@ uint8_t lis2dw12_emul_peek_odr(struct i2c_emul *emul) __builtin_ctz(LIS2DW12_ACC_ODR_MASK); } -uint8_t lis2dw12_emul_peek_mode(struct i2c_emul *emul) +uint8_t lis2dw12_emul_peek_mode(const struct emul *emul) { __ASSERT(emul, "emul is NULL"); @@ -191,7 +180,7 @@ uint8_t lis2dw12_emul_peek_mode(struct i2c_emul *emul) __builtin_ctz(LIS2DW12_ACC_MODE_MASK); } -uint8_t lis2dw12_emul_peek_lpmode(struct i2c_emul *emul) +uint8_t lis2dw12_emul_peek_lpmode(const struct emul *emul) { __ASSERT(emul, "emul is NULL"); @@ -200,10 +189,10 @@ uint8_t lis2dw12_emul_peek_lpmode(struct i2c_emul *emul) return (reg & LIS2DW12_ACC_LPMODE_MASK); } -static int lis2dw12_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, - int bytes) +static int lis2dw12_emul_write_byte(const struct emul *emul, int reg, + uint8_t val, int bytes) { - struct lis2dw12_emul_data *data = LIS2DW12_DATA_FROM_I2C_EMUL(emul); + struct lis2dw12_emul_data *data = emul->data; switch (reg) { case LIS2DW12_WHO_AM_I_REG: @@ -254,14 +243,13 @@ static int emul_lis2dw12_init(const struct emul *emul, const struct i2c_common_emul_cfg *cfg = &(lis2dw12_cfg->common); struct lis2dw12_emul_data *data = emul->data; - data->common.emul.api = &i2c_common_emul_api; data->common.emul.addr = cfg->addr; - data->common.emul.parent = emul; + data->common.emul.target = emul; data->common.i2c = parent; data->common.cfg = cfg; i2c_common_emul_init(&data->common); - return i2c_emul_register(parent, emul->dev_label, &data->common.emul); + return 0; } int lis2dw12_emul_set_accel_reading(const struct emul *emul, intv3_t reading) @@ -310,6 +298,14 @@ void lis2dw12_emul_clear_accel_reading(const struct emul *emul) }, \ }; \ EMUL_DEFINE(emul_lis2dw12_init, DT_DRV_INST(n), \ - &lis2dw12_emul_cfg_##n, &lis2dw12_emul_data_##n) + &lis2dw12_emul_cfg_##n, &lis2dw12_emul_data_##n, \ + &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(INIT_LIS2DW12) +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_lis2dw12_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/emul_ln9310.c b/zephyr/emul/emul_ln9310.c index 1c21c5a0b0..7903d0fe3f 100644 --- a/zephyr/emul/emul_ln9310.c +++ b/zephyr/emul/emul_ln9310.c @@ -5,27 +5,26 @@ #define DT_DRV_COMPAT cros_ln9310_emul +#include <errno.h> #include <zephyr/device.h> #include <zephyr/devicetree/gpio.h> +#include <zephyr/drivers/emul.h> +#include <zephyr/drivers/gpio.h> #include <zephyr/drivers/gpio/gpio_emul.h> #include <zephyr/drivers/i2c.h> #include <zephyr/drivers/i2c_emul.h> -#include <zephyr/drivers/emul.h> -#include <errno.h> #include <zephyr/sys/__assert.h> #include "driver/ln9310.h" #include "emul/emul_common_i2c.h" #include "emul/emul_ln9310.h" +#include "hooks.h" #include "i2c.h" +#include "emul/emul_stub_device.h" #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(ln9310_emul, CONFIG_LN9310_EMUL_LOG_LEVEL); -#define LN9310_DATA_FROM_I2C_EMUL(_emul) \ - CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \ - struct ln9310_emul_data, common) - enum functional_mode { /* TODO shutdown_mode, */ /* TODO bypass, */ @@ -249,20 +248,20 @@ enum battery_cell_type board_get_battery_cell_type(void) return data->battery_cell_type; } -static int ln9310_emul_start_write(struct i2c_emul *emul, int reg) +static int ln9310_emul_start_write(const struct emul *emul, int reg) { return 0; } -static int ln9310_emul_finish_write(struct i2c_emul *emul, int reg, int bytes) +static int ln9310_emul_finish_write(const struct emul *emul, int reg, int bytes) { return 0; } -static int ln9310_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, +static int ln9310_emul_write_byte(const struct emul *emul, int reg, uint8_t val, int bytes) { - struct ln9310_emul_data *data = LN9310_DATA_FROM_I2C_EMUL(emul); + struct ln9310_emul_data *data = emul->data; __ASSERT(bytes == 1, "bytes 0x%x != 0x1 on reg 0x%x", bytes, reg); @@ -350,14 +349,14 @@ static int ln9310_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, return 0; } -static int ln9310_emul_start_read(struct i2c_emul *emul, int reg) +static int ln9310_emul_start_read(const struct emul *emul, int reg) { return 0; } -static int ln9310_emul_finish_read(struct i2c_emul *emul, int reg, int bytes) +static int ln9310_emul_finish_read(const struct emul *emul, int reg, int bytes) { - struct ln9310_emul_data *data = LN9310_DATA_FROM_I2C_EMUL(emul); + struct ln9310_emul_data *data = emul->data; switch (reg) { case LN9310_REG_INT1: @@ -368,10 +367,10 @@ static int ln9310_emul_finish_read(struct i2c_emul *emul, int reg, int bytes) return 0; } -static int ln9310_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, +static int ln9310_emul_read_byte(const struct emul *emul, int reg, uint8_t *val, int bytes) { - struct ln9310_emul_data *data = LN9310_DATA_FROM_I2C_EMUL(emul); + struct ln9310_emul_data *data = emul->data; __ASSERT(bytes == 0, "bytes 0x%x != 0x0 on reg 0x%x", bytes, reg); @@ -457,7 +456,7 @@ static int ln9310_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, return 0; } -static int ln9310_emul_access_reg(struct i2c_emul *emul, int reg, int bytes, +static int ln9310_emul_access_reg(const struct emul *emul, int reg, int bytes, bool read) { return reg; @@ -469,16 +468,15 @@ static int emul_ln9310_init(const struct emul *emul, const struct i2c_common_emul_cfg *cfg = emul->cfg; struct ln9310_emul_data *data = emul->data; - data->common.emul.api = &i2c_common_emul_api; data->common.emul.addr = cfg->addr; - data->common.emul.parent = emul; + data->common.emul.target = emul; data->common.i2c = parent; data->common.cfg = cfg; i2c_common_emul_init(&data->common); singleton = emul; - return i2c_emul_register(parent, emul->dev_label, &data->common.emul); + return 0; } #define LN9310_GET_GPIO_INT_PORT(n) \ @@ -511,6 +509,13 @@ static int emul_ln9310_init(const struct emul *emul, .addr = DT_INST_REG_ADDR(n), \ }; \ EMUL_DEFINE(emul_ln9310_init, DT_DRV_INST(n), &ln9310_emul_cfg_##n, \ - &ln9310_emul_data_##n) + &ln9310_emul_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(INIT_LN9310) +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_ln9310_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/emul_pi3usb9201.c b/zephyr/emul/emul_pi3usb9201.c index d98db3b3e8..01fdf0b986 100644 --- a/zephyr/emul/emul_pi3usb9201.c +++ b/zephyr/emul/emul_pi3usb9201.c @@ -11,6 +11,8 @@ #include <zephyr/drivers/i2c_emul.h> #include "emul/emul_pi3usb9201.h" +#include "emul/emul_stub_device.h" +#include "emul/emul_common_i2c.h" #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(emul_pi3usb9201, LOG_LEVEL_DBG); @@ -20,6 +22,8 @@ LOG_MODULE_REGISTER(emul_pi3usb9201, LOG_LEVEL_DBG); /** Run-time data used by the emulator */ struct pi3usb9201_emul_data { + /** Common I2C data */ + struct i2c_common_emul_data common; /** I2C emulator detail */ struct i2c_emul emul; /** pi3usb9201 device being emulated */ @@ -40,37 +44,37 @@ struct pi3usb9201_emul_cfg { uint16_t addr; }; -int pi3usb9201_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val) +int pi3usb9201_emul_set_reg(const struct emul *emul, int reg, uint8_t val) { struct pi3usb9201_emul_data *data; if (!EMUL_REG_IS_VALID(reg)) return -EIO; - data = CONTAINER_OF(emul, struct pi3usb9201_emul_data, emul); + data = emul->data; data->reg[reg] = val; return 0; } -int pi3usb9201_emul_get_reg(struct i2c_emul *emul, int reg, uint8_t *val) +int pi3usb9201_emul_get_reg(const struct emul *emul, int reg, uint8_t *val) { struct pi3usb9201_emul_data *data; if (!EMUL_REG_IS_VALID(reg)) return -EIO; - data = CONTAINER_OF(emul, struct pi3usb9201_emul_data, emul); + data = emul->data; *val = data->reg[reg]; return 0; } -static void pi3usb9201_emul_reset(struct i2c_emul *emul) +static void pi3usb9201_emul_reset(const struct emul *emul) { struct pi3usb9201_emul_data *data; - data = CONTAINER_OF(emul, struct pi3usb9201_emul_data, emul); + data = emul->data; data->reg[PI3USB9201_REG_CTRL_1] = 0; data->reg[PI3USB9201_REG_CTRL_2] = 0; @@ -91,13 +95,14 @@ static void pi3usb9201_emul_reset(struct i2c_emul *emul) * @retval 0 If successful * @retval -EIO General input / output error */ -static int pi3usb9201_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, - int num_msgs, int addr) +static int pi3usb9201_emul_transfer(const struct emul *emul, + struct i2c_msg *msgs, int num_msgs, + int addr) { const struct pi3usb9201_emul_cfg *cfg; struct pi3usb9201_emul_data *data; - data = CONTAINER_OF(emul, struct pi3usb9201_emul_data, emul); + data = emul->data; cfg = data->cfg; if (cfg->addr != addr) { @@ -154,18 +159,16 @@ static int pi3usb9201_emul_init(const struct emul *emul, { const struct pi3usb9201_emul_cfg *cfg = emul->cfg; struct pi3usb9201_emul_data *data = cfg->data; - int ret; data->emul.api = &pi3usb9201_emul_api; data->emul.addr = cfg->addr; + data->emul.target = emul; data->i2c = parent; data->cfg = cfg; - ret = i2c_emul_register(parent, emul->dev_label, &data->emul); + pi3usb9201_emul_reset(emul); - pi3usb9201_emul_reset(&data->emul); - - return ret; + return 0; } #define PI3USB9201_EMUL(n) \ @@ -176,15 +179,16 @@ static int pi3usb9201_emul_init(const struct emul *emul, .addr = DT_INST_REG_ADDR(n), \ }; \ EMUL_DEFINE(pi3usb9201_emul_init, DT_DRV_INST(n), \ - &pi3usb9201_emul_cfg_##n, &pi3usb9201_emul_data_##n) + &pi3usb9201_emul_cfg_##n, &pi3usb9201_emul_data_##n, \ + &pi3usb9201_emul_api) DT_INST_FOREACH_STATUS_OKAY(PI3USB9201_EMUL) #define PI3USB9201_EMUL_CASE(n) \ case DT_INST_DEP_ORD(n): \ - return &pi3usb9201_emul_data_##n.emul; + return pi3usb9201_emul_data_##n.emul.target; -struct i2c_emul *pi3usb9201_emul_get(int ord) +const struct emul *pi3usb9201_emul_get(int ord) { switch (ord) { DT_INST_FOREACH_STATUS_OKAY(PI3USB9201_EMUL_CASE) @@ -193,3 +197,5 @@ struct i2c_emul *pi3usb9201_emul_get(int ord) return NULL; } } + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); diff --git a/zephyr/emul/emul_smart_battery.c b/zephyr/emul/emul_smart_battery.c index fd20997049..41d22c4924 100644 --- a/zephyr/emul/emul_smart_battery.c +++ b/zephyr/emul/emul_smart_battery.c @@ -20,10 +20,7 @@ LOG_MODULE_REGISTER(smart_battery); #include "crc8.h" #include "battery_smart.h" - -#define SBAT_DATA_FROM_I2C_EMUL(_emul) \ - CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \ - struct sbat_emul_data, common) +#include "emul/emul_stub_device.h" /** Run-time data used by the emulator */ struct sbat_emul_data { @@ -41,11 +38,11 @@ struct sbat_emul_data { }; /** Check description in emul_smart_battery.h */ -struct sbat_emul_bat_data *sbat_emul_get_bat_data(struct i2c_emul *emul) +struct sbat_emul_bat_data *sbat_emul_get_bat_data(const struct emul *emul) { struct sbat_emul_data *data; - data = SBAT_DATA_FROM_I2C_EMUL(emul); + data = emul->data; return &data->bat; } @@ -275,14 +272,12 @@ static int sbat_emul_read_at_rate_ok(struct sbat_emul_bat_data *bat, * * @return value which equals to computed status register */ -static uint16_t sbat_emul_read_status(struct i2c_emul *emul) +static uint16_t sbat_emul_read_status(const struct emul *emul) { uint16_t status, cap, rem_time, charge_percent; struct sbat_emul_bat_data *bat; - struct sbat_emul_data *data; - data = SBAT_DATA_FROM_I2C_EMUL(emul); - bat = &data->bat; + bat = sbat_emul_get_bat_data(emul); status = bat->status; @@ -330,15 +325,13 @@ static uint16_t sbat_emul_read_status(struct i2c_emul *emul) } /** Check description in emul_smart_battery.h */ -int sbat_emul_get_word_val(struct i2c_emul *emul, int cmd, uint16_t *val) +int sbat_emul_get_word_val(const struct emul *emul, int cmd, uint16_t *val) { struct sbat_emul_bat_data *bat; - struct sbat_emul_data *data; int mode_mw; int rate; - data = SBAT_DATA_FROM_I2C_EMUL(emul); - bat = &data->bat; + bat = sbat_emul_get_bat_data(emul); mode_mw = bat->mode & MODE_CAPACITY; switch (cmd) { @@ -467,13 +460,13 @@ int sbat_emul_get_word_val(struct i2c_emul *emul, int cmd, uint16_t *val) } /** Check description in emul_smart_battery.h */ -int sbat_emul_get_block_data(struct i2c_emul *emul, int cmd, uint8_t **blk, +int sbat_emul_get_block_data(const struct emul *emul, int cmd, uint8_t **blk, int *len) { struct sbat_emul_bat_data *bat; struct sbat_emul_data *data; - data = SBAT_DATA_FROM_I2C_EMUL(emul); + data = emul->data; bat = &data->bat; switch (cmd) { @@ -505,13 +498,15 @@ int sbat_emul_get_block_data(struct i2c_emul *emul, int cmd, uint8_t **blk, * @param data Pointer to smart battery emulator data * @param cmd Command for which PEC is calculated */ -static void sbat_emul_append_pec(struct sbat_emul_data *data, int cmd) +static void sbat_emul_append_pec(const struct emul *emul, int cmd) { uint8_t pec; + struct sbat_emul_data *data = emul->data; + const struct i2c_common_emul_cfg *cfg = emul->cfg; if (BATTERY_SPEC_VERSION(data->bat.spec_info) == BATTERY_SPEC_VER_1_1_WITH_PEC) { - pec = sbat_emul_pec_head(data->common.cfg->addr, 1, cmd); + pec = sbat_emul_pec_head(cfg->addr, 1, cmd); pec = cros_crc8_arg(data->msg_buf, data->num_to_read, pec); data->msg_buf[data->num_to_read] = pec; data->num_to_read++; @@ -519,12 +514,12 @@ static void sbat_emul_append_pec(struct sbat_emul_data *data, int cmd) } /** Check description in emul_smart_battery.h */ -void sbat_emul_set_response(struct i2c_emul *emul, int cmd, uint8_t *buf, +void sbat_emul_set_response(const struct emul *emul, int cmd, uint8_t *buf, int len, bool fail) { struct sbat_emul_data *data; - data = SBAT_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (fail) { data->bat.error_code = STATUS_CODE_UNKNOWN_ERROR; @@ -535,7 +530,7 @@ void sbat_emul_set_response(struct i2c_emul *emul, int cmd, uint8_t *buf, data->num_to_read = MIN(len, MSG_BUF_LEN - 1); memcpy(data->msg_buf, buf, data->num_to_read); data->bat.error_code = STATUS_CODE_OK; - sbat_emul_append_pec(data, cmd); + sbat_emul_append_pec(emul, cmd); } /** @@ -553,14 +548,14 @@ void sbat_emul_set_response(struct i2c_emul *emul, int cmd, uint8_t *buf, * @return 0 on success * @return -EIO on error */ -static int sbat_emul_handle_read_msg(struct i2c_emul *emul, int reg) +static int sbat_emul_handle_read_msg(const struct emul *emul, int reg) { struct sbat_emul_data *data; uint16_t word; uint8_t *blk; int ret, len; - data = SBAT_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (data->cur_cmd == SBAT_EMUL_NO_CMD) { /* Unexpected read message without preceding command select */ @@ -580,7 +575,7 @@ static int sbat_emul_handle_read_msg(struct i2c_emul *emul, int reg) data->msg_buf[0] = word & 0xff; data->msg_buf[1] = (word >> 8) & 0xff; data->bat.error_code = STATUS_CODE_OK; - sbat_emul_append_pec(data, reg); + sbat_emul_append_pec(emul, reg); return 0; } @@ -595,7 +590,7 @@ static int sbat_emul_handle_read_msg(struct i2c_emul *emul, int reg) data->msg_buf[0] = len; memcpy(&data->msg_buf[1], blk, len); data->bat.error_code = STATUS_CODE_OK; - sbat_emul_append_pec(data, reg); + sbat_emul_append_pec(emul, reg); return 0; } @@ -616,7 +611,7 @@ static int sbat_emul_handle_read_msg(struct i2c_emul *emul, int reg) * @return 0 on success * @return -EIO on error */ -static int sbat_emul_finalize_write_msg(struct i2c_emul *emul, int reg, +static int sbat_emul_finalize_write_msg(const struct emul *emul, int reg, int bytes) { struct sbat_emul_bat_data *bat; @@ -624,7 +619,7 @@ static int sbat_emul_finalize_write_msg(struct i2c_emul *emul, int reg, uint16_t word; uint8_t pec; - data = SBAT_DATA_FROM_I2C_EMUL(emul); + data = emul->data; bat = &data->bat; /* @@ -710,12 +705,12 @@ static int sbat_emul_finalize_write_msg(struct i2c_emul *emul, int reg, * * @return 0 on success */ -static int sbat_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, +static int sbat_emul_write_byte(const struct emul *emul, int reg, uint8_t val, int bytes) { struct sbat_emul_data *data; - data = SBAT_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (bytes < MSG_BUF_LEN) { data->msg_buf[bytes] = val; @@ -735,12 +730,12 @@ static int sbat_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, * * @return 0 on success */ -static int sbat_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, +static int sbat_emul_read_byte(const struct emul *emul, int reg, uint8_t *val, int bytes) { struct sbat_emul_data *data; - data = SBAT_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (data->num_to_read == 0) { data->bat.error_code = STATUS_CODE_UNSUPPORTED; @@ -767,7 +762,7 @@ static int sbat_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, * * @return Currently accessed register */ -static int sbat_emul_access_reg(struct i2c_emul *emul, int reg, int bytes, +static int sbat_emul_access_reg(const struct emul *emul, int reg, int bytes, bool read) { return reg; @@ -789,18 +784,16 @@ static int sbat_emul_access_reg(struct i2c_emul *emul, int reg, int bytes, static int sbat_emul_init(const struct emul *emul, const struct device *parent) { const struct i2c_common_emul_cfg *cfg = emul->cfg; - struct i2c_common_emul_data *data = cfg->data; - int ret; + struct sbat_emul_data *data = emul->data; - data->emul.api = &i2c_common_emul_api; - data->emul.addr = cfg->addr; - data->i2c = parent; - data->cfg = cfg; - i2c_common_emul_init(data); + data->common.emul.addr = cfg->addr; + data->common.emul.target = emul; + data->common.i2c = parent; + data->common.cfg = emul->cfg; - ret = i2c_emul_register(parent, emul->dev_label, &data->emul); + i2c_common_emul_init(&data->common); - return ret; + return 0; } #define SMART_BATTERY_EMUL(n) \ @@ -880,16 +873,16 @@ static int sbat_emul_init(const struct emul *emul, const struct device *parent) .addr = DT_INST_REG_ADDR(n), \ }; \ EMUL_DEFINE(sbat_emul_init, DT_DRV_INST(n), &sbat_emul_cfg_##n, \ - &sbat_emul_data_##n) + &sbat_emul_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(SMART_BATTERY_EMUL) #define SMART_BATTERY_EMUL_CASE(n) \ case DT_INST_DEP_ORD(n): \ - return &sbat_emul_data_##n.common.emul; + return sbat_emul_data_##n.common.emul.target; /** Check description in emul_smart_battery.h */ -struct i2c_emul *sbat_emul_get_ptr(int ord) +const struct emul *sbat_emul_get_ptr(int ord) { switch (ord) { DT_INST_FOREACH_STATUS_OKAY(SMART_BATTERY_EMUL_CASE) @@ -917,3 +910,11 @@ static void emul_sbat_reset(const struct ztest_unit_test *test, void *data) } ZTEST_RULE(emul_smart_battery_reset, NULL, emul_sbat_reset); + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_smart_battery_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/emul_sn5s330.c b/zephyr/emul/emul_sn5s330.c index b515b832a1..2877ed8573 100644 --- a/zephyr/emul/emul_sn5s330.c +++ b/zephyr/emul/emul_sn5s330.c @@ -19,14 +19,11 @@ #include "emul/emul_common_i2c.h" #include "emul/emul_sn5s330.h" #include "i2c.h" +#include "emul/emul_stub_device.h" #include <zephyr/logging/log.h> LOG_MODULE_REGISTER(sn5s330_emul, CONFIG_SN5S330_EMUL_LOG_LEVEL); -#define SN5S330_DATA_FROM_I2C_EMUL(_emul) \ - CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \ - struct sn5s330_emul_data, common) - struct sn5s330_emul_data { /** Common I2C data */ struct i2c_common_emul_data common; @@ -110,13 +107,6 @@ test_mockable_static void sn5s330_emul_interrupt_set_stub(void) /* Stub to be used by fff fakes during test */ } -struct i2c_emul *sn5s330_emul_to_i2c_emul(const struct emul *emul) -{ - struct sn5s330_emul_data *data = emul->data; - - return &(data->common.emul); -} - /* Workhorse for mapping i2c reg to internal emulator data access */ static uint8_t *sn5s330_emul_get_reg_ptr(struct sn5s330_emul_data *data, int reg) @@ -194,29 +184,29 @@ void sn5s330_emul_peek_reg(const struct emul *emul, uint32_t reg, uint8_t *val) *val = *data_reg; } -static void sn5s330_emul_set_int_pin(struct i2c_emul *emul, bool val) +static void sn5s330_emul_set_int_pin(const struct emul *emul, bool val) { - struct sn5s330_emul_data *data = SN5S330_DATA_FROM_I2C_EMUL(emul); + struct sn5s330_emul_data *data = emul->data; int res = gpio_emul_input_set(data->gpio_int_port, data->gpio_int_pin, val); __ASSERT_NO_MSG(res == 0); } -static void sn5s330_emul_assert_interrupt(struct i2c_emul *emul) +static void sn5s330_emul_assert_interrupt(const struct emul *emul) { sn5s330_emul_interrupt_set_stub(); sn5s330_emul_set_int_pin(emul, false); } -static void sn5s330_emul_deassert_interrupt(struct i2c_emul *emul) +static void sn5s330_emul_deassert_interrupt(const struct emul *emul) { sn5s330_emul_set_int_pin(emul, true); } -static int sn5s330_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, - int bytes) +static int sn5s330_emul_read_byte(const struct emul *emul, int reg, + uint8_t *val, int bytes) { - struct sn5s330_emul_data *data = SN5S330_DATA_FROM_I2C_EMUL(emul); + struct sn5s330_emul_data *data = emul->data; uint8_t *reg_to_read = sn5s330_emul_get_reg_ptr(data, reg); __ASSERT(bytes == 0, "bytes 0x%x != 0x0 on reg 0x%x", bytes, reg); @@ -225,10 +215,10 @@ static int sn5s330_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, return 0; } -static int sn5s330_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, - int bytes) +static int sn5s330_emul_write_byte(const struct emul *emul, int reg, + uint8_t val, int bytes) { - struct sn5s330_emul_data *data = SN5S330_DATA_FROM_I2C_EMUL(emul); + struct sn5s330_emul_data *data = emul->data; uint8_t *reg_to_write; bool deassert_int = false; @@ -278,7 +268,6 @@ static int sn5s330_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, void sn5s330_emul_make_vbus_overcurrent(const struct emul *emul) { struct sn5s330_emul_data *data = emul->data; - struct i2c_emul *i2c_emul = &data->common.emul; data->int_status_reg1 |= SN5S330_ILIM_PP1_MASK; data->int_trip_rise_reg1 |= SN5S330_ILIM_PP1_MASK; @@ -287,13 +276,12 @@ void sn5s330_emul_make_vbus_overcurrent(const struct emul *emul) if (data->int_mask_rise_reg1 & SN5S330_ILIM_PP1_MASK) return; - sn5s330_emul_assert_interrupt(i2c_emul); + sn5s330_emul_assert_interrupt(emul); } void sn5s330_emul_lower_vbus_below_minv(const struct emul *emul) { struct sn5s330_emul_data *data = emul->data; - struct i2c_emul *i2c_emul = &data->common.emul; data->int_status_reg4 |= SN5S330_VSAFE0V_STAT; @@ -301,7 +289,7 @@ void sn5s330_emul_lower_vbus_below_minv(const struct emul *emul) if (data->int_status_reg4 & SN5S330_VSAFE0V_MASK) return; - sn5s330_emul_assert_interrupt(i2c_emul); + sn5s330_emul_assert_interrupt(emul); } void sn5s330_emul_reset(const struct emul *emul) @@ -311,7 +299,7 @@ void sn5s330_emul_reset(const struct emul *emul) const struct device *gpio_int_port = data->gpio_int_port; gpio_pin_t gpio_int_pin = data->gpio_int_pin; - sn5s330_emul_deassert_interrupt(&data->common.emul); + sn5s330_emul_deassert_interrupt(emul); /* * TODO(b/203364783): Some registers reset with set bits; this should be @@ -331,16 +319,15 @@ static int emul_sn5s330_init(const struct emul *emul, const struct sn5s330_emul_cfg *cfg = emul->cfg; struct sn5s330_emul_data *data = emul->data; - sn5s330_emul_deassert_interrupt(&data->common.emul); + sn5s330_emul_deassert_interrupt(emul); - data->common.emul.api = &i2c_common_emul_api; data->common.emul.addr = cfg->common.addr; - data->common.emul.parent = emul; + data->common.emul.target = emul; data->common.i2c = parent; data->common.cfg = &cfg->common; i2c_common_emul_init(&data->common); - return i2c_emul_register(parent, emul->dev_label, &data->common.emul); + return 0; } #define SN5S330_GET_GPIO_INT_PORT(n) \ @@ -365,6 +352,13 @@ static int emul_sn5s330_init(const struct emul *emul, }, \ }; \ EMUL_DEFINE(emul_sn5s330_init, DT_DRV_INST(n), &sn5s330_emul_cfg_##n, \ - &sn5s330_emul_data_##n) + &sn5s330_emul_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(INIT_SN5S330) +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_sn5s330_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/emul_syv682x.c b/zephyr/emul/emul_syv682x.c index ab7ba9ff91..a0c1020cc3 100644 --- a/zephyr/emul/emul_syv682x.c +++ b/zephyr/emul/emul_syv682x.c @@ -20,6 +20,7 @@ LOG_MODULE_REGISTER(syv682x); #include "emul/emul_common_i2c.h" #include "emul/emul_syv682x.h" +#include "emul/emul_stub_device.h" #define EMUL_REG_COUNT (SYV682X_CONTROL_4_REG + 1) #define EMUL_REG_IS_VALID(reg) (reg >= 0 && reg < EMUL_REG_COUNT) @@ -53,17 +54,6 @@ struct syv682x_emul_cfg { const struct i2c_common_emul_cfg common; }; -/* Gets the SYV682x data struct that contains an I2C emulator struct. */ -static struct syv682x_emul_data * -i2c_emul_to_syv682x_emul_data(const struct i2c_emul *emul) -{ - struct i2c_common_emul_data *i2c_common_data = - CONTAINER_OF(emul, struct i2c_common_emul_data, emul); - struct syv682x_emul_data *syv682x_data = - CONTAINER_OF(i2c_common_data, struct syv682x_emul_data, common); - return syv682x_data; -} - /* Asserts or deasserts the interrupt signal to the EC. */ static void syv682x_emul_set_alert(struct syv682x_emul_data *data, bool alert) { @@ -73,9 +63,9 @@ static void syv682x_emul_set_alert(struct syv682x_emul_data *data, bool alert) __ASSERT_NO_MSG(res == 0); } -int syv682x_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val) +int syv682x_emul_set_reg(const struct emul *emul, int reg, uint8_t val) { - struct syv682x_emul_data *data = i2c_emul_to_syv682x_emul_data(emul); + struct syv682x_emul_data *data = emul->data; if (!EMUL_REG_IS_VALID(reg)) return -EIO; @@ -85,11 +75,11 @@ int syv682x_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val) return 0; } -void syv682x_emul_set_condition(struct i2c_emul *emul, uint8_t status, +void syv682x_emul_set_condition(const struct emul *emul, uint8_t status, uint8_t control_4) { uint8_t control_4_interrupt = control_4 & SYV682X_CONTROL_4_INT_MASK; - struct syv682x_emul_data *data = i2c_emul_to_syv682x_emul_data(emul); + struct syv682x_emul_data *data = emul->data; int frs_en_gpio = gpio_emul_output_get(data->frs_en_gpio_port, data->frs_en_gpio_pin); @@ -131,9 +121,9 @@ void syv682x_emul_set_condition(struct i2c_emul *emul, uint8_t status, syv682x_emul_set_alert(data, status | control_4_interrupt); } -void syv682x_emul_set_busy_reads(struct i2c_emul *emul, int reads) +void syv682x_emul_set_busy_reads(const struct emul *emul, int reads) { - struct syv682x_emul_data *data = i2c_emul_to_syv682x_emul_data(emul); + struct syv682x_emul_data *data = emul->data; data->busy_read_count = reads; if (reads) data->reg[SYV682X_CONTROL_3_REG] |= SYV682X_BUSY; @@ -141,9 +131,9 @@ void syv682x_emul_set_busy_reads(struct i2c_emul *emul, int reads) data->reg[SYV682X_CONTROL_3_REG] &= ~SYV682X_BUSY; } -int syv682x_emul_get_reg(struct i2c_emul *emul, int reg, uint8_t *val) +int syv682x_emul_get_reg(const struct emul *emul, int reg, uint8_t *val) { - struct syv682x_emul_data *data = i2c_emul_to_syv682x_emul_data(emul); + struct syv682x_emul_data *data = emul->data; if (!EMUL_REG_IS_VALID(reg)) return -EIO; @@ -153,10 +143,10 @@ int syv682x_emul_get_reg(struct i2c_emul *emul, int reg, uint8_t *val) return 0; } -static int syv682x_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, - int bytes) +static int syv682x_emul_write_byte(const struct emul *emul, int reg, + uint8_t val, int bytes) { - struct syv682x_emul_data *data = i2c_emul_to_syv682x_emul_data(emul); + struct syv682x_emul_data *data = emul->data; zassert_equal(bytes, 1, "Write: bytes == %i at offset 0x%x", bytes, reg); @@ -182,10 +172,10 @@ static int syv682x_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, return syv682x_emul_set_reg(emul, reg, val); } -static int syv682x_emul_read_byte(struct i2c_emul *emul, int reg, uint8_t *val, - int bytes) +static int syv682x_emul_read_byte(const struct emul *emul, int reg, + uint8_t *val, int bytes) { - struct syv682x_emul_data *data = i2c_emul_to_syv682x_emul_data(emul); + struct syv682x_emul_data *data = emul->data; int ret = syv682x_emul_get_reg(emul, reg, val); zassert_equal(bytes, 0, "Read: bytes == %i at offset 0x%x", bytes, reg); @@ -250,15 +240,14 @@ static int syv682x_emul_init(const struct emul *emul, data->cfg = cfg; - data->common.emul.api = &i2c_common_emul_api; data->common.emul.addr = cfg->common.addr; - data->common.emul.parent = emul; + data->common.emul.target = emul; data->common.i2c = parent; data->common.cfg = &cfg->common; i2c_common_emul_init(&data->common); syv682x_emul_reset(data); - return i2c_emul_register(parent, emul->dev_label, &data->common.emul); + return 0; } /* Device instantiation */ @@ -285,15 +274,15 @@ static int syv682x_emul_init(const struct emul *emul, }, \ }; \ EMUL_DEFINE(syv682x_emul_init, DT_DRV_INST(n), &syv682x_emul_cfg_##n, \ - &syv682x_emul_data_##n) + &syv682x_emul_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(SYV682X_EMUL) #define SYV682X_EMUL_CASE(n) \ case DT_INST_DEP_ORD(n): \ - return &syv682x_emul_data_##n.common.emul; + return syv682x_emul_data_##n.common.emul.target; -struct i2c_emul *syv682x_emul_get(int ord) +const struct emul *syv682x_emul_get(int ord) { switch (ord) { DT_INST_FOREACH_STATUS_OKAY(SYV682X_EMUL_CASE) @@ -315,3 +304,11 @@ static void emul_syv682x_reset_before(const struct ztest_unit_test *test, } ZTEST_RULE(emul_syv682x_reset, emul_syv682x_reset_before, NULL); #endif /* CONFIG_ZTEST_NEW_API */ + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_syv682x_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/emul_tcs3400.c b/zephyr/emul/emul_tcs3400.c index 0bb48fccd1..385dfb78c4 100644 --- a/zephyr/emul/emul_tcs3400.c +++ b/zephyr/emul/emul_tcs3400.c @@ -17,13 +17,10 @@ LOG_MODULE_REGISTER(emul_tcs); #include "emul/emul_common_i2c.h" #include "emul/emul_tcs3400.h" +#include "emul/emul_stub_device.h" #include "driver/als_tcs3400.h" -#define TCS_DATA_FROM_I2C_EMUL(_emul) \ - CONTAINER_OF(CONTAINER_OF(_emul, struct i2c_common_emul_data, emul), \ - struct tcs_emul_data, common) - /** Run-time data used by the emulator */ struct tcs_emul_data { /** Common I2C data */ @@ -64,7 +61,7 @@ struct tcs_emul_data { }; /** Check description in emul_tcs3400.h */ -void tcs_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val) +void tcs_emul_set_reg(const struct emul *emul, int reg, uint8_t val) { struct tcs_emul_data *data; @@ -73,12 +70,12 @@ void tcs_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val) } reg -= TCS_EMUL_FIRST_REG; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->reg[reg] = val; } /** Check description in emul_tcs3400.h */ -uint8_t tcs_emul_get_reg(struct i2c_emul *emul, int reg) +uint8_t tcs_emul_get_reg(const struct emul *emul, int reg) { struct tcs_emul_data *data; @@ -86,18 +83,18 @@ uint8_t tcs_emul_get_reg(struct i2c_emul *emul, int reg) return 0; } - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; reg -= TCS_EMUL_FIRST_REG; return data->reg[reg]; } /** Check description in emul_tcs3400.h */ -int tcs_emul_get_val(struct i2c_emul *emul, enum tcs_emul_axis axis) +int tcs_emul_get_val(const struct emul *emul, enum tcs_emul_axis axis) { struct tcs_emul_data *data; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case TCS_EMUL_R: @@ -116,11 +113,11 @@ int tcs_emul_get_val(struct i2c_emul *emul, enum tcs_emul_axis axis) } /** Check description in emul_tcs3400.h */ -void tcs_emul_set_val(struct i2c_emul *emul, enum tcs_emul_axis axis, int val) +void tcs_emul_set_val(const struct emul *emul, enum tcs_emul_axis axis, int val) { struct tcs_emul_data *data; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; switch (axis) { case TCS_EMUL_R: @@ -142,29 +139,29 @@ void tcs_emul_set_val(struct i2c_emul *emul, enum tcs_emul_axis axis, int val) } /** Check description in emul_tcs3400.h */ -void tcs_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set) +void tcs_emul_set_err_on_ro_write(const struct emul *emul, bool set) { struct tcs_emul_data *data; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_ro_write = set; } /** Check description in emul_tcs3400.h */ -void tcs_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set) +void tcs_emul_set_err_on_rsvd_write(const struct emul *emul, bool set) { struct tcs_emul_data *data; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_rsvd_write = set; } /** Check description in emul_tcs3400.h */ -void tcs_emul_set_err_on_msb_first(struct i2c_emul *emul, bool set) +void tcs_emul_set_err_on_msb_first(const struct emul *emul, bool set) { struct tcs_emul_data *data; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->error_on_msb_first = set; } @@ -202,11 +199,11 @@ static const uint8_t tcs_emul_rsvd_mask[] = { * * @param emul Pointer to TCS3400 emulator */ -static void tcs_emul_reset(struct i2c_emul *emul) +static void tcs_emul_reset(const struct emul *emul) { struct tcs_emul_data *data; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->reg[TCS_I2C_ENABLE - TCS_EMUL_FIRST_REG] = 0x00; data->reg[TCS_I2C_ATIME - TCS_EMUL_FIRST_REG] = 0xff; @@ -274,11 +271,11 @@ static int tcs_emul_get_cycles(uint8_t atime) * * @param emul Pointer to TCS3400 emulator */ -static void tcs_emul_clear_int(struct i2c_emul *emul) +static void tcs_emul_clear_int(const struct emul *emul) { struct tcs_emul_data *data; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; data->reg[TCS_I2C_STATUS - TCS_EMUL_FIRST_REG] = 0x00; } @@ -296,12 +293,12 @@ static void tcs_emul_clear_int(struct i2c_emul *emul) * @return 0 on success * @return -EIO on error */ -static int tcs_emul_handle_write(struct i2c_emul *emul, int reg, int bytes) +static int tcs_emul_handle_write(const struct emul *emul, int reg, int bytes) { struct tcs_emul_data *data; uint8_t val; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; /* This write only selected register for I2C read message */ if (bytes < 2) { @@ -388,8 +385,8 @@ static int tcs_emul_handle_write(struct i2c_emul *emul, int reg, int bytes) * @return 0 on success * @return -EIO when accessing MSB before LSB */ -static int tcs_emul_get_reg_val(struct i2c_emul *emul, int reg, bool *lsb_read, - bool lsb, unsigned int val) +static int tcs_emul_get_reg_val(const struct emul *emul, int reg, + bool *lsb_read, bool lsb, unsigned int val) { struct tcs_emul_data *data; uint64_t reg_val; @@ -398,7 +395,7 @@ static int tcs_emul_get_reg_val(struct i2c_emul *emul, int reg, bool *lsb_read, int cycles; int gain; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (lsb) { *lsb_read = 1; @@ -452,14 +449,14 @@ static int tcs_emul_get_reg_val(struct i2c_emul *emul, int reg, bool *lsb_read, * @return 0 on success * @return -EIO on error */ -static int tcs_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *buf, +static int tcs_emul_handle_read(const struct emul *emul, int reg, uint8_t *buf, int bytes) { struct tcs_emul_data *data; unsigned int c_ir; int ret; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; reg += bytes; @@ -547,12 +544,12 @@ static int tcs_emul_handle_read(struct i2c_emul *emul, int reg, uint8_t *buf, * @return 0 on success * @return -EIO on error */ -static int tcs_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, +static int tcs_emul_write_byte(const struct emul *emul, int reg, uint8_t val, int bytes) { struct tcs_emul_data *data; - data = TCS_DATA_FROM_I2C_EMUL(emul); + data = emul->data; if (bytes > 1) { LOG_ERR("Too long write command"); @@ -580,20 +577,19 @@ static int tcs_emul_write_byte(struct i2c_emul *emul, int reg, uint8_t val, static int tcs_emul_init(const struct emul *emul, const struct device *parent) { const struct i2c_common_emul_cfg *cfg = emul->cfg; - struct i2c_common_emul_data *data = cfg->data; - int ret; + struct tcs_emul_data *data = emul->data; - data->emul.api = &i2c_common_emul_api; - data->emul.addr = cfg->addr; - data->i2c = parent; - data->cfg = cfg; - i2c_common_emul_init(data); + data->common.emul.addr = cfg->addr; + data->common.emul.target = emul; + data->common.i2c = parent; + data->common.cfg = cfg; + i2c_common_emul_init(&data->common); - ret = i2c_emul_register(parent, emul->dev_label, &data->emul); + i2c_common_emul_init(&data->common); - tcs_emul_reset(&data->emul); + tcs_emul_reset(emul); - return ret; + return 0; } #define TCS3400_EMUL(n) \ @@ -627,16 +623,16 @@ static int tcs_emul_init(const struct emul *emul, const struct device *parent) .addr = DT_INST_REG_ADDR(n), \ }; \ EMUL_DEFINE(tcs_emul_init, DT_DRV_INST(n), &tcs_emul_cfg_##n, \ - &tcs_emul_data_##n) + &tcs_emul_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(TCS3400_EMUL) #define TCS3400_EMUL_CASE(n) \ case DT_INST_DEP_ORD(n): \ - return &tcs_emul_data_##n.common.emul; + return tcs_emul_data_##n.common.emul.target; /** Check description in emul_tcs3400.h */ -struct i2c_emul *tcs_emul_get(int ord) +const struct emul *tcs_emul_get(int ord) { switch (ord) { DT_INST_FOREACH_STATUS_OKAY(TCS3400_EMUL_CASE) @@ -648,7 +644,7 @@ struct i2c_emul *tcs_emul_get(int ord) #ifdef CONFIG_ZTEST_NEW_API #define TCS3400_EMUL_RESET_RULE_BEFORE(n) \ - tcs_emul_reset(&(tcs_emul_data_##n.common.emul)); + tcs_emul_reset((tcs_emul_data_##n.common.emul.target)); static void emul_tcs3400_reset_rule_before(const struct ztest_unit_test *test, void *data) { @@ -658,3 +654,11 @@ static void emul_tcs3400_reset_rule_before(const struct ztest_unit_test *test, } ZTEST_RULE(emul_tcs3400_reset, emul_tcs3400_reset_rule_before, NULL); #endif /* CONFIG_ZTEST_NEW_API */ + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_tcs3400_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/i2c_mock.c b/zephyr/emul/i2c_mock.c index 207151ef6c..d4b257731c 100644 --- a/zephyr/emul/i2c_mock.c +++ b/zephyr/emul/i2c_mock.c @@ -6,9 +6,11 @@ #define DT_DRV_COMPAT cros_i2c_mock #include <zephyr/device.h> +#include <zephyr/logging/log.h> + #include "emul/emul_common_i2c.h" +#include "emul/emul_stub_device.h" -#include <zephyr/logging/log.h> LOG_MODULE_REGISTER(i2c_mock, CONFIG_I2C_MOCK_LOG_LEVEL); struct i2c_emul *i2c_mock_to_i2c_emul(const struct emul *emul) @@ -20,14 +22,12 @@ struct i2c_emul *i2c_mock_to_i2c_emul(const struct emul *emul) void i2c_mock_reset(const struct emul *emul) { - struct i2c_emul *i2c_emul = i2c_mock_to_i2c_emul(emul); - - i2c_common_emul_set_read_fail_reg(i2c_emul, + i2c_common_emul_set_read_fail_reg(emul->data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_write_fail_reg(i2c_emul, + i2c_common_emul_set_write_fail_reg(emul->data, I2C_COMMON_EMUL_NO_FAIL_REG); - i2c_common_emul_set_read_func(i2c_emul, NULL, NULL); - i2c_common_emul_set_write_func(i2c_emul, NULL, NULL); + i2c_common_emul_set_read_func(emul->data, NULL, NULL); + i2c_common_emul_set_write_func(emul->data, NULL, NULL); } uint16_t i2c_mock_get_addr(const struct emul *emul) @@ -48,12 +48,12 @@ static int i2c_mock_init(const struct emul *emul, const struct device *parent) data->emul.api = &i2c_mock_api; data->emul.addr = cfg->addr; - data->emul.parent = emul; + data->emul.target = emul; data->i2c = parent; data->cfg = cfg; i2c_common_emul_init(data); - return i2c_emul_register(parent, emul->dev_label, &data->emul); + return 0; } #define INIT_I2C_MOCK(n) \ @@ -64,6 +64,14 @@ static int i2c_mock_init(const struct emul *emul, const struct device *parent) }; \ static struct i2c_common_emul_data i2c_mock_data_##n; \ EMUL_DEFINE(i2c_mock_init, DT_DRV_INST(n), &i2c_mock_cfg_##n, \ - &i2c_mock_data_##n) + &i2c_mock_data_##n, &i2c_common_emul_api) DT_INST_FOREACH_STATUS_OKAY(INIT_I2C_MOCK) + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_i2c_mock_get_i2c_common_data(const struct emul *emul) +{ + return emul->data; +} diff --git a/zephyr/emul/tcpc/emul_ps8xxx.c b/zephyr/emul/tcpc/emul_ps8xxx.c index 477070460f..df76afe2c3 100644 --- a/zephyr/emul/tcpc/emul_ps8xxx.c +++ b/zephyr/emul/tcpc/emul_ps8xxx.c @@ -19,6 +19,7 @@ LOG_MODULE_REGISTER(ps8xxx_emul, CONFIG_TCPCI_EMUL_LOG_LEVEL); #include "emul/emul_common_i2c.h" #include "emul/tcpc/emul_ps8xxx.h" #include "emul/tcpc/emul_tcpci.h" +#include "emul/emul_stub_device.h" #include "driver/tcpm/ps8xxx.h" @@ -125,8 +126,9 @@ uint16_t ps8xxx_emul_get_product_id(const struct emul *emul) } /** Check description in emul_ps8xxx.h */ -struct i2c_emul *ps8xxx_emul_get_i2c_emul(const struct emul *emul, - enum ps8xxx_emul_port port) +struct i2c_common_emul_data * +ps8xxx_emul_get_i2c_common_data(const struct emul *emul, + enum ps8xxx_emul_port port) { const struct ps8xxx_emul_cfg *cfg = emul->cfg; struct tcpc_emul_data *tcpc_data = emul->data; @@ -134,12 +136,12 @@ struct i2c_emul *ps8xxx_emul_get_i2c_emul(const struct emul *emul, switch (port) { case PS8XXX_EMUL_PORT_0: - return &data->p0_data.emul; + return &data->p0_data; case PS8XXX_EMUL_PORT_1: - return &data->p1_data.emul; + return &data->p1_data; case PS8XXX_EMUL_PORT_GPIO: if (cfg->gpio_cfg.addr != 0) { - return &data->gpio_data.emul; + return &data->gpio_data; } else { return NULL; } @@ -159,16 +161,14 @@ struct i2c_emul *ps8xxx_emul_get_i2c_emul(const struct emul *emul, * @return 0 on success * @return -EIO on invalid read request */ -static int ps8xxx_emul_tcpc_read_byte(struct i2c_emul *i2c_emul, int reg, +static int ps8xxx_emul_tcpc_read_byte(const struct emul *emul, int reg, uint8_t *val, int bytes) { uint16_t reg_val; - const struct emul *emul; + const struct i2c_emul *i2c_emul = emul->bus.i2c; LOG_DBG("PS8XXX TCPC 0x%x: read reg 0x%x", i2c_emul->addr, reg); - emul = i2c_emul->parent; - switch (reg) { case PS8XXX_REG_FW_REV: case PS8XXX_REG_I2C_DEBUGGING_ENABLE: @@ -196,7 +196,7 @@ static int ps8xxx_emul_tcpc_read_byte(struct i2c_emul *i2c_emul, int reg, /** * @brief Function called for each byte of write message to TCPC chip * - * @param i2c_emul Pointer to PS8xxx emulator + * @param emul Pointer to PS8xxx emulator * @param reg First byte of write message * @param val Received byte of write message * @param bytes Number of bytes already received @@ -204,16 +204,14 @@ static int ps8xxx_emul_tcpc_read_byte(struct i2c_emul *i2c_emul, int reg, * @return 0 on success * @return -EIO on invalid write request */ -static int ps8xxx_emul_tcpc_write_byte(struct i2c_emul *i2c_emul, int reg, +static int ps8xxx_emul_tcpc_write_byte(const struct emul *emul, int reg, uint8_t val, int bytes) { uint16_t prod_id; - const struct emul *emul; + struct i2c_emul *i2c_emul = emul->bus.i2c; LOG_DBG("PS8XXX TCPC 0x%x: write reg 0x%x", i2c_emul->addr, reg); - emul = i2c_emul->parent; - tcpci_emul_get_reg(emul, TCPC_REG_PRODUCT_ID, &prod_id); switch (reg) { @@ -246,23 +244,21 @@ static int ps8xxx_emul_tcpc_write_byte(struct i2c_emul *i2c_emul, int reg, /** * @brief Function called on the end of write message to TCPC chip * - * @param i2c_emul Pointer to PS8xxx emulator + * @param emul Pointer to PS8xxx emulator * @param reg Register which is written * @param msg_len Length of handled I2C message * * @return 0 on success * @return -EIO on error */ -static int ps8xxx_emul_tcpc_finish_write(struct i2c_emul *i2c_emul, int reg, +static int ps8xxx_emul_tcpc_finish_write(const struct emul *emul, int reg, int msg_len) { uint16_t prod_id; - const struct emul *emul; + struct i2c_emul *i2c_emul = emul->bus.i2c; LOG_DBG("PS8XXX TCPC 0x%x: finish write reg 0x%x", i2c_emul->addr, reg); - emul = i2c_emul->parent; - tcpci_emul_get_reg(emul, TCPC_REG_PRODUCT_ID, &prod_id); switch (reg) { @@ -289,14 +285,14 @@ static int ps8xxx_emul_tcpc_finish_write(struct i2c_emul *i2c_emul, int reg, * @brief Get currently accessed register, which always equals to selected * register from TCPC chip. * - * @param i2c_emul Pointer to TCPCI emulator + * @param emul Pointer to TCPCI emulator * @param reg First byte of last write message * @param bytes Number of bytes already handled from current message * @param read If currently handled is read message * * @return Currently accessed register */ -static int ps8xxx_emul_tcpc_access_reg(struct i2c_emul *i2c_emul, int reg, +static int ps8xxx_emul_tcpc_access_reg(const struct emul *emul, int reg, int bytes, bool read) { return reg; @@ -320,39 +316,9 @@ static int ps8xxx_emul_tcpc_reset(const struct emul *emul) } /** - * @brief Get port associated with given "hidden" I2C device - * - * @param i2c_emul Pointer to "hidden" I2C device - * - * @return Port associated with given I2C device - */ -static enum ps8xxx_emul_port ps8xxx_emul_get_port(struct i2c_emul *i2c_emul) -{ - const struct ps8xxx_emul_cfg *cfg; - const struct emul *emul; - - emul = i2c_emul->parent; - cfg = emul->cfg; - - if (cfg->p0_cfg.addr == i2c_emul->addr) { - return PS8XXX_EMUL_PORT_0; - } - - if (cfg->p1_cfg.addr == i2c_emul->addr) { - return PS8XXX_EMUL_PORT_1; - } - - if (cfg->gpio_cfg.addr != 0 && cfg->gpio_cfg.addr == i2c_emul->addr) { - return PS8XXX_EMUL_PORT_GPIO; - } - - return PS8XXX_EMUL_PORT_INVAL; -} - -/** * @brief Function called for each byte of read message * - * @param i2c_emul Pointer to PS8xxx emulator + * @param emul Pointer to PS8xxx emulator * @param reg First byte of last write message * @param val Pointer where byte to read should be stored * @param bytes Number of bytes already read @@ -360,18 +326,17 @@ static enum ps8xxx_emul_port ps8xxx_emul_get_port(struct i2c_emul *i2c_emul) * @return 0 on success * @return -EIO on invalid read request */ -static int ps8xxx_emul_read_byte(struct i2c_emul *i2c_emul, int reg, - uint8_t *val, int bytes) +static int ps8xxx_emul_read_byte_workhorse(const struct emul *emul, int reg, + uint8_t *val, int bytes, + enum ps8xxx_emul_port port) { struct tcpc_emul_data *tcpc_data; struct ps8xxx_emul_data *data; - enum ps8xxx_emul_port port; - const struct emul *emul; + struct i2c_emul *i2c_emul = emul->bus.i2c; uint16_t i2c_dbg_reg; LOG_DBG("PS8XXX 0x%x: read reg 0x%x", i2c_emul->addr, reg); - emul = i2c_emul->parent; tcpc_data = emul->data; data = tcpc_data->chip_data; @@ -382,8 +347,6 @@ static int ps8xxx_emul_read_byte(struct i2c_emul *i2c_emul, int reg, return -EIO; } - port = ps8xxx_emul_get_port(i2c_emul); - /* This is only 2 bytes register so handle it separately */ if (data->prod_id == PS8815_PRODUCT_ID && port == PS8XXX_EMUL_PORT_1 && reg == PS8815_P1_REG_HW_REVISION) { @@ -430,10 +393,31 @@ static int ps8xxx_emul_read_byte(struct i2c_emul *i2c_emul, int reg, return -EIO; } +static int ps8xxx_emul_p0_read_byte(const struct emul *emul, int reg, + uint8_t *val, int bytes) +{ + return ps8xxx_emul_read_byte_workhorse(emul, reg, val, bytes, + PS8XXX_EMUL_PORT_0); +} + +static int ps8xxx_emul_p1_read_byte(const struct emul *emul, int reg, + uint8_t *val, int bytes) +{ + return ps8xxx_emul_read_byte_workhorse(emul, reg, val, bytes, + PS8XXX_EMUL_PORT_1); +} + +static int ps8xxx_emul_gpio_read_byte(const struct emul *emul, int reg, + uint8_t *val, int bytes) +{ + return ps8xxx_emul_read_byte_workhorse(emul, reg, val, bytes, + PS8XXX_EMUL_PORT_GPIO); +} + /** * @brief Function called for each byte of write message * - * @param i2c_emul Pointer to PS8xxx emulator + * @param emul Pointer to PS8xxx emulator * @param reg First byte of write message * @param val Received byte of write message * @param bytes Number of bytes already received @@ -441,18 +425,17 @@ static int ps8xxx_emul_read_byte(struct i2c_emul *i2c_emul, int reg, * @return 0 on success * @return -EIO on invalid write request */ -static int ps8xxx_emul_write_byte(struct i2c_emul *i2c_emul, int reg, - uint8_t val, int bytes) +static int ps8xxx_emul_write_byte_workhorse(const struct emul *emul, int reg, + uint8_t val, int bytes, + enum ps8xxx_emul_port port) { struct ps8xxx_emul_data *data; struct tcpc_emul_data *tcpc_data; - enum ps8xxx_emul_port port; - const struct emul *emul; + const struct i2c_emul *i2c_emul = emul->bus.i2c; uint16_t i2c_dbg_reg; LOG_DBG("PS8XXX 0x%x: write reg 0x%x", i2c_emul->addr, reg); - emul = i2c_emul->parent; tcpc_data = emul->data; data = tcpc_data->chip_data; @@ -463,8 +446,6 @@ static int ps8xxx_emul_write_byte(struct i2c_emul *i2c_emul, int reg, return -EIO; } - port = ps8xxx_emul_get_port(i2c_emul); - if (bytes != 1) { LOG_ERR("Writing more than one byte at once"); return -EIO; @@ -494,6 +475,77 @@ static int ps8xxx_emul_write_byte(struct i2c_emul *i2c_emul, int reg, return -EIO; } +static int ps8xxx_emul_p0_write_byte(const struct emul *emul, int reg, + uint8_t val, int bytes) +{ + return ps8xxx_emul_write_byte_workhorse(emul, reg, val, bytes, + PS8XXX_EMUL_PORT_0); +} + +static int ps8xxx_emul_p1_write_byte(const struct emul *emul, int reg, + uint8_t val, int bytes) +{ + return ps8xxx_emul_write_byte_workhorse(emul, reg, val, bytes, + PS8XXX_EMUL_PORT_1); +} + +static int ps8xxx_emul_gpio_write_byte(const struct emul *emul, int reg, + uint8_t val, int bytes) +{ + return ps8xxx_emul_write_byte_workhorse(emul, reg, val, bytes, + PS8XXX_EMUL_PORT_GPIO); +} + +static int i2c_ps8xxx_emul_transfer(const struct emul *target, + struct i2c_msg *msgs, int num_msgs, + int addr) +{ + struct tcpc_emul_data *tcpc_data = target->data; + struct ps8xxx_emul_data *ps8_xxx_data = tcpc_data->chip_data; + const struct ps8xxx_emul_cfg *ps8_xxx_cfg = target->cfg; + struct i2c_common_emul_data *common_data; + + /* The chip itself */ + if (addr == tcpc_data->i2c_cfg.addr) { + const struct i2c_common_emul_cfg *common_cfg = + &tcpc_data->i2c_cfg; + common_data = &tcpc_data->tcpci_ctx->common; + + return i2c_common_emul_transfer_workhorse( + target, common_data, common_cfg, msgs, num_msgs, addr); + } + /* Subchips */ + else if (addr == ps8_xxx_cfg->gpio_cfg.addr) { + const struct i2c_common_emul_cfg *common_cfg = + &ps8_xxx_cfg->gpio_cfg; + common_data = &ps8_xxx_data->gpio_data; + + return i2c_common_emul_transfer_workhorse( + target, common_data, common_cfg, msgs, num_msgs, addr); + } else if (addr == ps8_xxx_cfg->p0_cfg.addr) { + const struct i2c_common_emul_cfg *common_cfg = + &ps8_xxx_cfg->p0_cfg; + common_data = &ps8_xxx_data->p0_data; + + return i2c_common_emul_transfer_workhorse( + target, common_data, common_cfg, msgs, num_msgs, addr); + } else if (addr == ps8_xxx_cfg->p1_cfg.addr) { + const struct i2c_common_emul_cfg *common_cfg = + &ps8_xxx_cfg->p1_cfg; + common_data = &ps8_xxx_data->p1_data; + + return i2c_common_emul_transfer_workhorse( + target, common_data, common_cfg, msgs, num_msgs, addr); + } + + LOG_ERR("Cannot map address %02x", addr); + return -EIO; +} + +struct i2c_emul_api i2c_ps8xxx_emul_api = { + .transfer = i2c_ps8xxx_emul_transfer, +}; + /** * @brief Set up a new PS8xxx emulator * @@ -514,7 +566,7 @@ static int ps8xxx_emul_init(const struct emul *emul, struct tcpci_ctx *tcpci_ctx = tcpc_data->tcpci_ctx; const struct ps8xxx_emul_cfg *cfg = emul->cfg; const struct device *i2c_dev; - int ret; + int ret = 0; i2c_dev = parent; @@ -525,34 +577,36 @@ static int ps8xxx_emul_init(const struct emul *emul, tcpci_emul_i2c_init(emul, i2c_dev); - data->p0_data.emul.api = &i2c_common_emul_api; + data->p0_data.emul.api = &i2c_ps8xxx_emul_api; data->p0_data.emul.addr = cfg->p0_cfg.addr; - data->p0_data.emul.parent = emul; + data->p0_data.emul.target = emul; data->p0_data.i2c = i2c_dev; data->p0_data.cfg = &cfg->p0_cfg; i2c_common_emul_init(&data->p0_data); - data->p1_data.emul.api = &i2c_common_emul_api; + data->p1_data.emul.api = &i2c_ps8xxx_emul_api; data->p1_data.emul.addr = cfg->p1_cfg.addr; - data->p1_data.emul.parent = emul; + data->p1_data.emul.target = emul; data->p1_data.i2c = i2c_dev; data->p1_data.cfg = &cfg->p1_cfg; i2c_common_emul_init(&data->p1_data); - ret = i2c_emul_register(i2c_dev, emul->dev_label, - &tcpci_ctx->common.emul); - ret |= i2c_emul_register(i2c_dev, emul->dev_label, &data->p0_data.emul); - ret |= i2c_emul_register(i2c_dev, emul->dev_label, &data->p1_data.emul); + /* Have to manually register "hidden" addressed chips under overarching + * ps8xxx + * TODO(b/240564574): Call EMUL_DEFINE for each "hidden" sub-chip. + */ + ret |= i2c_emul_register(i2c_dev, emul->dev->name, &data->p0_data.emul); + ret |= i2c_emul_register(i2c_dev, emul->dev->name, &data->p1_data.emul); if (cfg->gpio_cfg.addr != 0) { - data->gpio_data.emul.api = &i2c_common_emul_api; + data->gpio_data.emul.api = &i2c_ps8xxx_emul_api; data->gpio_data.emul.addr = cfg->gpio_cfg.addr; - data->gpio_data.emul.parent = emul; + data->gpio_data.emul.target = emul; data->gpio_data.i2c = i2c_dev; data->gpio_data.cfg = &cfg->gpio_cfg; - i2c_common_emul_init(&data->gpio_data); - ret |= i2c_emul_register(i2c_dev, emul->dev_label, + ret |= i2c_emul_register(i2c_dev, emul->dev->name, &data->gpio_data.emul); + i2c_common_emul_init(&data->gpio_data); } ret |= ps8xxx_emul_tcpc_reset(emul); @@ -568,16 +622,16 @@ static int ps8xxx_emul_init(const struct emul *emul, static struct ps8xxx_emul_data ps8xxx_emul_data_##n = { \ .prod_id = PS8805_PRODUCT_ID, \ .p0_data = { \ - .write_byte = ps8xxx_emul_write_byte, \ - .read_byte = ps8xxx_emul_read_byte, \ + .write_byte = ps8xxx_emul_p0_write_byte, \ + .read_byte = ps8xxx_emul_p0_read_byte, \ }, \ .p1_data = { \ - .write_byte = ps8xxx_emul_write_byte, \ - .read_byte = ps8xxx_emul_read_byte, \ + .write_byte = ps8xxx_emul_p1_write_byte, \ + .read_byte = ps8xxx_emul_p1_read_byte, \ }, \ .gpio_data = { \ - .write_byte = ps8xxx_emul_write_byte, \ - .read_byte = ps8xxx_emul_read_byte, \ + .write_byte = ps8xxx_emul_gpio_write_byte, \ + .read_byte = ps8xxx_emul_gpio_read_byte, \ }, \ }; \ \ @@ -602,7 +656,7 @@ static int ps8xxx_emul_init(const struct emul *emul, }, \ }; \ TCPCI_EMUL_DEFINE(n, ps8xxx_emul_init, &ps8xxx_emul_cfg_##n, \ - &ps8xxx_emul_data_##n) + &ps8xxx_emul_data_##n, &i2c_ps8xxx_emul_api) DT_INST_FOREACH_STATUS_OKAY(PS8XXX_EMUL) @@ -618,3 +672,5 @@ static void ps8xxx_emul_reset_rule_before(const struct ztest_unit_test *test, } ZTEST_RULE(PS8XXX_emul_reset, ps8xxx_emul_reset_rule_before, NULL); #endif /* CONFIG_ZTEST_NEW_API */ + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); diff --git a/zephyr/emul/tcpc/emul_tcpci.c b/zephyr/emul/tcpc/emul_tcpci.c index 4a728cd6a7..f97af97970 100644 --- a/zephyr/emul/tcpc/emul_tcpci.c +++ b/zephyr/emul/tcpc/emul_tcpci.c @@ -324,7 +324,7 @@ int tcpci_emul_add_rx_msg(const struct emul *emul, int rc; /* Acquire lock to prevent race conditions with TCPM accessing I2C */ - rc = i2c_common_emul_lock_data(&ctx->common.emul, K_FOREVER); + rc = i2c_common_emul_lock_data(&ctx->common, K_FOREVER); if (rc != 0) { LOG_ERR("Failed to acquire TCPCI lock"); return rc; @@ -353,7 +353,7 @@ int tcpci_emul_add_rx_msg(const struct emul *emul, rx_detect_mask = TCPC_REG_RX_DETECT_CABLE_RST; break; default: - i2c_common_emul_unlock_data(&ctx->common.emul); + i2c_common_emul_unlock_data(&ctx->common); return -EINVAL; } @@ -363,7 +363,7 @@ int tcpci_emul_add_rx_msg(const struct emul *emul, * TCPCI will not respond with GoodCRC, so from partner emulator * point of view it failed to send message */ - i2c_common_emul_unlock_data(&ctx->common.emul); + i2c_common_emul_unlock_data(&ctx->common); return TCPCI_EMUL_TX_FAILED; } @@ -378,7 +378,7 @@ int tcpci_emul_add_rx_msg(const struct emul *emul, set_reg(ctx, TCPC_REG_ALERT, alert_reg); rc = tcpci_emul_alert_changed(emul); - i2c_common_emul_unlock_data(&ctx->common.emul); + i2c_common_emul_unlock_data(&ctx->common); return rc; } @@ -394,7 +394,7 @@ int tcpci_emul_add_rx_msg(const struct emul *emul, rx_msg->cnt > 31) || rx_msg->cnt > 265) { LOG_ERR("Too long first message (%d)", rx_msg->cnt); - i2c_common_emul_unlock_data(&ctx->common.emul); + i2c_common_emul_unlock_data(&ctx->common); return -EINVAL; } @@ -402,7 +402,7 @@ int tcpci_emul_add_rx_msg(const struct emul *emul, } else if (ctx->rx_msg->next == NULL) { if (rx_msg->cnt > 31) { LOG_ERR("Too long second message (%d)", rx_msg->cnt); - i2c_common_emul_unlock_data(&ctx->common.emul); + i2c_common_emul_unlock_data(&ctx->common); return -EINVAL; } @@ -412,7 +412,7 @@ int tcpci_emul_add_rx_msg(const struct emul *emul, } } else { LOG_ERR("Cannot setup third message"); - i2c_common_emul_unlock_data(&ctx->common.emul); + i2c_common_emul_unlock_data(&ctx->common); return -EINVAL; } @@ -426,7 +426,7 @@ int tcpci_emul_add_rx_msg(const struct emul *emul, rc = tcpci_emul_alert_changed(emul); if (rc != 0) { - i2c_common_emul_unlock_data(&ctx->common.emul); + i2c_common_emul_unlock_data(&ctx->common); return rc; } } @@ -434,7 +434,7 @@ int tcpci_emul_add_rx_msg(const struct emul *emul, rx_msg->next = NULL; rx_msg->idx = 0; - i2c_common_emul_unlock_data(&ctx->common.emul); + i2c_common_emul_unlock_data(&ctx->common); return TCPCI_EMUL_TX_SUCCESS; } @@ -1383,23 +1383,12 @@ int tcpci_emul_handle_write(const struct emul *emul, int reg, int msg_len) } /** Check description in emul_tcpci.h */ -struct i2c_emul *tcpci_emul_get_i2c_emul(const struct emul *emul) -{ - struct tcpc_emul_data *tcpc_data = emul->data; - struct tcpci_ctx *ctx = tcpc_data->tcpci_ctx; - - return &ctx->common.emul; -} - -/** Check description in emul_tcpci.h */ void tcpci_emul_i2c_init(const struct emul *emul, const struct device *i2c_dev) { struct tcpc_emul_data *tcpc_data = emul->data; struct tcpci_ctx *ctx = tcpc_data->tcpci_ctx; - ctx->common.emul.api = &i2c_common_emul_api; ctx->common.emul.addr = tcpc_data->i2c_cfg.addr; - ctx->common.emul.parent = emul; ctx->common.i2c = i2c_dev; ctx->common.cfg = &tcpc_data->i2c_cfg; diff --git a/zephyr/emul/tcpc/emul_tcpci_generic.c b/zephyr/emul/tcpc/emul_tcpci_generic.c index 4ff05d1f40..0cc75fdf5b 100644 --- a/zephyr/emul/tcpc/emul_tcpci_generic.c +++ b/zephyr/emul/tcpc/emul_tcpci_generic.c @@ -15,6 +15,7 @@ LOG_MODULE_REGISTER(tcpci_generic_emul, CONFIG_TCPCI_EMUL_LOG_LEVEL); #include <ztest.h> #include "tcpm/tcpci.h" +#include "emul/emul_stub_device.h" #include "emul/emul_common_i2c.h" #include "emul/tcpc/emul_tcpci.h" @@ -22,7 +23,7 @@ LOG_MODULE_REGISTER(tcpci_generic_emul, CONFIG_TCPCI_EMUL_LOG_LEVEL); /** * @brief Function called for each byte of read message from TCPCI emulator * - * @param i2c_emul Pointer to I2C TCPCI emulator + * @param emul Pointer to I2C TCPCI emulator * @param reg First byte of last write message * @param val Pointer where byte to read should be stored * @param bytes Number of bytes already read @@ -30,14 +31,10 @@ LOG_MODULE_REGISTER(tcpci_generic_emul, CONFIG_TCPCI_EMUL_LOG_LEVEL); * @return 0 on success * @return -EIO on invalid read request */ -static int tcpci_generic_emul_read_byte(struct i2c_emul *i2c_emul, int reg, +static int tcpci_generic_emul_read_byte(const struct emul *emul, int reg, uint8_t *val, int bytes) { - const struct emul *emul; - - LOG_DBG("TCPCI Generic 0x%x: read reg 0x%x", i2c_emul->addr, reg); - - emul = i2c_emul->parent; + LOG_DBG("TCPCI Generic 0x%x: read reg 0x%x", emul->bus.i2c->addr, reg); return tcpci_emul_read_byte(emul, reg, val, bytes); } @@ -45,7 +42,7 @@ static int tcpci_generic_emul_read_byte(struct i2c_emul *i2c_emul, int reg, /** * @brief Function called for each byte of write message to TCPCI emulator * - * @param i2c_emul Pointer to I2C TCPCI emulator + * @param emul Pointer to I2C TCPCI emulator * @param reg First byte of write message * @param val Received byte of write message * @param bytes Number of bytes already received @@ -53,14 +50,10 @@ static int tcpci_generic_emul_read_byte(struct i2c_emul *i2c_emul, int reg, * @return 0 on success * @return -EIO on invalid write request */ -static int tcpci_generic_emul_write_byte(struct i2c_emul *i2c_emul, int reg, +static int tcpci_generic_emul_write_byte(const struct emul *emul, int reg, uint8_t val, int bytes) { - const struct emul *emul; - - LOG_DBG("TCPCI Generic 0x%x: write reg 0x%x", i2c_emul->addr, reg); - - emul = i2c_emul->parent; + LOG_DBG("TCPCI Generic 0x%x: write reg 0x%x", emul->bus.i2c->addr, reg); return tcpci_emul_write_byte(emul, reg, val, bytes); } @@ -68,22 +61,18 @@ static int tcpci_generic_emul_write_byte(struct i2c_emul *i2c_emul, int reg, /** * @brief Function called on the end of write message to TCPCI emulator * - * @param i2c_emul Pointer to I2C TCPCI emulator + * @param emul Pointer to I2C TCPCI emulator * @param reg Register which is written * @param msg_len Length of handled I2C message * * @return 0 on success * @return -EIO on error */ -static int tcpci_generic_emul_finish_write(struct i2c_emul *i2c_emul, int reg, +static int tcpci_generic_emul_finish_write(const struct emul *emul, int reg, int msg_len) { - const struct emul *emul; - - LOG_DBG("TCPCI Generic 0x%x: finish write reg 0x%x", i2c_emul->addr, - reg); - - emul = i2c_emul->parent; + LOG_DBG("TCPCI Generic 0x%x: finish write reg 0x%x", + emul->bus.i2c->addr, reg); return tcpci_emul_handle_write(emul, reg, msg_len); } @@ -92,14 +81,14 @@ static int tcpci_generic_emul_finish_write(struct i2c_emul *i2c_emul, int reg, * @brief Get currently accessed register, which always equals to selected * register from TCPCI emulator. * - * @param i2c_emul Pointer to I2C TCPCI emulator + * @param emul Pointer to I2C TCPCI emulator * @param reg First byte of last write message * @param bytes Number of bytes already handled from current message * @param read If currently handled is read message * * @return Currently accessed register */ -static int tcpci_generic_emul_access_reg(struct i2c_emul *i2c_emul, int reg, +static int tcpci_generic_emul_access_reg(const struct emul *emul, int reg, int bytes, bool read) { return reg; @@ -132,7 +121,6 @@ static int tcpci_generic_emul_init(const struct emul *emul, struct tcpc_emul_data *tcpc_data = emul->data; struct tcpci_ctx *tcpci_ctx = tcpc_data->tcpci_ctx; const struct device *i2c_dev; - int ret; i2c_dev = parent; @@ -143,16 +131,30 @@ static int tcpci_generic_emul_init(const struct emul *emul, tcpci_emul_i2c_init(emul, i2c_dev); - ret = i2c_emul_register(i2c_dev, emul->dev_label, - &tcpci_ctx->common.emul); - tcpci_generic_emul_reset(emul); - return ret; + return 0; +} + +static int i2c_tcpci_generic_emul_transfer(const struct emul *target, + struct i2c_msg *msgs, int num_msgs, + int addr) +{ + struct tcpc_emul_data *tcpc_data = target->data; + struct tcpci_ctx *tcpci_ctx = tcpc_data->tcpci_ctx; + + return i2c_common_emul_transfer_workhorse(target, &tcpci_ctx->common, + &tcpc_data->i2c_cfg, msgs, + num_msgs, addr); } -#define TCPCI_GENERIC_EMUL(n) \ - TCPCI_EMUL_DEFINE(n, tcpci_generic_emul_init, NULL, NULL) +struct i2c_emul_api i2c_tcpci_generic_emul_api = { + .transfer = i2c_tcpci_generic_emul_transfer, +}; + +#define TCPCI_GENERIC_EMUL(n) \ + TCPCI_EMUL_DEFINE(n, tcpci_generic_emul_init, NULL, NULL, \ + &i2c_tcpci_generic_emul_api) DT_INST_FOREACH_STATUS_OKAY(TCPCI_GENERIC_EMUL) @@ -170,3 +172,14 @@ tcpci_generic_emul_reset_rule_before(const struct ztest_unit_test *test, ZTEST_RULE(tcpci_generic_emul_reset, tcpci_generic_emul_reset_rule_before, NULL); #endif /* CONFIG_ZTEST_NEW_API */ + +DT_INST_FOREACH_STATUS_OKAY(EMUL_STUB_DEVICE); + +struct i2c_common_emul_data * +emul_tcpci_generic_get_i2c_common_data(const struct emul *emul) +{ + struct tcpc_emul_data *tcpc_data = emul->data; + struct tcpci_ctx *tcpci_ctx = tcpc_data->tcpci_ctx; + + return &tcpci_ctx->common; +} diff --git a/zephyr/include/emul/emul_bb_retimer.h b/zephyr/include/emul/emul_bb_retimer.h index 9c6a73c3f4..2e9b3cc8d7 100644 --- a/zephyr/include/emul/emul_bb_retimer.h +++ b/zephyr/include/emul/emul_bb_retimer.h @@ -43,7 +43,7 @@ * * @return Pointer to BB retimer emulator */ -struct i2c_emul *bb_emul_get(int ord); +const struct emul *bb_emul_get(int ord); /** * @brief Set value of given register of BB retimer @@ -52,7 +52,7 @@ struct i2c_emul *bb_emul_get(int ord); * @param reg Register address which value will be changed * @param val New value of the register */ -void bb_emul_set_reg(struct i2c_emul *emul, int reg, uint32_t val); +void bb_emul_set_reg(const struct emul *emul, int reg, uint32_t val); /** * @brief Get value of given register of BB retimer @@ -62,7 +62,7 @@ void bb_emul_set_reg(struct i2c_emul *emul, int reg, uint32_t val); * * @return Value of the register */ -uint32_t bb_emul_get_reg(struct i2c_emul *emul, int reg); +uint32_t bb_emul_get_reg(const struct emul *emul, int reg); /** * @brief Set if error should be generated when read only register is being @@ -71,7 +71,7 @@ uint32_t bb_emul_get_reg(struct i2c_emul *emul, int reg); * @param emul Pointer to BB retimer emulator * @param set Check for this error */ -void bb_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set); +void bb_emul_set_err_on_ro_write(const struct emul *emul, bool set); /** * @brief Set if error should be generated when reserved bits of register are @@ -80,7 +80,16 @@ void bb_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set); * @param emul Pointer to BB retimer emulator * @param set Check for this error */ -void bb_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set); +void bb_emul_set_err_on_rsvd_write(const struct emul *emul, bool set); + +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to bb retimer emulator + * @return Pointer to bb retimer emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_bb_retimer_get_i2c_common_data(const struct emul *emul); /** * @} diff --git a/zephyr/include/emul/emul_bma255.h b/zephyr/include/emul/emul_bma255.h index f5414e78b5..a1edd4ea01 100644 --- a/zephyr/include/emul/emul_bma255.h +++ b/zephyr/include/emul/emul_bma255.h @@ -65,7 +65,7 @@ * * @return Pointer to BMA255 emulator */ -struct i2c_emul *bma_emul_get(int ord); +const struct emul *bma_emul_get(int ord); /** * @brief Set value of given register of BMA255 @@ -74,7 +74,7 @@ struct i2c_emul *bma_emul_get(int ord); * @param reg Register address which value will be changed * @param val New value of the register */ -void bma_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); +void bma_emul_set_reg(const struct emul *emul, int reg, uint8_t val); /** * @brief Get value of given register of BMA255 @@ -84,7 +84,7 @@ void bma_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); * * @return Value of the register */ -uint8_t bma_emul_get_reg(struct i2c_emul *emul, int reg); +uint8_t bma_emul_get_reg(const struct emul *emul, int reg); /** * @brief Get internal value of offset for given axis @@ -94,7 +94,7 @@ uint8_t bma_emul_get_reg(struct i2c_emul *emul, int reg); * * @return Offset of given axis. LSB is 0.97mg */ -int16_t bma_emul_get_off(struct i2c_emul *emul, int axis); +int16_t bma_emul_get_off(const struct emul *emul, int axis); /** * @brief Set internal value of offset for given axis @@ -103,7 +103,7 @@ int16_t bma_emul_get_off(struct i2c_emul *emul, int axis); * @param axis Axis to access: 0 - X, 1 - Y, 2 - Z * @param val New value of offset. LSB is 0.97mg */ -void bma_emul_set_off(struct i2c_emul *emul, int axis, int16_t val); +void bma_emul_set_off(const struct emul *emul, int axis, int16_t val); /** * @brief Get internal value of accelerometer for given axis @@ -113,7 +113,7 @@ void bma_emul_set_off(struct i2c_emul *emul, int axis, int16_t val); * * @return Acceleration of given axis. LSB is 0.97mg */ -int16_t bma_emul_get_acc(struct i2c_emul *emul, int axis); +int16_t bma_emul_get_acc(const struct emul *emul, int axis); /** * @brief Set internal value of accelerometr for given axis @@ -122,7 +122,7 @@ int16_t bma_emul_get_acc(struct i2c_emul *emul, int axis); * @param axis Axis to access: 0 - X, 1 - Y, 2 - Z * @param val New value of accelerometer axis. LSB is 0.97mg */ -void bma_emul_set_acc(struct i2c_emul *emul, int axis, int16_t val); +void bma_emul_set_acc(const struct emul *emul, int axis, int16_t val); /** * @brief Set if error should be generated when fast compensation is triggered @@ -131,7 +131,7 @@ void bma_emul_set_acc(struct i2c_emul *emul, int axis, int16_t val); * @param emul Pointer to BMA255 emulator * @param set Check for this error */ -void bma_emul_set_err_on_cal_nrdy(struct i2c_emul *emul, bool set); +void bma_emul_set_err_on_cal_nrdy(const struct emul *emul, bool set); /** * @brief Set if error should be generated when fast compensation is triggered @@ -140,7 +140,7 @@ void bma_emul_set_err_on_cal_nrdy(struct i2c_emul *emul, bool set); * @param emul Pointer to BMA255 emulator * @param set Check for this error */ -void bma_emul_set_err_on_cal_bad_range(struct i2c_emul *emul, bool set); +void bma_emul_set_err_on_cal_bad_range(const struct emul *emul, bool set); /** * @brief Set if error should be generated when read only register is being @@ -149,7 +149,7 @@ void bma_emul_set_err_on_cal_bad_range(struct i2c_emul *emul, bool set); * @param emul Pointer to BMA255 emulator * @param set Check for this error */ -void bma_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set); +void bma_emul_set_err_on_ro_write(const struct emul *emul, bool set); /** * @brief Set if error should be generated when reserved bits of register are @@ -158,7 +158,7 @@ void bma_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set); * @param emul Pointer to BMA255 emulator * @param set Check for this error */ -void bma_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set); +void bma_emul_set_err_on_rsvd_write(const struct emul *emul, bool set); /** * @brief Set if error should be generated when MSB register is accessed before @@ -167,7 +167,7 @@ void bma_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set); * @param emul Pointer to BMA255 emulator * @param set Check for this error */ -void bma_emul_set_err_on_msb_first(struct i2c_emul *emul, bool set); +void bma_emul_set_err_on_msb_first(const struct emul *emul, bool set); /** * @brief Function calculate register that should be accessed when I2C message @@ -182,7 +182,16 @@ void bma_emul_set_err_on_msb_first(struct i2c_emul *emul, bool set); * * @retval Register address that should be accessed */ -int bma_emul_access_reg(struct i2c_emul *emul, int reg, int bytes, bool read); +int bma_emul_access_reg(const struct emul *emul, int reg, int bytes, bool read); + +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to BMA emulator + * @return Pointer to BMA emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_bma_get_i2c_common_data(const struct emul *emul); /** * @} diff --git a/zephyr/include/emul/emul_bmi.h b/zephyr/include/emul/emul_bmi.h index ad9cbb770d..2868a35918 100644 --- a/zephyr/include/emul/emul_bmi.h +++ b/zephyr/include/emul/emul_bmi.h @@ -147,7 +147,8 @@ struct bmi_emul_type_data { * * @return Register address that will be accessed */ - int (*access_reg)(struct i2c_emul *emul, int reg, int byte, bool read); + int (*access_reg)(const struct emul *emul, int reg, int byte, + bool read); /** * @brief Model specific write function. It should modify state of @@ -163,7 +164,7 @@ struct bmi_emul_type_data { * @return BMI_EMUL_ACCESS_E on RO register access * @return other on error */ - int (*handle_write)(uint8_t *regs, struct i2c_emul *emul, int reg, + int (*handle_write)(uint8_t *regs, const struct emul *emul, int reg, int byte, uint8_t val); /** * @brief Model specific read function. It should modify state of @@ -179,7 +180,7 @@ struct bmi_emul_type_data { * @return BMI_EMUL_ACCESS_E on WO register access * @return other on error */ - int (*handle_read)(uint8_t *regs, struct i2c_emul *emul, int reg, + int (*handle_read)(uint8_t *regs, const struct emul *emul, int reg, int byte, char *buf); /** * @brief Model specific reset function. It should modify state of @@ -188,7 +189,7 @@ struct bmi_emul_type_data { * @param regs Pointer to array of emulator's registers * @param emul Pointer to BMI emulator */ - void (*reset)(uint8_t *regs, struct i2c_emul *emul); + void (*reset)(uint8_t *regs, const struct emul *emul); /** Array of reserved bits mask for each register */ const uint8_t *rsvd_mask; @@ -226,7 +227,7 @@ const struct bmi_emul_type_data *get_bmi260_emul_type_data(void); * * @return Pointer to BMI emulator */ -struct i2c_emul *bmi_emul_get(int ord); +const struct emul *bmi_emul_get(int ord); /** * @brief Set value of given register of BMI @@ -235,7 +236,7 @@ struct i2c_emul *bmi_emul_get(int ord); * @param reg Register address which value will be changed * @param val New value of the register */ -void bmi_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); +void bmi_emul_set_reg(const struct emul *emul, int reg, uint8_t val); /** * @brief Get value of given register of BMI @@ -245,7 +246,7 @@ void bmi_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); * * @return Value of the register */ -uint8_t bmi_emul_get_reg(struct i2c_emul *emul, int reg); +uint8_t bmi_emul_get_reg(const struct emul *emul, int reg); /** * @brief Get internal value of offset for given axis and sensor @@ -256,7 +257,7 @@ uint8_t bmi_emul_get_reg(struct i2c_emul *emul, int reg); * @return Offset of given axis. LSB for accelerometer is 0.061mg and for * gyroscope is 0.0037°/s. */ -int16_t bmi_emul_get_off(struct i2c_emul *emul, enum bmi_emul_axis axis); +int16_t bmi_emul_get_off(const struct emul *emul, enum bmi_emul_axis axis); /** * @brief Set internal value of offset for given axis and sensor @@ -266,7 +267,7 @@ int16_t bmi_emul_get_off(struct i2c_emul *emul, enum bmi_emul_axis axis); * @param val New value of given axis. LSB for accelerometer is 0.061mg and for * gyroscope is 0.0037°/s. */ -void bmi_emul_set_off(struct i2c_emul *emul, enum bmi_emul_axis axis, +void bmi_emul_set_off(const struct emul *emul, enum bmi_emul_axis axis, int16_t val); /** @@ -278,7 +279,7 @@ void bmi_emul_set_off(struct i2c_emul *emul, enum bmi_emul_axis axis, * @return Sensor value of given axis. LSB for accelerometer is 0.061mg and for * gyroscope is 0.0037°/s. */ -int32_t bmi_emul_get_value(struct i2c_emul *emul, enum bmi_emul_axis axis); +int32_t bmi_emul_get_value(const struct emul *emul, enum bmi_emul_axis axis); /** * @brief Set internal value of sensor for given axis @@ -288,7 +289,7 @@ int32_t bmi_emul_get_value(struct i2c_emul *emul, enum bmi_emul_axis axis); * @param val New value of given axis. LSB for accelerometer is 0.061mg and for * gyroscope is 0.0037°/s. */ -void bmi_emul_set_value(struct i2c_emul *emul, enum bmi_emul_axis axis, +void bmi_emul_set_value(const struct emul *emul, enum bmi_emul_axis axis, int32_t val); /** @@ -298,7 +299,7 @@ void bmi_emul_set_value(struct i2c_emul *emul, enum bmi_emul_axis axis, * @param emul Pointer to BMI emulator * @param set Check for this error */ -void bmi_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set); +void bmi_emul_set_err_on_ro_write(const struct emul *emul, bool set); /** * @brief Set if error should be generated when reserved bits of register are @@ -307,7 +308,7 @@ void bmi_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set); * @param emul Pointer to BMI emulator * @param set Check for this error */ -void bmi_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set); +void bmi_emul_set_err_on_rsvd_write(const struct emul *emul, bool set); /** * @brief Set if error should be generated when write only register is read @@ -315,7 +316,7 @@ void bmi_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set); * @param emul Pointer to BMI emulator * @param set Check for this error */ -void bmi_emul_set_err_on_wo_read(struct i2c_emul *emul, bool set); +void bmi_emul_set_err_on_wo_read(const struct emul *emul, bool set); /** * @brief Set if effect of simulated command should take place after simulated @@ -324,7 +325,7 @@ void bmi_emul_set_err_on_wo_read(struct i2c_emul *emul, bool set); * @param emul Pointer to BMI emulator * @param set Simulate command execution time */ -void bmi_emul_simulate_cmd_exec_time(struct i2c_emul *emul, bool set); +void bmi_emul_simulate_cmd_exec_time(const struct emul *emul, bool set); /** * @brief Set number of skipped frames. It will generate skip frame on next @@ -333,7 +334,7 @@ void bmi_emul_simulate_cmd_exec_time(struct i2c_emul *emul, bool set); * @param emul Pointer to BMI emulator * @param skip Number of skipped frames */ -void bmi_emul_set_skipped_frames(struct i2c_emul *emul, uint8_t skip); +void bmi_emul_set_skipped_frames(const struct emul *emul, uint8_t skip); /** * @brief Clear all FIFO frames, set current frame to empty and reset fifo_skip @@ -343,14 +344,14 @@ void bmi_emul_set_skipped_frames(struct i2c_emul *emul, uint8_t skip); * @param tag_time Indicate if sensor time should be included in empty frame * @param header Indicate if header should be included in frame */ -void bmi_emul_flush_fifo(struct i2c_emul *emul, bool tag_time, bool header); +void bmi_emul_flush_fifo(const struct emul *emul, bool tag_time, bool header); /** * @brief Restore registers backed by NVM, reset sensor time and flush FIFO * * @param emul Pointer to BMI emulator */ -void bmi_emul_reset_common(struct i2c_emul *emul, bool tag_time, bool header); +void bmi_emul_reset_common(const struct emul *emul, bool tag_time, bool header); /** * @brief Set command end time to @p time ms from now @@ -358,14 +359,14 @@ void bmi_emul_reset_common(struct i2c_emul *emul, bool tag_time, bool header); * @param emul Pointer to BMI emulator * @param time After this amount of ms command should end */ -void bmi_emul_set_cmd_end_time(struct i2c_emul *emul, int time); +void bmi_emul_set_cmd_end_time(const struct emul *emul, int time); /** * @brief Check if command should end * * @param emul Pointer to BMI emulator */ -bool bmi_emul_is_cmd_end(struct i2c_emul *emul); +bool bmi_emul_is_cmd_end(const struct emul *emul); /** * @brief Append FIFO @p frame to the emulator list of frames. It can be read @@ -376,7 +377,8 @@ bool bmi_emul_is_cmd_end(struct i2c_emul *emul); * emulator may use this frame (until flush of FIFO or reading * it out through I2C) */ -void bmi_emul_append_frame(struct i2c_emul *emul, struct bmi_emul_frame *frame); +void bmi_emul_append_frame(const struct emul *emul, + struct bmi_emul_frame *frame); /** * @brief Get length of all frames that are on the emulator list of frames. @@ -385,7 +387,7 @@ void bmi_emul_append_frame(struct i2c_emul *emul, struct bmi_emul_frame *frame); * @param tag_time Indicate if sensor time should be included in empty frame * @param header Indicate if header should be included in frame */ -uint16_t bmi_emul_fifo_len(struct i2c_emul *emul, bool tag_time, bool header); +uint16_t bmi_emul_fifo_len(const struct emul *emul, bool tag_time, bool header); /** * @brief Get next byte that should be returned on FIFO data access. @@ -400,7 +402,7 @@ uint16_t bmi_emul_fifo_len(struct i2c_emul *emul, bool tag_time, bool header); * * @return FIFO data byte */ -uint8_t bmi_emul_get_fifo_data(struct i2c_emul *emul, int byte, bool tag_time, +uint8_t bmi_emul_get_fifo_data(const struct emul *emul, int byte, bool tag_time, bool header, int acc_shift, int gyr_shift); /** @@ -418,9 +420,18 @@ uint8_t bmi_emul_get_fifo_data(struct i2c_emul *emul, int byte, bool tag_time, * @param gyr_off_en Indicate if gyroscope offset should be included to * sensor data value */ -void bmi_emul_state_to_reg(struct i2c_emul *emul, int acc_shift, int gyr_shift, - int acc_reg, int gyr_reg, int sensortime_reg, - bool acc_off_en, bool gyr_off_en); +void bmi_emul_state_to_reg(const struct emul *emul, int acc_shift, + int gyr_shift, int acc_reg, int gyr_reg, + int sensortime_reg, bool acc_off_en, + bool gyr_off_en); +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to BMI emulator + * @return Pointer to BMI emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_bmi_get_i2c_common_data(const struct emul *emul); /** * @} diff --git a/zephyr/include/emul/emul_common_i2c.h b/zephyr/include/emul/emul_common_i2c.h index cf8ccf0f3b..7ab86ec868 100644 --- a/zephyr/include/emul/emul_common_i2c.h +++ b/zephyr/include/emul/emul_common_i2c.h @@ -67,20 +67,21 @@ enum i2c_common_emul_msg_state { * @brief Function type that is used by I2C device emulator for first byte of * I2C write message. * - * @param emul Pointer to emulator + * @param target Pointer to emulator * @param reg Address which is now accessed by write command (first byte of I2C * write message) * * @return 0 on success * @return -EIO on error */ -typedef int (*i2c_common_emul_start_write_func)(struct i2c_emul *emul, int reg); +typedef int (*i2c_common_emul_start_write_func)(const struct emul *target, + int reg); /** * @brief Function type that is used by I2C device emulator at the end of * I2C write message. * - * @param emul Pointer to emulator + * @param target Pointer to emulator * @param reg Address which is now accessed by write command (first byte of I2C * write message) * @param bytes Number of bytes received from the I2C write message @@ -88,14 +89,14 @@ typedef int (*i2c_common_emul_start_write_func)(struct i2c_emul *emul, int reg); * @return 0 on success * @return -EIO on error */ -typedef int (*i2c_common_emul_finish_write_func)(struct i2c_emul *emul, int reg, - int bytes); +typedef int (*i2c_common_emul_finish_write_func)(const struct emul *target, + int reg, int bytes); /** * @brief Function type that is used by I2C device emulator on each byte of * I2C write message (except first byte). * - * @param emul Pointer to emulator + * @param target Pointer to emulator * @param reg Address which is now accessed by write command (first byte of I2C * write message) * @param val Value of current byte @@ -105,27 +106,28 @@ typedef int (*i2c_common_emul_finish_write_func)(struct i2c_emul *emul, int reg, * @return 0 on success * @return -EIO on error */ -typedef int (*i2c_common_emul_write_byte_func)(struct i2c_emul *emul, int reg, - uint8_t val, int bytes); +typedef int (*i2c_common_emul_write_byte_func)(const struct emul *target, + int reg, uint8_t val, int bytes); /** * @brief Function type that is used by I2C device emulator before first byte of * I2C read message. * - * @param emul Pointer to emulator + * @param target Pointer to emulator * @param reg Address which is now accessed by read command (first byte of last * I2C write message) * * @return 0 on success * @return -EIO on error */ -typedef int (*i2c_common_emul_start_read_func)(struct i2c_emul *emul, int reg); +typedef int (*i2c_common_emul_start_read_func)(const struct emul *target, + int reg); /** * @brief Function type that is used by I2C device emulator at the end of * I2C read message. * - * @param emul Pointer to emulator + * @param target Pointer to emulator * @param reg Address which is now accessed by read command (first byte of last * I2C write message) * @param bytes Number of bytes responeded to the I2C read message @@ -133,14 +135,14 @@ typedef int (*i2c_common_emul_start_read_func)(struct i2c_emul *emul, int reg); * @return 0 on success * @return -EIO on error */ -typedef int (*i2c_common_emul_finish_read_func)(struct i2c_emul *emul, int reg, - int bytes); +typedef int (*i2c_common_emul_finish_read_func)(const struct emul *target, + int reg, int bytes); /** * @brief Function type that is used by I2C device emulator on each byte of * I2C read message. * - * @param emul Pointer to emulator + * @param target Pointer to emulator * @param reg Address which is now accessed by read command (first byte of last * I2C write message) * @param val Pointer to buffer where current response byte should be stored @@ -150,8 +152,8 @@ typedef int (*i2c_common_emul_finish_read_func)(struct i2c_emul *emul, int reg, * @return 0 on success * @return -EIO on error */ -typedef int (*i2c_common_emul_read_byte_func)(struct i2c_emul *emul, int reg, - uint8_t *val, int bytes); +typedef int (*i2c_common_emul_read_byte_func)(const struct emul *target, + int reg, uint8_t *val, int bytes); /** * @brief Function type that is used by I2C device emulator to select register @@ -159,7 +161,7 @@ typedef int (*i2c_common_emul_read_byte_func)(struct i2c_emul *emul, int reg, * @ref i2c_common_emul_set_read_fail_reg and * @ref i2c_common_emul_set_write_fail_reg * - * @param emul Pointer to emulator + * @param target Pointer to emulator * @param reg Address which is now accessed by read/write command (first byte * of last I2C write message) * @param bytes Number of bytes already processed in the I2C message handler @@ -169,14 +171,14 @@ typedef int (*i2c_common_emul_read_byte_func)(struct i2c_emul *emul, int reg, * @return Register address that should be compared with user-defined fail * register */ -typedef int (*i2c_common_emul_access_reg_func)(struct i2c_emul *emul, int reg, - int bytes, bool read); +typedef int (*i2c_common_emul_access_reg_func)(const struct emul *target, + int reg, int bytes, bool read); /** * @brief Custom function type that is used as user-defined callback in read * I2C messages handling. * - * @param emul Pointer to emulator + * @param target Pointer to emulator * @param reg Address which is now accessed by read command (first byte of last * I2C write message) * @param val Pointer to buffer where current response byte should be stored @@ -188,14 +190,14 @@ typedef int (*i2c_common_emul_access_reg_func)(struct i2c_emul *emul, int reg, * @return 1 continue with normal emulator handler * @return negative on error */ -typedef int (*i2c_common_emul_read_func)(struct i2c_emul *emul, int reg, +typedef int (*i2c_common_emul_read_func)(const struct emul *target, int reg, uint8_t *val, int bytes, void *data); /** * @brief Custom function type that is used as user-defined callback in write * I2C messages handling. * - * @param emul Pointer to emulator + * @param target Pointer to emulator * @param reg Address which is now accessed by write command (first byte of I2C * write message) * @param val Value of current byte @@ -207,7 +209,7 @@ typedef int (*i2c_common_emul_read_func)(struct i2c_emul *emul, int reg, * @return 1 continue with normal emulator handler * @return negative on error */ -typedef int (*i2c_common_emul_write_func)(struct i2c_emul *emul, int reg, +typedef int (*i2c_common_emul_write_func)(const struct emul *target, int reg, uint8_t val, int bytes, void *data); /** Static configuration, common for all i2c emulators */ @@ -283,31 +285,32 @@ extern struct i2c_emul_api i2c_common_emul_api; * @brief Lock access to emulator properties. After acquiring lock, user * may change emulator behaviour in multi-thread setup. * - * @param emul Pointer to emulator + * @param common_data Pointer to emulator common data * @param timeout Timeout in getting lock * * @return k_mutex_lock return code */ -int i2c_common_emul_lock_data(struct i2c_emul *emul, k_timeout_t timeout); +int i2c_common_emul_lock_data(struct i2c_common_emul_data *common_data, + k_timeout_t timeout); /** * @brief Unlock access to emulator properties. * - * @param emul Pointer to emulator + * @param common_data Pointer to emulator common data * * @return k_mutex_unlock return code */ -int i2c_common_emul_unlock_data(struct i2c_emul *emul); +int i2c_common_emul_unlock_data(struct i2c_common_emul_data *common_data); /** * @brief Set write handler for I2C messages. This function is called before * generic handler. * - * @param emul Pointer to emulator + * @param common_data Pointer to emulator common data * @param func Pointer to custom function * @param data User data passed on call of custom function */ -void i2c_common_emul_set_write_func(struct i2c_emul *emul, +void i2c_common_emul_set_write_func(struct i2c_common_emul_data *common_data, i2c_common_emul_write_func func, void *data); @@ -315,30 +318,32 @@ void i2c_common_emul_set_write_func(struct i2c_emul *emul, * @brief Set read handler for I2C messages. This function is called before * generic handler. * - * @param emul Pointer to emulator + * @param common_data Pointer to emulator common data * @param func Pointer to custom function * @param data User data passed on call of custom function */ -void i2c_common_emul_set_read_func(struct i2c_emul *emul, +void i2c_common_emul_set_read_func(struct i2c_common_emul_data *common_data, i2c_common_emul_read_func func, void *data); /** * @brief Setup fail on read of given register of emulator * - * @param emul Pointer to emulator + * @param common_data Pointer to emulator common data * @param reg Register address or one of special values * (I2C_COMMON_EMUL_FAIL_ALL_REG, I2C_COMMON_EMUL_NO_FAIL_REG) */ -void i2c_common_emul_set_read_fail_reg(struct i2c_emul *emul, int reg); +void i2c_common_emul_set_read_fail_reg(struct i2c_common_emul_data *common_data, + int reg); /** * @brief Setup fail on write of given register of emulator * - * @param emul Pointer to emulator + * @param common_data Pointer to emulator common data * @param reg Register address or one of special values * (I2C_COMMON_EMUL_FAIL_ALL_REG, I2C_COMMON_EMUL_NO_FAIL_REG) */ -void i2c_common_emul_set_write_fail_reg(struct i2c_emul *emul, int reg); +void i2c_common_emul_set_write_fail_reg( + struct i2c_common_emul_data *common_data, int reg); /** * @biref Emulate an I2C transfer to an emulator @@ -347,7 +352,7 @@ void i2c_common_emul_set_write_fail_reg(struct i2c_emul *emul, int reg); * I2C message, calling user custom functions, failing on reading/writing * registers selected by user and calling device specific functions. * - * @param emul I2C emulation information + * @param target The target peripheral emulated * @param msgs List of messages to process * @param num_msgs Number of messages to process * @param addr Address of the I2C target device @@ -355,9 +360,15 @@ void i2c_common_emul_set_write_fail_reg(struct i2c_emul *emul, int reg); * @retval 0 If successful * @retval -EIO General input / output error */ -int i2c_common_emul_transfer(struct i2c_emul *emul, struct i2c_msg *msgs, +int i2c_common_emul_transfer(const struct emul *target, struct i2c_msg *msgs, int num_msgs, int addr); +int i2c_common_emul_transfer_workhorse(const struct emul *target, + struct i2c_common_emul_data *data, + const struct i2c_common_emul_cfg *cfg, + struct i2c_msg *msgs, int num_msgs, + int addr); + /** * @brief Initialize common emulator data structure * diff --git a/zephyr/include/emul/emul_isl923x.h b/zephyr/include/emul/emul_isl923x.h index 5842cdcf02..8a1226839c 100644 --- a/zephyr/include/emul/emul_isl923x.h +++ b/zephyr/include/emul/emul_isl923x.h @@ -94,6 +94,15 @@ void raa489000_emul_set_acok_pin(const struct emul *emulator, uint16_t value); * @param reg The address of the register to query * @return The 16-bit value of the register */ -uint16_t isl923x_emul_peek_reg(struct i2c_emul *i2c_emul, int reg); +uint16_t isl923x_emul_peek_reg(const struct emul *emul, int reg); + +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to ISL923X emulator + * @return Pointer to ISL923X emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_isl923x_get_i2c_common_data(const struct emul *emul); #endif /* ZEPHYR_INCLUDE_EMUL_EMUL_ISL923X_H_ */ diff --git a/zephyr/include/emul/emul_lis2dw12.h b/zephyr/include/emul/emul_lis2dw12.h index a0ca6be8b5..e12f9411df 100644 --- a/zephyr/include/emul/emul_lis2dw12.h +++ b/zephyr/include/emul/emul_lis2dw12.h @@ -10,14 +10,6 @@ #include <zephyr/drivers/i2c_emul.h> /** - * @brief The the i2c emulator pointer from the top level emul. - * - * @param emul The emulator to query - * @return Pointer to the i2c emulator struct - */ -struct i2c_emul *lis2dw12_emul_to_i2c_emul(const struct emul *emul); - -/** * @brief Reset the state of the lis2dw12 emulator. * * @param emul The emulator to reset. @@ -54,7 +46,7 @@ uint32_t lis2dw12_emul_get_soft_reset_count(const struct emul *emul); * @param reg The register to access * @return The value of the register */ -uint8_t lis2dw12_emul_peek_reg(struct i2c_emul *emul, int reg); +uint8_t lis2dw12_emul_peek_reg(const struct emul *emul, int reg); /** * @brief Retrieves the ODR[3:0] bits from CRTL1 register @@ -62,7 +54,7 @@ uint8_t lis2dw12_emul_peek_reg(struct i2c_emul *emul, int reg); * @param emul The emulator to query * @return The ODR bits, right-aligned */ -uint8_t lis2dw12_emul_peek_odr(struct i2c_emul *emul); +uint8_t lis2dw12_emul_peek_odr(const struct emul *emul); /** * @brief Retrieves the MODE[1:0] bits from CRTL1 register @@ -70,7 +62,7 @@ uint8_t lis2dw12_emul_peek_odr(struct i2c_emul *emul); * @param emul The emulator to query * @return The MODE bits, right-aligned */ -uint8_t lis2dw12_emul_peek_mode(struct i2c_emul *emul); +uint8_t lis2dw12_emul_peek_mode(const struct emul *emul); /** * @brief Retrieves the LPMODE[1:0] bits from CRTL1 register @@ -78,7 +70,7 @@ uint8_t lis2dw12_emul_peek_mode(struct i2c_emul *emul); * @param emul The emulator to query * @return The LPMODE bits, right-aligned */ -uint8_t lis2dw12_emul_peek_lpmode(struct i2c_emul *emul); +uint8_t lis2dw12_emul_peek_lpmode(const struct emul *emul); /** * @brief Updates the current 3-axis acceleromter reading and @@ -96,4 +88,13 @@ int lis2dw12_emul_set_accel_reading(const struct emul *emul, intv3_t reading); */ void lis2dw12_emul_clear_accel_reading(const struct emul *emul); +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to LIS2DW12 emulator + * @return Pointer to LIS2DW12 emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_lis2dw12_get_i2c_common_data(const struct emul *emul); + #endif /* ZEPHYR_INCLUDE_EMUL_EMUL_LIS2DW12_H_ */ diff --git a/zephyr/include/emul/emul_ln9310.h b/zephyr/include/emul/emul_ln9310.h index 6f34a15f93..7cb1dc5d20 100644 --- a/zephyr/include/emul/emul_ln9310.h +++ b/zephyr/include/emul/emul_ln9310.h @@ -80,4 +80,13 @@ bool ln9310_emul_is_init(const struct emul *emulator); */ struct i2c_emul *ln9310_emul_get_i2c_emul(const struct emul *emulator); +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to LN9310 emulator + * @return Pointer to LN9310 emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_ln9310_get_i2c_common_data(const struct emul *emul); + #endif /* ZEPHYR_INCLUDE_EMUL_EMUL_LN9310_H_ */ diff --git a/zephyr/include/emul/emul_pi3usb9201.h b/zephyr/include/emul/emul_pi3usb9201.h index 93e87c20e0..bedbd608f0 100644 --- a/zephyr/include/emul/emul_pi3usb9201.h +++ b/zephyr/include/emul/emul_pi3usb9201.h @@ -28,7 +28,7 @@ * * @return Pointer to pi3usb9201 emulator */ -struct i2c_emul *pi3usb9201_emul_get(int ord); +const struct emul *pi3usb9201_emul_get(int ord); /** * @brief Set value of given register of pi3usb9201 @@ -39,7 +39,7 @@ struct i2c_emul *pi3usb9201_emul_get(int ord); * * @return 0 on success or error */ -int pi3usb9201_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); +int pi3usb9201_emul_set_reg(const struct emul *emul, int reg, uint8_t val); /** * @brief Get value of given register of pi3usb9201 @@ -50,6 +50,6 @@ int pi3usb9201_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); * * @return 0 on success or error */ -int pi3usb9201_emul_get_reg(struct i2c_emul *emul, int reg, uint8_t *val); +int pi3usb9201_emul_get_reg(const struct emul *emul, int reg, uint8_t *val); #endif /* __EMUL_PI3USB9201_H */ diff --git a/zephyr/include/emul/emul_smart_battery.h b/zephyr/include/emul/emul_smart_battery.h index fa55f23fa9..0711ea82fb 100644 --- a/zephyr/include/emul/emul_smart_battery.h +++ b/zephyr/include/emul/emul_smart_battery.h @@ -124,7 +124,7 @@ struct sbat_emul_bat_data { * * @return Pointer to smart battery emulator */ -struct i2c_emul *sbat_emul_get_ptr(int ord); +const struct emul *sbat_emul_get_ptr(int ord); /** * @brief Function which allows to get properties of emulated smart battery @@ -133,7 +133,7 @@ struct i2c_emul *sbat_emul_get_ptr(int ord); * * @return Pointer to smart battery properties */ -struct sbat_emul_bat_data *sbat_emul_get_bat_data(struct i2c_emul *emul); +struct sbat_emul_bat_data *sbat_emul_get_bat_data(const struct emul *emul); /** * @brief Convert date to format used by smart battery @@ -160,7 +160,7 @@ uint16_t sbat_emul_date_to_word(unsigned int day, unsigned int month, * @return 1 if command is unknown or return type different then word * @return negative on error while reading value */ -int sbat_emul_get_word_val(struct i2c_emul *emul, int cmd, uint16_t *val); +int sbat_emul_get_word_val(const struct emul *emul, int cmd, uint16_t *val); /** * @brief Function which gets return value for read commands that returns block @@ -175,7 +175,7 @@ int sbat_emul_get_word_val(struct i2c_emul *emul, int cmd, uint16_t *val); * @return 1 if command is unknown or return type different then word * @return negative on error while reading value */ -int sbat_emul_get_block_data(struct i2c_emul *emul, int cmd, uint8_t **blk, +int sbat_emul_get_block_data(const struct emul *emul, int cmd, uint8_t **blk, int *len); /** @@ -188,10 +188,19 @@ int sbat_emul_get_block_data(struct i2c_emul *emul, int cmd, uint8_t **blk, * @param len Length of the response * @param fail If emulator should fail to send response */ -void sbat_emul_set_response(struct i2c_emul *emul, int cmd, uint8_t *buf, +void sbat_emul_set_response(const struct emul *emul, int cmd, uint8_t *buf, int len, bool fail); /** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to smart_battery emulator + * @return Pointer to smart_battery emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_smart_battery_get_i2c_common_data(const struct emul *emul); + +/** * @} */ diff --git a/zephyr/include/emul/emul_sn5s330.h b/zephyr/include/emul/emul_sn5s330.h index cc5576819e..72a9a98689 100644 --- a/zephyr/include/emul/emul_sn5s330.h +++ b/zephyr/include/emul/emul_sn5s330.h @@ -47,4 +47,13 @@ void sn5s330_emul_make_vbus_overcurrent(const struct emul *emul); */ void sn5s330_emul_lower_vbus_below_minv(const struct emul *emul); +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to SN5S330 emulator + * @return Pointer to SN5S330 emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_sn5s330_get_i2c_common_data(const struct emul *emul); + #endif /* ZEPHYR_INCLUDE_EMUL_EMUL_SN5S330_H_ */ diff --git a/zephyr/include/emul/emul_stub_device.h b/zephyr/include/emul/emul_stub_device.h new file mode 100644 index 0000000000..b7be376dda --- /dev/null +++ b/zephyr/include/emul/emul_stub_device.h @@ -0,0 +1,42 @@ +/* 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. + */ + +#ifndef ZEPHYR_INCLUDE_EMUL_STUB_DEVICE_H_ +#define ZEPHYR_INCLUDE_EMUL_STUB_DEVICE_H_ + +#include <zephyr/device.h> +#include <zephyr/devicetree.h> + +/* + * Needed for emulators without corresponding DEVICE_DT_DEFINE drivers + */ + +struct emul_stub_dev_data { + /* Stub */ +}; +struct emul_stub_dev_config { + /* Stub */ +}; +struct emul_stub_dev_api { + /* Stub */ +}; + +/* For every instance of a DT_DRV_COMPAT stub out a device for that instance */ +#define EMUL_STUB_DEVICE(n) \ + __maybe_unused static int emul_init_stub_##n(const struct device *dev) \ + { \ + ARG_UNUSED(dev); \ + return 0; \ + } \ + \ + /* Since this is only stub, allocate the structs once. */ \ + static struct emul_stub_dev_data stub_data_##n; \ + static struct emul_stub_dev_config stub_config_##n; \ + static struct emul_stub_dev_api stub_api_##n; \ + DEVICE_DT_INST_DEFINE(n, &emul_init_stub_##n, NULL, &stub_data_##n, \ + &stub_config_##n, POST_KERNEL, 1, \ + &stub_api_##n); + +#endif /* ZEPHYR_INCLUDE_EMUL_STUB_DEVICE_H_ */ diff --git a/zephyr/include/emul/emul_syv682x.h b/zephyr/include/emul/emul_syv682x.h index 72ce2a7e8a..7de229884f 100644 --- a/zephyr/include/emul/emul_syv682x.h +++ b/zephyr/include/emul/emul_syv682x.h @@ -110,7 +110,7 @@ * * @return Pointer to smart battery emulator */ -struct i2c_emul *syv682x_emul_get(int ord); +const struct emul *syv682x_emul_get(int ord); /** * @brief Set the underlying interrupt conditions affecting the SYV682x @@ -122,7 +122,7 @@ struct i2c_emul *syv682x_emul_get(int ord); * conditions; only the bits in SYV682X_CONTROL_4_INT_MASK have * an effect. */ -void syv682x_emul_set_condition(struct i2c_emul *emul, uint8_t status, +void syv682x_emul_set_condition(const struct emul *emul, uint8_t status, uint8_t control_4); /** @@ -133,7 +133,7 @@ void syv682x_emul_set_condition(struct i2c_emul *emul, uint8_t status, * @param emul SYV682x emulator * @param reads The number of reads of CONTROL_3 to keep BUSY set for */ -void syv682x_emul_set_busy_reads(struct i2c_emul *emul, int reads); +void syv682x_emul_set_busy_reads(const struct emul *emul, int reads); /** * @brief Set value of a register of SYV682x @@ -144,7 +144,7 @@ void syv682x_emul_set_busy_reads(struct i2c_emul *emul, int reads); * * @return 0 on success, error code on error */ -int syv682x_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); +int syv682x_emul_set_reg(const struct emul *emul, int reg, uint8_t val); /** * @brief Get value of a register of SYV682x @@ -155,6 +155,15 @@ int syv682x_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); * * @return 0 on success, error code on error */ -int syv682x_emul_get_reg(struct i2c_emul *emul, int reg, uint8_t *val); +int syv682x_emul_get_reg(const struct emul *emul, int reg, uint8_t *val); + +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to SYV682X emulator + * @return Pointer to SYV682X emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_syv682x_get_i2c_common_data(const struct emul *emul); #endif /* __EMUL_SYV682X_H */ diff --git a/zephyr/include/emul/emul_tcs3400.h b/zephyr/include/emul/emul_tcs3400.h index e0b2e44c73..efc6349c7d 100644 --- a/zephyr/include/emul/emul_tcs3400.h +++ b/zephyr/include/emul/emul_tcs3400.h @@ -81,7 +81,7 @@ enum tcs_emul_axis { * * @return Pointer to TCS3400 emulator */ -struct i2c_emul *tcs_emul_get(int ord); +const struct emul *tcs_emul_get(int ord); /** * @brief Set value of given register of TCS3400 @@ -90,7 +90,7 @@ struct i2c_emul *tcs_emul_get(int ord); * @param reg Register address which value will be changed * @param val New value of the register */ -void tcs_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); +void tcs_emul_set_reg(const struct emul *emul, int reg, uint8_t val); /** * @brief Get value of given register of TCS3400 @@ -100,7 +100,7 @@ void tcs_emul_set_reg(struct i2c_emul *emul, int reg, uint8_t val); * * @return Value of the register */ -uint8_t tcs_emul_get_reg(struct i2c_emul *emul, int reg); +uint8_t tcs_emul_get_reg(const struct emul *emul, int reg); /** * @brief Get internal value of light sensor for given axis @@ -110,7 +110,7 @@ uint8_t tcs_emul_get_reg(struct i2c_emul *emul, int reg); * * @return Value of given axis with gain x64 and 256 integration cycles */ -int tcs_emul_get_val(struct i2c_emul *emul, enum tcs_emul_axis axis); +int tcs_emul_get_val(const struct emul *emul, enum tcs_emul_axis axis); /** * @brief Set internal value of light sensor for given axis @@ -120,7 +120,8 @@ int tcs_emul_get_val(struct i2c_emul *emul, enum tcs_emul_axis axis); * @param val New value of light sensor for given axis with gain x64 and * 256 integration cycles */ -void tcs_emul_set_val(struct i2c_emul *emul, enum tcs_emul_axis axis, int val); +void tcs_emul_set_val(const struct emul *emul, enum tcs_emul_axis axis, + int val); /** * @brief Set if error should be generated when read only register is being @@ -129,7 +130,7 @@ void tcs_emul_set_val(struct i2c_emul *emul, enum tcs_emul_axis axis, int val); * @param emul Pointer to TCS3400 emulator * @param set Check for this error */ -void tcs_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set); +void tcs_emul_set_err_on_ro_write(const struct emul *emul, bool set); /** * @brief Set if error should be generated when reserved bits of register are @@ -138,7 +139,7 @@ void tcs_emul_set_err_on_ro_write(struct i2c_emul *emul, bool set); * @param emul Pointer to TCS3400 emulator * @param set Check for this error */ -void tcs_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set); +void tcs_emul_set_err_on_rsvd_write(const struct emul *emul, bool set); /** * @brief Set if error should be generated when MSB register is accessed before @@ -147,7 +148,16 @@ void tcs_emul_set_err_on_rsvd_write(struct i2c_emul *emul, bool set); * @param emul Pointer to TCS3400 emulator * @param set Check for this error */ -void tcs_emul_set_err_on_msb_first(struct i2c_emul *emul, bool set); +void tcs_emul_set_err_on_msb_first(const struct emul *emul, bool set); + +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to LN9310 emulator + * @return Pointer to TCS3400 emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_tcs3400_get_i2c_common_data(const struct emul *emul); /** * @} diff --git a/zephyr/include/emul/i2c_mock.h b/zephyr/include/emul/i2c_mock.h index e52c4e7440..f1f3cc310c 100644 --- a/zephyr/include/emul/i2c_mock.h +++ b/zephyr/include/emul/i2c_mock.h @@ -32,4 +32,13 @@ struct i2c_emul *i2c_mock_to_i2c_emul(const struct emul *emul); */ uint16_t i2c_mock_get_addr(const struct emul *emul); +/** + * @brief Saves current internal state of sensors to emulator's registers. + * + * @param emul Pointer to i2c_mock emulator + * @return Pointer to i2c_mock emulator associated i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_i2c_mock_get_i2c_common_data(const struct emul *emul); + #endif /* ZEPHYR_INCLUDE_EMUL_I2C_MOCK_H_ */ diff --git a/zephyr/include/emul/tcpc/emul_ps8xxx.h b/zephyr/include/emul/tcpc/emul_ps8xxx.h index 75091f2232..afaa6eb253 100644 --- a/zephyr/include/emul/tcpc/emul_ps8xxx.h +++ b/zephyr/include/emul/tcpc/emul_ps8xxx.h @@ -56,10 +56,11 @@ enum ps8xxx_emul_port { * @param port Select which "hidden" I2C device should be obtained * * @return NULL if given "hidden" I2C device cannot be found - * @return pointer to "hidden" I2C device + * @return pointer to "hidden" device i2c_common_emul_data */ -struct i2c_emul *ps8xxx_emul_get_i2c_emul(const struct emul *emul, - enum ps8xxx_emul_port port); +struct i2c_common_emul_data * +ps8xxx_emul_get_i2c_common_data(const struct emul *emul, + enum ps8xxx_emul_port port); /** * @brief Get pointer to TCPCI emulator that is base for PS8xxx emulator diff --git a/zephyr/include/emul/tcpc/emul_tcpci.h b/zephyr/include/emul/tcpc/emul_tcpci.h index 9841915d1e..9d1fa0dabc 100644 --- a/zephyr/include/emul/tcpc/emul_tcpci.h +++ b/zephyr/include/emul/tcpc/emul_tcpci.h @@ -85,6 +85,7 @@ struct tcpci_ctx { /** Run-time data used by the emulator */ struct tcpc_emul_data { + struct i2c_common_emul_data common; /** Pointer to the common TCPCI emulator context */ struct tcpci_ctx *tcpci_ctx; @@ -94,7 +95,7 @@ struct tcpc_emul_data { const struct i2c_common_emul_cfg i2c_cfg; }; -#define TCPCI_EMUL_DEFINE(n, init, cfg_ptr, chip_data_ptr) \ +#define TCPCI_EMUL_DEFINE(n, init, cfg_ptr, chip_data_ptr, bus_api) \ static uint8_t tcpci_emul_tx_buf_##n[128]; \ static struct tcpci_emul_msg tcpci_emul_tx_msg_##n = { \ .buf = tcpci_emul_tx_buf_##n, \ @@ -123,7 +124,7 @@ struct tcpc_emul_data { .addr = DT_INST_REG_ADDR(n), \ }, \ }; \ - EMUL_DEFINE(init, DT_DRV_INST(n), cfg_ptr, &tcpc_emul_data_##n) + EMUL_DEFINE(init, DT_DRV_INST(n), cfg_ptr, &tcpc_emul_data_##n, bus_api) /** Response from TCPCI specific device operations */ enum tcpci_emul_ops_resp { @@ -258,15 +259,6 @@ struct tcpci_emul_partner_ops { }; /** - * @brief Get i2c_emul for TCPCI emulator - * - * @param emul Pointer to TCPC emulator - * - * @return Pointer to I2C TCPCI emulator - */ -struct i2c_emul *tcpci_emul_get_i2c_emul(const struct emul *emul); - -/** * @brief Set value of given register of TCPCI * * @param emul Pointer to TCPC emulator @@ -456,6 +448,15 @@ void tcpci_emul_partner_msg_status(const struct emul *emul, enum tcpci_emul_tx_status status); /** + * @brief Gets the common data associated with the tcpci chip overall + * + * @param emul Pointer to TCPC emulator + * @return Pointer to struct i2c_common_emul_data + */ +struct i2c_common_emul_data * +emul_tcpci_generic_get_i2c_common_data(const struct emul *emul); + +/** * @} */ diff --git a/zephyr/test/ap_power/src/signals.c b/zephyr/test/ap_power/src/signals.c index e50c8dd09b..8798483a19 100644 --- a/zephyr/test/ap_power/src/signals.c +++ b/zephyr/test/ap_power/src/signals.c @@ -20,6 +20,7 @@ #include "power_signals.h" #include "ec_tasks.h" +#include "emul/emul_stub_device.h" #include "gpio.h" #include "gpio/gpio.h" #include "gpio/gpio_int.h" @@ -420,3 +421,7 @@ static void init_signals(void *data) */ ZTEST_SUITE(signals, ap_power_predicate_post_main, init_dev, init_signals, NULL, NULL); + +/* 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/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), |