From 696c035b674e26674b82cef8fb486d0ee39cd010 Mon Sep 17 00:00:00 2001 From: Lucian Copeland Date: Fri, 25 Jun 2021 17:43:18 -0400 Subject: [PATCH] Add STM32 sleep memory --- ports/stm/common-hal/alarm/SleepMemory.c | 34 ++++++++++++++++++++---- 1 file changed, 29 insertions(+), 5 deletions(-) diff --git a/ports/stm/common-hal/alarm/SleepMemory.c b/ports/stm/common-hal/alarm/SleepMemory.c index 3b89efbca3..dc19222d63 100644 --- a/ports/stm/common-hal/alarm/SleepMemory.c +++ b/ports/stm/common-hal/alarm/SleepMemory.c @@ -29,21 +29,45 @@ #include "py/runtime.h" #include "common-hal/alarm/SleepMemory.h" +#include STM32_HAL_H + +#define STM_BKPSRAM_SIZE 0x1000 +#define STM_BKPSRAM_START BKPSRAM_BASE + +STATIC bool initialized = false; + +STATIC void lazy_init(void) { + if (!initialized) { + __HAL_RCC_BKPSRAM_CLK_ENABLE(); + HAL_PWREx_EnableBkUpReg(); + HAL_PWR_EnableBkUpAccess(); + initialized = true; + } +} + void alarm_sleep_memory_reset(void) { } uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) { - mp_raise_NotImplementedError(translate("Sleep Memory not available")); - return 0; + lazy_init(); + return STM_BKPSRAM_SIZE; } bool common_hal_alarm_sleep_memory_set_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, const uint8_t *values, uint32_t len) { - mp_raise_NotImplementedError(translate("Sleep Memory not available")); - return false; + if (start_index + len > STM_BKPSRAM_SIZE) { + return false; + } + lazy_init(); + memcpy((uint8_t *)(STM_BKPSRAM_START + start_index), values, len); + return true; } void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, uint8_t *values, uint32_t len) { - mp_raise_NotImplementedError(translate("Sleep Memory not available")); + if (start_index + len > STM_BKPSRAM_SIZE) { + return; + } + lazy_init(); + memcpy(values, (uint8_t *)(STM_BKPSRAM_START + start_index), len); return; }