raspberrypi / pico-examples

BSD 3-Clause "New" or "Revised" License
2.94k stars 844 forks source link

pio quadrature_encoder example not working on pico 2 RP2350 #550

Closed danielallstar closed 2 months ago

danielallstar commented 2 months ago

The pio - quadrature_encoder example does not seem to work with the raspberry pi pico 2 (RP2350). It does however work with the pico RP2040.

The expected behaviour is that when you turn a quarature encoder the "new_value" changes. This does however not happen with the pico 2

Any help to resolve this issue would be appreciated

lurch commented 2 months ago

ping @pmarques-dev who originally wrote this example.

pmarques-dev commented 2 months ago

I haven't got a RP2350 yet, but have one ordered and shipping at the moment. When that arrives, I can test it and figure out what's going on

peterharperuk commented 2 months ago

I've reproduced the problem - checking what's going on.

pmarques-dev commented 2 months ago

from the top of my head, I would verify that the code is actually being successfully loaded at address 0. Maybe the new SDK uses PIO's for something and the example is silently failing to load the code and then trying to use it anyway

Hax2022 commented 2 months ago

Le jeu. 19 sept. 2024 à 15:50, pmarques-dev @.***> a écrit :

from the top of my head, I would verify that the code is actually being successfully loaded at address 0. Maybe the new SDK uses PIO's for something and the example is silently failing to load the code and then trying to use it anyway

— Reply to this email directly, view it on GitHub https://github.com/raspberrypi/pico-examples/issues/550#issuecomment-2361044785, or unsubscribe https://github.com/notifications/unsubscribe-auth/A5PZEDQORLUNXBQHSBWWO6LZXLJBJAVCNFSM6AAAAABON2UZHGVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDGNRRGA2DINZYGU . You are receiving this because you are subscribed to this thread.Message ID: @.***>

peterharperuk commented 2 months ago

It looks like the gpio is not initialised. The question is, why does this work on rp2040?

--- a/pio/quadrature_encoder/quadrature_encoder.pio
+++ b/pio/quadrature_encoder/quadrature_encoder.pio
@@ -99,6 +99,9 @@ increment_cont:
 static inline void quadrature_encoder_program_init(PIO pio, uint sm, uint pin, int max_step_rate)
 {
     pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false);
+    pio_gpio_init(pio, pin);
+    pio_gpio_init(pio, pin + 1);
pmarques-dev commented 2 months ago

if I remember correctly, to use the pins as inputs you don't have to multiplex them to the PIO, at least on the RP2040, so maybe there is a difference in behaviour there

peterharperuk commented 2 months ago

Yes - this is covered in the datasheet, section 9.3, it's a change from rp2040 to save power. Calling gpio_init also fixes the problem. I will submit a patch.

The pad reset state is different from RP2040, which only disables digital inputs on GPIOs 26 through 29 (as of version B2) and does not have isolation latches. Applications must enable the pad input (GPIO0.IE = 1) and disable pad isolation latches (GPIO0.ISO = 0) before using the pads for digital I/O. The gpio_set_function() SDK function performs these tasks automatically.

pmarques-dev commented 2 months ago

this in fact is in the SDK documentation, so it may need some clarification there as well: https://www.raspberrypi.com/documentation/pico-sdk/hardware.html#group_hardware_pio_1gafa244b1be8f53a329db9d26298e054bb "Note that this is not necessary for a state machine to be able to read the input value from a GPIO, but only for it to set the output value or output enable."

peterharperuk commented 2 months ago

Well arguably that's correct, you just need to have setup the gpio for input.

danielallstar commented 2 months ago

Thank you very much for looking into the problem and quickly finding a solution!

I did a test and now the pio quadrature_encoder example works as expected on RP2350.