2017-09-22 21:05:51 -04:00
|
|
|
/*
|
|
|
|
* This file is part of the MicroPython project, http://micropython.org/
|
|
|
|
*
|
|
|
|
* The MIT License (MIT)
|
|
|
|
*
|
|
|
|
* Copyright (c) 2016-2017 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "extmod/vfs.h"
|
|
|
|
#include "extmod/vfs_fat.h"
|
|
|
|
|
2018-05-03 23:43:02 -04:00
|
|
|
#include "genhdr/mpversion.h"
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "py/nlr.h"
|
|
|
|
#include "py/compile.h"
|
|
|
|
#include "py/frozenmod.h"
|
|
|
|
#include "py/mphal.h"
|
|
|
|
#include "py/runtime.h"
|
|
|
|
#include "py/repl.h"
|
|
|
|
#include "py/gc.h"
|
|
|
|
#include "py/stackctrl.h"
|
|
|
|
|
|
|
|
#include "lib/mp-readline/readline.h"
|
|
|
|
#include "lib/utils/pyexec.h"
|
|
|
|
|
2019-04-09 20:23:01 -04:00
|
|
|
#include "background.h"
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "mpconfigboard.h"
|
2020-05-11 09:37:20 -04:00
|
|
|
#include "supervisor/background_callback.h"
|
2020-12-02 21:05:29 -05:00
|
|
|
#include "supervisor/board.h"
|
2018-06-28 13:36:28 -04:00
|
|
|
#include "supervisor/cpu.h"
|
2020-11-23 22:44:53 -05:00
|
|
|
#include "supervisor/filesystem.h"
|
2018-05-24 16:12:18 -04:00
|
|
|
#include "supervisor/memory.h"
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "supervisor/port.h"
|
2020-11-23 22:44:53 -05:00
|
|
|
#include "supervisor/serial.h"
|
2017-09-22 21:05:51 -04:00
|
|
|
#include "supervisor/shared/autoreload.h"
|
2018-12-06 17:24:20 -05:00
|
|
|
#include "supervisor/shared/safe_mode.h"
|
2018-07-25 08:06:30 -04:00
|
|
|
#include "supervisor/shared/stack.h"
|
2020-11-23 22:44:53 -05:00
|
|
|
#include "supervisor/shared/status_leds.h"
|
2020-09-27 06:58:51 -04:00
|
|
|
#include "supervisor/shared/traceback.h"
|
2020-11-23 22:44:53 -05:00
|
|
|
#include "supervisor/shared/translate.h"
|
2020-11-19 17:47:12 -05:00
|
|
|
#include "supervisor/shared/workflow.h"
|
2020-09-30 01:45:02 -04:00
|
|
|
#include "supervisor/usb.h"
|
2021-04-16 10:59:11 -04:00
|
|
|
#include "supervisor/workflow.h"
|
2020-09-30 01:45:02 -04:00
|
|
|
|
|
|
|
#include "shared-bindings/microcontroller/__init__.h"
|
2020-11-22 19:10:09 -05:00
|
|
|
#include "shared-bindings/microcontroller/Processor.h"
|
|
|
|
#include "shared-bindings/supervisor/Runtime.h"
|
2017-09-22 21:05:51 -04:00
|
|
|
|
2020-11-19 15:43:39 -05:00
|
|
|
#if CIRCUITPY_ALARM
|
|
|
|
#include "shared-bindings/alarm/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
2021-07-29 23:06:24 -04:00
|
|
|
#if CIRCUITPY_ATEXIT
|
|
|
|
#include "shared-module/atexit/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
2021-02-15 20:06:18 -05:00
|
|
|
#if CIRCUITPY_BLEIO
|
|
|
|
#include "shared-bindings/_bleio/__init__.h"
|
2021-06-25 17:58:32 -04:00
|
|
|
#include "supervisor/shared/bluetooth/bluetooth.h"
|
2021-02-15 20:06:18 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CIRCUITPY_BOARD
|
|
|
|
#include "shared-module/board/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CIRCUITPY_CANIO
|
|
|
|
#include "common-hal/canio/CAN.h"
|
|
|
|
#endif
|
|
|
|
|
2019-07-22 12:58:28 -04:00
|
|
|
#if CIRCUITPY_DISPLAYIO
|
|
|
|
#include "shared-module/displayio/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
2021-06-14 20:54:43 -04:00
|
|
|
#if CIRCUITPY_KEYPAD
|
|
|
|
#include "shared-module/keypad/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
2020-07-16 22:01:43 -04:00
|
|
|
#if CIRCUITPY_MEMORYMONITOR
|
|
|
|
#include "shared-module/memorymonitor/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
2021-04-27 14:37:36 -04:00
|
|
|
#if CIRCUITPY_USB_HID
|
|
|
|
#include "shared-module/usb_hid/__init__.h"
|
2020-08-20 12:08:00 -04:00
|
|
|
#endif
|
|
|
|
|
2020-12-07 11:06:33 -05:00
|
|
|
#if CIRCUITPY_WIFI
|
|
|
|
#include "shared-bindings/wifi/__init__.h"
|
|
|
|
#endif
|
|
|
|
|
2020-07-16 22:02:26 -04:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
2020-07-20 21:16:20 -04:00
|
|
|
static size_t PLACE_IN_DTCM_BSS(_pystack[CIRCUITPY_PYSTACK_SIZE / sizeof(size_t)]);
|
2020-07-16 22:02:26 -04:00
|
|
|
#endif
|
|
|
|
|
2020-08-04 18:24:37 -04:00
|
|
|
static void reset_devices(void) {
|
|
|
|
#if CIRCUITPY_BLEIO_HCI
|
|
|
|
bleio_reset();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2020-11-23 22:44:53 -05:00
|
|
|
STATIC void start_mp(supervisor_allocation* heap) {
|
2017-09-22 21:05:51 -04:00
|
|
|
autoreload_stop();
|
2020-11-23 22:44:53 -05:00
|
|
|
supervisor_workflow_reset();
|
2017-09-22 21:05:51 -04:00
|
|
|
|
2018-05-24 16:12:18 -04:00
|
|
|
// Stack limit should be less than real stack size, so we have a chance
|
|
|
|
// to recover from limit hit. (Limit is measured in bytes.)
|
|
|
|
mp_stack_ctrl_init();
|
2020-04-17 19:23:28 -04:00
|
|
|
|
2020-10-11 08:59:33 -04:00
|
|
|
if (stack_get_bottom() != NULL) {
|
|
|
|
mp_stack_set_limit(stack_get_length() - 1024);
|
2020-04-17 19:23:28 -04:00
|
|
|
}
|
|
|
|
|
2018-05-24 16:12:18 -04:00
|
|
|
|
|
|
|
#if MICROPY_MAX_STACK_USAGE
|
|
|
|
// _ezero (same as _ebss) is an int, so start 4 bytes above it.
|
2020-10-11 08:59:33 -04:00
|
|
|
if (stack_get_bottom() != NULL) {
|
|
|
|
mp_stack_set_bottom(stack_get_bottom());
|
2020-04-17 19:23:28 -04:00
|
|
|
mp_stack_fill_with_sentinel();
|
|
|
|
}
|
2018-05-24 16:12:18 -04:00
|
|
|
#endif
|
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
// Sync the file systems in case any used RAM from the GC to cache. As soon
|
|
|
|
// as we re-init the GC all bets are off on the cache.
|
|
|
|
filesystem_flush();
|
|
|
|
|
|
|
|
// Clear the readline history. It references the heap we're about to destroy.
|
|
|
|
readline_init0();
|
|
|
|
|
2020-07-16 22:02:26 -04:00
|
|
|
#if MICROPY_ENABLE_PYSTACK
|
2020-07-20 21:16:20 -04:00
|
|
|
mp_pystack_init(_pystack, _pystack + (sizeof(_pystack) / sizeof(size_t)));
|
2020-07-16 22:02:26 -04:00
|
|
|
#endif
|
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
#if MICROPY_ENABLE_GC
|
2020-10-11 08:59:33 -04:00
|
|
|
gc_init(heap->ptr, heap->ptr + get_allocation_length(heap) / 4);
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
|
|
|
mp_init();
|
|
|
|
mp_obj_list_init(mp_sys_path, 0);
|
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
|
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_));
|
|
|
|
// Frozen modules are in their own pseudo-dir, e.g., ".frozen".
|
2018-11-01 17:14:11 -04:00
|
|
|
// Prioritize .frozen over /lib.
|
2017-09-22 21:05:51 -04:00
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_FROZEN_FAKE_DIR_QSTR));
|
2018-11-01 17:14:11 -04:00
|
|
|
mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR__slash_lib));
|
2017-09-22 21:05:51 -04:00
|
|
|
|
|
|
|
mp_obj_list_init(mp_sys_argv, 0);
|
2018-10-04 08:31:47 -04:00
|
|
|
|
2020-12-11 08:10:26 -05:00
|
|
|
#if CIRCUITPY_ALARM
|
|
|
|
// Record which alarm woke us up, if any. An object may be created so the heap must be functional.
|
2021-04-13 18:31:57 -04:00
|
|
|
shared_alarm_save_wake_alarm(common_hal_alarm_create_wake_alarm());
|
2020-12-11 08:10:26 -05:00
|
|
|
// Reset alarm module only after we retrieved the wakeup alarm.
|
2021-04-02 14:08:48 -04:00
|
|
|
alarm_reset();
|
2020-12-11 08:10:26 -05:00
|
|
|
#endif
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
2018-05-24 16:12:18 -04:00
|
|
|
|
2020-11-23 22:44:53 -05:00
|
|
|
STATIC void stop_mp(void) {
|
2019-02-14 15:54:34 -05:00
|
|
|
#if MICROPY_VFS
|
|
|
|
mp_vfs_mount_t *vfs = MP_STATE_VM(vfs_mount_table);
|
|
|
|
|
|
|
|
// Unmount all heap allocated vfs mounts.
|
|
|
|
while (gc_nbytes(vfs) > 0) {
|
|
|
|
vfs = vfs->next;
|
|
|
|
}
|
|
|
|
MP_STATE_VM(vfs_mount_table) = vfs;
|
|
|
|
MP_STATE_VM(vfs_cur) = vfs;
|
|
|
|
#endif
|
|
|
|
|
2020-05-11 09:37:20 -04:00
|
|
|
background_callback_reset();
|
2021-04-16 10:59:11 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_USB
|
2021-02-04 22:15:32 -05:00
|
|
|
usb_background();
|
2021-04-16 10:59:11 -04:00
|
|
|
#endif
|
2020-05-11 09:37:20 -04:00
|
|
|
|
2019-03-08 18:29:14 -05:00
|
|
|
gc_deinit();
|
2018-05-24 16:12:18 -04:00
|
|
|
}
|
|
|
|
|
2017-11-08 02:00:44 -05:00
|
|
|
#define STRING_LIST(...) {__VA_ARGS__, ""}
|
2017-09-22 21:05:51 -04:00
|
|
|
|
2018-05-03 23:43:02 -04:00
|
|
|
// Look for the first file that exists in the list of filenames, using mp_import_stat().
|
|
|
|
// Return its index. If no file found, return -1.
|
2020-11-23 22:44:53 -05:00
|
|
|
STATIC const char* first_existing_file_in_list(const char * const * filenames) {
|
2017-11-08 02:00:44 -05:00
|
|
|
for (int i = 0; filenames[i] != (char*)""; i++) {
|
|
|
|
mp_import_stat_t stat = mp_import_stat(filenames[i]);
|
2018-05-03 23:43:02 -04:00
|
|
|
if (stat == MP_IMPORT_STAT_FILE) {
|
2018-05-04 08:28:05 -04:00
|
|
|
return filenames[i];
|
2017-11-08 02:00:44 -05:00
|
|
|
}
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
2018-05-04 08:28:05 -04:00
|
|
|
return NULL;
|
2018-05-03 23:43:02 -04:00
|
|
|
}
|
|
|
|
|
2020-11-23 22:44:53 -05:00
|
|
|
STATIC bool maybe_run_list(const char * const * filenames, pyexec_result_t* exec_result) {
|
2018-05-04 08:28:05 -04:00
|
|
|
const char* filename = first_existing_file_in_list(filenames);
|
|
|
|
if (filename == NULL) {
|
2018-05-03 23:43:02 -04:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-04 08:28:05 -04:00
|
|
|
mp_hal_stdout_tx_str(filename);
|
2021-08-23 21:00:34 -04:00
|
|
|
serial_write_compressed(translate(" output:\n"));
|
2018-05-04 08:28:05 -04:00
|
|
|
pyexec_file(filename, exec_result);
|
2021-08-01 04:31:40 -04:00
|
|
|
#if CIRCUITPY_ATEXIT
|
2021-08-16 12:52:22 -04:00
|
|
|
shared_module_atexit_execute(exec_result);
|
2021-08-01 04:31:40 -04:00
|
|
|
#endif
|
2018-05-03 23:43:02 -04:00
|
|
|
return true;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
|
|
|
|
2020-09-27 06:58:51 -04:00
|
|
|
STATIC void count_strn(void *data, const char *str, size_t len) {
|
|
|
|
*(size_t*)data += len;
|
|
|
|
}
|
|
|
|
|
|
|
|
STATIC void cleanup_after_vm(supervisor_allocation* heap, mp_obj_t exception) {
|
|
|
|
// Get the traceback of any exception from this run off the heap.
|
|
|
|
// MP_OBJ_SENTINEL means "this run does not contribute to traceback storage, don't touch it"
|
|
|
|
// MP_OBJ_NULL (=0) means "this run completed successfully, clear any stored traceback"
|
|
|
|
if (exception != MP_OBJ_SENTINEL) {
|
|
|
|
free_memory(prev_traceback_allocation);
|
|
|
|
// ReloadException is exempt from traceback printing in pyexec_file(), so treat it as "no
|
|
|
|
// traceback" here too.
|
|
|
|
if (exception && exception != MP_OBJ_FROM_PTR(&MP_STATE_VM(mp_reload_exception))) {
|
|
|
|
size_t traceback_len = 0;
|
|
|
|
mp_print_t print_count = {&traceback_len, count_strn};
|
|
|
|
mp_obj_print_exception(&print_count, exception);
|
|
|
|
prev_traceback_allocation = allocate_memory(align32_size(traceback_len + 1), false, true);
|
|
|
|
// Empirically, this never fails in practice - even when the heap is totally filled up
|
|
|
|
// with single-block-sized objects referenced by a root pointer, exiting the VM frees
|
|
|
|
// up several hundred bytes, sufficient for the traceback (which tends to be shortened
|
|
|
|
// because there wasn't memory for the full one). There may be convoluted ways of
|
|
|
|
// making it fail, but at this point I believe they are not worth spending code on.
|
|
|
|
if (prev_traceback_allocation != NULL) {
|
|
|
|
vstr_t vstr;
|
|
|
|
vstr_init_fixed_buf(&vstr, traceback_len, (char*)prev_traceback_allocation->ptr);
|
|
|
|
mp_print_t print = {&vstr, (mp_print_strn_t)vstr_add_strn};
|
|
|
|
mp_obj_print_exception(&print, exception);
|
|
|
|
((char*)prev_traceback_allocation->ptr)[traceback_len] = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
prev_traceback_allocation = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-13 17:47:35 -04:00
|
|
|
// Reset port-independent devices, like CIRCUITPY_BLEIO_HCI.
|
|
|
|
reset_devices();
|
2021-07-29 23:06:24 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_ATEXIT
|
|
|
|
atexit_reset();
|
|
|
|
#endif
|
|
|
|
|
2020-12-09 10:52:18 -05:00
|
|
|
// Turn off the display and flush the filesystem before the heap disappears.
|
2019-04-17 17:52:00 -04:00
|
|
|
#if CIRCUITPY_DISPLAYIO
|
|
|
|
reset_displays();
|
|
|
|
#endif
|
2021-06-14 20:54:43 -04:00
|
|
|
|
2020-07-16 22:01:43 -04:00
|
|
|
#if CIRCUITPY_MEMORYMONITOR
|
|
|
|
memorymonitor_reset();
|
|
|
|
#endif
|
2021-06-14 20:54:43 -04:00
|
|
|
|
2019-04-17 17:52:00 -04:00
|
|
|
filesystem_flush();
|
|
|
|
stop_mp();
|
|
|
|
free_memory(heap);
|
|
|
|
supervisor_move_memory();
|
|
|
|
|
2020-09-16 13:18:55 -04:00
|
|
|
#if CIRCUITPY_CANIO
|
2020-08-20 12:08:00 -04:00
|
|
|
common_hal_canio_reset();
|
|
|
|
#endif
|
|
|
|
|
2021-06-14 20:54:43 -04:00
|
|
|
#if CIRCUITPY_KEYPAD
|
|
|
|
keypad_reset();
|
|
|
|
#endif
|
|
|
|
|
2020-10-24 11:23:17 -04:00
|
|
|
// reset_board_busses() first because it may release pins from the never_reset state, so that
|
|
|
|
// reset_port() can reset them.
|
2019-07-24 14:21:27 -04:00
|
|
|
#if CIRCUITPY_BOARD
|
2019-07-22 12:58:28 -04:00
|
|
|
reset_board_busses();
|
2019-07-24 14:21:27 -04:00
|
|
|
#endif
|
2020-10-24 11:23:17 -04:00
|
|
|
reset_port();
|
2019-04-17 17:52:00 -04:00
|
|
|
reset_board();
|
|
|
|
}
|
|
|
|
|
2020-11-23 22:44:53 -05:00
|
|
|
STATIC void print_code_py_status_message(safe_mode_t safe_mode) {
|
2020-10-07 14:23:08 -04:00
|
|
|
if (autoreload_is_enabled()) {
|
|
|
|
serial_write_compressed(translate("Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.\n"));
|
|
|
|
} else {
|
|
|
|
serial_write_compressed(translate("Auto-reload is off.\n"));
|
|
|
|
}
|
|
|
|
if (safe_mode != NO_SAFE_MODE) {
|
2021-05-12 15:53:52 -04:00
|
|
|
serial_write_compressed(translate("Running in safe mode! Not running saved code.\n"));
|
2020-10-07 14:23:08 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 22:44:53 -05:00
|
|
|
STATIC bool run_code_py(safe_mode_t safe_mode) {
|
2017-09-22 21:05:51 -04:00
|
|
|
bool serial_connected_at_start = serial_connected();
|
2021-05-12 15:53:52 -04:00
|
|
|
bool printed_safe_mode_message = false;
|
2019-10-20 23:50:12 -04:00
|
|
|
#if CIRCUITPY_AUTORELOAD_DELAY_MS > 0
|
2021-05-12 15:53:52 -04:00
|
|
|
if (serial_connected_at_start) {
|
|
|
|
serial_write("\r\n");
|
|
|
|
print_code_py_status_message(safe_mode);
|
|
|
|
print_safe_mode_message(safe_mode);
|
|
|
|
printed_safe_mode_message = true;
|
|
|
|
}
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
pyexec_result_t result;
|
2017-11-22 21:56:28 -05:00
|
|
|
|
|
|
|
result.return_code = 0;
|
2020-09-27 06:58:51 -04:00
|
|
|
result.exception = MP_OBJ_NULL;
|
2017-11-22 21:56:28 -05:00
|
|
|
result.exception_line = 0;
|
|
|
|
|
2020-10-29 11:39:06 -04:00
|
|
|
bool skip_repl;
|
2021-06-20 16:57:20 -04:00
|
|
|
bool skip_wait = false;
|
2017-09-22 21:05:51 -04:00
|
|
|
bool found_main = false;
|
2020-10-29 11:39:06 -04:00
|
|
|
uint8_t next_code_options = 0;
|
|
|
|
// Collects stickiness bits that apply in the current situation.
|
|
|
|
uint8_t next_code_stickiness_situation = SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
|
2017-10-26 22:53:28 -04:00
|
|
|
|
2020-10-07 14:23:08 -04:00
|
|
|
if (safe_mode == NO_SAFE_MODE) {
|
2020-11-26 22:06:37 -05:00
|
|
|
static const char * const supported_filenames[] = STRING_LIST(
|
|
|
|
"code.txt", "code.py", "main.py", "main.txt");
|
2020-08-04 13:39:55 -04:00
|
|
|
#if CIRCUITPY_FULL_BUILD
|
2020-11-26 22:06:37 -05:00
|
|
|
static const char * const double_extension_filenames[] = STRING_LIST(
|
|
|
|
"code.txt.py", "code.py.txt", "code.txt.txt","code.py.py",
|
|
|
|
"main.txt.py", "main.py.txt", "main.txt.txt","main.py.py");
|
2020-08-04 13:39:55 -04:00
|
|
|
#endif
|
2018-05-24 16:12:18 -04:00
|
|
|
|
2018-07-25 08:06:30 -04:00
|
|
|
stack_resize();
|
|
|
|
filesystem_flush();
|
2018-05-24 16:12:18 -04:00
|
|
|
supervisor_allocation* heap = allocate_remaining_memory();
|
2021-04-02 14:08:48 -04:00
|
|
|
|
|
|
|
// Prepare the VM state. Includes an alarm check/reset for sleep.
|
2018-05-24 16:12:18 -04:00
|
|
|
start_mp(heap);
|
2020-12-11 08:10:26 -05:00
|
|
|
|
2021-04-28 23:48:26 -04:00
|
|
|
#if CIRCUITPY_USB
|
|
|
|
usb_setup_with_vm();
|
|
|
|
#endif
|
|
|
|
|
2021-06-20 15:25:37 -04:00
|
|
|
// Check if a different run file has been allocated
|
2020-10-29 11:39:06 -04:00
|
|
|
if (next_code_allocation) {
|
|
|
|
((next_code_info_t*)next_code_allocation->ptr)->options &= ~SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
|
|
|
|
next_code_options = ((next_code_info_t*)next_code_allocation->ptr)->options;
|
|
|
|
if (((next_code_info_t*)next_code_allocation->ptr)->filename[0] != '\0') {
|
|
|
|
const char* next_list[] = {((next_code_info_t*)next_code_allocation->ptr)->filename, ""};
|
2021-06-20 15:25:37 -04:00
|
|
|
// This is where the user's python code is actually executed:
|
2020-10-29 11:39:06 -04:00
|
|
|
found_main = maybe_run_list(next_list, &result);
|
|
|
|
if (!found_main) {
|
|
|
|
serial_write(((next_code_info_t*)next_code_allocation->ptr)->filename);
|
|
|
|
serial_write_compressed(translate(" not found.\n"));
|
|
|
|
}
|
2017-11-08 02:00:44 -05:00
|
|
|
}
|
|
|
|
}
|
2021-06-20 15:25:37 -04:00
|
|
|
// Otherwise, default to the standard list of filenames
|
2020-10-29 11:39:06 -04:00
|
|
|
if (!found_main) {
|
2021-06-20 15:25:37 -04:00
|
|
|
// This is where the user's python code is actually executed:
|
2020-10-29 11:39:06 -04:00
|
|
|
found_main = maybe_run_list(supported_filenames, &result);
|
2021-06-20 15:25:37 -04:00
|
|
|
// If that didn't work, double check the extensions
|
2020-10-29 11:39:06 -04:00
|
|
|
#if CIRCUITPY_FULL_BUILD
|
|
|
|
if (!found_main){
|
|
|
|
found_main = maybe_run_list(double_extension_filenames, &result);
|
|
|
|
if (found_main) {
|
|
|
|
serial_write_compressed(translate("WARNING: Your code filename has two extensions\n"));
|
|
|
|
}
|
|
|
|
}
|
2021-06-20 15:25:37 -04:00
|
|
|
#else
|
|
|
|
(void) found_main;
|
2020-10-29 11:39:06 -04:00
|
|
|
#endif
|
|
|
|
}
|
2020-12-01 20:01:14 -05:00
|
|
|
|
2021-07-08 22:11:05 -04:00
|
|
|
// 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 (reload_requested && result.return_code == PYEXEC_EXCEPTION) {
|
|
|
|
serial_write_compressed(translate("\nCode stopped by auto-reload.\n"));
|
|
|
|
} else {
|
|
|
|
serial_write_compressed(translate("\nCode done running.\n"));
|
|
|
|
}
|
|
|
|
|
2021-04-02 14:08:48 -04:00
|
|
|
// Finished executing python code. Cleanup includes a board reset.
|
2020-09-27 06:58:51 -04:00
|
|
|
cleanup_after_vm(heap, result.exception);
|
2017-09-22 21:05:51 -04:00
|
|
|
|
2020-10-29 11:39:06 -04:00
|
|
|
// If a new next code file was set, that is a reason to keep it (obviously). Stuff this into
|
|
|
|
// the options because it can be treated like any other reason-for-stickiness bit. The
|
|
|
|
// source is different though: it comes from the options that will apply to the next run,
|
|
|
|
// while the rest of next_code_options is what applied to this run.
|
|
|
|
if (next_code_allocation != NULL && (((next_code_info_t*)next_code_allocation->ptr)->options & SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET)) {
|
|
|
|
next_code_options |= SUPERVISOR_NEXT_CODE_OPT_NEWLY_SET;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (reload_requested) {
|
|
|
|
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_RELOAD;
|
2021-07-08 22:11:05 -04:00
|
|
|
} else if (result.return_code == 0) {
|
2020-10-29 11:39:06 -04:00
|
|
|
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_SUCCESS;
|
|
|
|
if (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_SUCCESS) {
|
|
|
|
skip_repl = true;
|
2021-06-20 16:57:20 -04:00
|
|
|
skip_wait = true;
|
2020-10-29 11:39:06 -04:00
|
|
|
}
|
2021-07-08 22:11:05 -04:00
|
|
|
} else {
|
2020-10-29 11:39:06 -04:00
|
|
|
next_code_stickiness_situation |= SUPERVISOR_NEXT_CODE_OPT_STICKY_ON_ERROR;
|
2021-06-20 17:20:47 -04:00
|
|
|
// Deep sleep cannot be skipped
|
|
|
|
// TODO: settings in deep sleep should persist, using a new sleep memory API
|
|
|
|
if (next_code_options & SUPERVISOR_NEXT_CODE_OPT_RELOAD_ON_ERROR
|
|
|
|
&& !(result.return_code & PYEXEC_DEEP_SLEEP)) {
|
2020-10-29 11:39:06 -04:00
|
|
|
skip_repl = true;
|
2021-06-20 16:57:20 -04:00
|
|
|
skip_wait = true;
|
2020-10-29 11:39:06 -04:00
|
|
|
}
|
|
|
|
}
|
2017-09-22 21:05:51 -04:00
|
|
|
if (result.return_code & PYEXEC_FORCED_EXIT) {
|
2020-10-29 11:39:06 -04:00
|
|
|
skip_repl = reload_requested;
|
2021-06-20 16:57:20 -04:00
|
|
|
skip_wait = true;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
2019-01-25 19:59:18 -05:00
|
|
|
}
|
|
|
|
|
2020-12-08 20:13:00 -05:00
|
|
|
// Program has finished running.
|
2021-01-20 21:40:53 -05:00
|
|
|
bool printed_press_any_key = false;
|
2019-08-21 16:18:43 -04:00
|
|
|
#if CIRCUITPY_DISPLAYIO
|
2021-04-16 19:18:01 -04:00
|
|
|
size_t time_to_epaper_refresh = 1;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Setup LED blinks.
|
|
|
|
#if CIRCUITPY_STATUS_LED
|
|
|
|
uint32_t color;
|
|
|
|
uint8_t blink_count;
|
2021-05-18 18:56:37 -04:00
|
|
|
bool led_active = false;
|
2021-04-16 19:18:01 -04:00
|
|
|
#if CIRCUITPY_ALARM
|
|
|
|
if (result.return_code & PYEXEC_DEEP_SLEEP) {
|
|
|
|
color = BLACK;
|
|
|
|
blink_count = 0;
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
if (result.return_code != PYEXEC_EXCEPTION) {
|
|
|
|
if (safe_mode == NO_SAFE_MODE) {
|
|
|
|
color = ALL_DONE;
|
|
|
|
blink_count = ALL_DONE_BLINKS;
|
|
|
|
} else {
|
|
|
|
color = SAFE_MODE;
|
|
|
|
blink_count = SAFE_MODE_BLINKS;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
color = EXCEPTION;
|
|
|
|
blink_count = EXCEPTION_BLINKS;
|
|
|
|
}
|
|
|
|
size_t pattern_start = supervisor_ticks_ms32();
|
|
|
|
size_t single_blink_time = (OFF_ON_RATIO + 1) * BLINK_TIME_MS;
|
|
|
|
size_t blink_time = single_blink_time * blink_count;
|
|
|
|
size_t total_time = blink_time + LED_SLEEP_TIME_MS;
|
2019-08-21 16:18:43 -04:00
|
|
|
#endif
|
2020-11-23 22:44:53 -05:00
|
|
|
|
2021-04-16 19:18:01 -04:00
|
|
|
#if CIRCUITPY_ALARM
|
2021-03-27 13:16:12 -04:00
|
|
|
bool fake_sleeping = false;
|
2021-04-16 19:18:01 -04:00
|
|
|
#endif
|
2021-06-20 16:57:20 -04:00
|
|
|
while (!skip_wait) {
|
2019-12-12 14:35:24 -05:00
|
|
|
RUN_BACKGROUND_TASKS;
|
2021-05-05 17:02:32 -04:00
|
|
|
|
|
|
|
// If a reload was requested by the supervisor or autoreload, return
|
2018-05-14 17:00:38 -04:00
|
|
|
if (reload_requested) {
|
2020-10-29 11:39:06 -04:00
|
|
|
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
|
|
|
|
// "no" for now, mainly because it's a bit less code. At this point, we have both a
|
|
|
|
// success or error and a reload, so let's have both of the respective options take
|
|
|
|
// effect (in OR combination).
|
2019-02-13 03:35:14 -05:00
|
|
|
reload_requested = false;
|
2021-04-16 19:18:01 -04:00
|
|
|
skip_repl = true;
|
|
|
|
break;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
2017-11-22 21:56:28 -05:00
|
|
|
|
2021-05-05 17:02:32 -04:00
|
|
|
// If interrupted by keyboard, return
|
2017-09-22 21:05:51 -04:00
|
|
|
if (serial_connected() && serial_bytes_available()) {
|
|
|
|
// Skip REPL if reload was requested.
|
2021-04-16 19:18:01 -04:00
|
|
|
skip_repl = serial_read() == CHAR_CTRL_D;
|
|
|
|
if (skip_repl) {
|
2020-11-22 19:10:09 -05:00
|
|
|
supervisor_set_run_reason(RUN_REASON_REPL_RELOAD);
|
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
break;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
|
|
|
|
2020-12-02 21:05:29 -05:00
|
|
|
// Check for a deep sleep alarm and restart the VM. This can happen if
|
|
|
|
// an alarm alerts faster than our USB delay or if we pretended to deep
|
|
|
|
// sleep.
|
|
|
|
#if CIRCUITPY_ALARM
|
2021-03-27 13:16:12 -04:00
|
|
|
if (fake_sleeping && common_hal_alarm_woken_from_sleep()) {
|
2020-12-02 21:05:29 -05:00
|
|
|
serial_write_compressed(translate("Woken up by alarm.\n"));
|
|
|
|
supervisor_set_run_reason(RUN_REASON_STARTUP);
|
2021-04-16 19:18:01 -04:00
|
|
|
skip_repl = true;
|
|
|
|
break;
|
2020-12-02 21:05:29 -05:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2021-05-05 17:02:32 -04:00
|
|
|
// If messages haven't been printed yet, print them
|
2021-01-20 21:40:53 -05:00
|
|
|
if (!printed_press_any_key && serial_connected()) {
|
2017-09-22 21:05:51 -04:00
|
|
|
if (!serial_connected_at_start) {
|
2020-10-07 14:23:08 -04:00
|
|
|
print_code_py_status_message(safe_mode);
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
2020-11-26 22:06:37 -05:00
|
|
|
|
2021-05-12 15:53:52 -04:00
|
|
|
if (!printed_safe_mode_message) {
|
|
|
|
print_safe_mode_message(safe_mode);
|
|
|
|
printed_safe_mode_message = true;
|
|
|
|
}
|
|
|
|
serial_write("\r\n");
|
2020-12-02 21:05:29 -05:00
|
|
|
serial_write_compressed(translate("Press any key to enter the REPL. Use CTRL-D to reload.\n"));
|
2021-01-20 21:40:53 -05:00
|
|
|
printed_press_any_key = true;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
2021-01-20 21:40:53 -05:00
|
|
|
if (!serial_connected()) {
|
2017-09-22 21:05:51 -04:00
|
|
|
serial_connected_at_start = false;
|
2021-01-20 21:40:53 -05:00
|
|
|
printed_press_any_key = false;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
|
|
|
|
2020-12-02 21:05:29 -05:00
|
|
|
// Sleep until our next interrupt.
|
|
|
|
#if CIRCUITPY_ALARM
|
|
|
|
if (result.return_code & PYEXEC_DEEP_SLEEP) {
|
|
|
|
// Make sure we have been awake long enough for USB to connect (enumeration delay).
|
|
|
|
int64_t connecting_delay_ticks = CIRCUITPY_USB_CONNECTED_SLEEP_DELAY * 1024 - port_get_raw_ticks(NULL);
|
2021-04-16 19:18:01 -04:00
|
|
|
// Until it's safe to decide whether we're real/fake sleeping
|
|
|
|
if (fake_sleeping) {
|
|
|
|
// This waits until a pretend deep sleep alarm occurs. They are set
|
|
|
|
// during common_hal_alarm_set_deep_sleep_alarms. On some platforms
|
|
|
|
// it may also return due to another interrupt, that's why we check
|
|
|
|
// for deep sleep alarms above. If it wasn't a deep sleep alarm,
|
|
|
|
// then we'll idle here again.
|
|
|
|
common_hal_alarm_pretending_deep_sleep();
|
|
|
|
} else if (connecting_delay_ticks < 0) {
|
|
|
|
// Entering deep sleep (may be fake or real.)
|
2020-12-02 21:05:29 -05:00
|
|
|
board_deinit();
|
|
|
|
if (!supervisor_workflow_active()) {
|
|
|
|
// Enter true deep sleep. When we wake up we'll be back at the
|
|
|
|
// top of main(), not in this loop.
|
2021-03-02 11:41:53 -05:00
|
|
|
common_hal_alarm_enter_deep_sleep();
|
2020-12-02 21:05:29 -05:00
|
|
|
// Does not return.
|
|
|
|
} else {
|
2020-12-22 19:13:02 -05:00
|
|
|
serial_write_compressed(translate("Pretending to deep sleep until alarm, CTRL-C or file write.\n"));
|
2021-04-16 19:18:01 -04:00
|
|
|
fake_sleeping = true;
|
2020-12-02 21:05:29 -05:00
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
} else {
|
|
|
|
// Loop while checking the time. We can't idle because we don't want to override a
|
|
|
|
// time alarm set for the deep sleep.
|
2020-12-02 21:05:29 -05:00
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
} else
|
2020-12-02 21:05:29 -05:00
|
|
|
#endif
|
2021-04-16 19:18:01 -04:00
|
|
|
{
|
|
|
|
// Refresh the ePaper display if we have one. That way it'll show an error message.
|
|
|
|
#if CIRCUITPY_DISPLAYIO
|
|
|
|
if (time_to_epaper_refresh > 0) {
|
|
|
|
time_to_epaper_refresh = maybe_refresh_epaperdisplay();
|
|
|
|
}
|
2020-12-02 21:05:29 -05:00
|
|
|
|
2021-04-16 19:18:01 -04:00
|
|
|
#if !CIRCUITPY_STATUS_LED
|
|
|
|
port_interrupt_after_ticks(time_to_epaper_refresh);
|
|
|
|
#endif
|
|
|
|
#endif
|
2021-04-09 13:48:45 -04:00
|
|
|
|
2021-04-16 19:18:01 -04:00
|
|
|
#if CIRCUITPY_STATUS_LED
|
|
|
|
uint32_t tick_diff = supervisor_ticks_ms32() - pattern_start;
|
|
|
|
|
|
|
|
// By default, don't sleep.
|
|
|
|
size_t time_to_next_change = 0;
|
|
|
|
if (tick_diff < blink_time) {
|
|
|
|
uint32_t blink_diff = tick_diff % (single_blink_time);
|
|
|
|
if (blink_diff >= BLINK_TIME_MS) {
|
2021-05-18 18:56:37 -04:00
|
|
|
if (led_active) {
|
|
|
|
new_status_color(BLACK);
|
|
|
|
status_led_deinit();
|
|
|
|
led_active = false;
|
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
time_to_next_change = single_blink_time - blink_diff;
|
|
|
|
} else {
|
2021-05-18 18:56:37 -04:00
|
|
|
if (!led_active) {
|
|
|
|
status_led_init();
|
|
|
|
new_status_color(color);
|
|
|
|
led_active = true;
|
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
time_to_next_change = BLINK_TIME_MS - blink_diff;
|
|
|
|
}
|
|
|
|
} else if (tick_diff > total_time) {
|
|
|
|
pattern_start = supervisor_ticks_ms32();
|
|
|
|
} else {
|
2021-05-18 18:56:37 -04:00
|
|
|
if (led_active) {
|
|
|
|
new_status_color(BLACK);
|
|
|
|
status_led_deinit();
|
|
|
|
led_active = false;
|
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
time_to_next_change = total_time - tick_diff;
|
|
|
|
}
|
|
|
|
#if CIRCUITPY_DISPLAYIO
|
|
|
|
if (time_to_epaper_refresh > 0 && time_to_next_change > 0) {
|
|
|
|
time_to_next_change = MIN(time_to_next_change, time_to_epaper_refresh);
|
|
|
|
}
|
|
|
|
#endif
|
2021-05-13 14:04:07 -04:00
|
|
|
|
|
|
|
// time_to_next_change is in ms and ticks are slightly shorter so
|
|
|
|
// we'll undersleep just a little. It shouldn't matter.
|
|
|
|
port_interrupt_after_ticks(time_to_next_change);
|
2021-03-27 14:08:44 -04:00
|
|
|
#endif
|
2021-04-16 19:18:01 -04:00
|
|
|
port_idle_until_interrupt();
|
2020-12-02 21:05:29 -05:00
|
|
|
}
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
2020-10-29 11:39:06 -04:00
|
|
|
|
2021-06-20 15:25:37 -04:00
|
|
|
// free code allocation if unused
|
2020-10-29 11:39:06 -04:00
|
|
|
if ((next_code_options & next_code_stickiness_situation) == 0) {
|
|
|
|
free_memory(next_code_allocation);
|
|
|
|
next_code_allocation = NULL;
|
|
|
|
}
|
2021-06-20 15:25:37 -04:00
|
|
|
|
2021-04-16 19:18:01 -04:00
|
|
|
// Done waiting, start the board back up.
|
|
|
|
#if CIRCUITPY_STATUS_LED
|
2021-05-18 18:56:37 -04:00
|
|
|
if (led_active) {
|
|
|
|
new_status_color(BLACK);
|
|
|
|
status_led_deinit();
|
|
|
|
}
|
2021-04-16 19:18:01 -04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if CIRCUITPY_ALARM
|
|
|
|
if (fake_sleeping) {
|
|
|
|
board_init();
|
|
|
|
}
|
|
|
|
#endif
|
2021-06-20 15:25:37 -04:00
|
|
|
|
2021-04-16 19:18:01 -04:00
|
|
|
return skip_repl;
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
|
|
|
|
2020-07-21 19:26:46 -04:00
|
|
|
FIL* boot_output_file;
|
|
|
|
|
2020-11-23 22:44:53 -05:00
|
|
|
STATIC void __attribute__ ((noinline)) run_boot_py(safe_mode_t safe_mode) {
|
2021-05-08 11:03:05 -04:00
|
|
|
// If not in safe mode, run boot before initing USB and capture output in a file.
|
2018-05-03 23:43:02 -04:00
|
|
|
|
2021-05-08 11:03:05 -04:00
|
|
|
// There is USB setup to do even if boot.py is not actually run.
|
|
|
|
const bool ok_to_run = filesystem_present()
|
|
|
|
&& safe_mode == NO_SAFE_MODE
|
|
|
|
&& MP_STATE_VM(vfs_mount_table) != NULL;
|
|
|
|
|
2021-07-26 14:04:27 -04:00
|
|
|
static const char * const boot_py_filenames[] = STRING_LIST("boot.py", "boot.txt");
|
2021-05-08 11:03:05 -04:00
|
|
|
bool skip_boot_output = false;
|
2021-08-02 17:38:23 -04:00
|
|
|
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
|
|
|
|
FIL file_pointer;
|
|
|
|
#endif
|
2018-05-03 23:43:02 -04:00
|
|
|
|
2021-05-08 11:03:05 -04:00
|
|
|
if (ok_to_run) {
|
2018-05-03 23:43:02 -04:00
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
|
|
|
|
boot_output_file = &file_pointer;
|
2018-05-03 23:43:02 -04:00
|
|
|
|
|
|
|
// Get the base filesystem.
|
|
|
|
FATFS *fs = &((fs_user_mount_t *) MP_STATE_VM(vfs_mount_table)->obj)->fatfs;
|
|
|
|
|
2018-05-04 08:28:05 -04:00
|
|
|
bool have_boot_py = first_existing_file_in_list(boot_py_filenames) != NULL;
|
2018-05-03 23:43:02 -04:00
|
|
|
|
|
|
|
// If there's no boot.py file that might write some changing output,
|
|
|
|
// read the existing copy of CIRCUITPY_BOOT_OUTPUT_FILE and see if its contents
|
|
|
|
// match the version info we would print anyway. If so, skip writing CIRCUITPY_BOOT_OUTPUT_FILE.
|
|
|
|
// This saves wear and tear on the flash and also prevents filesystem damage if power is lost
|
|
|
|
// during the write, which may happen due to bobbling the power connector or weak power.
|
|
|
|
|
2018-06-14 18:47:40 -04:00
|
|
|
static const size_t NUM_CHARS_TO_COMPARE = 160;
|
2018-05-03 23:43:02 -04:00
|
|
|
if (!have_boot_py && f_open(fs, boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_READ) == FR_OK) {
|
2018-06-14 18:47:40 -04:00
|
|
|
|
|
|
|
char file_contents[NUM_CHARS_TO_COMPARE];
|
2018-05-03 23:43:02 -04:00
|
|
|
UINT chars_read = 0;
|
2018-06-14 18:47:40 -04:00
|
|
|
f_read(boot_output_file, file_contents, NUM_CHARS_TO_COMPARE, &chars_read);
|
2018-05-03 23:43:02 -04:00
|
|
|
f_close(boot_output_file);
|
|
|
|
skip_boot_output =
|
|
|
|
// + 2 accounts for \r\n.
|
|
|
|
chars_read == strlen(MICROPY_FULL_VERSION_INFO) + 2 &&
|
|
|
|
strncmp(file_contents, MICROPY_FULL_VERSION_INFO, strlen(MICROPY_FULL_VERSION_INFO)) == 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!skip_boot_output) {
|
|
|
|
// Wait 1.5 seconds before opening CIRCUITPY_BOOT_OUTPUT_FILE for write,
|
|
|
|
// in case power is momentary or will fail shortly due to, say a low, battery.
|
2020-11-21 23:29:52 -05:00
|
|
|
if (common_hal_mcu_processor_get_reset_reason() == RESET_REASON_POWER_ON) {
|
2020-10-27 20:55:03 -04:00
|
|
|
mp_hal_delay_ms(1500);
|
|
|
|
}
|
2018-05-03 23:43:02 -04:00
|
|
|
// USB isn't up, so we can write the file.
|
2019-02-19 17:45:45 -05:00
|
|
|
filesystem_set_internal_writable_by_usb(false);
|
2018-05-03 23:43:02 -04:00
|
|
|
f_open(fs, boot_output_file, CIRCUITPY_BOOT_OUTPUT_FILE, FA_WRITE | FA_CREATE_ALWAYS);
|
|
|
|
|
2018-05-07 00:08:26 -04:00
|
|
|
// Switch the filesystem back to non-writable by Python now instead of later,
|
2018-05-06 23:48:11 -04:00
|
|
|
// since boot.py might change it back to writable.
|
2019-02-19 17:45:45 -05:00
|
|
|
filesystem_set_internal_writable_by_usb(true);
|
2018-05-06 23:48:11 -04:00
|
|
|
|
2018-05-03 23:43:02 -04:00
|
|
|
// Write version info to boot_out.txt.
|
|
|
|
mp_hal_stdout_tx_str(MICROPY_FULL_VERSION_INFO);
|
|
|
|
mp_hal_stdout_tx_str("\r\n");
|
|
|
|
}
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
|
|
|
|
2018-07-25 08:06:30 -04:00
|
|
|
filesystem_flush();
|
2021-05-08 11:03:05 -04:00
|
|
|
}
|
2018-07-25 08:06:30 -04:00
|
|
|
|
2021-05-08 11:03:05 -04:00
|
|
|
// Do USB setup even if boot.py is not run.
|
|
|
|
|
|
|
|
supervisor_allocation* heap = allocate_remaining_memory();
|
|
|
|
start_mp(heap);
|
|
|
|
|
|
|
|
#if CIRCUITPY_USB
|
|
|
|
// Set up default USB values after boot.py VM starts but before running boot.py.
|
|
|
|
usb_set_defaults();
|
|
|
|
#endif
|
2021-04-28 13:00:44 -04:00
|
|
|
|
2020-09-27 06:58:51 -04:00
|
|
|
pyexec_result_t result = {0, MP_OBJ_NULL, 0};
|
2021-05-08 11:03:05 -04:00
|
|
|
if (ok_to_run) {
|
2020-09-27 06:58:51 -04:00
|
|
|
bool found_boot = maybe_run_list(boot_py_filenames, &result);
|
2017-09-22 21:05:51 -04:00
|
|
|
(void) found_boot;
|
|
|
|
|
|
|
|
#ifdef CIRCUITPY_BOOT_OUTPUT_FILE
|
2018-05-03 23:43:02 -04:00
|
|
|
if (!skip_boot_output) {
|
|
|
|
f_close(boot_output_file);
|
|
|
|
filesystem_flush();
|
|
|
|
}
|
2018-05-19 01:01:37 -04:00
|
|
|
boot_output_file = NULL;
|
2017-09-22 21:05:51 -04:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-05-08 11:03:05 -04:00
|
|
|
#if CIRCUITPY_USB
|
|
|
|
// Some data needs to be carried over from the USB settings in boot.py
|
|
|
|
// to the next VM, while the heap is still available.
|
|
|
|
// Its size can vary, so save it temporarily on the stack,
|
|
|
|
// and then when the heap goes away, copy it in into a
|
|
|
|
// storage_allocation.
|
|
|
|
size_t size = usb_boot_py_data_size();
|
|
|
|
uint8_t usb_boot_py_data[size];
|
|
|
|
usb_get_boot_py_data(usb_boot_py_data, size);
|
|
|
|
#endif
|
2021-04-28 23:48:26 -04:00
|
|
|
|
2020-09-27 06:58:51 -04:00
|
|
|
cleanup_after_vm(heap, result.exception);
|
2021-04-28 23:48:26 -04:00
|
|
|
|
2021-05-08 11:03:05 -04:00
|
|
|
#if CIRCUITPY_USB
|
|
|
|
// Now give back the data we saved from the heap going away.
|
|
|
|
usb_return_boot_py_data(usb_boot_py_data, size);
|
|
|
|
#endif
|
2018-06-06 09:01:09 -04:00
|
|
|
}
|
|
|
|
|
2020-11-23 22:44:53 -05:00
|
|
|
STATIC int run_repl(void) {
|
2018-05-24 16:12:18 -04:00
|
|
|
int exit_code = PYEXEC_FORCED_EXIT;
|
2018-07-25 08:06:30 -04:00
|
|
|
stack_resize();
|
|
|
|
filesystem_flush();
|
2018-05-24 16:12:18 -04:00
|
|
|
supervisor_allocation* heap = allocate_remaining_memory();
|
|
|
|
start_mp(heap);
|
2021-04-28 23:48:26 -04:00
|
|
|
|
|
|
|
#if CIRCUITPY_USB
|
|
|
|
usb_setup_with_vm();
|
|
|
|
#endif
|
|
|
|
|
2018-05-24 16:12:18 -04:00
|
|
|
autoreload_suspend();
|
2021-04-16 19:18:01 -04:00
|
|
|
|
|
|
|
// Set the status LED to the REPL color before running the REPL. For
|
|
|
|
// NeoPixels and DotStars this will be sticky but for PWM or single LED it
|
|
|
|
// won't. This simplifies pin sharing because they won't be in use when
|
|
|
|
// actually in the REPL.
|
|
|
|
#if CIRCUITPY_STATUS_LED
|
|
|
|
status_led_init();
|
2018-05-24 16:12:18 -04:00
|
|
|
new_status_color(REPL_RUNNING);
|
2021-04-16 19:18:01 -04:00
|
|
|
status_led_deinit();
|
|
|
|
#endif
|
2018-05-24 16:12:18 -04:00
|
|
|
if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) {
|
|
|
|
exit_code = pyexec_raw_repl();
|
|
|
|
} else {
|
|
|
|
exit_code = pyexec_friendly_repl();
|
|
|
|
}
|
2021-08-01 04:31:40 -04:00
|
|
|
#if CIRCUITPY_ATEXIT
|
2021-08-16 12:52:22 -04:00
|
|
|
pyexec_result_t result;
|
|
|
|
shared_module_atexit_execute(&result);
|
|
|
|
if (result.return_code == PYEXEC_DEEP_SLEEP) {
|
|
|
|
exit_code = PYEXEC_DEEP_SLEEP;
|
|
|
|
}
|
2021-08-01 04:31:40 -04:00
|
|
|
#endif
|
2020-09-27 06:58:51 -04:00
|
|
|
cleanup_after_vm(heap, MP_OBJ_SENTINEL);
|
2021-05-18 14:32:45 -04:00
|
|
|
#if CIRCUITPY_STATUS_LED
|
|
|
|
status_led_init();
|
|
|
|
new_status_color(BLACK);
|
|
|
|
status_led_deinit();
|
|
|
|
#endif
|
2020-09-27 06:58:51 -04:00
|
|
|
|
2018-05-24 16:12:18 -04:00
|
|
|
autoreload_resume();
|
|
|
|
return exit_code;
|
|
|
|
}
|
|
|
|
|
2018-06-06 09:01:09 -04:00
|
|
|
int __attribute__((used)) main(void) {
|
|
|
|
// initialise the cpu and peripherals
|
|
|
|
safe_mode_t safe_mode = port_init();
|
|
|
|
|
2021-04-16 19:18:01 -04:00
|
|
|
// Turn on RX and TX LEDs if we have them.
|
|
|
|
init_rxtx_leds();
|
2018-06-06 09:01:09 -04:00
|
|
|
|
2018-12-06 17:24:20 -05:00
|
|
|
// Wait briefly to give a reset window where we'll enter safe mode after the reset.
|
|
|
|
if (safe_mode == NO_SAFE_MODE) {
|
|
|
|
safe_mode = wait_for_safe_mode_reset();
|
|
|
|
}
|
|
|
|
|
2018-08-31 15:27:56 -04:00
|
|
|
stack_init();
|
|
|
|
|
2021-05-19 19:22:07 -04:00
|
|
|
#if CIRCUITPY_BLEIO
|
|
|
|
// Early init so that a reset press can cause BLE public advertising.
|
|
|
|
supervisor_bluetooth_init();
|
|
|
|
#endif
|
|
|
|
|
2018-06-06 09:01:09 -04:00
|
|
|
// Create a new filesystem only if we're not in a safe mode.
|
|
|
|
// A power brownout here could make it appear as if there's
|
|
|
|
// no SPI flash filesystem, and we might erase the existing one.
|
|
|
|
filesystem_init(safe_mode == NO_SAFE_MODE, false);
|
|
|
|
|
2020-01-29 16:29:43 -05:00
|
|
|
// displays init after filesystem, since they could share the flash SPI
|
2020-03-06 21:15:16 -05:00
|
|
|
board_init();
|
2020-01-29 16:29:43 -05:00
|
|
|
|
2020-05-18 08:04:55 -04:00
|
|
|
// Start the debug serial
|
|
|
|
serial_early_init();
|
|
|
|
|
2018-06-06 09:01:09 -04:00
|
|
|
// Reset everything and prep MicroPython to run boot.py.
|
|
|
|
reset_port();
|
2020-08-04 18:24:37 -04:00
|
|
|
// Port-independent devices, like CIRCUITPY_BLEIO_HCI.
|
|
|
|
reset_devices();
|
2018-06-06 09:01:09 -04:00
|
|
|
reset_board();
|
|
|
|
|
2020-11-22 19:10:09 -05:00
|
|
|
// This is first time we are running CircuitPython after a reset or power-up.
|
|
|
|
supervisor_set_run_reason(RUN_REASON_STARTUP);
|
|
|
|
|
2020-10-07 12:45:05 -04:00
|
|
|
// If not in safe mode turn on autoreload by default but before boot.py in case it wants to change it.
|
|
|
|
if (safe_mode == NO_SAFE_MODE) {
|
|
|
|
autoreload_enable();
|
|
|
|
}
|
2018-06-06 09:01:09 -04:00
|
|
|
|
|
|
|
// By default our internal flash is readonly to local python code and
|
|
|
|
// writable over USB. Set it here so that boot.py can change it.
|
2019-02-19 17:45:45 -05:00
|
|
|
filesystem_set_internal_concurrent_write_protection(true);
|
|
|
|
filesystem_set_internal_writable_by_usb(true);
|
2018-06-06 09:01:09 -04:00
|
|
|
|
|
|
|
run_boot_py(safe_mode);
|
2017-09-22 21:05:51 -04:00
|
|
|
|
2021-04-28 13:00:44 -04:00
|
|
|
// Start USB after giving boot.py a chance to tweak behavior.
|
2021-04-27 23:53:23 -04:00
|
|
|
#if CIRCUITPY_USB
|
|
|
|
// Setup USB connection after heap is available.
|
|
|
|
// It needs the heap to build descriptors.
|
|
|
|
usb_init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Set up any other serial connection.
|
2017-09-22 21:05:51 -04:00
|
|
|
serial_init();
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
#if CIRCUITPY_BLEIO
|
2021-08-02 21:37:19 -04:00
|
|
|
supervisor_bluetooth_enable_workflow();
|
2019-09-14 15:40:24 -04:00
|
|
|
supervisor_start_bluetooth();
|
|
|
|
#endif
|
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
// Boot script is finished, so now go into REPL/main mode.
|
|
|
|
int exit_code = PYEXEC_FORCED_EXIT;
|
|
|
|
bool skip_repl = true;
|
|
|
|
bool first_run = true;
|
|
|
|
for (;;) {
|
|
|
|
if (!skip_repl) {
|
2018-05-24 16:12:18 -04:00
|
|
|
exit_code = run_repl();
|
2021-05-05 17:02:32 -04:00
|
|
|
supervisor_set_run_reason(RUN_REASON_REPL_RELOAD);
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
|
|
|
if (exit_code == PYEXEC_FORCED_EXIT) {
|
|
|
|
if (!first_run) {
|
2018-12-06 17:24:20 -05:00
|
|
|
serial_write_compressed(translate("soft reboot\n"));
|
2017-09-22 21:05:51 -04:00
|
|
|
}
|
|
|
|
first_run = false;
|
2018-05-24 16:12:18 -04:00
|
|
|
skip_repl = run_code_py(safe_mode);
|
2017-09-22 21:05:51 -04:00
|
|
|
} else if (exit_code != 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_deinit();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void gc_collect(void) {
|
|
|
|
gc_collect_start();
|
2018-06-28 13:36:28 -04:00
|
|
|
|
|
|
|
mp_uint_t regs[10];
|
|
|
|
mp_uint_t sp = cpu_get_regs_and_sp(regs);
|
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
// This collects root pointers from the VFS mount table. Some of them may
|
|
|
|
// have lost their references in the VM even though they are mounted.
|
|
|
|
gc_collect_root((void**)&MP_STATE_VM(vfs_mount_table), sizeof(mp_vfs_mount_t) / sizeof(mp_uint_t));
|
2019-06-06 17:49:32 -04:00
|
|
|
|
2020-07-17 09:36:26 -04:00
|
|
|
background_callback_gc_collect();
|
|
|
|
|
2020-12-22 10:06:43 -05:00
|
|
|
#if CIRCUITPY_ALARM
|
|
|
|
common_hal_alarm_gc_collect();
|
|
|
|
#endif
|
|
|
|
|
2021-08-01 04:31:40 -04:00
|
|
|
#if CIRCUITPY_ATEXIT
|
|
|
|
atexit_gc_collect();
|
|
|
|
#endif
|
|
|
|
|
2019-06-06 17:49:32 -04:00
|
|
|
#if CIRCUITPY_DISPLAYIO
|
|
|
|
displayio_gc_collect();
|
|
|
|
#endif
|
|
|
|
|
2019-09-14 15:40:24 -04:00
|
|
|
#if CIRCUITPY_BLEIO
|
|
|
|
common_hal_bleio_gc_collect();
|
|
|
|
#endif
|
|
|
|
|
2021-04-28 23:48:26 -04:00
|
|
|
#if CIRCUITPY_USB_HID
|
|
|
|
usb_hid_gc_collect();
|
|
|
|
#endif
|
|
|
|
|
2020-12-07 11:06:33 -05:00
|
|
|
#if CIRCUITPY_WIFI
|
|
|
|
common_hal_wifi_gc_collect();
|
|
|
|
#endif
|
|
|
|
|
2017-09-22 21:05:51 -04:00
|
|
|
// This naively collects all object references from an approximate stack
|
|
|
|
// range.
|
2019-10-18 05:05:08 -04:00
|
|
|
gc_collect_root((void**)sp, ((uint32_t)port_stack_get_top() - sp) / sizeof(uint32_t));
|
2017-09-22 21:05:51 -04:00
|
|
|
gc_collect_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
void NORETURN nlr_jump_fail(void *val) {
|
2018-12-06 17:24:20 -05:00
|
|
|
reset_into_safe_mode(MICROPY_NLR_JUMP_FAIL);
|
2017-09-22 21:05:51 -04:00
|
|
|
while (true) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
void NORETURN __fatal_error(const char *msg) {
|
2018-12-06 17:24:20 -05:00
|
|
|
reset_into_safe_mode(MICROPY_FATAL_ERROR);
|
2017-09-22 21:05:51 -04:00
|
|
|
while (true) {}
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifndef NDEBUG
|
|
|
|
void MP_WEAK __assert_func(const char *file, int line, const char *func, const char *expr) {
|
2018-08-31 17:46:03 -04:00
|
|
|
mp_printf(&mp_plat_print, "Assertion '%s' failed, at file %s:%d\n", expr, file, line);
|
2017-09-22 21:05:51 -04:00
|
|
|
__fatal_error("Assertion failed");
|
|
|
|
}
|
|
|
|
#endif
|