Skip to content

STM32: Alarms and sleep #4388

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 23 commits into from
May 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f1792c8
Extract EXTI, create base sleep framework
hierophect Mar 2, 2021
6b2c998
Extract RTC, implement fake RTC deepsleep
hierophect Mar 11, 2021
d8a2d69
Working deep sleep via RTC reset, with debug pins
hierophect Mar 21, 2021
529fb52
Style changes and wakeup detection
hierophect Mar 27, 2021
d35a307
Merge branch 'sleep-api-update' into stm-alarm
hierophect Mar 29, 2021
33418ce
Merge remote-tracking branch 'upstream/main' into stm-alarm
hierophect Mar 29, 2021
4f52554
Fix to RTC reset
hierophect Apr 2, 2021
91e530d
Resolve wake alarm crash
hierophect Apr 2, 2021
17585e1
Clean up API
hierophect Apr 2, 2021
66a1583
Implement fake and true deep sleep alarm differentiation
hierophect Apr 2, 2021
9240146
Make light sleep store true alarm object to global map
hierophect Apr 2, 2021
8a2143f
Clean up comments and debugging pins
hierophect Apr 2, 2021
e0024c7
Merge remote-tracking branch 'upstream/main' into stm-alarm
hierophect Apr 15, 2021
8ce8986
Pass pre-commit formatting
hierophect Apr 21, 2021
9feef41
Merge remote-tracking branch 'upstream/main' into stm-alarm
hierophect Apr 28, 2021
9fe8d61
Fix broken MP macros from 1.11 merge
hierophect Apr 28, 2021
bab5a22
Fix build issues
hierophect Apr 29, 2021
07b86fc
fix sleepmemory object redefine error
hierophect Apr 29, 2021
bd510cb
Merge branch 'main' into stm-alarm
hierophect May 4, 2021
0d3c522
Merge remote-tracking branch 'upstream/main' into stm-alarm
hierophect May 10, 2021
f59a198
Merge branch 'stm-alarm' of https://github.com/hierophect/circuitpyth…
hierophect May 10, 2021
525b34b
Fix formatting and broken links
hierophect May 10, 2021
849e3a7
Style fixes, submodules
hierophect May 13, 2021
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
30 changes: 26 additions & 4 deletions locale/circuitpython.pot
Original file line number Diff line number Diff line change
Expand Up @@ -846,6 +846,10 @@ msgstr ""
msgid "Data too large for advertisement packet"
msgstr ""

#: ports/stm/common-hal/alarm/pin/PinAlarm.c
msgid "Deep sleep pins must use a rising edge with pulldown"
msgstr ""

#: shared-bindings/audiobusio/PDMIn.c
msgid "Destination capacity is smaller than destination_length."
msgstr ""
Expand Down Expand Up @@ -1670,6 +1674,10 @@ msgid ""
"Only Windows format, uncompressed BMP supported: given header size is %d"
msgstr ""

#: ports/stm/common-hal/alarm/pin/PinAlarm.c
msgid "Only edge detection is available on this hardware"
msgstr ""

#: shared-module/displayio/OnDiskBitmap.c
#, c-format
msgid ""
Expand All @@ -1683,6 +1691,7 @@ msgstr ""

#: ports/esp32s2/common-hal/alarm/time/TimeAlarm.c
#: ports/nrf/common-hal/alarm/time/TimeAlarm.c
#: ports/stm/common-hal/alarm/time/TimeAlarm.c
msgid "Only one alarm.time alarm can be set."
msgstr ""

Expand Down Expand Up @@ -1758,6 +1767,10 @@ msgstr ""
msgid "Permission denied"
msgstr ""

#: ports/stm/common-hal/alarm/pin/PinAlarm.c
msgid "Pin cannot wake from Deep Sleep"
msgstr ""

#: ports/raspberrypi/bindings/rp2pio/StateMachine.c
msgid "Pin count must be at least 1"
msgstr ""
Expand All @@ -1776,6 +1789,11 @@ msgstr ""
msgid "Pin does not have ADC capabilities"
msgstr ""

#: ports/stm/common-hal/alarm/pin/PinAlarm.c
#: ports/stm/common-hal/pulseio/PulseIn.c
msgid "Pin interrupt already in use"
msgstr ""

#: shared-bindings/adafruit_bus_device/SPIDevice.c
#: shared-bindings/digitalio/DigitalInOut.c
msgid "Pin is input only"
Expand All @@ -1789,10 +1807,6 @@ msgstr ""
msgid "Pin must support hardware interrupts"
msgstr ""

#: ports/stm/common-hal/pulseio/PulseIn.c
msgid "Pin number already reserved by EXTI"
msgstr ""

#: shared-bindings/rgbmatrix/RGBMatrix.c
#, c-format
msgid ""
Expand Down Expand Up @@ -2013,6 +2027,10 @@ msgstr ""
msgid "Size not supported"
msgstr ""

#: ports/stm/common-hal/alarm/SleepMemory.c
msgid "Sleep Memory not available"
msgstr ""

#: shared-bindings/alarm/SleepMemory.c shared-bindings/nvm/ByteArray.c
msgid "Slice and value different lengths."
msgstr ""
Expand Down Expand Up @@ -2153,6 +2171,10 @@ msgstr ""
msgid "Total data to write is larger than %q"
msgstr ""

#: ports/stm/common-hal/alarm/touch/TouchAlarm.c
msgid "Touch alarms not available"
msgstr ""

#: py/obj.c
msgid "Traceback (most recent call last):\n"
msgstr ""
Expand Down
5 changes: 5 additions & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,13 +297,17 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
stack_resize();
filesystem_flush();
supervisor_allocation* heap = allocate_remaining_memory();

// Prepare the VM state. Includes an alarm check/reset for sleep.
start_mp(heap);

#if CIRCUITPY_USB
usb_setup_with_vm();
#endif

// This is where the user's python code is actually executed:
found_main = maybe_run_list(supported_filenames, &result);
// If that didn't work, double check the extensions
#if CIRCUITPY_FULL_BUILD
if (!found_main){
found_main = maybe_run_list(double_extension_filenames, &result);
Expand All @@ -313,6 +317,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
}
#endif

// Finished executing python code. Cleanup includes a board reset.
cleanup_after_vm(heap);

if (result.return_code & PYEXEC_FORCED_EXIT) {
Expand Down
5 changes: 5 additions & 0 deletions ports/esp32s2/common-hal/alarm/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,8 @@ bool common_hal_alarm_woken_from_sleep(void) {
return _get_wakeup_cause() != ESP_SLEEP_WAKEUP_UNDEFINED;
}

// When called to populate the global dict, the module functions create new alarm objects.
// Otherwise, they scan the existing alarms for matches.
STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
esp_sleep_wakeup_cause_t cause = _get_wakeup_cause();
switch (cause) {
Expand All @@ -104,6 +106,8 @@ STATIC mp_obj_t _get_wake_alarm(size_t n_alarms, const mp_obj_t *alarms) {
return mp_const_none;
}

// This function is used to create a new alarm object for the global dict after deep sleep,
// rather than finding an existing one during runtime.
mp_obj_t common_hal_alarm_get_wake_alarm(void) {
return _get_wake_alarm(0, NULL);
}
Expand All @@ -121,6 +125,7 @@ STATIC void _idle_until_alarm(void) {
RUN_BACKGROUND_TASKS;
// Allow ctrl-C interrupt.
if (common_hal_alarm_woken_from_sleep()) {
// This saves the return of common_hal_alarm_get_wake_alarm through Shared Bindings
shared_alarm_save_wake_alarm();
return;
}
Expand Down
7 changes: 4 additions & 3 deletions ports/esp32s2/common-hal/alarm/pin/PinAlarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
#include "components/soc/src/esp32s2/include/hal/gpio_ll.h"
#include "components/xtensa/include/esp_debug_helpers.h"

void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, mcu_pin_obj_t *pin, bool value, bool edge, bool pull) {
void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull) {
if (edge) {
mp_raise_ValueError(translate("Cannot wake on pin edge. Only level."));
}
Expand All @@ -51,7 +51,7 @@ void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, mcu
self->pull = pull;
}

mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self) {
const mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self) {
return self->pin;
}

Expand Down Expand Up @@ -101,7 +101,7 @@ bool alarm_pin_pinalarm_woke_us_up(void) {
}

mp_obj_t alarm_pin_pinalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *alarms) {
// First, check to see if we match any given alarms.
// For light sleep, we check if we match any existing alarms
uint64_t pin_status = ((uint64_t)pin_63_32_status) << 32 | pin_31_0_status;
for (size_t i = 0; i < n_alarms; i++) {
if (!mp_obj_is_type(alarms[i], &alarm_pin_pinalarm_type)) {
Expand All @@ -112,6 +112,7 @@ mp_obj_t alarm_pin_pinalarm_get_wakeup_alarm(size_t n_alarms, const mp_obj_t *al
return alarms[i];
}
}
// For deep sleep, a new alarm must be created
esp_sleep_wakeup_cause_t cause = esp_sleep_get_wakeup_cause();
size_t pin_number = 64;
if (cause == ESP_SLEEP_WAKEUP_EXT0) {
Expand Down
2 changes: 1 addition & 1 deletion ports/esp32s2/common-hal/alarm/pin/PinAlarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

typedef struct {
mp_obj_base_t base;
mcu_pin_obj_t *pin;
const mcu_pin_obj_t *pin;
bool value;
bool pull;
} alarm_pin_pinalarm_obj_t;
Expand Down
4 changes: 2 additions & 2 deletions ports/nrf/common-hal/alarm/pin/PinAlarm.c
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static bool pins_configured = false;
extern uint32_t reset_reason_saved;
extern void dbg_dump_GPIOregs(void);

void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, mcu_pin_obj_t *pin, bool value, bool edge, bool pull) {
void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, const mcu_pin_obj_t *pin, bool value, bool edge, bool pull) {
if (edge) {
mp_raise_ValueError(translate("Cannot wake on pin edge. Only level."));
}
Expand All @@ -62,7 +62,7 @@ void common_hal_alarm_pin_pinalarm_construct(alarm_pin_pinalarm_obj_t *self, mcu
self->pull = pull;
}

mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self) {
const mcu_pin_obj_t *common_hal_alarm_pin_pinalarm_get_pin(alarm_pin_pinalarm_obj_t *self) {
return self->pin;
}

Expand Down
2 changes: 1 addition & 1 deletion ports/nrf/common-hal/alarm/pin/PinAlarm.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@

typedef struct {
mp_obj_base_t base;
mcu_pin_obj_t *pin;
const mcu_pin_obj_t *pin;
bool value;
bool pull;
} alarm_pin_pinalarm_obj_t;
Expand Down
2 changes: 2 additions & 0 deletions ports/stm/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ SRC_C += \
boards/$(BOARD)/board.c \
boards/$(BOARD)/pins.c \
peripherals/timers.c \
peripherals/exti.c \
peripherals/rtc.c \
peripherals/stm32$(MCU_SERIES_LOWER)/clocks.c \
peripherals/stm32$(MCU_SERIES_LOWER)/$(MCU_VARIANT_LOWER)/pins.c \
peripherals/stm32$(MCU_SERIES_LOWER)/$(MCU_VARIANT_LOWER)/gpio.c \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,9 @@ MCU_PACKAGE = UFQFPN48
LD_COMMON = boards/common_default.ld
LD_FILE = boards/STM32F411_nvm_nofs.ld

# Too big for the flash
CIRCUITPY_AUDIOCORE = 0
CIRCUITPY_AUDIOPWMIO = 0
CIRCUITPY_SYNTHIO = 0
CIRCUITPY_BITMAPTOOLS = 0
CIRCUITPY_VECTORIO = 0
49 changes: 49 additions & 0 deletions ports/stm/common-hal/alarm/SleepMemory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Lucian Copeland 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"

void alarm_sleep_memory_reset(void) {

}

uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
mp_raise_NotImplementedError(translate("Sleep Memory not available"));
return 0;
}

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;
}

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"));
return;
}
38 changes: 38 additions & 0 deletions ports/stm/common-hal/alarm/SleepMemory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Lucian Copeland 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_STM32_COMMON_HAL_ALARM_SLEEPMEMORY_H
#define MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_SLEEPMEMORY_H

#include "py/obj.h"

typedef struct {
mp_obj_base_t base;
} alarm_sleep_memory_obj_t;

extern void alarm_sleep_memory_reset(void);

#endif // MICROPY_INCLUDED_STM32_COMMON_HAL_ALARM_SLEEPMEMORY_H
Loading