test_usb_host_async.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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/task.h"
  9. #include "freertos/semphr.h"
  10. #include "esp_intr_alloc.h"
  11. #include "test_usb_mock_classes.h"
  12. #include "msc_client.h"
  13. #include "ctrl_client.h"
  14. #include "usb/usb_host.h"
  15. #include "unity.h"
  16. #include "test_utils.h"
  17. #define TEST_MSC_NUM_SECTORS_TOTAL 10
  18. #define TEST_MSC_NUM_SECTORS_PER_XFER 2
  19. #define TEST_MSC_SCSI_TAG 0xDEADBEEF
  20. #define TEST_CTRL_NUM_TRANSFERS 30
  21. // --------------------------------------------------- Test Cases ------------------------------------------------------
  22. /*
  23. Test USB Host Asynchronous API single client
  24. Requires: This test requires an MSC SCSI device to be attached (see the MSC mock class)
  25. Purpose:
  26. - Test that USB Host Asynchronous API works correctly with a single client
  27. - Test that a client can be created
  28. - Test that client can operate concurrently in a separate thread
  29. - Test that the main thread is able to detect library events (such as USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS)
  30. Procedure:
  31. - Install USB Host Library
  32. - Create a task to run an MSC client
  33. - Start the MSC client task. It will execute a bunch of MSC SCSI sector reads
  34. - Wait for the host library event handler to report a USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS event
  35. - Free all devices
  36. - Uninstall USB Host Library
  37. */
  38. TEST_CASE("Test USB Host async (single client)", "[usb_host][ignore]")
  39. {
  40. //Install USB Host
  41. usb_host_config_t host_config = {
  42. .intr_flags = ESP_INTR_FLAG_LEVEL1,
  43. };
  44. ESP_ERROR_CHECK(usb_host_install(&host_config));
  45. printf("Installed\n");
  46. //Create task to run client that communicates with MSC SCSI interface
  47. msc_client_test_param_t params = {
  48. .num_sectors_to_read = TEST_MSC_NUM_SECTORS_TOTAL,
  49. .num_sectors_per_xfer = TEST_MSC_NUM_SECTORS_PER_XFER,
  50. .msc_scsi_xfer_tag = TEST_MSC_SCSI_TAG,
  51. .idVendor = MOCK_MSC_SCSI_DEV_ID_VENDOR,
  52. .idProduct = MOCK_MSC_SCSI_DEV_ID_PRODUCT,
  53. };
  54. TaskHandle_t task_hdl;
  55. xTaskCreatePinnedToCore(msc_client_async_seq_task, "async", 4096, (void *)&params, 2, &task_hdl, 0);
  56. //Start the task
  57. xTaskNotifyGive(task_hdl);
  58. while (1) {
  59. //Start handling system events
  60. uint32_t event_flags;
  61. usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
  62. if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
  63. printf("No more clients\n");
  64. TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_free_all());
  65. }
  66. if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
  67. break;
  68. }
  69. }
  70. //Short delay to allow task to be cleaned up
  71. vTaskDelay(10);
  72. //Clean up USB Host
  73. ESP_ERROR_CHECK(usb_host_uninstall());
  74. }
  75. /*
  76. Test USB Host Asynchronous API with multiple clients
  77. Requires: This test requires an MSC SCSI device to be attached (see the MSC mock class)
  78. Purpose:
  79. - Test the USB Host Asynchronous API works correctly with multiple clients
  80. - Test that multiple clients can be created
  81. - Test that multiple clients can operate concurrently in separate threads
  82. - Test that the main thread is able to detect library events (such as USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS)
  83. Procedure:
  84. - Install USB Host Library
  85. - Create separate tasks to run an MSC client and Ctrl Client
  86. - MSC Client will execute a bunch of MSC SCSI sector reads
  87. - Ctrl Client will execute a bunch of control transfers
  88. - Wait for the host library event handler to report a USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS event
  89. - Free all devices
  90. - Uninstall USB Host Library
  91. */
  92. TEST_CASE("Test USB Host async (multi client)", "[usb_host][ignore]")
  93. {
  94. //Install USB Host
  95. usb_host_config_t host_config = {
  96. .intr_flags = ESP_INTR_FLAG_LEVEL1,
  97. };
  98. ESP_ERROR_CHECK(usb_host_install(&host_config));
  99. printf("Installed\n");
  100. //Create task to run the MSC client
  101. msc_client_test_param_t msc_params = {
  102. .num_sectors_to_read = TEST_MSC_NUM_SECTORS_TOTAL,
  103. .num_sectors_per_xfer = TEST_MSC_NUM_SECTORS_PER_XFER,
  104. .msc_scsi_xfer_tag = TEST_MSC_SCSI_TAG,
  105. .idVendor = MOCK_MSC_SCSI_DEV_ID_VENDOR,
  106. .idProduct = MOCK_MSC_SCSI_DEV_ID_PRODUCT,
  107. };
  108. TaskHandle_t msc_task_hdl;
  109. xTaskCreatePinnedToCore(msc_client_async_seq_task, "msc", 4096, (void *)&msc_params, 2, &msc_task_hdl, 0);
  110. //Create task a control transfer client
  111. ctrl_client_test_param_t ctrl_params = {
  112. .num_ctrl_xfer_to_send = TEST_CTRL_NUM_TRANSFERS,
  113. .idVendor = MOCK_MSC_SCSI_DEV_ID_VENDOR,
  114. .idProduct = MOCK_MSC_SCSI_DEV_ID_PRODUCT,
  115. };
  116. TaskHandle_t ctrl_task_hdl;
  117. xTaskCreatePinnedToCore(ctrl_client_async_seq_task, "ctrl", 4096, (void *)&ctrl_params, 2, &ctrl_task_hdl, 0);
  118. //Start both tasks
  119. xTaskNotifyGive(msc_task_hdl);
  120. xTaskNotifyGive(ctrl_task_hdl);
  121. while (1) {
  122. //Start handling system events
  123. uint32_t event_flags;
  124. usb_host_lib_handle_events(portMAX_DELAY, &event_flags);
  125. if (event_flags & USB_HOST_LIB_EVENT_FLAGS_NO_CLIENTS) {
  126. printf("No more clients\n");
  127. TEST_ASSERT_EQUAL(ESP_OK, usb_host_device_free_all());
  128. }
  129. if (event_flags & USB_HOST_LIB_EVENT_FLAGS_ALL_FREE) {
  130. break;
  131. }
  132. }
  133. //Short delay to allow task to be cleaned up
  134. vTaskDelay(10);
  135. //Clean up USB Host
  136. ESP_ERROR_CHECK(usb_host_uninstall());
  137. }