From 9f4f9e70406305a8745dd4c35c5c16c8ff1e5bb8 Mon Sep 17 00:00:00 2001 From: Jerzy Kasenberg Date: Wed, 6 Mar 2024 13:53:29 +0100 Subject: [PATCH] tinyusb: Enable USB for STM32H7 STM32H7 uses same USB hardware block but requires enabling voltage detector to work. This slightly reworks code to work on all synopsys bases devices. Signed-off-by: Jerzy Kasenberg --- hw/usb/tinyusb/pkg.yml | 2 + hw/usb/tinyusb/synopsys/include/tusb_hw.h | 2 + hw/usb/tinyusb/synopsys/src/synopsys.c | 48 +++++++++++++++++------ 3 files changed, 39 insertions(+), 13 deletions(-) diff --git a/hw/usb/tinyusb/pkg.yml b/hw/usb/tinyusb/pkg.yml index 187deead18..df95547009 100644 --- a/hw/usb/tinyusb/pkg.yml +++ b/hw/usb/tinyusb/pkg.yml @@ -46,6 +46,8 @@ pkg.deps.'MCU_TARGET == "nRF5340_APP"': - "@apache-mynewt-core/hw/usb/tinyusb/nrf53" pkg.deps.MCU_STM32F4: - "@apache-mynewt-core/hw/usb/tinyusb/synopsys" +pkg.deps.MCU_STM32H7: + - "@apache-mynewt-core/hw/usb/tinyusb/synopsys" pkg.deps.MCU_STM32F7: - "@apache-mynewt-core/hw/usb/tinyusb/synopsys" pkg.deps.MCU_STM32L4: diff --git a/hw/usb/tinyusb/synopsys/include/tusb_hw.h b/hw/usb/tinyusb/synopsys/include/tusb_hw.h index b54bb2a808..9b136122cf 100755 --- a/hw/usb/tinyusb/synopsys/include/tusb_hw.h +++ b/hw/usb/tinyusb/synopsys/include/tusb_hw.h @@ -32,6 +32,8 @@ #define CFG_TUSB_MCU OPT_MCU_STM32F4 #elif MYNEWT_VAL(MCU_STM32F7) #define CFG_TUSB_MCU OPT_MCU_STM32F7 +#elif MYNEWT_VAL(MCU_STM32H7) +#define CFG_TUSB_MCU OPT_MCU_STM32H7 #elif MYNEWT_VAL(MCU_STM32L4) #define CFG_TUSB_MCU OPT_MCU_STM32L4 #elif MYNEWT_VAL(MCU_STM32U5) diff --git a/hw/usb/tinyusb/synopsys/src/synopsys.c b/hw/usb/tinyusb/synopsys/src/synopsys.c index bca255082e..c7f4db5a4b 100755 --- a/hw/usb/tinyusb/synopsys/src/synopsys.c +++ b/hw/usb/tinyusb/synopsys/src/synopsys.c @@ -24,6 +24,7 @@ #include +#if defined(USB_OTG_FS) #if defined(STM32U5) #define GPIO_AF_USB GPIO_AF10_USB #else @@ -36,13 +37,31 @@ OTG_FS_IRQHandler(void) /* TinyUSB provides interrupt handler code */ tud_int_handler(0); } +#define USB_OTG USB_OTG_FS +#define USB_OTG_CLK_ENABLE __HAL_RCC_USB_OTG_FS_CLK_ENABLE +#elif defined(USB_OTG_HS) +static void +OTG_HS_IRQHandler(void) +{ + /* TinyUSB provides interrupt handler code */ + tud_int_handler(0); +} +#define USB_OTG USB_OTG_HS +#define USB_OTG_CLK_ENABLE __HAL_RCC_USB_OTG_HS_CLK_ENABLE +#define GPIO_AF10_OTG_FS GPIO_AF10_OTG1_FS +#define GPIO_AF_USB GPIO_AF10_OTG_FS +#endif void tinyusb_hardware_init(void) { +#if defined(USB_OTG_FS) NVIC_SetVector(OTG_FS_IRQn, (uint32_t)OTG_FS_IRQHandler); NVIC_SetPriority(OTG_FS_IRQn, 2); - +#elif defined(USB_OTG_HS) + NVIC_SetVector(OTG_HS_IRQn, (uint32_t)OTG_HS_IRQHandler); + NVIC_SetPriority(OTG_HS_IRQn, 2); +#endif /* * USB Pin Init * PA11- DM, PA12- DP @@ -58,28 +77,31 @@ tinyusb_hardware_init(void) #endif hal_gpio_init_af(MCU_GPIO_PORTA(12), GPIO_AF_USB, GPIO_NOPULL, GPIO_MODE_AF_PP); +#if MYNEWT_VAL(MCU_STM32H7) + HAL_PWREx_EnableUSBVoltageDetector(); +#endif /* * Enable USB OTG clock, force device mode */ - __HAL_RCC_USB_OTG_FS_CLK_ENABLE(); + USB_OTG_CLK_ENABLE(); #if MYNEWT_VAL(USB_ID_PIN_ENABLE) hal_gpio_init_af(MCU_GPIO_PORTA(10), GPIO_AF_USB, GPIO_PULLUP, GPIO_MODE_AF_OD); #else - USB_OTG_FS->GUSBCFG &= ~USB_OTG_GUSBCFG_FHMOD; - USB_OTG_FS->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD; + USB_OTG->GUSBCFG &= ~USB_OTG_GUSBCFG_FHMOD; + USB_OTG->GUSBCFG |= USB_OTG_GUSBCFG_FDMOD; #endif #ifdef USB_OTG_GCCFG_NOVBUSSENS #if !MYNEWT_VAL(USB_VBUS_DETECTION_ENABLE) /* PA9- VUSB not used for USB */ - USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS; - USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN; - USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN; + USB_OTG->GCCFG |= USB_OTG_GCCFG_NOVBUSSENS; + USB_OTG->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN; + USB_OTG->GCCFG &= ~USB_OTG_GCCFG_VBUSASEN; #else - USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS; - USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN; - USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBUSASEN; + USB_OTG->GCCFG &= ~USB_OTG_GCCFG_NOVBUSSENS; + USB_OTG->GCCFG &= ~USB_OTG_GCCFG_VBUSBSEN; + USB_OTG->GCCFG |= USB_OTG_GCCFG_VBUSASEN; hal_gpio_init_in(MCU_GPIO_PORTA(9), HAL_GPIO_PULL_NONE); #endif #elif USB_OTG_GCCFG_VBDEN @@ -88,10 +110,10 @@ tinyusb_hardware_init(void) USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBDEN; #else /* PA9- VUSB not used for USB */ - USB_OTG_FS->GCCFG &= ~USB_OTG_GCCFG_VBDEN; + USB_OTG->GCCFG &= ~USB_OTG_GCCFG_VBDEN; /* B-peripheral session valid override enable */ - USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN; - USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL; + USB_OTG->GOTGCTL |= USB_OTG_GOTGCTL_BVALOEN; + USB_OTG->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL; #endif #endif