From 890d27d1dd9af13223c16e690c06adcfad8d3b3f Mon Sep 17 00:00:00 2001 From: iabdalkader Date: Tue, 15 Nov 2016 18:53:34 +0200 Subject: [PATCH] Cleanup USBDBG code. --- src/omv/main.c | 10 +++------- src/omv/usbdbg.c | 30 ++++++++++++++---------------- 2 files changed, 17 insertions(+), 23 deletions(-) diff --git a/src/omv/main.c b/src/omv/main.c index bd7c318f3..68399d2f8 100644 --- a/src/omv/main.c +++ b/src/omv/main.c @@ -465,9 +465,6 @@ soft_reset: while (!usbdbg_script_ready()) { nlr_buf_t nlr; - // Re-enable FS IRQ (disabled in usbdbg) - HAL_NVIC_EnableIRQ(OTG_FS_IRQn); - if (nlr_push(&nlr) == 0) { // enable IDE interrupt usbdbg_set_irq_enabled(true); @@ -482,12 +479,11 @@ soft_reset: if (usbdbg_script_ready()) { nlr_buf_t nlr; - // Re-enable FS IRQ (disabled in usbdbg) - HAL_NVIC_EnableIRQ(OTG_FS_IRQn); - // execute the script if (nlr_push(&nlr) == 0) { - // parse, compile and execute script + // enable IDE interrupt + usbdbg_set_irq_enabled(true); + pyexec_str(usbdbg_get_script()); nlr_pop(); } else { diff --git a/src/omv/usbdbg.c b/src/omv/usbdbg.c index 68d096d70..16f54d5e5 100644 --- a/src/omv/usbdbg.c +++ b/src/omv/usbdbg.c @@ -25,7 +25,6 @@ static enum usbdbg_cmd cmd; static volatile bool script_ready; static volatile bool script_running; -static volatile bool irq_enabled; static vstr_t script_buf; static mp_obj_t mp_const_ide_interrupt = MP_OBJ_NULL; @@ -36,7 +35,6 @@ extern const char *ffs_strerror(FRESULT res); void usbdbg_init() { - irq_enabled=false; script_ready=false; script_running=false; vstr_init(&script_buf, 32); @@ -53,14 +51,14 @@ vstr_t *usbdbg_get_script() return &script_buf; } -inline bool usbdbg_get_irq_enabled() -{ - return irq_enabled; -} - inline void usbdbg_set_irq_enabled(bool enabled) { - irq_enabled=enabled; __DSB(); + if (enabled) { + HAL_NVIC_EnableIRQ(OTG_FS_IRQn); + } else { + HAL_NVIC_DisableIRQ(OTG_FS_IRQn); + } + __DSB(); __ISB(); } void usbdbg_data_in(void *buffer, int length) @@ -150,7 +148,7 @@ void usbdbg_data_out(void *buffer, int length) // check if GC is locked before allocating memory for vstr. If GC was locked // at least once before the script is fully uploaded xfer_bytes will be less // than the total length (xfer_length) and the script will Not be executed. - if (!script_running && usbdbg_get_irq_enabled() && !gc_is_locked()) { + if (!script_running && !gc_is_locked()) { vstr_add_strn(&script_buf, buffer, length); xfer_bytes += length; if (xfer_bytes == xfer_length) { @@ -163,8 +161,11 @@ void usbdbg_data_out(void *buffer, int length) // Disable IDE IRQ (re-enabled by pyexec or main). usbdbg_set_irq_enabled(false); - // interrupt running script/REPL + // Clear interrupt traceback mp_obj_exception_clear_traceback(mp_const_ide_interrupt); + // Interrupt running REPL + // Note: setting pendsv explicitly here because the VM is probably + // waiting in REPL and the soft interrupt flag will not be checked. pendsv_nlr_jump_hard(mp_const_ide_interrupt); } } @@ -240,24 +241,21 @@ void usbdbg_control(void *buffer, uint8_t request, uint32_t length) case USBDBG_SCRIPT_EXEC: xfer_bytes = 0; - xfer_length =length; + xfer_length = length; vstr_reset(&script_buf); break; case USBDBG_SCRIPT_STOP: - if (usbdbg_get_irq_enabled()) { + if (script_running) { // Set script running flag script_running = false; // Disable IDE IRQ (re-enabled by pyexec or main). usbdbg_set_irq_enabled(false); - // We can safely disable FS IRQ here - HAL_NVIC_DisableIRQ(OTG_FS_IRQn); __DSB(); __ISB(); - // interrupt running code by raising an exception mp_obj_exception_clear_traceback(mp_const_ide_interrupt); - pendsv_nlr_jump_hard(mp_const_ide_interrupt); + pendsv_nlr_jump(mp_const_ide_interrupt); } cmd = USBDBG_NONE; break;