diff options
-rw-r--r-- | include/system.h | 8 | ||||
-rw-r--r-- | zephyr/emul/emul_isl923x.c | 9 | ||||
-rw-r--r-- | zephyr/include/emul/emul_isl923x.h | 7 | ||||
-rw-r--r-- | zephyr/shim/src/ztest_system.c | 13 | ||||
-rw-r--r-- | zephyr/test/drivers/src/isl923x.c | 110 |
5 files changed, 146 insertions, 1 deletions
diff --git a/include/system.h b/include/system.h index 6d46392e38..57592217ad 100644 --- a/include/system.h +++ b/include/system.h @@ -185,6 +185,14 @@ uintptr_t get_program_memory_addr(enum ec_image copy); */ int system_jumped_to_this_image(void); +#ifdef CONFIG_ZTEST +struct system_jumped_late_mock { + int ret_val; + int call_count; +}; +extern struct system_jumped_late_mock system_jumped_late_mock; +#endif + /** * Return non-zero if late (legacy) sysjump occurred. * diff --git a/zephyr/emul/emul_isl923x.c b/zephyr/emul/emul_isl923x.c index 9b3aaec4f5..e7a286e6fb 100644 --- a/zephyr/emul/emul_isl923x.c +++ b/zephyr/emul/emul_isl923x.c @@ -105,6 +105,15 @@ struct i2c_emul *isl923x_emul_get_i2c_emul(const struct emul *emulator) return &(data->common.emul); } +void isl923x_emul_reset(const struct emul *emulator) +{ + struct isl923x_emul_data *data = emulator->data; + struct i2c_common_emul_data common_backup = data->common; + + memset(data, 0, sizeof(struct isl923x_emul_data)); + data->common = common_backup; +} + void isl923x_emul_set_manufacturer_id(const struct emul *emulator, uint16_t manufacturer_id) { diff --git a/zephyr/include/emul/emul_isl923x.h b/zephyr/include/emul/emul_isl923x.h index 3093b0cfe3..f24922afd5 100644 --- a/zephyr/include/emul/emul_isl923x.h +++ b/zephyr/include/emul/emul_isl923x.h @@ -29,6 +29,13 @@ const struct device *isl923x_emul_get_parent(const struct emul *emulator); struct i2c_emul *isl923x_emul_get_i2c_emul(const struct emul *emulator); /** + * @brief Reset all registers + * + * @param emulator The emulator to modify + */ +void isl923x_emul_reset(const struct emul *emulator); + +/** * @brief Set the manufacturer ID * * @param emulator The emulator to modify diff --git a/zephyr/shim/src/ztest_system.c b/zephyr/shim/src/ztest_system.c index 14796b5bd5..580368d7f9 100644 --- a/zephyr/shim/src/ztest_system.c +++ b/zephyr/shim/src/ztest_system.c @@ -23,9 +23,22 @@ const uint8_t *system_get_jump_tag(uint16_t tag, int *version, int *size) return NULL; } +#ifdef CONFIG_ZTEST +struct system_jumped_late_mock system_jumped_late_mock = { + .ret_val = 0, + .call_count = 0, +}; +#endif + int system_jumped_late(void) { +#ifdef CONFIG_ZTEST + system_jumped_late_mock.call_count++; + + return system_jumped_late_mock.ret_val; +#else return 0; +#endif } enum ec_image system_get_image_copy(void) diff --git a/zephyr/test/drivers/src/isl923x.c b/zephyr/test/drivers/src/isl923x.c index 2753c12b26..0bcf8c8382 100644 --- a/zephyr/test/drivers/src/isl923x.c +++ b/zephyr/test/drivers/src/isl923x.c @@ -13,6 +13,7 @@ #include "driver/charger/isl923x_public.h" #include "emul/emul_common_i2c.h" #include "emul/emul_isl923x.h" +#include "system.h" BUILD_ASSERT(CONFIG_CHARGER_SENSE_RESISTOR == 10 || CONFIG_CHARGER_SENSE_RESISTOR == 5); @@ -566,6 +567,112 @@ static void test_get_vbus_voltage(void) } } +static void test_init(void) +{ + const struct emul *isl923x_emul = ISL923X_EMUL; + struct i2c_emul *i2c_emul = isl923x_emul_get_i2c_emul(isl923x_emul); + int input_current; + + /* Test failed CTRL2 register read (prochot debounce) */ + isl923x_emul_reset(isl923x_emul); + i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); + isl923x_drv.init(CHARGER_NUM); + i2c_common_emul_set_read_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, + &input_current), + NULL); + zassert_equal(0, input_current, + "Expected input current 0mV but got %dmV", input_current); + + /* Test failed CTRL2 register write */ + isl923x_emul_reset(isl923x_emul); + i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL2); + isl923x_drv.init(CHARGER_NUM); + i2c_common_emul_set_write_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, + &input_current), + NULL); + zassert_equal(0, input_current, + "Expected input current 0mV but got %dmV", input_current); + + /* Test failed CTRL 0 read */ + isl923x_emul_reset(isl923x_emul); + i2c_common_emul_set_read_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + isl923x_drv.init(CHARGER_NUM); + i2c_common_emul_set_read_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, + &input_current), + NULL); + zassert_equal(0, input_current, + "Expected input current 0mV but got %dmV", input_current); + + /* Test failed CTRL 0 write */ + isl923x_emul_reset(isl923x_emul); + i2c_common_emul_set_write_fail_reg(i2c_emul, ISL923X_REG_CONTROL0); + isl923x_drv.init(CHARGER_NUM); + i2c_common_emul_set_write_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, + &input_current), + NULL); + zassert_equal(0, input_current, + "Expected input current 0mV but got %dmV", input_current); + + /* Test failed CTRL 3 read */ + isl923x_emul_reset(isl923x_emul); + i2c_common_emul_set_read_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + isl923x_drv.init(CHARGER_NUM); + i2c_common_emul_set_read_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, + &input_current), + NULL); + zassert_equal(0, input_current, + "Expected input current 0mV but got %dmV", input_current); + + /* Test failed CTRL 3 write */ + isl923x_emul_reset(isl923x_emul); + i2c_common_emul_set_write_fail_reg(i2c_emul, ISL9238_REG_CONTROL3); + isl923x_drv.init(CHARGER_NUM); + i2c_common_emul_set_write_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, + &input_current), + NULL); + zassert_equal(0, input_current, + "Expected input current 0mV but got %dmV", input_current); + + /* Test failed write adapter current limit */ + isl923x_emul_reset(isl923x_emul); + i2c_common_emul_set_write_fail_reg(i2c_emul, + ISL923X_REG_ADAPTER_CURRENT_LIMIT1); + isl923x_drv.init(CHARGER_NUM); + i2c_common_emul_set_write_fail_reg(i2c_emul, + I2C_COMMON_EMUL_NO_FAIL_REG); + zassert_ok(isl923x_drv.get_input_current_limit(CHARGER_NUM, + &input_current), + NULL); + zassert_equal(0, input_current, + "Expected input current 0mV but got %dmV", input_current); + + /* + * Test system_jumped_late being true (will not call + * set_input_current_limit) + */ + system_jumped_late_mock.ret_val = true; + system_jumped_late_mock.call_count = 0; + isl923x_emul_reset(isl923x_emul); + isl923x_drv.init(CHARGER_NUM); + zassert_equal( + 1, system_jumped_late_mock.call_count, + "Expected to have called system_jumped_late() once, but got %d calls", + system_jumped_late_mock.call_count); + system_jumped_late_mock.ret_val = false; +} + void test_suite_isl923x(void) { ztest_test_suite(isl923x, @@ -583,6 +690,7 @@ void test_suite_isl923x(void) ztest_unit_test(test_set_dc_prochot), ztest_unit_test(test_comparator_inversion), ztest_unit_test(test_discharge_on_ac), - ztest_unit_test(test_get_vbus_voltage)); + ztest_unit_test(test_get_vbus_voltage), + ztest_unit_test(test_init)); ztest_run_test_suite(isl923x); } |