Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add USB Support for CH32L103 (But not work) #457

Closed
wants to merge 17 commits into from
30 changes: 26 additions & 4 deletions src/arduino/ports/ch32/Adafruit_TinyUSB_ch32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@
#include "tusb_option.h"

#if CFG_TUD_ENABLED && \
(defined(ARDUINO_ARCH_CH32) || defined(CH32V20x) || defined(CH32V30x))
(defined(ARDUINO_ARCH_CH32) || defined(CH32V20x) || defined(CH32V30x) || \
defined(CH32X035) || defined(CH32L10x))

#include "Arduino.h"
#include "arduino/Adafruit_USBD_Device.h"
Expand Down Expand Up @@ -60,10 +61,13 @@ USBWakeUp_IRQHandler(void) {
// USBFS
#if CFG_TUD_WCH_USBIP_USBFS

#if defined(CH32V10x) || defined(CH32V20x)
#if defined(CH32V10x) || defined(CH32V20x) || defined(CH32L10x)

#if defined(CH32V10x)
#define USBHDWakeUp_IRQHandler USBWakeUp_IRQHandler
#elif defined(CH32X035) || defined(CH32L10x)
#define USBHD_IRQHandler USBFS_IRQHandler
#define USBHDWakeUp_IRQHandler USBFSWakeUp_IRQHandler
#endif

__attribute__((interrupt("WCH-Interrupt-fast"))) void USBHD_IRQHandler(void) {
Expand Down Expand Up @@ -112,9 +116,14 @@ void TinyUSB_Port_InitDevice(uint8_t rhport) {
EXTEN->EXTEN_CTR &= ~EXTEN_USB_5V_SEL;

#define RCC_AHBPeriph_OTG_FS RCC_AHBPeriph_USBHD
#elif defined(CH32X035)
#define RCC_AHBPeriph_OTG_FS RCC_AHBPeriph_USBFS
#elif defined(CH32L10x)
#define RCC_AHBPeriph_OTG_FS RCC_HBPeriph_USBFS
#define RCC_AHBPeriphClockCmd RCC_HBPeriphClockCmd
#endif

uint8_t usb_div;
uint32_t usb_div;
switch (SystemCoreClock) {
#if defined(CH32V20x) || defined(CH32V30x)
case 48000000:
Expand All @@ -133,14 +142,27 @@ void TinyUSB_Port_InitDevice(uint8_t rhport) {
case 72000000:
usb_div = RCC_USBCLKSource_PLLCLK_1Div5;
break;
#elif defined(CH32X035)
case 48000000:
break;
#elif defined(CH32L10x)
case 48000000:
usb_div = RCC_USBCLKSource_PLLCLK_Div1;
break;
case 72000000:
usb_div = RCC_USBCLKSource_PLLCLK_Div1_5;
break;
case 96000000:
usb_div = RCC_USBCLKSource_PLLCLK_Div2;
break;
#endif
default:
return; // unsupported
}

#if defined(CH32V30x)
RCC_OTGFSCLKConfig(usb_div);
#else
#elif !defined(CH32X035)
RCC_USBCLKConfig(usb_div);
#endif

Expand Down
7 changes: 6 additions & 1 deletion src/arduino/ports/ch32/tusb_config_ch32.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,11 +34,16 @@ extern "C" {
//--------------------------------------------------------------------
#if defined(CH32V10x)
#define CFG_TUSB_MCU OPT_MCU_CH32V103
#warnning "CH32v103 is not working yet"
#warning "CH32V103 is not working yet"
#elif defined(CH32V20x)
#define CFG_TUSB_MCU OPT_MCU_CH32V20X
#elif defined(CH32V30x)
#define CFG_TUSB_MCU OPT_MCU_CH32V307
#elif defined(CH32X035)
#define CFG_TUSB_MCU OPT_MCU_CH32X035
#elif defined(CH32L10x)
#define CFG_TUSB_MCU OPT_MCU_CH32L10X
#warning "CH32L103 is not working yet"
#endif

#define CFG_TUSB_OS OPT_OS_NONE
Expand Down
10 changes: 10 additions & 0 deletions src/common/tusb_mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -473,6 +473,16 @@
#define TUP_RHPORT_HIGHSPEED CFG_TUD_WCH_USBIP_USBHS
#define TUP_DCD_ENDPOINT_MAX (CFG_TUD_WCH_USBIP_USBHS ? 16 : 8)

#elif TU_CHECK_MCU(OPT_MCU_CH32X035) || TU_CHECK_MCU(OPT_MCU_CH32L10X)
// x035 and l103 support USBFS only
#define TUP_USBIP_WCH_USBFS

#if !defined(CFG_TUD_WCH_USBIP_USBFS)
#define CFG_TUD_WCH_USBIP_USBFS 1
#endif

#define TUP_DCD_ENDPOINT_MAX 8

//--------------------------------------------------------------------+
// Analog Devices
//--------------------------------------------------------------------+
Expand Down
8 changes: 8 additions & 0 deletions src/portable/wch/ch32_usbfs_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,14 @@
#elif CFG_TUSB_MCU == OPT_MCU_CH32V307
#include <ch32v30x.h>
#define USBHD_IRQn OTG_FS_IRQn
#elif CFG_TUSB_MCU == OPT_MCU_CH32X035
#include <ch32x035.h>
#define USBOTG_FS USBFSD
#define USBHD_IRQn USBFS_IRQn
#elif CFG_TUSB_MCU == OPT_MCU_CH32L10X
#include <ch32l103.h>
#define USBOTG_FS USBFSD
#define USBHD_IRQn USBFS_IRQn
#endif

#ifdef __GNUC__
Expand Down
4 changes: 3 additions & 1 deletion src/tusb_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,10 @@
// WCH
#define OPT_MCU_CH32V307 2200 ///< WCH CH32V307
#define OPT_MCU_CH32F20X 2210 ///< WCH CH32F20x
#define OPT_MCU_CH32V20X 2220 ///< WCH CH32V20X
#define OPT_MCU_CH32V20X 2220 ///< WCH CH32V20x
#define OPT_MCU_CH32V103 2230 ///< WCH CH32V103
#define OPT_MCU_CH32X035 2240 ///< WCH CH32X035
#define OPT_MCU_CH32L10X 2250 ///< WCH CH32L10x

// NXP LPC MCX
#define OPT_MCU_MCXN9 2300 ///< NXP MCX N9 Series
Expand Down