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);