From c79be30ca661194d50fb1dc5a4af70aef50d29f4 Mon Sep 17 00:00:00 2001 From: Sandor Attila Gerendi Date: Thu, 30 Dec 2021 17:50:12 +0200 Subject: [PATCH] Cater for the case where another state change occurs while the ISR is running. Thank you peterhinch --- examples/rp2/pio_quadrature_encoder.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/examples/rp2/pio_quadrature_encoder.py b/examples/rp2/pio_quadrature_encoder.py index 7945b3410b..cc6cf2ec51 100644 --- a/examples/rp2/pio_quadrature_encoder.py +++ b/examples/rp2/pio_quadrature_encoder.py @@ -66,11 +66,12 @@ lut_index = 0 def encoder_state_changed_irq_handler(sm): global counter, direction, lut_index - lut_index |= sm.get() & 3 - counter += state_look_up_table[lut_index] - if state_look_up_table[lut_index] != 0: - direction = 1 if (state_look_up_table[lut_index] > 0) else 0 - lut_index = ((lut_index << 2) & 0b1100) | (direction << 4) + while sm.rx_fifo(): + lut_index |= sm.get() & 3 + counter += state_look_up_table[lut_index] + if state_look_up_table[lut_index] != 0: + direction = 1 if (state_look_up_table[lut_index] > 0) else 0 + lut_index = ((lut_index << 2) & 0b1100) | (direction << 4) @rp2.asm_pio()