8000 sleep and wakeup for nrf52 by jun2sak · Pull Request #4236 · adafruit/circuitpython · GitHub
[go: up one dir, main page]

Skip to content

sleep and wakeup for nrf52 #4236

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 56 commits into from
Apr 28, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
56 commits
Select commit Hold shift + click to select a range
e6350ff
Initial commit.
jun2sak Feb 17, 2021
cf2427c
light sleep reason fix.
jun2sak Feb 17, 2021
26f8f53
safe mode fix.
jun2sak Feb 20, 2021
41d9d4e
call sd API only when sd enabled.
jun2sak Feb 20, 2021
7931989
set SleepMemory size to 256 bytes.
jun2sak Feb 20, 2021
d659c2c
move port-specific debug functions from supervisor/shared/serial.c to…
jun2sak Feb 21, 2021
36c5925
some cleanups.
jun2sak Feb 21, 2021
1da8b99
Merge branch 'main' into nrf52-sleep
jun2sak Feb 21, 2021
62b38e2
update translation file.
jun2sak Feb 21, 2021
5c858a1
add weak board_deinit().
jun2sak Feb 21, 2021
9df0f43
alarm/pin/__init__.c is no longer used.
jun2sak Feb 23, 2021
372c986
move all the debug codes from port.c to debug_uart.c.
jun2sak Feb 23, 2021
ec64fa6
move dump_xxx functions to debug_uart.c.
jun2sak Feb 23, 2021
105042e
move dump_xxx functions to debug_uart.c.
jun2sak Feb 23, 2021
0f188be
set RAM retention just before deep sleep
jun2sak Feb 23, 2021
057682f
cleanup.
jun2sak Feb 23, 2021
72b5f1a
clean up my personal settings.
jun2sak Feb 24, 2021
9661d67
replace MY_DEBUG_UART -> NRF_DEBUG_PRINT.
jun2sak Feb 24, 2021
f66896c
use nRF SDK function to set up memory retention.
jun2sak Feb 24, 2021 8000
61a69da
raise NotImplementedError when construct TouchAlarm.
jun2sak Feb 24, 2021
9328d09
re-enable parameters check.
jun2sak Feb 25, 2021
c86ca2a
move externs to .h
jun2sak Feb 25, 2021
c713d31
move externs to .h
jun2sak Feb 25, 2021
5b3c6ed
update translation file.
jun2sak Feb 25, 2021
2aa5aec
cleanup.
jun2sak Feb 25, 2021
277a67d
call touchalarm funcs as well as pin/timealarm.
jun2sak Feb 25, 2021
7fd4648
rase error if Alarm time >= 512 sec.
jun2sak Feb 26, 2021
0f8c96f
remove trailing whitespaces.
jun2sak Feb 26, 2021
fac86c8
remove unused debug printf's.
jun2sak Feb 28, 2021
9b34726
GPIO and GPIOTE reg dump for debug.
jun2sak Feb 28, 2021
498debc
remove unused debug printf's.
jun2sak Feb 28, 2021
3e47e00
address the pretending-to-deep-sleep issue.
jun2sak Feb 28, 2021
7cecd99
remove unused debug printf's.
jun2sak Feb 28, 2021
7bb7896
use the original port_idle_until_interrupt() if not CIRCUITPY_ALARM.
jun2sak Feb 28, 2021
3fab9f8
new wait-until-alarm design, don't use System OFF.
jun2sak Mar 6, 2021
cd5c0e9
Clean up.
jun2sak Mar 7, 2021
2795c00
disable QSPI while system ON idle loop.
jun2sak Mar 7, 2021
7430b92
address hw pin-reset while sleep.
jun2sak Mar 7, 2021
2eed9a1
old System OFF sleep code for future reference.
jun2sak Mar 7, 2021
5bd1107
Clean up.
jun2sak Mar 7, 2021
51c7a49
Merge branch 'main' into nrf52-sleep
jun2sak Mar 7, 2021
c8f36e6
reset myself, not go REPL, after wakeup from deep sleep.
jun2sak Mar 8, 2021
15764b4
Merge remote-tracking branch 'upstream/main' into nrf52-sleep
hierophect Apr 9, 2021
6e9429d
soft reboot for pretending deep sleep.
jun2sak Apr 12, 2021
a702409
Merge remote-tracking branch 'upstream/main' into nrf52-sleep
hierophect Apr 20, 2021
436e33e
Fix CI formatting issues
hierophect Apr 20, 2021
a1dc423
Remove Alarm from small boards
hierophect Apr 20, 2021
39b7d9b
Fix indentation
hierophect Apr 20, 2021
779169d
Reduce Simmel size
hierophect Apr 21, 2021
beb7e33
Fix attribution and revert changes to main
hierophect Apr 21, 2021
a17fcb6
add copyright notice. remove obsolete comments.
jun2sak Apr 25, 2021
7accb8b
modify copyright notice.
jun2sak Apr 25, 2021
7daba64
merge.
jun2sak Apr 25, 2021
8bd77b7
fix conflicts.
jun2sak Apr 25, 2021
872fff7
simplify system_on_idle_until_alarm()
jun2sak Apr 25, 2021
4384036
Fix submodule
hierophect Apr 26, 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
6 changes: 5 additions & 1 deletion locale/circuitpython.pot
10000
Original file line number Diff line number Diff line change
Expand Up @@ -681,6 +681,7 @@ msgid "Cannot vary frequency on a timer that is already in use"
msgstr ""

#: ports/esp32s2/common-hal/alarm/pin/PinAlarm.c
#: ports/nrf/common-hal/alarm/pin/PinAlarm.c
msgid "Cannot wake on pin edge. Only level."
msgstr ""

Expand Down Expand Up @@ -1287,7 +1288,8 @@ msgstr ""
#: ports/atmel-samd/common-hal/audioio/AudioOut.c
#: ports/atmel-samd/common-hal/touchio/TouchIn.c
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
#: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
#: ports/esp32s2/common-hal/touchio/TouchIn.c
#: ports/nrf/common-hal/alarm/pin/PinAlarm.c shared-bindings/pwmio/PWMOut.c
#: shared-module/rgbmatrix/RGBMatrix.c
msgid "Invalid pin"
msgstr ""
Expand Down Expand Up @@ -1665,6 +1667,7 @@ msgid "Only one TouchAlarm can be set in deep sleep."
msgstr ""

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

Expand Down Expand Up @@ -3715,6 +3718,7 @@ msgstr ""
#: ports/esp32s2/boards/adafruit_magtag_2.9_grayscale/mpconfigboard.h
#: ports/esp32s2/boards/adafruit_metro_esp32s2/mpconfigboard.h
#: ports/esp32s2/boards/artisense_rd00/mpconfigboard.h
#: ports/esp32s2/boards/atmegazero_esp32s2/mpconfigboard.h
#: ports/esp32s2/boards/electroniccats_bastwifi/mpconfigboard.h
#: ports/esp32s2/boards/espressif_kaluga_1/mpconfigboard.h
#: ports/esp32s2/boards/espressif_saola_1_wroom/mpconfigboard.h
Expand Down
1 change: 1 addition & 0 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
// it may also return due to another interrupt, that's why we check
// for deep sleep alarms above. If it wasn't a deep sleep alarm,
// then we'll idle here again.

#if CIRCUITPY_ALARM
common_hal_alarm_pretending_deep_sleep();
#else
Expand Down
5 changes: 5 additions & 0 deletions ports/nrf/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,11 @@ else
CFLAGS += -flto -flto-partition=none
endif

ifeq ($(NRF_DEBUG_PRINT), 1)
CFLAGS += -DNRF_DEBUG_PRINT=1
SRC_SUPERVISOR += supervisor/debug_uart.c
endif

# option to override compiler optimization level, set in boards/$(BOARD)/mpconfigboard.mk
CFLAGS += $(OPTIMIZATION_FLAGS)

Expand Down
1 change: 1 addition & 0 deletions ports/nrf/boards/pca10100/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ MCU_CHIP = nrf52833

INTERNAL_FLASH_FILESYSTEM = 1

CIRCUITPY_ALARM = 0
CIRCUITPY_AUDIOMP3 = 0
CIRCUITPY_BITBANGIO = 0
CIRCUITPY_BITMAPTOOLS = 0
Expand Down
5 changes: 5 additions & 0 deletions ports/nrf/boards/simmel/mpconfigboard.mk
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@ MCU_CHIP = nrf52833

INTERNAL_FLASH_FILESYSTEM = 1

CIRCUITPY_ALARM = 0
CIRCUITPY_AESIO = 1
CIRCUITPY_AUDIOMP3 = 0
CIRCUITPY_BITMAPTOOLS = 0
CIRCUITPY_BUSDEVICE = 0
CIRCUITPY_BUSIO = 1
CIRCUITPY_COUNTIO = 0
CIRCUITPY_DISPLAYIO = 0
CIRCUITPY_ERRNO = 0
CIRCUITPY_FRAMEBUFFERIO = 0
Expand All @@ -23,10 +25,13 @@ CIRCUITPY_MSGPACK = 0
CIRCUITPY_NEOPIXEL_WRITE = 0
CIRCUITPY_NVM = 0
CIRCUITPY_PIXELBUF = 0
CIRCUITPY_PULSEIO = 0
CIRCUITPY_PWMIO = 1
CIRCUITPY_RGBMATRIX = 0
CIRCUITPY_ROTARYIO = 0
CIRCUITPY_RTC = 1
CIRCUITPY_SDCARDIO = 0
CIRCUITPY_SYNTHIO = 0
CIRCUITPY_TOUCHIO = 0
CIRCUITPY_ULAB = 0
CIRCUITPY_USB_CDC = 0
Expand Down
118 changes: 118 additions & 0 deletions ports/nrf/common-hal/alarm/SleepMemory.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Junji Sakai
*
* 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/__init__.h"
#include "common-hal/alarm/SleepMemory.h"
#include "nrf_power.h"

#ifdef NRF_DEBUG_PRINT
extern void dbg_dump_RAMreg(void);
#include "supervisor/serial.h" // dbg_printf()
#endif

__attribute__((section(".uninitialized"))) static uint8_t _sleepmem[SLEEP_MEMORY_LENGTH];
__attribute__((section(".uninitialized"))) uint8_t sleepmem_wakeup_event;
__attribute__((section(".uninitialized"))) uint8_t sleepmem_wakeup_pin;
__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;
}

void set_memory_retention(void) {
// set RAM[n].POWER register for RAM retention
// nRF52840 has RAM[0..7].Section[0..1] and RAM[8].Section[0..5]
// nRF52833 has RAM[0..7].Section[0..1] and RAM[8].Section[0,1]
for(int block = 0; block <= 7; ++block) {
nrf_power_rampower_mask_on(NRF_POWER, block,
NRF_POWER_RAMPOWER_S0RETENTION_MASK |
NRF_POWER_RAMPOWER_S1RETENTION_MASK);
};
#ifdef NRF52840
nrf_power_rampower_mask_on(NRF_POWER, 8,
NRF_POWER_RAMPOWER_S0RETENTION_MASK |
NRF_POWER_RAMPOWER_S1RETENTION_MASK |
NRF_POWER_RAMPOWER_S2RETENTION_MASK |
NRF_POWER_RAMPOWER_S3RETENTION_MASK |
NRF_POWER_RAMPOWER_S4RETENTION_MASK |
NRF_POWER_RAMPOWER_S5RETENTION_MASK);
#endif
#ifdef NRF52833
nrf_power_rampower_mask_on(NRF_POWER, 8,
NRF_POWER_RAMPOWER_S0RETENTION_MASK |
NRF_POWER_RAMPOWER_S1RETENTION_MASK);
#endif
}

static void initialize_sleep_memory(void) {
memset((uint8_t *)_sleepmem, 0, SLEEP_MEMORY_LENGTH);
sleepmem_wakeup_event = 0;
sleepmem_wakeup_pin = 0;

set_memory_retention();
#ifdef NRF_DEBUG_PRINT
//dbg_dump_RAMreg();
#endif

_sleepmem_magicnum = SLEEP_MEMORY_DATA_GUARD;
}

void alarm_sleep_memory_reset(void) {
if (!is_sleep_memory_valid()) {
initialize_sleep_memory();
#ifdef NRF_DEBUG_PRINT
dbg_printf("sleep memory initialized\r\n");
#endif
}
}

uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
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) {
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) {
if (start_index + len > sizeof(_sleepmem)) {
return;
}
memcpy(values, (uint8_t *) (_sleepmem + start_index), len);
}
41 changes: 41 additions & 0 deletions ports/nrf/common-hal/alarm/SleepMemory.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* This file is part of the MicroPython project, http://micropython.org/
*
* The MIT License (MIT)
*
* Copyright (c) 2021 Junji Sakai
*
* 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_NRF_COMMON_HAL_ALARM_SLEEPMEMORY_H
#define MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM_SLEEPMEMORY_H

#include "py/obj.h"

#define SLEEP_MEMORY_LENGTH (256)

typedef struct {
mp_obj_base_t base;
} alarm_sleep_memory_obj_t;

extern void set_memory_retention(void);
extern void alarm_sleep_memory_reset(void);

#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM_SLEEPMEMORY_H
Loading
0