cancel
Showing results for 
Search instead for 
Did you mean: 

RTC with Internal Crystal - STM32F303CB

Vinod Kumar
Associate II

Hi,

I am trying to use RTC with Internal Crystal, I am not able to initialize the internal crystal of RTC.

I am trying to initialize the internal clock by the RCC_CSR_LSION flag but I am not getting the RCC_CSR_LSIRDY flag set by the controller.

void RTC_Init(){
	PWR->CR |= PWR_CR_DBP;
	RCC->BDCR &= ~(RCC_BDCR_BDRST);
	RCC->BDCR &= ~(RCC_BDCR_LSEON);						
	RCC->CIR = RCC_CIR_LSIRDYF;
	RCC->BDCR |= RCC_BDCR_RTCSEL_LSI;
	RCC->CSR |= RCC_CSR_LSION;
	timeout = TIMEOUT;
	while((RCC->CSR & RCC_CSR_LSIRDY)!=RCC_CSR_LSIRDY){
		if(timeout<=0){
			UART3_SendString("\n\rRTC LSI Initialization Failed\n");
			return;
		}
		Delay_ms(1);
		timeout--;
	}
	RCC->BDCR |= RCC_BDCR_RTCEN;
	RTC->WPR = 0xCA;
	RTC->WPR = 0x53;
	RTC->ISR |= (1<<7);
	timeout = 2000;
	while((RTC->ISR & RTC_ISR_INITF)!= RTC_ISR_INITF){
		if(timeout<=0){
			UART3_SendString("\n\rRTC Initialization Failed\n");
			return;
		}
		Delay_ms(1);
		timeout--;
	}
	RTC->PRER = (311<<0);
	RTC->PRER |= (127<<16);
	RTC->ISR &= (uint32_t)~(1<<7);
	RTC->WPR = 0xFF;
}

3 REPLIES 3

No sure its a crystal.

Might I suggest testing and reviewing the SPL or HAL code as working examples, which you can then translate and test at a register level if you so choose.

Note doing multiple load/stores against the same register is inefficient and can't be folded by the compiler.

Watch also for time criticalities between enabling clocks and touching peripherals, and also the RTC/PWR unlock sequence.

Do you get the failure messages?

What are the register contents of the peripheral(s) in these situations?

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..

Try to enable LSI before selecting it as RTC source in BDCR. You may need to perform a backup-domain reset first.

JW

Hi,

May I know exactly the behavior of BDRST (Backup domain reset)?

Is RTC internal clock is fixed 40KHz as per the datasheet it is between 30 to 50 kHz and typical 40KHz. But I couldn't find the register to configure the desired clock speed for RTC with the Internal clock.

The values are loaded for 40 khz in prescalar register for LSI are:

  1. RTC->PRER = (311<<0);
  2. RTC->PRER = (127<<16);

Do I need to configure both Asncychoronous (127) and Synchronous (311) values?

While updating the new timestamp in RTC, did we required to reinitialize the RTC?

Thanking you,

Vinod.