2

I use:

  • STM32L071K8U3
  • Segger JLink + Segger Ozone + SWD with 4 MHz
  • Cube Ide
  • External LSE or internal LSE, same problem.

I wrote firmware that displays the time using the RTC. To update the display, I check if the time has changed, like this:

if (hrtc.Instance->TR != trBefore) {
  trBefore = hrtc.Instance->TR;
  ...
}

Or like this (more portable):

if (HAL_RTC_GetTime(&hrtc, &rtcTime, RTC_FORMAT_BCD) != HAL_OK) {
  Error_Handler();
}
      
if (rtcTime.Seconds != secondsBefore) {
  secondsBefore = rtcTime.Seconds;
  ...
}

Everything works fine, except that the time is not changing.

Using the debugger, I found that the RTC registers don't change.

If I use breakpoints and step into this HAL_RTC_GetTime and observe the RTC registers, then they change, and as a consequence, my clock display is updated with the new time value.

Maybe related to this: if I use Cube Ide Eclipse CDT debugger instead of Segger Ozone, I sometimes get an error message "interrupt failed". I suspect this is related; something weird is going on here.

Also, if I completely disconnect the debugger, and flash the firmware with JFlash, everything works properly, except the time is not advancing on my display.

ocrdu
  • 8,705
  • 21
  • 30
  • 42
Oliver Richter
  • 141
  • 1
  • 7
  • The question is: How to get the RTC running correctly. – Oliver Richter Aug 24 '22 at 12:46
  • 1
    Does your program behave differently if you rebuild with all compiler optimizations disabled? If so then perhaps some of your memory mapped registers or variables should be declared volatile. – kkrambo Aug 24 '22 at 13:54

1 Answers1

3

The answer is here.

In short, RTC time/date registers have to be read all of them (SSR, TR, DR), and twice.

Otherwise a lock mechanism, which is supposed to prevent data inconsistency, disconnects the registers from the APB bus, so the clock appears not to be running.

ocrdu
  • 8,705
  • 21
  • 30
  • 42
Oliver Richter
  • 141
  • 1
  • 7