Skip to content

Commit e4cd969

Browse files
committed
rework auto-reload delay logic
1 parent 0957c15 commit e4cd969

File tree

19 files changed

+71
-138
lines changed

19 files changed

+71
-138
lines changed

main.c

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
#include "supervisor/memory.h"
5353
#include "supervisor/port.h"
5454
#include "supervisor/serial.h"
55-
#include "supervisor/shared/autoreload.h"
55+
#include "supervisor/shared/reload.h"
5656
#include "supervisor/shared/safe_mode.h"
5757
#include "supervisor/shared/stack.h"
5858
#include "supervisor/shared/status_leds.h"
@@ -389,12 +389,28 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
389389

390390
// Print done before resetting everything so that we get the message over
391391
// BLE before it is reset and we have a delay before reconnect.
392-
if (result.return_code == PYEXEC_RELOAD) {
392+
if ((result.return_code & PYEXEC_RELOAD) && supervisor_get_run_reason() == RUN_REASON_AUTO_RELOAD) {
393393
serial_write_compressed(translate("\nCode stopped by auto-reload.\n"));
394+
395+
// Wait for autoreload interval before reloading
396+
uint64_t start_ticks = 0;
397+
do {
398+
// Start waiting, or restart interval if another reload request was initiated
399+
// while we were waiting.
400+
if (reload_requested) {
401+
reload_requested = false;
402+
start_ticks = supervisor_ticks_ms64();
403+
}
404+
RUN_BACKGROUND_TASKS;
405+
} while (supervisor_ticks_ms64() - start_ticks < CIRCUITPY_AUTORELOAD_DELAY_MS);
406+
407+
// Restore request for use below.
408+
reload_requested = true;
394409
} else {
395410
serial_write_compressed(translate("\nCode done running.\n"));
396411
}
397412

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

@@ -474,8 +490,8 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
474490
while (!skip_wait) {
475491
RUN_BACKGROUND_TASKS;
476492

477-
// If a reload was requested by the supervisor or autoreload, return
478-
if (result.return_code & PYEXEC_RELOAD) {
493+
// If a reload was requested by the supervisor or autoreload, return.
494+
if (reload_requested) {
479495
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD;
480496
// Should the STICKY_ON_SUCCESS and STICKY_ON_ERROR bits be cleared in
481497
// next_code_stickiness_situation? I can see arguments either way, but I'm deciding

ports/atmel-samd/mphalport.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "py/smallint.h"
3535
#include "shared-bindings/microcontroller/__init__.h"
3636
#include "shared-bindings/time/__init__.h"
37-
#include "supervisor/shared/autoreload.h"
3837

3938
#include "hal/include/hal_atomic.h"
4039
#include "hal/include/hal_delay.h"

ports/raspberrypi/mphalport.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include "py/smallint.h"
3535
#include "shared-bindings/microcontroller/__init__.h"
3636
#include "shared-bindings/time/__init__.h"
37-
#include "supervisor/shared/autoreload.h"
3837

3938
#include "mpconfigboard.h"
4039
#include "mphalport.h"

py/circuitpy_mpconfig.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ void supervisor_run_background_tasks_if_tick(void);
436436

437437
// CIRCUITPY_AUTORELOAD_DELAY_MS = 0 will completely disable autoreload.
438438
#ifndef CIRCUITPY_AUTORELOAD_DELAY_MS
439-
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
439+
#define CIRCUITPY_AUTORELOAD_DELAY_MS 750
440440
#endif
441441

442442
#ifndef CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS

py/py.mk

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,6 @@ PY_CORE_O_BASENAME = $(addprefix py/,\
159159
objzip.o \
160160
opmethods.o \
161161
proto.o \
162-
reload.o \
163162
sequence.o \
164163
stream.o \
165164
binary.o \

py/reload.c

Lines changed: 0 additions & 32 deletions
This file was deleted.

py/reload.h

Lines changed: 0 additions & 26 deletions
This file was deleted.

shared-bindings/alarm/__init__.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
*/
2626

2727
#include "py/obj.h"
28-
#include "py/reload.h"
2928
#include "py/runtime.h"
3029

3130
#include "shared-bindings/alarm/__init__.h"
@@ -35,7 +34,6 @@
3534
#include "shared-bindings/alarm/touch/TouchAlarm.h"
3635
#include "shared-bindings/supervisor/Runtime.h"
3736
#include "shared-bindings/time/__init__.h"
38-
#include "supervisor/shared/autoreload.h"
3937
#include "supervisor/shared/workflow.h"
4038

4139
//| """Alarms and sleep

shared-bindings/supervisor/Runtime.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,14 @@ const mp_obj_property_t supervisor_runtime_serial_bytes_available_obj = {
108108
MP_ROM_NONE},
109109
};
110110

111+
supervisor_run_reason_t supervisor_get_run_reason(void) {
112+
return _run_reason;
113+
}
114+
115+
void supervisor_set_run_reason(supervisor_run_reason_t run_reason) {
116+
_run_reason = run_reason;
117+
}
118+
111119
//| run_reason: RunReason
112120
//| """Returns why CircuitPython started running this particular time."""
113121
//|
@@ -123,10 +131,6 @@ const mp_obj_property_t supervisor_runtime_run_reason_obj = {
123131
MP_ROM_NONE},
124132
};
125133

126-
void supervisor_set_run_reason(supervisor_run_reason_t run_reason) {
127-
_run_reason = run_reason;
128-
}
129-
130134
STATIC const mp_rom_map_elem_t supervisor_runtime_locals_dict_table[] = {
131135
{ MP_ROM_QSTR(MP_QSTR_usb_connected), MP_ROM_PTR(&supervisor_runtime_usb_connected_obj) },
132136
{ MP_ROM_QSTR(MP_QSTR_serial_connected), MP_ROM_PTR(&supervisor_runtime_serial_connected_obj) },

shared-bindings/supervisor/Runtime.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434

3535
extern const mp_obj_type_t supervisor_runtime_type;
3636

37+
supervisor_run_reason_t supervisor_get_run_reason(void);
3738
void supervisor_set_run_reason(supervisor_run_reason_t run_reason);
3839

3940
bool common_hal_supervisor_runtime_get_serial_connected(void);

0 commit comments

Comments
 (0)