diff --git a/ports/stm32/main.c b/ports/stm32/main.c index a3f631aaf..803c8fcdd 100644 --- a/ports/stm32/main.c +++ b/ports/stm32/main.c @@ -79,10 +79,10 @@ #include "omv_gpio.h" #include "omv_i2c.h" #include "omv_csi.h" +#include "omv_protocol.h" #include "mp_utils.h" #include "framebuffer.h" -#include "usbdbg.h" #include "sdram.h" #include "stm_xspi.h" #include "fb_alloc.h" @@ -225,7 +225,7 @@ soft_reset: #if MICROPY_HW_ENABLE_CAN pyb_can_init0(); #endif - #if MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C + #if MICROPY_PY_PYB_LEGACY && MICROPY_HW_ENABLE_HW_I2C i2c_init0(); #endif spi_init0(); @@ -245,7 +245,6 @@ soft_reset: #if MICROPY_HW_ENABLE_SERVO servo_init(); #endif - usbdbg_init(); #if MICROPY_HW_ENABLE_SDCARD sdcard_init(); #endif @@ -297,6 +296,9 @@ soft_reset: mod_network_init(); #endif + // Initialize OpenMV protocol + omv_protocol_init_default(); + // Execute _boot.py to set up the filesystem. pyexec_frozen_module("_boot.py", false); @@ -326,24 +328,18 @@ soft_reset: } #endif - // Run boot.py script. - bool interrupted = mp_exec_bootscript("boot.py", true); - - // Run main.py script on first soft-reset. - if (first_soft_reset && !interrupted && mp_vfs_import_stat("main.py")) { - mp_exec_bootscript("main.py", true); - goto soft_reset_exit; + // Run boot.py every reset and main.py on first soft-reset + if (pyexec_file_if_exists("boot.py") && first_soft_reset) { + pyexec_file_if_exists("main.py"); } - // If there's no script ready, just re-exec REPL - while (!usbdbg_script_ready()) { + while (!omv_protocol_exec_script()) { nlr_buf_t nlr; if (nlr_push(&nlr) == 0) { - // enable IDE interrupt - usbdbg_set_irq_enabled(true); + // Enable Ctrl+C to interrupt script or REPL. + mp_hal_set_interrupt_char(CHAR_CTRL_C); - // run REPL if (pyexec_mode_kind == PYEXEC_MODE_RAW_REPL) { if (pyexec_raw_repl() != 0) { break; @@ -353,31 +349,15 @@ soft_reset: break; } } - nlr_pop(); } } - if (usbdbg_script_ready()) { - nlr_buf_t nlr; - if (nlr_push(&nlr) == 0) { - // Enable IDE interrupts - usbdbg_set_irq_enabled(true); - // Execute the script. - pyexec_str(usbdbg_get_script(), true); - // Disable IDE interrupts - usbdbg_set_irq_enabled(false); - nlr_pop(); - } else { - mp_obj_print_exception(MP_PYTHON_PRINTER, (mp_obj_t) nlr.ret_val); - } - } - -soft_reset_exit: // soft reset + mp_hal_set_interrupt_char(-1); mp_printf(MP_PYTHON_PRINTER, "MPY: soft reboot\n"); #if MICROPY_PY_CSI - omv_csi_abort_all(); + omv_csi_abort_all(); #endif #if MICROPY_PY_LWIP systick_disable_dispatch(SYSTICK_DISPATCH_LWIP); diff --git a/ports/stm32/omv_mpconfigport.h b/ports/stm32/omv_mpconfigport.h index ba6c8f6df..0396368b2 100644 --- a/ports/stm32/omv_mpconfigport.h +++ b/ports/stm32/omv_mpconfigport.h @@ -29,6 +29,19 @@ fb_alloc_free_till_mark(); \ } while (0); + +#define MICROPY_BOARD_BEFORE_PYTHON_EXEC(input_kind, exec_flags) \ + do { \ + extern void stdio_channel_pyexec_hook(bool); \ + stdio_channel_pyexec_hook(true); \ + } while (0); + +#define MICROPY_BOARD_AFTER_PYTHON_EXEC(input_kind, exec_flags, nlr, ret) \ + do { \ + extern void stdio_channel_pyexec_hook(bool); \ + stdio_channel_pyexec_hook(false); \ + } while (0); + #define MICROPY_ENABLE_VM_ABORT (1) #define MICROPY_OPT_COMPUTED_GOTO (1) #define MICROPY_GC_SPLIT_HEAP (1) diff --git a/ports/stm32/omv_portconfig.mk b/ports/stm32/omv_portconfig.mk index 061998d18..e608b8619 100644 --- a/ports/stm32/omv_portconfig.mk +++ b/ports/stm32/omv_portconfig.mk @@ -80,6 +80,10 @@ LDFLAGS = -mthumb \ -mabi=aapcs-linux \ -Wl,--print-memory-usage \ -Wl,--gc-sections \ + -Wl,--wrap=usbd_cdc_control \ + -Wl,--wrap=usbd_cdc_receive \ + -Wl,--wrap=mp_os_dupterm_rx_chr \ + -Wl,--wrap=mp_hal_stdout_tx_strn \ -Wl,-T$(BUILD)/$(LDSCRIPT).lds \ -Wl,-Map=$(BUILD)/$(FIRMWARE).map @@ -141,6 +145,7 @@ CFLAGS += $(HAL_CFLAGS) $(MPY_CFLAGS) $(OMV_CFLAGS) include lib/cmsis/cmsis.mk include lib/stm32/stm32.mk include common/common.mk +include protocol/protocol.mk include drivers/drivers.mk include lib/imlib/imlib.mk include lib/tflm/tflm.mk