Skip to content

Commit

Permalink
tinyusb: Enable USB for STM32H7
Browse files Browse the repository at this point in the history
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 <jerzy.kasenberg@codecoup.pl>
  • Loading branch information
kasjer committed Mar 6, 2024
1 parent e0b0516 commit be6e5fb
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 13 deletions.
2 changes: 2 additions & 0 deletions hw/usb/tinyusb/pkg.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
2 changes: 2 additions & 0 deletions hw/usb/tinyusb/synopsys/include/tusb_hw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
48 changes: 35 additions & 13 deletions hw/usb/tinyusb/synopsys/src/synopsys.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@

#include <mcu/stm32_hal.h>

#if defined(USB_OTG_FS)
#if defined(STM32U5)
#define GPIO_AF_USB GPIO_AF10_USB
#else
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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

Expand Down

0 comments on commit be6e5fb

Please sign in to comment.