diff --git a/src/arduino/ports/ch32/Adafruit_TinyUSB_ch32.cpp b/src/arduino/ports/ch32/Adafruit_TinyUSB_ch32.cpp index 00e06921..8fd9bea1 100644 --- a/src/arduino/ports/ch32/Adafruit_TinyUSB_ch32.cpp +++ b/src/arduino/ports/ch32/Adafruit_TinyUSB_ch32.cpp @@ -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); } @@ -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); diff --git a/src/arduino/ports/ch32/tusb_config_ch32.h b/src/arduino/ports/ch32/tusb_config_ch32.h index 3bb0eb6b..f8e2ef90 100644 --- a/src/arduino/ports/ch32/tusb_config_ch32.h +++ b/src/arduino/ports/ch32/tusb_config_ch32.h @@ -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 diff --git a/src/common/tusb_mcu.h b/src/common/tusb_mcu.h index ba04f460..eb49eddf 100644 --- a/src/common/tusb_mcu.h +++ b/src/common/tusb_mcu.h @@ -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 diff --git a/src/portable/st/stm32_fsdev/fsdev_ch32.h b/src/portable/st/stm32_fsdev/fsdev_ch32.h index 85fe3266..f63a80d5 100644 --- a/src/portable/st/stm32_fsdev/fsdev_ch32.h +++ b/src/portable/st/stm32_fsdev/fsdev_ch32.h @@ -36,11 +36,21 @@ #include "common/tusb_compiler.h" -#if CFG_TUSB_MCU == OPT_MCU_CH32V20X - #include +// 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 +#elif CFG_TUSB_MCU == OPT_MCU_CH32V20X + #include +#endif + +#ifdef __GNUC__ +#pragma GCC diagnostic pop #endif #define FSDEV_PMA_SIZE (512u) diff --git a/src/portable/wch/ch32_usbfs_reg.h b/src/portable/wch/ch32_usbfs_reg.h index 0a50a616..68be64f5 100644 --- a/src/portable/wch/ch32_usbfs_reg.h +++ b/src/portable/wch/ch32_usbfs_reg.h @@ -28,15 +28,79 @@ #ifndef USB_CH32_USBFS_REG_H #define USB_CH32_USBFS_REG_H -#if CFG_TUSB_MCU == OPT_MCU_CH32V307 - #include - #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 +#elif CFG_TUSB_MCU == OPT_MCU_CH32V103 + #include + 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 +#elif CFG_TUSB_MCU == OPT_MCU_CH32V307 + #include + #define USBHD_IRQn OTG_FS_IRQn +#endif -#elif CFG_TUSB_MCU == OPT_MCU_CH32F20X - #include +#ifdef __GNUC__ +#pragma GCC diagnostic pop #endif // CTRL diff --git a/src/portable/wch/ch32_usbhs_reg.h b/src/portable/wch/ch32_usbhs_reg.h index 130e4f22..87300b49 100644 --- a/src/portable/wch/ch32_usbhs_reg.h +++ b/src/portable/wch/ch32_usbhs_reg.h @@ -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 #elif CFG_TUSB_MCU == OPT_MCU_CH32F20X #include #endif +#ifdef __GNUC__ +#pragma GCC diagnostic pop +#endif + + /******************* GLOBAL ******************/ // USB CONTROL diff --git a/src/tusb_option.h b/src/tusb_option.h index b6f27b5d..611aaca3 100644 --- a/src/tusb_option.h +++ b/src/tusb_option.h @@ -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