Skip to content

Commit

Permalink
Properly handle HSI if selected (betaflight#14259)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>

* 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 <[email protected]>

* Update src/platform/STM32/startup/system_stm32g4xx.c

Co-authored-by: Mark Haslinghuis <[email protected]>

---------

Co-authored-by: Mark Haslinghuis <[email protected]>
Co-authored-by: Petr Ledvina <[email protected]>
  • Loading branch information
3 people authored Feb 27, 2025
1 parent a801aad commit d8cd6b7
Showing 1 changed file with 6 additions and 5 deletions.
11 changes: 6 additions & 5 deletions src/platform/STM32/startup/system_stm32g4xx.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 -------------------------------------------------------*/
Expand Down Expand Up @@ -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;
Expand Down

0 comments on commit d8cd6b7

Please sign in to comment.