RTC initialisation and wakeup from external events
If the Pyboard is woken from standby by the tamper pin X18, the RTC is reset to 1st January 2014 regardless of any setting performed in code.
It appears impossible to prevent this from Python as it occurs in rtc_init() which is called from main.c. The problem occurs because rtc_init() checks backup register 0 for a specific value 0x32f2 to see if the RTC has already been set. If the value is not correct it assumes the Pyboard has been powered up, sets the RTC to that date, and sets the value of backup register 0 to 0x32f2 to ensure that the RTC continues to run through reset events.
Unfortunately the tamper pin clears the backup registers, so on recovery from standby rtc_init() runs again, detects that the register does not hold 0x32f2 and resets the RTC.
I have produced a build where RTC initialisation occurs in the constructor. This has a boolean argument defaulting True: if present and set False, initialisation is inhibited. This allows the user to take control of the detection of boot events and initialisation of the RTC (power up can be detected by checking the contents of backup RAM which are unaffected by tamper events).
If the argument is absent or True initialisation and boot detection work as at present so existing code should be unaffected.
If this approach meets with approval I can submit a PR.
stmhal: Improve RTC startup
We can improve the way stmhal starts up and initialises the RTC. This is tied into the way that the device resets, either by power up, hard reset, soft reset, or exiting standby mode. Some desired features:
- Detect exactly when RTC initialisation is needed, and when calendar reset is need. This may already be solved but needs to be tidied up.
- Support LSE/LSI detection on startup, see #1546.
- Perform LSE startup "in the background" while booting, because currently it can take up to 500ms.
- Provide a way to indicate to the user what the reset cause was. machine.reset_cause() and machine.wake_reason() are used for this.
- It would be good to set the CPU clock to it's previous value when exiting standby mode, since in low power applications you wan't a slower clock and having it 168MHz on boot (even for a few ms) wastes power. This is not really RTC related, but is related to detecting what kind of reset occurred.