cancel
Showing results for 
Search instead for 
Did you mean: 

prescalar for RTC has no effect

red_bambakadis
Associate
Posted on January 24, 2013 at 01:58

Recently migrated from STM32F1 to STM32F4, trying to set up an alarm which fires an interrupt at 512Hz using LSE with prescalar (RTC_AsynchPrediv, RTC_SynchPrediv), more or less as follows:

  /* Enable the PWR clock */

  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);

  /* Allow access to RTC */

  PWR_BackupAccessCmd(ENABLE);

  /* Reset RTC Domain */

  RCC_BackupResetCmd(ENABLE);

  RCC_BackupResetCmd(DISABLE);

  /* Enable the LSE OSC */

  RCC_LSEConfig(RCC_LSE_ON);

  /* Wait till LSE is ready */  

  while(RCC_GetFlagStatus(RCC_FLAG_LSERDY) == RESET) {}

  /* Select the RTC Clock Source */

 RCC_RTCCLKConfig(RCC_RTCCLKSource_LSE);

  /* Configure the RTC data register and RTC prescaler */

  RTC_InitStructure.RTC_AsynchPrediv = 0x04;

  RTC_InitStructure.RTC_SynchPrediv  = 0x04; // 32.768kHz 

  RTC_InitStructure.RTC_HourFormat   = RTC_HourFormat_24;

  RTC_Init(&RTC_InitStructure);

/* Enable the RTC Clock */

  RCC_RTCCLKCmd(ENABLE);

  /* Wait for RTC APB registers synchronisation */

  RTC_WaitForSynchro();

Subsequently, the alarm is configured as follows:

  EXTI_InitTypeDef EXTI_InitStructure;

  RTC_AlarmTypeDef RTC_AlarmStructure;

  NVIC_InitTypeDef NVIC_InitStructure;

  

  /* EXTI configuration */

  EXTI_ClearITPendingBit(EXTI_Line17);

  EXTI_InitStructure.EXTI_Line = EXTI_Line17;

  EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;

  EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Rising;

  EXTI_InitStructure.EXTI_LineCmd = ENABLE;

  EXTI_Init(&EXTI_InitStructure);

  

  /* Enable the RTC Alarm Interrupt */

  NVIC_InitStructure.NVIC_IRQChannel = RTC_Alarm_IRQn;

  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;

  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;

  NVIC_Init(&NVIC_InitStructure);

 

  /* Set the alarmA Masks */

  RTC_AlarmStructure.RTC_AlarmMask = RTC_AlarmMask_All;

  RTC_SetAlarm(RTC_Format_BCD, RTC_Alarm_A, &RTC_AlarmStructure);

  

  /* Enable AlarmA interrupt */

  RTC_ITConfig(RTC_IT_ALRA, ENABLE);

  /* Enable the alarm  A */

  RTC_AlarmCmd(RTC_Alarm_A, ENABLE);

The problem is that the following simple alarm interrupt only fires twice a second, regardless of RTC_AsynchPrediv and RTC_SynchPrediv:

void RTC_Alarm_IRQHandler(void)

{

  RTC_ClearITPendingBit(RTC_IT_ALRA);

  RTCAlarmCount++;

  EXTI_ClearITPendingBit(EXTI_Line17);

}

1 REPLY 1
Posted on January 25, 2013 at 09:13

I don't understand what do you intend to achieve. Do you want to increment the RTC at a significantly faster rate than once per second, and then want to trigger an alarm at every increment of RTC? Is this what you expect and it does not work as you expected?

While this might be expected to work, and may be irritating if does not work (may be even called a bug or at least a documentation issue, if confirmed); why don't you use the RTC as it was intended, incrementing once per the one real-world-second, and then either set the alarm to trigger at sub-second rate (using the sub-second mask register), or, probably better, use the periodic wakeup feature?

JW

PS. IMHO, dropping the ''library'' and working directly out of the manual on the registers might help understanding of the RTC's working, but YMMV.