What to do for freertos when MCU frequency is changed while program running


I’m using freertos 8.2.3 with atmel ASF and configSYSTICK_CLOCK_HZ is configed in FreeRTOSConfig.h as

#define configCPU_CLOCK_HZ ( sysclk_get_cpu_hz() )

sysclk_get_cpu_hz is the function that get MCU frequency.

It is OK as frequency do not change while program running.

but we are changing MCU frequency now and I’m afraid configSYSTICK_CLOCK_HZ

will not update when we have changed the MCU Frequency.


Yes, if you change the clock frequency, you will need to manually change all the setup in the hardware that are dependent on it (like the divider on the systick counter).

Other #define values based on it will change, so if #configSYSTICK_CLOCK_HZ uses configCPU_CLOCK_HZ in it definition then the value of that define will change, but the existing code only uses it value at scheduler start time to set the systick, so later changes won’t have any effect.

Generally, if you are changing the system clock rate, you want to make as many of the other clocks in the system used for timing to be not based on it as possible, as you will need to reset everything that is dependent on it when you change the clock frequency.

Hi, richard,

Thanks for your reply.

I have consulted chip maker for changing clock for hardware.

and as for freertos, I’d like to know what to do when I changed MCU frequency while program running.

Do you mean it is not suitable for freertos to change systick while mcu freqency modified for the time being?


I am not familiar with the ASF, so I don’t know its capabilities, but generally IF you can make peripherals like UARTS generate their baud rates for clock sources that don’t change when you change the MCU frequency and can find a timer that also doesn’t have its clock frequency change when you change the MCU frequency, then there isn’t an issue with changing the MCU clock. Some times this is available, some times it isn’t. If it isn’t, then you will need to live with the fact that those peripherals that do change their basic int clock frequency when you change the MCU frequency, will have their operating frequency change, and likely will need to be reprogrammed, and this might disrupt any transactions currently happening.

If you are just changing the frequency before starting FreeRTOS, then presumably no transactions are in progress, so not a problem, you just need to make sure that configSYSTICK_CLOCK_HZ has the right value, which is likely going to be based on the value of configCPU_CLOCK_HZ.