Skip to content

Commit

Permalink
rp2/rp2_flash: Call MICROPY_EVENT_POLL_HOOK_FAST after reading flash.
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
robert-hh authored and dpgeorge committed Jan 12, 2023
1 parent b208cf2 commit 5890a17
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions ports/rp2/rp2_flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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]);
Expand All @@ -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;
}
Expand Down

0 comments on commit 5890a17

Please sign in to comment.