Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feature(esp_tinyusb): Added tusb_teardown() call while tinyusb_driver_uninstall() #39

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

roma-jam
Copy link
Collaborator

esp_tinyusb (Not a Release)

Requirements

Description

  • Added tusb_teardown() call while tinyusb_driver_uninstall()
  • Added test case to verify reconfiguration procedure

Changes

  • Added tusb_teardown()
  • Added test case device_class_reconfiguration which:
  1. Configure tinyusb driver with HID class
  2. Install tinyusb driver
  3. Wait connection event (which means the enumeration completed successufully)
  4. Uninstall tinyusb driver
  5. Repeat steps 1-4 for next classes [CDC, MSC]

Notes

  1. Test case only covers the enumeration and configuration for a class. There is no communication in between tinyusb driver tinyusb_driver_install() and tinyusb_driver_uninstall() calls.

Resolve espressif/esp-idf#13788

@roma-jam roma-jam self-assigned this Jun 11, 2024
@@ -69,5 +69,6 @@ esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
esp_err_t tinyusb_driver_uninstall()
{
tinyusb_free_descriptors();
tusb_teardown();
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tore-espressif ,

when you will have time

Is there any good way how I can make this change backward compatible with previous espressif/tinyusb components (previous versions, which doesn't have this implementation)?
Will appreciate any suggestions.

Copy link
Collaborator Author

@roma-jam roma-jam Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here are some ideas, that came to me:

  1. Tie it to the configuration and use idf version to determine the necessity and implementation of tusb_teardown()
  2. Define it somewhere in esp_tinyusb with WEAK attribute. Which let us redefine in with new version and don't panic with old versions, when there is no implementation of it
  3. Enable it with menuconfig (which seems really strange for me, but anyway maybe it is possible).

I like option No.2, but maybe there are other simple and better solution.

};


TEST_CASE("device_class_reconfiguration", "[usb_device]")
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@peter-marcisovsky

After adding the feature from here: espressif/tinyusb#27, we will be able to run esp_tinyusb device tests, because they could be run one by one in a one test launch.

Currently, I have no idea how to add them to our CI and where, but if you have some ideas, please feel free to share.
Thanks.

Copy link
Collaborator

@peter-marcisovsky peter-marcisovsky Jun 11, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To add usb device test, you just need to create a new pytest marker in your pytest*.py file, like so:

@pytest.mark.usb_device

Then, pytest command being run from build_and_run_test_app_usb.yml file, must include this new marker.
Right now, we are using only -m usb_host marker. Thus there will be one more marker -m usb_device
So the new pytest command would look like:
pytest --target=${{ matrix.idf_target }} -m usb_host -m usb_device --build-dir=build_${{ matrix.idf_target }}

This should be all.

@roma-jam
Copy link
Collaborator Author

roma-jam commented Jun 11, 2024

@tore-espressif , @peter-marcisovsky
This changes unlock the possibility to run usb device tests one by one (espressif/tinyusb#27 should be merged first, though).

I posted two questions for both of you, because it seems that we can make it better and we can use it.

I need that for enumeration driver testing (but maybe I will eliminate esp_tinyusb component from the chain, but anyway), so feel free to check it out when possible.

Meanwhile, I will go and cover the Enum Driver as much as I can.

@roma-jam roma-jam force-pushed the feature/esp_tinyusb_stack_teardown branch 3 times, most recently from 8b60d62 to ce33baf Compare June 11, 2024 12:45
@roma-jam roma-jam force-pushed the feature/esp_tinyusb_stack_teardown branch from ce33baf to d7cb17d Compare June 11, 2024 12:48
@roma-jam roma-jam force-pushed the feature/esp_tinyusb_stack_teardown branch from d7cb17d to c14eebb Compare June 11, 2024 12:57
@roma-jam roma-jam marked this pull request as ready for review June 11, 2024 13:14
Copy link
Collaborator

@peter-marcisovsky peter-marcisovsky left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left some observations from using the test app

CONFIG_TINYUSB_CDC_ENABLED=y
CONFIG_TINYUSB_CDC_COUNT=2
CONFIG_TINYUSB_HID_COUNT=0
CONFIG_TINYUSB_HID_COUNT=1
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After enabling HID in the menuconfig, the
tinyusb_set_descriptors() would fail
because of
// Default configuration descriptor is provided only for CDC, MSC and NCM classes
ESP_GOTO_ON_FALSE(config->configuration_descriptor, ESP_ERR_INVALID_ARG, fail, TAG, "Configuration descriptor must be provided for this device");

Thus making all the descriptors test cases failing.

@@ -66,8 +78,18 @@ esp_err_t tinyusb_driver_install(const tinyusb_config_t *config)
return ESP_OK;
}

esp_err_t tinyusb_driver_uninstall()
esp_err_t tinyusb_driver_uninstall(void)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we call this function from tearDown()?
Once a test case fails, the PHY does not get released, thus all the following test cases would fail because of the:

E (23549) usb_phy: usb_new_phy(296): selected PHY is in use
E (23559) TinyUSB: tinyusb_driver_install(65): Install USB PHY failed

if (!tusb_teardown()) {
return ESP_ERR_NOT_FINISHED;
}

Copy link
Collaborator

@leeebo leeebo Jun 21, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roma-jam Should we restore the USB to the default function (eg. USB-Serial-JTAG) at the end of uninstall?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@leeebo I don't think that would be a good idea, as for user it is transparent in that case.
Even, IDK, whether we have such feature before installing tinyUSB driver?

Just in case: do we have such call (maybe in rom) to enable this feature? Or you thinking about configure the PHY during teardown in the way, to enable USB-Serial-JTAG?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The USB-Serial-JTAG is a hardware circuit that can respond to the host's requests and is enabled by default. Users can use the USB-Serial-JTAG for USB download without any code in APP.

But after tinyUSB install then uninstalled, the USB phy not be switched back to USB-Serial-JTAG

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@roma-jam @leeebo as a user of the library in esp32s3 I think teardown/uninstall should not auto-reinstall jtag but there should be a convenience function that allows to restore it, if necessary it could be called automatically and be driven by Kconfig to provide best defaults. In my use case i want to be able to switch from usb cdc guest to msc usb host and viceversa and that is currently not possible without a device restart

@finger563
Copy link

Is there a timeline for this feature to be finalized?

@roma-jam
Copy link
Collaborator Author

Hi @finger563 ,

This feature allows the re-configure the driver with these changes: espressif/tinyusb#27
But as far as the same changes are already in the upstream, so we will rebase these changes after updating version to v0.17.0.
For current feature there is no available timeline for now, but the one thing for sure: the update to v0.17.0 will be done first.

Meanwhile, this feature should be available in the upstream of TinyUSB, if anything it is possible to use it.

Sorry for the inconvenience.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

esp32s3, restarting cdc software serial doesn't work (IDFGH-12813)
5 participants