summaryrefslogtreecommitdiff
path: root/hw
diff options
context:
space:
mode:
authorhathach <[email protected]>2026-06-18 17:59:30 +0700
committerhathach <[email protected]>2026-06-18 17:59:30 +0700
commitea5b8d677f22f4bd1574f864fd0487dfd329c090 (patch)
treec73641e9dee6992bdff0f12edf471427fed840a2 /hw
parent953abfb3931c67b834838c7fe0b31834cb0d42d6 (diff)
dcd/ch58x: drive CH582/583 with shared dcd_ch32_usbfs.c
Replace PR #3515's separate dcd_ch58x_usbfs.c / hcd_ch58x_usbfs.c with the shared WCH USBFS device driver (combined per-endpoint control, like CH32V103), adding two CH58x-specific behaviors guarded so CH32V103/V20x/V307 are unchanged: - CH32_USBFS_EP_MANUAL_TOG: CH58x's hardware AUTO_TOG does not stay in sync, so the ISR toggles DATA0/DATA1 manually and discards toggle-mismatched OUT packets. Fixes multi-packet bulk-IN (e.g. MSC READ10) that otherwise hung. - CH32_USBFS_EP4_SHARES_EP0: EP4 has no DMA register and overlays EP0's region (EP0[0:63] + EP4 OUT[64:127] + EP4 IN[128:191]); add a 192-byte shared buffer and buffer-pointer helpers (transparent for the other parts). Fixes cdc_dual_ports (Port1 is on EP4). Add the ch582m_evt board. Device only on USB0 (rhport 0): the shared hcd_ch32_usbfs.c is CH32V20x-specific and cannot drive CH58x, so host / USB2 (rhport 1) is left commented out in the BSP for easy re-add. Verified on ch582m_evt via local HIL: all device examples pass. Co-Authored-By: Claude Opus 4.8 (1M context) <[email protected]>
Diffstat (limited to 'hw')
-rw-r--r--hw/bsp/ch58x/boards/ch582m_evt/board.cmake5
-rw-r--r--hw/bsp/ch58x/boards/ch582m_evt/board.h61
-rw-r--r--hw/bsp/ch58x/boards/ch582m_evt/board.mk3
-rw-r--r--hw/bsp/ch58x/boards/yd-ch582m/board.h10
-rw-r--r--hw/bsp/ch58x/ch58x_it.h2
-rw-r--r--hw/bsp/ch58x/family.c27
-rw-r--r--hw/bsp/ch58x/family.cmake3
-rw-r--r--hw/bsp/ch58x/family.mk3
8 files changed, 92 insertions, 22 deletions
diff --git a/hw/bsp/ch58x/boards/ch582m_evt/board.cmake b/hw/bsp/ch58x/boards/ch582m_evt/board.cmake
new file mode 100644
index 000000000..4129c4550
--- /dev/null
+++ b/hw/bsp/ch58x/boards/ch582m_evt/board.cmake
@@ -0,0 +1,5 @@
+set(LD_FLASH_SIZE 448K)
+set(LD_RAM_SIZE 32K)
+
+function(update_board TARGET)
+endfunction()
diff --git a/hw/bsp/ch58x/boards/ch582m_evt/board.h b/hw/bsp/ch58x/boards/ch582m_evt/board.h
new file mode 100644
index 000000000..c3483bf17
--- /dev/null
+++ b/hw/bsp/ch58x/boards/ch582m_evt/board.h
@@ -0,0 +1,61 @@
+/*
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2024 TinyUSB contributors
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+/* metadata:
+ name: CH582M-EVT evaluation board
+ url: https://www.wch-ic.com/products/CH582.html
+*/
+
+#ifndef BOARD_H_
+#define BOARD_H_
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+// LED: PB4 on CH582M-EVT
+#define LED_PIN GPIO_Pin_4
+#define LED_STATE_ON 0
+
+// Directly reuse BOOT pin as user button
+#define BUTTON_PIN GPIO_Pin_22
+#define BUTTON_STATE_ACTIVE 0
+
+// UART: UART1 TX=PA9, RX=PA8
+#define CFG_BOARD_UART_BAUDRATE 115200
+
+// Device only on USB1 (rhport 0). CH58x host / USB2 is not supported by the shared usbfs dcd;
+// BOARD_TUH_RHPORT is kept commented out to ease re-adding host later.
+#ifndef BOARD_TUD_RHPORT
+#define BOARD_TUD_RHPORT 0
+#endif
+// #ifndef BOARD_TUH_RHPORT
+// #define BOARD_TUH_RHPORT 1
+// #endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif
diff --git a/hw/bsp/ch58x/boards/ch582m_evt/board.mk b/hw/bsp/ch58x/boards/ch582m_evt/board.mk
new file mode 100644
index 000000000..a13979799
--- /dev/null
+++ b/hw/bsp/ch58x/boards/ch582m_evt/board.mk
@@ -0,0 +1,3 @@
+LDFLAGS += \
+ -Wl,--defsym=__FLASH_SIZE=448K \
+ -Wl,--defsym=__RAM_SIZE=32K \
diff --git a/hw/bsp/ch58x/boards/yd-ch582m/board.h b/hw/bsp/ch58x/boards/yd-ch582m/board.h
index a14ffd198..0da5747bc 100644
--- a/hw/bsp/ch58x/boards/yd-ch582m/board.h
+++ b/hw/bsp/ch58x/boards/yd-ch582m/board.h
@@ -45,14 +45,14 @@ extern "C" {
// UART: UART1 TX=PA9, RX=PA8
#define CFG_BOARD_UART_BAUDRATE 115200
-// Dual-port: USB1 (rhport 0) = Device, USB2 (rhport 1) = Host
-// Swap these two if you want the opposite assignment
+// Device only on USB1 (rhport 0). CH58x host / USB2 is not supported by the shared usbfs dcd;
+// BOARD_TUH_RHPORT is kept commented out to ease re-adding host later.
#ifndef BOARD_TUD_RHPORT
#define BOARD_TUD_RHPORT 0
#endif
-#ifndef BOARD_TUH_RHPORT
-#define BOARD_TUH_RHPORT 1
-#endif
+// #ifndef BOARD_TUH_RHPORT
+// #define BOARD_TUH_RHPORT 1
+// #endif
#ifdef __cplusplus
}
diff --git a/hw/bsp/ch58x/ch58x_it.h b/hw/bsp/ch58x/ch58x_it.h
index 18ea52bc9..3e050344d 100644
--- a/hw/bsp/ch58x/ch58x_it.h
+++ b/hw/bsp/ch58x/ch58x_it.h
@@ -36,7 +36,7 @@ extern "C" {
void NMI_Handler(void);
void HardFault_Handler(void);
void USB_IRQHandler(void);
-void USB2_IRQHandler(void);
+// void USB2_IRQHandler(void); // host on USB2 (rhport 1) — re-add together with the host driver
void SysTick_Handler(void);
#ifdef __cplusplus
diff --git a/hw/bsp/ch58x/family.c b/hw/bsp/ch58x/family.c
index 3b1382e61..8e5f2826b 100644
--- a/hw/bsp/ch58x/family.c
+++ b/hw/bsp/ch58x/family.c
@@ -50,13 +50,16 @@
// Forward USB interrupt events to TinyUSB IRQ Handler
//--------------------------------------------------------------------+
+// Device only: the shared dcd_ch32_usbfs.c drives USB0 (rhport 0). CH58x's second controller
+// (USB2 / rhport 1) was driven by the now-removed ch58x host driver; its handler is kept
+// commented out below to ease re-adding host support.
__INTERRUPT __HIGH_CODE void USB_IRQHandler(void) {
tusb_int_handler(0, true);
}
-__INTERRUPT __HIGH_CODE void USB2_IRQHandler(void) {
- tusb_int_handler(1, true);
-}
+// __INTERRUPT __HIGH_CODE void USB2_IRQHandler(void) {
+// tusb_int_handler(1, true);
+// }
//--------------------------------------------------------------------+
// SysTick
@@ -111,16 +114,12 @@ void board_init(void) {
#endif
#endif
- // USB pin enable: enable analog function for USB1 and USB2 D+/D-
- R16_PIN_ANALOG_IE |= RB_PIN_USB_IE | RB_PIN_USB2_IE;
-
- // D+ pull-up is only needed for the device-role port
+ // Device only on USB0 (rhport 0): enable analog function for USB1 D+/D- and assert its D+
+ // pull-up. The shared dcd_ch32_usbfs.c drives USB0; CH58x host / USB2 (rhport 1) is unsupported
+ // here — to re-add host, also OR in RB_PIN_USB2_IE below.
+ R16_PIN_ANALOG_IE |= RB_PIN_USB_IE; // | RB_PIN_USB2_IE (re-add for host on USB2)
#if CFG_TUD_ENABLED
- #if BOARD_TUD_RHPORT == 0
- R16_PIN_ANALOG_IE |= RB_PIN_USB_DP_PU;
- #else
- R16_PIN_ANALOG_IE |= RB_PIN_USB2_DP_PU;
- #endif
+ R16_PIN_ANALOG_IE |= RB_PIN_USB_DP_PU;
#endif
// Keep USB clock active during sleep
@@ -164,6 +163,10 @@ uint32_t board_button_read(void) {
#endif
}
+// Note: CH58x exposes no memory-mapped unique-ID register (unlike ch32v10x/v20x at
+// 0x1FFFF7E8), and the SDK's GET_UNIQUE_ID() is a BootROM stub not present in
+// libISP583.a. So board_get_unique_id() falls back to the fixed default in board.c.
+
int board_uart_read(uint8_t* buf, int len) {
(void) buf;
(void) len;
diff --git a/hw/bsp/ch58x/family.cmake b/hw/bsp/ch58x/family.cmake
index b4835cb41..cbe1ffe72 100644
--- a/hw/bsp/ch58x/family.cmake
+++ b/hw/bsp/ch58x/family.cmake
@@ -77,8 +77,7 @@ function(family_configure_example TARGET RTOS)
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/family.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/debug_uart.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/../board.c
- ${TOP}/src/portable/wch/dcd_ch58x_usbfs.c
- ${TOP}/src/portable/wch/hcd_ch58x_usbfs.c
+ ${TOP}/src/portable/wch/dcd_ch32_usbfs.c
${STARTUP_FILE_${CMAKE_C_COMPILER_ID}}
)
target_include_directories(${TARGET} PUBLIC
diff --git a/hw/bsp/ch58x/family.mk b/hw/bsp/ch58x/family.mk
index 71c2cada6..2ef90a8c3 100644
--- a/hw/bsp/ch58x/family.mk
+++ b/hw/bsp/ch58x/family.mk
@@ -33,8 +33,7 @@ LDFLAGS_GCC += \
LIBS += $(TOP)/$(SDK_SRC_DIR)/StdPeriphDriver/libISP583.a
SRC_C += \
- src/portable/wch/dcd_ch58x_usbfs.c \
- src/portable/wch/hcd_ch58x_usbfs.c \
+ src/portable/wch/dcd_ch32_usbfs.c \
$(SDK_SRC_DIR)/StdPeriphDriver/CH58x_gpio.c \
$(SDK_SRC_DIR)/StdPeriphDriver/CH58x_clk.c \
$(SDK_SRC_DIR)/StdPeriphDriver/CH58x_uart1.c \