Skip to content

Commit 1b9e433

Browse files
committed
update to work with ch32v307 usbfs
1 parent f5ad52f commit 1b9e433

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

src/arduino/ports/ch32/Adafruit_TinyUSB_ch32.cpp

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@
2424

2525
#include "tusb_option.h"
2626

27-
#if defined(CH32V20x) && CFG_TUD_ENABLED
27+
#if CFG_TUD_ENABLED && \
28+
(defined(ARDUINO_ARCH_CH32) || defined(CH32V20x) || defined(CH32V30x))
2829

2930
#include "Arduino.h"
3031
#include "arduino/Adafruit_USBD_Device.h"
@@ -38,41 +39,52 @@
3839
//--------------------------------------------------------------------+
3940
extern "C" {
4041

41-
// USBFS
42-
__attribute__((interrupt("WCH-Interrupt-fast"))) void USBHD_IRQHandler(void) {
43-
#if CFG_TUD_WCH_USBIP_USBFS
42+
// USBD (fsdev)
43+
#if CFG_TUD_WCH_USBIP_FSDEV
44+
__attribute__((interrupt("WCH-Interrupt-fast"))) void
45+
USB_LP_CAN1_RX0_IRQHandler(void) {
4446
tud_int_handler(0);
45-
#endif
4647
}
4748

4849
__attribute__((interrupt("WCH-Interrupt-fast"))) void
49-
USBHDWakeUp_IRQHandler(void) {
50-
#if CFG_TUD_WCH_USBIP_USBFS
50+
USB_HP_CAN1_TX_IRQHandler(void) {
5151
tud_int_handler(0);
52-
#endif
5352
}
5453

55-
// USBD (fsdev)
5654
__attribute__((interrupt("WCH-Interrupt-fast"))) void
57-
USB_LP_CAN1_RX0_IRQHandler(void) {
58-
#if CFG_TUD_WCH_USBIP_FSDEV
55+
USBWakeUp_IRQHandler(void) {
5956
tud_int_handler(0);
57+
}
6058
#endif
59+
60+
// USBFS
61+
#if CFG_TUD_WCH_USBIP_USBFS
62+
63+
#ifdef CH32V20x
64+
__attribute__((interrupt("WCH-Interrupt-fast"))) void USBHD_IRQHandler(void) {
65+
tud_int_handler(0);
6166
}
6267

6368
__attribute__((interrupt("WCH-Interrupt-fast"))) void
64-
USB_HP_CAN1_TX_IRQHandler(void) {
65-
#if CFG_TUD_WCH_USBIP_FSDEV
69+
USBHDWakeUp_IRQHandler(void) {
6670
tud_int_handler(0);
67-
#endif
6871
}
72+
#endif
6973

70-
__attribute__((interrupt("WCH-Interrupt-fast"))) void
71-
USBWakeUp_IRQHandler(void) {
72-
#if CFG_TUD_WCH_USBIP_FSDEV
74+
#ifdef CH32V30x
75+
__attribute__((interrupt("WCH-Interrupt-fast"))) void OTG_FS_IRQHandler(void) {
7376
tud_int_handler(0);
77+
}
78+
#endif
79+
7480
#endif
81+
82+
// USBHS
83+
#if CFG_TUD_WCH_USBIP_USBHS
84+
__attribute__((interrupt("WCH-Interrupt-fast"))) void USBHS_IRQHandler(void) {
85+
tud_int_handler(0);
7586
}
87+
#endif
7688

7789
void yield(void) {
7890
tud_task();
@@ -87,28 +99,33 @@ void yield(void) {
8799
//--------------------------------------------------------------------+
88100

89101
void TinyUSB_Port_InitDevice(uint8_t rhport) {
102+
#if CFG_TUD_WCH_USBIP_FSDEV || CFG_TUD_WCH_USBIP_USBFS
103+
// Full speed OTG or FSDev
90104
uint8_t usb_div;
91105
switch (SystemCoreClock) {
92106
case 48000000:
93-
usb_div = RCC_USBCLKSource_PLLCLK_Div1;
107+
usb_div = 0; // div1
94108
break;
95109
case 96000000:
96-
usb_div = RCC_USBCLKSource_PLLCLK_Div2;
110+
usb_div = 1; // div2
97111
break;
98112
case 144000000:
99-
usb_div = RCC_USBCLKSource_PLLCLK_Div3;
113+
usb_div = 2; // div3
100114
break;
101115
default:
102116
return; // unsupported
103117
}
104-
RCC_USBCLKConfig(usb_div);
118+
// RCC_USBCLKConfig(usb_div) or RCC_OTGFSCLKConfig(usb_div)
119+
RCC->CFGR0 &= ~(3 << 22);
120+
RCC->CFGR0 |= usb_div << 22;
105121

106122
#if CFG_TUD_WCH_USBIP_FSDEV
107123
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
108124
#endif
109125

110126
#if CFG_TUD_WCH_USBIP_USBFS
111127
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE);
128+
#endif
112129
#endif
113130

114131
tud_init(rhport);

0 commit comments

Comments
 (0)