test_hcd_isoc.c 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. /*
  2. * SPDX-FileCopyrightText: 2015-2021 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_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. /*
  19. Test HCD ISOC pipe URBs
  20. Purpose:
  21. - Test that an isochronous pipe can be created
  22. - URBs can be created and enqueued to the isoc pipe pipe
  23. - isoc pipe returns HCD_PIPE_EVENT_URB_DONE for completed URBs
  24. - Test utilizes ISOC OUT transfers and do not require ACKs. So the isoc pipe will target a non existing endpoint
  25. Procedure:
  26. - Setup HCD and wait for connection
  27. - Allocate default pipe and enumerate the device
  28. - Allocate an isochronous pipe and multiple URBs. Each URB should contain multiple packets to test HCD's ability to
  29. schedule an URB across multiple intervals.
  30. - Enqueue those URBs
  31. - Expect HCD_PIPE_EVENT_URB_DONE for each URB. Verify that data is correct using logic analyzer
  32. - Deallocate URBs
  33. - Teardown
  34. */
  35. TEST_CASE("Test HCD isochronous pipe URBs", "[hcd][ignore]")
  36. {
  37. hcd_port_handle_t port_hdl = test_hcd_setup(); //Setup the HCD and port
  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. for (int pkt_idx = 0; pkt_idx < NUM_PACKETS_PER_URB; pkt_idx++) {
  74. TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, urb->transfer.isoc_packet_desc[pkt_idx].status);
  75. }
  76. }
  77. //Free URB list and pipe
  78. for (int i = 0; i < NUM_URBS; i++) {
  79. test_hcd_free_urb(urb_list[i]);
  80. }
  81. test_hcd_pipe_free(isoc_out_pipe);
  82. test_hcd_pipe_free(default_pipe);
  83. //Cleanup
  84. test_hcd_wait_for_disconn(port_hdl, false);
  85. test_hcd_teardown(port_hdl);
  86. }