STM32F0 RTC using HSE/32 as the timing source

This is only a short post really. With my watch, I’d been trying for AGES to try and get the RTC working with the external crystal. It might seem simple to some but it was certainly not simple to me and ST don’t give an example, unlike most things; in their standard peripheral downloadable pack. I had read through the data sheets multiple times, along with the dedicated RTC datasheet and still couldn’t manage to get it to work! After asking on the EEVBlog forum, a kind user eventually sent me in the correct direction allowing me to get it work! So for those who are also in my boat, the really simple initialization to set the RTC clock source to HSE/32 is as follows:

 

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

	RCC_HSEConfig(RCC_HSE_ON);
	while(RCC_GetFlagStatus(RCC_FLAG_HSERDY) == RESET);
	RCC_SYSCLKConfig(RCC_SYSCLKSource_HSE);

	PWR_BackupAccessCmd(ENABLE);

	RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div32);
	RCC_RTCCLKCmd(ENABLE);

	RCC_BackupResetCmd(ENABLE);
	RCC_BackupResetCmd(DISABLE);

	RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div32);
	RCC_RTCCLKCmd(ENABLE);

It wasn’t apparent to me that you have to reconfigure the clock after you have reset the backup domain, not forgetting the fact you just have to send the reset command to the backup domain, there doesn’t seem to be any timing required (although, as far as I know, the RTC may add in 2 wait states or something)…

Either way, the above code will allow the RTC to run of the HSE crystal, giving much more accurate timing compared to LSI – vital for the STM32F0’s in the TSSOP20 or LQFP32 package where access to the LSE crystal pins doesn’t exist.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s