Skip to content

Commit 5964163

Browse files
committed
Initial SleepMemory code
1 parent e9fd689 commit 5964163

File tree

8 files changed

+373
-5
lines changed

8 files changed

+373
-5
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 microDev
7+
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include <string.h>
29+
30+
#include "py/runtime.h"
31+
#include "common-hal/alarm/SleepMemory.h"
32+
33+
#include "esp_sleep.h"
34+
35+
void alarm_sleep_memory_reset(void) {
36+
// Power RTC slow memory during deep sleep
37+
esp_sleep_pd_config(ESP_PD_DOMAIN_RTC_SLOW_MEM, ESP_PD_OPTION_ON);
38+
}
39+
40+
uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
41+
return SLEEP_MEMORY_LENGTH;
42+
}
43+
44+
bool common_hal_alarm_sleep_memory_set_bytes(alarm_sleep_memory_obj_t *self,
45+
uint32_t start_index, uint8_t* values, uint32_t len) {
46+
47+
if (start_index + len > SLEEP_MEMORY_LENGTH) {
48+
return false;
49+
}
50+
51+
memcpy((uint8_t *) (SLEEP_MEMORY_BASE + start_index), values, len);
52+
return true;
53+
}
54+
55+
void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self,
56+
uint32_t start_index, uint32_t len, uint8_t* values) {
57+
58+
if (start_index + len > SLEEP_MEMORY_LENGTH) {
59+
return;
60+
}
61+
memcpy(values, (uint8_t *) (SLEEP_MEMORY_BASE + start_index), len);
62+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM_SLEEPMEMORY_H
28+
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM_SLEEPMEMORY_H
29+
30+
#include "py/obj.h"
31+
32+
#define SLEEP_MEMORY_LENGTH (8192)
33+
34+
// There are several places we could store persistent data for SleepMemory:
35+
//
36+
// RTC registers: There are a few 32-bit registers maintained during deep sleep.
37+
// We are already using one for saving sleep information during deep sleep.
38+
//
39+
// RTC Fast Memory: 8kB, also used for deep-sleep power on stub, and for heap
40+
// during normal operation if CONFIG_ESP32S2_ALLOW_RTC_FAST_MEM_AS_HEAP is set.
41+
// Power-on during deep sleep must be enabled.
42+
// I experimented with using RTC Fast Memory. It seemed to work, but occasionally,
43+
// got smashed for unknown reasons.
44+
// Base of RTC Fast memory on the data bus is 0x3FF9E000. The address is different on the instruction bus.
45+
//
46+
// RTC Slow Memory: 8kB, also used for the ULP (tiny co-processor available during sleep).
47+
// Less likely to be used by ESP-IDF.
48+
// Since we may want to use the ULP in the future, we will use the upper half
49+
// of Slow Memory and reserve the lower half for ULP.
50+
// From ulp.h:
51+
// #define RTC_SLOW_MEM ((uint32_t*) 0x50000000) /*!< RTC slow memory, 8k size */
52+
53+
// Upper half of RTC_SLOW_MEM.
54+
#define SLEEP_MEMORY_LENGTH (4096)
55+
#define SLEEP_MEMORY_BASE (0x50000000 + 4096)
56+
57+
typedef struct {
58+
mp_obj_base_t base;
59+
} alarm_sleep_memory_obj_t;
60+
61+
extern void alarm_sleep_memory_reset(void);
62+
63+
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM_SLEEPMEMORY_H

ports/esp32s2/common-hal/alarm/__init__.c

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
1-
/*
1+
/*
22
* This file is part of the MicroPython project, http://micropython.org/
33
*
44
* The MIT License (MIT)
55
*
6-
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
7-
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
6+
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
7+
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
88
*
99
* Permission is hereby granted, free of charge, to any person obtaining a copy
1010
* of this software and associated documentation files (the "Software"), to deal
@@ -30,6 +30,7 @@
3030
#include "py/runtime.h"
3131

3232
#include "shared-bindings/alarm/pin/PinAlarm.h"
33+
#include "shared-bindings/alarm/SleepMemory.h"
3334
#include "shared-bindings/alarm/time/TimeAlarm.h"
3435
#include "shared-bindings/microcontroller/__init__.h"
3536
#include "shared-bindings/wifi/__init__.h"
@@ -41,8 +42,17 @@
4142

4243
#include "esp_sleep.h"
4344

45+
// Singleton instance of SleepMemory.
46+
const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
47+
.base = {
48+
.type = &alarm_sleep_memory_type,
49+
},
50+
};
51+
52+
4453
void alarm_reset(void) {
4554
alarm_time_timealarm_reset();
55+
alarm_sleep_memory_reset();
4656
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
4757
}
4858

ports/esp32s2/common-hal/alarm/__init__.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@
2727
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM__INIT__H
2828
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM__INIT__H
2929

30-
void alarm_reset(void);
30+
#include "common-hal/alarm/SleepMemory.h"
31+
32+
const alarm_sleep_memory_obj_t alarm_sleep_memory_obj;
33+
34+
extern void alarm_reset(void);
3135

3236
#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM__INIT__H

py/circuitpy_defns.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ SRC_COMMON_HAL_ALL = \
304304
_bleio/__init__.c \
305305
_pew/PewPew.c \
306306
_pew/__init__.c \
307+
alarm/SleepMemory.c \
307308
alarm/__init__.c \
308309
alarm/pin/PinAlarm.c \
309310
alarm/time/TimeAlarm.c \

shared-bindings/alarm/SleepMemory.c

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
/*
2+
* This file is part of the Micro Python project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2017 Scott Shawcroft for Adafruit Industries
7+
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
8+
*
9+
* Permission is hereby granted, free of charge, to any person obtaining a copy
10+
* of this software and associated documentation files (the "Software"), to deal
11+
* in the Software without restriction, including without limitation the rights
12+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13+
* copies of the Software, and to permit persons to whom the Software is
14+
* furnished to do so, subject to the following conditions:
15+
*
16+
* The above copyright notice and this permission notice shall be included in
17+
* all copies or substantial portions of the Software.
18+
*
19+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25+
* THE SOFTWARE.
26+
*/
27+
28+
#include "py/binary.h"
29+
#include "py/objproperty.h"
30+
#include "py/runtime.h"
31+
#include "py/runtime0.h"
32+
33+
#include "shared-bindings/alarm/SleepMemory.h"
34+
#include "supervisor/shared/translate.h"
35+
36+
//| class SleepMemory:
37+
//| """Store raw bytes in RAM that persists during deep sleep.
38+
//| The class acts as a ``bytearray``.
39+
//| If power is lost, the memory contents are lost.
40+
//|
41+
//| Note that this class can't be imported and used directly. The sole
42+
//| instance of :class:`SleepMemory` is available at
43+
//| :attr:`alarm.sleep_memory`.
44+
//|
45+
//| Usage::
46+
//|
47+
//| import alarm
48+
//| alarm.sleep_memory[0] = True
49+
//| alarm.sleep_memory[1] = 12
50+
//|
51+
52+
//| def __init__(self) -> None:
53+
//| """Not currently dynamically supported. Access the sole instance through `microcontroller.nvm`."""
54+
//| ...
55+
//|
56+
57+
//| def __bool__(self) -> bool:
58+
//| """``sleep_memory`` is ``True`` if its length is greater than zero.
59+
//| This is an easy way to check for its existence.
60+
//| """
61+
//| ...
62+
//|
63+
//| def __len__(self) -> int:
64+
//| """Return the length. This is used by (`len`)"""
65+
//| ...
66+
//|
67+
STATIC mp_obj_t alarm_sleep_memory_unary_op(mp_unary_op_t op, mp_obj_t self_in) {
68+
alarm_sleep_memory_obj_t *self = MP_OBJ_TO_PTR(self_in);
69+
uint16_t len = common_hal_alarm_sleep_memory_get_length(self);
70+
switch (op) {
71+
case MP_UNARY_OP_BOOL: return mp_obj_new_bool(len != 0);
72+
case MP_UNARY_OP_LEN: return MP_OBJ_NEW_SMALL_INT(len);
73+
default: return MP_OBJ_NULL; // op not supported
74+
}
75+
}
76+
77+
STATIC const mp_rom_map_elem_t alarm_sleep_memory_locals_dict_table[] = {
78+
};
79+
80+
STATIC MP_DEFINE_CONST_DICT(alarm_sleep_memory_locals_dict, alarm_sleep_memory_locals_dict_table);
81+
82+
//| @overload
83+
//| def __getitem__(self, index: slice) -> bytearray: ...
84+
//| @overload
85+
//| def __getitem__(self, index: int) -> int:
86+
//| """Returns the value at the given index."""
87+
//| ...
88+
//|
89+
//| @overload
90+
//| def __setitem__(self, index: slice, value: ReadableBuffer) -> None: ...
91+
//| @overload
92+
//| def __setitem__(self, index: int, value: int) -> None:
93+
//| """Set the value at the given index."""
94+
//| ...
95+
//|
96+
STATIC mp_obj_t alarm_sleep_memory_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t value) {
97+
if (value == MP_OBJ_NULL) {
98+
// delete item
99+
// slice deletion
100+
return MP_OBJ_NULL; // op not supported
101+
} else {
102+
alarm_sleep_memory_obj_t *self = MP_OBJ_TO_PTR(self_in);
103+
if (0) {
104+
#if MICROPY_PY_BUILTINS_SLICE
105+
} else if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
106+
mp_bound_slice_t slice;
107+
if (!mp_seq_get_fast_slice_indexes(common_hal_alarm_sleep_memory_get_length(self), index_in, &slice)) {
108+
mp_raise_NotImplementedError(translate("only slices with step=1 (aka None) are supported"));
109+
}
110+
if (value != MP_OBJ_SENTINEL) {
111+
#if MICROPY_PY_ARRAY_SLICE_ASSIGN
112+
// Assign
113+
size_t src_len = slice.stop - slice.start;
114+
uint8_t* src_items;
115+
if (MP_OBJ_IS_TYPE(value, &mp_type_array) ||
116+
MP_OBJ_IS_TYPE(value, &mp_type_bytearray) ||
117+
MP_OBJ_IS_TYPE(value, &mp_type_memoryview) ||
118+
MP_OBJ_IS_TYPE(value, &mp_type_bytes)) {
119+
mp_buffer_info_t bufinfo;
120+
mp_get_buffer_raise(value, &bufinfo, MP_BUFFER_READ);
121+
if (bufinfo.len != src_len) {
122+
mp_raise_ValueError(translate("Slice and value different lengths."));
123+
}
124+
src_len = bufinfo.len;
125+
src_items = bufinfo.buf;
126+
if (1 != mp_binary_get_size('@', bufinfo.typecode, NULL)) {
127+
mp_raise_ValueError(translate("Array values should be single bytes."));
128+
}
129+
} else {
130+
mp_raise_NotImplementedError(translate("array/bytes required on right side"));
131+
}
132+
133+
if (!common_hal_alarm_sleep_memory_set_bytes(self, slice.start, src_items, src_len)) {
134+
mp_raise_RuntimeError(translate("Unable to write to nvm."));
135+
}
136+
return mp_const_none;
137+
#else
138+
return MP_OBJ_NULL; // op not supported
139+
#endif
140+
} else {
141+
// Read slice.
142+
size_t len = slice.stop - slice.start;
143+
uint8_t *items = m_new(uint8_t, len);
144+
common_hal_alarm_sleep_memory_get_bytes(self, slice.start, len, items);
145+
return mp_obj_new_bytearray_by_ref(len, items);
146+
}
147+
#endif
148+
} else {
149+
// Single index rather than slice.
150+
size_t index = mp_get_index(self->base.type, common_hal_alarm_sleep_memory_get_length(self),
151+
index_in, false);
152+
if (value == MP_OBJ_SENTINEL) {
153+
// load
154+
uint8_t value_out;
155+
common_hal_alarm_sleep_memory_get_bytes(self, index, 1, &value_out);
156+
return MP_OBJ_NEW_SMALL_INT(value_out);
157+
} else {
158+
// store
159+
mp_int_t byte_value = mp_obj_get_int(value);
160+
if (byte_value > 0xff || byte_value < 0) {
161+
mp_raise_ValueError(translate("Bytes must be between 0 and 255."));
162+
}
163+
uint8_t short_value = byte_value;
164+
if (!common_hal_alarm_sleep_memory_set_bytes(self, index, &short_value, 1)) {
165+
mp_raise_RuntimeError(translate("Unable to write to nvm."));
166+
}
167+
return mp_const_none;
168+
}
169+
}
170+
}
171+
}
172+
173+
const mp_obj_type_t alarm_sleep_memory_type = {
174+
{ &mp_type_type },
175+
.name = MP_QSTR_SleepMemory,
176+
.subscr = alarm_sleep_memory_subscr,
177+
.unary_op = alarm_sleep_memory_unary_op,
178+
.print = NULL,
179+
.locals_dict = (mp_obj_t)&alarm_sleep_memory_locals_dict,
180+
};

0 commit comments

Comments
 (0)