Changing frequency and source clock at runtime

Discussions and support about ChibiOS/HAL, the MCU Hardware Abstraction Layer.
User avatar
Cesare
Posts: 14
Joined: Tue Jul 11, 2017 11:51 am
Location: Milan, Italy

Changing frequency and source clock at runtime

Postby Cesare » Mon Jan 15, 2018 3:05 pm

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

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

Re: Changing frequency and source clock at runtime

Postby Giovanni » Mon Jan 15, 2018 3:20 pm

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

User avatar
Cesare
Posts: 14
Joined: Tue Jul 11, 2017 11:51 am
Location: Milan, Italy

Re: Changing frequency and source clock at runtime

Postby Cesare » Mon Jan 15, 2018 3:36 pm

ok at first I am planning to follow your solution and then see if any limitation.

thank you Giovanni.


Return to “ChibiOS/HAL”

Who is online

Users browsing this forum: No registered users and 1 guest