Skip to content
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

7.2.x autoreload rework (again) #6142

Merged
merged 1 commit into from
Mar 12, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 20 additions & 4 deletions main.c
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@
#include "supervisor/memory.h"
#include "supervisor/port.h"
#include "supervisor/serial.h"
#include "supervisor/shared/autoreload.h"
#include "supervisor/shared/reload.h"
#include "supervisor/shared/safe_mode.h"
#include "supervisor/shared/stack.h"
#include "supervisor/shared/status_leds.h"
Expand Down Expand Up @@ -389,12 +389,28 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re

// Print done before resetting everything so that we get the message over
// BLE before it is reset and we have a delay before reconnect.
if (result.return_code == PYEXEC_RELOAD) {
if ((result.return_code & PYEXEC_RELOAD) && supervisor_get_run_reason() == RUN_REASON_AUTO_RELOAD) {
serial_write_compressed(translate("\nCode stopped by auto-reload.\n"));

// Wait for autoreload interval before reloading
uint64_t start_ticks = 0;
do {
// Start waiting, or restart interval if another reload request was initiated
// while we were waiting.
if (reload_requested) {
reload_requested = false;
start_ticks = supervisor_ticks_ms64();
}
RUN_BACKGROUND_TASKS;
} while (supervisor_ticks_ms64() - start_ticks < CIRCUITPY_AUTORELOAD_DELAY_MS);

// Restore request for use below.
reload_requested = true;
} else {
serial_write_compressed(translate("\nCode done running.\n"));
}


// Finished executing python code. Cleanup includes filesystem flush and a board reset.
cleanup_after_vm(heap, result.exception);

Expand Down Expand Up @@ -474,8 +490,8 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
while (!skip_wait) {
RUN_BACKGROUND_TASKS;

// If a reload was requested by the supervisor or autoreload, return
if (result.return_code & PYEXEC_RELOAD) {
// If a reload was requested by the supervisor or autoreload, return.
if (reload_requested) {
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD;
// Should the STICKY_ON_SUCCESS and STICKY_ON_ERROR bits be cleared in
// next_code_stickiness_situation? I can see arguments either way, but I'm deciding
Expand Down
1 change: 0 additions & 1 deletion ports/atmel-samd/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "py/smallint.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/time/__init__.h"
#include "supervisor/shared/autoreload.h"

#include "hal/include/hal_atomic.h"
#include "hal/include/hal_delay.h"
Expand Down
1 change: 0 additions & 1 deletion ports/raspberrypi/mphalport.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
#include "py/smallint.h"
#include "shared-bindings/microcontroller/__init__.h"
#include "shared-bindings/time/__init__.h"
#include "supervisor/shared/autoreload.h"

#include "mpconfigboard.h"
#include "mphalport.h"
Expand Down
2 changes: 1 addition & 1 deletion py/circuitpy_mpconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ void supervisor_run_background_tasks_if_tick(void);

// CIRCUITPY_AUTORELOAD_DELAY_MS = 0 will completely disable autoreload.
#ifndef CIRCUITPY_AUTORELOAD_DELAY_MS
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
#define CIRCUITPY_AUTORELOAD_DELAY_MS 750
#endif

#ifndef CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS
Expand Down
1 change: 0 additions & 1 deletion py/py.mk
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,6 @@ PY_CORE_O_BASENAME = $(addprefix py/,\
objzip.o \
opmethods.o \
proto.o \
reload.o \
sequence.o \
stream.o \
binary.o \
Expand Down
32 changes: 0 additions & 32 deletions py/reload.c

This file was deleted.

26 changes: 0 additions & 26 deletions py/reload.h

This file was deleted.

2 changes: 0 additions & 2 deletions shared-bindings/alarm/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
*/

#include "py/obj.h"
#include "py/reload.h"
#include "py/runtime.h"

#include "shared-bindings/alarm/__init__.h"
Expand All @@ -35,7 +34,6 @@
#include "shared-bindings/alarm/touch/TouchAlarm.h"
#include "shared-bindings/supervisor/Runtime.h"
#include "shared-bindings/time/__init__.h"
#include "supervisor/shared/autoreload.h"
#include "supervisor/shared/workflow.h"

//| """Alarms and sleep
Expand Down
12 changes: 8 additions & 4 deletions shared-bindings/supervisor/Runtime.c
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,14 @@ const mp_obj_property_t supervisor_runtime_serial_bytes_available_obj = {
MP_ROM_NONE},
};

supervisor_run_reason_t supervisor_get_run_reason(void) {
return _run_reason;
}

void supervisor_set_run_reason(supervisor_run_reason_t run_reason) {
_run_reason = run_reason;
}

//| run_reason: RunReason
//| """Returns why CircuitPython started running this particular time."""
//|
Expand All @@ -123,10 +131,6 @@ const mp_obj_property_t supervisor_runtime_run_reason_obj = {
MP_ROM_NONE},
};

void supervisor_set_run_reason(supervisor_run_reason_t run_reason) {
_run_reason = run_reason;
}

STATIC const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_usb_connected), MP_ROM_PTR(&supervisor_runtime_usb_connected_obj) },
{ MP_ROM_QSTR(MP_QSTR_serial_connected), MP_ROM_PTR(&supervisor_runtime_serial_connected_obj) },
Expand Down
1 change: 1 addition & 0 deletions shared-bindings/supervisor/Runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@

extern const mp_obj_type_t supervisor_runtime_type;

supervisor_run_reason_t supervisor_get_run_reason(void);
void supervisor_set_run_reason(supervisor_run_reason_t run_reason);

bool common_hal_supervisor_runtime_get_serial_connected(void);
Expand Down
6 changes: 2 additions & 4 deletions shared-bindings/supervisor/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@

#include "py/obj.h"
#include "py/runtime.h"
#include "py/reload.h"
#include "py/objstr.h"

#include "shared/runtime/interrupt_char.h"
#include "supervisor/shared/autoreload.h"
#include "supervisor/shared/bluetooth/bluetooth.h"
#include "supervisor/shared/display.h"
#include "supervisor/shared/status_leds.h"
#include "supervisor/shared/reload.h"
#include "supervisor/shared/stack.h"
#include "supervisor/shared/traceback.h"
#include "supervisor/shared/translate.h"
Expand Down Expand Up @@ -95,8 +94,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_s
//| ...
//|
STATIC mp_obj_t supervisor_reload(void) {
supervisor_set_run_reason(RUN_REASON_SUPERVISOR_RELOAD);
mp_raise_reload_exception();
reload_initiate(RUN_REASON_SUPERVISOR_RELOAD);
return mp_const_none;
}
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_reload_obj, supervisor_reload);
Expand Down
3 changes: 1 addition & 2 deletions shared-module/displayio/__init__.c
Original file line number Diff line number Diff line change
Expand Up @@ -29,16 +29,15 @@
#include "shared-module/displayio/__init__.h"

#include "shared/runtime/interrupt_char.h"
#include "py/reload.h"
#include "py/runtime.h"
#include "shared-bindings/board/__init__.h"
#include "shared-bindings/displayio/Bitmap.h"
#include "shared-bindings/displayio/Display.h"
#include "shared-bindings/displayio/Group.h"
#include "shared-bindings/displayio/Palette.h"
#include "shared-module/displayio/area.h"
#include "supervisor/shared/autoreload.h"
#include "supervisor/shared/display.h"
#include "supervisor/shared/reload.h"
#include "supervisor/memory.h"

#include "supervisor/spi_flash_api.h"
Expand Down
12 changes: 6 additions & 6 deletions supervisor/shared/bluetooth/file_transfer.c
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
#include "common-hal/_bleio/__init__.h"

#include "supervisor/fatfs_port.h"
#include "supervisor/shared/autoreload.h"
#include "supervisor/shared/reload.h"
#include "supervisor/shared/bluetooth/file_transfer.h"
#include "supervisor/shared/bluetooth/file_transfer_protocol.h"
#include "supervisor/shared/tick.h"
Expand Down Expand Up @@ -326,7 +326,7 @@ STATIC uint8_t _process_write(const uint8_t *raw_buf, size_t command_len) {
// Don't reload until everything is written out of the packet buffer.
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
// Trigger an autoreload
autoreload_start_countdown();
autoreload_start();
return ANY_COMMAND;
}

Expand Down Expand Up @@ -384,7 +384,7 @@ STATIC uint8_t _process_write_data(const uint8_t *raw_buf, size_t command_len) {
// Don't reload until everything is written out of the packet buffer.
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
// Trigger an autoreload
autoreload_start_countdown();
autoreload_start();
return ANY_COMMAND;
}
return WRITE_DATA;
Expand Down Expand Up @@ -466,7 +466,7 @@ STATIC uint8_t _process_delete(const uint8_t *raw_buf, size_t command_len) {
// Don't reload until everything is written out of the packet buffer.
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
// Trigger an autoreload
autoreload_start_countdown();
autoreload_start();
}
return ANY_COMMAND;
}
Expand Down Expand Up @@ -521,7 +521,7 @@ STATIC uint8_t _process_mkdir(const uint8_t *raw_buf, size_t command_len) {
// Don't reload until everything is written out of the packet buffer.
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
// Trigger an autoreload
autoreload_start_countdown();
autoreload_start();
}
return ANY_COMMAND;
}
Expand Down Expand Up @@ -669,7 +669,7 @@ STATIC uint8_t _process_move(const uint8_t *raw_buf, size_t command_len) {
// Don't reload until everything is written out of the packet buffer.
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
// Trigger an autoreload
autoreload_start_countdown();
autoreload_start();
}
return ANY_COMMAND;
}
Expand Down
56 changes: 18 additions & 38 deletions supervisor/shared/autoreload.c → supervisor/shared/reload.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,65 +24,49 @@
* THE SOFTWARE.
*/

#include "autoreload.h"
#include "reload.h"

#include "py/mphal.h"
#include "py/reload.h"
#include "py/mpstate.h"
#include "supervisor/shared/reload.h"
#include "supervisor/shared/tick.h"

supervisor_allocation *next_code_allocation;
#include "shared-bindings/supervisor/Runtime.h"

static volatile uint32_t autoreload_countdown_ms = 0;

// True if user has disabled autoreload.
static bool autoreload_enabled = false;

// Non-zero if autoreload is temporarily off, due to an AUTORELOAD_SUSPEND_... reason.
static uint32_t autoreload_suspended = 0;

// True if autoreload has been triggered. Wait for CIRCUITPY_AUTORELOAD_DELAY_MS before doing the
// autoreload, in case further writes arrive.
static bool autoreload_countdown = false;

// True if something has requested a reload/restart.
volatile bool reload_requested = false;

void autoreload_reset() {
if (autoreload_countdown) {
supervisor_disable_tick();
autoreload_countdown = false;
void reload_initiate(supervisor_run_reason_t run_reason) {
reload_requested = true;
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;
}
autoreload_countdown_ms = 0;
reload_requested = false;
#endif
}

inline void autoreload_tick() {
if (!autoreload_countdown) {
return;
}
if (autoreload_countdown_ms > 0) {
autoreload_countdown_ms--;
}
if (autoreload_countdown_ms == 0 && autoreload_enabled &&
autoreload_suspended == 0 && !reload_requested) {
reload_requested = true;
autoreload_countdown = false;
supervisor_disable_tick();
supervisor_set_run_reason(RUN_REASON_AUTO_RELOAD);
mp_raise_reload_exception();
}
void autoreload_reset() {
reload_requested = false;
}

void autoreload_enable() {
autoreload_enabled = true;
reload_requested = false;
autoreload_countdown = false;
}

void autoreload_disable() {
autoreload_enabled = false;
autoreload_countdown = false;
}

void autoreload_suspend(uint32_t suspend_reason_mask) {
Expand All @@ -97,12 +81,8 @@ inline bool autoreload_is_enabled() {
return autoreload_enabled;
}

void autoreload_start_countdown() {
// Avoid multiple tick enables.
if (!autoreload_countdown) {
supervisor_enable_tick();
autoreload_countdown = true;
void autoreload_start() {
if (autoreload_enabled && autoreload_suspended == 0) {
reload_initiate(RUN_REASON_AUTO_RELOAD);
}
// Start or restart the countdown interval.
autoreload_countdown_ms = CIRCUITPY_AUTORELOAD_DELAY_MS;
}
Loading