Skip to content

Feat/tinyusb esp32h4 support#3244

Closed
igi540 wants to merge 39 commits into
hathach:masterfrom
espressif:feat/tinyusb_esp32h4_support
Closed

Feat/tinyusb esp32h4 support#3244
igi540 wants to merge 39 commits into
hathach:masterfrom
espressif:feat/tinyusb_esp32h4_support

Conversation

@igi540
Copy link
Copy Markdown
Contributor

@igi540 igi540 commented Sep 10, 2025

Describe the PR
A clear and concise description of what this PR solve.

Additional context
If applicable, add any other context about the PR and/or screenshots here.

roma-jam and others added 30 commits November 25, 2024 12:11
…ounter

fix(usbd/dcd_dwc2): Fixed epin counter assert, update buffer name for usbd_control debug
…_build_check

feature(ci): Added esp-iot-solution USB Device examples build verification
…st_apps

ci(tinyusb): Added esp-usb test_apps build and run (esp32s2)
…_class

feature(uvc): Applied proprietary changes in UVC Device class
feature(tusb): Added tusb_teardown()
ci(tinyusb): Added esp-idf run (esp32s2)
* fixed unit test with ceedling 1.0.0
roma-jam and others added 9 commits February 11, 2025 19:21
ci(workflow): Update ubuntu runners to ubuntu-latest
TinyUSB does not provide any locking means to protect the DCD variables.
This can lead to race conditions when the user is trying to submit
a transfer while the device is being disconnected. This can cause
the device to be in an inconsistent state, leading to a crash or
undefined behavior.

This commit adds a spin-lock to protect the DCD variables during
device disconnect.

Closes espressif/esp-idf#9691
Also reported in espressif/esp-usb#131
fix(dcd): Fixed race condition on device disconnect
Add `OPT_MCU_ESP32H4` and integrate it into TinyUSB build and
runtime logic. Updates include:

- CMakeLists: add esp32h4 target mapping to `OPT_MCU_ESP32H4`
- tusb_option.h: define `OPT_MCU_ESP32H4`
- tusb_mcu.h, dwc2_esp32.h: extend DWC2 USBIP support
- dcd_esp32sx.c: enable DCD for ESP32-H4
- family.c: include ESP32-H4 in USB init/PHY setup
- idf_component.yml: add esp32h4 to supported targets

This enables TinyUSB examples to build for ESP32-H4 using the
ESP32-Sx USB backend as a base.
Copilot AI review requested due to automatic review settings September 10, 2025 16:30
@igi540 igi540 closed this Sep 10, 2025
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the ESP32-H4 microcontroller to TinyUSB, enabling USB functionality on this new Espressif chip. The changes extend the existing ESP32 family support by adding H4-specific configurations and drivers.

Key changes:

  • Add ESP32-H4 MCU definition and configuration in TinyUSB option files
  • Extend DWC2 controller support for H4's specific register mapping and interrupt sources
  • Update component files to include H4 in build targets and workflows

Reviewed Changes

Copilot reviewed 25 out of 25 changed files in this pull request and generated 3 comments.

Show a summary per file
File Description
src/tusb_option.h Defines OPT_MCU_ESP32H4 constant for H4 MCU identification
src/tusb.h Adds teardown functionality with backward compatibility macros
src/tusb.c Implements tusb_rhport_teardown() function for proper cleanup
src/portable/synopsys/dwc2/dwc2_esp32.h Adds H4-specific DWC2 controller configuration
src/portable/synopsys/dwc2/dwc2_critical.h New critical section macros for thread safety
src/portable/synopsys/dwc2/dcd_dwc2.c Enhanced DWC2 driver with H4 field mappings and critical sections
src/portable/espressif/esp32sx/dcd_esp32sx.c Extends ESP32SX driver to support H4
src/common/tusb_mcu.h Adds H4 to ESP32 DWC2 MCU definitions
src/class/video/video_device.c Fixes uninitialized variable warnings
src/class/audio/audio_device.c Updates DWC2 MCU checks to include H4
Multiple config/workflow files Build system and CI integration for H4 support

Comment on lines +625 to +626
uint_fast32_t interval = 0;
uint_fast32_t interval_ms = 0;
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

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

Initialize variables at declaration to prevent potential use of uninitialized values. While these variables are assigned in all switch cases, the initialization provides better safety and clarity.

Copilot uses AI. Check for mistakes.
Comment on lines +602 to +605
if (xfer->max_size == 0) {
DCD_EXIT_CRITICAL();
return false; // Endpoint is closed
}
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

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

The check for endpoint closure (xfer->max_size == 0) should be documented more clearly. Consider adding a comment explaining when and why max_size would be zero, as this is not immediately obvious from the context.

Copilot uses AI. Check for mistakes.

// Clear A override, force B Valid
dwc2->gotgctl = (dwc2->gotgctl & ~GOTGCTL_AVALOEN) | GOTGCTL_BVALOEN | GOTGCTL_BVALOVAL;
// No overrides
Copy link

Copilot AI Sep 10, 2025

Choose a reason for hiding this comment

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

The change from setting specific override bits to clearing all override bits is a significant behavioral change. This should be documented explaining why "No overrides" is the correct approach for H4, as it differs from the previous implementation.

Suggested change
// No overrides
/*
* Clear all override bits in GOTGCTL ("No overrides").
*
* For ESP32-H4 (and similar MCUs), the USB controller's override bits (BVALOEN, BVALOVAL, VBVALOVAL)
* should be cleared during device initialization. Setting these bits can interfere with the controller's
* normal operation and may cause incorrect device detection or enumeration failures.
*
* Previous implementations may have set specific override bits to force certain line states or behaviors.
* However, for H4, the hardware expects no overrides to be active, allowing the controller to manage
* line states automatically. This change is based on the ESP32-H4 Technical Reference Manual and
* observed behavior during device bring-up.
*
* If porting to other MCUs, verify their requirements for GOTGCTL overrides, as this may differ.
*/

Copilot uses AI. Check for mistakes.
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.

7 participants