test_hcd_intr.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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_usb_mock_msc.h"
  11. #include "test_usb_mock_hid.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 MOCK_HID_MOUSE_INTR_IN_MPS
  34. #define NUM_URB_ITERS (NUM_URBS * 100)
  35. TEST_CASE("Test HCD interrupt pipe URBs", "[intr][low_speed]")
  36. {
  37. usb_speed_t port_speed = test_hcd_wait_for_conn(port_hdl); //Trigger a connection
  38. TEST_ASSERT_EQUAL_MESSAGE(TEST_HID_DEV_SPEED, port_speed, "Connected device is not Low Speed!");
  39. vTaskDelay(pdMS_TO_TICKS(100)); //Short delay send of SOF (for FS) or EOPs (for LS)
  40. 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)
  41. uint8_t dev_addr = test_hcd_enum_device(default_pipe);
  42. //Allocate interrupt pipe and URBS
  43. hcd_pipe_handle_t intr_pipe = test_hcd_pipe_alloc(port_hdl, &mock_hid_mouse_in_ep_desc, dev_addr, port_speed);
  44. urb_t *urb_list[NUM_URBS];
  45. for (int i = 0; i < NUM_URBS; i++) {
  46. urb_list[i] = test_hcd_alloc_urb(0, URB_DATA_BUFF_SIZE);
  47. urb_list[i]->transfer.num_bytes = URB_DATA_BUFF_SIZE;
  48. urb_list[i]->transfer.context = URB_CONTEXT_VAL;
  49. }
  50. //Enqueue URBs
  51. for (int i = 0; i < NUM_URBS; i++) {
  52. TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(intr_pipe, urb_list[i]));
  53. }
  54. int iter_count = NUM_URB_ITERS;
  55. for (iter_count = NUM_URB_ITERS; iter_count > 0; iter_count--) {
  56. //Wait for an URB to be done
  57. test_hcd_expect_pipe_event(intr_pipe, HCD_PIPE_EVENT_URB_DONE);
  58. //Dequeue the URB and check results
  59. urb_t *urb = hcd_urb_dequeue(intr_pipe);
  60. TEST_ASSERT_EQUAL_MESSAGE(USB_TRANSFER_STATUS_COMPLETED, urb->transfer.status, "Transfer NOT completed");
  61. TEST_ASSERT_EQUAL(URB_CONTEXT_VAL, urb->transfer.context);
  62. mock_hid_process_report((mock_hid_mouse_report_t *)urb->transfer.data_buffer, iter_count);
  63. //Requeue URB
  64. if (iter_count > NUM_URBS) {
  65. TEST_ASSERT_EQUAL(ESP_OK, hcd_urb_enqueue(intr_pipe, urb));
  66. }
  67. }
  68. //Free URB list and pipe
  69. for (int i = 0; i < NUM_URBS; i++) {
  70. test_hcd_free_urb(urb_list[i]);
  71. }
  72. test_hcd_pipe_free(intr_pipe);
  73. test_hcd_pipe_free(default_pipe);
  74. //Clearnup
  75. test_hcd_wait_for_disconn(port_hdl, false);
  76. }