AnsweredAssumed Answered

STM32 RTC alarm and counting problem

Question asked by kiran.adithya on Apr 1, 2013
Latest reply on Apr 1, 2013 by Clive One
Hi, I want to use the RTC's alarm A for waking up from standby. However, I cannot get the alarm interrupt. I am initialising timer 3 for interrupting every 5 seconds and reading the rtc within the interrupt handler. However, if the alarm is set, then the RTC time registers are not counting at all. My code is pasted below. Please help:


void RTC_Initialise()
{
    
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_PWR, ENABLE);
 
  /* Allow access to RTC */
  PWR_RTCAccessCmd(ENABLE);
   
  /*!< LSE Enable */
  RCC_LSICmd(ENABLE);
 
  /*!< Wait till LSE is ready */
  while (RCC_GetFlagStatus(RCC_FLAG_LSIRDY) == RESET)
  {}
 
  /*!< RTC Clock Source Selection */
  RCC_RTCCLKConfig(RCC_RTCCLKSource_LSI);
 
  /* Enable the RTC Clock */
  RCC_RTCCLKCmd(ENABLE);
 
  /* Wait for RTC APB registers synchronisation */
  if(RTC_WaitForSynchro() != SUCCESS)
  {
    // TODO: Handle RTC Error
  }
 
}

Code that sets up the RTC alarm:
void RTC_Set_Alarm()
{
    RTC_TimeTypeDef time;
    RTC_AlarmTypeDef alarm;
    EXTI_InitTypeDef exti_init;
     
    INIT_NVIC_Disable_Peripheral_Interrupt(TIM3_IRQn,0,0);
     
    time.RTC_Hours = 11;
    time.RTC_Minutes = 0;
    time.RTC_Seconds = 0;
    time.RTC_H12 = RTC_H12_AM;
     
    alarm.RTC_AlarmTime = time ;
    alarm.RTC_AlarmMask = RTC_AlarmMask_DateWeekDay|RTC_AlarmMask_Seconds  ;
    alarm.RTC_AlarmDateWeekDaySel = RTC_AlarmDateWeekDaySel_Date;
    alarm.RTC_AlarmDateWeekDay = RTC_current_time.date;
     
    /* I had to insert all this stuff here otherwise the alarm register wouldnt initialise */    
    RTC_WriteProtectionCmd(DISABLE);
    RTC_EnterInitMode();
    RTC_SetAlarm(RTC_Format_BIN, RTC_Alarm_A, &alarm);
    RTC_WriteProtectionCmd(ENABLE);
    RTC_ExitInitMode();
    RTC_ITConfig(RTC_IT_ALRA,ENABLE);
    RTC_AlarmCmd(RTC_Alarm_A,ENABLE);
        
    EXTI_StructInit(&exti_init);
    exti_init.EXTI_Line = EXTI_Line17;
    exti_init.EXTI_Trigger = EXTI_Trigger_Falling;
    exti_init.EXTI_LineCmd = ENABLE;
    EXTI_Init(&exti_init);
     
    INIT_NVIC_Enable_Peripheral_Interrupt(RTC_Alarm_IRQn,0,1);
    INIT_NVIC_Enable_Peripheral_Interrupt(TIM3_IRQn,0,0);   
}

Timer interrupt to read RTC and the function that reads RTC values:
void TIM3_IRQHandler(void)
{
  TIM_ClearFlag(TIM3,TIM_FLAG_Update | TIM_FLAG_Trigger) ;
  TIM_ClearITPendingBit( TIM3,TIM_IT_Update | TIM_IT_Trigger ) ;
  /* The waitforsynchro function was causing problems so I commented it */
  //RTC_WaitForSynchro();
  RTC_Read();
}
 
void RTC_Read()
{
  /* Structure variables to hold read date and time */
  RTC_DateTypeDef RTC_DateStruct;
  RTC_TimeTypeDef RTC_TimeStruct;
   
  /* Reads the Date and Time from the RTC registers */
  RTC_GetDate(RTC_Format_BIN, &RTC_DateStruct);
  DELAY_ms(5);
   
  RTC_GetTime(RTC_Format_BIN, &RTC_TimeStruct); 
  DELAY_ms(5);
   
  /* RTC_current_time is a structure defined to hold both date and time together */
  RTC_current_time.year  = ( RTC_DateStruct.RTC_Year );
  RTC_current_time.month = ( RTC_DateStruct.RTC_Month );
  RTC_current_time.date  = ( RTC_DateStruct.RTC_Date );
 
  RTC_current_time.hour   = ( RTC_TimeStruct.RTC_Hours );
  RTC_current_time.minute = ( RTC_TimeStruct.RTC_Minutes );
  RTC_current_time.second = ( RTC_TimeStruct.RTC_Seconds );
}

Outcomes