Skip to content

Commit 98cd989

Browse files
authored
Merge pull request #5083 from hierophect/stm32-sleepmem
STM32: add SleepMemory
2 parents bfe2978 + da149b0 commit 98cd989

File tree

1 file changed

+29
-5
lines changed

1 file changed

+29
-5
lines changed

ports/stm/common-hal/alarm/SleepMemory.c

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,45 @@
2929
#include "py/runtime.h"
3030
#include "common-hal/alarm/SleepMemory.h"
3131

32+
#include STM32_HAL_H
33+
34+
#define STM_BKPSRAM_SIZE 0x1000
35+
#define STM_BKPSRAM_START BKPSRAM_BASE
36+
37+
STATIC bool initialized = false;
38+
39+
STATIC void lazy_init(void) {
40+
if (!initialized) {
41+
__HAL_RCC_BKPSRAM_CLK_ENABLE();
42+
HAL_PWREx_EnableBkUpReg();
43+
HAL_PWR_EnableBkUpAccess();
44+
initialized = true;
45+
}
46+
}
47+
3248
void alarm_sleep_memory_reset(void) {
3349

3450
}
3551

3652
uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
37-
mp_raise_NotImplementedError(translate("Sleep Memory not available"));
38-
return 0;
53+
lazy_init();
54+
return STM_BKPSRAM_SIZE;
3955
}
4056

4157
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) {
42-
mp_raise_NotImplementedError(translate("Sleep Memory not available"));
43-
return false;
58+
if (start_index + len > STM_BKPSRAM_SIZE) {
59+
return false;
60+
}
61+
lazy_init();
62+
memcpy((uint8_t *)(STM_BKPSRAM_START + start_index), values, len);
63+
return true;
4464
}
4565

4666
void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, uint8_t *values, uint32_t len) {
47-
mp_raise_NotImplementedError(translate("Sleep Memory not available"));
67+
if (start_index + len > STM_BKPSRAM_SIZE) {
68+
return;
69+
}
70+
lazy_init();
71+
memcpy(values, (uint8_t *)(STM_BKPSRAM_START + start_index), len);
4872
return;
4973
}

0 commit comments

Comments
 (0)