Question
RTC doesn't tick with HSE/128
Posted on December 22, 2012 at 17:11
I meet troubles with RTC on STM32F103RBT6. And it seems RTC in STM32 is very big problem. Many people have problem with RTC.STM32 has many restrictions for LSE crystal.
Noname crystal from China which works fine with LPC and AVR doesn't work stable with STM. I know about AN28 My board works fine some weeks with RTC, but now RTC doesn't start. I test with 3 different noname crystals, without capacitor and with capacitors from 5pF to 22pF. Also try to solder 5 MOhm resistor. Nothing. Doesn't work. After thatI tried to implement switching RTC signal source to HSE/ But also doesn't work. RTC doesn't counts. It looks as HSE/128 is not connected as clock signal source. But on KEIL debugger I see clock signal source is HSE/ From LSI RTC works fine. Code is below. You can see two lines (one of them is not active):RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128); // doesn't work
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI); // it works
No other difference only signal source. Maybe I forgot something??? Why doesn't work from HSE/128?
/* Enable PWR and BKP clocks */
RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR | RCC_APB1Periph_BKP, ENABLE);
/* LSI clock stabilization time */
//for(i=0;i<5000;i++) { ; }
if (BKP_ReadBackupRegister(BKP_DR1) != 0xA5A5) {
/* Backup data register value is not correct or not yet programmed (when
the first time the program is executed) */
/* Allow access to BKP Domain */
PWR_BackupAccessCmd(ENABLE);
/* Reset Backup Domain */
BKP_DeInit();
/* Enable LSE */
RCC_LSEConfig(RCC_LSE_ON);
// enable LSI
RCC_LSICmd(ENABLE);
/* Wait till LSE is ready */
os_dly_wait(10*SEC);
// while (RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
// {
//
// }
if(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET)
{
// LSE is not ready
// select HSE/128 as RTC clock source
#warning dont' forget
//RCC_RTCCLKConfig(RCC_RTCCLKSource_HSE_Div128);
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
//RTC_SetPrescaler((HSE_VALUE/128)-1);
RTC_SetPrescaler(62499);
printf(''RTC clock source is HSE/128\r\n'');
}
else
{
/* Select LSE as RTC Clock Source */
RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);
/* Enable RTC Clock */
RCC_RTCCLKCmd(ENABLE);
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set RTC prescaler: set RTC period to 1sec */
RTC_SetPrescaler(32767); /* RTC period = RTCCLK/RTC_PR = (768 KHz)/(32767+1) */
printf(''RTC clock source is LSE\r\n'');
}
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
/* Set initial value */
RTC_SetCounter( (uint32_t)((11*60+55)*60) ); // here: 1st January 2000 11:55:00
/* Wait until last write operation on RTC registers has finished */
RTC_WaitForLastTask();
BKP_WriteBackupRegister(BKP_DR1, 0xA5A5);
/* Lock access to BKP Domain */
PWR_BackupAccessCmd(DISABLE);
} else {
/* Wait for RTC registers synchronization */
RTC_WaitForSynchro();
}