Merge pull request #1816 from openmv/usb_debug_fixes

misc/usbdbg: Refactor USB debugger code.
This commit is contained in:
Ibrahim Abdelkader 2023-04-07 13:17:42 +02:00 committed by GitHub
commit c73229802f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,6 +14,7 @@
#include "py/gc.h" #include "py/gc.h"
#include "py/mphal.h" #include "py/mphal.h"
#include "py/obj.h" #include "py/obj.h"
#include "py/objstr.h"
#include "py/runtime.h" #include "py/runtime.h"
#include "pendsv.h" #include "pendsv.h"
@ -36,7 +37,13 @@ static volatile bool script_ready;
static volatile bool script_running; static volatile bool script_running;
static volatile bool irq_enabled; static volatile bool irq_enabled;
static vstr_t script_buf; static vstr_t script_buf;
static mp_obj_t mp_const_ide_interrupt = MP_OBJ_NULL;
static mp_obj_exception_t ide_exception;
static const MP_DEFINE_STR_OBJ(ide_exception_msg, "IDE interrupt");
static const mp_rom_obj_tuple_t ide_exception_args_obj = {
{&mp_type_tuple}, 1, {MP_ROM_PTR(&ide_exception_msg)}
};
// These functions must be implemented in MicroPython CDC driver. // These functions must be implemented in MicroPython CDC driver.
extern uint32_t usb_cdc_buf_len(); extern uint32_t usb_cdc_buf_len();
@ -52,8 +59,15 @@ void usbdbg_init()
script_ready=false; script_ready=false;
script_running=false; script_running=false;
irq_enabled = false; irq_enabled = false;
vstr_init(&script_buf, 32); vstr_init(&script_buf, 32);
mp_const_ide_interrupt = mp_obj_new_exception_msg(&mp_type_Exception, MP_ERROR_TEXT("IDE interrupt"));
// Initialize the IDE exception object.
ide_exception.base.type = &mp_type_Exception;
ide_exception.traceback_alloc = 0;
ide_exception.traceback_len = 0;
ide_exception.traceback_data = NULL;
ide_exception.args = (mp_obj_tuple_t *) &ide_exception_args_obj;
} }
void usbdbg_wait_for_command(uint32_t timeout) void usbdbg_wait_for_command(uint32_t timeout)
@ -88,6 +102,29 @@ inline void usbdbg_set_irq_enabled(bool enabled)
irq_enabled = enabled; irq_enabled = enabled;
} }
static void usbdbg_interrupt_vm(bool ready)
{
// Set script ready flag
script_ready = ready;
// Set script running flag
script_running = ready;
// Disable IDE IRQ (re-enabled by pyexec or main).
usbdbg_set_irq_enabled(false);
// Clear interrupt traceback
mp_obj_exception_clear_traceback(&ide_exception);
// Remove the BASEPRI masking (if any)
__set_BASEPRI(0);
// 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(&ide_exception);
}
bool usbdbg_get_irq_enabled() bool usbdbg_get_irq_enabled()
{ {
return irq_enabled; return irq_enabled;
@ -209,29 +246,17 @@ void usbdbg_data_out(void *buffer, int length)
// check if GC is locked before allocating memory for vstr. If GC was locked // 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 // 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. // than the total length (xfer_length) and the script will Not be executed.
if (!script_running && !gc_is_locked()) { if (!script_running) {
vstr_add_strn(&script_buf, buffer, length); nlr_buf_t nlr;
if (!gc_is_locked() && nlr_push(&nlr) == 0) {
vstr_add_strn(&script_buf, buffer, length);
nlr_pop();
}
xfer_bytes += length; xfer_bytes += length;
if (xfer_bytes == xfer_length) { if (xfer_bytes == xfer_length) {
// Set script ready flag cmd = USBDBG_NONE;
script_ready = true; // Schedule the IDE exception to interrupt the VM.
usbdbg_interrupt_vm(true);
// Set script running flag
script_running = true;
// Disable IDE IRQ (re-enabled by pyexec or main).
usbdbg_set_irq_enabled(false);
// Clear interrupt traceback
mp_obj_exception_clear_traceback(mp_const_ide_interrupt);
// Remove the BASEPRI masking (if any)
__set_BASEPRI(0);
// 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(mp_const_ide_interrupt);
} }
} }
break; break;
@ -364,22 +389,11 @@ void usbdbg_control(void *buffer, uint8_t request, uint32_t length)
case USBDBG_SCRIPT_STOP: case USBDBG_SCRIPT_STOP:
if (script_running) { if (script_running) {
// Set script running flag // Reset CDC buffers.
script_running = false;
// Disable IDE IRQ (re-enabled by pyexec or main).
usbdbg_set_irq_enabled(false);
// Reset CDC buffers after disabling IRQs.
usb_cdc_reset_buffers(); usb_cdc_reset_buffers();
// interrupt running code by raising an exception // Schedule the IDE exception to interrupt the VM.
mp_obj_exception_clear_traceback(mp_const_ide_interrupt); usbdbg_interrupt_vm(false);
// Remove the BASEPRI masking (if any)
__set_BASEPRI(0);
pendsv_nlr_jump(mp_const_ide_interrupt);
} }
cmd = USBDBG_NONE; cmd = USBDBG_NONE;
break; break;