Skip to content

Commit 6e88895

Browse files
committed
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
1 parent 1b5f97f commit 6e88895

File tree

5 files changed

+477
-571
lines changed

5 files changed

+477
-571
lines changed

hw/bsp/ch32v20x/family.c

Lines changed: 35 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,59 +20,56 @@ manufacturer: WCH
2020
#include "bsp/board_api.h"
2121
#include "board.h"
2222

23-
/* CH32v203 depending on variants can support 2 USB IPs: FSDEV and USBFS.
23+
/* CH32v203 depending on variants can support 2 USB IPs: FSDEV (port0) and USBFS (port1).
2424
* By default, we use FSDEV, but you can explicitly select by define:
2525
* - CFG_TUD_WCH_USBIP_FSDEV
2626
* - CFG_TUD_WCH_USBIP_USBFS
2727
*/
2828

29-
// USBFS
30-
__attribute__((interrupt)) __attribute__((used))
31-
void USBHD_IRQHandler(void) {
32-
#if CFG_TUD_WCH_USBIP_USBFS
29+
// Port0: USBD (fsdev)
30+
__attribute__((interrupt)) __attribute__((used)) void USB_LP_CAN1_RX0_IRQHandler(void) {
31+
#if CFG_TUD_WCH_USBIP_FSDEV
3332
tud_int_handler(0);
3433
#endif
35-
#if defined(CFG_TUH_WCH_USBIP_USBFS) && CFG_TUH_WCH_USBIP_USBFS
36-
tuh_int_handler(0);
37-
#endif
3834
}
3935

40-
__attribute__((interrupt)) __attribute__((used))
41-
void USBHDWakeUp_IRQHandler(void) {
42-
#if CFG_TUD_WCH_USBIP_USBFS
36+
__attribute__((interrupt)) __attribute__((used)) void USB_HP_CAN1_TX_IRQHandler(void) {
37+
#if CFG_TUD_WCH_USBIP_FSDEV
4338
tud_int_handler(0);
4439
#endif
40+
4541
}
4642

47-
// USBD (fsdev)
48-
__attribute__((interrupt)) __attribute__((used))
49-
void USB_LP_CAN1_RX0_IRQHandler(void) {
43+
__attribute__((interrupt)) __attribute__((used)) void USBWakeUp_IRQHandler(void) {
5044
#if CFG_TUD_WCH_USBIP_FSDEV
5145
tud_int_handler(0);
5246
#endif
5347
}
5448

55-
__attribute__((interrupt)) __attribute__((used))
56-
void USB_HP_CAN1_TX_IRQHandler(void) {
57-
#if CFG_TUD_WCH_USBIP_FSDEV
58-
tud_int_handler(0);
49+
// Port1: USBFS
50+
__attribute__((interrupt)) __attribute__((used)) void USBHD_IRQHandler(void) {
51+
#if CFG_TUD_ENABLED && CFG_TUD_WCH_USBIP_USBFS
52+
tud_int_handler(1);
5953
#endif
6054

55+
#if CFG_TUH_ENABLED
56+
tuh_int_handler(1);
57+
#endif
6158
}
6259

63-
__attribute__((interrupt)) __attribute__((used))
64-
void USBWakeUp_IRQHandler(void) {
65-
#if CFG_TUD_WCH_USBIP_FSDEV
60+
__attribute__((interrupt)) __attribute__((used)) void USBHDWakeUp_IRQHandler(void) {
61+
#if CFG_TUD_WCH_USBIP_USBFS
6662
tud_int_handler(0);
6763
#endif
6864
}
6965

70-
66+
//--------------------------------------------------------------------+
67+
// Board API
68+
//--------------------------------------------------------------------+
7169
#if CFG_TUSB_OS == OPT_OS_NONE
7270
volatile uint32_t system_ticks = 0;
7371

74-
__attribute__((interrupt))
75-
void SysTick_Handler(void) {
72+
__attribute__((interrupt)) void SysTick_Handler(void) {
7673
SysTick->SR = 0;
7774
system_ticks++;
7875
}
@@ -111,7 +108,7 @@ void board_init(void) {
111108
#ifdef UART_DEV
112109
UART_CLOCK_EN();
113110
GPIO_InitTypeDef usart_init = {
114-
.GPIO_Pin = UART_TX_PIN,
111+
.GPIO_Pin = UART_TX_PIN | UART_RX_PIN,
115112
.GPIO_Speed = GPIO_Speed_50MHz,
116113
.GPIO_Mode = GPIO_Mode_AF_PP,
117114
};
@@ -122,7 +119,7 @@ void board_init(void) {
122119
.USART_WordLength = USART_WordLength_8b,
123120
.USART_StopBits = USART_StopBits_1,
124121
.USART_Parity = USART_Parity_No,
125-
.USART_Mode = USART_Mode_Tx,
122+
.USART_Mode = USART_Mode_Tx | USART_Mode_Rx,
126123
.USART_HardwareFlowControl = USART_HardwareFlowControl_None,
127124
};
128125
USART_Init(UART_DEV, &usart);
@@ -192,9 +189,19 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) {
192189
}
193190

194191
int board_uart_read(uint8_t *buf, int len) {
195-
(void) buf;
196-
(void) len;
192+
#ifdef UART_DEV
193+
int count;
194+
for (count = 0; count < len; count++) {
195+
if (USART_GetFlagStatus(UART_DEV, USART_FLAG_RXNE) == RESET) {
196+
break;
197+
}
198+
buf[count] = USART_ReceiveData(UART_DEV);
199+
}
200+
return count;
201+
#else
202+
(void) buf; (void) len;
197203
return 0;
204+
#endif
198205
}
199206

200207
int board_uart_write(void const *buf, int len) {
@@ -210,7 +217,3 @@ int board_uart_write(void const *buf, int len) {
210217

211218
return len;
212219
}
213-
214-
//--------------------------------------------------------------------
215-
// Neopixel
216-
//--------------------------------------------------------------------

hw/bsp/ch32v20x/family.cmake

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,12 @@ set(FAMILY_MCUS CH32V20X CACHE INTERNAL "")
1616
set(OPENOCD_OPTION "-f ${CMAKE_CURRENT_LIST_DIR}/wch-riscv.cfg")
1717

1818
# Port0 use FSDev, Port1 use USBFS
19-
if (NOT DEFINED PORT)
20-
set(PORT 0)
21-
endif()
19+
if (NOT DEFINED RHPORT_DEVICE)
20+
set(RHPORT_DEVICE 0)
21+
endif ()
22+
23+
# only port1 support host mode
24+
set(RHPORT_HOST 1)
2225

2326
#------------------------------------
2427
# BOARD_TARGET
@@ -56,19 +59,16 @@ function(add_board_target BOARD_TARGET)
5659
)
5760
target_compile_definitions(${BOARD_TARGET} PUBLIC
5861
CH32V20x_${MCU_VARIANT}
62+
BOARD_TUD_RHPORT=${RHPORT_DEVICE}
63+
BOARD_TUH_RHPORT=${RHPORT_HOST}
5964
)
6065

61-
if (PORT EQUAL 0)
62-
target_compile_definitions(${BOARD_TARGET} PUBLIC
63-
CFG_TUD_WCH_USBIP_FSDEV=1
64-
CFG_TUH_WCH_USBIP_USBFS=1
65-
)
66-
elseif (PORT EQUAL 1)
67-
target_compile_definitions(${BOARD_TARGET} PUBLIC
68-
CFG_TUD_WCH_USBIP_USBFS=1
69-
)
66+
if (RHPORT_DEVICE EQUAL 0)
67+
target_compile_definitions(${BOARD_TARGET} PUBLIC CFG_TUD_WCH_USBIP_FSDEV=1)
68+
elseif (RHPORT_DEVICE EQUAL 1)
69+
target_compile_definitions(${BOARD_TARGET} PUBLIC CFG_TUH_WCH_USBIP_USBFS=1)
7070
else()
71-
message(FATAL_ERROR "Invalid PORT ${PORT}")
71+
message(FATAL_ERROR "Invalid RHPORT_DEVICE ${RHPORT_DEVICE}")
7272
endif()
7373

7474
update_board(${BOARD_TARGET})
@@ -133,8 +133,6 @@ function(family_configure_example TARGET RTOS)
133133
)
134134
target_link_libraries(${TARGET} PUBLIC board_${BOARD})
135135

136-
137-
138136
# Flashing
139137
family_add_bin_hex(${TARGET})
140138
family_flash_openocd_wch(${TARGET})

src/common/tusb_mcu.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -503,11 +503,17 @@
503503
#define TUP_DCD_ENDPOINT_MAX 8
504504

505505
#elif TU_CHECK_MCU(OPT_MCU_CH32V20X)
506-
// v20x support both FSDEV (USBD) and USBFS, default to FSDEV
506+
// v20x support both port0 FSDEV (USBD) and port1 USBFS
507507
#define TUP_USBIP_WCH_USBFS
508+
509+
#ifndef CFG_TUH_WCH_USBIP_USBFS
510+
#define CFG_TUH_WCH_USBIP_USBFS 1
511+
#endif
512+
508513
#define TUP_USBIP_FSDEV
509514
#define TUP_USBIP_FSDEV_CH32
510515

516+
// default to FSDEV for device
511517
#if !defined(CFG_TUD_WCH_USBIP_USBFS)
512518
#define CFG_TUD_WCH_USBIP_USBFS 0
513519
#endif

src/portable/wch/dcd_ch32_usbhs.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
#include "tusb_option.h"
2929

30-
#if CFG_TUD_ENABLED && defined(TUP_USBIP_WCH_USBHS) && CFG_TUD_WCH_USBIP_USBHS
30+
#if CFG_TUD_ENABLED && defined(TUP_USBIP_WCH_USBHS) && defined(CFG_TUD_WCH_USBIP_USBHS) && CFG_TUD_WCH_USBIP_USBHS
3131
#include "ch32_usbhs_reg.h"
3232

3333
#include "device/dcd.h"

0 commit comments

Comments
 (0)