From bdc0492846962ecaf244c5e490d27222514c5c7b Mon Sep 17 00:00:00 2001 From: Aaron Massey Date: Fri, 25 Feb 2022 14:12:47 -0700 Subject: zephyr: test: Attach src snk extract before/after Reduce cognitive in parsing the before and after test suite rules for the attach src and snk integration test by extract code into common functions. BRANCH=none BUG=b:209907615 TEST=zmake configure --test test-drivers Signed-off-by: Aaron Massey Change-Id: Ibe2c286f8b346676f12d60dc09dbd34117abceca Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3491403 Reviewed-by: Yuval Peress --- .../src/integration/usbc/usb_attach_src_snk.c | 152 ++++++++++----------- 1 file changed, 71 insertions(+), 81 deletions(-) (limited to 'zephyr/test/drivers/src/integration') diff --git a/zephyr/test/drivers/src/integration/usbc/usb_attach_src_snk.c b/zephyr/test/drivers/src/integration/usbc/usb_attach_src_snk.c index f57d6365e1..140b122a96 100644 --- a/zephyr/test/drivers/src/integration/usbc/usb_attach_src_snk.c +++ b/zephyr/test/drivers/src/integration/usbc/usb_attach_src_snk.c @@ -94,19 +94,13 @@ static void *integration_usb_src_snk_setup(void) return &fixture_state; } -static void integration_usb_attach_snk_then_src_before(void *state) +static void attach_src_snk_common_before(struct emul_state *my_emul_state) { - const struct integration_usb_attach_src_then_snk_fixture *fixture = - state; - struct emul_state *my_state = fixture->my_emulator_state; - const struct emul *tcpci_emul_src = my_state->tcpci_generic_emul; - const struct emul *tcpci_emul_snk = my_state->tcpci_ps8xxx_emul; - const struct emul *charger_emul = my_state->charger_isl923x_emul; - struct tcpci_src_emul *my_src = &my_state->my_src; - struct tcpci_snk_emul *my_snk = &my_state->my_snk; + const struct emul *tcpci_emul_src = my_emul_state->tcpci_generic_emul; + const struct emul *tcpci_emul_snk = my_emul_state->tcpci_ps8xxx_emul; + const struct emul *charger_emul = my_emul_state->charger_isl923x_emul; /* Reset vbus to 0mV */ - /* TODO(b/217610871): Remove redundant test state cleanup */ /* TODO(b/217737667): Remove driver specific code. */ isl923x_emul_set_adc_vbus(charger_emul, 0); @@ -116,22 +110,43 @@ static void integration_usb_attach_snk_then_src_before(void *state) * during its init. */ tcpci_emul_set_reg(tcpci_emul_snk, PS8XXX_REG_FW_REV, 0x31); + zassume_ok(tcpc_config[SRC_PORT].drv->init(SRC_PORT), NULL); - tcpci_emul_set_rev(tcpci_emul_src, TCPCI_EMUL_REV1_0_VER1_0); + pd_set_suspend(SNK_PORT, false); pd_set_suspend(SRC_PORT, false); + /* Reset to disconnected state. */ zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul_src), NULL); zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul_snk), NULL); - /* 1) Attach SINK */ - /* Set chipset to ON, this will set TCPM to DRP */ test_set_chipset_to_s0(); +} - /* TODO(b/214401892): Check why need to give time TCPM to spin */ +static void attach_src_snk_common_after(struct emul_state *my_emul_state) +{ + const struct emul *tcpci_generic_emul = + my_emul_state->tcpci_generic_emul; + const struct emul *tcpci_ps8xxx_emul = my_emul_state->tcpci_ps8xxx_emul; + const struct emul *charger_emul = my_emul_state->charger_isl923x_emul; + + tcpci_emul_disconnect_partner(tcpci_generic_emul); + tcpci_emul_disconnect_partner(tcpci_ps8xxx_emul); + + /* Give time to actually disconnect */ k_sleep(K_SECONDS(1)); + /* Reset vbus to 0mV */ + /* TODO(b/217737667): Remove driver specific code. */ + isl923x_emul_set_adc_vbus(charger_emul, 0); +} + +static void attach_emulated_snk(struct emul_state *my_emul_state) +{ + const struct emul *tcpci_emul_snk = my_emul_state->tcpci_ps8xxx_emul; + struct tcpci_snk_emul *my_snk = &my_emul_state->my_snk; + /* Attach emulated sink */ tcpci_snk_emul_init(my_snk); tcpci_emul_set_rev(tcpci_emul_snk, TCPCI_EMUL_REV1_0_VER1_0); @@ -140,107 +155,82 @@ static void integration_usb_attach_snk_then_src_before(void *state) tcpci_emul_snk), NULL); - /* Wait for PD negotiation */ - k_sleep(K_SECONDS(10)); + /* TODO(b/214401892): Check why need to give time TCPM to spin */ + k_sleep(K_SECONDS(1)); +} - /* 2) Attach SOURCE */ +static void attach_emulated_src(struct emul_state *my_emul_state) +{ + const struct emul *tcpci_emul_src = my_emul_state->tcpci_generic_emul; + const struct emul *charger_emul = my_emul_state->charger_isl923x_emul; + struct tcpci_src_emul *my_src = &my_emul_state->my_src; /* Attach emulated charger. */ tcpci_src_emul_init(my_src); + tcpci_emul_set_rev(tcpci_emul_src, TCPCI_EMUL_REV1_0_VER1_0); zassume_ok(tcpci_src_emul_connect_to_tcpci( &my_src->data, &my_src->common_data, &my_src->ops, tcpci_emul_src), NULL); isl923x_emul_set_adc_vbus(charger_emul, DEFAULT_VBUS_MV); - - /* Wait for current ramp. */ - k_sleep(K_SECONDS(10)); } -static void integration_usb_attach_src_then_snk_before(void *state) +static void integration_usb_attach_snk_then_src_before(void *state) { const struct integration_usb_attach_src_then_snk_fixture *fixture = state; struct emul_state *my_state = fixture->my_emulator_state; - const struct emul *tcpci_emul_src = my_state->tcpci_generic_emul; - const struct emul *tcpci_emul_snk = my_state->tcpci_ps8xxx_emul; - const struct emul *charger_emul = my_state->charger_isl923x_emul; - - struct tcpci_src_emul *my_src = &my_state->my_src; - struct tcpci_snk_emul *my_snk = &my_state->my_snk; + attach_src_snk_common_before(my_state); - /* Reset vbus to 0mV */ - /* TODO(b/217610871): Remove redundant test state cleanup */ - /* TODO(b/217737667): Remove driver specific code. */ - isl923x_emul_set_adc_vbus(charger_emul, 0); - - zassume_ok(tcpc_config[SNK_PORT].drv->init(SNK_PORT), NULL); - /* - * Arbitrary FW ver. The emulator should really be setting this - * during its init. - */ - tcpci_emul_set_reg(tcpci_emul_snk, PS8XXX_REG_FW_REV, 0x31); - zassume_ok(tcpc_config[SRC_PORT].drv->init(SRC_PORT), NULL); - tcpci_emul_set_rev(tcpci_emul_src, TCPCI_EMUL_REV1_0_VER1_0); - pd_set_suspend(SNK_PORT, false); - pd_set_suspend(SRC_PORT, false); - /* Reset to disconnected state. */ - zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul_src), NULL); - zassume_ok(tcpci_emul_disconnect_partner(tcpci_emul_snk), NULL); + /* 1) Attach SINK */ + attach_emulated_snk(my_state); - /* 1) Attach SOURCE */ + /* Wait for PD negotiation */ + k_sleep(K_SECONDS(10)); - /* Attach emulated charger. */ - tcpci_src_emul_init(my_src); - zassume_ok(tcpci_src_emul_connect_to_tcpci( - &my_src->data, &my_snk->common_data, &my_snk->ops, - tcpci_emul_src), - NULL); - isl923x_emul_set_adc_vbus(charger_emul, DEFAULT_VBUS_MV); + /* 2) Attach SOURCE */ + attach_emulated_src(my_state); - /* Wait for current ramp. */ + /* Wait for PD negotiation */ k_sleep(K_SECONDS(10)); +} - /* 2) Attach SINK */ +static void integration_usb_attach_src_then_snk_before(void *state) +{ + const struct integration_usb_attach_src_then_snk_fixture *fixture = + state; + struct emul_state *my_state = fixture->my_emulator_state; - /* Set chipset to ON, this will set TCPM to DRP */ - test_set_chipset_to_s0(); + attach_src_snk_common_before(my_state); - /* TODO(b/214401892): Check why need to give time TCPM to spin */ - k_sleep(K_SECONDS(1)); + /* 1) Attach SOURCE */ + attach_emulated_src(my_state); - /* Attach emulated sink */ - tcpci_snk_emul_init(my_snk); - tcpci_emul_set_rev(tcpci_emul_snk, TCPCI_EMUL_REV1_0_VER1_0); - zassume_ok(tcpci_snk_emul_connect_to_tcpci( - &my_snk->data, &my_snk->common_data, &my_snk->ops, - tcpci_emul_snk), - NULL); + /* Wait for PD negotiation */ + k_sleep(K_SECONDS(10)); + + /* 2) Attach SINK */ + attach_emulated_snk(my_state); /* Wait for PD negotiation */ k_sleep(K_SECONDS(10)); } -static void integration_usb_attach_src_snk_after(void *state) +static void integration_usb_attach_src_then_snk_after(void *state) { const struct integration_usb_attach_src_then_snk_fixture *fixture = state; - struct emul_state *my_state = fixture->my_emulator_state; - - const struct emul *tcpci_generic_emul = my_state->tcpci_generic_emul; - const struct emul *tcpci_ps8xxx_emul = my_state->tcpci_ps8xxx_emul; - const struct emul *charger_emul = my_state->charger_isl923x_emul; - tcpci_emul_disconnect_partner(tcpci_generic_emul); - tcpci_emul_disconnect_partner(tcpci_ps8xxx_emul); + attach_src_snk_common_after(fixture->my_emulator_state); +} - /* Give time to actually disconnect */ - k_sleep(K_SECONDS(1)); +static void integration_usb_attach_snk_then_src_after(void *state) +{ + const struct integration_usb_attach_snk_then_src_fixture *fixture = + state; - /* Reset vbus to 0mV */ - /* TODO(b/217737667): Remove driver specific code. */ - isl923x_emul_set_adc_vbus(charger_emul, 0); + attach_src_snk_common_after(fixture->my_emulator_state); } ZTEST_F(integration_usb_attach_src_then_snk, verify_snk_port_pd_info) @@ -462,9 +452,9 @@ ZTEST_F(integration_usb_attach_src_then_snk, verify_src_port_typec_status) ZTEST_SUITE(integration_usb_attach_src_then_snk, drivers_predicate_post_main, integration_usb_src_snk_setup, integration_usb_attach_src_then_snk_before, - integration_usb_attach_src_snk_after, NULL); + integration_usb_attach_src_then_snk_after, NULL); ZTEST_SUITE(integration_usb_attach_snk_then_src, drivers_predicate_post_main, integration_usb_src_snk_setup, integration_usb_attach_snk_then_src_before, - integration_usb_attach_src_snk_after, NULL); + integration_usb_attach_snk_then_src_after, NULL); -- cgit v1.2.1