Feat/stm32 rtc#10103
Conversation
|
Force-pushed with an additional comment in stm32.ini to explain this line,
|
|
This looks very interesting. |
There was a problem hiding this comment.
Pull request overview
Adds STM32WL hardware-RTC support backed by the STM32duino STM32RTC library (LSE clock source) and fixes RTC header include collisions on STM32 case-insensitive filesystems.
Changes:
- Implement STM32WL (
ARCH_STM32WL)readFromRTC()/perhapsSetRTC()support usingSTM32RTCwhenHAS_RTC=1. - Enable RTC usage on specific STM32 variants by defining
HAS_RTCand per-boardRCC_LSEDRIVE_CONFIG. - Update STM32 build config and switch several includes from
<RTC.h>to"RTC.h"to avoid clashing with STM32rtc.h.
Reviewed changes
Copilot reviewed 8 out of 8 changed files in this pull request and generated 2 comments.
Show a summary per file
| File | Description |
|---|---|
| variants/stm32/wio-e5/variant.h | Enables RTC via HAS_RTC and sets LSE drive strength for Wio-E5. |
| variants/stm32/rak3172/variant.h | Enables RTC via HAS_RTC and sets LSE drive strength for RAK3172. |
| variants/stm32/russell/variant.h | Enables RTC via HAS_RTC and sets LSE drive strength for Russell. |
| variants/stm32/stm32.ini | Adds STM32RTC dependency and adjusts include search path to prefer project RTC.h. |
| src/gps/RTC.cpp | Adds STM32WL RTC read/set logic using STM32RTC (guarded by HAS_RTC). |
| src/mqtt/MQTT.cpp | Switches to "RTC.h" include form. |
| src/mesh/PhoneAPI.cpp | Switches to "RTC.h" include form. |
| src/graphics/draw/UIRenderer.cpp | Switches to "RTC.h" include form. |
Review feedback addressed + build test ✅Resolved both Copilot review threads:
Build-tested the two variants with RAM: 33.0% (21640 / 65536 bytes) |
|
Do tag as ai-generated, sorry. I use the RTC extensively at my day job for sleep of ~24 hours, the STM32 consumes vanishingly little power in deep sleeps like that (edit: usually over a year of battery life on 2x 19Ah Li-SOCl2 batteries if LoRa is used for just sensor rather than kept on for mesh). Not sure that particular use case is useful to Meshtastic in general but it may come in handy in future if STM32LowPower is implemented so that sensor roles can deep sleep. For now, it’s just plugging a hole in feature support for STM32 (RTC). Edit: That being said, the core implementation is CC generated:
However, I did point CC to my existing repos from other projects that use STM32RTC and also provide the LSE drive level init sequencing and cut out some register writes I know from my other projects are not needed. While these are added by myself for testing the implementation: |
|
I need to add another check to prevent the RTC readback from setting the clock to 1970, by checking against build time. It looks like STM32RTC sometimes thinks it's set when it was set with epoch 0 somehow. |
Add a proper ARCH_STM32WL branch in readFromRTC() and perhapsSetRTC() backed by the STM32duino STM32RTC library (1.9.0), using the hardware RTC peripheral with LSE_CLOCK as the clock source. The branch is guarded by HAS_RTC, which should be set to 1 only in variant files for boards that have a 32.768kHz LSE crystal populated. The LSE drive strength is configured per-board via RCC_LSEDRIVE_CONFIG (one of the RCC_LSEDRIVE_* values), set before begin() so it takes effect before the library starts the LSE oscillator. When HAS_RTC=0, the branch returns RTCSetResultNotSet immediately without touching zeroOffsetSecs, bypassing the broken gettimeofday() fallback. Add STM32duino RTC to stm32.ini lib_deps and HAL_RTC_MODULE_ENABLED to build_flags. LTO strips the unused RTC code on HAS_RTC=0 boards. Coincidentally this also fixes an issue for STM32 targets, where they previously fell through to a gettimeofday() fallback in readFromRTC() that returns millis()/1000 on STM32duino — not a Unix timestamp — silently corrupting zeroOffsetSecs on first boot. Also changed incorrect include convention for RTC.h in src/graphics/draw/UIRenderer.cpp and src/mesh/PhoneAPI.cpp which caused clashing includes with STM32's rtc.h on case-insensitive file systems. Build-tested on: * rak3172 * wio-e5 No regressions expected as HAS_RTC gates the inclusion of STM32RTC. Signed-off-by: Andrew Yong <me@ndoo.sg> Assisted-by: Claude:claude-sonnet-4-6
RAK3172 has a built-in 32.768 kHz LSE crystal so we can expect built-in RTC to be functional. Enable HAS_RTC and configure the LSE drive level for the RAK3172. LSE drive level from here: https://github.com/RAKWireless/RAK-STM32-RUI/blob/main/variants/WisDuo_RAK3172_Evaluation_Board/rui_inner_main.c Also added the same defines for russell which uses the RAK3172 module. Build-tested on: * rak3172 * wio-e5 (no changes, just a regression test) Couldn't build test russell due to build errors fixed in meshtastic#10097. Signed-off-by: Andrew Yong <me@ndoo.sg>
Wio-E5 has a built-in 32.768 kHz LSE crystal, so we can expect built-in RTC to be functional. Enable HAS_RTC and configure the LSE drive level for the Wio-E5. LSE drive level from here: https://github.com/Seeed-Studio/LoRaWan-E5-Node/blob/main/Projects/Applications/LoRaWAN/LoRaWAN_End_Node/Core/Src/main.c Tested on: * wio-e5 (Wio-E5 Dev Kit mini) Signed-off-by: Andrew Yong <me@ndoo.sg>
Mirror the triggerNodeInfoCheckOnTimeSource() pattern used in all other RTC branches so NodeInfo is rechecked when the STM32WL RTC first provides a valid time source. Signed-off-by: Andrew Yong <me@ndoo.sg>
|
I’ll re-open a new one once I have done some cleanup, working with time is always fraught with problems (and ironically, burns lots of time) |
Implement LSE-backed hardware RTC using STM32RTC library:
Add a proper ARCH_STM32WL branch in readFromRTC() and perhapsSetRTC() backed by the STM32duino STM32RTC library (1.9.0), using the hardware RTC peripheral with LSE_CLOCK as the clock source. The branch is guarded by HAS_RTC, which should be set to 1 only in variant files for boards that have a 32.768kHz LSE crystal populated.
The LSE drive strength is configured per-board via RCC_LSEDRIVE_CONFIG (one of the RCC_LSEDRIVE_* values), set before begin() so it takes effect before the library starts the LSE oscillator. When HAS_RTC=0, the branch returns RTCSetResultNotSet immediately without touching zeroOffsetSecs, bypassing the broken gettimeofday() fallback.
Add STM32duino RTC to stm32.ini lib_deps and HAL_RTC_MODULE_ENABLED to build_flags. LTO strips the unused RTC code on HAS_RTC=0 boards.
Coincidentally this also fixes an issue for STM32 targets, where they previously fell through to a gettimeofday() fallback in readFromRTC() that returns millis()/1000 on STM32duino — not a Unix timestamp — silently corrupting zeroOffsetSecs on first boot.
Also changed incorrect include convention for RTC.h in src/graphics/draw/UIRenderer.cpp and src/mesh/PhoneAPI.cpp which caused clashing includes with STM32's rtc.h on case-insensitive file systems.
Implemented to Wio-E5, RAK3172 and Russell variant (Russell build will fail due to another bug, fix in #10097).
🤝 Attestations