test_hcd_common.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include "freertos/FreeRTOS.h"
  7. #include "freertos/semphr.h"
  8. #include "hcd.h"
  9. #include "usb_private.h"
  10. #include "usb/usb_types_ch9.h"
  11. #define URB_CONTEXT_VAL ((void *)0xDEADBEEF)
  12. // ------------------------------------------------- HCD Event Test ----------------------------------------------------
  13. /**
  14. * @brief Expect (wait) for an HCD port event
  15. *
  16. * @param port_hdl Port handle to expect event from
  17. * @param expected_event Port event to expect
  18. */
  19. void test_hcd_expect_port_event(hcd_port_handle_t port_hdl, hcd_port_event_t expected_event);
  20. /**
  21. * @brief Expect (wait) for an HCD pipe event
  22. *
  23. * @param pipe_hdl Pipe handle to expect event from
  24. * @param expected_event Pipe event to expect
  25. */
  26. void test_hcd_expect_pipe_event(hcd_pipe_handle_t pipe_hdl, hcd_pipe_event_t expected_event);
  27. /**
  28. * @brief Get the current number of queued port events (dequeued using test_hcd_expect_port_event())
  29. *
  30. * @param port_hdl Port handle
  31. * @return int Number of port events currently queued
  32. */
  33. int test_hcd_get_num_port_events(hcd_port_handle_t port_hdl);
  34. /**
  35. * @brief Get the current number of queued pipe events (dequeued using test_hcd_expect_pipe_event())
  36. *
  37. * @param pipe_hdl Pipe handle
  38. * @return int Number of pipe events currently queued
  39. */
  40. int test_hcd_get_num_pipe_events(hcd_pipe_handle_t pipe_hdl);
  41. // ----------------------------------------------- Driver/Port Related -------------------------------------------------
  42. /**
  43. * @brief For the USB PHY into the connected or disconnected state
  44. *
  45. * @param connected For into connected state if true, disconnected if false
  46. * @param delay_ticks Delay in ticks before forcing state
  47. */
  48. void test_hcd_force_conn_state(bool connected, TickType_t delay_ticks);
  49. /**
  50. * @brief Sets up the HCD and initializes an HCD port.
  51. *
  52. * @return hcd_port_handle_t Port handle
  53. */
  54. hcd_port_handle_t test_hcd_setup(void);
  55. /**
  56. * @brief Frees and HCD port and uninstalls the HCD
  57. *
  58. * @param port_hdl Port handle
  59. */
  60. void test_hcd_teardown(hcd_port_handle_t port_hdl);
  61. /**
  62. * @brief Wait for a connection on an HCD port
  63. *
  64. * @note This function will internally call test_hcd_force_conn_state() to allow for a connection
  65. *
  66. * @param port_hdl Port handle
  67. * @return usb_speed_t Speed of the connected device
  68. */
  69. usb_speed_t test_hcd_wait_for_conn(hcd_port_handle_t port_hdl);
  70. /**
  71. * @brief Wait for a disconnection on an HCD port
  72. *
  73. * @note This fucntion will internally call test_hcd_force_conn_state() to force a disconnection
  74. *
  75. * @param port_hdl Port handle
  76. * @param already_disabled Whether the HCD port is already in the disabled state
  77. */
  78. void test_hcd_wait_for_disconn(hcd_port_handle_t port_hdl, bool already_disabled);
  79. // ------------------------------------------------- Pipe alloc/free ---------------------------------------------------
  80. /**
  81. * @brief Test the allocation of a pipe
  82. *
  83. * @param port_hdl Port handle
  84. * @param ep_desc Endpoint descriptor
  85. * @param dev_addr Device address of the pipe
  86. * @param dev_speed Device speed of the pipe
  87. * @return hcd_pipe_handle_t Pipe handle
  88. */
  89. hcd_pipe_handle_t test_hcd_pipe_alloc(hcd_port_handle_t port_hdl, const usb_ep_desc_t *ep_desc, uint8_t dev_addr, usb_speed_t dev_speed);
  90. /**
  91. * @brief Test the freeing of a pipe
  92. *
  93. * @param pipe_hdl Pipe handle
  94. */
  95. void test_hcd_pipe_free(hcd_pipe_handle_t pipe_hdl);
  96. /**
  97. * @brief Allocate a URB
  98. *
  99. * @param num_isoc_packets Number of isochronous packets
  100. * @param data_buffer_size Size of the data buffer of the URB
  101. * @return urb_t* URB
  102. */
  103. urb_t *test_hcd_alloc_urb(int num_isoc_packets, size_t data_buffer_size);
  104. /**
  105. * @brief Free a URB
  106. *
  107. * @param urb URB
  108. */
  109. void test_hcd_free_urb(urb_t *urb);
  110. // --------------------------------------------------- Enumeration -----------------------------------------------------
  111. /**
  112. * @brief Do some basic enumeration of the device
  113. *
  114. * For tests that need a device to have been enumerated (such as bulk tests). This function will enumerate that device
  115. * using the device's default pipe. The minimal enumeration will include
  116. *
  117. * - Getting the device's descriptor and updating the default pipe's MPS
  118. * - Setting the device's address and updating the default pipe to use that address
  119. * - Setting the device to configuration 1 (i.e., the first configuration found
  120. *
  121. * @param default_pipe The connected device's default pipe
  122. * @return uint8_t The address of the device after enumeration
  123. */
  124. uint8_t test_hcd_enum_device(hcd_pipe_handle_t default_pipe);