test_hcd_isoc.c 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2022 Espressif Systems (Shanghai) CO LTD
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. */
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include "freertos/FreeRTOS.h"
  9. #include "freertos/semphr.h"
  10. #include "unity.h"
  11. #include "test_utils.h"
  12. #include "test_usb_mock_classes.h"
  13. #include "test_usb_common.h"
  14. #include "test_hcd_common.h"
  15. #define NUM_URBS 3
  16. #define NUM_PACKETS_PER_URB 3
  17. #define ISOC_PACKET_SIZE MOCK_ISOC_EP_MPS
  18. #define URB_DATA_BUFF_SIZE (NUM_PACKETS_PER_URB * ISOC_PACKET_SIZE)
  19. #define POST_ENQUEUE_DELAY_US 20
  20. /*
  21. Test HCD ISOC pipe URBs
  22. Purpose:
  23. - Test that an isochronous pipe can be created
  24. - URBs can be created and enqueued to the isoc pipe pipe
  25. - isoc pipe returns HCD_PIPE_EVENT_URB_DONE for completed URBs
  26. - Test utilizes ISOC OUT transfers and do not require ACKs. So the isoc pipe will target a non existing endpoint
  27. Procedure:
  28. - Setup HCD and wait for connection
  29. - Allocate default pipe and enumerate the device
  30. - Allocate an isochronous pipe and multiple URBs. Each URB should contain multiple packets to test HCD's ability to
  31. schedule an URB across multiple intervals.
  32. - Enqueue those URBs
  33. - Expect HCD_PIPE_EVENT_URB_DONE for each URB. Verify that data is correct using logic analyzer
  34. - Deallocate URBs
  35. - Teardown
  36. */
  37. TEST_CASE("Test HCD isochronous pipe URBs", "[hcd][ignore]")
  38. {
  39. hcd_port_handle_t port_hdl = test_hcd_setup(); //Setup the HCD and port
  40. usb_speed_t port_speed = test_hcd_wait_for_conn(port_hdl); //Trigger a connection
  41. //The MPS of the ISOC OUT pipe is quite large, so we need to bias the FIFO sizing
  42. TEST_ASSERT_EQUAL(ESP_OK, hcd_port_set_fifo_bias(port_hdl, HCD_PORT_FIFO_BIAS_PTX));
  43. vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
  44. //Enumerate and reset device
  45. hcd_pipe_handle_t default_pipe = test_hcd_pipe_alloc(port_hdl, NULL, 0, port_speed); //Create a default pipe (using a NULL EP descriptor)
  46. uint8_t dev_addr = test_hcd_enum_device(default_pipe);
  47. //Create ISOC OUT pipe to non-existent device
  48. hcd_pipe_handle_t isoc_out_pipe = test_hcd_pipe_alloc(port_hdl, &mock_isoc_out_ep_desc, dev_addr + 1, port_speed);
  49. //Create URBs
  50. urb_t *urb_list[NUM_URBS];
  51. //Initialize URBs
  52. for (int urb_idx = 0; urb_idx < NUM_URBS; urb_idx++) {
  53. urb_list[urb_idx] = test_hcd_alloc_urb(NUM_PACKETS_PER_URB, URB_DATA_BUFF_SIZE);
  54. urb_list[urb_idx]->transfer.num_bytes = URB_DATA_BUFF_SIZE;
  55. urb_list[urb_idx]->transfer.context = URB_CONTEXT_VAL;
  56. for (int pkt_idx = 0; pkt_idx < NUM_PACKETS_PER_URB; pkt_idx++) {
  57. urb_list[urb_idx]->transfer.isoc_packet_desc[pkt_idx].num_bytes = ISOC_PACKET_SIZE;
  58. //Each packet will consist of the same byte, but each subsequent packet's byte will increment (i.e., packet 0 transmits all 0x0, packet 1 transmits all 0x1)
  59. memset(&urb_list[urb_idx]->transfer.data_buffer[pkt_idx * ISOC_PACKET_SIZE], (urb_idx * NUM_URBS) + pkt_idx, ISOC_PACKET_SIZE);
  60. }
  61. }
  62. //Enqueue URBs
  63. for (int i = 0; i < NUM_URBS; i++) {
  64. TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(isoc_out_pipe, urb_list[i]));
  65. }
  66. //Wait for each done event from each URB
  67. for (int i = 0; i < NUM_URBS; i++) {
  68. test_hcd_expect_pipe_event(isoc_out_pipe, HCD_PIPE_EVENT_URB_DONE);
  69. }
  70. //Dequeue URBs
  71. for (int urb_idx = 0; urb_idx < NUM_URBS; urb_idx++) {
  72. urb_t *urb = hcd_urb_dequeue(isoc_out_pipe);
  73. TEST_ASSERT_EQUAL(urb_list[urb_idx], urb);
  74. TEST_ASSERT_EQUAL(URB_CONTEXT_VAL, urb->transfer.context);
  75. //Overall URB status and overall number of bytes
  76. TEST_ASSERT_EQUAL(URB_DATA_BUFF_SIZE, urb->transfer.actual_num_bytes);
  77. TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, urb->transfer.status);
  78. for (int pkt_idx = 0; pkt_idx < NUM_PACKETS_PER_URB; pkt_idx++) {
  79. TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, urb->transfer.isoc_packet_desc[pkt_idx].status);
  80. }
  81. }
  82. //Free URB list and pipe
  83. for (int i = 0; i < NUM_URBS; i++) {
  84. test_hcd_free_urb(urb_list[i]);
  85. }
  86. test_hcd_pipe_free(isoc_out_pipe);
  87. test_hcd_pipe_free(default_pipe);
  88. //Cleanup
  89. test_hcd_wait_for_disconn(port_hdl, false);
  90. test_hcd_teardown(port_hdl);
  91. }
  92. /*
  93. Test a port sudden disconnect with an active ISOC pipe
  94. Purpose: Test that when sudden disconnection happens on an HCD port, the ISOC pipe will
  95. - Remain active after the HCD_PORT_EVENT_SUDDEN_DISCONN port event
  96. - ISOC pipe can be halted
  97. - ISOC pipe can be flushed (and transfers status are updated accordingly)
  98. Procedure:
  99. - Setup HCD and wait for connection
  100. - Allocate default pipe and enumerate the device
  101. - Allocate an isochronous pipe and multiple URBs. Each URB should contain multiple packets to test HCD's ability to
  102. schedule an URB across multiple intervals.
  103. - Enqueue those URBs
  104. - Trigger a disconnect after a short delay
  105. - Check that HCD_PORT_EVENT_SUDDEN_DISCONN event is generated. Handle that port event.
  106. - Check that both pipes remain in the HCD_PIPE_STATE_ACTIVE after the port error.
  107. - Check that both pipes pipe can be halted.
  108. - Check that the default pipe can be flushed. A HCD_PIPE_EVENT_URB_DONE event should be generated for the ISOC pipe
  109. because it had enqueued URBs.
  110. - Check that all URBs can be dequeued and their status is updated
  111. - Free both pipes
  112. - Teardown
  113. */
  114. TEST_CASE("Test HCD isochronous pipe sudden disconnect", "[hcd][ignore]")
  115. {
  116. hcd_port_handle_t port_hdl = test_hcd_setup(); //Setup the HCD and port
  117. usb_speed_t port_speed = test_hcd_wait_for_conn(port_hdl); //Trigger a connection
  118. //The MPS of the ISOC OUT pipe is quite large, so we need to bias the FIFO sizing
  119. TEST_ASSERT_EQUAL(ESP_OK, hcd_port_set_fifo_bias(port_hdl, HCD_PORT_FIFO_BIAS_PTX));
  120. vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
  121. //Enumerate and reset device
  122. hcd_pipe_handle_t default_pipe = test_hcd_pipe_alloc(port_hdl, NULL, 0, port_speed); //Create a default pipe (using a NULL EP descriptor)
  123. uint8_t dev_addr = test_hcd_enum_device(default_pipe);
  124. //Create ISOC OUT pipe to non-existent device
  125. hcd_pipe_handle_t isoc_out_pipe = test_hcd_pipe_alloc(port_hdl, &mock_isoc_out_ep_desc, dev_addr + 1, port_speed);
  126. //Create URBs
  127. urb_t *urb_list[NUM_URBS];
  128. //Initialize URBs
  129. for (int urb_idx = 0; urb_idx < NUM_URBS; urb_idx++) {
  130. urb_list[urb_idx] = test_hcd_alloc_urb(NUM_PACKETS_PER_URB, URB_DATA_BUFF_SIZE);
  131. urb_list[urb_idx]->transfer.num_bytes = URB_DATA_BUFF_SIZE;
  132. urb_list[urb_idx]->transfer.context = URB_CONTEXT_VAL;
  133. for (int pkt_idx = 0; pkt_idx < NUM_PACKETS_PER_URB; pkt_idx++) {
  134. urb_list[urb_idx]->transfer.isoc_packet_desc[pkt_idx].num_bytes = ISOC_PACKET_SIZE;
  135. //Each packet will consist of the same byte, but each subsequent packet's byte will increment (i.e., packet 0 transmits all 0x0, packet 1 transmits all 0x1)
  136. memset(&urb_list[urb_idx]->transfer.data_buffer[pkt_idx * ISOC_PACKET_SIZE], (urb_idx * NUM_URBS) + pkt_idx, ISOC_PACKET_SIZE);
  137. }
  138. }
  139. //Enqueue URBs
  140. for (int i = 0; i < NUM_URBS; i++) {
  141. TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(isoc_out_pipe, urb_list[i]));
  142. }
  143. //Add a short delay to let the transfers run for a bit
  144. esp_rom_delay_us(POST_ENQUEUE_DELAY_US);
  145. test_usb_set_phy_state(false, 0);
  146. //Disconnect event should have occurred. Handle the port event
  147. test_hcd_expect_port_event(port_hdl, HCD_PORT_EVENT_DISCONNECTION);
  148. TEST_ASSERT_EQUAL(HCD_PORT_EVENT_DISCONNECTION, hcd_port_handle_event(port_hdl));
  149. TEST_ASSERT_EQUAL(HCD_PORT_STATE_RECOVERY, hcd_port_get_state(port_hdl));
  150. printf("Sudden disconnect\n");
  151. //Both pipes should still be active
  152. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_ACTIVE, hcd_pipe_get_state(default_pipe));
  153. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_ACTIVE, hcd_pipe_get_state(isoc_out_pipe));
  154. //Halt both pipes
  155. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_HALT));
  156. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(isoc_out_pipe, HCD_PIPE_CMD_HALT));
  157. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_HALTED, hcd_pipe_get_state(default_pipe));
  158. TEST_ASSERT_EQUAL(HCD_PIPE_STATE_HALTED, hcd_pipe_get_state(isoc_out_pipe));
  159. //Flush both pipes. ISOC pipe should return completed URBs
  160. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(default_pipe, HCD_PIPE_CMD_FLUSH));
  161. TEST_ASSERT_EQUAL(ESP_OK, hcd_pipe_command(isoc_out_pipe, HCD_PIPE_CMD_FLUSH));
  162. //Dequeue ISOC URBs
  163. for (int urb_idx = 0; urb_idx < NUM_URBS; urb_idx++) {
  164. urb_t *urb = hcd_urb_dequeue(isoc_out_pipe);
  165. TEST_ASSERT_EQUAL(urb_list[urb_idx], urb);
  166. TEST_ASSERT_EQUAL(URB_CONTEXT_VAL, urb->transfer.context);
  167. //The URB has either completed entirely or is marked as no_device
  168. TEST_ASSERT(urb->transfer.status == USB_TRANSFER_STATUS_COMPLETED || urb->transfer.status == USB_TRANSFER_STATUS_NO_DEVICE);
  169. }
  170. //Free URB list and pipe
  171. for (int i = 0; i < NUM_URBS; i++) {
  172. test_hcd_free_urb(urb_list[i]);
  173. }
  174. test_hcd_pipe_free(isoc_out_pipe);
  175. test_hcd_pipe_free(default_pipe);
  176. //Cleanup
  177. test_hcd_teardown(port_hdl);
  178. }