test_usb_common.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdbool.h>
  7. #include "freertos/FreeRTOS.h"
  8. #include "freertos/task.h"
  9. #include "esp_err.h"
  10. #include "hal/usb_phy_types.h"
  11. #include "esp_private/usb_phy.h"
  12. #include "test_usb_common.h"
  13. #include "unity.h"
  14. static usb_phy_handle_t phy_hdl = NULL;
  15. void test_usb_init_phy(void)
  16. {
  17. //Initialize the internal USB PHY to connect to the USB OTG peripheral
  18. usb_phy_config_t phy_config = {
  19. .controller = USB_PHY_CTRL_OTG,
  20. .target = USB_PHY_TARGET_INT,
  21. .otg_mode = USB_OTG_MODE_HOST,
  22. .otg_speed = USB_PHY_SPEED_UNDEFINED, //In Host mode, the speed is determined by the connected device
  23. .ext_io_conf = NULL,
  24. .otg_io_conf = NULL,
  25. };
  26. TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, usb_new_phy(&phy_config, &phy_hdl), "Failed to init USB PHY");
  27. }
  28. void test_usb_deinit_phy(void)
  29. {
  30. //Deinitialize the internal USB PHY
  31. TEST_ASSERT_EQUAL_MESSAGE(ESP_OK, usb_del_phy(phy_hdl), "Failed to delete PHY");
  32. phy_hdl = NULL;
  33. }
  34. void test_usb_set_phy_state(bool connected, TickType_t delay_ticks)
  35. {
  36. if (delay_ticks > 0) {
  37. //Delay of 0 ticks causes a yield. So skip if delay_ticks is 0.
  38. vTaskDelay(delay_ticks);
  39. }
  40. ESP_ERROR_CHECK(usb_phy_action(phy_hdl, (connected) ? USB_PHY_ACTION_HOST_ALLOW_CONN : USB_PHY_ACTION_HOST_FORCE_DISCONN));
  41. }