Merge pull request #8015 from dhalbert/rp2040-sleep-memory

RP2040 SleepMemory
This commit is contained in:
Scott Shawcroft 2023-05-22 13:08:37 -07:00 committed by GitHub
commit 3f7e6db4fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 37 additions and 6 deletions

View File

@ -30,18 +30,47 @@
#include "common-hal/alarm/SleepMemory.h"
#include "shared-bindings/alarm/SleepMemory.h"
void alarm_sleep_memory_reset(void) {
}
__attribute__((section(".uninitialized"))) static uint8_t _sleepmem[SLEEP_MEMORY_LENGTH];
__attribute__((section(".uninitialized"))) static uint32_t _sleepmem_magicnum;
#define SLEEP_MEMORY_DATA_GUARD 0xad0000af
#define SLEEP_MEMORY_DATA_GUARD_MASK 0xff0000ff
uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
static int is_sleep_memory_valid(void) {
if ((_sleepmem_magicnum & SLEEP_MEMORY_DATA_GUARD_MASK)
== SLEEP_MEMORY_DATA_GUARD) {
return 1;
}
return 0;
}
static void initialize_sleep_memory(void) {
memset((uint8_t *)_sleepmem, 0, SLEEP_MEMORY_LENGTH);
_sleepmem_magicnum = SLEEP_MEMORY_DATA_GUARD;
}
void alarm_sleep_memory_reset(void) {
if (!is_sleep_memory_valid()) {
initialize_sleep_memory();
}
}
uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
return sizeof(_sleepmem);
}
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 > sizeof(_sleepmem)) {
return false;
}
memcpy((uint8_t *)(_sleepmem + 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 > sizeof(_sleepmem)) {
return;
}
memcpy(values, (uint8_t *)(_sleepmem + start_index), len);
}

View File

@ -28,6 +28,8 @@
#include "py/obj.h"
#define SLEEP_MEMORY_LENGTH (256)
typedef struct {
mp_obj_base_t base;
} alarm_sleep_memory_obj_t;