Skip to content

Commit 9b2e22a

Browse files
committed
Make autoreload checking more robust
- Add reset for autoreload. De-request ticks. - Separate state a little more in autoreload.c - Rename some routines. - Remove redundant settings of CIRCUITPY_AUTORELOAD_DELAY_MS.
1 parent aeeb58f commit 9b2e22a

File tree

21 files changed

+97
-98
lines changed

21 files changed

+97
-98
lines changed

main.c

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ static void reset_devices(void) {
124124
}
125125

126126
STATIC void start_mp(supervisor_allocation *heap, bool first_run) {
127-
autoreload_stop();
127+
autoreload_reset();
128128
supervisor_workflow_reset();
129129

130130
// Stack limit should be less than real stack size, so we have a chance
@@ -329,7 +329,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
329329
result.exception = MP_OBJ_NULL;
330330
result.exception_line = 0;
331331

332-
bool skip_repl;
332+
bool skip_repl = false;
333333
bool skip_wait = false;
334334
bool found_main = false;
335335
uint8_t next_code_options = 0;
@@ -389,13 +389,13 @@ 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 (reload_requested && result.return_code == PYEXEC_EXCEPTION) {
392+
if (result.return_code == PYEXEC_RELOAD) {
393393
serial_write_compressed(translate("\nCode stopped by auto-reload.\n"));
394394
} else {
395395
serial_write_compressed(translate("\nCode done running.\n"));
396396
}
397397

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

401401
// If a new next code file was set, that is a reason to keep it (obviously). Stuff this into
@@ -407,8 +407,10 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
407407
next_code_options |= SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
408408
}
409409

410-
if (reload_requested) {
410+
if (result.return_code & PYEXEC_RELOAD) {
411411
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD;
412+
skip_repl = true;
413+
skip_wait = true;
412414
} else if (result.return_code == 0) {
413415
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_SUCCESS;
414416
if (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS) {
@@ -426,7 +428,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
426428
}
427429
}
428430
if (result.return_code & PYEXEC_FORCED_EXIT) {
429-
skip_repl = reload_requested;
431+
skip_repl = false;
430432
skip_wait = true;
431433
}
432434
}
@@ -473,7 +475,7 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
473475
RUN_BACKGROUND_TASKS;
474476

475477
// If a reload was requested by the supervisor or autoreload, return
476-
if (reload_requested) {
478+
if (result.return_code & PYEXEC_RELOAD) {
477479
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD;
478480
// Should the STICKY_ON_SUCCESS and STICKY_ON_ERROR bits be cleared in
479481
// next_code_stickiness_situation? I can see arguments either way, but I'm deciding
@@ -627,13 +629,14 @@ STATIC bool run_code_py(safe_mode_t safe_mode, bool first_run, bool *simulate_re
627629
}
628630
}
629631

632+
// Done waiting, start the board back up.
633+
630634
// free code allocation if unused
631635
if ((next_code_options & next_code_stickiness_situation) == 0) {
632636
free_memory(next_code_allocation);
633637
next_code_allocation = NULL;
634638
}
635639

636-
// Done waiting, start the board back up.
637640
#if CIRCUITPY_STATUS_LED
638641
if (led_active) {
639642
new_status_color(BLACK);
@@ -757,7 +760,7 @@ STATIC int run_repl(bool first_run) {
757760
usb_setup_with_vm();
758761
#endif
759762

760-
autoreload_suspend(AUTORELOAD_LOCK_REPL);
763+
autoreload_suspend(AUTORELOAD_SUSPEND_REPL);
761764

762765
// Set the status LED to the REPL color before running the REPL. For
763766
// NeoPixels and DotStars this will be sticky but for PWM or single LED it
@@ -787,7 +790,7 @@ STATIC int run_repl(bool first_run) {
787790
status_led_deinit();
788791
#endif
789792

790-
autoreload_resume(AUTORELOAD_LOCK_REPL);
793+
autoreload_resume(AUTORELOAD_SUSPEND_REPL);
791794
return exit_code;
792795
}
793796

ports/atmel-samd/supervisor/port.c

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,9 @@
129129
#include "common-hal/_pew/PewPew.h"
130130
#endif
131131
static volatile bool sleep_ok = true;
132+
132133
#ifdef SAMD21
133-
static uint8_t _tick_event_channel = 0;
134+
uint8_t _tick_event_channel;
134135

135136
// Sleeping requires a register write that can stall interrupt handling. Turning
136137
// off sleeps allows for more accurate interrupt timing. (Python still thinks
@@ -142,7 +143,13 @@ void rtc_start_pulse(void) {
142143
void rtc_end_pulse(void) {
143144
sleep_ok = true;
144145
}
145-
#endif
146+
#endif // SAMD21
147+
148+
static void reset_ticks(void) {
149+
#ifdef SAMD21
150+
_tick_event_channel = EVSYS_SYNCH_NUM;
151+
#endif
152+
}
146153

147154
extern volatile bool mp_msc_enabled;
148155

@@ -426,9 +433,7 @@ void reset_port(void) {
426433
#endif
427434

428435
reset_event_system();
429-
#ifdef SAMD21
430-
_tick_event_channel = EVSYS_SYNCH_NUM;
431-
#endif
436+
reset_ticks();
432437

433438
reset_all_pins();
434439

@@ -498,7 +503,7 @@ uint32_t port_get_saved_word(void) {
498503
static volatile uint64_t overflowed_ticks = 0;
499504

500505
static uint32_t _get_count(uint64_t *overflow_count) {
501-
while(1) {
506+
while (1) {
502507
// Disable interrupts so we can grab the count and the overflow atomically.
503508
common_hal_mcu_disable_interrupts();
504509

@@ -521,7 +526,7 @@ static uint32_t _get_count(uint64_t *overflow_count) {
521526
return count;
522527
}
523528

524-
// Try again if overflow hasn't been processed yet.
529+
// Try again if overflow hasn't been processed yet.
525530
}
526531
}
527532

@@ -620,7 +625,7 @@ void port_enable_tick(void) {
620625
RTC->MODE0.INTENSET.reg = RTC_MODE0_INTENSET_PER2;
621626
#endif
622627
#ifdef SAMD21
623-
// SAMD21 ticks won't survive port_reset(). This *should* be ok since it'll
628+
// SAMD21 ticks won't survive reset_port(). This *should* be ok since it'll
624629
// be triggered by ticks and no Python will be running.
625630
if (_tick_event_channel >= EVSYS_SYNCH_NUM) {
626631
turn_on_event_system();
@@ -653,6 +658,7 @@ void port_disable_tick(void) {
653658
uint8_t value = 1 << _tick_event_channel;
654659
EVSYS->INTENCLR.reg = EVSYS_INTENSET_EVD(value);
655660
}
661+
_tick_event_channel = EVSYS_SYNCH_NUM;
656662
#endif
657663
}
658664

ports/nrf/boards/aramcon2_badge/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,8 +54,6 @@
5454

5555
#define BOARD_USER_SAFE_MODE_ACTION translate("pressing the left button at start up\n")
5656

57-
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
58-
5957
#define CIRCUITPY_INTERNAL_NVM_SIZE (4096)
6058

6159
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)

ports/nrf/boards/arduino_nano_33_ble/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#define MICROPY_HW_BOARD_NAME "Arduino Nano 33 BLE"
44
#define MICROPY_HW_MCU_NAME "nRF52840"
55

6-
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
7-
86
#define DEFAULT_I2C_BUS_SCL (&pin_P0_02)
97
#define DEFAULT_I2C_BUS_SDA (&pin_P0_31)
108

ports/nrf/boards/bastble/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#define MICROPY_HW_BOARD_NAME "BastBLE"
44
#define MICROPY_HW_MCU_NAME "nRF52840"
55

6-
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
7-
86
#if QSPI_FLASH_FILESYSTEM
97
#define MICROPY_QSPI_DATA0 NRF_GPIO_PIN_MAP(0, 30)
108
#define MICROPY_QSPI_DATA1 NRF_GPIO_PIN_MAP(0, 29)

ports/nrf/boards/feather_bluefruit_sense/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,6 @@
5353
#define SPI_FLASH_CS_PIN &pin_P0_20
5454
#endif
5555

56-
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
57-
5856
#define CIRCUITPY_INTERNAL_NVM_SIZE (4096)
5957

6058
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)

ports/nrf/boards/ikigaisense_vita/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#define MICROPY_HW_LED_STATUS (&pin_P0_27)
77

8-
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
9-
108
#define CIRCUITPY_INTERNAL_NVM_SIZE (4096)
119

1210
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)

ports/nrf/boards/itsybitsy_nrf52840_express/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
#define SPI_FLASH_CS_PIN &pin_P0_23
2525
#endif
2626

27-
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
28-
2927
#define CIRCUITPY_INTERNAL_NVM_SIZE (4096)
3028

3129
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)

ports/nrf/boards/warmbit_bluepixel/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66
#define MICROPY_HW_LED_STATUS (&pin_P0_12)
77

8-
#define CIRCUITPY_AUTORELOAD_DELAY_MS 500
9-
108
#define CIRCUITPY_INTERNAL_NVM_SIZE (4096)
119

1210
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x4000 - CIRCUITPY_INTERNAL_NVM_SIZE)

ports/stm/boards/pyb_nano_v2/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,6 @@
4242
#define SPI_FLASH_SCK_PIN (&pin_PB13)
4343
#define SPI_FLASH_CS_PIN (&pin_PB12)
4444

45-
#define CIRCUITPY_AUTORELOAD_DELAY_MS (500)
46-
4745
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x2000 - 0xC000)
4846

4947
#define AUTORESET_DELAY_MS (500)

ports/stm/boards/stm32f411ce_blackpill/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,8 +45,6 @@
4545
#define DEFAULT_I2C_BUS_SCL (&pin_PB06)
4646
#define DEFAULT_I2C_BUS_SDA (&pin_PB07)
4747

48-
#define CIRCUITPY_AUTORELOAD_DELAY_MS (500)
49-
5048
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x2000 - 0xC000)
5149

5250
#define AUTORESET_DELAY_MS (500)

ports/stm/boards/stm32f411ce_blackpill_with_flash/mpconfigboard.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
/*
1+
`/*
22
* This file is part of the MicroPython project, http://micropython.org/
33
*
44
* The MIT License (MIT)
@@ -49,6 +49,4 @@
4949

5050
#define BOARD_FLASH_SIZE (FLASH_SIZE - 0x2000 - 0xC000)
5151

52-
#define AUTORESET_DELAY_MS (500)
53-
5452
#define MICROPY_FATFS_EXFAT 0

shared-bindings/supervisor/__init__.c

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ MP_DEFINE_CONST_FUN_OBJ_1(supervisor_set_rgb_status_brightness_obj, supervisor_s
9595
//| ...
9696
//|
9797
STATIC mp_obj_t supervisor_reload(void) {
98-
reload_requested = true;
9998
supervisor_set_run_reason(RUN_REASON_SUPERVISOR_RELOAD);
10099
mp_raise_reload_exception();
101100
return mp_const_none;

shared/runtime/pyexec.c

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -174,12 +174,13 @@ STATIC int parse_compile_execute(const void *source, mp_parse_input_kind_t input
174174
} else if (mp_obj_is_subclass_fast(MP_OBJ_FROM_PTR(mp_obj_get_type((mp_obj_t)nlr.ret_val)), &mp_type_DeepSleepRequest)) {
175175
ret = PYEXEC_DEEP_SLEEP;
176176
#endif
177+
} else if ((mp_obj_t)nlr.ret_val == MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
178+
ret = PYEXEC_RELOAD;
177179
} else {
178-
if ((mp_obj_t)nlr.ret_val != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
179-
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
180-
}
180+
mp_obj_print_exception(&mp_plat_print, MP_OBJ_FROM_PTR(nlr.ret_val));
181181
ret = PYEXEC_EXCEPTION;
182182
}
183+
183184
}
184185
if (result != NULL) {
185186
result->return_code = ret;

shared/runtime/pyexec.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ extern int pyexec_system_exit;
4949
#define PYEXEC_FORCED_EXIT (0x100)
5050
#define PYEXEC_EXCEPTION (0x200)
5151
#define PYEXEC_DEEP_SLEEP (0x400)
52+
#define PYEXEC_RELOAD (0x800)
5253

5354
int pyexec_raw_repl(void);
5455
int pyexec_friendly_repl(void);

0 commit comments

Comments
 (0)