refactor(usb): Remove use of usb_phy_action() from unit tests

Currently, USB Host unit tests that require a software triggered disconnection/
reconnection rely on the 'usb_phy_action()' function.

This commit replaces those calls with 'hcd_port_command()' or
'usb_host_lib_set_root_port_power()'.

Note: Also removed 'test_usb_common.h/c' as it is no longer necessary are the
function call replacements.
This commit is contained in:
Darian Leung 2024-05-30 02:13:26 +08:00 committed by Tomas Rezucha
parent 54db1cda8b
commit cac0ef9d11
15 changed files with 50 additions and 110 deletions

View File

@ -2,6 +2,5 @@ idf_component_register(SRCS "dev_hid.c"
"dev_isoc.c"
"dev_msc.c"
"mock_msc.c"
"test_usb_common.c"
INCLUDE_DIRS "."
REQUIRES usb unity)

View File

@ -1,46 +0,0 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_err.h"
#include "hal/usb_phy_types.h"
#include "esp_private/usb_phy.h"
#include "test_usb_common.h"
#include "unity.h"
static usb_phy_handle_t phy_hdl = NULL;
void test_usb_init_phy(void)
{
// Initialize the internal USB PHY to connect to the USB OTG peripheral
usb_phy_config_t phy_config = {
.controller = USB_PHY_CTRL_OTG,
.target = USB_PHY_TARGET_INT,
.otg_mode = USB_OTG_MODE_HOST,
.otg_speed = USB_PHY_SPEED_UNDEFINED, // In Host mode, the speed is determined by the connected device
.ext_io_conf = NULL,
.otg_io_conf = NULL,
};
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, usb_new_phy(&phy_config, &phy_hdl), "Failed to init USB PHY");
}
void test_usb_deinit_phy(void)
{
// Deinitialize the internal USB PHY
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, usb_del_phy(phy_hdl), "Failed to delete PHY");
phy_hdl = NULL;
}
void test_usb_set_phy_state(bool connected, TickType_t delay_ticks)
{
if (delay_ticks > 0) {
// Delay of 0 ticks causes a yield. So skip if delay_ticks is 0.
vTaskDelay(delay_ticks);
}
ESP_ERROR_CHECK(usb_phy_action(phy_hdl, (connected) ? USB_PHY_ACTION_HOST_ALLOW_CONN : USB_PHY_ACTION_HOST_FORCE_DISCONN));
}

View File

@ -1,26 +0,0 @@
/*
* SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
*
* SPDX-License-Identifier: Apache-2.0
*/
#include <stdbool.h>
#include "freertos/FreeRTOS.h"
/**
* @brief Initialize the internal USB PHY and USB Controller for USB Host testing
*/
void test_usb_init_phy(void);
/**
* @brief Deinitialize the internal USB PHY and USB Controller after USB Host testing
*/
void test_usb_deinit_phy(void);
/**
* @brief For the USB PHY into the connected or disconnected state
*
* @param connected For into connected state if true, disconnected if false
* @param delay_ticks Delay in ticks before forcing state
*/
void test_usb_set_phy_state(bool connected, TickType_t delay_ticks);

View File

@ -17,8 +17,8 @@
#include "hcd.h"
#include "usb_private.h"
#include "usb/usb_types_ch9.h"
#include "esp_private/usb_phy.h"
#include "test_hcd_common.h"
#include "test_usb_common.h"
#include "mock_msc.h"
#include "unity.h"
@ -38,6 +38,7 @@ typedef struct {
} pipe_event_msg_t;
hcd_port_handle_t port_hdl = NULL;
static usb_phy_handle_t phy_hdl = NULL;
// ---------------------------------------------------- Private --------------------------------------------------------
@ -144,7 +145,21 @@ int test_hcd_get_num_pipe_events(hcd_pipe_handle_t pipe_hdl)
hcd_port_handle_t test_hcd_setup(void)
{
test_usb_init_phy(); // Initialize the internal USB PHY and USB Controller for testing
// Deinitialize PHY from previous failed test
if (phy_hdl != NULL) {
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, usb_del_phy(phy_hdl), "Failed to delete PHY");
phy_hdl = NULL;
}
// Initialize the internal USB PHY to connect to the USB OTG peripheral
usb_phy_config_t phy_config = {
.controller = USB_PHY_CTRL_OTG,
.target = USB_PHY_TARGET_INT,
.otg_mode = USB_OTG_MODE_HOST,
.otg_speed = USB_PHY_SPEED_UNDEFINED, // In Host mode, the speed is determined by the connected device
.ext_io_conf = NULL,
.otg_io_conf = NULL,
};
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, usb_new_phy(&phy_config, &phy_hdl), "Failed to init USB PHY");
// Create a queue for port callback to queue up port events
QueueHandle_t port_evt_queue = xQueueCreate(EVENT_QUEUE_LEN, sizeof(port_event_msg_t));
TEST_ASSERT_NOT_NULL(port_evt_queue);
@ -163,8 +178,6 @@ hcd_port_handle_t test_hcd_setup(void)
hcd_port_handle_t port_hdl;
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_init(PORT_NUM, &port_config, &port_hdl));
TEST_ASSERT_NOT_NULL(port_hdl);
TEST_ASSERT_EQUAL(HCD_PORT_STATE_NOT_POWERED, hcd_port_get_state(port_hdl));
test_usb_set_phy_state(false, 0); // Force disconnected state on PHY
return port_hdl;
}
@ -181,17 +194,18 @@ void test_hcd_teardown(hcd_port_handle_t port_hdl)
// Uninstall the HCD
TEST_ASSERT_EQUAL(ESP_OK, hcd_uninstall());
vQueueDelete(port_evt_queue);
test_usb_deinit_phy(); // Deinitialize the internal USB PHY after testing
// Deinitialize the internal USB PHY
TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, usb_del_phy(phy_hdl), "Failed to delete PHY");
phy_hdl = NULL;
}
usb_speed_t test_hcd_wait_for_conn(hcd_port_handle_t port_hdl)
{
// Power ON the port
// Power ON the port. This should allow for connections to occur
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_POWER_ON));
TEST_ASSERT_EQUAL(HCD_PORT_STATE_DISCONNECTED, hcd_port_get_state(port_hdl));
// Wait for connection event
printf("Waiting for connection\n");
test_usb_set_phy_state(true, pdMS_TO_TICKS(100)); // Allow for connected state on PHY
test_hcd_expect_port_event(port_hdl, HCD_PORT_EVENT_CONNECTION);
TEST_ASSERT_EQUAL(HCD_PORT_EVENT_CONNECTION, hcd_port_handle_event(port_hdl));
TEST_ASSERT_EQUAL(HCD_PORT_STATE_DISABLED, hcd_port_get_state(port_hdl));
@ -217,9 +231,10 @@ void test_hcd_wait_for_disconn(hcd_port_handle_t port_hdl, bool already_disabled
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_DISABLE));
TEST_ASSERT_EQUAL(HCD_PORT_STATE_DISABLED, hcd_port_get_state(port_hdl));
}
// Wait for a safe disconnect
printf("Waiting for disconnection\n");
test_usb_set_phy_state(false, pdMS_TO_TICKS(100)); // Force disconnected state on PHY
// Power-off the port to trigger a disconnection
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_POWER_OFF));
// Wait for the port disconnection event
test_hcd_expect_port_event(port_hdl, HCD_PORT_EVENT_DISCONNECTION);
TEST_ASSERT_EQUAL(HCD_PORT_EVENT_DISCONNECTION, hcd_port_handle_event(port_hdl));
TEST_ASSERT_EQUAL(HCD_PORT_STATE_RECOVERY, hcd_port_get_state(port_hdl));

View File

@ -53,6 +53,8 @@ int test_hcd_get_num_pipe_events(hcd_pipe_handle_t pipe_hdl);
/**
* @brief Sets up the HCD and initializes an HCD port.
*
* The HCD port is left in the HCD_PORT_STATE_NOT_POWERED state
*
* @return hcd_port_handle_t Port handle
*/
hcd_port_handle_t test_hcd_setup(void);
@ -67,7 +69,7 @@ void test_hcd_teardown(hcd_port_handle_t port_hdl);
/**
* @brief Wait for a connection on an HCD port
*
* @note This function will internally call test_usb_set_phy_state() to allow for a connection
* @note HCD_PORT_CMD_POWER_ON is called internally to allow connections
*
* @param port_hdl Port handle
* @return usb_speed_t Speed of the connected device
@ -77,7 +79,7 @@ usb_speed_t test_hcd_wait_for_conn(hcd_port_handle_t port_hdl);
/**
* @brief Wait for a disconnection on an HCD port
*
* @note This fucntion will internally call test_usb_set_phy_state() to force a disconnection
* @note HCD_PORT_CMD_POWER_OFF is called internally to force a disconnection
*
* @param port_hdl Port handle
* @param already_disabled Whether the HCD port is already in the disabled state

View File

@ -12,7 +12,6 @@
#include "unity.h"
#include "dev_isoc.h"
#include "usb/usb_types_ch9.h"
#include "test_usb_common.h"
#include "test_hcd_common.h"
#define NUM_URBS 3
@ -261,7 +260,8 @@ TEST_CASE("Test HCD isochronous pipe sudden disconnect", "[isoc][full_speed]")
}
// Add a short delay to let the transfers run for a bit
esp_rom_delay_us(POST_ENQUEUE_DELAY_US);
test_usb_set_phy_state(false, 0);
// Power-off the port to trigger a disconnection
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_POWER_OFF));
// Disconnect event should have occurred. Handle the port event
test_hcd_expect_port_event(port_hdl, HCD_PORT_EVENT_DISCONNECTION);
TEST_ASSERT_EQUAL(HCD_PORT_EVENT_DISCONNECTION, hcd_port_handle_event(port_hdl));

View File

@ -9,7 +9,6 @@
#include "freertos/semphr.h"
#include "unity.h"
#include "esp_rom_sys.h"
#include "test_usb_common.h"
#include "test_hcd_common.h"
#define TEST_DEV_ADDR 0
@ -87,7 +86,8 @@ TEST_CASE("Test HCD port sudden disconnect", "[port][low_speed][full_speed]")
}
// Add a short delay to let the transfers run for a bit
esp_rom_delay_us(POST_ENQUEUE_DELAY_US);
test_usb_set_phy_state(false, 0);
// Power-off the port to trigger a disconnection
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_POWER_OFF));
// Disconnect event should have occurred. Handle the port event
test_hcd_expect_port_event(port_hdl, HCD_PORT_EVENT_DISCONNECTION);
TEST_ASSERT_EQUAL(HCD_PORT_EVENT_DISCONNECTION, hcd_port_handle_event(port_hdl));
@ -290,8 +290,8 @@ static void concurrent_task(void *arg)
SemaphoreHandle_t sync_sem = (SemaphoreHandle_t) arg;
xSemaphoreTake(sync_sem, portMAX_DELAY);
vTaskDelay(pdMS_TO_TICKS(10)); // Give a short delay let reset command start in main thread
// Force a disconnection
test_usb_set_phy_state(false, 0);
// Power-off the port to trigger a disconnection
TEST_ASSERT_EQUAL(ESP_OK, hcd_port_command(port_hdl, HCD_PORT_CMD_POWER_OFF));
vTaskDelay(portMAX_DELAY); // Block forever and wait to be deleted
}
@ -314,7 +314,8 @@ TEST_CASE("Test HCD port command bailout", "[port][low_speed][full_speed]")
// Attempt to resume the port. But the concurrent task should override this with a disconnection event
printf("Attempting to resume\n");
xSemaphoreGive(sync_sem); // Trigger concurrent task
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_RESPONSE, hcd_port_command(port_hdl, HCD_PORT_CMD_RESUME));
vTaskDelay(pdMS_TO_TICKS(20)); // Short delay for concurrent task to trigger disconnection
TEST_ASSERT_EQUAL(ESP_ERR_INVALID_STATE, hcd_port_command(port_hdl, HCD_PORT_CMD_RESUME));
// Check that concurrent task triggered a sudden disconnection
test_hcd_expect_port_event(port_hdl, HCD_PORT_EVENT_DISCONNECTION);

View File

@ -10,7 +10,6 @@
#include "freertos/task.h"
#include "esp_err.h"
#include "esp_log.h"
#include "test_usb_common.h"
#include "dev_msc.h"
#include "ctrl_client.h"
#include "usb/usb_host.h"

View File

@ -14,7 +14,6 @@
#include "esp_log.h"
#include "mock_msc.h"
#include "dev_msc.h"
#include "test_usb_common.h"
#include "msc_client.h"
#include "usb/usb_types_ch9.h"
#include "usb/usb_host.h"
@ -251,8 +250,8 @@ void msc_client_async_dconn_task(void *arg)
for (int i = 0; i < msc_obj.num_data_transfers; i++) {
TEST_ASSERT_EQUAL(ESP_OK, usb_host_transfer_submit(xfer_in[i]));
}
// Trigger a disconnect
test_usb_set_phy_state(false, 0);
// Trigger a disconnect by powering OFF the root port
usb_host_lib_set_root_port_power(false);
// Next stage set from transfer callback
break;
}
@ -265,7 +264,8 @@ void msc_client_async_dconn_task(void *arg)
// Start the next test iteration by going back to TEST_STAGE_WAIT_CONN and reenabling connections
msc_obj.next_stage = TEST_STAGE_WAIT_CONN;
skip_event_handling = true; // Need to execute TEST_STAGE_WAIT_CONN
test_usb_set_phy_state(true, 0);
// Allow connections again by powering ON the root port
usb_host_lib_set_root_port_power(true);
} else {
exit_loop = true;
}

View File

@ -14,7 +14,6 @@
#include "esp_log.h"
#include "dev_msc.h"
#include "mock_msc.h"
#include "test_usb_common.h"
#include "msc_client.h"
#include "usb/usb_host.h"
#include "unity.h"
@ -177,8 +176,8 @@ void msc_client_async_enum_task(void *arg)
enum_iter++;
if (enum_iter < TEST_ENUM_ITERATIONS) {
// Start the next test iteration by disconnecting the device, then going back to TEST_STAGE_WAIT_CONN stage
test_usb_set_phy_state(false, 0);
test_usb_set_phy_state(true, 0);
usb_host_lib_set_root_port_power(false);
usb_host_lib_set_root_port_power(true);
msc_obj.next_stage = TEST_STAGE_WAIT_CONN;
skip_event_handling = true; // Need to execute TEST_STAGE_WAIT_CONN
} else {

View File

@ -12,7 +12,6 @@
#include "freertos/task.h"
#include "esp_err.h"
#include "esp_log.h"
#include "test_usb_common.h"
#include "mock_msc.h"
#include "dev_msc.h"
#include "msc_client.h"

View File

@ -11,7 +11,6 @@
#include "freertos/semphr.h"
#include "esp_err.h"
#include "esp_log.h"
#include "test_usb_common.h"
#include "multiconf_client.h"
#include "mock_msc.h"
#include "dev_msc.h"

View File

@ -9,7 +9,6 @@
#include "unity_test_utils_memory.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "test_usb_common.h"
#include "dev_msc.h"
#include "usb/usb_host.h"
@ -17,10 +16,10 @@ void setUp(void)
{
unity_utils_record_free_mem();
dev_msc_init();
test_usb_init_phy(); // Initialize the internal USB PHY and USB Controller for testing
// Install USB Host
usb_host_config_t host_config = {
.skip_phy_setup = true, // test_usb_init_phy() will already have setup the internal USB PHY for us
.skip_phy_setup = false,
.root_port_unpowered = false,
.intr_flags = ESP_INTR_FLAG_LEVEL1,
};
ESP_ERROR_CHECK(usb_host_install(&host_config));
@ -35,7 +34,6 @@ void tearDown(void)
ESP_ERROR_CHECK(usb_host_uninstall());
// Short delay to allow task to be cleaned up after client uninstall
vTaskDelay(10);
test_usb_deinit_phy(); // Deinitialize the internal USB PHY after testing
unity_utils_evaluate_leaks();
}

View File

@ -10,7 +10,6 @@
#include "freertos/semphr.h"
#include "esp_err.h"
#include "esp_intr_alloc.h"
#include "test_usb_common.h"
#include "dev_msc.h"
#include "msc_client.h"
#include "ctrl_client.h"
@ -252,8 +251,9 @@ TEST_CASE("Test USB Host async API", "[usb_host][full_speed][low_speed]")
client0_dev_hdl,
dev_info->bInterfaceNumber));
// Trigger a disconnect by powering OFF the root port
usb_host_lib_set_root_port_power(false);
// Wait until the device disconnects and the clients receive the event
test_usb_set_phy_state(false, 0);
while (!(client0_stage == CLIENT_TEST_STAGE_DCONN && client1_stage == CLIENT_TEST_STAGE_DCONN)) {
usb_host_lib_handle_events(0, NULL);
usb_host_client_handle_events(client0_hdl, 0);

View File

@ -9,7 +9,6 @@
#include "freertos/task.h"
#include "esp_err.h"
#include "esp_intr_alloc.h"
#include "test_usb_common.h"
#include "mock_msc.h"
#include "dev_msc.h"
#include "msc_client.h"
@ -49,7 +48,8 @@ TEST_CASE("Test USB Host sudden disconnection (no client)", "[usb_host][full_spe
// We've just connected. Trigger a disconnect
connected = true;
printf("Forcing Sudden Disconnect\n");
test_usb_set_phy_state(false, 0);
// Trigger a disconnect by powering OFF the root port
usb_host_lib_set_root_port_power(false);
}
}
if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
@ -58,7 +58,8 @@ TEST_CASE("Test USB Host sudden disconnection (no client)", "[usb_host][full_spe
if (++dconn_iter < TEST_DCONN_NO_CLIENT_ITERATIONS) {
// Start next iteration
connected = false;
test_usb_set_phy_state(true, 0);
// Allow connections again by powering ON the root port
usb_host_lib_set_root_port_power(true);
} else {
break;
}