From 046164098337cc79dd0f6c710f49bdf6765aa711 Mon Sep 17 00:00:00 2001 From: robert-hh Date: Wed, 3 Mar 2021 08:44:38 +0100 Subject: [PATCH] rp2/rp2_pio: Fix sm.get(buf) to not wait after getting last item. sm.get(buf) was waiting for one item more than the length of the supplied buffer. Even if this item was not stored, sm_get would block trying to get an item from the RX fifo. As part of the fix, the edge case for a zero length buffer was moved up to the section where the function arguments are handled. In case of a zero length buffer, sm.get() now returns immediately that buffer. --- ports/rp2/rp2_pio.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ports/rp2/rp2_pio.c b/ports/rp2/rp2_pio.c index 6101164a15..9786e569d6 100644 --- a/ports/rp2/rp2_pio.c +++ b/ports/rp2/rp2_pio.c @@ -608,6 +608,9 @@ STATIC mp_obj_t rp2_state_machine_get(size_t n_args, const mp_obj_t *args) { } else { bufinfo.typecode |= 0x20; // make lowercase to support upper and lower } + if (bufinfo.len == 0) { // edge case: buffer of zero length supplied + return args[1]; + } } if (n_args > 2) { shift = mp_obj_get_int(args[2]); @@ -625,9 +628,6 @@ STATIC mp_obj_t rp2_state_machine_get(size_t n_args, const mp_obj_t *args) { if (dest == NULL) { return mp_obj_new_int_from_uint(value); } - if (dest >= dest_top) { - return args[1]; - } if (bufinfo.typecode == 'b') { *(uint8_t *)dest = value; dest += sizeof(uint8_t); @@ -640,6 +640,9 @@ STATIC mp_obj_t rp2_state_machine_get(size_t n_args, const mp_obj_t *args) { } else { mp_raise_ValueError("unsupported buffer type"); } + if (dest >= dest_top) { + return args[1]; + } } } STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(rp2_state_machine_get_obj, 1, 3, rp2_state_machine_get);