AnsweredAssumed Answered

STM32 How to set/use the Real Time Clock

Question asked by koss.david on Apr 3, 2017
Latest reply on Apr 3, 2017 by Nesrine M

Hello,

I am having issues with the Real Time clock. Occasionally it will miss its interrupt ( always when I stop the debugger )

 

I am not setting the date/time as I just want to have interrupt trigger a second from the current time

 

I have a catch so when I increment the seconds in the TR counter and it reaches 60 to set to zero so just want to make sure my code is correct

 

 

My code

      uint32_t value = 0U;

   
    // Disable the write protection for RTC registers
    RTC->WPR = 0xCAU;  
    RTC->WPR = 0x53U;

    // Disable the Alarm timer
    RTC->CR &= ~(RTC_CR_ALRAE);
 
    // Clear flag Wake-Up
    RTC->ISR &= ~( RTC_ISR_ALRAF );
  
    while((RTC->ISR & RTC_ISR_ALRAWF) != RTC_ISR_ALRAWF) /* (3) */
    {
    /* add time out here for a robust application */
    }
   
    RTC->ISR |= RTC_ISR_INIT;
    while((RTC->ISR & RTC_ISR_INITF) != RTC_ISR_INITF) /* (3) */
    {
    /* add time out here for a robust application */
    }
   
    value  = ( RTC->TR ) ;
    value = RTCBcdToByteSeconds(value); 
    value = value + 1;
    value = RTCByteToBcdSeconds( value );
    // Configure the AlarmA
    //RTC->ALRMAR = RTC_ALRMAR_MSK4 | RTC_ALRMAR_MSK3 | RTC_ALRMAR_MSK2 | RTC_ALRMAR_MSK1;
    RTC->ALRMAR = RTC_ALRMAR_MSK4 | RTC_ALRMAR_MSK3 | RTC_ALRMAR_MSK2 | value;
    //RTC->ALRMASSR = RTC_ALRMBSSR_MASKSS | ( RTC->SSR & RTC_ALRMASSR_SS );

 

    // Configure the Interrupt in the RTC_CR register
    RTC->CR |= RTC_CR_ALRAIE;
 
    // Enable the Wakeup Timer
    RTC->CR |= RTC_CR_ALRAE;

    RTC->ISR &= ~(RTC_ISR_INIT);
   
    // Enable the write protection for RTC registers
    RTC->WPR = 0xFFU;

Outcomes