How to switch clock source at runtime

How to switch the clock source of the external crystal oscillator to the internal low-speed clock source during operation

Unfortunately your question is too vague and lacks necessary details to provide an answer.
Why do you want to do that ? Which bigger problem do you want to solve ?

I want to switch the clock source in tickless. Adapt to high-speed clock operation in working mode, and use low-speed clock in standby mode

It will really depend on the processor. FreeRTOS has no code to do that sort of thing.

You may need to adjust the code for tickless idle to figure out how much time expired to update the system time.

One thing I have done for some systems is base the tick on a ‘low power’ timer running off the 32768 Hz external crystal, so I don’t need to change the tick to go to low power mode.

Many roads lead to Rome…

I took the following approach: the system tick was used for the normal 1000 Hz system clock-tick.

When my vPortSuppressTicksAndSleep() gets called by the kernel, it is supposed to sleep at most xExpectedIdleTime clock ticks.
In my project it will typically block for 30 seconds or more, to save the batteries. The power consumption is hard to measure, in the range of uA.

It will will disable the systick:


I use any Timer Counter (TC), which I program to cause an alarm when xExpectedIdleTime expires. It is also possible that another source, like a push button, will wakeup the CPU. So when waking up, it will inspect the TC to see how long the sleep has lasted. This will be reported to the kernel as follows:

    vTaskStepTick( ulCompleteTickPeriods );

And finally, it will re-enable the SYSTICK before leaving the function.

But like @hs2 asked: what is your CPU / platform?