cancel
Showing results for 
Search instead for 
Did you mean: 

Clock settings in STM32l152 (startup/stability issues)

fifi_22
Associate III

Hello.

I'm playing with my stm32l152 discovery board. I've got lcd, buttons, leds working. I wanted to change my clock speed to 16Mhz HSI (default 2Mhz MSI) and problem started.

To write my code I use RM - and I'm missing something. How this configuration should be done?

There is my clocks setting code: (If nedded I can upload whole project)

	RCC->CR |= (RCC_CR_HSION);					//enable high-speed internal oscillator
	while(!(RCC->CR & RCC_CR_HSIRDY));			//wait until oscillator starts
 
	RCC->CFGR |= RCC_CFGR_SW_HSI;				//set hsi as systemclk
	while((RCC->CR & RCC_CFGR_SWS_HSI)!= RCC_CFGR_SWS_HSI);
 
	RCC->CFGR |= (RCC_CFGR_MCOSEL_SYSCLK);		//set mco output to sysclk
 
	RCC->AHBENR |= (RCC_AHBENR_GPIOAEN);		//enable clock for PORTA peripheral
	RCC->AHBENR |= (RCC_AHBENR_GPIOBEN);		//enable clock for PORTB peripheral
	RCC->AHBENR |= (RCC_AHBENR_GPIOCEN);		//enable clock for PORTC peripheral

This is just after main(). Is this correct way to do it?

What actually happens:

After reset processor doesn't always start up (led doesn't blink). When it sometimes starts - the signal on MCO output is not present every time(2/10 times?) And moreover - the led sometimes flashes as on 2Mhz and sometimes as on 16Mhz clocks (somehow returning to default?) .

Any help really appreciated!

11 REPLIES 11

Oh! once more! Added this line at the beginning (before anything else in main() )

	RCC->APB1ENR |= (RCC_APB1ENR_PWREN);		//enable clock for PWRMGT peripheral

And then nothing works - no signal on mco, no led. When deleting these accesses it works as before (unstable)

	PWR->CR |= (PWR_CR_VOS_1);
	PWR->CR |= (PWR_CR_VOS_0);
	PWR->CR &= ~(PWR_CR_VOS_1);

It's not the whiles() who block it - it is these 3 lines... I thought they are protected or something but it looks like only rtc ones are.

Still missing some clock?

fifi_22
Associate III

I got it working. There is good configuration - I missed '!' on setting hsi (actually found it using debugger), and of course issues which waclawek.jan pointed out. Thanks for spending time! There is one more stm32 running in happiness.

	RCC->APB1ENR |= (RCC_APB1ENR_PWREN);		//enable clock for PWRMGT peripheral
 
	while((PWR->CSR & PWR_CSR_VOSF));			//poll for voltage stabilization
	PWR->CR |= (PWR_CR_VOS_1);
	PWR->CR |= (PWR_CR_VOS_0);
	PWR->CR &= ~(PWR_CR_VOS_1);
	while((PWR->CSR & PWR_CSR_VOSF));			//poll for voltage stabilization
 
	RCC->CR |= (RCC_CR_HSION);					//enable high-speed internal oscillator
	while(!(RCC->CR & RCC_CR_HSIRDY));			//wait until oscillator starts
 
	RCC->CFGR |= RCC_CFGR_SW_HSI;				//set hsi as systemclk
	while(!((RCC->CR & RCC_CFGR_SWS_HSI)!= RCC_CFGR_SWS_HSI));
 
	RCC->CFGR |= (RCC_CFGR_MCOSEL_SYSCLK);		//set mco output to sysclk
 
	RCC->AHBENR |= (RCC_AHBENR_GPIOAEN);		//enable clock for PORTA peripheral
	RCC->AHBENR |= (RCC_AHBENR_GPIOBEN);		//enable clock for PORTB peripheral
	RCC->AHBENR |= (RCC_AHBENR_GPIOCEN);		//enable clock for PORTC peripheral