Skip to content

Commit 8cb4032

Browse files
committed
update tinyusb core for better ch32v103 support, but not working with v103 just yet
1 parent 468f720 commit 8cb4032

File tree

7 files changed

+142
-21
lines changed

7 files changed

+142
-21
lines changed

src/arduino/ports/ch32/Adafruit_TinyUSB_ch32.cpp

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,12 @@ USBWakeUp_IRQHandler(void) {
6060
// USBFS
6161
#if CFG_TUD_WCH_USBIP_USBFS
6262

63-
#ifdef CH32V20x
63+
#if defined(CH32V10x) || defined(CH32V20x)
64+
65+
#if defined(CH32V10x)
66+
#define USBHDWakeUp_IRQHandler USBWakeUp_IRQHandler
67+
#endif
68+
6469
__attribute__((interrupt("WCH-Interrupt-fast"))) void USBHD_IRQHandler(void) {
6570
tud_int_handler(0);
6671
}
@@ -101,23 +106,43 @@ void yield(void) {
101106
void TinyUSB_Port_InitDevice(uint8_t rhport) {
102107
#if CFG_TUD_WCH_USBIP_FSDEV || CFG_TUD_WCH_USBIP_USBFS
103108
// 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+
104117
uint8_t usb_div;
105118
switch (SystemCoreClock) {
119+
#if defined(CH32V20x) || defined(CH32V30x)
106120
case 48000000:
107-
usb_div = 0; // div1
108-
break;
121+
usb_div = 0;
122+
break; // div1
109123
case 96000000:
110-
usb_div = 1; // div2
111-
break;
124+
usb_div = 1;
125+
break; // div2
112126
case 144000000:
113-
usb_div = 2; // div3
127+
usb_div = 2;
128+
break; // div3
129+
#elif defined(CH32V10x)
130+
case 48000000:
131+
usb_div = RCC_USBCLKSource_PLLCLK_Div1;
132+
break;
133+
case 72000000:
134+
usb_div = RCC_USBCLKSource_PLLCLK_1Div5;
114135
break;
136+
#endif
115137
default:
116138
return; // unsupported
117139
}
118-
// RCC_USBCLKConfig(usb_div) or RCC_OTGFSCLKConfig(usb_div)
119-
RCC->CFGR0 &= ~(3 << 22);
120-
RCC->CFGR0 |= usb_div << 22;
140+
141+
#if defined(CH32V30x)
142+
RCC_OTGFSCLKConfig(usb_div);
143+
#else
144+
RCC_USBCLKConfig(usb_div);
145+
#endif
121146

122147
#if CFG_TUD_WCH_USBIP_FSDEV
123148
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);

src/arduino/ports/ch32/tusb_config_ch32.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,13 @@ 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-
3840
#elif defined(CH32V30x)
3941
#define CFG_TUSB_MCU OPT_MCU_CH32V307
40-
4142
#endif
4243

4344
#define CFG_TUSB_OS OPT_OS_NONE

src/common/tusb_mcu.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -420,6 +420,15 @@
420420
#define TUP_RHPORT_HIGHSPEED CFG_TUD_WCH_USBIP_USBHS
421421
#define TUP_DCD_ENDPOINT_MAX (CFG_TUD_WCH_USBIP_USBHS ? 16 : 8)
422422

423+
#elif TU_CHECK_MCU(OPT_MCU_CH32V103)
424+
#define TUP_USBIP_WCH_USBFS
425+
426+
#if !defined(CFG_TUD_WCH_USBIP_USBFS)
427+
#define CFG_TUD_WCH_USBIP_USBFS 1
428+
#endif
429+
430+
#define TUP_DCD_ENDPOINT_MAX 8
431+
423432
#elif TU_CHECK_MCU(OPT_MCU_CH32V20X)
424433
// v20x support both FSDEV (USBD) and USBFS, default to FSDEV
425434
#define TUP_USBIP_WCH_USBFS

src/portable/st/stm32_fsdev/fsdev_ch32.h

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,21 @@
3636

3737
#include "common/tusb_compiler.h"
3838

39-
#if CFG_TUSB_MCU == OPT_MCU_CH32V20X
40-
#include <ch32v20x.h>
39+
// https://github.com/openwch/ch32v307/pull/90
40+
// https://github.com/openwch/ch32v20x/pull/12
41+
#ifdef __GNUC__
42+
#pragma GCC diagnostic push
43+
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
44+
#endif
4145

42-
#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X
46+
#if CFG_TUSB_MCU == OPT_MCU_CH32F20X
4347
#include <ch32f20x.h>
48+
#elif CFG_TUSB_MCU == OPT_MCU_CH32V20X
49+
#include <ch32v20x.h>
50+
#endif
51+
52+
#ifdef __GNUC__
53+
#pragma GCC diagnostic pop
4454
#endif
4555

4656
#define FSDEV_PMA_SIZE (512u)

src/portable/wch/ch32_usbfs_reg.h

Lines changed: 69 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,15 +28,79 @@
2828
#ifndef USB_CH32_USBFS_REG_H
2929
#define USB_CH32_USBFS_REG_H
3030

31-
#if CFG_TUSB_MCU == OPT_MCU_CH32V307
32-
#include <ch32v30x.h>
33-
#define USBHD_IRQn OTG_FS_IRQn
31+
// https://github.com/openwch/ch32v307/pull/90
32+
// https://github.com/openwch/ch32v20x/pull/12
33+
#ifdef __GNUC__
34+
#pragma GCC diagnostic push
35+
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
36+
#endif
37+
38+
#if CFG_TUSB_MCU == OPT_MCU_CH32F20X
39+
#include <ch32f20x.h>
40+
#elif CFG_TUSB_MCU == OPT_MCU_CH32V103
41+
#include <ch32v10x.h>
42+
typedef struct
43+
{
44+
__IO uint8_t BASE_CTRL;
45+
__IO uint8_t UDEV_CTRL;
46+
__IO uint8_t INT_EN;
47+
__IO uint8_t DEV_ADDR;
48+
__IO uint8_t Reserve0;
49+
__IO uint8_t MIS_ST;
50+
__IO uint8_t INT_FG;
51+
__IO uint8_t INT_ST;
52+
__IO uint32_t RX_LEN;
53+
__IO uint8_t UEP4_1_MOD;
54+
__IO uint8_t UEP2_3_MOD;
55+
__IO uint8_t UEP5_6_MOD;
56+
__IO uint8_t UEP7_MOD;
57+
__IO uint32_t UEP0_DMA;
58+
__IO uint32_t UEP1_DMA;
59+
__IO uint32_t UEP2_DMA;
60+
__IO uint32_t UEP3_DMA;
61+
__IO uint32_t UEP4_DMA;
62+
__IO uint32_t UEP5_DMA;
63+
__IO uint32_t UEP6_DMA;
64+
__IO uint32_t UEP7_DMA;
65+
__IO uint16_t UEP0_TX_LEN;
66+
__IO uint8_t UEP0_TX_CTRL;
67+
__IO uint8_t UEP0_RX_CTRL;
68+
__IO uint16_t UEP1_TX_LEN;
69+
__IO uint8_t UEP1_TX_CTRL;
70+
__IO uint8_t UEP1_RX_CTRL;
71+
__IO uint16_t UEP2_TX_LEN;
72+
__IO uint8_t UEP2_TX_CTRL;
73+
__IO uint8_t UEP2_RX_CTRL;
74+
__IO uint16_t UEP3_TX_LEN;
75+
__IO uint8_t UEP3_TX_CTRL;
76+
__IO uint8_t UEP3_RX_CTRL;
77+
__IO uint16_t UEP4_TX_LEN;
78+
__IO uint8_t UEP4_TX_CTRL;
79+
__IO uint8_t UEP4_RX_CTRL;
80+
__IO uint16_t UEP5_TX_LEN;
81+
__IO uint8_t UEP5_TX_CTRL;
82+
__IO uint8_t UEP5_RX_CTRL;
83+
__IO uint16_t UEP6_TX_LEN;
84+
__IO uint8_t UEP6_TX_CTRL;
85+
__IO uint8_t UEP6_RX_CTRL;
86+
__IO uint16_t UEP7_TX_LEN;
87+
__IO uint8_t UEP7_TX_CTRL;
88+
__IO uint8_t UEP7_RX_CTRL;
89+
__IO uint32_t Reserve1;
90+
__IO uint32_t OTG_CR;
91+
__IO uint32_t OTG_SR;
92+
} USBOTG_FS_TypeDef;
3493

94+
#define USBOTG_FS ((USBOTG_FS_TypeDef *) 0x40023400)
3595
#elif CFG_TUSB_MCU == OPT_MCU_CH32V20X
3696
#include <ch32v20x.h>
97+
#elif CFG_TUSB_MCU == OPT_MCU_CH32V307
98+
#include <ch32v30x.h>
99+
#define USBHD_IRQn OTG_FS_IRQn
100+
#endif
37101

38-
#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X
39-
#include <ch32f20x.h>
102+
#ifdef __GNUC__
103+
#pragma GCC diagnostic pop
40104
#endif
41105

42106
// CTRL

src/portable/wch/ch32_usbhs_reg.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,24 @@
2828
#ifndef USB_CH32_USBHS_REG_H
2929
#define USB_CH32_USBHS_REG_H
3030

31+
// https://github.com/openwch/ch32v307/pull/90
32+
// https://github.com/openwch/ch32v20x/pull/12
33+
#ifdef __GNUC__
34+
#pragma GCC diagnostic push
35+
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
36+
#endif
37+
3138
#if CFG_TUSB_MCU == OPT_MCU_CH32V307
3239
#include <ch32v30x.h>
3340
#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X
3441
#include <ch32f20x.h>
3542
#endif
3643

44+
#ifdef __GNUC__
45+
#pragma GCC diagnostic pop
46+
#endif
47+
48+
3749
/******************* GLOBAL ******************/
3850

3951
// USB CONTROL

src/tusb_option.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@
182182
#define OPT_MCU_CH32V307 2200 ///< WCH CH32V307
183183
#define OPT_MCU_CH32F20X 2210 ///< WCH CH32F20x
184184
#define OPT_MCU_CH32V20X 2220 ///< WCH CH32V20X
185-
185+
#define OPT_MCU_CH32V103 2230 ///< WCH CH32V103
186186

187187
// NXP LPC MCX
188188
#define OPT_MCU_MCXN9 2300 ///< NXP MCX N9 Series

0 commit comments

Comments
 (0)