2016-10-25 17:27:59 -04:00
|
|
|
/*
|
2017-08-27 15:02:50 -04:00
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
2016-10-25 17:27:59 -04:00
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016 Scott Shawcroft 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.
|
|
|
|
*/
|
|
|
|
|
2022-03-11 13:29:20 -05:00
|
|
|
#include "reload.h"
|
2016-10-25 17:27:59 -04:00
|
|
|
|
|
|
|
#include "py/mphal.h"
|
2022-03-11 13:29:20 -05:00
|
|
|
#include "py/mpstate.h"
|
2022-12-02 17:54:42 -05:00
|
|
|
#include "supervisor/port.h"
|
2022-03-11 13:29:20 -05:00
|
|
|
#include "supervisor/shared/reload.h"
|
2020-03-06 21:15:16 -05:00
|
|
|
#include "supervisor/shared/tick.h"
|
2016-10-25 17:27:59 -04:00
|
|
|
|
2021-05-04 17:04:24 -04:00
|
|
|
#include "shared-bindings/supervisor/Runtime.h"
|
2020-10-29 11:39:06 -04:00
|
|
|
|
2022-03-04 13:34:20 -05:00
|
|
|
// True if user has disabled autoreload.
|
2019-03-20 12:21:36 -04:00
|
|
|
static bool autoreload_enabled = false;
|
|
|
|
|
2022-03-04 13:34:20 -05:00
|
|
|
// Non-zero if autoreload is temporarily off, due to an AUTORELOAD_SUSPEND_... reason.
|
|
|
|
static uint32_t autoreload_suspended = 0;
|
|
|
|
|
2022-03-14 19:49:30 -04:00
|
|
|
volatile uint32_t last_autoreload_trigger = 0;
|
2016-10-28 23:16:39 -04:00
|
|
|
|
2022-03-11 13:29:20 -05:00
|
|
|
void reload_initiate(supervisor_run_reason_t run_reason) {
|
|
|
|
supervisor_set_run_reason(run_reason);
|
|
|
|
|
|
|
|
// Raise reload exception, in case code is running.
|
|
|
|
MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception));
|
|
|
|
#if MICROPY_ENABLE_SCHEDULER
|
|
|
|
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
|
|
|
|
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
|
2022-03-04 13:34:20 -05:00
|
|
|
}
|
2022-03-11 13:29:20 -05:00
|
|
|
#endif
|
2022-12-02 17:54:42 -05:00
|
|
|
port_wake_main_task();
|
2022-03-04 13:34:20 -05:00
|
|
|
}
|
|
|
|
|
2022-03-11 13:29:20 -05:00
|
|
|
void autoreload_reset() {
|
2022-03-14 19:49:30 -04:00
|
|
|
last_autoreload_trigger = 0;
|
2016-10-25 17:27:59 -04:00
|
|
|
}
|
|
|
|
|
2017-05-12 19:45:38 -04:00
|
|
|
void autoreload_enable() {
|
|
|
|
autoreload_enabled = true;
|
2022-03-14 19:49:30 -04:00
|
|
|
last_autoreload_trigger = 0;
|
2016-10-25 17:27:59 -04:00
|
|
|
}
|
|
|
|
|
2017-05-12 19:45:38 -04:00
|
|
|
void autoreload_disable() {
|
|
|
|
autoreload_enabled = false;
|
2016-10-25 17:27:59 -04:00
|
|
|
}
|
|
|
|
|
2022-03-04 13:34:20 -05:00
|
|
|
void autoreload_suspend(uint32_t suspend_reason_mask) {
|
|
|
|
autoreload_suspended |= suspend_reason_mask;
|
2017-11-15 17:44:14 -05:00
|
|
|
}
|
|
|
|
|
2022-03-04 13:34:20 -05:00
|
|
|
void autoreload_resume(uint32_t suspend_reason_mask) {
|
|
|
|
autoreload_suspended &= ~suspend_reason_mask;
|
2017-11-15 17:44:14 -05:00
|
|
|
}
|
|
|
|
|
2017-05-12 19:45:38 -04:00
|
|
|
inline bool autoreload_is_enabled() {
|
|
|
|
return autoreload_enabled;
|
2017-03-27 15:12:30 -04:00
|
|
|
}
|
|
|
|
|
2022-03-14 19:49:30 -04:00
|
|
|
void autoreload_trigger() {
|
2023-03-21 12:04:30 -04:00
|
|
|
if (!autoreload_enabled || autoreload_suspended != 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
bool reload_initiated = autoreload_pending();
|
|
|
|
last_autoreload_trigger = supervisor_ticks_ms32();
|
|
|
|
// Guard against the rare time that ticks is 0;
|
|
|
|
if (last_autoreload_trigger == 0) {
|
|
|
|
last_autoreload_trigger += 1;
|
|
|
|
}
|
|
|
|
// Initiate a reload of the VM immediately. Later code will pause to
|
|
|
|
// wait for the autoreload to become ready. Doing the VM exit
|
|
|
|
// immediately is clearer for the user.
|
|
|
|
if (!reload_initiated) {
|
2022-03-11 13:29:20 -05:00
|
|
|
reload_initiate(RUN_REASON_AUTO_RELOAD);
|
2020-03-06 21:15:16 -05:00
|
|
|
}
|
2019-09-14 15:40:24 -04:00
|
|
|
}
|
2022-03-14 19:49:30 -04:00
|
|
|
|
|
|
|
bool autoreload_ready() {
|
|
|
|
if (last_autoreload_trigger == 0 || autoreload_suspended != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Wait for autoreload interval before reloading
|
|
|
|
uint32_t now = supervisor_ticks_ms32();
|
|
|
|
uint32_t diff;
|
|
|
|
if (now >= last_autoreload_trigger) {
|
|
|
|
diff = now - last_autoreload_trigger;
|
|
|
|
} else {
|
|
|
|
diff = now + (0xffffffff - last_autoreload_trigger);
|
|
|
|
}
|
|
|
|
return diff > CIRCUITPY_AUTORELOAD_DELAY_MS;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool autoreload_pending(void) {
|
2023-03-21 12:04:30 -04:00
|
|
|
return last_autoreload_trigger > 0;
|
2022-03-14 19:49:30 -04:00
|
|
|
}
|