From 5890a17ae049aad39f97744cadf5844c06a5a4c0 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Fri, 6 Jan 2023 11:36:18 +0100 Subject: [PATCH] rp2/rp2_flash: Call MICROPY_EVENT_POLL_HOOK_FAST after reading flash. To allow the USB to work in cases where there is a lot of filesystem access, in particular on boot. For example, registering of the USB CDC interface may fail if: - the board file system is lfs2 (default), and - sys.path contains entries for the local file system (default), and - files are imported by boot.py or main.py from frozen bytecode of the file system (common) and the file system contains many files, like 100. In that case the board is very busy with scanning LFS, and registering the USB interface seems to time out. This commit fixes this by allowing the USB to make progress during filesystem reads. Also switch existing MICROPY_EVENT_POLL_HOOK uses in this file to MICROPY_EVENT_POLL_HOOK_FAST now that the latter macro exists. --- ports/rp2/rp2_flash.c | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/ports/rp2/rp2_flash.c b/ports/rp2/rp2_flash.c index 0a94b6cc90..26ec41949e 100644 --- a/ports/rp2/rp2_flash.c +++ b/ports/rp2/rp2_flash.c @@ -120,6 +120,11 @@ STATIC mp_obj_t rp2_flash_readblocks(size_t n_args, const mp_obj_t *args) { offset += mp_obj_get_int(args[3]); } memcpy(bufinfo.buf, (void *)(XIP_BASE + self->flash_base + offset), bufinfo.len); + // MICROPY_EVENT_POLL_HOOK_FAST is called here to avoid a fail in registering + // USB at boot time, if the board is busy loading files or scanning the file + // system. MICROPY_EVENT_POLL_HOOK_FAST calls tud_task(). As the alternative + // tud_task() should be called in the USB IRQ. See discussion in PR #10423. + MICROPY_EVENT_POLL_HOOK_FAST; return mp_const_none; } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_flash_readblocks_obj, 3, 4, rp2_flash_readblocks); @@ -134,7 +139,7 @@ STATIC mp_obj_t rp2_flash_writeblocks(size_t n_args, const mp_obj_t *args) { mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); flash_range_erase(self->flash_base + offset, bufinfo.len); MICROPY_END_ATOMIC_SECTION(atomic_state); - MICROPY_EVENT_POLL_HOOK + MICROPY_EVENT_POLL_HOOK_FAST; // TODO check return value } else { offset += mp_obj_get_int(args[3]); @@ -143,7 +148,7 @@ STATIC mp_obj_t rp2_flash_writeblocks(size_t n_args, const mp_obj_t *args) { mp_uint_t atomic_state = MICROPY_BEGIN_ATOMIC_SECTION(); flash_range_program(self->flash_base + offset, bufinfo.buf, bufinfo.len); MICROPY_END_ATOMIC_SECTION(atomic_state); - MICROPY_EVENT_POLL_HOOK + MICROPY_EVENT_POLL_HOOK_FAST; // TODO check return value return mp_const_none; }