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

pkg/tinyusb: add STM32 USB FS device driver #18786

Merged
merged 3 commits into from
Oct 26, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions boards/nucleo-f303ze/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,6 @@ config BOARD_NUCLEO_F303ZE

# Put other features for this board (in alphabetical order)
select HAS_RIOTBOOT
select HAS_TINYUSB_DEVICE

source "$(RIOTBOARD)/common/nucleo144/Kconfig"
1 change: 1 addition & 0 deletions boards/nucleo-f303ze/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ FEATURES_PROVIDED += periph_usbdev

# Put other features for this board (in alphabetical order)
FEATURES_PROVIDED += riotboot
FEATURES_PROVIDED += tinyusb_device

# load the common Makefile.features for Nucleo 144 boards
include $(RIOTBOARD)/common/nucleo144/Makefile.features
1 change: 1 addition & 0 deletions boards/p-nucleo-wb55/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ config BOARD_P_NUCLEO_WB55
# Put other features for this board (in alphabetical order)
select HAS_ARDUINO
select HAS_RIOTBOOT
select HAS_TINYUSB_DEVICE

# Clock configuration
select BOARD_HAS_HSE
Expand Down
1 change: 1 addition & 0 deletions boards/p-nucleo-wb55/Makefile.features
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ FEATURES_PROVIDED += periph_usbdev
# Put other features for this board (in alphabetical order)
FEATURES_PROVIDED += arduino
FEATURES_PROVIDED += riotboot
FEATURES_PROVIDED += tinyusb_device
10 changes: 5 additions & 5 deletions pkg/tinyusb/Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ menuconfig PACKAGE_TINYUSB
select MODULE_TINYUSB_PORTABLE_SYNOPSYS_DWC2 if CPU_STM32 && CPU_FAM_F7
select MODULE_TINYUSB_PORTABLE_SYNOPSYS_DWC2 if CPU_STM32 && CPU_FAM_H7
select MODULE_TINYUSB_PORTABLE_SYNOPSYS_DWC2 if CPU_STM32 && CPU_FAM_L4
select MODULE_TINYUSB_PORTABLE_STM32_FSEDV if CPU_STM32 && CPU_FAM_F0
select MODULE_TINYUSB_PORTABLE_STM32_FSEDV if CPU_STM32 && CPU_FAM_F1
select MODULE_TINYUSB_PORTABLE_STM32_FSEDV if CPU_STM32 && CPU_FAM_G4
select MODULE_TINYUSB_PORTABLE_STM32_FSEDV if CPU_STM32 && CPU_FAM_L0
select MODULE_TINYUSB_PORTABLE_STM32_FSEDV if CPU_STM32 && CPU_FAM_WB
select MODULE_TINYUSB_PORTABLE_STM32_FSDEV if CPU_STM32 && CPU_FAM_F0
select MODULE_TINYUSB_PORTABLE_STM32_FSDEV if CPU_STM32 && CPU_FAM_F1
select MODULE_TINYUSB_PORTABLE_STM32_FSDEV if CPU_STM32 && CPU_FAM_G4
select MODULE_TINYUSB_PORTABLE_STM32_FSDEV if CPU_STM32 && CPU_FAM_L0
select MODULE_TINYUSB_PORTABLE_STM32_FSDEV if CPU_STM32 && CPU_FAM_WB
select MODULE_TINYUSB_PORTABLE_MICROCHIP if CPU_FAM_SAMD21 || CPU_FAM_SAMR21 \
|| CPU_COMMON_SAMD5X || CPU_FAM_SAML21 || CPU_FAM_SAMR34 \
|| CPU_FAM_SAMR30
Expand Down
8 changes: 6 additions & 2 deletions pkg/tinyusb/Makefile.dep
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ else ifneq (,$(filter saml21 samd5x samd21,$(CPU)))
USEMODULE += tinyusb_portable_microchip
else ifeq (stm32,$(CPU))
ifneq (,$(filter f2 f4 f7 h7 l4,$(CPU_FAM)))
# STM32 families F2, F4, F7, H7 and L4 use the Synopsys DWC2 USB OTG IP core
USEMODULE += tinyusb_portable_synopsys_dwc2
else ifneq (,$(filter f0 f1 g4 l0 wb,$(CPU_FAM)))
# TODO not yet working, the driver has to be ported
else ifneq (,$(filter stm32f105% stm32f107%,$(CPU_MODEL)))
# STM32F105xx and STM32F107xx also use the Synopsys DWC2 USB OTG IP core
USEMODULE += tinyusb_portable_synopsys_dwc2
else
# All other STM32 MCUs use the USB Device FS IP core.
USEMODULE += tinyusb_portable_stm32_fsdev
endif
endif
Expand Down
1 change: 1 addition & 0 deletions pkg/tinyusb/Makefile.include
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ else ifeq (esp32s3,$(CPU_FAM))
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_ESP32S3
else ifeq (stm32,$(CPU))
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_STM32$(call uppercase_and_underscore,$(CPU_FAM))
CFLAGS += -Dasm=__asm
benpicco marked this conversation as resolved.
Show resolved Hide resolved
else ifeq (saml21,$(CPU))
CFLAGS += -DCFG_TUSB_MCU=OPT_MCU_SAML21
else ifeq (samd21,$(CPU))
Expand Down
11 changes: 11 additions & 0 deletions pkg/tinyusb/hw/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ MODULE = tinyusb_hw

ifneq (,$(filter saml21 samd5x samd21,$(CPU)))
SRC = hw_sam0.c
else ifeq (stm32,$(CPU))
ifeq (,$(filter f1 f3 wb,$(CPU_FAM)))
# All STM32 families except for F1, F3 and WB use the Synopsys DWC2 USB OTG IP core
SRC += hw_stm32_otg.c
else ifneq (,$(filter stm32f105% stm32f107%,$(CPU_MODEL)))
# STM32F105xx and STM32F107xx also use the Synopsys DWC2 USB OTG IP core
SRC += hw_stm32_otg.c
else
# All other STM32 MCUs use the USB Device FS IP core.
SRC += hw_stm32_fsdev.c
endif
else
SRC = hw_$(CPU).c
endif
Expand Down
88 changes: 88 additions & 0 deletions pkg/tinyusb/hw/hw_stm32_fsdev.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
/*
* Copyright (C) 2022 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 STM32 MCUs with USB FS device port
* @author Gunar Schorcht <[email protected]>
*/

#include <errno.h>

#include "periph_conf.h"
#include "periph/gpio.h"
#include "periph/pm.h"

#include "tusb.h"
#include "device/usbd.h"
#include "host/usbh.h"

static int tinyusb_hw_init_dev(const stm32_usbdev_fs_config_t *conf)
{
/* Block both STOP and STANDBY, TODO STOP is unblocked during USB suspend
* status */
pm_block(STM32_PM_STOP);
pm_block(STM32_PM_STANDBY);

/* Enable the clock to the peripheral */
periph_clk_en(conf->apb, conf->rcc_mask);

#if defined(PWR_CR2_USV)
/* Validate USB Supply */
PWR->CR2 |= PWR_CR2_USV;
#endif

#if defined(RCC_APB1SMENR_USBSMEN)
RCC->APB1SMENR |= RCC_APB1SMENR_USBSMEN;
#elif defined(RCC_APB1SMENR1_USBSMEN)
RCC->APB1SMENR1 |= RCC_APB1SMENR1_USBSMEN;
#endif

#if defined(CRS_CR_AUTOTRIMEN) && defined(CRS_CR_CEN)
/* Enable CRS with auto trim enabled */
CRS->CR |= (CRS_CR_AUTOTRIMEN | CRS_CR_CEN);
#endif

if (conf->af != GPIO_AF_UNDEF) {
/* Configure AF for the pins */
gpio_init_af(conf->dp, conf->af);
gpio_init_af(conf->dm, conf->af);
}

if (conf->disconn != GPIO_UNDEF) {
/* In case the MCU has no internal D+ pullup, a GPIO is used to
* connect/disconnect from USB bus */
gpio_init(conf->disconn, GPIO_OUT);
gpio_set(conf->disconn);
}

return 0;
}

int tinyusb_hw_init(void)
{
for (unsigned i = 0; i < USBDEV_NUMOF; i++) {
if (tinyusb_hw_init_dev(&stm32_usbdev_fs_config[i]) != 0) {
return -ENODEV;
}
}
return 0;
}

void USBDEV_ISR(void)
{
/* call device interrupt handler with the last device */
if (IS_USED(MODULE_TINYUSB_DEVICE)) {
tud_int_handler(TINYUSB_TUD_RHPORT);
}

cortexm_isr_end();
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
* @brief
* @{
*
* @brief tinyUSB hardware driver for STM32 MCUs
* @brief tinyUSB hardware driver for STM32 MCUs with USB OTG FS/HS ports
* @author Gunar Schorcht <[email protected]>
*/

Expand Down