From d8cd6b743be808278115f55cb0383671fb188ce9 Mon Sep 17 00:00:00 2001 From: crteensy Date: Thu, 27 Feb 2025 13:46:26 +0100 Subject: [PATCH] Properly handle HSI if selected (#14259) * Properly handle HSI if selected In some places, PERSISTENT_OBJECT_HSE_VALUE being zero is used to imply that the MCU should be clocked from HSI, but the code doesn't really follow through and results in an invalid clock tree setup. The proposed changes should fix this. * Apply suggestions from code review Co-authored-by: Mark Haslinghuis * Update system_stm32g4xx.c HSI should now be implicitly selected when SYSTEM_HSE_MHZ is defined as 0 or omitted in the target config. USE_CLOCK_SOURCE_HSI is not necessary. * Update src/platform/STM32/startup/system_stm32g4xx.c Co-authored-by: Petr Ledvina * Update src/platform/STM32/startup/system_stm32g4xx.c Co-authored-by: Mark Haslinghuis --------- Co-authored-by: Mark Haslinghuis Co-authored-by: Petr Ledvina --- src/platform/STM32/startup/system_stm32g4xx.c | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/platform/STM32/startup/system_stm32g4xx.c b/src/platform/STM32/startup/system_stm32g4xx.c index 5690987942..9a35bdef64 100644 --- a/src/platform/STM32/startup/system_stm32g4xx.c +++ b/src/platform/STM32/startup/system_stm32g4xx.c @@ -118,7 +118,6 @@ void SystemInit(void) void SystemCoreClockUpdate(void) { uint32_t hse_value = persistentObjectRead(PERSISTENT_OBJECT_HSE_VALUE); - uint32_t tmp, pllvco, pllr, pllsource, pllm; /* Get SYSCLK source -------------------------------------------------------*/ @@ -381,10 +380,12 @@ void SystemClock_Config(void) // Initializes the CPU, AHB and APB busses clocks - RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI|RCC_OSCILLATORTYPE_HSI48 - |RCC_OSCILLATORTYPE_LSI|RCC_OSCILLATORTYPE_HSE; - - RCC_OscInitStruct.HSEState = RCC_HSE_ON; + const bool useHse = persistentObjectRead(PERSISTENT_OBJECT_HSE_VALUE) != 0; + RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI | RCC_OSCILLATORTYPE_HSI48 + | RCC_OSCILLATORTYPE_LSI + | (useHse ? RCC_OSCILLATORTYPE_HSE : 0); + RCC_OscInitStruct.HSEState = useHse ? RCC_HSE_ON : RCC_HSE_OFF; + RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.LSIState = RCC_LSI_ON;