1
0
mirror of https://github.com/RIOT-OS/RIOT.git synced 2024-12-29 04:50:03 +01:00
19256: pkg/tinyusb: add GD32VF103 support r=gschorcht a=gschorcht

### Contribution description

This PR provides the tinyUSB support for GD32VF103 and enables the `tinyusb_device` feature as well as `stdio_tinyusb_cdc_acm` for GD32VF103 boards.

### Testing procedure

```
BOARD=sipeeed-longan-nano make -C tests/shell flash term
```
should work

### Issues/PRs references


Co-authored-by: Gunar Schorcht <gunar@schorcht.net>
This commit is contained in:
bors[bot] 2023-02-21 16:53:06 +00:00 committed by GitHub
commit 37b64912d4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
30 changed files with 1824 additions and 2 deletions

View File

@ -0,0 +1,63 @@
/*
* Copyright (C) 2023 Gunar Schorcht <gunar@schorcht.net>
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup boards_common_gd32v
* @{
*
* @file
* @brief Default USB OTG configuration for GD32 RISC-V board
*
* @author Gunar Schorcht <gunar@schorcht.net>
*/
#ifndef CFG_USBDEV_DEFAULT_H
#define CFG_USBDEV_DEFAULT_H
#include "vendor/usbdev_gd32v.h"
#include "usbdev_synopsys_dwc2.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @name USB OTG configuration
* @{
*/
/**
* @brief Enable the full speed USB OTG peripheral
*/
#define DWC2_USB_OTG_FS_ENABLED
/**
* @brief Common USB OTG FS configuration
*/
static const dwc2_usb_otg_fshs_config_t dwc2_usb_otg_fshs_config[] = {
{
.periph = USB_OTG_FS_PERIPH_BASE,
.type = DWC2_USB_OTG_FS,
.phy = DWC2_USB_OTG_PHY_BUILTIN,
.rcu_mask = RCU_AHBEN_USBFSEN_Msk,
.bus = AHB,
.irqn = USBFS_IRQn,
}
};
/**
* @brief Number of available USB OTG peripherals
*/
#define USBDEV_NUMOF ARRAY_SIZE(dwc2_usb_otg_fshs_config)
/** @} */
#ifdef __cplusplus
}
#endif
#endif /* CFG_USBDEV_DEFAULT_H */
/** @} */

View File

@ -28,7 +28,14 @@
extern "C" {
#endif
#if defined(MODULE_USBDEV_SYNOPSYS_DWC2) || defined(MODULE_TINYUSB_DEVICE)
/* If the USB OTG peripheral is used, the USB clock of 48 MHz is derived by
* dividing the PLL clock by 1, 1.5, 2, or 2.5. That is the maximum core clock
* frequency can be 96 MHz. */
#define CLOCK_CORECLOCK MHZ(96) /**< CPU clock frequency in Hz */
#else
#define CLOCK_CORECLOCK MHZ(108) /**< CPU clock frequency in Hz */
#endif
#define CLOCK_AHB CLOCK_CORECLOCK /**< Equal to the CPU clock */
#define CLOCK_APB1 CLOCK_AHB/2 /**< Half AHB clock */

View File

@ -14,12 +14,20 @@ config BOARD_SEEEDSTUDIO_GD32
select CPU_MODEL_GD32VF103VBT6
select BOARD_HAS_HXTAL
select BOARD_HAS_LXTAL
select HAS_HIGHLEVEL_STDIO
select HAS_PERIPH_ADC
select HAS_PERIPH_I2C
select HAS_PERIPH_PWM
select HAS_PERIPH_SPI
select HAS_PERIPH_TIMER
select HAS_PERIPH_UART
select HAS_TINYUSB_DEVICE
select HAVE_SAUL_GPIO
select PACKAGE_TINYUSB if TEST_KCONFIG
choice STDIO_IMPLEMENTATION
default MODULE_STDIO_TINYUSB_CDC_ACM if PACKAGE_TINYUSB
endchoice
source "$(RIOTBOARD)/common/gd32v/Kconfig"

View File

@ -2,4 +2,5 @@ ifneq (,$(filter saul_default,$(USEMODULE)))
USEMODULE += saul_gpio
endif
include $(RIOTBOARD)/common/makefiles/stdio_tinyusb_cdc_acm.dep.mk
include $(RIOTBOARD)/common/gd32v/Makefile.dep

View File

@ -8,4 +8,8 @@ FEATURES_PROVIDED += periph_spi
FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart
include $(RIOTBOARD)/common/gd32v/Makefile.features
# Other features provided by the board (in alphabetical order)
FEATURES_PROVIDED += highlevel_stdio
FEATURES_PROVIDED += tinyusb_device
include $(RIOTBOARD)/common/gd32v/Makefile.features

View File

@ -1 +1,2 @@
PORT_LINUX ?= /dev/ttyACM0
include $(RIOTBOARD)/common/gd32v/Makefile.include

View File

@ -44,6 +44,7 @@
#include "cfg_spi_default.h"
#include "cfg_timer_default.h"
#include "cfg_uart_default.h"
#include "cfg_usbdev_default.h"
#ifdef __cplusplus
extern "C" {

View File

@ -14,6 +14,7 @@ config BOARD_SIPEED_LONGAN_NANO
select CPU_MODEL_GD32VF103CBT6
select BOARD_HAS_HXTAL
select BOARD_HAS_LXTAL
select HAS_HIGHLEVEL_STDIO
select HAS_PERIPH_ADC
select HAS_PERIPH_DAC
select HAS_PERIPH_I2C
@ -21,10 +22,12 @@ config BOARD_SIPEED_LONGAN_NANO
select HAS_PERIPH_SPI
select HAS_PERIPH_TIMER
select HAS_PERIPH_UART
select HAS_TINYUSB_DEVICE
select HAVE_SAUL_GPIO
select HAVE_MTD_SDCARD_DEFAULT
select MODULE_FATFS_VFS if MODULE_VFS_DEFAULT
select PACKAGE_TINYUSB if TEST_KCONFIG
menu "Sipeed Longan Nano Board Configuration"
@ -35,4 +38,8 @@ menu "Sipeed Longan Nano Board Configuration"
endmenu
choice STDIO_IMPLEMENTATION
default MODULE_STDIO_TINYUSB_CDC_ACM if PACKAGE_TINYUSB
endchoice
source "$(RIOTBOARD)/common/gd32v/Kconfig"

View File

@ -12,4 +12,5 @@ ifneq (,$(filter vfs_default,$(USEMODULE)))
USEMODULE += mtd
endif
include $(RIOTBOARD)/common/makefiles/stdio_tinyusb_cdc_acm.dep.mk
include $(RIOTBOARD)/common/gd32v/Makefile.dep

View File

@ -10,4 +10,8 @@ FEATURES_PROVIDED += periph_timer
FEATURES_PROVIDED += periph_uart
FEATURES_PROVIDED += sdcard_spi
# Other features provided by the board (in alphabetical order)
FEATURES_PROVIDED += highlevel_stdio
FEATURES_PROVIDED += tinyusb_device
include $(RIOTBOARD)/common/gd32v/Makefile.features

View File

@ -1,2 +1,3 @@
PORT_LINUX ?= /dev/ttyACM0
PROGRAMMER ?= dfu-util
include $(RIOTBOARD)/common/gd32v/Makefile.include

View File

@ -44,6 +44,7 @@
#include "cfg_spi_default.h"
#include "cfg_timer_default.h"
#include "cfg_uart_default.h"
#include "cfg_usbdev_default.h"
#ifdef __cplusplus
extern "C" {

1576
cpu/gd32v/include/vendor/usbdev_gd32v.h vendored Normal file

File diff suppressed because it is too large Load Diff

View File

@ -101,6 +101,11 @@ typedef struct {
* register in STM32 Reference Manual */
#endif /* defined(MODULE_PERIPH_USBDEV_HS_UTMI) */
#endif /* defined(MCU_STM32) || DOXYGEN */
#if defined(MCU_GD32V)
uint32_t rcu_mask; /**< bit in clock enable register */
uint8_t irqn; /**< IRQ channel */
uint8_t bus; /**< Peripheral bus */
#endif
} dwc2_usb_otg_fshs_config_t;
#ifdef __cplusplus

View File

@ -44,7 +44,6 @@ endif
config MODULE_PERIPH_USBDEV_CLK
bool
depends on HAS_PERIPH_USBDEV
help
Enable the USB device specific clock settings, if there are any

View File

@ -29,6 +29,8 @@ BOARD_INSUFFICIENT_MEMORY := \
samd10-xmini \
saml10-xpro \
saml11-xpro \
seeedstudio-gd32 \
sipeed-longan-nano \
slstk3400a \
stk3200 \
stm32f030f4-demo \

View File

@ -23,6 +23,8 @@ BOARD_INSUFFICIENT_MEMORY := \
samd10-xmini \
saml10-xpro \
saml11-xpro \
seeedstudio-gd32 \
sipeed-longan-nano \
slstk3400a \
stk3200 \
stm32f030f4-demo \

View File

@ -28,6 +28,7 @@ menuconfig PACKAGE_TINYUSB
select MODULE_TINYUSB_CONTRIB
select MODULE_TINYUSB_HW
select MODULE_TINYUSB_PORTABLE_ESPRESSIF if CPU_FAM_ESP32S2 || CPU_FAM_ESP32S3
select MODULE_TINYUSB_PORTABLE_SYNOPSYS_DWC2 if CPU_FAM_GD32V
# Whole STM32 families F2, F4, F7 and U5 use the Synopsys DWC2 USB OTG core
select MODULE_TINYUSB_PORTABLE_SYNOPSYS_DWC2 if HAS_CPU_STM32F2
select MODULE_TINYUSB_PORTABLE_SYNOPSYS_DWC2 if HAS_CPU_STM32F4

View File

@ -115,6 +115,8 @@ else ifeq (stm32,$(CPU))
# all other STM32 with USB interface use USB FS Device core
USEMODULE += tinyusb_portable_stm32_fsdev
endif
else ifeq (gd32v,$(CPU))
USEMODULE += tinyusb_portable_synopsys_dwc2
endif
# other module dependencies

View File

@ -30,6 +30,8 @@ else ifeq (samd21,$(CPU))
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_SAMD21
else ifeq (samd5x,$(CPU))
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_SAME5X
else ifeq (gd32v,$(CPU))
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_GD32VF103
else
$(error CPU $(CPU) or CPU family $(CPU_FAM) not supported)
endif

116
pkg/tinyusb/hw/hw_gd32v.c Normal file
View File

@ -0,0 +1,116 @@
/*
* Copyright (C) 2023 Gunar Schorcht
*
* This file is subject to the terms and conditions of the GNU Lesser
* General Public License v2.1. See the file LICENSE in the top level
* directory for more details.
*/
/**
* @ingroup pkg_tinyusb
* @brief
* @{
*
* @brief tinyUSB hardware driver for GD32VF103 MCUs
* @author Gunar Schorcht <gunar@schorcht.net>
*/
#include <errno.h>
#include "periph_conf.h"
#include "periph/gpio.h"
#include "pm_layered.h"
#include "tusb.h"
#include "device/usbd.h"
#include "host/usbh.h"
#define RCU_CFG0_SCS_PLL (2UL << RCU_CFG0_SCS_Pos) /* PLL used */
#define USB_OTG_GCCFG_PWRON (1UL << 16) /* Power on */
#define USB_OTG_GCCFG_VBUSBCEN (1UL << 19) /* VBUS B-device comparer enable */
#define USB_OTG_GCCFG_VBUSIG (1UL << 21) /* VBUS detection ignore */
/* Although the following defines are done in `vendor/gd32vf103_rcu.h`, they
* have to be defined here since `vendor/gd32vf103_rcu.h` can't be included due
* to type conflicts with `vendor/gd32vf103_periph.h`. */
#define RCU_CKUSB_CKPLL_DIV1_5 (0UL << 22) /* USB clock is PLL clock/1.5 */
#define RCU_CKUSB_CKPLL_DIV1 (1UL << 22) /* USB clock is PLL clock */
#define RCU_CKUSB_CKPLL_DIV2_5 (2UL << 22) /* USB clock is PLL clock/2.5 */
#define RCU_CKUSB_CKPLL_DIV2 (3UL << 22) /* USB clock is PLL clock/2 */
static int tinyusb_hw_init_dev(const dwc2_usb_otg_fshs_config_t *conf)
{
/* Block both DEEP_SLEEP and STANDBY, TODO DEEP_SLEEP is unblocked during
* USB suspend status */
pm_block(GD32V_PM_DEEPSLEEP);
pm_block(GD32V_PM_STANDBY);
/* ensure that PLL clock is used */
assert((RCU->CFG0 & RCU_CFG0_SCS_Msk) == RCU_CFG0_SCS_PLL);
/* ensure that the 48 MHz USB clock can be generated */
assert((CLOCK_CORECLOCK == MHZ(48)) || (CLOCK_CORECLOCK == MHZ(72)) ||
(CLOCK_CORECLOCK == MHZ(96)) || (CLOCK_CORECLOCK == MHZ(120)));
/* divide the core clock to get 48 MHz USB clock */
RCU->CFG0 &= ~RCU_CFG0_USBFSPSC_Msk;
switch (CLOCK_CORECLOCK) {
case MHZ(48):
RCU->CFG0 |= RCU_CKUSB_CKPLL_DIV1;
break;
case MHZ(72):
RCU->CFG0 |= RCU_CKUSB_CKPLL_DIV1_5;
break;
case MHZ(96):
RCU->CFG0 |= RCU_CKUSB_CKPLL_DIV2;
break;
case MHZ(120):
RCU->CFG0 |= RCU_CKUSB_CKPLL_DIV2_5;
break;
}
/* enable USB OTG clock */
periph_clk_en(conf->bus, conf->rcu_mask);
/* reset the USB OTG peripheral */
RCU->AHBRST |= RCU_AHBRST_USBFSRST_Msk;
RCU->AHBRST &= ~RCU_AHBRST_USBFSRST_Msk;
void isr_otg_fs(unsigned irq);
clic_set_handler(conf->irqn, isr_otg_fs);
clic_enable_interrupt(conf->irqn, CPU_DEFAULT_IRQ_PRIO);
USB_OTG_GlobalTypeDef *global_regs =
(USB_OTG_GlobalTypeDef *)(conf->periph + USB_OTG_GLOBAL_BASE);
/* disable VBUS sensing */
global_regs->GCCFG |= USB_OTG_GCCFG_PWRON |
USB_OTG_GCCFG_VBUSIG |
USB_OTG_GCCFG_VBUSBCEN;
return 0;
}
int tinyusb_hw_init(void)
{
for (unsigned i = 0; i < USBDEV_NUMOF; i++) {
if (tinyusb_hw_init_dev(&dwc2_usb_otg_fshs_config[i]) != 0) {
return -ENODEV;
}
}
return 0;
}
void isr_otg_fs(unsigned irq)
{
(void)irq;
/* call device interrupt handler with the first device */
if (IS_USED(MODULE_TINYUSB_DEVICE)) {
tud_int_handler(TINYUSB_TUD_RHPORT);
}
/* call host interrupt handler with the first device */
if (IS_USED(MODULE_TINYUSB_HOST)) {
tuh_int_handler(TINYUSB_TUH_RHPORT);
}
}

View File

@ -21,6 +21,8 @@ BOARD_INSUFFICIENT_MEMORY := \
samd10-xmini \
saml10-xpro \
saml11-xpro \
seeedstudio-gd32 \
sipeed-longan-nano \
slstk3400a \
stk3200 \
stm32f030f4-demo \

View File

@ -33,6 +33,8 @@ BOARD_INSUFFICIENT_MEMORY := \
samd10-xmini \
saml10-xpro \
saml11-xpro \
seeedstudio-gd32 \
sipeed-longan-nano \
slstk3400a \
stk3200 \
stm32f030f4-demo \

View File

@ -22,6 +22,8 @@ BOARD_INSUFFICIENT_MEMORY := \
nucleo-l031k6 \
nucleo-l053r8 \
samd10-xmini \
seeedstudio-gd32 \
sipeed-longan-nano \
slstk3400a \
stk3200 \
stm32f030f4-demo \

View File

@ -27,6 +27,8 @@ BOARD_INSUFFICIENT_MEMORY := \
samd10-xmini \
saml10-xpro \
saml11-xpro \
seeedstudio-gd32 \
sipeed-longan-nano \
stm32f7508-dk \
waspmote-pro \
zigduino \

View File

@ -15,6 +15,8 @@ BOARD_INSUFFICIENT_MEMORY := \
samd10-xmini \
saml10-xpro \
saml11-xpro \
seeedstudio-gd32 \
sipeed-longan-nano \
slstk3400a \
spark-core \
stk3200 \

View File

@ -32,9 +32,11 @@ BOARD_INSUFFICIENT_MEMORY := \
samd10-xmini \
saml10-xpro \
saml11-xpro \
seeedstudio-gd32 \
seeeduino_xiao \
sensebox_samd21 \
serpente \
sipeed-longan-nano \
slstk3400a \
sodaq-autonomo \
sodaq-explorer \

View File

@ -15,6 +15,8 @@ BOARD_INSUFFICIENT_MEMORY := \
samd10-xmini \
saml10-xpro \
saml11-xpro \
seeedstudio-gd32 \
sipeed-longan-nano \
slstk3400a \
spark-core \
stk3200 \

View File

@ -13,6 +13,8 @@ BOARD_BLACKLIST += \
mulle \
pinetime \
ruuvitag \
seeedstudio-gd32 \
sipeed-longan-nano \
stm32f429i-disco \
stm32f4discovery \
thingy52 \

View File

@ -14,6 +14,8 @@ BOARD_BLACKLIST += \
mulle \
pinetime \
ruuvitag \
seeedstudio-gd32 \
sipeed-longan-nano \
stm32f429i-disco \
stm32f4discovery \
thingy52 \