[DEV] Dynamic clocking in HAL

This forum is dedicated to feedback, discussions about ongoing or future developments, ideas and suggestions regarding the ChibiOS projects are welcome. This forum is NOT for support.
User avatar
Giovanni
Site Admin
Posts: 14432
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 917 times
Contact:

[DEV] Dynamic clocking in HAL

Postby Giovanni » Thu May 20, 2021 8:26 pm

Hi,

Small update, HAL has received a new API to switch clock/power configurations at runtime: halClockSwitchMode()

Configurations are encoded in structures, two configurations are pre-defined:

  1. hal_clkcfg_reset, which restores a post-reset state, this can be used to de-initialize clocks, for example from a bootloader before jumping the in application code.
  2. hal_clkcfg_default, which programs the settings in halconf.h (this is performed implicitly on halInit() in order to keep exactly the current behaviour).

Of course it is possible to define multiple configurations and switch those on the fly. The API checks the configuration first then applies it, the function can fail (returning true) if the configuration is not correct (this will probably be changed in assertions, it is a dev time error) or if cannot be applied (HW timeouts, unexpected conditions and similar).

Another function halGetClockPoint() allows to read the current frequency on all relevant points of the clock tree, for example SYSCLK, PCLKx, PLLVCO etc etc

HAL drivers read the frequency on xxxStart(), so if you switch clocks you need to stop and restart the drivers affected, for example the serial driver can recalculate the baud rate divider based on the new clock settings.

The new API is already implemented on STM32G4, STM32L4+ and STM32WL, more will follow but, probably, not the older ones. There is also an mcuconf.h settings to disable the new API and restore the old static initialization.

Giovanni

djedski30
Posts: 6
Joined: Thu Jun 11, 2020 1:13 am

Re: [DEV] Dynamic clocking in HAL

Postby djedski30 » Wed Apr 19, 2023 4:37 pm

Does this API work for the STM32G071 SoC? I'm trying to use it to enter into low power mode (switch clock from PLL to HSI: 16Mhz to 1MHz) and then switch to the low power regulator from the main, but it doesn't seem to work. After the switch, I would have expected SystemCoreClock to switch to 1Mhz, but it just returns 16MHz again.
Thanks.

User avatar
Giovanni
Site Admin
Posts: 14432
Joined: Wed May 27, 2009 8:48 am
Location: Salerno, Italy
Has thanked: 1074 times
Been thanked: 917 times
Contact:

Re: [DEV] Dynamic clocking in HAL

Postby Giovanni » Wed Apr 19, 2023 6:13 pm

The API is supported on the G0, it could have a bug, what is the configuration you are switching to?

Giovanni


Return to “Development and Feedback”

Who is online

Users browsing this forum: No registered users and 12 guests