Content-Length: 274431 | pFad | https://github.com/adafruit/circuitpython/issues/10157

48 rotaryio freezes when creating 6 IncrementalEncoder on RP2040 · Issue #10157 · adafruit/circuitpython · GitHub
Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

rotaryio freezes when creating 6 IncrementalEncoder on RP2040 #10157

Closed
Neradoc opened this issue Mar 20, 2025 · 6 comments
Closed

rotaryio freezes when creating 6 IncrementalEncoder on RP2040 #10157

Neradoc opened this issue Mar 20, 2025 · 6 comments
Labels
Milestone

Comments

@Neradoc
Copy link

Neradoc commented Mar 20, 2025

CircuitPython version and board name

Adafruit CircuitPython 9.2.5 on 2025-03-19; Raspberry Pi Pico with rp2040

Code/REPL

import board
import rotaryio

encoder_pins = [
    (board.GP0, board.GP1),
    (board.GP2, board.GP3),
    (board.GP4, board.GP5),
    (board.GP6, board.GP7),
    (board.GP8, board.GP9),
    (board.GP10, board.GP11),
    (board.GP12, board.GP13),
    (board.GP14, board.GP15),
    (board.GP16, board.GP17),
    (board.GP18, board.GP19),
]

encoders = []

print("setup encoders")
# Initialize encoders
for pin_a, pin_b in encoder_pins:
    print(f"Encoder at {pin_a}, {pin_b}")
    encoder = rotaryio.IncrementalEncoder(pin_a, pin_b)
    encoders.append(encoder)

Behavior

Prints out:

code.py output:
setup encoders
Encoder at board.GP0, board.GP1
Encoder at board.GP2, board.GP3
Encoder at board.GP4, board.GP5
Encoder at board.GP6, board.GP7
Encoder at board.GP8, board.GP9
Encoder at board.GP10, board.GP11

And freezes there, ctrl-C doesn't stop the code.

Description

I would expect an exception being raised when using too many encoders.
Or is it supposed to support like 8 encoders ?
Seems like a followup to #10024

Additional information

No response

@Neradoc Neradoc added the bug label Mar 20, 2025
@Neradoc Neradoc changed the title rotaryio freezes when creating too many IncrementalEncoder on RP2040 rotaryio freezes when creating 6 IncrementalEncoder on RP2040 Mar 20, 2025
@todbot
Copy link

todbot commented Mar 20, 2025

CircuitPython RP2040 used to support 8 encoders (but nothing else that required PIO). Because this project used to work: https://github.com/todbot/pico8enc

@todbot
Copy link

todbot commented Mar 20, 2025

(btw if any CircuitPython dev wants a pico8enc PCB mailed to them, let me know)

@dhalbert dhalbert added this to the 9.x.x milestone Mar 22, 2025
@eightycc
Copy link
Collaborator

eightycc commented Mar 25, 2025

With a debug build and debugprobe attached, seeing a hang-up in _transfer() of common-hal/rp2pio/StateMachine.c at line 978:[ while (rx_remaining || tx_remaining) {](https://github.com/eightycc/circuitpython/blob/9825f62f96994db556fbc18bfab4f951347a2c37/ports/raspberrypi/common-hal/rp2pio/StateMachine.c#L962-L990)

The pertinent part of the gdb backtrace:

#0  0x1002d8ac in _transfer (self=self@entry=0x200111e4, data_out=data_out@entry=0x0, out_len=out_len@entry=0, 
    out_stride_in_bytes=out_stride_in_bytes@entry=0 '\000', data_in=data_in@entry=0x20040dd7 "", in_len=<optimized out>, in_len@entry=1, 
    in_stride_in_bytes=<optimized out>, in_stride_in_bytes@entry=1 '\001', swap_out=<optimized out>, swap_out@entry=false, 
    swap_in=<optimized out>, swap_in@entry=false) at common-hal/rp2pio/StateMachine.c:978
#1  0x1002e3fe in common_hal_rp2pio_statemachine_readinto (self=self@entry=0x200111e4, data=data@entry=0x20040dd7 "", len=len@entry=1, 
    stride_in_bytes=stride_in_bytes@entry=1 '\001', swap=swap@entry=false) at common-hal/rp2pio/StateMachine.c:1021
#2  0x1003c674 in common_hal_rotaryio_incrementalencoder_construct (self=self@entry=0x200111e0, pin_a=pin_a@entry=0x100aaa2c <pin_GPIO10>, 
    pin_b=pin_b@entry=0x100aaa24 <pin_GPIO11>) at common-hal/rotaryio/IncrementalEncoder.c:85
#3  0x10048d78 in rotaryio_incrementalencoder_make_new (type=<optimized out>, n_args=<optimized out>, n_kw=<optimized out>, 
    all_args=<optimized out>) at ../../shared-bindings/rotaryio/IncrementalEncoder.c:69

We're looping because self->state_machine is NULL. Clearly that's wrong.

@eightycc
Copy link
Collaborator

eightycc commented Mar 25, 2025

rp2pio_statemachine_construct() is silently failing by setting self->state_machine to 0xffffffff for the 5th encoder.

@eightycc
Copy link
Collaborator

eightycc commented Mar 25, 2025

Incorrect processor dependent includes of iobank0.h and platform_defs.h in StateMachine.c. This would keep RP2350 boards from using the 3rd PIO. There are other instances of this in the RP2 port. Will write new issue for this.

StateMachine.c:use_existing_program() storing return from SDK function pio_claim_unused_sm() in unsigned int but later testing for <=0. Fixing this uncovers another bug where common_hal_rotaryio_incrementalencoder_deinit() causes an SDK assert after 9th IncrementalEncoder constructor fails.

@eightycc
Copy link
Collaborator

Now working as expected, throwing "All state machines in use" on 9th constructor:

setup encoders
Encoder at board.GP2, board.GP3
Encoder at board.GP4, board.GP5
Encoder at board.GP6, board.GP7
Encoder at board.GP8, board.GP9
Encoder at board.GP10, board.GP11
Encoder at board.GP12, board.GP13
Encoder at board.GP14, board.GP15
Encoder at board.GP16, board.GP17
Encoder at board.GP18, board.GP19
Traceback (most recent call last):
  File "code.py", line 25, in <module>
RuntimeError: All state machines in use
0;🐍25@code.py RuntimeError | 9.2.6-dirty
Code done running.

(I removed the first encoder in the list so I could use the UART debug console)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

5 participants








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: https://github.com/adafruit/circuitpython/issues/10157

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy