24
24
25
25
#include " tusb_option.h"
26
26
27
- #if defined(CH32V20x) && CFG_TUD_ENABLED
27
+ #if CFG_TUD_ENABLED && \
28
+ (defined(ARDUINO_ARCH_CH32) || defined(CH32V20x) || defined(CH32V30x))
28
29
29
30
#include " Arduino.h"
30
31
#include " arduino/Adafruit_USBD_Device.h"
38
39
// --------------------------------------------------------------------+
39
40
extern " C" {
40
41
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 ) {
44
46
tud_int_handler (0 );
45
- #endif
46
47
}
47
48
48
49
__attribute__ ((interrupt(" WCH-Interrupt-fast" ))) void
49
- USBHDWakeUp_IRQHandler(void ) {
50
- #if CFG_TUD_WCH_USBIP_USBFS
50
+ USB_HP_CAN1_TX_IRQHandler(void ) {
51
51
tud_int_handler (0 );
52
- #endif
53
52
}
54
53
55
- // USBD (fsdev)
56
54
__attribute__ ((interrupt(" WCH-Interrupt-fast" ))) void
57
- USB_LP_CAN1_RX0_IRQHandler(void ) {
58
- #if CFG_TUD_WCH_USBIP_FSDEV
55
+ USBWakeUp_IRQHandler(void ) {
59
56
tud_int_handler (0 );
57
+ }
60
58
#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 );
61
66
}
62
67
63
68
__attribute__ ((interrupt(" WCH-Interrupt-fast" ))) void
64
- USB_HP_CAN1_TX_IRQHandler(void ) {
65
- #if CFG_TUD_WCH_USBIP_FSDEV
69
+ USBHDWakeUp_IRQHandler(void ) {
66
70
tud_int_handler (0 );
67
- #endif
68
71
}
72
+ #endif
69
73
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 ) {
73
76
tud_int_handler (0 );
77
+ }
78
+ #endif
79
+
74
80
#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 );
75
86
}
87
+ #endif
76
88
77
89
void yield (void ) {
78
90
tud_task ();
@@ -87,28 +99,33 @@ void yield(void) {
87
99
// --------------------------------------------------------------------+
88
100
89
101
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
90
104
uint8_t usb_div;
91
105
switch (SystemCoreClock) {
92
106
case 48000000 :
93
- usb_div = RCC_USBCLKSource_PLLCLK_Div1;
107
+ usb_div = 0 ; // div1
94
108
break ;
95
109
case 96000000 :
96
- usb_div = RCC_USBCLKSource_PLLCLK_Div2;
110
+ usb_div = 1 ; // div2
97
111
break ;
98
112
case 144000000 :
99
- usb_div = RCC_USBCLKSource_PLLCLK_Div3;
113
+ usb_div = 2 ; // div3
100
114
break ;
101
115
default :
102
116
return ; // unsupported
103
117
}
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 ;
105
121
106
122
#if CFG_TUD_WCH_USBIP_FSDEV
107
123
RCC_APB1PeriphClockCmd (RCC_APB1Periph_USB, ENABLE);
108
124
#endif
109
125
110
126
#if CFG_TUD_WCH_USBIP_USBFS
111
127
RCC_AHBPeriphClockCmd (RCC_AHBPeriph_OTG_FS, ENABLE);
128
+ #endif
112
129
#endif
113
130
114
131
tud_init (rhport);
0 commit comments