Skip to content

Commit d076296

Browse files
authored
Merge pull request #3816 from dhalbert/sleepmemory
alarm.sleep_memory + alarm.wake_alarm
2 parents d6ee17c + e0bb4a8 commit d076296

File tree

12 files changed

+376
-17
lines changed

12 files changed

+376
-17
lines changed

locale/circuitpython.pot

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ msgid ""
88
msgstr ""
99
"Project-Id-Version: PACKAGE VERSION\n"
1010
"Report-Msgid-Bugs-To: \n"
11-
"POT-Creation-Date: 2020-12-08 09:56-0800\n"
11+
"POT-Creation-Date: 2020-12-14 11:48-0500\n"
1212
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
1313
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
1414
"Language-Team: LANGUAGE <[email protected]>\n"
@@ -388,7 +388,7 @@ msgstr ""
388388
msgid "Array must contain halfwords (type 'H')"
389389
msgstr ""
390390

391-
#: shared-bindings/nvm/ByteArray.c
391+
#: shared-bindings/alarm/SleepMemory.c shared-bindings/nvm/ByteArray.c
392392
msgid "Array values should be single bytes."
393393
msgstr ""
394394

@@ -522,7 +522,7 @@ msgstr ""
522522
msgid "Byte buffer must be 16 bytes."
523523
msgstr ""
524524

525-
#: shared-bindings/nvm/ByteArray.c
525+
#: shared-bindings/alarm/SleepMemory.c shared-bindings/nvm/ByteArray.c
526526
msgid "Bytes must be between 0 and 255."
527527
msgstr ""
528528

@@ -1696,7 +1696,7 @@ msgstr ""
16961696
msgid "Size not supported"
16971697
msgstr ""
16981698

1699-
#: shared-bindings/nvm/ByteArray.c
1699+
#: shared-bindings/alarm/SleepMemory.c shared-bindings/nvm/ByteArray.c
17001700
msgid "Slice and value different lengths."
17011701
msgstr ""
17021702

@@ -1911,6 +1911,10 @@ msgstr ""
19111911
msgid "Unable to write to nvm."
19121912
msgstr ""
19131913

1914+
#: shared-bindings/alarm/SleepMemory.c
1915+
msgid "Unable to write to sleep_memory."
1916+
msgstr ""
1917+
19141918
#: ports/nrf/common-hal/_bleio/UUID.c
19151919
msgid "Unexpected nrfx uuid type"
19161920
msgstr ""
@@ -2117,7 +2121,8 @@ msgstr ""
21172121
msgid "array and index length must be equal"
21182122
msgstr ""
21192123

2120-
#: py/objarray.c shared-bindings/nvm/ByteArray.c
2124+
#: py/objarray.c shared-bindings/alarm/SleepMemory.c
2125+
#: shared-bindings/nvm/ByteArray.c
21212126
msgid "array/bytes required on right side"
21222127
msgstr ""
21232128

@@ -3215,7 +3220,7 @@ msgid "only sample_rate=16000 is supported"
32153220
msgstr ""
32163221

32173222
#: py/objarray.c py/objstr.c py/objstrunicode.c py/objtuple.c
3218-
#: shared-bindings/nvm/ByteArray.c
3223+
#: shared-bindings/alarm/SleepMemory.c shared-bindings/nvm/ByteArray.c
32193224
msgid "only slices with step=1 (aka None) are supported"
32203225
msgstr ""
32213226

main.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,6 @@ STATIC void start_mp(supervisor_allocation* heap) {
112112
reset_status_led();
113113
autoreload_stop();
114114
supervisor_workflow_reset();
115-
#if CIRCUITPY_ALARM
116-
alarm_reset();
117-
#endif
118115

119116
// Stack limit should be less than real stack size, so we have a chance
120117
// to recover from limit hit. (Limit is measured in bytes.)
@@ -158,6 +155,13 @@ STATIC void start_mp(supervisor_allocation* heap) {
158155

159156
mp_obj_list_init(mp_sys_argv, 0);
160157

158+
#if CIRCUITPY_ALARM
159+
// Record which alarm woke us up, if any. An object may be created so the heap must be functional.
160+
alarm_save_wakeup_alarm();
161+
// Reset alarm module only after we retrieved the wakeup alarm.
162+
alarm_reset();
163+
#endif
164+
161165
#if CIRCUITPY_NETWORK
162166
network_module_init();
163167
#endif
@@ -285,6 +289,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
285289
filesystem_flush();
286290
supervisor_allocation* heap = allocate_remaining_memory();
287291
start_mp(heap);
292+
288293
found_main = maybe_run_list(supported_filenames, &result);
289294
#if CIRCUITPY_FULL_BUILD
290295
if (!found_main){
@@ -354,7 +359,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
354359
serial_write_compressed(translate("Woken up by alarm.\n"));
355360
board_init();
356361
supervisor_set_run_reason(RUN_REASON_STARTUP);
357-
// TODO: Reset any volatile memory the user may have access to.
358362
return true;
359363
}
360364
#endif
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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_log.h"
34+
#include "esp_sleep.h"
35+
36+
// Data storage for singleton instance of SleepMemory.
37+
// Might be RTC_SLOW_MEM or RTC_FAST_MEM, depending on setting of CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM.
38+
static RTC_DATA_ATTR uint8_t _sleep_mem[SLEEP_MEMORY_LENGTH];
39+
40+
void alarm_sleep_memory_reset(void) {
41+
// ESP-IDF build system takes care of doing esp_sleep_pd_config() or the equivalentwith
42+
// the correct settings, depending on which RTC mem we are using.
43+
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/system/sleep_modes.html#power-down-of-rtc-peripherals-and-memories
44+
}
45+
46+
uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
47+
return sizeof(_sleep_mem);
48+
}
49+
50+
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) {
51+
52+
if (start_index + len > sizeof(_sleep_mem)) {
53+
return false;
54+
}
55+
56+
memcpy((uint8_t *) (_sleep_mem + start_index), values, len);
57+
return true;
58+
}
59+
60+
void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, uint8_t* values, uint32_t len) {
61+
62+
if (start_index + len > sizeof(_sleep_mem)) {
63+
return;
64+
}
65+
memcpy(values, (uint8_t *) (_sleep_mem + start_index), len);
66+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
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+
// There are several places we could store persistent data for SleepMemory:
33+
//
34+
// RTC registers: There are a few 32-bit registers maintained during deep sleep.
35+
// We are already using one for saving sleep information during deep sleep.
36+
//
37+
// RTC Fast Memory: 8kB, also used for deep-sleep power-on stub.
38+
// RTC Slow Memory: 8kB, also used for the ULP (tiny co-processor available during sleep).
39+
//
40+
// The ESP-IDF build system takes care of the power management of these regions.
41+
// RTC_DATA_ATTR will allocate storage in RTC_SLOW_MEM unless CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM
42+
// is set. Any memory not allocated by us can be used by the ESP-IDF for heap or other purposes.
43+
44+
// Use half of RTC_SLOW_MEM or RTC_FAST_MEM.
45+
#define SLEEP_MEMORY_LENGTH (4096)
46+
47+
typedef struct {
48+
mp_obj_base_t base;
49+
} alarm_sleep_memory_obj_t;
50+
51+
extern void alarm_sleep_memory_reset(void);
52+
53+
#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

ports/esp32s2/common-hal/alarm/time/TimeAlarm.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ mp_obj_t alarm_time_timealarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *
5252
alarm_time_time_alarm_obj_t *timer = m_new_obj(alarm_time_time_alarm_obj_t);
5353
timer->base.type = &alarm_time_time_alarm_type;
5454
// TODO: Set monotonic_time based on the RTC state.
55+
timer->monotonic_time = 0.0f;
5556
return timer;
5657
}
5758

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 \

0 commit comments

Comments
 (0)