diff options
author | Tomasz Michalec <tm@semihalf.com> | 2021-09-02 17:23:34 +0200 |
---|---|---|
committer | Commit Bot <commit-bot@chromium.org> | 2021-09-03 15:35:24 +0000 |
commit | 2766160a4bcec633694492915b2a8d0c3068851f (patch) | |
tree | 5e5062f28c043ba04835496940e3b888bd6a0e46 /zephyr/test | |
parent | 7c543a2dc09f9302eaf4ae9a2f529a0fb9f88244 (diff) | |
download | chrome-ec-2766160a4bcec633694492915b2a8d0c3068851f.tar.gz |
zephyr: emul: Use common I2C code in emulators
Align following emulators to use common I2C code:
- bb_retimer
- bma255
- bmi (both 160 and 260)
- smart_battery
- tcs3400
BUG=none
BRANCH=none
TEST=make configure --test zephyr/test/drivers
Signed-off-by: Tomasz Michalec <tm@semihalf.com>
Change-Id: I7515407b867487574a29dcae3456d96920a24979
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3140202
Commit-Queue: Jeremy Bettis <jbettis@chromium.org>
Reviewed-by: Jeremy Bettis <jbettis@chromium.org>
Diffstat (limited to 'zephyr/test')
-rw-r--r-- | zephyr/test/drivers/src/bb_retimer.c | 6 | ||||
-rw-r--r-- | zephyr/test/drivers/src/bma2x2.c | 83 | ||||
-rw-r--r-- | zephyr/test/drivers/src/bmi160.c | 156 | ||||
-rw-r--r-- | zephyr/test/drivers/src/bmi260.c | 167 | ||||
-rw-r--r-- | zephyr/test/drivers/src/smart.c | 58 | ||||
-rw-r--r-- | zephyr/test/drivers/src/tcs3400.c | 25 |
6 files changed, 241 insertions, 254 deletions
diff --git a/zephyr/test/drivers/src/bb_retimer.c b/zephyr/test/drivers/src/bb_retimer.c index ac53df4779..c7e5fb7789 100644 --- a/zephyr/test/drivers/src/bb_retimer.c +++ b/zephyr/test/drivers/src/bb_retimer.c @@ -9,6 +9,7 @@ #include "common.h" #include "ec_tasks.h" #include "emul/emul_bb_retimer.h" +#include "emul/emul_common_i2c.h" #include "hooks.h" #include "i2c.h" #include "stubs.h" @@ -41,7 +42,8 @@ static void test_bb_set_state(void) set_test_runner_tid(); /* Setup emulator fail on write */ - bb_emul_set_write_fail_reg(emul, BB_RETIMER_REG_CONNECTION_STATE); + i2c_common_emul_set_write_fail_reg(emul, + BB_RETIMER_REG_CONNECTION_STATE); /* Test fail on reset register write */ zassert_equal(-EIO, bb_usb_retimer.set(&usb_muxes[USBC_PORT_C1], @@ -50,7 +52,7 @@ static void test_bb_set_state(void) zassert_false(ack_required, "ACK is never required for BB retimer"); /* Do not fail on write */ - bb_emul_set_write_fail_reg(emul, BB_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set UFP role for whole test */ tc_set_data_role(USBC_PORT_C1, PD_ROLE_UFP); diff --git a/zephyr/test/drivers/src/bma2x2.c b/zephyr/test/drivers/src/bma2x2.c index 457aeeef5d..f655e0c99a 100644 --- a/zephyr/test/drivers/src/bma2x2.c +++ b/zephyr/test/drivers/src/bma2x2.c @@ -9,6 +9,7 @@ #include "common.h" #include "i2c.h" #include "emul/emul_bma255.h" +#include "emul/emul_common_i2c.h" #include "accelgyro.h" #include "motion_sense.h" @@ -120,10 +121,12 @@ 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, void *data) +static int emul_read_reset(struct i2c_emul *emul, int reg, uint8_t *buf, + int bytes, void *data) { struct reset_func_data *d = data; + reg = bma_emul_access_reg(emul, reg, bytes, true /* = read */); if (reg != BMA2x2_RST_ADDR) { return 1; } @@ -161,15 +164,15 @@ static void test_bma_get_offset(void) emul = bma_emul_get(BMA_ORD); /* Test fail on each axis */ - bma_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR); zassert_equal(-EIO, ms.drv->get_offset(&ms, ret_offset, &temp), NULL); - bma_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR); zassert_equal(-EIO, ms.drv->get_offset(&ms, ret_offset, &temp), NULL); - bma_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR); zassert_equal(-EIO, ms.drv->get_offset(&ms, ret_offset, &temp), NULL); /* Do not fail on read */ - bma_emul_set_read_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set emulator offset */ exp_offset[0] = BMA_EMUL_1G / 10; @@ -209,15 +212,15 @@ static void test_bma_set_offset(void) emul = bma_emul_get(BMA_ORD); /* Test fail on each axis */ - bma_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR); + i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_X_AXIS_ADDR); zassert_equal(-EIO, ms.drv->set_offset(&ms, exp_offset, temp), NULL); - bma_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR); + i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Y_AXIS_ADDR); zassert_equal(-EIO, ms.drv->set_offset(&ms, exp_offset, temp), NULL); - bma_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR); + i2c_common_emul_set_write_fail_reg(emul, BMA2x2_OFFSET_Z_AXIS_ADDR); zassert_equal(-EIO, ms.drv->set_offset(&ms, exp_offset, temp), NULL); /* Do not fail on write */ - bma_emul_set_write_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_offset[0] = BMA_EMUL_1G / 10; @@ -305,7 +308,7 @@ static void test_bma_set_range(void) ms.current_range = start_range; bma_emul_set_reg(emul, BMA2x2_RANGE_SELECT_ADDR, BMA2x2_RANGE_2G); /* Setup emulator fail on read */ - bma_emul_set_read_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); /* Test fail on read */ zassert_equal(-EIO, ms.drv->set_range(&ms, 12, 0), NULL); @@ -318,10 +321,10 @@ static void test_bma_set_range(void) bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL); /* Do not fail on read */ - bma_emul_set_read_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - bma_emul_set_write_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); + i2c_common_emul_set_write_fail_reg(emul, BMA2x2_RANGE_SELECT_ADDR); /* Test fail on write */ zassert_equal(-EIO, ms.drv->set_range(&ms, 12, 0), NULL); @@ -334,7 +337,7 @@ static void test_bma_set_range(void) bma_emul_get_reg(emul, BMA2x2_RANGE_SELECT_ADDR), NULL); /* Do not fail on write */ - bma_emul_set_write_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_range(emul, 1, 0, 2); @@ -374,42 +377,42 @@ static void test_bma_init(void) emul = bma_emul_get(BMA_ORD); /* Setup emulator fail read function */ - bma_emul_set_read_fail_reg(emul, BMA2x2_CHIP_ID_ADDR); + i2c_common_emul_set_read_fail_reg(emul, 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 */ - bma_emul_set_read_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, 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 */ - bma_emul_set_read_fail_reg(emul, BMA2x2_RST_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_RST_ADDR); bma_emul_set_reg(emul, BMA2x2_CHIP_ID_ADDR, BMA255_CHIP_ID_MAJOR); /* Test fail on reset register read */ zassert_equal(-EIO, ms.drv->init(&ms), NULL); /* Do not fail on read */ - bma_emul_set_read_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - bma_emul_set_write_fail_reg(emul, BMA2x2_RST_ADDR); + i2c_common_emul_set_write_fail_reg(emul, BMA2x2_RST_ADDR); /* Test fail on reset register write */ zassert_equal(-EIO, ms.drv->init(&ms), NULL); /* Do not fail on write */ - bma_emul_set_write_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, 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; - bma_emul_set_read_func(emul, emul_read_reset, &reset_func_data); + i2c_common_emul_set_read_func(emul, emul_read_reset, &reset_func_data); /* Test fail on too many reset read errors */ zassert_equal(EC_ERROR_TIMEOUT, ms.drv->init(&ms), NULL); @@ -434,7 +437,7 @@ static void test_bma_init(void) zassert_equal(EC_RES_SUCCESS, ms.drv->init(&ms), NULL); /* Remove custom emulator read function */ - bma_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(emul, NULL, NULL); } /* @@ -568,7 +571,7 @@ static void test_bma_rate(void) reg_rate = bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR); /* Setup emulator fail on read */ - bma_emul_set_read_fail_reg(emul, BMA2x2_BW_SELECT_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_BW_SELECT_ADDR); /* Test fail on read */ zassert_equal(-EIO, ms.drv->set_data_rate(&ms, 15625, 0), NULL); @@ -581,10 +584,10 @@ static void test_bma_rate(void) bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR), NULL); /* Do not fail on read */ - bma_emul_set_read_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - bma_emul_set_write_fail_reg(emul, BMA2x2_BW_SELECT_ADDR); + i2c_common_emul_set_write_fail_reg(emul, BMA2x2_BW_SELECT_ADDR); /* Test fail on write */ zassert_equal(-EIO, ms.drv->set_data_rate(&ms, 15625, 0), NULL); @@ -597,7 +600,7 @@ static void test_bma_rate(void) bma_emul_get_reg(emul, BMA2x2_BW_SELECT_ADDR), NULL); /* Do not fail on write */ - bma_emul_set_write_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); } /** Test read with and without I2C errors. */ @@ -616,21 +619,21 @@ static void test_bma_read(void) bma_emul_set_off(emul, BMA_EMUL_AXIS_Z, 0); /* Test fail on each axis */ - bma_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_LSB_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_LSB_ADDR); zassert_equal(-EIO, ms.drv->read(&ms, ret_acc_v), NULL); - bma_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_MSB_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_X_AXIS_MSB_ADDR); zassert_equal(-EIO, ms.drv->read(&ms, ret_acc_v), NULL); - bma_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_LSB_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_LSB_ADDR); zassert_equal(-EIO, ms.drv->read(&ms, ret_acc_v), NULL); - bma_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_MSB_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Y_AXIS_MSB_ADDR); zassert_equal(-EIO, ms.drv->read(&ms, ret_acc_v), NULL); - bma_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_LSB_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_LSB_ADDR); zassert_equal(-EIO, ms.drv->read(&ms, ret_acc_v), NULL); - bma_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_MSB_ADDR); + i2c_common_emul_set_read_fail_reg(emul, BMA2x2_Z_AXIS_MSB_ADDR); zassert_equal(-EIO, ms.drv->read(&ms, ret_acc_v), NULL); /* Do not fail on read */ - bma_emul_set_read_fail_reg(emul, BMA_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input accelerometer values */ exp_acc[0] = BMA_EMUL_1G / 10; @@ -692,12 +695,14 @@ 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, void *data) +static int emul_read_calib_func(struct i2c_emul *emul, int reg, uint8_t *val, + int bytes, void *data) { struct calib_func_data *d = data; uint8_t reg_val; int cur_time; + reg = bma_emul_access_reg(emul, reg, bytes, true /* = read */); if (reg != BMA2x2_OFFSET_CTRL_ADDR) { return 1; } @@ -723,10 +728,11 @@ static int emul_read_calib_func(struct i2c_emul *emul, int reg, void *data) * triggerd. */ static int emul_write_calib_func(struct i2c_emul *emul, int reg, uint8_t val, - void *data) + int bytes, void *data) { struct calib_func_data *d = data; + reg = bma_emul_access_reg(emul, reg, bytes, false /* = read */); if (reg != BMA2x2_OFFSET_CTRL_ADDR) { return 1; } @@ -783,8 +789,8 @@ static void test_bma_perform_calib(void) exp_off[2] = BMA_EMUL_1G - exp_off[2]; /* Setup emulator calibration functions */ - bma_emul_set_read_func(emul, emul_read_calib_func, &func_data); - bma_emul_set_write_func(emul, emul_write_calib_func, &func_data); + 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); /* Setup emulator to fail on first access to offset control register */ func_data.calib_start = k_uptime_get_32(); @@ -833,7 +839,6 @@ static void test_bma_perform_calib(void) func_data.read_fail = 0; func_data.time = 1000; - /* Test fail on too long offset compensation */ 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); @@ -883,8 +888,8 @@ static void test_bma_perform_calib(void) compare_int3v(exp_off, ret_off); /* Remove custom emulator functions */ - bma_emul_set_read_func(emul, NULL, NULL); - bma_emul_set_write_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_write_func(emul, NULL, NULL); } /** Test get resolution. */ diff --git a/zephyr/test/drivers/src/bmi160.c b/zephyr/test/drivers/src/bmi160.c index d0cfed200d..030325674b 100644 --- a/zephyr/test/drivers/src/bmi160.c +++ b/zephyr/test/drivers/src/bmi160.c @@ -9,6 +9,7 @@ #include "common.h" #include "i2c.h" #include "emul/emul_bmi.h" +#include "emul/emul_common_i2c.h" #include "motion_sense_fifo.h" #include "driver/accelgyro_bmi160.h" @@ -154,13 +155,13 @@ static void test_bmi_acc_get_offset(void) exp_v[2] = -1000 / 30; /* Test fail on offset read */ - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; @@ -198,7 +199,7 @@ static void test_bmi_gyr_get_offset(void) ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Do not fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set emulator offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -211,15 +212,15 @@ static void test_bmi_gyr_get_offset(void) exp_v[2] = -125000 / 300; /* Test fail on offset read */ - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; @@ -260,21 +261,21 @@ static void test_bmi_acc_set_offset(void) ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Test fail on OFFSET EN GYR98 register read and write */ - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + 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); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_v[0] = BMI_EMUL_1G / 10; @@ -331,21 +332,21 @@ static void test_bmi_gyr_set_offset(void) ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Test fail on OFFSET EN GYR98 register read and write */ - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + 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); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70); + i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); + i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); + i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -441,7 +442,7 @@ static void test_bmi_acc_set_range(void) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI160_ACC_RANGE, BMI160_GSEL_2G); /* Setup emulator fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI160_ACC_RANGE); + i2c_common_emul_set_write_fail_reg(emul, BMI160_ACC_RANGE); /* Test fail on write */ zassert_equal(-EIO, ms->drv->set_range(ms, 12, 0), NULL); @@ -454,7 +455,7 @@ static void test_bmi_acc_set_range(void) bmi_emul_get_reg(emul, BMI160_ACC_RANGE), NULL); /* Do not fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_acc_range(emul, ms, 1, 0, 2); @@ -549,7 +550,7 @@ static void test_bmi_gyr_set_range(void) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI160_GYR_RANGE, BMI160_DPS_SEL_250); /* Setup emulator fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI160_GYR_RANGE); + i2c_common_emul_set_write_fail_reg(emul, BMI160_GYR_RANGE); /* Test fail on write */ zassert_equal(-EIO, ms->drv->set_range(ms, 125, 0), NULL); @@ -562,7 +563,7 @@ static void test_bmi_gyr_set_range(void) bmi_emul_get_reg(emul, BMI160_GYR_RANGE), NULL); /* Do not fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_gyr_range(emul, ms, 1, 0, 125); @@ -756,7 +757,7 @@ static void test_bmi_acc_rate(void) reg_rate = bmi_emul_get_reg(emul, BMI160_ACC_CONF); /* Setup emulator fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI160_ACC_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_CONF); /* Test fail on read */ zassert_equal(-EIO, ms->drv->set_data_rate(ms, 50000, 0), NULL); @@ -767,10 +768,10 @@ static void test_bmi_acc_rate(void) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL); /* Do not fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI160_ACC_CONF); + i2c_common_emul_set_write_fail_reg(emul, BMI160_ACC_CONF); /* Test fail on write */ zassert_equal(-EIO, ms->drv->set_data_rate(ms, 50000, 0), NULL); @@ -781,7 +782,7 @@ static void test_bmi_acc_rate(void) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_ACC_CONF), NULL); /* Do not fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET; @@ -935,7 +936,7 @@ static void test_bmi_gyr_rate(void) reg_rate = bmi_emul_get_reg(emul, BMI160_GYR_CONF); /* Setup emulator fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI160_GYR_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_CONF); /* Test fail on read */ zassert_equal(-EIO, ms->drv->set_data_rate(ms, 50000, 0), NULL); @@ -946,10 +947,10 @@ static void test_bmi_gyr_rate(void) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL); /* Do not fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI160_GYR_CONF); + i2c_common_emul_set_write_fail_reg(emul, BMI160_GYR_CONF); /* Test fail on write */ zassert_equal(-EIO, ms->drv->set_data_rate(ms, 50000, 0), NULL); @@ -960,7 +961,7 @@ static void test_bmi_gyr_rate(void) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI160_GYR_CONF), NULL); /* Do not fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ pmu_status = BMI160_PMU_NORMAL << BMI160_PMU_ACC_OFFSET; @@ -1030,18 +1031,18 @@ static void test_bmi_read_temp(void) ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Setup emulator fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI160_TEMPERATURE_0); + i2c_common_emul_set_read_fail_reg(emul, 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); - bmi_emul_set_read_fail_reg(emul, BMI160_TEMPERATURE_1); + i2c_common_emul_set_read_fail_reg(emul, 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 */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Fail on invalid temperature */ bmi_emul_set_reg(emul, BMI160_TEMPERATURE_0, 0x00); @@ -1116,10 +1117,10 @@ static void test_bmi_acc_read(void) bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, 0); /* Fail on read status */ - bmi_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1187,20 +1188,20 @@ static void test_bmi_acc_read(void) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - bmi_emul_set_read_fail_reg(emul, BMI160_ACC_X_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_X_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_ACC_X_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_X_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_ACC_Y_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Y_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_ACC_Y_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Y_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_ACC_Z_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Z_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_ACC_Z_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_Z_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1224,10 +1225,10 @@ static void test_bmi_gyr_read(void) bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, 0); /* Fail on read status */ - bmi_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1295,20 +1296,20 @@ static void test_bmi_gyr_read(void) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - bmi_emul_set_read_fail_reg(emul, BMI160_GYR_X_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_X_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_GYR_X_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_X_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_GYR_Y_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Y_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_GYR_Y_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Y_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_GYR_Z_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Z_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_GYR_Z_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_Z_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1316,10 +1317,12 @@ static void test_bmi_gyr_read(void) * 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, int byte, void *data) +static int emul_nrdy(struct i2c_emul *emul, int reg, uint8_t *val, int byte, + void *data) { if (reg == BMI160_STATUS) { bmi_emul_set_reg(emul, BMI160_STATUS, 0); + *val = 0; return 0; } @@ -1379,13 +1382,13 @@ static void test_bmi_acc_perform_calib(void) exp_off[2] = BMI_EMUL_1G - exp_off[2]; /* Test fail on rate set */ - bmi_emul_set_read_fail_reg(emul, BMI160_ACC_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI160_ACC_CONF); zassert_equal(-EIO, 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 */ - bmi_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); zassert_equal(-EIO, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); @@ -1393,13 +1396,13 @@ static void test_bmi_acc_perform_calib(void) bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); /* Test fail on data not ready */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); - bmi_emul_set_read_func(emul, emul_nrdy, NULL); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_func(emul, 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 */ - bmi_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(emul, NULL, NULL); /* Stop fast offset compensation before next test */ bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); @@ -1503,13 +1506,13 @@ static void test_bmi_gyr_perform_calib(void) zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on rate set */ - bmi_emul_set_read_fail_reg(emul, BMI160_GYR_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI160_GYR_CONF); zassert_equal(-EIO, 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 */ - bmi_emul_set_read_fail_reg(emul, BMI160_STATUS); + i2c_common_emul_set_read_fail_reg(emul, BMI160_STATUS); zassert_equal(-EIO, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); @@ -1517,13 +1520,13 @@ static void test_bmi_gyr_perform_calib(void) bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); /* Test fail on data not ready */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); - bmi_emul_set_read_func(emul, emul_nrdy, NULL); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_func(emul, 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 */ - bmi_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(emul, NULL, NULL); /* Stop fast offset compensation before next test */ bmi_emul_set_reg(emul, BMI160_CMD_REG, BMI160_CMD_NOOP); @@ -1568,7 +1571,8 @@ 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, int byte, void *data) +static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val, + int byte, void *data) { struct fifo_func_data *d = data; @@ -1703,11 +1707,11 @@ static void test_bmi_acc_fifo(void) event = BMI_INT_EVENT; /* Test fail to read interrupt status registers */ - bmi_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_0); + i2c_common_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_0); zassert_equal(-EIO, ms->drv->irq_handler(ms, &event), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_1); + i2c_common_emul_set_read_fail_reg(emul, BMI160_INT_STATUS_1); zassert_equal(-EIO, ms->drv->irq_handler(ms, &event), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test no interrupt */ bmi_emul_set_reg(emul, BMI160_INT_STATUS_0, 0); @@ -1717,7 +1721,7 @@ static void test_bmi_acc_fifo(void) check_fifo(ms, ms_gyr, NULL, acc_range, gyr_range); /* Set custom function for FIFO test */ - bmi_emul_set_read_func(emul, emul_fifo_func, &func_data); + i2c_common_emul_set_read_func(emul, emul_fifo_func, &func_data); /* Enable sensor FIFO */ zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 50000, 0), NULL); /* Set range */ @@ -1802,7 +1806,7 @@ static void test_bmi_acc_fifo(void) check_fifo(ms, ms_gyr, f, acc_range, gyr_range); /* Remove custom emulator read function */ - bmi_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(emul, NULL, NULL); } /** Test irq handler of gyroscope sensor */ diff --git a/zephyr/test/drivers/src/bmi260.c b/zephyr/test/drivers/src/bmi260.c index 63b5da0784..b4bd9b540a 100644 --- a/zephyr/test/drivers/src/bmi260.c +++ b/zephyr/test/drivers/src/bmi260.c @@ -9,6 +9,7 @@ #include "common.h" #include "i2c.h" #include "emul/emul_bmi.h" +#include "emul/emul_common_i2c.h" #include "motion_sense_fifo.h" #include "driver/accelgyro_bmi260.h" @@ -154,13 +155,13 @@ static void test_bmi_acc_get_offset(void) exp_v[2] = -1000 / 30; /* Test fail on offset read */ - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; @@ -198,7 +199,7 @@ static void test_bmi_gyr_get_offset(void) ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Do not fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set emulator offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -211,15 +212,15 @@ static void test_bmi_gyr_get_offset(void) exp_v[2] = -125000 / 300; /* Test fail on offset read */ - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 1); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_GYR70 + 2); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(emul, BMI160_OFFSET_EN_GYR98); zassert_equal(-EIO, ms->drv->get_offset(ms, ret, &temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Disable rotation */ ms->rot_standard_ref = NULL; @@ -261,21 +262,21 @@ static void test_bmi_acc_set_offset(void) ms = &motion_sensors[BMI_ACC_SENSOR_ID]; /* Test fail on NV CONF register read and write */ - bmi_emul_set_read_fail_reg(emul, BMI260_NV_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI260_NV_CONF); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); - bmi_emul_set_write_fail_reg(emul, BMI260_NV_CONF); + 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); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70); + i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); + i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 1); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); + i2c_common_emul_set_write_fail_reg(emul, BMI160_OFFSET_ACC70 + 2); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup NV_CONF register value */ bmi_emul_set_reg(emul, BMI260_NV_CONF, 0x7); @@ -339,21 +340,21 @@ static void test_bmi_gyr_set_offset(void) ms = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Test fail on OFFSET EN GYR98 register read and write */ - bmi_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); - bmi_emul_set_write_fail_reg(emul, BMI260_OFFSET_EN_GYR98); + 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); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test fail on offset write */ - bmi_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70); + i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 1); + i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 1); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 2); + i2c_common_emul_set_write_fail_reg(emul, BMI260_OFFSET_GYR70 + 2); zassert_equal(-EIO, ms->drv->set_offset(ms, input_v, temp), NULL); - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Set input offset */ exp_v[0] = BMI_EMUL_125_DEG_S / 100; @@ -453,7 +454,7 @@ static void test_bmi_acc_set_range(void) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI260_ACC_RANGE, BMI260_GSEL_2G); /* Setup emulator fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI260_ACC_RANGE); + i2c_common_emul_set_write_fail_reg(emul, BMI260_ACC_RANGE); /* Test fail on write */ zassert_equal(-EIO, ms->drv->set_range(ms, 12, 0), NULL); @@ -466,7 +467,7 @@ static void test_bmi_acc_set_range(void) bmi_emul_get_reg(emul, BMI260_ACC_RANGE), NULL); /* Do not fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_acc_range(emul, ms, 1, 0, 2); @@ -561,7 +562,7 @@ static void test_bmi_gyr_set_range(void) ms->current_range = start_range; bmi_emul_set_reg(emul, BMI260_GYR_RANGE, BMI260_DPS_SEL_250); /* Setup emulator fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI260_GYR_RANGE); + i2c_common_emul_set_write_fail_reg(emul, BMI260_GYR_RANGE); /* Test fail on write */ zassert_equal(-EIO, ms->drv->set_range(ms, 125, 0), NULL); @@ -574,7 +575,7 @@ static void test_bmi_gyr_set_range(void) bmi_emul_get_reg(emul, BMI260_GYR_RANGE), NULL); /* Do not fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test setting range with rounding down */ check_set_gyr_range(emul, ms, 1, 0, 125); @@ -768,7 +769,7 @@ static void test_bmi_acc_rate(void) reg_rate = bmi_emul_get_reg(emul, BMI260_ACC_CONF); /* Setup emulator fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI260_ACC_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_CONF); /* Test fail on read */ zassert_equal(-EIO, ms->drv->set_data_rate(ms, 50000, 0), NULL); @@ -779,10 +780,10 @@ static void test_bmi_acc_rate(void) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL); /* Do not fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI260_ACC_CONF); + i2c_common_emul_set_write_fail_reg(emul, BMI260_ACC_CONF); /* Test fail on write */ zassert_equal(-EIO, ms->drv->set_data_rate(ms, 50000, 0), NULL); @@ -793,7 +794,7 @@ static void test_bmi_acc_rate(void) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_ACC_CONF), NULL); /* Do not fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ bmi_emul_set_reg(emul, BMI260_PWR_CTRL, @@ -948,7 +949,7 @@ static void test_bmi_gyr_rate(void) reg_rate = bmi_emul_get_reg(emul, BMI260_GYR_CONF); /* Setup emulator fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI260_GYR_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_CONF); /* Test fail on read */ zassert_equal(-EIO, ms->drv->set_data_rate(ms, 50000, 0), NULL); @@ -959,10 +960,10 @@ static void test_bmi_gyr_rate(void) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL); /* Do not fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Setup emulator fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI260_GYR_CONF); + i2c_common_emul_set_write_fail_reg(emul, BMI260_GYR_CONF); /* Test fail on write */ zassert_equal(-EIO, ms->drv->set_data_rate(ms, 50000, 0), NULL); @@ -973,7 +974,7 @@ static void test_bmi_gyr_rate(void) zassert_equal(reg_rate, bmi_emul_get_reg(emul, BMI260_GYR_CONF), NULL); /* Do not fail on write */ - bmi_emul_set_write_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test disabling sensor */ bmi_emul_set_reg(emul, BMI260_PWR_CTRL, @@ -1047,18 +1048,18 @@ static void test_bmi_read_temp(void) ms_gyr = &motion_sensors[BMI_GYR_SENSOR_ID]; /* Setup emulator fail on read */ - bmi_emul_set_read_fail_reg(emul, BMI260_TEMPERATURE_0); + i2c_common_emul_set_read_fail_reg(emul, 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); - bmi_emul_set_read_fail_reg(emul, BMI260_TEMPERATURE_1); + i2c_common_emul_set_read_fail_reg(emul, 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 */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Fail on invalid temperature */ bmi_emul_set_reg(emul, BMI260_TEMPERATURE_0, 0x00); @@ -1133,10 +1134,10 @@ static void test_bmi_acc_read(void) bmi_emul_set_off(emul, BMI_EMUL_ACC_Z, 0); /* Fail on read status */ - bmi_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1204,20 +1205,20 @@ static void test_bmi_acc_read(void) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - bmi_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_ACC_X_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_ACC_Y_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Y_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_ACC_Y_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Y_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_ACC_Z_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Z_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_ACC_Z_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_Z_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1241,10 +1242,10 @@ static void test_bmi_gyr_read(void) bmi_emul_set_off(emul, BMI_EMUL_GYR_Z, 0); /* Fail on read status */ - bmi_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* When not ready, driver should return saved raw value */ exp_v[0] = 100; @@ -1312,20 +1313,20 @@ static void test_bmi_gyr_read(void) compare_int3v(exp_v, ret_v); /* Fail on read of data registers */ - bmi_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_GYR_X_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_GYR_Y_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Y_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_GYR_Y_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Y_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_GYR_Z_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Z_L_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_GYR_Z_H_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_Z_H_G); zassert_equal(-EIO, ms->drv->read(ms, ret_v), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); ms->rot_standard_ref = NULL; } @@ -1372,20 +1373,20 @@ static void test_bmi_acc_perform_calib(void) zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on rate read */ - bmi_emul_set_read_fail_reg(emul, BMI260_ACC_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_CONF); zassert_equal(-EIO, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - bmi_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); zassert_equal(-EIO, 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 */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, 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); @@ -1395,18 +1396,18 @@ static void test_bmi_acc_perform_calib(void) bmi_emul_set_reg(emul, BMI260_STATUS, BMI260_DRDY_ACC); /* Test fail on data read */ - bmi_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_ACC_X_L_G); zassert_equal(-EIO, 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 */ - bmi_emul_set_read_fail_reg(emul, BMI260_NV_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI260_NV_CONF); zassert_equal(-EIO, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful offset compenastion */ zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL); @@ -1463,20 +1464,20 @@ static void test_bmi_gyr_perform_calib(void) zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on rate read */ - bmi_emul_set_read_fail_reg(emul, BMI260_GYR_CONF); + i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_CONF); zassert_equal(-EIO, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); /* Test fail on status read */ - bmi_emul_set_read_fail_reg(emul, BMI260_STATUS); + i2c_common_emul_set_read_fail_reg(emul, BMI260_STATUS); zassert_equal(-EIO, 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 */ - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, 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); @@ -1490,18 +1491,18 @@ static void test_bmi_gyr_perform_calib(void) BMI260_DRDY_ACC | BMI260_DRDY_GYR); /* Test fail on data read */ - bmi_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G); + i2c_common_emul_set_read_fail_reg(emul, BMI260_GYR_X_L_G); zassert_equal(-EIO, 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 */ - bmi_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98); + i2c_common_emul_set_read_fail_reg(emul, BMI260_OFFSET_EN_GYR98); zassert_equal(-EIO, ms->drv->perform_calib(ms, 1), NULL); zassert_equal(range, ms->current_range, NULL); zassert_equal(rate, ms->drv->get_data_rate(ms), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test successful offset compenastion */ zassert_equal(EC_SUCCESS, ms->drv->perform_calib(ms, 1), NULL); @@ -1519,7 +1520,8 @@ static void test_bmi_gyr_perform_calib(void) * Custom emulatro 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, int byte, void *data) +static int emul_init_ok(struct i2c_emul *emul, int reg, uint8_t *val, int byte, + void *data) { bmi_emul_set_reg(emul, BMI260_INTERNAL_STATUS, BMI260_INIT_OK); @@ -1541,13 +1543,13 @@ static void test_bmi_init(void) * BMI260_INTERNAL_STATUS register, because init function triggers reset * which clears value set in this register before test. */ - bmi_emul_set_read_func(emul, emul_init_ok, NULL); + i2c_common_emul_set_read_func(emul, emul_init_ok, NULL); zassert_equal(EC_RES_SUCCESS, ms_acc->drv->init(ms_acc), NULL); zassert_equal(EC_RES_SUCCESS, ms_gyr->drv->init(ms_gyr), NULL); /* Remove custom emulator read function */ - bmi_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(emul, NULL, NULL); } /** Data for custom emulator read function used in FIFO test */ @@ -1560,7 +1562,8 @@ 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, int byte, void *data) +static int emul_fifo_func(struct i2c_emul *emul, int reg, uint8_t *val, + int byte, void *data) { struct fifo_func_data *d = data; @@ -1695,11 +1698,11 @@ static void test_bmi_acc_fifo(void) event = BMI_INT_EVENT; /* Test fail to read interrupt status registers */ - bmi_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_0); + i2c_common_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_0); zassert_equal(-EIO, ms->drv->irq_handler(ms, &event), NULL); - bmi_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_1); + i2c_common_emul_set_read_fail_reg(emul, BMI260_INT_STATUS_1); zassert_equal(-EIO, ms->drv->irq_handler(ms, &event), NULL); - bmi_emul_set_read_fail_reg(emul, BMI_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test no interrupt */ bmi_emul_set_reg(emul, BMI260_INT_STATUS_0, 0); @@ -1709,7 +1712,7 @@ static void test_bmi_acc_fifo(void) check_fifo(ms, ms_gyr, NULL, acc_range, gyr_range); /* Set custom function for FIFO test */ - bmi_emul_set_read_func(emul, emul_fifo_func, &func_data); + i2c_common_emul_set_read_func(emul, emul_fifo_func, &func_data); /* Enable sensor FIFO */ zassert_equal(EC_SUCCESS, ms->drv->set_data_rate(ms, 50000, 0), NULL); /* Set range */ @@ -1794,7 +1797,7 @@ static void test_bmi_acc_fifo(void) check_fifo(ms, ms_gyr, f, acc_range, gyr_range); /* Remove custom emulator read function */ - bmi_emul_set_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_func(emul, NULL, NULL); } /** Test irq handler of gyroscope sensor */ diff --git a/zephyr/test/drivers/src/smart.c b/zephyr/test/drivers/src/smart.c index 5f5c4f52fc..7c053f1c23 100644 --- a/zephyr/test/drivers/src/smart.c +++ b/zephyr/test/drivers/src/smart.c @@ -8,6 +8,7 @@ #include "common.h" #include "i2c.h" +#include "emul/emul_common_i2c.h" #include "emul/emul_smart_battery.h" #include "battery.h" @@ -102,13 +103,6 @@ static void test_battery_status(void) zassert_equal(expected, status, "%d != %d", expected, status); } -/** Custom battery function which always fail */ -int fail_func(struct i2c_emul *emul, uint8_t *buf, int *len, int cmd, - void *data) -{ - return -EINVAL; -} - /** Test wait for stable function */ static void test_battery_wait_for_stable(void) { @@ -117,11 +111,11 @@ static void test_battery_wait_for_stable(void) emul = sbat_emul_get_ptr(BATTERY_ORD); /* Should fail when read function always fail */ - sbat_emul_set_custom_read_func(emul, fail_func, NULL); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(EC_ERROR_NOT_POWERED, battery_wait_for_stable(), NULL); /* Should be ok with default handler */ - sbat_emul_set_custom_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); zassert_equal(EC_SUCCESS, battery_wait_for_stable(), NULL); } @@ -179,29 +173,10 @@ static void test_battery_time_at_rate(void) zassert_equal(expect_time, minutes, "%d != %d", expect_time, minutes); } -/** Parameter for fail_cmd_func */ -struct fail_cmd_data { - int cmd; -}; - -/** Custom battery function which fail on specific command */ -int fail_cmd_func(struct i2c_emul *emul, uint8_t *buf, int *len, int cmd, - void *data) -{ - struct fail_cmd_data *p = data; - - if (p->cmd == cmd) - return -EINVAL; - - /* Use default handler */ - return 1; -} - /** Test battery get params */ static void test_battery_get_params(void) { struct sbat_emul_bat_data *bat; - struct fail_cmd_data func_data; struct batt_params batt; struct i2c_emul *emul; int flags; @@ -213,84 +188,81 @@ static void test_battery_get_params(void) bat->desired_charg_cur = 1000; bat->desired_charg_volt = 5000; - /* Use function which allows to fail for specific command */ - sbat_emul_set_custom_read_func(emul, fail_cmd_func, &func_data); - /* Fail temperature read */ - func_data.cmd = SB_TEMPERATURE; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - func_data.cmd = SB_RELATIVE_STATE_OF_CHARGE; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - func_data.cmd = SB_VOLTAGE; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - func_data.cmd = SB_CURRENT; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - func_data.cmd = SB_AVERAGE_CURRENT; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - func_data.cmd = SB_CHARGING_VOLTAGE; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - func_data.cmd = SB_CHARGING_CURRENT; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - func_data.cmd = SB_REMAINING_CAPACITY; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - func_data.cmd = SB_FULL_CHARGE_CAPACITY; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - func_data.cmd = SB_BATTERY_STATUS; + i2c_common_emul_set_read_fail_reg(emul, 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 */ - sbat_emul_set_custom_read_func(emul, fail_func, NULL); + i2c_common_emul_set_read_fail_reg(emul, 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 */ - sbat_emul_set_custom_read_func(emul, NULL, NULL); + i2c_common_emul_set_read_fail_reg(emul, 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); diff --git a/zephyr/test/drivers/src/tcs3400.c b/zephyr/test/drivers/src/tcs3400.c index 8aaa3c2f38..c9f650f497 100644 --- a/zephyr/test/drivers/src/tcs3400.c +++ b/zephyr/test/drivers/src/tcs3400.c @@ -9,6 +9,7 @@ #include "common.h" #include "i2c.h" #include "emul/emul_tcs3400.h" +#include "emul/emul_common_i2c.h" #include "motion_sense.h" #include "motion_sense_fifo.h" @@ -37,9 +38,9 @@ static void test_tcs_init(void) zassert_equal(EC_SUCCESS, ms_rgb->drv->init(ms_rgb), NULL); /* Fail init on communication errors */ - tcs_emul_set_read_fail_reg(emul, TCS_EMUL_FAIL_ALL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_FAIL_ALL_REG); zassert_equal(-EIO, ms->drv->init(ms), NULL); - tcs_emul_set_read_fail_reg(emul, TCS_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Fail on bad ID */ tcs_emul_set_reg(emul, TCS_I2C_ID, 0); @@ -68,13 +69,13 @@ static void test_tcs_read(void) ms = &motion_sensors[TCS_CLR_SENSOR_ID]; /* Test error on writing registers */ - tcs_emul_set_write_fail_reg(emul, TCS_I2C_ATIME); + i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ATIME); zassert_equal(-EIO, ms->drv->read(ms, v), NULL); - tcs_emul_set_write_fail_reg(emul, TCS_I2C_CONTROL); + i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_CONTROL); zassert_equal(-EIO, ms->drv->read(ms, v), NULL); - tcs_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE); + i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE); zassert_equal(-EIO, ms->drv->read(ms, v), NULL); - tcs_emul_set_write_fail_reg(emul, TCS_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); /* Test starting read with calibration */ tcs_emul_set_reg(emul, TCS_I2C_ATIME, 0); @@ -152,15 +153,15 @@ static void test_tcs_irq_handler_fail(void) event = TCS_INT_EVENT; /* Test error on reading status */ - tcs_emul_set_read_fail_reg(emul, TCS_I2C_STATUS); + i2c_common_emul_set_read_fail_reg(emul, TCS_I2C_STATUS); zassert_equal(-EIO, ms->drv->irq_handler(ms, &event), NULL); - tcs_emul_set_read_fail_reg(emul, TCS_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); check_fifo_empty(ms, ms_rgb); /* Test fail on changing device power state */ - tcs_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE); + i2c_common_emul_set_write_fail_reg(emul, TCS_I2C_ENABLE); zassert_equal(-EIO, ms->drv->irq_handler(ms, &event), NULL); - tcs_emul_set_write_fail_reg(emul, TCS_EMUL_NO_FAIL_REG); + i2c_common_emul_set_write_fail_reg(emul, I2C_COMMON_EMUL_NO_FAIL_REG); check_fifo_empty(ms, ms_rgb); /* Test that no data is committed when status is 0 */ @@ -536,12 +537,12 @@ static void test_tcs_data_rate(void) ms_rgb = &motion_sensors[TCS_RGB_SENSOR_ID]; /* Test fail on reading device power state */ - tcs_emul_set_read_fail_reg(emul, TCS_I2C_ENABLE); + i2c_common_emul_set_read_fail_reg(emul, TCS_I2C_ENABLE); zassert_equal(-EIO, ms->drv->set_data_rate(ms, 0, 0), NULL); zassert_equal(-EIO, ms->drv->set_data_rate(ms, 0, 1), NULL); zassert_equal(-EIO, ms->drv->set_data_rate(ms, 100, 0), NULL); zassert_equal(-EIO, ms->drv->set_data_rate(ms, 100, 1), NULL); - tcs_emul_set_read_fail_reg(emul, TCS_EMUL_NO_FAIL_REG); + i2c_common_emul_set_read_fail_reg(emul, 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); |