8000 Merge pull request #6142 from dhalbert/7.2.x-autoreload-fake-sleep · adafruit/circuitpython@7cb66dd · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 7cb66dd

Browse files
authored
Merge pull request #6142 from dhalbert/7.2.x-autoreload-fake-sleep
7.2.x autoreload rework (again)
2 parents dbc8a31 + e4cd969 commit 7cb66dd

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);

shared-bindings/supervisor/__init__.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,14 +27,13 @@
2727

2828
#include "py/obj.h"
2929
#include "py/runtime.h"
30-
#include "py/reload.h"
3130
#include "py/objstr.h"
3231

3332
#include "shared/runtime/interrupt_char.h"
34-
#include "supervisor/shared/autoreload.h"
3533
#include "supervisor/shared/bluetooth/bluetooth.h"
3634
#include "supervisor/shared/display.h"
3735
#include "supervisor/shared/status_leds.h"
36+
#include "supervisor/shared/reload.h"
3837
#include "supervisor/shared/stack.h"
3938
#include "supervisor/shared/traceback.h"
4039
#include "supervisor/shared/translate.h"
@@ -95,8 +94,7 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_s
9594
//| ...
9695
//|
9796
STATIC mp_obj_t supervisor_reload(void) {
98-
supervisor_set_run_reason(RUN_REASON_SUPERVISOR_RELOAD);
99-
mp_raise_reload_exception();
97+
reload_initiate(RUN_REASON_SUPERVISOR_RELOAD);
10098
return mp_const_none;
10199
}
102100
MP_DEFINE_CONST_FUN_OBJ_0(supervisor_reload_obj, supervisor_reload);

shared-module/displayio/__init__.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,16 +29,15 @@
2929
#include "shared-module/displayio/__init__.h"
3030

3131
#include "shared/runtime/interrupt_char.h"
32-
#include "py/reload.h"
3332
#include "py/runtime.h"
3433
#include "shared-bindings/board/__init__.h"
3534
#include "shared-bindings/displayio/Bitmap.h"
3635
#include "shared-bindings/displayio/Display.h"
3736
#include "shared-bindings/displayio/Group.h"
3837
#include "shared-bindings/displayio/Palette.h"
3938
#include "shared-module/displayio/area.h"
40-
#include "supervisor/shared/autoreload.h"
4139
#include "supervisor/shared/display.h"
40+
#include "supervisor/shared/reload.h"
4241
#include "supervisor/memory.h"
4342

4443
#include "supervisor/spi_flash_api.h"

supervisor/shared/bluetooth/file_transfer.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@
4343
#include "common-hal/_bleio/__init__.h"
4444

4545
#include "supervisor/fatfs_port.h"
46-
#include "supervisor/shared/autoreload.h"
46+
#include "supervisor/shared/reload.h"
4747
#include "supervisor/shared/bluetooth/file_transfer.h"
4848
#include "supervisor/shared/bluetooth/file_transfer_protocol.h"
4949
#include "supervisor/shared/tick.h"
@@ -326,7 +326,7 @@ STATIC uint8_t _process_write(const uint8_t *raw_buf, size_t command_len) {
326326
// Don't reload until everything is written out of the packet buffer.
327327
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
328328
// Trigger an autoreload
329-
autoreload_start_countdown();
329+
autoreload_start();
330330
return ANY_COMMAND;
331331
}
332332

@@ -384,7 +384,7 @@ STATIC uint8_t _process_write_data(const uint8_t *raw_buf, size_t command_len) {
384384
// Don't reload until everything is written out of the packet buffer.
385385
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
386386
// Trigger an autoreload
387-
autoreload_start_countdown();
387+
autoreload_start();
388388
return ANY_COMMAND;
389389
}
390390
return WRITE_DATA;
@@ -466,7 +466,7 @@ STATIC uint8_t _process_delete(const uint8_t *raw_buf, size_t command_len) {
466466
// Don't reload until everything is written out of the packet buffer.
467467
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
468468
// Trigger an autoreload
469-
autoreload_start_countdown();
469+
autoreload_start();
470470
}
471471
return ANY_COMMAND;
472472
}
@@ -521,7 +521,7 @@ STATIC uint8_t _process_mkdir(const uint8_t *raw_buf, size_t command_len) {
521521
// Don't reload until everything is written out of the packet buffer.
522522
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
523523
// Trigger an autoreload
524-
autoreload_start_countdown();
524+
autoreload_start();
525525
}
526526
return ANY_COMMAND;
527527
}
@@ -669,7 +669,7 @@ STATIC uint8_t _process_move(const uint8_t *raw_buf, size_t command_len) {
669669
// Don't reload until everything is written out of the packet buffer.
670670
common_hal_bleio_packet_buffer_flush(&_transfer_packet_buffer);
671671
// Trigger an autoreload
672-
autoreload_start_countdown();
672+
autoreload_start();
673673
}
674674
return ANY_COMMAND;
675675
}

supervisor/shared/autoreload.c renamed to supervisor/shared/reload.c

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,65 +24,49 @@
2424
* THE SOFTWARE.
2525
*/
2626

27-
#include "autoreload.h"
27+
#include "reload.h"
2828

2929
#include "py/mphal.h"
30-
#include "py/reload.h"
30+
#include "py/mpstate.h"
31+
#include "supervisor/shared/reload.h"
3132
#include "supervisor/shared/tick.h"
3233

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

36-
static volatile uint32_t autoreload_countdown_ms = 0;
37-
3837
// True if user has disabled autoreload.
3938
static bool autoreload_enabled = false;
4039

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

44-
// True if autoreload has been triggered. Wait for CIRCUITPY_AUTORELOAD_DELAY_MS before doing the
45-
// autoreload, in case further writes arrive.
46-
static bool autoreload_countdown = false;
47-
4843
// True if something has requested a reload/restart.
4944
volatile bool reload_requested = false;
5045

51-
void autoreload_reset() {
52-
if (autoreload_countdown) {
53-
supervisor_disable_tick();
54-
autoreload_countdown = false;
46+
void reload_initiate(supervisor_run_reason_t run_reason) {
47+
reload_requested = true;
48+
supervisor_set_run_reason(run_reason);
49+
50+
// Raise reload exception, in case code is running.
51+
MP_STATE_THREAD(mp_pending_exception) = MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception));
52+
#if MICROPY_ENABLE_SCHEDULER
53+
if (MP_STATE_VM(sched_state) == MP_SCHED_IDLE) {
54+
MP_STATE_VM(sched_state) = MP_SCHED_PENDING;
5555
}
56-
autoreload_countdown_ms = 0;
57-
reload_requested = false;
56+
#endif
5857
}
5958

60-
inline void autoreload_tick() {
61-
if (!autoreload_countdown) {
62-
return;
63-
}
64-
if (autoreload_countdown_ms > 0) {
65-
autoreload_countdown_ms--;
66-
}
67-
if (autoreload_countdown_ms == 0 && autoreload_enabled &&
68-
autoreload_suspended == 0 && !reload_requested) {
69-
reload_requested = true;
70-
autoreload_countdown = false;
71-
supervisor_disable_tick();
72-
supervisor_set_run_reason(RUN_REASON_AUTO_RELOAD);
73-
mp_raise_reload_exception();
74-
}
59+
void autoreload_reset() {
60+
reload_requested = false;
7561
}
7662

7763
void autoreload_enable() {
7864
autoreload_enabled = true;
7965
reload_requested = false;
80-
autoreload_countdown = false;
8166
}
8267

8368
void autoreload_disable() {
8469
autoreload_enabled = false;
85-
autoreload_countdown = false;
8670
}
8771

8872
void autoreload_suspend(uint32_t suspend_reason_mask) {
@@ -97,12 +81,8 @@ inline bool autoreload_is_enabled() {
9781
return autoreload_enabled;
9882
}
9983

100-
void autoreload_start_countdown() {
101-
// Avoid multiple tick enables.
102-
if (!autoreload_countdown) {
103-
supervisor_enable_tick();
104-
autoreload_countdown = true;
84+
void autoreload_start() {
85+
if (autoreload_enabled && autoreload_suspended == 0) {
86+
reload_initiate(RUN_REASON_AUTO_RELOAD);
10587
}
106-
// Start or restart the countdown interval.
107-
autoreload_countdown_ms = CIRCUITPY_AUTORELOAD_DELAY_MS;
10888
}

0 commit comments

Comments
 (0)
0