Add support for WCH ch582/583 series#3515
Conversation
There was a problem hiding this comment.
Pull request overview
Adds TinyUSB support for WCH CH58x (CH582/CH583) by introducing a new USBFS portable layer (device + host) and a new BSP/board, and wiring the new family into dependency fetching + CI.
Changes:
- Added CH58x MCU options (with CH582/CH583 aliases) and hooked CH58x into
tusb_mcu.hconfiguration defaults. - Introduced CH58x USBFS register definitions plus new DCD/HCD implementations.
- Added
hw/bsp/ch58x(build scripts, linker script, OpenOCD config, UART debug, interrupts) and documented/registered the newyd-ch582mboard in docs + CI matrix.
Reviewed changes
Copilot reviewed 28 out of 28 changed files in this pull request and generated 11 comments.
Show a summary per file
| File | Description |
|---|---|
| tools/get_deps.py | Fetches the WCH CH58x SDK repository into hw/mcu/wch/ch58x. |
| src/tusb_option.h | Adds OPT_MCU_CH58X and CH582/CH583 aliases. |
| src/portable/wch/hcd_ch58x_usbfs.c | New CH58x USBFS host controller driver implementation. |
| src/portable/wch/dcd_ch58x_usbfs.c | New CH58x USBFS device controller driver implementation. |
| src/portable/wch/ch58x_usbfs_reg.h | CH58x USBFS register map and bit definitions used by DCD/HCD. |
| src/common/tusb_mcu.h | Integrates CH58x with TinyUSB’s MCU selection/config defaults (DCD/HCD enable flags, endpoint max). |
| hw/bsp/ch58x/wch-riscv.cfg | OpenOCD config for WCHLink(“wlinke”) on CH58x. |
| hw/bsp/ch58x/system_ch58x.h | Declares SystemInit/SystemCoreClockUpdate for BSP. |
| hw/bsp/ch58x/system_ch58x.c | Implements basic clock init/update using the WCH SDK. |
| hw/bsp/ch58x/linker/ch582.ld | Linker script for CH582-style memory layout with size overrides. |
| hw/bsp/ch58x/family.mk | Make-based build integration for CH58x (SDK paths, sources, flags). |
| hw/bsp/ch58x/family.cmake | CMake-based build integration for CH58x (SDK sources, flags, linker defs). |
| hw/bsp/ch58x/family.c | BSP glue: IRQ forwarding to TinyUSB, board init, UART/LED/button, DEBUG guard. |
| hw/bsp/ch58x/debug_uart.h | UART debug API used by the BSP for printf output. |
| hw/bsp/ch58x/debug_uart.c | UART1 ring-buffer TX implementation used by board UART write. |
| hw/bsp/ch58x/ch58x_it.h | Declares exception/IRQ handlers for CH58x BSP. |
| hw/bsp/ch58x/ch58x_it.c | Implements NMI/HardFault default handlers. |
| hw/bsp/ch58x/boards/yd-ch582m/board.mk | Board-specific linker size defs for Make builds. |
| hw/bsp/ch58x/boards/yd-ch582m/board.h | Board pinout + rhport role mapping for the yd-ch582m board. |
| hw/bsp/ch58x/boards/yd-ch582m/board.cmake | Board-specific linker size defs for CMake builds. |
| examples/device/video_capture_2ch/skip.txt | Attempts to skip example for CH58x family in CI. |
| examples/device/midi_test_freertos/skip.txt | Attempts to skip example for CH58x family in CI. |
| examples/device/hid_composite_freertos/skip.txt | Attempts to skip example for CH58x family in CI. |
| examples/device/cdc_msc_freertos/skip.txt | Attempts to skip example for CH58x family in CI. |
| examples/device/audio_test_freertos/skip.txt | Attempts to skip example for CH58x family in CI. |
| examples/device/audio_4_channel_mic_freertos/skip.txt | Attempts to skip example for CH58x family in CI. |
| docs/reference/boards.rst | Adds yd-ch582m to the boards reference table. |
| .github/workflows/ci_set_matrix.py | Enables ch58x family in CI toolchain matrix. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
4bb608e to
bcb4795
Compare
|
cdc_msc broken, more fix needed... |
|
|
||
| #include "tusb_option.h" | ||
|
|
||
| #if CFG_TUD_ENABLED && defined(TUP_USBIP_WCH_CH58X) && CFG_TUD_WCH_USBIP_USBFS |
There was a problem hiding this comment.
does the chip use the new usbip or same usbfs as other wch. If it is the same, the existing drivers should be update instead of adding new files
There was a problem hiding this comment.
No, though they are alike in core but sdks are totally different, so it's better to keep drivers sepreted
There was a problem hiding this comment.
what is the different in the dcd driver ?
There was a problem hiding this comment.
If only sdk is different and the dcd driver is pretty much the same. Then We should use the same .c file (We can't have same .c for every families). Here is how we got dwc2 working for multiple mcu vendors with totally different sdks for reference https://github.com/hathach/tinyusb/tree/master/src/portable/synopsys/dwc2
There was a problem hiding this comment.
You're right, I reviewed both drivers side by side and the logic is essentially the same, differences are all at the register access level. I'll refactor into a shared file later.
There was a problem hiding this comment.
Sorry, this is harder than I expected. ch32 and ch58x have quite different usb register layouts (split/merged ep control, dma width, interrupt controller), so merging them isn't as straightforward as dwc2. I tried the port-header approach but still ended up with about 20 #if blocks.
The ch58x driver follows its sdk conventions, which are pretty different from ch32's (ch32 sdk mimics stm32 style, while ch58x sdk is more simplified). A full rewrite could help, but given how different these chip families are, keeping separate usb IPs seems more practical for maintenance(actually wch have many differences from each chips, makes it hard to maintance)
There was a problem hiding this comment.
there is indeed some difference, let keep this open, I will try to take a look when possible. I still prefer to keep it under 1 driver to make it easier to maintain the driver.
|
Tested some demos and runs well, cdc_msc bugs also fixed. |
- Switch from AUTO_TOG to manual toggle for all endpoints to fix toggle mismatch after clear-stall causing bus resets - Fix SETUP race condition: track ep0_completion_pending and setup_pending to avoid arming stale EP0 transfers - Defer Set Address to ISR after status ZLP is ACK'd - Improve bus reset: reset all EPs, clear pending state - Fix EP4 DMA macro to correctly map to EP0 base (0x10) - Move CH58X_UIS_TOG_OK to common section (used by both DCD/HCD) - Add #ifndef guards for BOARD_TUD/TUH_RHPORT in board.h - Track isochronous per direction: isochronous[ep][dir] - NAK EP0 OUT on completion to prevent premature data acceptance
This PR adds support for the WCH CH58x series (CH582, CH583), device, host and dual role demo have been tested and confirmed working on real hardware, but there are still some things that need to be discussed/improved:
CH581: It shares the same SDK, but I have only one USB device and different flash size, and it isn't on the market yet, So there is no support for it.
CH584/CH585: Despite the similar name, their SDK is in a separate
repository with a different register layout. I haven't looked into whether the USB IP is compatible.
So I made a change like this:
OPT_MCU_CH58X/CH582/CH583: All three aliases intentionally map to the same value,leaving room for per-variant differentiation down the road without breaking existing code.
_writesymbol conflict: WCH's SDK has a_writemacro ,will be compiled error under WCH's MounRiver Studio. I added a#errorto make thisobvious at compile time instead of leaving users with a cryptic linker error. Although this is not a perfect solution, there is no need to make too many changes to the source code and SDK.