Skip to content

Commit a56612b

Browse files
authored
Merge pull request #429 from adafruit/ch32-core-support
Add Ch32 built-inn core support
2 parents 6cc5113 + 8cb4032 commit a56612b

25 files changed

+235
-69
lines changed

examples/CDC/cdc_multi/.feather_esp32s2.test.skip

Whitespace-only changes.

examples/CDC/cdc_multi/.feather_esp32s3.test.skip

Whitespace-only changes.

examples/CDC/cdc_multi/.funhouse.test.skip

Whitespace-only changes.

examples/CDC/cdc_multi/.magtag.test.skip

Whitespace-only changes.

examples/CDC/cdc_multi/.metroesp32s2.test.skip

Whitespace-only changes.

examples/CDC/cdc_multi/.pico_rp2040_tinyusb_host.test.skip

Whitespace-only changes.

examples/CDC/cdc_multi/.skip.txt

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
feather_esp32s2
2+
feather_esp32s3
3+
funhouse
4+
magtag
5+
metroesp32s2
6+
pico_rp2040_tinyusb_host

examples/CDC/serial_echo/serial_echo.ino

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
/* This sketch demonstrates USB CDC Serial echo (convert to upper case) using SerialTinyUSB which
1515
* is available for both core with built-in USB support and without.
16+
* Note: on core with built-in support Serial is alias to SerialTinyUSB
1617
*/
1718

1819
void setup() {

src/Adafruit_TinyUSB.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,6 @@
2525
#ifndef ADAFRUIT_TINYUSB_H_
2626
#define ADAFRUIT_TINYUSB_H_
2727

28-
// Error message for Core that must select TinyUSB via menu
29-
#if !defined(USE_TINYUSB) && \
30-
(defined(ARDUINO_ARCH_SAMD) || \
31-
(defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)))
32-
#error TinyUSB is not selected, please select it in "Tools->Menu->USB Stack"
33-
#endif
34-
3528
// ESP32 out-of-sync
3629
#ifdef ARDUINO_ARCH_ESP32
3730
#include "arduino/ports/esp32/tusb_config_esp32.h"

src/arduino/Adafruit_TinyUSB_API.h

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,24 @@
3232
// TinyUSB_API, USBD_CDC, USBD_Device, USBD_Interface,
3333
#define TINYUSB_API_VERSION 30000
3434

35+
#if defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_NRF52_ADAFRUIT) || \
36+
defined(ARDUINO_ARCH_ESP32) || defined(ARDUINO_ARCH_CH32) || \
37+
(defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED))
38+
#define TINYUSB_HAS_BUITLTIN_CORE_SUPPORT
39+
#endif
40+
3541
// Core that has built-in support: Adafruit SAMD, Adafruit nRF, rp2040, esp32
36-
#if !(defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_NRF52_ADAFRUIT) || \
37-
defined(ARDUINO_ARCH_ESP32) || \
38-
(defined(ARDUINO_ARCH_RP2040) && !defined(ARDUINO_ARCH_MBED)))
42+
#if !defined(TINYUSB_HAS_BUITLTIN_CORE_SUPPORT)
3943
#define TINYUSB_NEED_POLLING_TASK
4044
#endif
4145

46+
// Error message for Core that must select TinyUSB via menu (built-in except
47+
// esp32)
48+
#if !defined(USE_TINYUSB) && (defined(TINYUSB_HAS_BUITLTIN_CORE_SUPPORT) && \
49+
!defined(ARDUINO_ARCH_ESP32))
50+
#error TinyUSB is not selected, please select it in "Tools->Menu->USB Stack"
51+
#endif
52+
4253
//--------------------------------------------------------------------+
4354
// Core API
4455
// Should be called by BSP Core to initialize, process task

src/arduino/Adafruit_USBD_CDC.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
#define TINYUSB_API_VERSION 0
3939
#endif
4040

41+
#define BULK_PACKET_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
42+
4143
// SerialTinyUSB can be macro expanding to "Serial" on supported cores
4244
Adafruit_USBD_CDC SerialTinyUSB;
4345

@@ -69,8 +71,8 @@ uint16_t Adafruit_USBD_CDC::getInterfaceDescriptor(uint8_t itfnum_deprecated,
6971
uint8_t _strid = 0;
7072
#endif
7173

72-
uint8_t const desc[] = {
73-
TUD_CDC_DESCRIPTOR(itfnum, _strid, ep_notif, 8, ep_out, ep_in, 64)};
74+
uint8_t const desc[] = {TUD_CDC_DESCRIPTOR(itfnum, _strid, ep_notif, 8,
75+
ep_out, ep_in, BULK_PACKET_SIZE)};
7476

7577
uint16_t const len = sizeof(desc);
7678

src/arduino/Adafruit_USBD_CDC.h

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,15 @@ class Adafruit_USBD_CDC : public Stream, public Adafruit_USBD_Interface {
9393
bool isValid(void) { return _instance != INVALID_INSTANCE; }
9494
};
9595

96-
// "Serial" is used with TinyUSB CDC
97-
#if defined(USE_TINYUSB)
98-
extern Adafruit_USBD_CDC Serial;
96+
extern Adafruit_USBD_CDC SerialTinyUSB;
97+
98+
// Built-in support "Serial" is assigned to TinyUSB CDC
99+
// CH32 defines Serial as alias in WSerial.h
100+
#if defined(USE_TINYUSB) && !defined(ARDUINO_ARCH_CH32)
99101
#define SerialTinyUSB Serial
100102
#endif
101103

102-
// Serial is probably used with HW Uart
103-
#ifndef SerialTinyUSB
104104
extern Adafruit_USBD_CDC SerialTinyUSB;
105-
#endif
106105

107106
#endif // else of ESP32
108107
#endif // __cplusplus

src/arduino/Adafruit_USBD_Interface.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#include <stddef.h>
2929
#include <stdint.h>
3030

31-
#if defined(CH32V20x) || defined(CH32V30x)
31+
#if defined(ARDUINO_ARCH_CH32) || defined(CH32V20x) || defined(CH32V30x)
3232
// HACK: required for ch32 core version 1.0.4 or prior, removed when 1.0.5 is
3333
// released
3434
extern "C" void yield(void);

src/arduino/midi/Adafruit_USBD_MIDI.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
//--------------------------------------------------------------------+
3232
// MACRO TYPEDEF CONSTANT ENUM DECLARATION
3333
//--------------------------------------------------------------------+
34-
#define EPSIZE 64
34+
#define BULK_PACKET_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
3535

3636
// TODO multiple instances
3737
static Adafruit_USBD_MIDI *_midi_dev = NULL;
@@ -103,7 +103,7 @@ uint16_t Adafruit_USBD_MIDI::getInterfaceDescriptor(uint8_t itfnum_deprecated,
103103

104104
// Endpoint OUT + jack mapping
105105
{
106-
uint8_t desc[] = {TUD_MIDI_DESC_EP(ep_out, EPSIZE, _n_cables)};
106+
uint8_t desc[] = {TUD_MIDI_DESC_EP(ep_out, BULK_PACKET_SIZE, _n_cables)};
107107
memcpy(buf + len, desc, sizeof(desc));
108108
len += sizeof(desc);
109109
}
@@ -116,7 +116,7 @@ uint16_t Adafruit_USBD_MIDI::getInterfaceDescriptor(uint8_t itfnum_deprecated,
116116

117117
// Endpoint IN + jack mapping
118118
{
119-
uint8_t desc[] = {TUD_MIDI_DESC_EP(ep_in, EPSIZE, _n_cables)};
119+
uint8_t desc[] = {TUD_MIDI_DESC_EP(ep_in, BULK_PACKET_SIZE, _n_cables)};
120120
memcpy(buf + len, desc, sizeof(desc));
121121
len += sizeof(desc);
122122
}

src/arduino/msc/Adafruit_USBD_MSC.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828

2929
#include "Adafruit_USBD_MSC.h"
3030

31-
#define EPSIZE 64 // TODO must be 512 for highspeed device
31+
#define BULK_PACKET_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
3232

3333
static Adafruit_USBD_MSC *_msc_dev = NULL;
3434

@@ -52,7 +52,7 @@ uint16_t Adafruit_USBD_MSC::getInterfaceDescriptor(uint8_t itfnum_deprecated,
5252
uint8_t const ep_out = TinyUSBDevice.allocEndpoint(TUSB_DIR_OUT);
5353

5454
uint8_t const desc[] = {
55-
TUD_MSC_DESCRIPTOR(itfnum, _strid, ep_out, ep_in, EPSIZE)};
55+
TUD_MSC_DESCRIPTOR(itfnum, _strid, ep_out, ep_in, BULK_PACKET_SIZE)};
5656
uint16_t const len = sizeof(desc);
5757

5858
if (bufsize < len) {

src/arduino/ports/ch32/Adafruit_TinyUSB_ch32.cpp

Lines changed: 77 additions & 23 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,45 +39,63 @@
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+
}
58+
#endif
59+
60+
// USBFS
61+
#if CFG_TUD_WCH_USBIP_USBFS
62+
63+
#if defined(CH32V10x) || defined(CH32V20x)
64+
65+
#if defined(CH32V10x)
66+
#define USBHDWakeUp_IRQHandler USBWakeUp_IRQHandler
6067
#endif
68+
69+
__attribute__((interrupt("WCH-Interrupt-fast"))) void USBHD_IRQHandler(void) {
70+
tud_int_handler(0);
6171
}
6272

6373
__attribute__((interrupt("WCH-Interrupt-fast"))) void
64-
USB_HP_CAN1_TX_IRQHandler(void) {
65-
#if CFG_TUD_WCH_USBIP_FSDEV
74+
USBHDWakeUp_IRQHandler(void) {
6675
tud_int_handler(0);
67-
#endif
6876
}
77+
#endif
6978

70-
__attribute__((interrupt("WCH-Interrupt-fast"))) void
71-
USBWakeUp_IRQHandler(void) {
72-
#if CFG_TUD_WCH_USBIP_FSDEV
79+
#ifdef CH32V30x
80+
__attribute__((interrupt("WCH-Interrupt-fast"))) void OTG_FS_IRQHandler(void) {
7381
tud_int_handler(0);
82+
}
83+
#endif
84+
7485
#endif
86+
87+
// USBHS
88+
#if CFG_TUD_WCH_USBIP_USBHS
89+
__attribute__((interrupt("WCH-Interrupt-fast"))) void USBHS_IRQHandler(void) {
90+
tud_int_handler(0);
7591
}
92+
#endif
7693

7794
void yield(void) {
7895
tud_task();
79-
// flush cdc
96+
if (tud_cdc_connected()) {
97+
tud_cdc_write_flush();
98+
}
8099
}
81100
}
82101

@@ -85,21 +104,45 @@ void yield(void) {
85104
//--------------------------------------------------------------------+
86105

87106
void TinyUSB_Port_InitDevice(uint8_t rhport) {
107+
#if CFG_TUD_WCH_USBIP_FSDEV || CFG_TUD_WCH_USBIP_USBFS
108+
// Full speed OTG or FSDev
109+
110+
#if defined(CH32V10x)
111+
EXTEN->EXTEN_CTR |= EXTEN_USBHD_IO_EN;
112+
EXTEN->EXTEN_CTR &= ~EXTEN_USB_5V_SEL;
113+
114+
#define RCC_AHBPeriph_OTG_FS RCC_AHBPeriph_USBHD
115+
#endif
116+
88117
uint8_t usb_div;
89118
switch (SystemCoreClock) {
119+
#if defined(CH32V20x) || defined(CH32V30x)
90120
case 48000000:
91-
usb_div = RCC_USBCLKSource_PLLCLK_Div1;
92-
break;
121+
usb_div = 0;
122+
break; // div1
93123
case 96000000:
94-
usb_div = RCC_USBCLKSource_PLLCLK_Div2;
95-
break;
124+
usb_div = 1;
125+
break; // div2
96126
case 144000000:
97-
usb_div = RCC_USBCLKSource_PLLCLK_Div3;
127+
usb_div = 2;
128+
break; // div3
129+
#elif defined(CH32V10x)
130+
case 48000000:
131+
usb_div = RCC_USBCLKSource_PLLCLK_Div1;
98132
break;
133+
case 72000000:
134+
usb_div = RCC_USBCLKSource_PLLCLK_1Div5;
135+
break;
136+
#endif
99137
default:
100138
return; // unsupported
101139
}
140+
141+
#if defined(CH32V30x)
142+
RCC_OTGFSCLKConfig(usb_div);
143+
#else
102144
RCC_USBCLKConfig(usb_div);
145+
#endif
103146

104147
#if CFG_TUD_WCH_USBIP_FSDEV
105148
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
@@ -108,6 +151,17 @@ void TinyUSB_Port_InitDevice(uint8_t rhport) {
108151
#if CFG_TUD_WCH_USBIP_USBFS
109152
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_OTG_FS, ENABLE);
110153
#endif
154+
#endif
155+
156+
#if CFG_TUD_WCH_USBIP_USBHS
157+
// High speed USB: currently require 144MHz HSE, update later
158+
RCC_USBCLK48MConfig(RCC_USBCLK48MCLKSource_USBPHY);
159+
RCC_USBHSPLLCLKConfig(RCC_HSBHSPLLCLKSource_HSE);
160+
RCC_USBHSConfig(RCC_USBPLL_Div2);
161+
RCC_USBHSPLLCKREFCLKConfig(RCC_USBHSPLLCKREFCLK_4M);
162+
RCC_USBHSPHYPLLALIVEcmd(ENABLE);
163+
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_USBHS, ENABLE);
164+
#endif
111165

112166
tud_init(rhport);
113167
}

src/arduino/ports/ch32/tusb_config_ch32.h

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,11 @@ extern "C" {
3232
//--------------------------------------------------------------------
3333
// COMMON CONFIGURATION
3434
//--------------------------------------------------------------------
35-
#if defined(CH32V20x)
35+
#if defined(CH32V10x)
36+
#define CFG_TUSB_MCU OPT_MCU_CH32V103
37+
#warnning "CH32v103 is not working yet"
38+
#elif defined(CH32V20x)
3639
#define CFG_TUSB_MCU OPT_MCU_CH32V20X
37-
#define CFG_TUD_WCH_USBIP_FSDEV 1 // use USBD
3840
#elif defined(CH32V30x)
3941
#define CFG_TUSB_MCU OPT_MCU_CH32V307
4042
#endif
@@ -106,8 +108,8 @@ extern "C" {
106108
#define CFG_TUD_VIDEO_STREAMING_EP_BUFSIZE 256
107109

108110
// CDC FIFO size of TX and RX
109-
#define CFG_TUD_CDC_RX_BUFSIZE 256
110-
#define CFG_TUD_CDC_TX_BUFSIZE 256
111+
#define CFG_TUD_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 256)
112+
#define CFG_TUD_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 256)
111113

112114
// MSC Buffer size of Device Mass storage
113115
#define CFG_TUD_MSC_EP_BUFSIZE 512
@@ -121,17 +123,17 @@ extern "C" {
121123

122124
// Vendor FIFO size of TX and RX
123125
#ifndef CFG_TUD_VENDOR_RX_BUFSIZE
124-
#define CFG_TUD_VENDOR_RX_BUFSIZE 64
126+
#define CFG_TUD_VENDOR_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
125127
#endif
126128

127129
#ifndef CFG_TUD_VENDOR_TX_BUFSIZE
128-
#define CFG_TUD_VENDOR_TX_BUFSIZE 64
130+
#define CFG_TUD_VENDOR_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
129131
#endif
130132

131133
//--------------------------------------------------------------------
132134
// Host Configuration
133135
//--------------------------------------------------------------------
134-
136+
#if 0
135137
// Size of buffer to hold descriptors and other data used for enumeration
136138
#define CFG_TUH_ENUMERATION_BUFSIZE 256
137139

@@ -159,8 +161,8 @@ extern "C" {
159161
#define CFG_TUH_CDC_CH34X 1
160162

161163
// RX & TX fifo size
162-
#define CFG_TUH_CDC_RX_BUFSIZE 64
163-
#define CFG_TUH_CDC_TX_BUFSIZE 64
164+
#define CFG_TUH_CDC_RX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
165+
#define CFG_TUH_CDC_TX_BUFSIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
164166

165167
// Set Line Control state on enumeration/mounted:
166168
// DTR ( bit 0), RTS (bit 1)
@@ -171,6 +173,7 @@ extern "C" {
171173
// This need Pico-PIO-USB at least 0.5.1
172174
#define CFG_TUH_CDC_LINE_CODING_ON_ENUM \
173175
{ 115200, CDC_LINE_CONDING_STOP_BITS_1, CDC_LINE_CODING_PARITY_NONE, 8 }
176+
#endif
174177

175178
#ifdef __cplusplus
176179
}

src/arduino/video/Adafruit_USBD_Video.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@
3030

3131
#include "Adafruit_USBD_Video.h"
3232

33+
#define BULK_PACKET_SIZE (TUD_OPT_HIGH_SPEED ? 512 : 64)
34+
3335
Adafruit_USBD_Video::Adafruit_USBD_Video(void) {
3436
_vc_id = 0;
3537
memset(&_camera_terminal, 0, sizeof(_camera_terminal));
@@ -231,7 +233,7 @@ uint16_t Adafruit_USBD_Video::getInterfaceDescriptor(uint8_t itfnum_deprecated,
231233

232234
.bEndpointAddress = ep_in,
233235
.bmAttributes = {.xfer = TUSB_XFER_BULK, .sync = 0, .usage = 0},
234-
.wMaxPacketSize = 64,
236+
.wMaxPacketSize = BULK_PACKET_SIZE,
235237
.bInterval = 1}};
236238

237239
uint16_t const len_iad = sizeof(desc_iad);

0 commit comments

Comments
 (0)