Page 1 of 1

Changing frequency and source clock at runtime

Posted: Mon Jan 15, 2018 3:05 pm
by Cesare
Hello to everyone.
I am developing a low power application with STM32L151, the power just comes from the land line.

The application has different states in which the System Clock needs to be differently configured. I was able successfully to:
    - stop the RTOS
    - change frequency and source of System Clock
    - restart the RTOS
following the instruction found here: http://wiki.chibios.org/dokuwiki/doku.php?id=chibios:howtos:stop_os.

The issue is just after when the System Clock has been changed. For instance when the USART driver is started again calling sdStart(), the value for the baud rate register is calculated using the symbol STM32_PCLK2 which is defined at compile time. The problem is precisely this one, STM32_PCLK2 should not be a predefined symbol but should be a function which gets the real actual value of PCLK2 frequency. I am sure this issue will affect also other peripherals.

Idea#1 might be to replace STM32_PCLK2, STM32_PCLK1, ecc... with a function but I fear about portability and any hidden consequences.
Idea#2 might be to initialize the USART using the HAL layer available CubeMX.

I am open to any suggestion and tip.
Thank you in advance

Re: Changing frequency and source clock at runtime

Posted: Mon Jan 15, 2018 3:20 pm
by Giovanni
Hi,

Replacing the macros with functions is one of the things planned for next development cycle, at least for the L platforms. Some degree of runtime changes is also being considered.

One possible workaround is to trick drivers use different divider values by specifying specific baud rates or frequencies. For example, if you need 38400 and know that the current frequency is half of the one configured then specify 38400*2. You can create sets of driver configurations to match your clock configurations.

Giovanni

Re: Changing frequency and source clock at runtime

Posted: Mon Jan 15, 2018 3:36 pm
by Cesare
ok at first I am planning to follow your solution and then see if any limitation.

thank you Giovanni.