THE FINAL SOLUTION: TRUE RTC ON PYBOARD

The official pyboard running MicroPython.
This is the reference design and main target board for MicroPython.
You can buy one at the store.
Target audience: Users with a pyboard.
Post Reply
User avatar
Franklin
Posts: 1
Joined: Wed Oct 09, 2019 12:54 pm
Contact:

THE FINAL SOLUTION: TRUE RTC ON PYBOARD

Post by Franklin » Wed Oct 09, 2019 1:37 pm

Hello everyone, I'm Santa Claus with a big gift for you all! A solution to make the PyBoard's RTC working like eveyone wish it have to but no ones had a solution to do it!

First of all: Sry my english...

Ok. Let's start!

If you alredy had tried to use the RTC with a battery backup to make the time counting even if there is no power supply, you may probably hit you in the biggest proble of the milenary: The RTC keep it's time but don't continue to run.
But why?
the solution is inside the deepest registers of the STM32.
Firstly, the batterry has to be connected beetween VBACK and GND. With that, all the RAM registers of the STM32 will be kept in memory (like you're rtc object that you've instancied from pyb.RTC()). To make it "forwarding" the RTC, you have, like written in the page 131 of the link here:https://www.st.com/content/ccc/resource ... 171190.pdf
You have to modify the Backup domain control register (RCC_BDCR) to enable the RTC of using the VBAT (VBACK on PyBoard) power input.

Passing the details, you will have to do some asembler to modify the two bits of RTCSEL to 01, and in code, that looks like that:

Code: Select all

import stm

@micropython.asm_thumb
def enableRTC():
    movwt(r2, stm.RCC)
    ldr(r0, [r2, stm.RCC_BDCR])
    movwt(r1, 0xFFFFFCFF)
    and_(r0, r1)
    movwt(r1, 0x00000100)
    orr(r0, r1)
    str(r0, [r2, stm.RCC_BDCR])

enableRTC()
Link of asembler actions doc:
https://docs.micropython.org/en/latest/ ... index.html

Then, you just have to setup you're RTC with a simple pyb.RTC.datetime([year, month, day, weekday, hours, minutes, seconds, subseconds]) and the magic happend! The RTC will work infinitly with a ~1[uA] curent consumption.

Have a nice developpment time!

smhodge
Posts: 14
Joined: Tue Jan 22, 2019 2:16 am

Re: THE FINAL SOLUTION: TRUE RTC ON PYBOARD

Post by smhodge » Wed Oct 09, 2019 6:58 pm

Thanks for posting this. It made me double-check my RTC operation because I have been suspecting the backup battery is not doing its job. Sure enough it is not. Yes, my CR2032, measuring a nice 3.0+ volts, is connected to the VBACK pin (not the VBAT one), and sure enough the date/time set with RTC.datetime((tuple)) does not survive a power cycle. I am running the latest version of upython, 1.11.

chuckbook
Posts: 105
Joined: Fri Oct 30, 2015 11:55 pm

Re: THE FINAL SOLUTION: TRUE RTC ON PYBOARD

Post by chuckbook » Thu Oct 10, 2019 9:23 am

RTC backup battery works pretty well on PYBV11 but there are some caveats:
  • RTC driver inside MPY has a multi level fallback strategy to handle various hardware options and even HW faults
  • if 32kHz crystal isn't installed, broken or shows a power-up delay above a certain limit, RTC clock uses LSI oscillator.
  • Once a specific oscillator is selected, it isn't changed anymore as long as the oscillator isn't power cycled
  • Once the LSI clock is selected, a full power cycle is needed (including RTC backup voltage!).
  • Any power cycle to reset RTC clock system needs an off period sufficient to drain all charge. Even LSI osc. runs quite some time after power removal.
  • To start a PYBV11 with RTC backup enabled, first attach RTC backup battery then power-up main supply.
  • After a full power cycle stm.mem32[RCC+RCC_BDCR] will read 0x00000003 if LSE osc. is up and running ok.
  • The first access to RTC.datetime() selects the adequate RTC clock.
  • If LSE is selected, stm.mem32[RCC+RCC_BDCR] will read 0x00008103.
Note:
Due to the fact that LSE osc. often has a very long startup delay, the first access to RTC may happen before LSE startup completes.
To avoid this situation, first RTC access can be either postponed or LSE startup timeout can be increased.
Note2:
Once the RTC clock mode is selected, it will be locked in this mode until the next full power cycle.

Post Reply