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 /zephyr/test/drivers/default/src/bmi160.c | |
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>
Diffstat (limited to 'zephyr/test/drivers/default/src/bmi160.c')
-rw-r--r-- | zephyr/test/drivers/default/src/bmi160.c | 290 |
1 files changed, 174 insertions, 116 deletions
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 */ |