1
\$\begingroup\$

I am working on an application that must keep track of time (using a RTC), and will be uploaded on a STM32L052K8.

When I was making my hardware I just assumed that I will use the low speed internal (LSI) clock of the MCU. However, now that I am writing the code I saw something that really worries me.

According to the datasheet of the MCU the LSI oscillator characteristics are:

LSI oscillator characteristics

If this is indeed the case then does that mean that my time base clock will be anywhere between 26kHz and 56kHz? If this is the case, then the RTC is completely useless. If this is not the case then how does ST manage to get the a high accuracy RTC which runs on such an oscillator? Also according to the datasheet the nominal frequency of the LSI oscillator is 38kHz, but according to STM32CubeMX it is 37kHz as seen in this picture below.

RTC setup STM32CubeMX

What is the nominal frequency I should assume?

I really hope this has some solution with an acceptable accuracy (even +/- 1% is enough for me).

\$\endgroup\$
6
  • \$\begingroup\$ MSI has much tighter specifications. So if you can use MSI instead of LSI, or calibrate LSI against MSI periodically you should get more accurate time interval measurement. I am running on MSI at default 2.097 MHz without any power saving effort and still around 650uA with UART and ADC enabled continuously. Not sure how low you need to get on power. But MSI can be run at much lower frequencies and unused peripherals can be de-powered. It might be possible to get down to 100uA or less without power saving modes. And MSI can be used in standby also for even more savings. \$\endgroup\$
    – user57037
    Commented Apr 9, 2021 at 19:07
  • \$\begingroup\$ @mkeith Indeed the MSI is much better, but I cannot use the MSI with the RTC unfortunately (at least according to the STM32CubeMX software)... And as far as I know you cannot use the synchronization/compensation functions with the LSI. Power for the RTC is not really a concern for me (for now). I just want some rough indication of the time that can be kept in synch for at least 12 hours. If you know of a way that I can synchronize the LSI with the MSI (or use the MSI directly) I would be very grateful if you share it. \$\endgroup\$ Commented Apr 10, 2021 at 8:19
  • \$\begingroup\$ Well, if you are using CubeMX to generate code, there is an elapsed time counter available. The systick counter. (HAL_GetSystick()). If you boot and run on the MSI clock, you can just count milliseconds and do basically anything you want, time wise, in code. Another option is to run a PWM output based on MSI clock, and loop that back to LSE input (like with a jumper wire). There could be some sequencing there. You would need to get the PWM output going before switching the RTC over to LSE. \$\endgroup\$
    – user57037
    Commented Apr 10, 2021 at 8:40
  • 1
    \$\begingroup\$ I don't know if you are using interrupts or polling or whatever. But systick generates an interrupt ever millisecond, and by default the HAL code will increment a variable every millisecond. This is a uint32_t. So every time that variable (the systick counter) increases by 1000, that is one elapsed second. So you can implement your own second counter using nothing but systick. See what I mean? \$\endgroup\$
    – user57037
    Commented Apr 10, 2021 at 8:45
  • \$\begingroup\$ @mkeith Thank you very much for the suggestion! Indeed I will do exactly this. With just a little bit of tweaking the systick should do everything I actually need. \$\endgroup\$ Commented Apr 10, 2021 at 14:37

1 Answer 1

5
\$\begingroup\$

The LSI cannot keep the time accurately.

Accurate timekeeping always requires a crystal.

All RTCs typically use a 32768 Hz crystal for that, be it a separate crystal or a crystal molded into same plastic package as a complete module.

The evaluation boards from ST are no exception to this, there usually is a crystal already mounted on boards or at least a place for mounting it if needed.

I am actually a bit surprised it is possible to select something else than LSE for RTC timing.

\$\endgroup\$
6
  • \$\begingroup\$ Maybe LSI can be calibrated if an external clock reference is available. RTC module has calibration feature, but I'm not sure if its range can cover all LSI deviations. Some devices which are powered from mains can calibrate themselves using the well known mains frequency as reference. But I'm not sure if this method is applicable to STM32 devices. \$\endgroup\$
    – Tagli
    Commented Apr 9, 2021 at 18:02
  • \$\begingroup\$ The DS3231 RTC has a (temperature-compensated) crystal oscillator incorporated in a 16 SO package, so no external crystal for that one. \$\endgroup\$ Commented Apr 9, 2021 at 18:35
  • \$\begingroup\$ STM32L011x3/4 has a multi speed internal clock (MSI) that is good to 0.5% at room temp and 3.3V. It does have sensitivity to temperature and vcc and it probably drifts over time as well. But it is pretty good for an internal clock. \$\endgroup\$
    – user57037
    Commented Apr 9, 2021 at 18:53
  • 1
    \$\begingroup\$ @AndrewMorton Indeed, there is no external crystal because it is integrated into the plastic molding. It still uses a crystal, that is external to the silicon die. I will change my wording to suit better. \$\endgroup\$
    – Justme
    Commented Apr 9, 2021 at 19:31
  • \$\begingroup\$ @Justme Hmm I was afraid that the answer will be this one.. Still I wonder why then the MCU that I use advertises itself with a RTC capable of running from LSI. I guess 'capable' doesn't mean 'usable' in this case.... \$\endgroup\$ Commented Apr 10, 2021 at 8:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.