cancel
Showing results for 
Search instead for 
Did you mean: 

Trouble setting RTC from LSI to LSE on STM32F4

gw
Associate II
Posted on September 08, 2016 at 17:52

Hi,

I have a problem where I'm trying to detect the presence of the low speed external oscillator, and to use it if it is installed. On bootup, it does the following:

   * Turn LSI on (it always defaults to off)

   * If RTC is set up already then great. Job done.

   * If not, set up the RTC with the LSI, but turn the LSE on

   * Around 1 sec later, in SysTick, check if the LSE is working

   * If it isn't, turn it off

   * If it is, reset backup domain, switch over to LSE and disable LSI

When I do this on STM32F1 it works great. On the STM32F4 it works fine for just the LSI, but when the LSE is installed it's left in a state where the registers say:

* LSI is off

* LSE in on, and working

* LSE is set as the RTC clock source

* RTC is enabled

However the RTC itself is no longer working.

Does anyone have any ideas what the problem could be? When I reset the backup domain (which I have to in order to switch the RTC over) could I have missed out some configuration?

The odd thing is that if I reset the board after this, everything comes up fine - so it's as if I got the RTC into some strange state.

RTC Initialisation code:

https://github.com/espruino/Espruino/blob/6ec6946bdbb7e35d19037615d421eda5d19a44f4/targets/stm32/jshardware.c&sharpL1111

Code that runs after ~1 sec:

https://github.com/espruino/Espruino/blob/6ec6946bdbb7e35d19037615d421eda5d19a44f4/targets/stm32/jshardware.c&sharpL736

And yes, one answer would be just to pause the whole MCU while I waited for the LSE - but I need a fast startup time and to be able to use the RTC during that time (so I have to run it off the LSI until the LSE has started).

#rtc #stm32
7 REPLIES 7
Posted on September 08, 2016 at 19:03

> * RTC is enabled

>

> However the RTC itself is no longer working.

Post content of relevant registers at that point.

JW

Walid FTITI_O
Senior II
Posted on September 08, 2016 at 19:49

Hi Gordon Williams,

Since the code that you provided managed RTC for more than one device, it is not so obvious to identify the root cause. Try to clear and keep only RTC part in a new main.

Meanwhile, after a quick check , you may check the following points:

* Assynchronous and Synchronous predivs setting for the STM32F4 case

 * Why you are setting alarm with STM32F1 and WUT in case of STM32F4 ? Is RTC not supposed to behave the same ?

-Hannibal-

gw
Associate II
Posted on September 08, 2016 at 20:10

Post content of relevant registers at that point.

Which ones would you like? I posted the specific parts of registers I thought were relevant already but I can post the contents of any registers you'd like.

> Since the code that you provided managed RTC for more than one device, it is not so obvious to identify the root cause. Try to clear and keep only RTC part in a new main.

It's just anything that is ifdef'd STM32F4 and not ifdef'd STM32F1. I know it's a little frustrating but it might also help knowing that it works on F1 but not F4 - and you'd lose that if I deleted the ifdefs for you.

> Assynchronous and Synchronous predivs setting for the STM32F4 case

Do these get cleared when the backup register flags are set? Also, I'd have thought they would only be able to change the speed of the RTC, not stop it completely which appears to be what's happening?

> Why you are setting alarm with STM32F1 and WUT in case of STM32F4 ? Is RTC not supposed to behave the same ?

I didn't think the F1 had a wake up timer? I can't use an alarm on the F4 because I want higher accuracy than 1 second on F4 (it doesn't seem possible on F1).

Even so, that shouldn't be relevant here as it's broken long before that point. It's literally just the two areas I pointed at.

Is there a way to set the RTC prescaler and clock source without resetting everything first? That could make life a lot easier.
Posted on September 08, 2016 at 20:28

> > 

Post content of relevant registers at that point.

>

> Which ones would you like?

> I posted the specific parts of registers I thought were relevant already

Where?

I mean, contents of registers read out at the moment when you judged RTC inoperative.

> but I can post the contents of any registers you'd like.

*I* don't need any. Post what *you* deem relevant - post all of RTC and RTC- and LSE- specific from RCC and PWR.

JW

gw
Associate II
Posted on September 09, 2016 at 10:53

> Where?

I posted LSE state, clock selection, etc - I didn't post the raw register contents though.

> Post what *you* deem relevant

If I knew what was relevant I'd be a lot closer to having the answer already 🙂

I just read all the RTC/RCC/PWR registers when it was working (after reset) and when it was broken (after power on), and diffed them.

The only differences were:

  • Time + Subsecond (which you'd expect)
  • ISR
Turns out the RTC was in initialisation mode, which explains why it is stopped. However I wasn't able to just reset the 'init' flag by writing to the register (write protection).

Looks like RTC_WaitForSynchro after RTC_SetDate seems to be quite vital

Posted on September 09, 2016 at 11:15

So, problem solved?

> Looks like RTC_WaitForSynchro after RTC_SetDate seems to be quite vital

Surely something well-described in the ''library's'' documentation...?

JW

gw
Associate II
Posted on September 09, 2016 at 11:29

Yes, problem solved.