Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RP2040 SleepMemory #8015

Merged
merged 1 commit into from
May 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions ports/raspberrypi/common-hal/alarm/SleepMemory.c
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,47 @@
#include "common-hal/alarm/SleepMemory.h"
#include "shared-bindings/alarm/SleepMemory.h"

__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

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 0;
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);
}
2 changes: 2 additions & 0 deletions ports/raspberrypi/common-hal/alarm/SleepMemory.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down