2021-02-17 06:53:18 -05:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2020 Scott Shawcroft for Adafruit Industries
|
|
|
|
* Copyright (c) 2020 Dan Halbert for Adafruit Industries
|
2021-04-25 06:19:11 -04:00
|
|
|
* Copyright (c) 2021 Junji Sakai
|
2021-02-17 06:53:18 -05:00
|
|
|
*
|
|
|
|
* 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 "py/gc.h"
|
|
|
|
#include "py/obj.h"
|
|
|
|
#include "py/objtuple.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "shared-bindings/alarm/__init__.h"
|
|
|
|
#include "shared-bindings/alarm/SleepMemory.h"
|
|
|
|
#include "shared-bindings/alarm/pin/PinAlarm.h"
|
|
|
|
#include "shared-bindings/alarm/time/TimeAlarm.h"
|
|
|
|
#include "shared-bindings/alarm/touch/TouchAlarm.h"
|
2021-03-06 11:09:54 -05:00
|
|
|
#include "shared-bindings/time/__init__.h"
|
2021-02-17 06:53:18 -05:00
|
|
|
|
|
|
|
#include "supervisor/port.h"
|
2021-03-06 11:09:54 -05:00
|
|
|
#include "supervisor/serial.h" // serial_connected()
|
2021-02-24 11:49:57 -05:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-02-20 10:47:07 -05:00
|
|
|
#include "supervisor/serial.h" // dbg_printf()
|
2021-03-06 11:09:54 -05:00
|
|
|
extern int dbg_check_RTCprescaler(void);
|
2021-02-20 10:47:07 -05:00
|
|
|
#endif
|
2021-03-07 05:14:04 -05:00
|
|
|
#include "supervisor/qspi_flash.h"
|
2021-02-17 06:53:18 -05:00
|
|
|
|
|
|
|
#include "nrf.h"
|
|
|
|
#include "nrf_power.h"
|
|
|
|
#include "nrfx.h"
|
|
|
|
#include "nrfx_gpiote.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) {
|
2021-02-20 10:47:07 -05:00
|
|
|
alarm_sleep_memory_reset();
|
2021-02-17 06:53:18 -05:00
|
|
|
alarm_pin_pinalarm_reset();
|
|
|
|
alarm_time_timealarm_reset();
|
2021-02-25 18:11:50 -05:00
|
|
|
alarm_touch_touchalarm_reset();
|
2021-02-17 06:53:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
extern uint32_t reset_reason_saved;
|
2021-02-20 10:47:07 -05:00
|
|
|
STATIC nrf_sleep_source_t _get_wakeup_cause(void) {
|
2021-05-14 14:34:35 -04:00
|
|
|
// First check if the modules remember what last woke up
|
|
|
|
if (alarm_pin_pinalarm_woke_this_cycle()) {
|
2021-02-17 06:53:18 -05:00
|
|
|
return NRF_SLEEP_WAKEUP_GPIO;
|
|
|
|
}
|
2021-05-14 14:34:35 -04:00
|
|
|
if (alarm_time_timealarm_woke_this_cycle()) {
|
2021-02-17 06:53:18 -05:00
|
|
|
return NRF_SLEEP_WAKEUP_TIMER;
|
|
|
|
}
|
2021-05-14 14:34:35 -04:00
|
|
|
if (alarm_touch_touchalarm_woke_this_cycle()) {
|
2021-02-25 11:45:12 -05:00
|
|
|
return NRF_SLEEP_WAKEUP_TOUCHPAD;
|
2021-02-17 06:53:18 -05:00
|
|
|
}
|
2021-05-14 14:34:35 -04:00
|
|
|
// If waking from true deep sleep, modules will have lost their state,
|
|
|
|
// so check the deep wakeup cause manually
|
2021-02-17 06:53:18 -05:00
|
|
|
if (reset_reason_saved & NRF_POWER_RESETREAS_RESETPIN_MASK) {
|
|
|
|
return NRF_SLEEP_WAKEUP_RESETPIN;
|
2021-04-30 11:47:37 -04:00
|
|
|
} else if (reset_reason_saved & NRF_POWER_RESETREAS_OFF_MASK) {
|
2021-02-17 06:53:18 -05:00
|
|
|
return NRF_SLEEP_WAKEUP_GPIO;
|
2021-04-30 11:47:37 -04:00
|
|
|
} else if (reset_reason_saved & NRF_POWER_RESETREAS_VBUS_MASK) {
|
2021-02-17 06:53:18 -05:00
|
|
|
return NRF_SLEEP_WAKEUP_VBUS;
|
|
|
|
}
|
|
|
|
return NRF_SLEEP_WAKEUP_UNDEFINED;
|
|
|
|
}
|
|
|
|
|
2021-02-24 11:49:57 -05:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-04-30 11:47:37 -04:00
|
|
|
static const char *cause_str[] = {
|
|
|
|
"UNDEFINED",
|
|
|
|
"GPIO",
|
|
|
|
"TIMER",
|
|
|
|
"TOUCHPAD",
|
|
|
|
"VBUS",
|
|
|
|
"RESETPIN",
|
2021-02-20 10:47:07 -05:00
|
|
|
};
|
2021-02-25 11:45:12 -05:00
|
|
|
void print_wakeup_cause(nrf_sleep_source_t cause) {
|
|
|
|
if (cause >= 0 && cause < NRF_SLEEP_WAKEUP_ZZZ) {
|
|
|
|
dbg_printf("wakeup cause = NRF_SLEEP_WAKEUP_%s\r\n",
|
2021-04-30 11:47:37 -04:00
|
|
|
cause_str[(int)cause]);
|
2021-02-25 11:45:12 -05:00
|
|
|
}
|
|
|
|
}
|
2021-02-20 18:17:57 -05:00
|
|
|
#endif
|
2021-02-20 10:47:07 -05:00
|
|
|
|
2021-04-09 13:48:45 -04:00
|
|
|
bool common_hal_alarm_woken_from_sleep(void) {
|
2021-04-30 11:47:37 -04:00
|
|
|
nrf_sleep_source_t cause = _get_wakeup_cause();
|
|
|
|
#ifdef NRF_DEBUG_PRINT
|
|
|
|
if (cause != NRF_SLEEP_WAKEUP_UNDEFINED) {
|
|
|
|
// print_wakeup_cause(cause);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return cause == NRF_SLEEP_WAKEUP_GPIO || cause == NRF_SLEEP_WAKEUP_TIMER
|
|
|
|
|| cause == NRF_SLEEP_WAKEUP_TOUCHPAD;
|
2021-02-28 01:57:37 -05:00
|
|
|
}
|
|
|
|
|
2021-05-14 14:34:35 -04:00
|
|
|
mp_obj_t common_hal_alarm_create_wake_alarm(void) {
|
|
|
|
// If woken from deep sleep, create a copy alarm similar to what would have
|
|
|
|
// been passed in originally. Otherwise, just return none
|
2021-02-20 10:47:07 -05:00
|
|
|
nrf_sleep_source_t cause = _get_wakeup_cause();
|
2021-02-17 06:53:18 -05:00
|
|
|
switch (cause) {
|
2021-04-26 17:59:21 -04:00
|
|
|
case NRF_SLEEP_WAKEUP_TIMER: {
|
2021-05-14 14:34:35 -04:00
|
|
|
return alarm_time_timealarm_create_wakeup_alarm();
|
2021-04-26 17:59:21 -04:00
|
|
|
}
|
|
|
|
case NRF_SLEEP_WAKEUP_TOUCHPAD: {
|
2021-05-14 14:34:35 -04:00
|
|
|
return alarm_touch_touchalarm_create_wakeup_alarm();
|
2021-04-26 17:59:21 -04:00
|
|
|
}
|
|
|
|
case NRF_SLEEP_WAKEUP_GPIO: {
|
2021-05-14 14:34:35 -04:00
|
|
|
return alarm_pin_pinalarm_create_wakeup_alarm();
|
2021-04-26 17:59:21 -04:00
|
|
|
}
|
|
|
|
default:
|
2021-04-30 11:47:37 -04:00
|
|
|
break;
|
2021-02-17 06:53:18 -05:00
|
|
|
}
|
|
|
|
return mp_const_none;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set up light sleep or deep sleep alarms.
|
|
|
|
STATIC void _setup_sleep_alarms(bool deep_sleep, size_t n_alarms, const mp_obj_t *alarms) {
|
|
|
|
alarm_pin_pinalarm_set_alarms(deep_sleep, n_alarms, alarms);
|
|
|
|
alarm_time_timealarm_set_alarms(deep_sleep, n_alarms, alarms);
|
|
|
|
alarm_touch_touchalarm_set_alarm(deep_sleep, n_alarms, alarms);
|
|
|
|
}
|
|
|
|
|
2021-04-25 06:57:21 -04:00
|
|
|
// TODO: this handles all possible types of wakeup, which is redundant with main.
|
|
|
|
// revise to extract all parts essential to enabling sleep wakeup, but leave the
|
|
|
|
// alarm/non-alarm sorting to the existing main loop.
|
2021-04-25 10:59:21 -04:00
|
|
|
void system_on_idle_until_alarm(int64_t timediff_ms, uint32_t prescaler) {
|
2021-03-06 11:09:54 -05:00
|
|
|
bool have_timeout = false;
|
|
|
|
uint64_t start_tick = 0, end_tick = 0;
|
2021-03-08 10:42:22 -05:00
|
|
|
int64_t tickdiff;
|
2021-03-06 11:09:54 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
#if defined(MICROPY_QSPI_CS)
|
2021-03-07 05:14:04 -05:00
|
|
|
qspi_flash_enter_sleep();
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-03-07 05:14:04 -05:00
|
|
|
|
2021-03-06 11:09:54 -05:00
|
|
|
if (timediff_ms != -1) {
|
|
|
|
have_timeout = true;
|
2021-04-30 11:47:37 -04:00
|
|
|
#if 0
|
|
|
|
int64_t now = common_hal_time_monotonic_ms();
|
|
|
|
dbg_printf("now_ms=%ld timediff_ms=%ld\r\n", (long)now, (long)timediff_ms);
|
|
|
|
#endif
|
|
|
|
if (timediff_ms < 0) {
|
|
|
|
timediff_ms = 0;
|
|
|
|
}
|
|
|
|
if (prescaler == 0) {
|
|
|
|
// 1 tick = 1/1024 sec = 1000/1024 ms
|
|
|
|
// -> 1 ms = 1024/1000 ticks
|
|
|
|
tickdiff = (mp_uint_t)(timediff_ms * 1024 / 1000); // ms -> ticks
|
|
|
|
} else {
|
|
|
|
// 1 tick = prescaler/1024 sec = prescaler*1000/1024 ms
|
|
|
|
// -> 1ms = 1024/(1000*prescaler) ticks
|
|
|
|
tickdiff = (mp_uint_t)(timediff_ms * 1024 / (1000 * prescaler));
|
|
|
|
}
|
|
|
|
start_tick = port_get_raw_ticks(NULL);
|
|
|
|
end_tick = start_tick + tickdiff;
|
2021-03-06 11:09:54 -05:00
|
|
|
}
|
2021-04-30 11:47:37 -04:00
|
|
|
#if 0
|
2021-03-06 11:09:54 -05:00
|
|
|
dbg_printf("start_tick=%ld end_tick=%ld have_timeout=%c\r\n", (long)start_tick, (long)end_tick, have_timeout ? 'T' : 'F');
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-03-06 11:09:54 -05:00
|
|
|
|
|
|
|
int64_t remaining;
|
|
|
|
sleepmem_wakeup_event = SLEEPMEM_WAKEUP_BY_NONE;
|
2021-04-30 11:47:37 -04:00
|
|
|
sleepmem_wakeup_pin = WAKEUP_PIN_UNDEF;
|
2021-03-06 11:09:54 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-02-17 06:53:18 -05:00
|
|
|
int ct = 40;
|
2021-03-06 11:09:54 -05:00
|
|
|
char reason = '?';
|
|
|
|
#define WAKEUP_REASON(x) reason = (x)
|
2021-04-30 11:47:37 -04:00
|
|
|
#else
|
2021-03-06 11:09:54 -05:00
|
|
|
#define WAKEUP_REASON(x)
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-03-06 11:09:54 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
while (1) {
|
2021-04-25 10:59:21 -04:00
|
|
|
if (mp_hal_is_interrupted()) {
|
|
|
|
WAKEUP_REASON('I');
|
|
|
|
break;
|
|
|
|
}
|
2021-03-06 11:09:54 -05:00
|
|
|
if (serial_connected() && serial_bytes_available()) {
|
2021-04-25 10:59:21 -04:00
|
|
|
WAKEUP_REASON('S');
|
|
|
|
break;
|
|
|
|
}
|
2021-02-17 06:53:18 -05:00
|
|
|
RUN_BACKGROUND_TASKS;
|
2021-04-25 10:59:21 -04:00
|
|
|
if (common_hal_alarm_woken_from_sleep()) {
|
|
|
|
WAKEUP_REASON('W');
|
2021-04-25 06:57:21 -04:00
|
|
|
break;
|
|
|
|
}
|
2021-04-25 10:59:21 -04:00
|
|
|
if (have_timeout) {
|
|
|
|
remaining = end_tick - port_get_raw_ticks(NULL);
|
|
|
|
// We break a bit early so we don't risk setting the alarm before the time when we call
|
|
|
|
// sleep.
|
|
|
|
if (remaining < 1) {
|
|
|
|
WAKEUP_REASON('t');
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
port_interrupt_after_ticks(remaining);
|
|
|
|
}
|
2021-03-06 11:09:54 -05:00
|
|
|
// Idle until an interrupt happens.
|
2021-02-17 06:53:18 -05:00
|
|
|
port_idle_until_interrupt();
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-02-28 01:57:37 -05:00
|
|
|
if (ct > 0) {
|
|
|
|
dbg_printf("_");
|
|
|
|
--ct;
|
|
|
|
}
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-04-25 10:59:21 -04:00
|
|
|
if (have_timeout) {
|
|
|
|
remaining = end_tick - port_get_raw_ticks(NULL);
|
|
|
|
if (remaining <= 0) {
|
|
|
|
sleepmem_wakeup_event = SLEEPMEM_WAKEUP_BY_TIMER;
|
|
|
|
WAKEUP_REASON('T');
|
|
|
|
break;
|
|
|
|
}
|
2021-04-25 06:57:21 -04:00
|
|
|
}
|
|
|
|
}
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-03-06 11:09:54 -05:00
|
|
|
dbg_printf("%c\r\n", reason);
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-03-07 05:14:04 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
#if defined(MICROPY_QSPI_CS)
|
2021-03-07 05:14:04 -05:00
|
|
|
qspi_flash_exit_sleep();
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-03-07 05:14:04 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-03-08 10:42:22 -05:00
|
|
|
tickdiff = port_get_raw_ticks(NULL) - start_tick;
|
2021-04-25 06:19:11 -04:00
|
|
|
double sec;
|
2021-03-08 10:42:22 -05:00
|
|
|
if (prescaler == 0) {
|
2021-04-25 06:19:11 -04:00
|
|
|
sec = (double)tickdiff / 1024;
|
2021-04-30 11:47:37 -04:00
|
|
|
} else {
|
2021-04-25 06:19:11 -04:00
|
|
|
sec = (double)(tickdiff * prescaler) / 1024;
|
2021-03-08 10:42:22 -05:00
|
|
|
}
|
2021-04-25 06:19:11 -04:00
|
|
|
dbg_printf("lapse %6.1f sec\r\n", sec);
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-02-17 06:53:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
mp_obj_t common_hal_alarm_light_sleep_until_alarms(size_t n_alarms, const mp_obj_t *alarms) {
|
2021-05-14 14:34:35 -04:00
|
|
|
mp_obj_t wake_alarm = mp_const_none;
|
2021-03-06 11:09:54 -05:00
|
|
|
alarm_time_timealarm_clear_wakeup_time();
|
2021-02-17 06:53:18 -05:00
|
|
|
_setup_sleep_alarms(false, n_alarms, alarms);
|
2021-03-06 11:09:54 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-03-06 11:09:54 -05:00
|
|
|
dbg_printf("\r\nlight sleep...");
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-02-17 06:53:18 -05:00
|
|
|
|
2021-03-06 11:09:54 -05:00
|
|
|
int64_t timediff_ms = alarm_time_timealarm_get_wakeup_timediff_ms();
|
2021-04-25 10:59:21 -04:00
|
|
|
system_on_idle_until_alarm(timediff_ms, 0);
|
2021-02-17 06:53:18 -05:00
|
|
|
|
|
|
|
if (mp_hal_is_interrupted()) {
|
2021-04-25 10:59:21 -04:00
|
|
|
wake_alarm = mp_const_none;
|
2021-04-30 11:47:37 -04:00
|
|
|
} else {
|
2021-05-14 14:34:35 -04:00
|
|
|
if (common_hal_alarm_woken_from_sleep()) {
|
|
|
|
nrf_sleep_source_t cause = _get_wakeup_cause();
|
|
|
|
switch (cause) {
|
|
|
|
case NRF_SLEEP_WAKEUP_TIMER: {
|
|
|
|
wake_alarm = alarm_time_timealarm_find_triggered_alarm(n_alarms,alarms);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case NRF_SLEEP_WAKEUP_GPIO: {
|
|
|
|
wake_alarm = alarm_pin_pinalarm_find_triggered_alarm(n_alarms,alarms);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
default:
|
|
|
|
// Should not reach this, if all light sleep types are covered correctly
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
shared_alarm_save_wake_alarm(wake_alarm);
|
|
|
|
}
|
2021-02-17 06:53:18 -05:00
|
|
|
}
|
2021-02-28 01:57:37 -05:00
|
|
|
alarm_reset();
|
2021-04-25 10:59:21 -04:00
|
|
|
return wake_alarm;
|
2021-02-17 06:53:18 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
void common_hal_alarm_set_deep_sleep_alarms(size_t n_alarms, const mp_obj_t *alarms) {
|
2021-03-06 11:09:54 -05:00
|
|
|
alarm_time_timealarm_clear_wakeup_time();
|
2021-02-17 06:53:18 -05:00
|
|
|
_setup_sleep_alarms(true, n_alarms, alarms);
|
|
|
|
}
|
|
|
|
|
2021-03-06 11:09:54 -05:00
|
|
|
#define PRESCALER_VALUE_IN_DEEP_SLEEP (1024)
|
2021-02-23 10:12:25 -05:00
|
|
|
|
2021-04-09 13:48:45 -04:00
|
|
|
void NORETURN common_hal_alarm_enter_deep_sleep(void) {
|
2021-02-17 06:53:18 -05:00
|
|
|
alarm_pin_pinalarm_prepare_for_deep_sleep();
|
2021-03-06 11:09:54 -05:00
|
|
|
alarm_time_timealarm_prepare_for_deep_sleep();
|
2021-02-17 06:53:18 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-03-06 11:09:54 -05:00
|
|
|
dbg_printf("\r\ndeep sleep...");
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-03-06 11:09:54 -05:00
|
|
|
int64_t timediff_ms = alarm_time_timealarm_get_wakeup_timediff_ms();
|
2021-04-30 11:47:37 -04:00
|
|
|
tick_set_prescaler(PRESCALER_VALUE_IN_DEEP_SLEEP - 1);
|
|
|
|
#ifdef NRF_DEBUG_PRINT
|
|
|
|
dbg_check_RTCprescaler(); // XXX
|
|
|
|
#endif
|
2021-04-25 10:59:21 -04:00
|
|
|
system_on_idle_until_alarm(timediff_ms, PRESCALER_VALUE_IN_DEEP_SLEEP);
|
2021-02-23 10:12:25 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-03-06 11:09:54 -05:00
|
|
|
dbg_printf("RESET...\r\n\r\n");
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-03-06 11:09:54 -05:00
|
|
|
|
|
|
|
reset_cpu();
|
2021-02-17 06:53:18 -05:00
|
|
|
|
|
|
|
// should not reach here..
|
2021-04-30 11:47:37 -04:00
|
|
|
while (1) {
|
|
|
|
;
|
|
|
|
}
|
2021-02-17 06:53:18 -05:00
|
|
|
}
|
|
|
|
|
2021-04-12 07:53:14 -04:00
|
|
|
// old version deep sleep code that was used in common_hal_alarm_enter_deep_sleep()
|
2021-03-07 06:21:33 -05:00
|
|
|
// for anyone who might want true System OFF sleep ..
|
|
|
|
#if 0
|
|
|
|
void OLD_go_system_off(void) {
|
|
|
|
sleepmem_wakeup_event = SLEEPMEM_WAKEUP_BY_NONE;
|
2021-04-30 11:47:37 -04:00
|
|
|
sleepmem_wakeup_pin = WAKEUP_PIN_UNDEF;
|
2021-03-07 06:21:33 -05:00
|
|
|
uint8_t sd_enabled;
|
|
|
|
sd_softdevice_is_enabled(&sd_enabled);
|
|
|
|
set_memory_retention();
|
|
|
|
dbg_printf("OLD go system off.. %d\r\n", sd_enabled);
|
|
|
|
if (sd_enabled) {
|
|
|
|
sd_power_system_off();
|
2021-04-30 11:47:37 -04:00
|
|
|
} else {
|
2021-03-07 06:21:33 -05:00
|
|
|
NRF_POWER->SYSTEMOFF = 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-04-09 13:48:45 -04:00
|
|
|
void common_hal_alarm_pretending_deep_sleep(void) {
|
2021-02-28 01:57:37 -05:00
|
|
|
alarm_pin_pinalarm_prepare_for_deep_sleep();
|
2021-03-06 11:09:54 -05:00
|
|
|
alarm_time_timealarm_prepare_for_deep_sleep();
|
2021-02-28 01:57:37 -05:00
|
|
|
|
2021-04-30 11:47:37 -04:00
|
|
|
#ifdef NRF_DEBUG_PRINT
|
2021-03-06 11:09:54 -05:00
|
|
|
dbg_printf("\r\npretending to deep sleep...");
|
2021-04-30 11:47:37 -04:00
|
|
|
#endif
|
2021-03-06 11:09:54 -05:00
|
|
|
|
|
|
|
int64_t timediff_ms = alarm_time_timealarm_get_wakeup_timediff_ms();
|
2021-04-25 10:59:21 -04:00
|
|
|
system_on_idle_until_alarm(timediff_ms, 0);
|
2021-03-06 11:09:54 -05:00
|
|
|
|
2021-04-25 10:59:21 -04:00
|
|
|
alarm_reset();
|
2021-02-28 01:57:37 -05:00
|
|
|
}
|
|
|
|
|
2021-02-17 06:53:18 -05:00
|
|
|
void common_hal_alarm_gc_collect(void) {
|
2021-04-12 07:53:14 -04:00
|
|
|
gc_collect_ptr(shared_alarm_get_wake_alarm());
|
2021-02-17 06:53:18 -05:00
|
|
|
}
|