always define CFG_TUH_WCH_USBIP_USBFS=1 for ch32v20x since only port1 support host mode

reformat hcd usbfs
add uart rx for ch32v20x bsp
This commit is contained in:
hathach
2025-07-05 11:26:48 +07:00
parent 1b5f97ff23
commit 6e88895dbc
5 changed files with 483 additions and 577 deletions

View File

@@ -20,59 +20,56 @@ manufacturer: WCH
#include "bsp/board_api.h" #include "bsp/board_api.h"
#include "board.h" #include "board.h"
/* CH32v203 depending on variants can support 2 USB IPs: FSDEV and USBFS. /* CH32v203 depending on variants can support 2 USB IPs: FSDEV (port0) and USBFS (port1).
* By default, we use FSDEV, but you can explicitly select by define: * By default, we use FSDEV, but you can explicitly select by define:
* - CFG_TUD_WCH_USBIP_FSDEV * - CFG_TUD_WCH_USBIP_FSDEV
* - CFG_TUD_WCH_USBIP_USBFS * - CFG_TUD_WCH_USBIP_USBFS
*/ */
// USBFS // Port0: USBD (fsdev)
__attribute__((interrupt)) __attribute__((used)) __attribute__((interrupt)) __attribute__((used)) void USB_LP_CAN1_RX0_IRQHandler(void) {
void USBHD_IRQHandler(void) { #if CFG_TUD_WCH_USBIP_FSDEV
#if CFG_TUD_WCH_USBIP_USBFS
tud_int_handler(0); tud_int_handler(0);
#endif #endif
#if defined(CFG_TUH_WCH_USBIP_USBFS) && CFG_TUH_WCH_USBIP_USBFS
tuh_int_handler(0);
#endif
} }
__attribute__((interrupt)) __attribute__((used)) __attribute__((interrupt)) __attribute__((used)) void USB_HP_CAN1_TX_IRQHandler(void) {
void USBHDWakeUp_IRQHandler(void) { #if CFG_TUD_WCH_USBIP_FSDEV
tud_int_handler(0);
#endif
}
__attribute__((interrupt)) __attribute__((used)) void USBWakeUp_IRQHandler(void) {
#if CFG_TUD_WCH_USBIP_FSDEV
tud_int_handler(0);
#endif
}
// Port1: USBFS
__attribute__((interrupt)) __attribute__((used)) void USBHD_IRQHandler(void) {
#if CFG_TUD_ENABLED && CFG_TUD_WCH_USBIP_USBFS
tud_int_handler(1);
#endif
#if CFG_TUH_ENABLED
tuh_int_handler(1);
#endif
}
__attribute__((interrupt)) __attribute__((used)) void USBHDWakeUp_IRQHandler(void) {
#if CFG_TUD_WCH_USBIP_USBFS #if CFG_TUD_WCH_USBIP_USBFS
tud_int_handler(0); tud_int_handler(0);
#endif #endif
} }
// USBD (fsdev) //--------------------------------------------------------------------+
__attribute__((interrupt)) __attribute__((used)) // Board API
void USB_LP_CAN1_RX0_IRQHandler(void) { //--------------------------------------------------------------------+
#if CFG_TUD_WCH_USBIP_FSDEV
tud_int_handler(0);
#endif
}
__attribute__((interrupt)) __attribute__((used))
void USB_HP_CAN1_TX_IRQHandler(void) {
#if CFG_TUD_WCH_USBIP_FSDEV
tud_int_handler(0);
#endif
}
__attribute__((interrupt)) __attribute__((used))
void USBWakeUp_IRQHandler(void) {
#if CFG_TUD_WCH_USBIP_FSDEV
tud_int_handler(0);
#endif
}
#if CFG_TUSB_OS == OPT_OS_NONE #if CFG_TUSB_OS == OPT_OS_NONE
volatile uint32_t system_ticks = 0; volatile uint32_t system_ticks = 0;
__attribute__((interrupt)) __attribute__((interrupt)) void SysTick_Handler(void) {
void SysTick_Handler(void) {
SysTick->SR = 0; SysTick->SR = 0;
system_ticks++; system_ticks++;
} }
@@ -111,7 +108,7 @@ void board_init(void) {
#ifdef UART_DEV #ifdef UART_DEV
UART_CLOCK_EN(); UART_CLOCK_EN();
GPIO_InitTypeDef usart_init = { GPIO_InitTypeDef usart_init = {
.GPIO_Pin = UART_TX_PIN, .GPIO_Pin = UART_TX_PIN | UART_RX_PIN,
.GPIO_Speed = GPIO_Speed_50MHz, .GPIO_Speed = GPIO_Speed_50MHz,
.GPIO_Mode = GPIO_Mode_AF_PP, .GPIO_Mode = GPIO_Mode_AF_PP,
}; };
@@ -122,7 +119,7 @@ void board_init(void) {
.USART_WordLength = USART_WordLength_8b, .USART_WordLength = USART_WordLength_8b,
.USART_StopBits = USART_StopBits_1, .USART_StopBits = USART_StopBits_1,
.USART_Parity = USART_Parity_No, .USART_Parity = USART_Parity_No,
.USART_Mode = USART_Mode_Tx, .USART_Mode = USART_Mode_Tx | USART_Mode_Rx,
.USART_HardwareFlowControl = USART_HardwareFlowControl_None, .USART_HardwareFlowControl = USART_HardwareFlowControl_None,
}; };
USART_Init(UART_DEV, &usart); USART_Init(UART_DEV, &usart);
@@ -192,9 +189,19 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) {
} }
int board_uart_read(uint8_t *buf, int len) { int board_uart_read(uint8_t *buf, int len) {
(void) buf; #ifdef UART_DEV
(void) len; int count;
for (count = 0; count < len; count++) {
if (USART_GetFlagStatus(UART_DEV, USART_FLAG_RXNE) == RESET) {
break;
}
buf[count] = USART_ReceiveData(UART_DEV);
}
return count;
#else
(void) buf; (void) len;
return 0; return 0;
#endif
} }
int board_uart_write(void const *buf, int len) { int board_uart_write(void const *buf, int len) {
@@ -210,7 +217,3 @@ int board_uart_write(void const *buf, int len) {
return len; return len;
} }
//--------------------------------------------------------------------
// Neopixel
//--------------------------------------------------------------------

View File

@@ -16,9 +16,12 @@ set(FAMILY_MCUS CH32V20X CACHE INTERNAL "")
set(OPENOCD_OPTION "-f ${CMAKE_CURRENT_LIST_DIR}/wch-riscv.cfg") set(OPENOCD_OPTION "-f ${CMAKE_CURRENT_LIST_DIR}/wch-riscv.cfg")
# Port0 use FSDev, Port1 use USBFS # Port0 use FSDev, Port1 use USBFS
if (NOT DEFINED PORT) if (NOT DEFINED RHPORT_DEVICE)
set(PORT 0) set(RHPORT_DEVICE 0)
endif() endif ()
# only port1 support host mode
set(RHPORT_HOST 1)
#------------------------------------ #------------------------------------
# BOARD_TARGET # BOARD_TARGET
@@ -56,19 +59,16 @@ function(add_board_target BOARD_TARGET)
) )
target_compile_definitions(${BOARD_TARGET} PUBLIC target_compile_definitions(${BOARD_TARGET} PUBLIC
CH32V20x_${MCU_VARIANT} CH32V20x_${MCU_VARIANT}
BOARD_TUD_RHPORT=${RHPORT_DEVICE}
BOARD_TUH_RHPORT=${RHPORT_HOST}
) )
if (PORT EQUAL 0) if (RHPORT_DEVICE EQUAL 0)
target_compile_definitions(${BOARD_TARGET} PUBLIC target_compile_definitions(${BOARD_TARGET} PUBLIC CFG_TUD_WCH_USBIP_FSDEV=1)
CFG_TUD_WCH_USBIP_FSDEV=1 elseif (RHPORT_DEVICE EQUAL 1)
CFG_TUH_WCH_USBIP_USBFS=1 target_compile_definitions(${BOARD_TARGET} PUBLIC CFG_TUH_WCH_USBIP_USBFS=1)
)
elseif (PORT EQUAL 1)
target_compile_definitions(${BOARD_TARGET} PUBLIC
CFG_TUD_WCH_USBIP_USBFS=1
)
else() else()
message(FATAL_ERROR "Invalid PORT ${PORT}") message(FATAL_ERROR "Invalid RHPORT_DEVICE ${RHPORT_DEVICE}")
endif() endif()
update_board(${BOARD_TARGET}) update_board(${BOARD_TARGET})
@@ -133,8 +133,6 @@ function(family_configure_example TARGET RTOS)
) )
target_link_libraries(${TARGET} PUBLIC board_${BOARD}) target_link_libraries(${TARGET} PUBLIC board_${BOARD})
# Flashing # Flashing
family_add_bin_hex(${TARGET}) family_add_bin_hex(${TARGET})
family_flash_openocd_wch(${TARGET}) family_flash_openocd_wch(${TARGET})

View File

@@ -503,11 +503,17 @@
#define TUP_DCD_ENDPOINT_MAX 8 #define TUP_DCD_ENDPOINT_MAX 8
#elif TU_CHECK_MCU(OPT_MCU_CH32V20X) #elif TU_CHECK_MCU(OPT_MCU_CH32V20X)
// v20x support both FSDEV (USBD) and USBFS, default to FSDEV // v20x support both port0 FSDEV (USBD) and port1 USBFS
#define TUP_USBIP_WCH_USBFS #define TUP_USBIP_WCH_USBFS
#ifndef CFG_TUH_WCH_USBIP_USBFS
#define CFG_TUH_WCH_USBIP_USBFS 1
#endif
#define TUP_USBIP_FSDEV #define TUP_USBIP_FSDEV
#define TUP_USBIP_FSDEV_CH32 #define TUP_USBIP_FSDEV_CH32
// default to FSDEV for device
#if !defined(CFG_TUD_WCH_USBIP_USBFS) #if !defined(CFG_TUD_WCH_USBIP_USBFS)
#define CFG_TUD_WCH_USBIP_USBFS 0 #define CFG_TUD_WCH_USBIP_USBFS 0
#endif #endif

View File

@@ -27,7 +27,7 @@
#include "tusb_option.h" #include "tusb_option.h"
#if CFG_TUD_ENABLED && defined(TUP_USBIP_WCH_USBHS) && CFG_TUD_WCH_USBIP_USBHS #if CFG_TUD_ENABLED && defined(TUP_USBIP_WCH_USBHS) && defined(CFG_TUD_WCH_USBIP_USBHS) && CFG_TUD_WCH_USBIP_USBHS
#include "ch32_usbhs_reg.h" #include "ch32_usbhs_reg.h"
#include "device/dcd.h" #include "device/dcd.h"

File diff suppressed because it is too large Load Diff