cancel
Showing results for 
Search instead for 
Did you mean: 

RTC can't set date and time. I've been used LL_RTC_TIME_SetHour but it not working (even with LL_RTC_DisableWriteProtection(RTC) and LL_RTC_EnableInitMode(RTC), as in UM1725 instruction). Thanks.

David Soares
Associate II
6 REPLIES 6
TDK
Guru

Chip? Is RTC clock enabled? Show code.

If you feel a post has answered your question, please click "Accept as Solution".

Which STM32?

The procedure is:

  • enable PWR clock in RCC
  • set DBP bit in PWR (typically in PWR_CR but that depends on particular STM32 model)
  • a few STM32 models have also RTCAPBEN bit in RCC, so if there's such then that needs to be set, too
  • RTC write protection unlock sequence, by consecutively writing the two "magic" numbers into RTC_WPR
  • set RTC into INIT mode, usually by setting RTC_ISR.INIT and then waiting until RTC_ISR.INITF reads back as 1; in some STM32 models this bit is in a different register
  • now you can set RTC_TR (and/or RTC_DR, RTC_SSR)
  • start RTC to tick by clearing INIT, here you may want to wait for INITF getting cleared, and perhaps perform the RSF resynchronization if BYPSHAD = 0
  • you may want to disable RTC write by writing non-unlocking value (traditionally 0xFF) into RTC_WPR
  • you may want to disable backup domain access by clearing DBP in PWR

I don't use Cube/LL, but it's open source so you can find easily the respective LL function/macro names yourself, if you want to stick to them.

JW

Good morning (in Brazil).

I'm using STM32F407ZET6 and CubeMX to configure.

Yes, I've been activated the RTC in CubeMX.

The situation is: The time are running. My problem is configurating the time / date.

Follow the code:

LL_RTC_DisableWriteProtection(RTC);

LL_RTC_EnableInitMode(RTC);

LL_RTC_TIME_SetHour(RTC, Hours_BCD);

LL_RTC_TIME_SetMinute(RTC, Minutes_BCD);

LL_RTC_TIME_SetSecond(RTC, Seconds_BCD);

LL_RTC_DATE_SetDay(RTC, Day_BCD);

LL_RTC_DATE_SetMonth(RTC, Month_BCD);

LL_RTC_DATE_SetYear(RTC, Year_BCD);

LL_RTC_DisableInitMode(RTC);

LL_RTC_EnableWriteProtection(RTC);

Good morning (in Brazil).

Which STM32? - STM32F407ZET6

I've been tried both HAL and LL drivers to configure the time / date. With HAL driver I got success in configuring, but the values resets when I turn the MCU off. But with the LL driver I can't even configure date nor time.

The code is above.

> The code is above.

Check if that code fulfills *all* steps I mentioned above.

In debugger, step through and check the respective registers/bits if they contain what they should.

JW

RCC->APB1ENR |= RCC_APB1ENR_PWREN;   // anywhere before calling the below function
 
  void RtcSetTime(uint_least8_t h, uint_least8_t m, uint_least8_t s) {
    uint32_t i;
 
    // unlock write to backup domain
    PWR->CR |= PWR_CR_DBP;
    // unlock RTC write protection after power-on reset
    RTC->WPR = 0xCA;
    RTC->WPR = 0x53;
    // set to init mode and wait until in sync -- it should take around 2 RTCCLK clocks
    RTC->ISR |= RTC_ISR_INIT;
    for (i = 0; i < 10000; i++) if ((RTC->ISR AND RTC_ISR_INITF) != 0) break;
    // set time
 
    RTC->TR = (TimeToBcdTime(h, m, s, 0)).all;  // modeAmPm is always 0 here, as we always set RTC in 24h format!
 
    // exit init mode - this should start the RTC
    RTC->ISR &= (uint32_t)~RTC_ISR_INIT;
      // maybe we should wait for resyc here - hw had cleared RSF bit during init mode, we should be waiting until it gets set by hardware, to ensure that subsequent reading will return valid time, but let's defer that to the routine which desires to read out the time
    // reenable the write protection for RTC registers */
    RTC->WPR = 0xFF;
    // lock write to backup domain
    PWR->CR &= ~PWR_CR_DBP;
  }

This is actually used to set time on an STM32F407.

JW