cancel
Showing results for 
Search instead for 
Did you mean: 

Cant seem to get RTC operating.

Scott Dev
Senior
Posted on August 04, 2017 at 16:43

Hi

   Im still new to the STM32, and now working on the RTC.  The problem I am having is when I read the time from the RTC, it never changes.  I Initialise the RTC, and every second I read from the rtc and display its values. I expected to see the time changing every second , but always get the same values back. Anyone please let me know what I am doing wrong?

Many Thanks

Scott

MX_RTC_Init(); //below

    RTC_TimeTypeDef sTime;

    __HAL_RCC_RTC_ENABLE();

  while (1) //main loop

 {

    if(Second)

    {

       Second=0;

      HAL_RTC_GetTime( &hrtc, &sTime, RTC_FORMAT_BIN );

      //display value of the rtc time

    }

}

/////////////////////////////////////////////

static void MX_RTC_Init(void)

{

  RTC_TimeTypeDef sTime;

  RTC_DateTypeDef sDate;

 RTC_DateTypeDef dt;

RTC_TimeTypeDef tt;

    

hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

hrtc.Init.AsynchPrediv = 127;

hrtc.Init.SynchPrediv = 255;

hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

HAL_RTC_Init(&hrtc);

    

HAL_RTC_SetTime(&hrtc, &tt, RTC_FORMAT_BIN);// or bcd ofcourse

HAL_RTC_SetDate(&hrtc,&dt, RTC_FORMAT_BIN);// or bcd ofcourse

    /**Initialize RTC Only

    */

  hrtc.Instance = RTC;

  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;

  hrtc.Init.AsynchPrediv = 127;

  hrtc.Init.SynchPrediv = 255;

  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;

  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;

  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;

  if (HAL_RTC_Init(&hrtc) != HAL_OK)

  {

  //  _Error_Handler(__FILE__, __LINE__);

  }

    /**Initialize RTC and set the Time and Date

    */

  if(HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2)

        {

  sTime.Hours = 0;

  sTime.Minutes = 0;

  sTime.Seconds = 0;

  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;

  sTime.StoreOperation = RTC_STOREOPERATION_SET;

  if (HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BIN) != HAL_OK)

  {

 //   _Error_Handler(__FILE__, __LINE__);

  }

  sDate.WeekDay = RTC_WEEKDAY_TUESDAY;

  sDate.Month = RTC_MONTH_JULY;

  sDate.Date = 9;

  sDate.Year = 17;

  if (HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BIN) != HAL_OK)

  {

//    _Error_Handler(__FILE__, __LINE__);

  }

    HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);

  }
8 REPLIES 8
Posted on August 04, 2017 at 17:51

Hello!

You would help us(and you also) if you provide more details about your hardware .

Do you use a crystal for LSE ? VBAT if exists, is connected to a power source?

If you use CubeMX , please provide to the comunity your.IOC file.

Posted on August 04, 2017 at 19:03

Hello again !!

 __HAL_RCC_RTC_ENABLE();  must be called prior to initialise the rtc .

Posted on August 04, 2017 at 17:58

Hi

  Sorry, I should have mentioned these things.

I am using CubeMX, and processor STm32L073 on a nucleo L073 board. I only have the 32Kz crystal on board. Dont know why it would matter if VBatt was the source as wouldnt effect the rtc. But anyway, just using the usb cable

regards

Scott

Posted on August 04, 2017 at 18:16

>>

Dont know why it would matter if VBatt was the source as wouldnt effect the rtc.

LSE is in the low power domain, of import if core enters sleep or standby.

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
Posted on August 04, 2017 at 18:31

Ah, I see. As I am just trying to get used to all the different modules one at a time, I am just concentrating on being powered all the time, and will later look at sleep,standby at a later stage.  Now it makes sense to me why the VBATT was asked, sorry about that.

Thanks

Scott

Posted on August 04, 2017 at 19:31

Hi Vangelis

  Thanks for your reply.

I moved it to just before the initialisation of the rtc, but still the same. I found some documentation where it says RTC has to be disabled first. Values 0xca then 0x53 has to be written to the  RTC_WPR register, then after initialisation clear the INIT bit in the RTC_ISR . I am not sure if any of the commands above does this. Also, Im not sure what the above commands does (I used someones else post of the code to test)

HAL_RTCEx_BKUPRead(&hrtc, RTC_BKP_DR0) != 0x32F2

HAL_RTCEx_BKUPWrite(&hrtc,RTC_BKP_DR0,0x32F2);

I am assuming its the backup registers, but not sure exactly what they do.

Thanks

Scott

Posted on August 04, 2017 at 19:56

Hi Scott!

At least, check with your debug functionality the RTC registers. 

I think you need

https://my.st.com/content/my_st_com/en/products/embedded-software/mcus-embedded-software/stm32-embedded-software/stm32cube-embedded-software/stm32cubel0.html

from ST to study a lot of examples. (RTC and others)

You can also download

https://my.st.com/content/my_st_com/en/products/development-tools/software-development-tools/stm32-software-development-tools/stm32-configurators-and-code-generators/stm32cubemx.html

to produce automated initialization code for your projects.

Rgrds

Posted on August 05, 2017 at 05:58

 

JW