From 7a854cf50fe6048c1a76c3bbbe07dfb1e6b0d73c Mon Sep 17 00:00:00 2001 From: HiFiPhile Date: Fri, 30 Jan 2026 19:34:47 +0100 Subject: [PATCH] bsp/stm32u5: update vbus sense Signed-off-by: HiFiPhile --- hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h | 5 + hw/bsp/stm32u5/boards/stm32u545nucleo/board.h | 3 + hw/bsp/stm32u5/boards/stm32u575eval/board.h | 5 + hw/bsp/stm32u5/boards/stm32u575nucleo/board.h | 102 +++++++++++++++++ hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h | 106 +++++++++++++++++- hw/bsp/stm32u5/family.c | 34 +++--- hw/bsp/stm32u5/family.cmake | 5 +- hw/bsp/stm32u5/family.mk | 7 +- hw/bsp/stm32u5/stm32u5xx_hal_conf.h | 4 +- 9 files changed, 249 insertions(+), 22 deletions(-) diff --git a/hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h b/hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h index cf3f63ea5..c99743738 100644 --- a/hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h +++ b/hw/bsp/stm32u5/boards/b_u585i_iot2a/board.h @@ -55,6 +55,8 @@ extern "C" #define UART_TX_PIN GPIO_PIN_9 #define UART_RX_PIN GPIO_PIN_10 +#define VBUS_SENSE_EN 0 + //--------------------------------------------------------------------+ // RCC Clock //--------------------------------------------------------------------+ @@ -110,6 +112,9 @@ static void SystemClock_Config(void) { static void SystemPower_Config(void) { } +static inline void board_vbus_sense_init(void) { +} + #ifdef __cplusplus } #endif diff --git a/hw/bsp/stm32u5/boards/stm32u545nucleo/board.h b/hw/bsp/stm32u5/boards/stm32u545nucleo/board.h index 0c3439b2c..eb2b63721 100644 --- a/hw/bsp/stm32u5/boards/stm32u545nucleo/board.h +++ b/hw/bsp/stm32u5/boards/stm32u545nucleo/board.h @@ -110,6 +110,9 @@ static void SystemClock_Config(void) { static void SystemPower_Config(void) { } +static inline void board_vbus_sense_init(void) { +} + #ifdef __cplusplus } #endif diff --git a/hw/bsp/stm32u5/boards/stm32u575eval/board.h b/hw/bsp/stm32u5/boards/stm32u575eval/board.h index b11f6a747..cce3e38b3 100644 --- a/hw/bsp/stm32u5/boards/stm32u575eval/board.h +++ b/hw/bsp/stm32u5/boards/stm32u575eval/board.h @@ -56,6 +56,8 @@ extern "C" #define UART_TX_PIN GPIO_PIN_9 #define UART_RX_PIN GPIO_PIN_10 +#define VBUS_SENSE_EN 0 + //--------------------------------------------------------------------+ // RCC Clock //--------------------------------------------------------------------+ @@ -111,6 +113,9 @@ static void SystemClock_Config(void) { static void SystemPower_Config(void) { } +static inline void board_vbus_sense_init(void) { +} + #ifdef __cplusplus } #endif diff --git a/hw/bsp/stm32u5/boards/stm32u575nucleo/board.h b/hw/bsp/stm32u5/boards/stm32u575nucleo/board.h index be037b68a..b6b60f021 100644 --- a/hw/bsp/stm32u5/boards/stm32u575nucleo/board.h +++ b/hw/bsp/stm32u5/boards/stm32u575nucleo/board.h @@ -37,6 +37,8 @@ extern "C" { #endif +#include "stm32u5xx_ll_tim.h" + // LED GREEN #define LED_PORT GPIOC #define LED_PIN GPIO_PIN_7 @@ -55,6 +57,8 @@ extern "C" #define UART_TX_PIN GPIO_PIN_7 #define UART_RX_PIN GPIO_PIN_8 +#define VBUS_SENSE_EN 0 + //--------------------------------------------------------------------+ // RCC Clock //--------------------------------------------------------------------+ @@ -110,6 +114,104 @@ static void SystemClock_Config(void) { static void SystemPower_Config(void) { } +static inline void board_vbus_sense_init(void) { + /* ADC config */ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADCDAC; + PeriphClkInit.AdcDacClockSelection = RCC_ADCDACCLKSOURCE_HSE; + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) + { + Error_Handler(); + } + __HAL_RCC_ADC12_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + + ADC_HandleTypeDef hadc1; + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc1.Init.Resolution = ADC_RESOLUTION_14B; + hadc1.Init.GainCompensation = 0; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH; + hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; + hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; + hadc1.Init.OversamplingMode = DISABLE; + if (HAL_ADC_Init(&hadc1) != HAL_OK) { + Error_Handler(); + } + + ADC_ChannelConfTypeDef sConfig = {0}; + sConfig.Channel = ADC_CHANNEL_3; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLETIME_68CYCLES; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } + HAL_NVIC_EnableIRQ(ADC1_IRQn); + + /* TIM1 init for TRGO */ + __HAL_RCC_TIM1_CLK_ENABLE(); + TIM_HandleTypeDef htim1; + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + htim1.Instance = TIM1; + htim1.Init.Prescaler = 159; + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + htim1.Init.Period = 999; + htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim1.Init.RepetitionCounter = 0; + htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim1) != HAL_OK) { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) { + Error_Handler(); + } + LL_TIM_SetTriggerOutput(htim1.Instance, LL_TIM_TRGO_UPDATE); + + HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED); + HAL_ADC_Start_IT(&hadc1); + HAL_TIM_Base_Start(&htim1); +} + +void ADC1_IRQHandler(void) { + if(LL_ADC_IsActiveFlag_EOC(ADC1) != 0) { + /* Clear flag ADC group regular end of unitary conversion */ + LL_ADC_ClearFlag_EOC(ADC1); + /* ADC code = 4.5V * R2 / (R1 + R2) * (2^14) / 3.3V + * with R1 = 330kOhm and R2 = 50kOhm + */ + const uint32_t threshold = 4500 * 50 / (50 + 330) * 16384 / 3300; + if((ADC1->DR > threshold) && (USB_OTG_FS->GOTGCTL & USB_OTG_GOTGCTL_BVALOEN)) { + USB_OTG_FS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL; + } else { + USB_OTG_FS->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOVAL; + } + } + if(LL_ADC_IsActiveFlag_OVR(ADC1) != 0) { + /* Clear flag ADC group regular overrun */ + LL_ADC_ClearFlag_OVR(ADC1); + } +} + #ifdef __cplusplus } #endif diff --git a/hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h b/hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h index 0785fb36b..15106aee6 100644 --- a/hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h +++ b/hw/bsp/stm32u5/boards/stm32u5a5nucleo/board.h @@ -37,6 +37,8 @@ extern "C" { #endif +#include "stm32u5xx_ll_tim.h" + // LED GREEN #define LED_PORT GPIOC #define LED_PIN GPIO_PIN_7 @@ -55,11 +57,13 @@ extern "C" #define UART_TX_PIN GPIO_PIN_9 #define UART_RX_PIN GPIO_PIN_10 +#define VBUS_SENSE_EN 0 + //--------------------------------------------------------------------+ // RCC Clock //--------------------------------------------------------------------+ -static void SystemClock_Config(void) { +static inline void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = { 0 }; RCC_ClkInitTypeDef RCC_ClkInitStruct = { 0 }; @@ -128,7 +132,7 @@ static void SystemClock_Config(void) { } } -static void SystemPower_Config(void) { +static inline void SystemPower_Config(void) { HAL_PWREx_EnableVddIO2(); /* @@ -141,7 +145,105 @@ static void SystemPower_Config(void) { /* USER CODE END PWR */ } +static inline void board_vbus_sense_init(void) { + /* ADC config */ + GPIO_InitTypeDef GPIO_InitStruct = {0}; + RCC_PeriphCLKInitTypeDef PeriphClkInit = {0}; + PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_ADCDAC; + PeriphClkInit.AdcDacClockSelection = RCC_ADCDACCLKSOURCE_HSE; + if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK) + { + Error_Handler(); + } + __HAL_RCC_ADC12_CLK_ENABLE(); + __HAL_RCC_GPIOC_CLK_ENABLE(); + GPIO_InitStruct.Pin = GPIO_PIN_2; + GPIO_InitStruct.Mode = GPIO_MODE_ANALOG; + GPIO_InitStruct.Pull = GPIO_NOPULL; + HAL_GPIO_Init(GPIOC, &GPIO_InitStruct); + ADC_HandleTypeDef hadc1; + hadc1.Instance = ADC1; + hadc1.Init.ClockPrescaler = ADC_CLOCK_ASYNC_DIV1; + hadc1.Init.Resolution = ADC_RESOLUTION_14B; + hadc1.Init.GainCompensation = 0; + hadc1.Init.DataAlign = ADC_DATAALIGN_RIGHT; + hadc1.Init.ScanConvMode = ADC_SCAN_DISABLE; + hadc1.Init.EOCSelection = ADC_EOC_SINGLE_CONV; + hadc1.Init.LowPowerAutoWait = DISABLE; + hadc1.Init.ContinuousConvMode = DISABLE; + hadc1.Init.NbrOfConversion = 1; + hadc1.Init.DiscontinuousConvMode = DISABLE; + hadc1.Init.ExternalTrigConv = ADC_EXTERNALTRIG_T1_TRGO; + hadc1.Init.ExternalTrigConvEdge = ADC_EXTERNALTRIGCONVEDGE_RISING; + hadc1.Init.DMAContinuousRequests = DISABLE; + hadc1.Init.TriggerFrequencyMode = ADC_TRIGGER_FREQ_HIGH; + hadc1.Init.Overrun = ADC_OVR_DATA_OVERWRITTEN; + hadc1.Init.LeftBitShift = ADC_LEFTBITSHIFT_NONE; + hadc1.Init.ConversionDataManagement = ADC_CONVERSIONDATA_DR; + hadc1.Init.OversamplingMode = DISABLE; + if (HAL_ADC_Init(&hadc1) != HAL_OK) { + Error_Handler(); + } + + ADC_ChannelConfTypeDef sConfig = {0}; + sConfig.Channel = ADC_CHANNEL_3; + sConfig.Rank = ADC_REGULAR_RANK_1; + sConfig.SamplingTime = ADC_SAMPLETIME_68CYCLES; + sConfig.SingleDiff = ADC_SINGLE_ENDED; + sConfig.OffsetNumber = ADC_OFFSET_NONE; + sConfig.Offset = 0; + if (HAL_ADC_ConfigChannel(&hadc1, &sConfig) != HAL_OK) { + Error_Handler(); + } + HAL_NVIC_EnableIRQ(ADC1_2_IRQn); + + /* TIM1 init for TRGO */ + __HAL_RCC_TIM1_CLK_ENABLE(); + TIM_HandleTypeDef htim1; + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + htim1.Instance = TIM1; + htim1.Init.Prescaler = 159; + htim1.Init.CounterMode = TIM_COUNTERMODE_UP; + htim1.Init.Period = 999; + htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim1.Init.RepetitionCounter = 0; + htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; + if (HAL_TIM_Base_Init(&htim1) != HAL_OK) { + Error_Handler(); + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim1, &sClockSourceConfig) != HAL_OK) { + Error_Handler(); + } + LL_TIM_SetTriggerOutput(htim1.Instance, LL_TIM_TRGO_UPDATE); + + HAL_ADCEx_Calibration_Start(&hadc1, ADC_CALIB_OFFSET, ADC_SINGLE_ENDED); + HAL_ADC_Start_IT(&hadc1); + HAL_TIM_Base_Start(&htim1); +} + +void ADC1_2_IRQHandler(void) { + if(LL_ADC_IsActiveFlag_EOC(ADC1) != 0) { + /* Clear flag ADC group regular end of unitary conversion */ + LL_ADC_ClearFlag_EOC(ADC1); + /* ADC code = 4.5V * R2 / (R1 + R2) * (2^14) / 3.3V + * with R1 = 330kOhm and R2 = 50kOhm + */ + const uint32_t threshold = 4500 * 50 / (50 + 330) * 16384 / 3300; + if((ADC1->DR > threshold) && (USB_OTG_HS->GOTGCTL & USB_OTG_GOTGCTL_BVALOEN)) { + USB_OTG_HS->GOTGCTL |= USB_OTG_GOTGCTL_BVALOVAL; + USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_VBVALOVAL; + } else { + USB_OTG_HS->GOTGCTL &= ~USB_OTG_GOTGCTL_BVALOVAL; + USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_VBVALOVAL; + } + } + if(LL_ADC_IsActiveFlag_OVR(ADC1) != 0) { + /* Clear flag ADC group regular overrun */ + LL_ADC_ClearFlag_OVR(ADC1); + } +} #ifdef __cplusplus } #endif diff --git a/hw/bsp/stm32u5/family.c b/hw/bsp/stm32u5/family.c index 26d72d6a0..c2ea270df 100644 --- a/hw/bsp/stm32u5/family.c +++ b/hw/bsp/stm32u5/family.c @@ -179,18 +179,16 @@ void board_init(void) { GPIO_InitStruct.Mode = GPIO_MODE_INPUT; GPIO_InitStruct.Pull = GPIO_NOPULL; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); - - // Enable VBUS sense (B device) via pin PA9 - USB_OTG_FS->GCCFG |= USB_OTG_GCCFG_VBDEN; - #else - // Disable VBUS sense (B device) via pin PA9 - USB_OTG_FS->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; #endif // vbus sense +#if CFG_TUD_ENABLED + tud_configure_dwc2_t cfg = { + .bm_double_buffered = 0, + .vbus_sensing = VBUS_SENSE_EN + }; + tud_configure(0, TUD_CFGID_DWC2, &cfg); +#endif + /* Enable USB power on Pwrctrl CR2 register */ HAL_PWREx_EnableVddUSB(); @@ -218,13 +216,17 @@ void board_init(void) { /*Configuring the SYSCFG registers OTG_HS PHY*/ HAL_SYSCFG_EnableOTGPHY(SYSCFG_OTG_HS_PHY_ENABLE); - // Disable VBUS sense (B device) - USB_OTG_HS->GCCFG &= ~USB_OTG_GCCFG_VBDEN; - - // B-peripheral session valid override enable - USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_VBVALEXTOEN; - USB_OTG_HS->GCCFG |= USB_OTG_GCCFG_VBVALOVAL; +#if CFG_TUD_ENABLED + tud_configure_dwc2_t cfg = { + .bm_double_buffered = 0, + .vbus_sensing = VBUS_SENSE_EN + }; + tud_configure(0, TUD_CFGID_DWC2, &cfg); +#endif #endif // USB_OTG_FS + + /* Non-standard VBus sense settings */ + board_vbus_sense_init(); } //--------------------------------------------------------------------+ diff --git a/hw/bsp/stm32u5/family.cmake b/hw/bsp/stm32u5/family.cmake index 58dc63ae3..3d23c554d 100644 --- a/hw/bsp/stm32u5/family.cmake +++ b/hw/bsp/stm32u5/family.cmake @@ -45,6 +45,9 @@ function(family_add_board BOARD_TARGET) ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_rcc_ex.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_uart.c ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_uart_ex.c + ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_adc.c + ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_adc_ex.c + ${ST_HAL_DRIVER}/Src/${ST_PREFIX}_hal_tim.c ) target_include_directories(${BOARD_TARGET} PUBLIC ${CMAKE_CURRENT_FUNCTION_LIST_DIR} @@ -96,7 +99,7 @@ function(family_configure_example TARGET RTOS) endif () if (CMAKE_C_COMPILER_ID STREQUAL "GNU" OR CMAKE_C_COMPILER_ID STREQUAL "Clang") - set_source_files_properties(${CMAKE_CURRENT_FUNCTION_LIST_DIR}/family.c PROPERTIES COMPILE_FLAGS "-Wno-missing-prototypes") + set_source_files_properties(${CMAKE_CURRENT_FUNCTION_LIST_DIR}/family.c PROPERTIES COMPILE_FLAGS "-Wno-missing-prototypes -Wno-self-assign") endif () set_source_files_properties(${STARTUP_FILE_${CMAKE_C_COMPILER_ID}} PROPERTIES SKIP_LINTING ON diff --git a/hw/bsp/stm32u5/family.mk b/hw/bsp/stm32u5/family.mk index 47aed10a9..3dab8c610 100644 --- a/hw/bsp/stm32u5/family.mk +++ b/hw/bsp/stm32u5/family.mk @@ -16,6 +16,7 @@ CFLAGS_GCC += \ -Wno-error=undef \ -Wno-error=unused-parameter \ -Wno-error=type-limits \ + -Wno-self-assign \ ifeq ($(TOOLCHAIN),gcc) CFLAGS_GCC += -Wno-error=maybe-uninitialized @@ -35,11 +36,15 @@ SRC_C += \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_pwr_ex.c \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_rcc.c \ $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_rcc_ex.c \ - $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_uart.c + $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_uart.c \ + $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_adc.c \ + $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_adc_ex.c \ + $(ST_HAL_DRIVER)/Src/stm32$(ST_FAMILY)xx_hal_tim.c ifneq ($(filter stm32u545xx stm32u535xx,$(MCU_VARIANT)),) SRC_C += \ src/portable/st/stm32_fsdev/dcd_stm32_fsdev.c \ + src/portable/st/stm32_fsdev/hcd_stm32_fsdev.c \ src/portable/st/stm32_fsdev/fsdev_common.c else SRC_C += \ diff --git a/hw/bsp/stm32u5/stm32u5xx_hal_conf.h b/hw/bsp/stm32u5/stm32u5xx_hal_conf.h index 87c3683de..5d95862f6 100644 --- a/hw/bsp/stm32u5/stm32u5xx_hal_conf.h +++ b/hw/bsp/stm32u5/stm32u5xx_hal_conf.h @@ -36,7 +36,7 @@ #define HAL_MODULE_ENABLED -/*#define HAL_ADC_MODULE_ENABLED */ +#define HAL_ADC_MODULE_ENABLED /*#define HAL_MDF_MODULE_ENABLED */ /*#define HAL_COMP_MODULE_ENABLED */ /*#define HAL_CORDIC_MODULE_ENABLED */ @@ -76,7 +76,7 @@ /*#define HAL_SMBUS_MODULE_ENABLED */ /*#define HAL_SPI_MODULE_ENABLED */ /*#define HAL_SRAM_MODULE_ENABLED */ -/*#define HAL_TIM_MODULE_ENABLED */ +#define HAL_TIM_MODULE_ENABLED /*#define HAL_TSC_MODULE_ENABLED */ /*#define HAL_RAMCFG_MODULE_ENABLED */ #define HAL_UART_MODULE_ENABLED