cancel
Showing results for 
Search instead for 
Did you mean: 

Clock Switching Problem

Mohammed Galal
Associate
Posted on March 13, 2018 at 19:14

Hi all,

I'm using STM8l052R8 MCU.

I'm trying to switch the clock from HSI to LSI but I can't. 

this is my code:

{

CLK_DeInit(); //deinitiate clock

CLK_SYSCLKSourceSwitchCmd(ENABLE); //enable switching

CLK_LSICmd(ENABLE); //enable LSI oscillator

CLK->SWR = 0x02; // choose LSI as a clock source

}

the debugger doesn't work correctly. I'm using ST-Link Debugger.

Is this code Is correct or How to confirm that I switched to LSI correctly ??
1 REPLY 1
Szymon PANECKI
Senior III
Posted on March 14, 2018 at 13:19

HelloMohammed,

Here is the snippet, which can be used to switch the system clock from HSI to LSI:

CLK_SYSCLKSourceSwitchCmd(ENABLE);
CLK_SYSCLKSourceConfig(CLK_SYSCLKSource_LSI);
while (CLK_GetSYSCLKSource() != CLK_SYSCLKSource_LSI)
{}�?�?�?�?�?�?�?�?

Looking at your code you do a similar thing. The difference is that you use direct register access to change the clock source for system clock and you don't wait for end of this operation. Anywayfrom my point of view it should work as well.

You mentioned thatdebugger doesn't work correctly.SWIM communication requirescertain clock frequency to work normally. When LSI is used, this frequency is too slow. So if on your side you see that after executing

CLK->SWR = 0x02; debug session is broken, then it is expected behaviour.

You canimplement a simple procedure to check if switching to LSI was succesful. For example you check in your codethe current system clock souce (CLK->SCSR or CLK_GetSYSCLKSource() ) and if the result is LSI, then you simply switch on the LED.

Regards

Szymon