Skip to content

alarm.sleep_memory + alarm.wake_alarm #3816

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

Merged
merged 7 commits into from
Dec 15, 2020
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
1 change: 1 addition & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ jobs:
python-version: 3.8
- name: Install deps
run: |
sudo apt-get update
sudo apt-get install -y eatmydata
sudo eatmydata apt-get install -y gettext librsvg2-bin mingw-w64 latexmk texlive-fonts-recommended texlive-latex-recommended texlive-latex-extra
pip install requests sh click setuptools cpp-coveralls "Sphinx<4" sphinx-rtd-theme recommonmark sphinx-autoapi sphinxcontrib-svg2pdfconverter polib pyyaml astroid isort black awscli mypy
Expand Down
17 changes: 11 additions & 6 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2020-12-08 09:56-0800\n"
"POT-Creation-Date: 2020-12-14 11:48-0500\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <[email protected]>\n"
Expand Down Expand Up @@ -388,7 +388,7 @@ msgstr ""
msgid "Array must contain halfwords (type 'H')"
msgstr ""

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

Expand Down Expand Up @@ -522,7 +522,7 @@ msgstr ""
msgid "Byte buffer must be 16 bytes."
msgstr ""

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

Expand Down Expand Up @@ -1696,7 +1696,7 @@ msgstr ""
msgid "Size not supported"
msgstr ""

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

Expand Down Expand Up @@ -1911,6 +1911,10 @@ msgstr ""
msgid "Unable to write to nvm."
msgstr ""

#: shared-bindings/alarm/SleepMemory.c
msgid "Unable to write to sleep_memory."
msgstr ""

#: ports/nrf/common-hal/_bleio/UUID.c
msgid "Unexpected nrfx uuid type"
msgstr ""
Expand Down Expand Up @@ -2117,7 +2121,8 @@ msgstr ""
msgid "array and index length must be equal"
msgstr ""

#: py/objarray.c shared-bindings/nvm/ByteArray.c
#: py/objarray.c shared-bindings/alarm/SleepMemory.c
#: shared-bindings/nvm/ByteArray.c
msgid "array/bytes required on right side"
msgstr ""

Expand Down Expand Up @@ -3215,7 +3220,7 @@ msgid "only sample_rate=16000 is supported"
msgstr ""

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

Expand Down
12 changes: 8 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,6 @@ STATIC void start_mp(supervisor_allocation* heap) {
reset_status_led();
autoreload_stop();
supervisor_workflow_reset();
#if CIRCUITPY_ALARM
alarm_reset();
#endif

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

mp_obj_list_init(mp_sys_argv, 0);

#if CIRCUITPY_ALARM
// Record which alarm woke us up, if any. An object may be created so the heap must be functional.
alarm_save_wakeup_alarm();
// Reset alarm module only after we retrieved the wakeup alarm.
alarm_reset();
#endif

#if CIRCUITPY_NETWORK
network_module_init();
#endif
Expand Down Expand Up @@ -285,6 +289,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
filesystem_flush();
supervisor_allocation* heap = allocate_remaining_memory();
start_mp(heap);

found_main = maybe_run_list(supported_filenames, &result);
#if CIRCUITPY_FULL_BUILD
if (!found_main){
Expand Down Expand Up @@ -354,7 +359,6 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
serial_write_compressed(translate("Woken up by alarm.\n"));
board_init();
supervisor_set_run_reason(RUN_REASON_STARTUP);
// TODO: Reset any volatile memory the user may have access to.
return true;
}
#endif
Expand Down
66 changes: 66 additions & 0 deletions ports/esp32s2/common-hal/alarm/SleepMemory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 microDev
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#include <string.h>

#include "py/runtime.h"
#include "common-hal/alarm/SleepMemory.h"

#include "esp_log.h"
#include "esp_sleep.h"

// Data storage for singleton instance of SleepMemory.
// Might be RTC_SLOW_MEM or RTC_FAST_MEM, depending on setting of CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM.
static RTC_DATA_ATTR uint8_t _sleep_mem[SLEEP_MEMORY_LENGTH];

void alarm_sleep_memory_reset(void) {
// ESP-IDF build system takes care of doing esp_sleep_pd_config() or the equivalentwith
// the correct settings, depending on which RTC mem we are using.
// https://docs.espressif.com/projects/esp-idf/en/latest/esp32s2/api-reference/system/sleep_modes.html#power-down-of-rtc-peripherals-and-memories
}

uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
return sizeof(_sleep_mem);
}

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) {

if (start_index + len > sizeof(_sleep_mem)) {
return false;
}

memcpy((uint8_t *) (_sleep_mem + 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) {

if (start_index + len > sizeof(_sleep_mem)) {
return;
}
memcpy(values, (uint8_t *) (_sleep_mem + start_index), len);
}
53 changes: 53 additions & 0 deletions ports/esp32s2/common-hal/alarm/SleepMemory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/

#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM_SLEEPMEMORY_H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM_SLEEPMEMORY_H

#include "py/obj.h"

// There are several places we could store persistent data for SleepMemory:
//
// RTC registers: There are a few 32-bit registers maintained during deep sleep.
// We are already using one for saving sleep information during deep sleep.
//
// RTC Fast Memory: 8kB, also used for deep-sleep power-on stub.
// RTC Slow Memory: 8kB, also used for the ULP (tiny co-processor available during sleep).
//
// The ESP-IDF build system takes care of the power management of these regions.
// RTC_DATA_ATTR will allocate storage in RTC_SLOW_MEM unless CONFIG_ESP32S2_RTCDATA_IN_FAST_MEM
// is set. Any memory not allocated by us can be used by the ESP-IDF for heap or other purposes.

// Use half of RTC_SLOW_MEM or RTC_FAST_MEM.
#define SLEEP_MEMORY_LENGTH (4096)

typedef struct {
mp_obj_base_t base;
} alarm_sleep_memory_obj_t;

extern void alarm_sleep_memory_reset(void);

#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM_SLEEPMEMORY_H
16 changes: 13 additions & 3 deletions ports/esp32s2/common-hal/alarm/__init__.c
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/*
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2016 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2019 Lucian Copeland for Adafruit Industries
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
Expand All @@ -30,6 +30,7 @@
#include "py/runtime.h"

#include "shared-bindings/alarm/pin/PinAlarm.h"
#include "shared-bindings/alarm/SleepMemory.h"
#include "shared-bindings/alarm/time/TimeAlarm.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/wifi/__init__.h"
Expand All @@ -41,8 +42,17 @@

#include "esp_sleep.h"

// Singleton instance of SleepMemory.
const alarm_sleep_memory_obj_t alarm_sleep_memory_obj = {
.base = {
.type = &alarm_sleep_memory_type,
},
};


void alarm_reset(void) {
alarm_time_timealarm_reset();
alarm_sleep_memory_reset();
esp_sleep_disable_wakeup_source(ESP_SLEEP_WAKEUP_ALL);
}

Expand Down
6 changes: 5 additions & 1 deletion ports/esp32s2/common-hal/alarm/__init__.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@
#ifndef MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM__INIT__H
#define MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM__INIT__H

void alarm_reset(void);
#include "common-hal/alarm/SleepMemory.h"

const alarm_sleep_memory_obj_t alarm_sleep_memory_obj;

extern void alarm_reset(void);

#endif // MICROPY_INCLUDED_ESP32S2_COMMON_HAL_ALARM__INIT__H
1 change: 1 addition & 0 deletions ports/esp32s2/common-hal/alarm/time/TimeAlarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ mp_obj_t alarm_time_timealarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *
alarm_time_time_alarm_obj_t *timer = m_new_obj(alarm_time_time_alarm_obj_t);
timer->base.type = &alarm_time_time_alarm_type;
// TODO: Set monotonic_time based on the RTC state.
timer->monotonic_time = 0.0f;
return timer;
}

Expand Down
1 change: 1 addition & 0 deletions py/circuitpy_defns.mk
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,7 @@ SRC_COMMON_HAL_ALL = \
_bleio/__init__.c \
_pew/PewPew.c \
_pew/__init__.c \
alarm/SleepMemory.c \
alarm/__init__.c \
alarm/pin/PinAlarm.c \
alarm/time/TimeAlarm.c \
Expand Down
Loading