test_hcd_intr.c 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 "freertos/FreeRTOS.h"
  8. #include "freertos/semphr.h"
  9. #include "unity.h"
  10. #include "test_utils.h"
  11. #include "test_usb_mock_classes.h"
  12. #include "test_hcd_common.h"
  13. // --------------------------------------------------- Test Cases ------------------------------------------------------
  14. /*
  15. Test HCD interrupt pipe URBs
  16. Purpose:
  17. - Test that an interrupt pipe can be created
  18. - URBs can be created and enqueued to the interrupt pipe
  19. - Interrupt pipe returns HCD_PIPE_EVENT_URB_DONE
  20. - Test that URBs can be aborted when enqueued
  21. Procedure:
  22. - Setup HCD and wait for connection
  23. - Allocate default pipe and enumerate the device
  24. - Setup interrupt pipe and allocate URBs
  25. - Enqueue URBs, expect HCD_PIPE_EVENT_URB_DONE, and requeue
  26. - Stop after fixed number of iterations
  27. - Deallocate URBs
  28. - Teardown
  29. Note: Some mice will NAK until it is moved, so try moving the mouse around if this test case gets stuck.
  30. */
  31. #define TEST_HID_DEV_SPEED USB_SPEED_LOW
  32. #define NUM_URBS 3
  33. #define URB_DATA_BUFF_SIZE 4 //MPS is 4
  34. #define NUM_URB_ITERS (NUM_URBS * 100)
  35. TEST_CASE("Test HCD interrupt 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. TEST_ASSERT_EQUAL(TEST_HID_DEV_SPEED, TEST_HID_DEV_SPEED);
  40. vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
  41. 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)
  42. uint8_t dev_addr = test_hcd_enum_device(default_pipe);
  43. //Allocate interrupt pipe and URBS
  44. hcd_pipe_handle_t intr_pipe = test_hcd_pipe_alloc(port_hdl, &mock_hid_mouse_in_ep_desc, dev_addr, port_speed);
  45. urb_t *urb_list[NUM_URBS];
  46. for (int i = 0; i < NUM_URBS; i++) {
  47. urb_list[i] = test_hcd_alloc_urb(0, URB_DATA_BUFF_SIZE);
  48. urb_list[i]->transfer.num_bytes = URB_DATA_BUFF_SIZE;
  49. urb_list[i]->transfer.context = URB_CONTEXT_VAL;
  50. }
  51. //Enqueue URBs
  52. for (int i = 0; i < NUM_URBS; i++) {
  53. TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(intr_pipe, urb_list[i]));
  54. }
  55. int iter_count = NUM_URB_ITERS;
  56. for (iter_count = NUM_URB_ITERS; iter_count > 0; iter_count--) {
  57. //Wait for an URB to be done
  58. test_hcd_expect_pipe_event(intr_pipe, HCD_PIPE_EVENT_URB_DONE);
  59. //Dequeue the URB and check results
  60. urb_t *urb = hcd_urb_dequeue(intr_pipe);
  61. TEST_ASSERT_EQUAL(USB_TRANSFER_STATUS_COMPLETED, urb->transfer.status);
  62. TEST_ASSERT_EQUAL(URB_CONTEXT_VAL, urb->transfer.context);
  63. mock_hid_process_report((mock_hid_mouse_report_t *)urb->transfer.data_buffer, iter_count);
  64. //Requeue URB
  65. if (iter_count > NUM_URBS) {
  66. TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(intr_pipe, urb));
  67. }
  68. }
  69. //Free URB list and pipe
  70. for (int i = 0; i < NUM_URBS; i++) {
  71. test_hcd_free_urb(urb_list[i]);
  72. }
  73. test_hcd_pipe_free(intr_pipe);
  74. test_hcd_pipe_free(default_pipe);
  75. //Clearnup
  76. test_hcd_wait_for_disconn(port_hdl, false);
  77. test_hcd_teardown(port_hdl);
  78. }