8000 Merge pull request #4236 from jun2sak/nrf52-sleep · adafruit/circuitpython@d4d96bb · GitHub
[go: up one dir, main page]

Skip to content

Commit d4d96bb

Browse files
authored
Merge pull request #4236 from jun2sak/nrf52-sleep
sleep and wakeup for nrf52
2 parents fcabbe9 + 4384036 commit d4d96bb

26 files changed

+1553
-7
lines changed

locale/circuitpython.pot

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -681,6 +681,7 @@ msgid "Cannot vary frequency on a timer that is already in use"
681681
msgstr ""
682682

683683
#: ports/esp32s2/common-hal/alarm/pin/PinAlarm.c
684+
#: ports/nrf/common-hal/alarm/pin/PinAlarm.c
684685
msgid "Cannot wake on pin edge. Only level."
685686
msgstr ""
686687

@@ -1285,7 +1286,8 @@ msgstr ""
12851286
#: ports/atmel-samd/common-hal/audioio/AudioOut.c
12861287
#: ports/atmel-samd/common-hal/touchio/TouchIn.c
12871288
#: ports/esp32s2/common-hal/alarm/touch/TouchAlarm.c
1288-
#: ports/esp32s2/common-hal/touchio/TouchIn.c shared-bindings/pwmio/PWMOut.c
1289+
#: ports/esp32s2/common-hal/touchio/TouchIn.c
1290+
#: ports/nrf/common-hal/alarm/pin/PinAlarm.c shared-bindings/pwmio/PWMOut.c
12891291
#: shared-module/rgbmatrix/RGBMatrix.c
12901292
msgid "Invalid pin"
12911293
msgstr ""
@@ -1663,6 +1665,7 @@ msgid "Only one TouchAlarm can be set in deep sleep."
16631665
msgstr ""
16641666

16651667
#: ports/esp32s2/common-hal/alarm/time/TimeAlarm.c
1668+
#: ports/nrf/common-hal/alarm/time/TimeAlarm.c
16661669
msgid "Only one alarm.time alarm can be set."
16671670
msgstr ""
16681671

main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode) {
427427
// it may also return due to another interrupt, that's why we check
428428
// for deep sleep alarms above. If it wasn't a deep sleep alarm,
429429
// then we'll idle here again.
430+
430431
#if CIRCUITPY_ALARM
431432
common_hal_alarm_pretending_deep_sleep();
432433
#else

ports/nrf/Makefile

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,11 @@ else
9494
CFLAGS += -flto -flto-partition=none
9595
endif
9696

97+
ifeq ($(NRF_DEBUG_PRINT), 1)
98+
CFLAGS += -DNRF_DEBUG_PRINT=1
99+
SRC_SUPERVISOR += supervisor/debug_uart.c
100+
endif
101+
97102
# option to override compiler optimization level, set in boards/$(BOARD)/mpconfigboard.mk
98103
CFLAGS += $(OPTIMIZATION_FLAGS)
99104

ports/nrf/boards/pca10100/mpconfigboard.mk

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ MCU_CHIP = nrf52833
77

88
INTERNAL_FLASH_FILESYSTEM = 1
99

10+
CIRCUITPY_ALARM = 0
1011
CIRCUITPY_AUDIOMP3 = 0
1112
CIRCUITPY_BITBANGIO = 0
1213
CIRCUITPY_BITMAPTOOLS = 0

ports/nrf/boards/simmel/mpconfigboard.mk

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ MCU_CHIP = nrf52833
1010

1111
INTERNAL_FLASH_FILESYSTEM = 1
1212

13+
CIRCUITPY_ALARM = 0
1314
CIRCUITPY_AESIO = 1
1415
CIRCUITPY_AUDIOMP3 = 0
1516
CIRCUITPY_BITMAPTOOLS = 0
@@ -24,6 +25,8 @@ CIRCUITPY_MSGPACK = 0
2425
CIRCUITPY_NEOPIXEL_WRITE = 0
2526
CIRCUITPY_NVM = 0
2627
CIRCUITPY_PIXELBUF = 0
28+
CIRCUITPY_PULSEIO = 0
29+
CIRCUITPY_PWMIO = 1
2730
CIRCUITPY_RGBMATRIX = 0
2831
CIRCUITPY_ROTARYIO = 0
2932
CIRCUITPY_RTC = 1
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Junji Sakai
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+
#include <string.h>
28+
#include "py/runtime.h"
29+
#include "common-hal/alarm/__init__.h"
30+
#include "common-hal/alarm/SleepMemory.h"
31+
#include "nrf_power.h"
32+
33+
#ifdef NRF_DEBUG_PRINT
34+
extern void dbg_dump_RAMreg(void);
35+
#include "supervisor/serial.h" // dbg_printf()
36+
#endif
37+
38+
__attribute__((section(".uninitialized"))) static uint8_t _sleepmem[SLEEP_MEMORY_LENGTH];
39+
__attribute__((section(".uninitialized"))) uint8_t sleepmem_wakeup_event;
40+
__attribute__((section(".uninitialized"))) uint8_t sleepmem_wakeup_pin;
41+
__attribute__((section(".uninitialized"))) static uint32_t _sleepmem_magicnum;
42+
#define SLEEP_MEMORY_DATA_GUARD 0xad0000af
43+
#define SLEEP_MEMORY_DATA_GUARD_MASK 0xff0000ff
44+
45+
static int is_sleep_memory_valid(void) {
46+
if ((_sleepmem_magicnum & SLEEP_MEMORY_DATA_GUARD_MASK)
47+
== SLEEP_MEMORY_DATA_GUARD) {
48+
return 1;
49+
}
50+
return 0;
51+
}
52+
53+
void set_memory_retention(void) {
54+
// set RAM[n].POWER register for RAM retention
55+
// nRF52840 has RAM[0..7].Section[0..1] and RAM[8].Section[0..5]
56+
// nRF52833 has RAM[0..7].Section[0..1] and RAM[8].Section[0,1]
57+
for(int block = 0; block <= 7; ++block) {
58+
nrf_power_rampower_mask_on(NRF_POWER, block,
59+
NRF_POWER_RAMPOWER_S0RETENTION_MASK |
60+
NRF_POWER_RAMPOWER_S1RETENTION_MASK);
61+
};
62+
#ifdef NRF52840
63+
nrf_power_rampower_mask_on(NRF_POWER, 8,
64+
NRF_POWER_RAMPOWER_S0RETENTION_MASK |
65+
NRF_POWER_RAMPOWER_S1RETENTION_MASK |
66+
NRF_POWER_RAMPOWER_S2RETENTION_MASK |
67+
NRF_POWER_RAMPOWER_S3RETENTION_MASK |
68+
NRF_POWER_RAMPOWER_S4RETENTION_MASK |
69+
NRF_POWER_RAMPOWER_S5RETENTION_MASK);
70+
#endif
71+
#ifdef NRF52833
72+
nrf_power_rampower_mask_on(NRF_POWER, 8,
73+
NRF_POWER_RAMPOWER_S0RETENTION_MASK |
74+
NRF_POWER_RAMPOWER_S1RETENTION_MASK);
75+
#endif
76+
}
77+
78+
static void initialize_sleep_memory(void) {
79+
memset((uint8_t *)_sleepmem, 0, SLEEP_MEMORY_LENGTH);
80+
sleepmem_wakeup_event = 0;
81+
sleepmem_wakeup_pin = 0;
82+
83+
set_memory_retention();
84+
#ifdef NRF_DEBUG_PRINT
85+
//dbg_dump_RAMreg();
86+
#endif
87+
88+
_sleepmem_magicnum = SLEEP_MEMORY_DATA_GUARD;
89+
}
90+
91+
void alarm_sleep_memory_reset(void) {
92+
if (!is_sleep_memory_valid()) {
93+
initialize_sleep_memory();
94+
#ifdef NRF_DEBUG_PRINT
95+
dbg_printf("sleep memory initialized\r\n");
96+
#endif
97+
}
98+
}
99+
100+
uint32_t common_hal_alarm_sleep_memory_get_length(alarm_sleep_memory_obj_t *self) {
101+
return sizeof(_sleepmem);
102+
}
103+
104+
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) {
105+
if (start_index + len > sizeof(_sleepmem)) {
106+
return false;
107+
}
108+
109+
memcpy((uint8_t *) (_sleepmem + start_index), values, len);
110+
return true;
111+
}
112+
113+
void common_hal_alarm_sleep_memory_get_bytes(alarm_sleep_memory_obj_t *self, uint32_t start_index, uint8_t* values, uint32_t len) {
114+
if (start_index + len > sizeof(_sleepmem)) {
115+
return;
116+
}
117+
memcpy(values, (uint8_t *) (_sleepmem + start_index), len);
118+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2021 Junji Sakai
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_NRF_COMMON_HAL_ALARM_SLEEPMEMORY_H
28+
#define MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM_SLEEPMEMORY_H
29+
30+
#include "py/obj.h"
31+
32+
#define SLEEP_MEMORY_LENGTH (256)
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
} alarm_sleep_memory_obj_t;
37+
38+
extern void set_memory_retention(void);
39+
extern void alarm_sleep_memory_reset(void);
40+
41+
#endif // MICROPY_INCLUDED_NRF_COMMON_HAL_ALARM_SLEEPMEMORY_H

0 commit comments

Comments
 (0)
0