STM32F7 - Switch clock source after initialization

ChibiOS public support forum for topics related to the STMicroelectronics STM32 family of micro-controllers.

Moderator: RoccoMarco

Post Reply
elagil
Posts: 94
Joined: Tue Sep 19, 2017 7:38 am
Has thanked: 8 times
Been thanked: 7 times

STM32F7 - Switch clock source after initialization

Post by elagil »

Hello,

I need to switch clock sources from internal oscillator to external after full initialization. I will have to use the PAL driver in internal oscillator mode, do some switching of pins and then go to external oscillator mode.

Is there an easy way (clock init function?) to switch to external clock source while running on the internal oscillator? I will keep the same peripheral and main clock, I only need to change a few PLL parameters and MUXes.

Thanks in advance,
Adrian
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F7 - Switch clock source after initialization

Post by Giovanni »

Hi,

The clock initialization code is not that flexible, you need to switch manually to HSI and do your work, calling stm32_clock_init() again should work in order to return to the previous state assuming you de-initialized everything.

If you just operate on the clocks mux without stopping PLLs then you need just to switch the mux again in order to return to the previous situation.

Giovanni
steved
Posts: 834
Joined: Fri Nov 09, 2012 2:22 pm
Has thanked: 12 times
Been thanked: 138 times

Re: STM32F7 - Switch clock source after initialization

Post by steved »

Giovanni wrote:Hi,

The clock initialization code is not that flexible, you need to switch manually to HSI and do your work, calling stm32_clock_init() again should work in order to return to the previous state assuming you de-initialized everything.

If you just operate on the clocks mux without stopping PLLs then you need just to switch the mux again in order to return to the previous situation.

Giovanni

I think OP needs to start in HSI, then move to HSE.
I've done this myself (not on the F7), by directly operating on the affected clock registers. A potential 'gotcha' is that (AFAIK) Chibi uses the configured SYSCLK frequency and so on in the calculation of other timings. I managed to avoid this potential problem by using the same SYSCLK frequency throughout - if it was actually slower on boot it didn't matter in my situation.
User avatar
Giovanni
Site Admin
Posts: 14893
Joined: Wed May 27, 2009 8:48 am
Has thanked: 1202 times
Been thanked: 997 times

Re: STM32F7 - Switch clock source after initialization

Post by Giovanni »

Hi,

If you need to run code before clock initialization just add an hook to the __early_init() function (in board.c) before the clock is initialized, perform your initializations there.

Giovanni
Post Reply