Skip to content

Commit b012e95

Browse files
authored
Merge pull request #2793 from verylowfreq/pr-ch32v-usbfs-host
Add Host support for ch32v20x usbfs
2 parents 9d872d5 + 4b95a70 commit b012e95

File tree

14 files changed

+693
-45
lines changed

14 files changed

+693
-45
lines changed

examples/dual/host_hid_to_device_cdc/only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
board:mimxrt1060_evk
22
board:mimxrt1064_evk
33
board:mcb1800
4+
mcu:CH32V20X
45
mcu:RP2040
56
mcu:ra6m5
67
mcu:MAX3421

examples/dual/host_info_to_device_cdc/only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
board:mimxrt1060_evk
22
board:mimxrt1064_evk
33
board:mcb1800
4+
mcu:CH32V20X
45
mcu:RP2040
56
mcu:ra6m5
67
mcu:MAX3421

examples/host/bare_api/only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V20X
12
mcu:KINETIS_KL
23
mcu:LPC175X_6X
34
mcu:LPC177X_8X

examples/host/cdc_msc_hid/only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V20X
12
mcu:KINETIS_KL
23
mcu:LPC175X_6X
34
mcu:LPC177X_8X

examples/host/device_info/only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V20X
12
mcu:ESP32S2
23
mcu:ESP32S3
34
mcu:ESP32P4

examples/host/hid_controller/only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V20X
12
mcu:KINETIS_KL
23
mcu:LPC175X_6X
34
mcu:LPC177X_8X

examples/host/midi_rx/only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V20X
12
mcu:ESP32S2
23
mcu:ESP32S3
34
mcu:ESP32P4

examples/host/msc_file_explorer/only.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
mcu:CH32V20X
12
mcu:KINETIS_KL
23
mcu:LPC175X_6X
34
mcu:LPC177X_8X

hw/bsp/ch32v20x/family.c

Lines changed: 35 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -20,56 +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
3534
}
3635

37-
__attribute__((interrupt)) __attribute__((used))
38-
void USBHDWakeUp_IRQHandler(void) {
39-
#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
4038
tud_int_handler(0);
4139
#endif
40+
4241
}
4342

44-
// USBD (fsdev)
45-
__attribute__((interrupt)) __attribute__((used))
46-
void USB_LP_CAN1_RX0_IRQHandler(void) {
43+
__attribute__((interrupt)) __attribute__((used)) void USBWakeUp_IRQHandler(void) {
4744
#if CFG_TUD_WCH_USBIP_FSDEV
4845
tud_int_handler(0);
4946
#endif
5047
}
5148

52-
__attribute__((interrupt)) __attribute__((used))
53-
void USB_HP_CAN1_TX_IRQHandler(void) {
54-
#if CFG_TUD_WCH_USBIP_FSDEV
55-
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);
5653
#endif
5754

55+
#if CFG_TUH_ENABLED
56+
tuh_int_handler(1);
57+
#endif
5858
}
5959

60-
__attribute__((interrupt)) __attribute__((used))
61-
void USBWakeUp_IRQHandler(void) {
62-
#if CFG_TUD_WCH_USBIP_FSDEV
60+
__attribute__((interrupt)) __attribute__((used)) void USBHDWakeUp_IRQHandler(void) {
61+
#if CFG_TUD_WCH_USBIP_USBFS
6362
tud_int_handler(0);
6463
#endif
6564
}
6665

67-
66+
//--------------------------------------------------------------------+
67+
// Board API
68+
//--------------------------------------------------------------------+
6869
#if CFG_TUSB_OS == OPT_OS_NONE
6970
volatile uint32_t system_ticks = 0;
7071

71-
__attribute__((interrupt))
72-
void SysTick_Handler(void) {
72+
__attribute__((interrupt)) void SysTick_Handler(void) {
7373
SysTick->SR = 0;
7474
system_ticks++;
7575
}
@@ -108,7 +108,7 @@ void board_init(void) {
108108
#ifdef UART_DEV
109109
UART_CLOCK_EN();
110110
GPIO_InitTypeDef usart_init = {
111-
.GPIO_Pin = UART_TX_PIN,
111+
.GPIO_Pin = UART_TX_PIN | UART_RX_PIN,
112112
.GPIO_Speed = GPIO_Speed_50MHz,
113113
.GPIO_Mode = GPIO_Mode_AF_PP,
114114
};
@@ -119,7 +119,7 @@ void board_init(void) {
119119
.USART_WordLength = USART_WordLength_8b,
120120
.USART_StopBits = USART_StopBits_1,
121121
.USART_Parity = USART_Parity_No,
122-
.USART_Mode = USART_Mode_Tx,
122+
.USART_Mode = USART_Mode_Tx | USART_Mode_Rx,
123123
.USART_HardwareFlowControl = USART_HardwareFlowControl_None,
124124
};
125125
USART_Init(UART_DEV, &usart);
@@ -189,9 +189,19 @@ size_t board_get_unique_id(uint8_t id[], size_t max_len) {
189189
}
190190

191191
int board_uart_read(uint8_t *buf, int len) {
192-
(void) buf;
193-
(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;
194203
return 0;
204+
#endif
195205
}
196206

197207
int board_uart_write(void const *buf, int len) {
@@ -207,7 +217,3 @@ int board_uart_write(void const *buf, int len) {
207217

208218
return len;
209219
}
210-
211-
//--------------------------------------------------------------------
212-
// Neopixel
213-
//--------------------------------------------------------------------

hw/bsp/ch32v20x/family.cmake

Lines changed: 14 additions & 14 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,18 +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-
)
65-
elseif (PORT EQUAL 1)
66-
target_compile_definitions(${BOARD_TARGET} PUBLIC
67-
CFG_TUD_WCH_USBIP_USBFS=1
68-
)
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)
6970
else()
70-
message(FATAL_ERROR "Invalid PORT ${PORT}")
71+
message(FATAL_ERROR "Invalid RHPORT_DEVICE ${RHPORT_DEVICE}")
7172
endif()
7273

7374
update_board(${BOARD_TARGET})
@@ -127,12 +128,11 @@ function(family_configure_example TARGET RTOS)
127128

128129
target_sources(${TARGET} PUBLIC
129130
${TOP}/src/portable/wch/dcd_ch32_usbfs.c
131+
${TOP}/src/portable/wch/hcd_ch32_usbfs.c
130132
${TOP}/src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c
131133
)
132134
target_link_libraries(${TARGET} PUBLIC board_${BOARD})
133135

134-
135-
136136
# Flashing
137137
family_add_bin_hex(${TARGET})
138138
family_flash_openocd_wch(${TARGET})

0 commit comments

Comments
 (0)