Skip to content

Commit

Permalink
update tinyusb core for better ch32v103 support, but not working with…
Browse files Browse the repository at this point in the history
… v103 just yet
  • Loading branch information
hathach committed Jun 14, 2024
1 parent 468f720 commit 8cb4032
Show file tree
Hide file tree
Showing 7 changed files with 142 additions and 21 deletions.
43 changes: 34 additions & 9 deletions src/arduino/ports/ch32/Adafruit_TinyUSB_ch32.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,12 @@ USBWakeUp_IRQHandler(void) {
// USBFS
#if CFG_TUD_WCH_USBIP_USBFS

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

#if defined(CH32V10x)
#define USBHDWakeUp_IRQHandler USBWakeUp_IRQHandler
#endif

__attribute__((interrupt("WCH-Interrupt-fast"))) void USBHD_IRQHandler(void) {
tud_int_handler(0);
}
Expand Down Expand Up @@ -101,23 +106,43 @@ void yield(void) {
void TinyUSB_Port_InitDevice(uint8_t rhport) {
#if CFG_TUD_WCH_USBIP_FSDEV || CFG_TUD_WCH_USBIP_USBFS
// Full speed OTG or FSDev

#if defined(CH32V10x)
EXTEN->EXTEN_CTR |= EXTEN_USBHD_IO_EN;
EXTEN->EXTEN_CTR &= ~EXTEN_USB_5V_SEL;

#define RCC_AHBPeriph_OTG_FS RCC_AHBPeriph_USBHD
#endif

uint8_t usb_div;
switch (SystemCoreClock) {
#if defined(CH32V20x) || defined(CH32V30x)
case 48000000:
usb_div = 0; // div1
break;
usb_div = 0;
break; // div1
case 96000000:
usb_div = 1; // div2
break;
usb_div = 1;
break; // div2
case 144000000:
usb_div = 2; // div3
usb_div = 2;
break; // div3
#elif defined(CH32V10x)
case 48000000:
usb_div = RCC_USBCLKSource_PLLCLK_Div1;
break;
case 72000000:
usb_div = RCC_USBCLKSource_PLLCLK_1Div5;
break;
#endif
default:
return; // unsupported
}
// RCC_USBCLKConfig(usb_div) or RCC_OTGFSCLKConfig(usb_div)
RCC->CFGR0 &= ~(3 << 22);
RCC->CFGR0 |= usb_div << 22;

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

#if CFG_TUD_WCH_USBIP_FSDEV
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USB, ENABLE);
Expand Down
7 changes: 4 additions & 3 deletions src/arduino/ports/ch32/tusb_config_ch32.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,13 @@ extern "C" {
//--------------------------------------------------------------------
// COMMON CONFIGURATION
//--------------------------------------------------------------------
#if defined(CH32V20x)
#if defined(CH32V10x)
#define CFG_TUSB_MCU OPT_MCU_CH32V103
#warnning "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

#endif

#define CFG_TUSB_OS OPT_OS_NONE
Expand Down
9 changes: 9 additions & 0 deletions src/common/tusb_mcu.h
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,15 @@
#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_CH32V103)
#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

#elif TU_CHECK_MCU(OPT_MCU_CH32V20X)
// v20x support both FSDEV (USBD) and USBFS, default to FSDEV
#define TUP_USBIP_WCH_USBFS
Expand Down
16 changes: 13 additions & 3 deletions src/portable/st/stm32_fsdev/fsdev_ch32.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,21 @@

#include "common/tusb_compiler.h"

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

#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X
#if CFG_TUSB_MCU == OPT_MCU_CH32F20X
#include <ch32f20x.h>
#elif CFG_TUSB_MCU == OPT_MCU_CH32V20X
#include <ch32v20x.h>
#endif

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

#define FSDEV_PMA_SIZE (512u)
Expand Down
74 changes: 69 additions & 5 deletions src/portable/wch/ch32_usbfs_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,79 @@
#ifndef USB_CH32_USBFS_REG_H
#define USB_CH32_USBFS_REG_H

#if CFG_TUSB_MCU == OPT_MCU_CH32V307
#include <ch32v30x.h>
#define USBHD_IRQn OTG_FS_IRQn
// https://github.com/openwch/ch32v307/pull/90
// https://github.com/openwch/ch32v20x/pull/12
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#endif

#if CFG_TUSB_MCU == OPT_MCU_CH32F20X
#include <ch32f20x.h>
#elif CFG_TUSB_MCU == OPT_MCU_CH32V103
#include <ch32v10x.h>
typedef struct
{
__IO uint8_t BASE_CTRL;
__IO uint8_t UDEV_CTRL;
__IO uint8_t INT_EN;
__IO uint8_t DEV_ADDR;
__IO uint8_t Reserve0;
__IO uint8_t MIS_ST;
__IO uint8_t INT_FG;
__IO uint8_t INT_ST;
__IO uint32_t RX_LEN;
__IO uint8_t UEP4_1_MOD;
__IO uint8_t UEP2_3_MOD;
__IO uint8_t UEP5_6_MOD;
__IO uint8_t UEP7_MOD;
__IO uint32_t UEP0_DMA;
__IO uint32_t UEP1_DMA;
__IO uint32_t UEP2_DMA;
__IO uint32_t UEP3_DMA;
__IO uint32_t UEP4_DMA;
__IO uint32_t UEP5_DMA;
__IO uint32_t UEP6_DMA;
__IO uint32_t UEP7_DMA;
__IO uint16_t UEP0_TX_LEN;
__IO uint8_t UEP0_TX_CTRL;
__IO uint8_t UEP0_RX_CTRL;
__IO uint16_t UEP1_TX_LEN;
__IO uint8_t UEP1_TX_CTRL;
__IO uint8_t UEP1_RX_CTRL;
__IO uint16_t UEP2_TX_LEN;
__IO uint8_t UEP2_TX_CTRL;
__IO uint8_t UEP2_RX_CTRL;
__IO uint16_t UEP3_TX_LEN;
__IO uint8_t UEP3_TX_CTRL;
__IO uint8_t UEP3_RX_CTRL;
__IO uint16_t UEP4_TX_LEN;
__IO uint8_t UEP4_TX_CTRL;
__IO uint8_t UEP4_RX_CTRL;
__IO uint16_t UEP5_TX_LEN;
__IO uint8_t UEP5_TX_CTRL;
__IO uint8_t UEP5_RX_CTRL;
__IO uint16_t UEP6_TX_LEN;
__IO uint8_t UEP6_TX_CTRL;
__IO uint8_t UEP6_RX_CTRL;
__IO uint16_t UEP7_TX_LEN;
__IO uint8_t UEP7_TX_CTRL;
__IO uint8_t UEP7_RX_CTRL;
__IO uint32_t Reserve1;
__IO uint32_t OTG_CR;
__IO uint32_t OTG_SR;
} USBOTG_FS_TypeDef;

#define USBOTG_FS ((USBOTG_FS_TypeDef *) 0x40023400)
#elif CFG_TUSB_MCU == OPT_MCU_CH32V20X
#include <ch32v20x.h>
#elif CFG_TUSB_MCU == OPT_MCU_CH32V307
#include <ch32v30x.h>
#define USBHD_IRQn OTG_FS_IRQn
#endif

#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X
#include <ch32f20x.h>
#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif

// CTRL
Expand Down
12 changes: 12 additions & 0 deletions src/portable/wch/ch32_usbhs_reg.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,24 @@
#ifndef USB_CH32_USBHS_REG_H
#define USB_CH32_USBHS_REG_H

// https://github.com/openwch/ch32v307/pull/90
// https://github.com/openwch/ch32v20x/pull/12
#ifdef __GNUC__
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wstrict-prototypes"
#endif

#if CFG_TUSB_MCU == OPT_MCU_CH32V307
#include <ch32v30x.h>
#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X
#include <ch32f20x.h>
#endif

#ifdef __GNUC__
#pragma GCC diagnostic pop
#endif


/******************* GLOBAL ******************/

// USB CONTROL
Expand Down
2 changes: 1 addition & 1 deletion src/tusb_option.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@
#define OPT_MCU_CH32V307 2200 ///< WCH CH32V307
#define OPT_MCU_CH32F20X 2210 ///< WCH CH32F20x
#define OPT_MCU_CH32V20X 2220 ///< WCH CH32V20X

#define OPT_MCU_CH32V103 2230 ///< WCH CH32V103

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

0 comments on commit 8cb4032

Please sign in to comment.