From 92d2de82e29fb27d329417a76f0dc95b5b29e9b1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Wed, 14 Jun 2023 17:10:22 +1000 Subject: [PATCH] stm32/usbd_conf: Treat G0 USB periph as MICROPY_HW_USB_IS_MULTI_OTG=0. The G0 USB peripheral behaves more like MICROPY_HW_USB_IS_MULTI_OTG=0 than that config =1. This fixes the configuration of the PMA FIFO buffers. Signed-off-by: Damien George --- ports/stm32/mpconfigboard_common.h | 4 +++- ports/stm32/usbd_cdc_interface.c | 4 ++-- ports/stm32/usbd_conf.c | 18 ++++++------------ 3 files changed, 11 insertions(+), 15 deletions(-) diff --git a/ports/stm32/mpconfigboard_common.h b/ports/stm32/mpconfigboard_common.h index 300ca55d5b..62311324aa 100644 --- a/ports/stm32/mpconfigboard_common.h +++ b/ports/stm32/mpconfigboard_common.h @@ -574,7 +574,9 @@ #endif // Whether the USB peripheral is device-only, or multiple OTG -#if defined(STM32G4) || defined(STM32L0) || defined(STM32L432xx) || defined(STM32WB) +// For STM32G0 the USB peripheral supports device and host mode, +// but otherwise acts like a non-multi-OTG peripheral. +#if defined(STM32G0) || defined(STM32G4) || defined(STM32L0) || defined(STM32L432xx) || defined(STM32WB) #define MICROPY_HW_USB_IS_MULTI_OTG (0) #else #define MICROPY_HW_USB_IS_MULTI_OTG (1) diff --git a/ports/stm32/usbd_cdc_interface.c b/ports/stm32/usbd_cdc_interface.c index 399411d8ab..1e5d6c80a4 100644 --- a/ports/stm32/usbd_cdc_interface.c +++ b/ports/stm32/usbd_cdc_interface.c @@ -52,9 +52,9 @@ #if !MICROPY_HW_USB_IS_MULTI_OTG #define USE_USB_CNTR_SOFM (1) -#elif defined(STM32G0) -#define USE_USB_CNTR_SOFM (1) +#if defined(STM32G0) #define USB USB_DRD_FS +#endif #else #define USE_USB_CNTR_SOFM (0) #endif diff --git a/ports/stm32/usbd_conf.c b/ports/stm32/usbd_conf.c index 371558d775..9c53c6323a 100644 --- a/ports/stm32/usbd_conf.c +++ b/ports/stm32/usbd_conf.c @@ -44,13 +44,11 @@ PCD_HandleTypeDef pcd_fs_handle; PCD_HandleTypeDef pcd_hs_handle; #endif -#if !MICROPY_HW_USB_IS_MULTI_OTG -// The MCU has a single USB device-only instance -#define USB_OTG_FS USB -#endif - #if defined(STM32G0) #define USB_OTG_FS USB_DRD_FS +#elif !MICROPY_HW_USB_IS_MULTI_OTG +// The MCU has a single USB device-only instance +#define USB_OTG_FS USB #endif /******************************************************************************* @@ -119,7 +117,7 @@ void HAL_PCD_MspInit(PCD_HandleTypeDef *hpcd) { #endif // Enable USB FS Clocks - #if !MICROPY_HW_USB_IS_MULTI_OTG || defined(STM32G0) + #if !MICROPY_HW_USB_IS_MULTI_OTG __HAL_RCC_USB_CLK_ENABLE(); #else __USB_OTG_FS_CLK_ENABLE(); @@ -262,11 +260,7 @@ void HAL_PCD_MspDeInit(PCD_HandleTypeDef *hpcd) { #if MICROPY_HW_USB_FS if (hpcd->Instance == USB_OTG_FS) { /* Disable USB FS Clocks */ - #if defined(STM32G0) - __HAL_RCC_USB_CLK_DISABLE(); - #else __USB_OTG_FS_CLK_DISABLE(); - #endif return; } #endif @@ -443,7 +437,7 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev, int high_speed, const pcd_fs_handle.Init.speed = PCD_SPEED_FULL; pcd_fs_handle.Init.lpm_enable = DISABLE; pcd_fs_handle.Init.battery_charging_enable = DISABLE; - #if MICROPY_HW_USB_IS_MULTI_OTG + #if MICROPY_HW_USB_IS_MULTI_OTG || defined(STM32G0) #if !defined(STM32G0) pcd_fs_handle.Init.use_dedicated_ep1 = 0; #endif @@ -463,7 +457,7 @@ USBD_StatusTypeDef USBD_LL_Init(USBD_HandleTypeDef *pdev, int high_speed, const HAL_PCD_Init(&pcd_fs_handle); // Set FIFO buffer sizes - #if !MICROPY_HW_USB_IS_MULTI_OTG || defined(STM32G0) + #if !MICROPY_HW_USB_IS_MULTI_OTG uint32_t fifo_offset = USBD_PMA_RESERVE; // need to reserve some data at start of FIFO for (size_t i = 0; i < USBD_PMA_NUM_FIFO; ++i) { uint16_t ep_addr = ((i & 1) * 0x80) | (i >> 1);