Skip to main content
Scott Dev
Senior
December 24, 2018
Question

How do I read the RTC on the STM32L433 after power up.

  • December 24, 2018
  • 9 replies
  • 1650 views

Hi

I am using the RTC on the STM32L433 with battery backup. After the date/time been set and running , then either processor reset or repoert the unit the date/time resets to the default time. As it happens even after reset (power not disconnected) I know it isnt a repower issue.

Basically as I have a battery backup, I want to not set the date/time after repowering (or resetting) the processor , but simply keep the same date and time. How is this done?

Thanks

Scott

This topic has been closed for replies.

9 replies

waclawek.jan
Super User
December 27, 2018

Simply by not doing anything.

Ditched Cube yet?

JW

Scott Dev
Scott DevAuthor
Senior
December 27, 2018

Thanks, that worked, cheers. I thought it still had to be reinitialised.

I only use Cube to do the clock initialisation, but the rest by simply programming the registers. Still getting used to the stm32 , but getting there :)

Scott

Scott Dev
Scott DevAuthor
Senior
January 11, 2019

With the battery backup the time is kept up to date, but the date isnt. Anyone know why this is?

Regards

Scott

waclawek.jan
Super User
January 11, 2019

post details

Scott Dev
Scott DevAuthor
Senior
January 11, 2019

Sorry , I thought I posted the below...

After power up I check the backup register to see if previous been setup, if its not, then initialise RTC. Else read date and time.

Regards

Scott

//Routine for initialising RTC after power up

void InitRTC(void)

{

   if((RCC->APB1ENR1 & RCC_APB1ENR1_PWREN)==0)

      RCC->APB1ENR1 |= RCC_APB1ENR1_PWREN;

//check to see previously set

  if(RTC->BKP0R!=0x1954)

   {

     //nope

      if((PWR->CR1 & PWR_CR1_DBP) ==0)

      {

         PWR->CR1 |= PWR_CR1_DBP;

         while((PWR->CR1 & PWR_CR1_DBP)==0);

      }

      RCC->BDCR &= ~(RCC_BDCR_LSEON | RCC_BDCR_LSEBYP);

      RCC->BDCR |= RCC_BDCR_BDRST;

      RCC->BDCR &= ~RCC_BDCR_BDRST;

      while((RCC->BDCR & RCC_BDCR_LSERDY)==0)

         RCC->BDCR |= RCC_BDCR_LSEON;

      RCC->BDCR &= ~RCC_BDCR_RTCSEL;

      RCC->BDCR |= RCC_BDCR_RTCSEL_0;

      RCC->BDCR |= RCC_BDCR_RTCEN;

      RTC->WPR = 0xCA;

      RTC->WPR = 0x53;

      RTC->ISR |= RTC_ISR_INIT;

      if((RTC->ISR & RTC_ISR_INITF) == 0)

         RTC->ISR = (uint32_t)0xFFFFFFFFU;

      while((RTC->ISR & RTC_ISR_INITF) == 0);

      while((RTC->ISR & RTC_ISR_ALRAWF)==0);

      RTC->PRER = 0x007F00FF;

//set initialise date/time

      RTC->TR= 0U<<22 | 1U <<20 | 5U<<16 | 1U<<12 | 1U<<8 |1<< 4 | 5;

      RTC->DR= 1U<<20 | 8U <<16 | 1U << 13| 1U<<12 | 2U<<8 | 6U<<4 | 4;

      RTC->ISR &=~ RTC_ISR_INIT;

      RTC->WPR = 0xFF;

      RCC->BDCR |= RCC_BDCR_RTCEN;

      RTC->BKP0R=0x1954;

   }

   while((RTC->ISR & RTC_ISR_RSF)==0);

   ReadTime();

   ReadDate();

}

void ReadTime(void)

{

   Tme.Sec=(((RTC->TR & 0x7f) >> 4)*10)+(RTC->TR & 0xf);

   Tme.Min=((RTC->TR & 0x7f00) >> 8);

   Tme.Min=(((Tme.Min & 0x7f)>>4)*10)+(Tme.Min & 0xf);

   Tme.Hour=((RTC->TR & 0x7f0000) >> 16);

   Tme.Hour=(((Tme.Hour & 0x7f)>>4)*10)+(Tme.Hour & 0xf);

   Tme.Time=Tme.Hour*100+Tme.Min;

   Tme.FullTime=(long)Tme.Hour*10000+(long)Tme.Min*100+(long)Tme.Sec;

}

void ReadDate(void)

{

   Dte.Year=((RTC->DR >> 20)*10) + ((RTC->DR >>16) & 0xf);

   Dte.Month=((RTC->DR >> 12) & 1)*10 + ((RTC->DR >>8) & 0xf);

   Dte.Day=((RTC->DR >> 4) & 3)*10 + (RTC->DR & 0xf);

   Dte.Date=((long)Dte.Year+2000)*10000+(long)Dte.Month*100+(long)Dte.Day;

}

waclawek.jan
Super User
January 11, 2019

> time is kept up to date, but the date isnt

What exactly are the symptoms and how exactly do you observe them?

While this shouldn't cause problems you describe, I would recommend against multiple reading of the RTC_TR and RTC_DR registers. Read them once into a temporary variable to be dissected later. Mind the order of reading if BYPSHAD=0 ( To ensure consistency between the 3 values, reading either RTC_SSR or

RTC_TR locks the values in the higher-order calendar shadow registers until RTC_DR is

read.)

JW

Scott Dev
Scott DevAuthor
Senior
January 11, 2019

After I read the date/time into variables, I display them on a terminal application. Every few seconds I display date/time on the terminal. I never copied the date/time into variables as I am still learning the STM32 processor (previously a freescale, or NXP now person), so this is just temporary code just till I get used to each module. I switch off the power and after a few days, re-apply it , just to see the time is still accurate, but the date hasnt changed. Which I dont understand. I am not sure if I am doing anything wrong, but must be somewhere :(

Regards

Scott

waclawek.jan
Super User
January 11, 2019

Humm this is weird and I never experienced anything like this.

Can you please now read out the RTC registers using a debugger and post them, pointing out what is the expected value of DR?

JW

Scott Dev
Scott DevAuthor
Senior
January 15, 2019

It worked this time, this is weird. I couldnt see why it wouldnt , but I am sure it didnt the other day. I have currently set it, and will test it every few days to make sure all is what it should be. I will blame it on my New Year , we like to celibrate it here in Scotland ...

Scott