test_hcd_isoc.c 9.0 KB

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