Skip to content

Replace both ESP tinyusb component and ASF4 USB stack with TinyUSB 0.20.0 submodule#371

Draft
SukuWc wants to merge 32 commits into
mainfrom
SUKU-usb-d51
Draft

Replace both ESP tinyusb component and ASF4 USB stack with TinyUSB 0.20.0 submodule#371
SukuWc wants to merge 32 commits into
mainfrom
SUKU-usb-d51

Conversation

@SukuWc
Copy link
Copy Markdown
Member

@SukuWc SukuWc commented May 24, 2026

Changes

  • ESP tinyusb managed component replaced with tinyusb 0.20.0 as a submodule in common/dep/tinyusb
  • ASF4 USB device stack replaced with tinyusb 0.20.0 as a submodule in common/dep/tinyusb
  • USB class drivers in grid_usb.c separated into class specific files
    • grid_usb_midi.c
    • grid_usb_acm.c
    • grid_usb_hid.c
  • unified connected/disconnected state
  • Proper HIDKEYBOARD frame termination added for correct decoding
  • Flow-control implemented for all USB classes with proper back-pressure for RX mechanizms
  • TX buffer overflow is not accumulated in per-class counters and reported periodically

SukuWc and others added 9 commits May 23, 2026 20:57
Split the monolithic grid_esp32_usb.c into separate files by USB class:
- grid_esp32_usb_acm.c/h  CDC-ACM serial, migrated from tinyusb_cdcacm
                           high-level API to native tud_cdc_* callbacks
- grid_esp32_usb_hid.c/h  HID (keyboard, mouse, gamepad)
- grid_esp32_usb_midi.c/h MIDI RX/TX

grid_esp32_usb.c now contains only USB descriptors and init.
grid_esp32_usb.h is an umbrella header that includes all subsystems.

The ACM migration to native callbacks removes the esp_tinyusb cdcacm
layer while keeping identical behaviour: tud_cdc_line_state_cb sets
usb_tx_ready on DTR, tud_cdc_tx_complete_cb clears the busy flag.

Also removes the stale #include "tinyusb_cdc_acm.h" from grid_esp32s3.c.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Pin upstream hathach/tinyusb at tag 0.19.0 (331c263) as a git submodule.
Update common/.gitignore allowlist to include dep/tinyusb.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
…B submodule

Use hathach/tinyusb 0.19.0 (common/dep/tinyusb submodule) directly instead
of the espressif/esp_tinyusb managed component.

Changes:
- Add esp32s3/components/tinyusb/ local component:
  - CMakeLists.txt: compiles TinyUSB source files from the submodule,
    sets CFG_TUSB_MCU via compile flag for ESP32-S2/S3
  - include/tusb_config.h: hand-written config with CDC=1, MIDI=1, HID=1,
    slave/IRQ mode (no DMA), FREERTOS OS abstraction

- Rewrite grid_esp32_usb.c:
  - Drop tinyusb_driver_install() / TINYUSB_DEFAULT_CONFIG()
  - Add static tusb_desc_device_t with IAD class (CDC present)
  - Add tud_descriptor_device_cb(), tud_descriptor_configuration_cb(),
    tud_descriptor_string_cb() (TinyUSB descriptor callbacks)
  - Replace driver install with usb_new_phy() + FreeRTOS task calling
    tusb_rhport_init() + tud_task()

- Replace #include "tinyusb.h" with #include "tusb.h" everywhere:
  grid_esp32_usb_acm.c, _hid.c, _hid.h, _midi.c,
  grid_esp32_port.c, main/grid_esp32s3.c

- Update CMakeLists REQUIRES:
  - grid_esp32_usb: esp_tinyusb -> tinyusb + esp_hw_support
  - grid_esp32_port: esp_tinyusb -> tinyusb

- Remove espressif/esp_tinyusb from main/idf_component.yml

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
common/dep/tinyusb is now a submodule; add submodules: recursive to the
checkout step so the TinyUSB source is available during the build.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Synopsys (the IP vendor behind the DWC2 USB core) would otherwise be
flagged as a misspelling of synopsis.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Replace the Atmel START / ASF4 composite USB stack on the SAMD51
firmware with the same upstream TinyUSB submodule already used by
the ESP32-S3 target (common/dep/tinyusb, pinned to tag 0.19.0).

Key changes:
- d51n20a/grid/d51/tusb_config.h (new): TinyUSB config for SAMD51
    OPT_OS_NONE + OPT_MCU_SAMD51, CDC/MIDI/HID enabled.
    CFG_TUSB_RHPORT0_MODE = full-speed device.
- d51n20a/gcc/Makefile: swap ASF4 USB sources for TinyUSB sources
    (tusb.c, usbd.c, cdc/midi/hid class drivers, dcd_samd.c).
    Remove old ASF4 USB include paths; add common/dep/tinyusb/src.
- d51n20a/grid/d51/grid_d51_usb.h: strip ASF4 includes; expose
    TinyUSB-based platform API.
- d51n20a/grid/d51/grid_d51_usb.c: full rewrite — USB descriptors,
    SAMD51 IRQ handlers (USB_0–3_Handler → dcd_int_handler),
    tud_cdc_rx/tx callbacks, MIDI via tud_midi_packet_read,
    HID via tud_hid_keyboard/mouse_report.
    Stub usb_d_init() so driver_init.c USB clock/pin-mux init
    still runs; TinyUSB takes over the peripheral in tusb_init().
- d51n20a/atmel_start.h: remove #include "usb_start.h" (ASF4 USB).
- d51n20a/atmel_start.c: remove usb_init() call (now handled by
    grid_d51_usb_init() via tusb_init()).
- d51n20a/grid_d51n20a.c: tud_task() in main loop; tud_mounted()
    replaces usb_d_get_frame_num(); grid_d51_midi_rx_poll() replaces
    grid_d51_midi_bulkout_poll().

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
Comment thread d51n20a/atmel_start.c
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just remove the comment, not need to keep it for documentation

Comment thread d51n20a/atmel_start.h
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Just remove the comment, not need to keep it for documentation

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Remove all the added comments

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Separate to 3 files based on the interfaces similar to how it is done for the ESP32S3 firmware variant: CDC, MIDI, HID

SukuWc and others added 2 commits May 25, 2026 09:17
- grid_d51_usb.c now only contains descriptors, IRQ handlers, and init
- grid_d51_usb_acm.{c,h}: CDC/ACM platform API and TinyUSB callbacks
- grid_d51_usb_midi.{c,h}: MIDI platform API and TinyUSB callbacks
- grid_d51_usb_hid.{c,h}: HID platform API, TinyUSB callbacks, websocket stubs
- Remove explanatory comments from atmel_start.c and atmel_start.h
- Matches ESP32-S3 per-interface file structure

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
@SukuWc SukuWc marked this pull request as draft May 26, 2026 11:11
@SukuWc SukuWc changed the base branch from SUKU-usb-refactor to main June 2, 2026 07:37
@SukuWc SukuWc changed the title d51: replace ASF4 USB stack with TinyUSB 0.19.0 Replace both ESP tinyusb component and ASF4 USB stack with TinyUSB 0.20.0 submodule Jun 2, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Status: Todo

Development

Successfully merging this pull request may close these issues.

1 participant