From fa52f1e9860d0b20a4066c56b1eed96720425ebe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Fabian=20H=C3=BC=C3=9Fler?= Date: Thu, 7 Oct 2021 21:08:27 +0200 Subject: [PATCH] cpu/stm32: Consider VBAT on CPU init --- cpu/stm32/cpu_init.c | 24 ++++++++++++++++++++---- drivers/periph_common/init.c | 7 +++++++ 2 files changed, 27 insertions(+), 4 deletions(-) diff --git a/cpu/stm32/cpu_init.c b/cpu/stm32/cpu_init.c index b6e7fdd209..c85d3a0f61 100644 --- a/cpu/stm32/cpu_init.c +++ b/cpu/stm32/cpu_init.c @@ -38,6 +38,7 @@ #include "periph_cpu.h" #include "periph/init.h" #include "periph/gpio.h" +#include "periph/vbat.h" #include "board.h" #include "pm_layered.h" @@ -395,14 +396,29 @@ void backup_ram_init(void) #define BACKUP_RAM_MAGIC {'R', 'I', 'O', 'T'} #endif -bool cpu_woke_from_backup(void) -{ +static inline bool _backup_battery_connected(void) { +#if IS_USED(MODULE_PERIPH_VBAT) + vbat_init(); /* early use of VBAT requires init() */ + return !vbat_is_empty(); +#endif + return false; +} + +bool cpu_woke_from_backup(void) { #if IS_ACTIVE(CPU_HAS_BACKUP_RAM) static const char _signature[] BACKUP_RAM_DATA = BACKUP_RAM_MAGIC; - /* switch off regulator to save power */ + if (_backup_battery_connected()) { + /* in case the board has a backup battery the regulator must be on + to mitigate (unexpected) outage of VDD, so RTC register and + backup domain register contents are not lost */ + pm_backup_regulator_on(); + } + else { #ifndef RIOTBOOT - pm_backup_regulator_off(); + /* switch off regulator to save power */ + pm_backup_regulator_off(); #endif + } for (unsigned i = 0; i < sizeof(_signature); i++) { if (_signature[i] != ((char[])BACKUP_RAM_MAGIC)[i]) { return false; diff --git a/drivers/periph_common/init.c b/drivers/periph_common/init.c index 8fbd394f30..9d88170b72 100644 --- a/drivers/periph_common/init.c +++ b/drivers/periph_common/init.c @@ -50,6 +50,9 @@ #ifdef MODULE_PERIPH_INIT_PTP #include "periph/ptp.h" #endif +#ifdef MODULE_PERIPH_INIT_VBAT +#include "periph/vbat.h" +#endif #endif /* MODULE_PERIPH_INIT */ void periph_init(void) @@ -105,5 +108,9 @@ void periph_init(void) ptp_init(); #endif +#if defined(MODULE_PERIPH_INIT_VBAT) + vbat_init(); +#endif + #endif /* MODULE_PERIPH_INIT */ }