Skip to content

Commit c23dc24

Browse files
authored
Merge pull request #941 from makermelissa/main
Add PIO implementation of rotaryio for the Pi 5
2 parents 1393351 + a79ec18 commit c23dc24

File tree

4 files changed

+370
-126
lines changed

4 files changed

+370
-126
lines changed

LICENSES/BSD-3-Clause.txt

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
Copyright (c) 2023, Raspberry Pi Ltd.
2+
All rights reserved.
3+
4+
Redistribution and use in source and binary forms, with or without
5+
modification, are permitted provided that the following conditions are met:
6+
* Redistributions of source code must retain the above copyright
7+
notice, this list of conditions and the following disclaimer.
8+
* Redistributions in binary form must reproduce the above copyright
9+
notice, this list of conditions and the following disclaimer in the
10+
documentation and/or other materials provided with the distribution.
11+
* Neither the name of the copyright holder nor the
12+
names of its contributors may be used to endorse or promote products
13+
derived from this software without specific prior written permission.
14+
15+
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16+
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17+
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18+
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY
19+
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20+
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21+
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22+
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23+
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24+
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
# SPDX-License-Identifier: BSD-3-Clause
5+
"""
6+
`rotaryio` - Support for reading rotation sensors
7+
===========================================================
8+
See `CircuitPython:rotaryio` in CircuitPython for more details.
9+
10+
Raspberry Pi PIO implementation
11+
12+
* Author(s): Melissa LeBlanc-Williams
13+
"""
14+
15+
from __future__ import annotations
16+
import array
17+
import microcontroller
18+
19+
try:
20+
import adafruit_pioasm
21+
from adafruit_rp1pio import StateMachine
22+
except ImportError as exc:
23+
raise ImportError(
24+
"adafruit_pioasm and adafruit_rp1pio are required for this module"
25+
) from exc
26+
27+
_n_read = 17
28+
_program = adafruit_pioasm.Program(
29+
"""
30+
;
31+
; Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
32+
;
33+
; SPDX-License-Identifier: BSD-3-Clause
34+
;
35+
.pio_version 0 // only requires PIO version 0
36+
37+
.program quadrature_encoder
38+
39+
; the code must be loaded at address 0, because it uses computed jumps
40+
.origin 0
41+
42+
43+
; the code works by running a loop that continuously shifts the 2 phase pins into
44+
; ISR and looks at the lower 4 bits to do a computed jump to an instruction that
45+
; does the proper "do nothing" | "increment" | "decrement" action for that pin
46+
; state change (or no change)
47+
48+
; ISR holds the last state of the 2 pins during most of the code. The Y register
49+
; keeps the current encoder count and is incremented / decremented according to
50+
; the steps sampled
51+
52+
; the program keeps trying to write the current count to the RX FIFO without
53+
; blocking. To read the current count, the user code must drain the FIFO first
54+
; and wait for a fresh sample (takes ~4 SM cycles on average). The worst case
55+
; sampling loop takes 10 cycles, so this program is able to read step rates up
56+
; to sysclk / 10 (e.g., sysclk 125MHz, max step rate = 12.5 Msteps/sec)
57+
58+
; 00 state
59+
jmp update ; read 00
60+
jmp decrement ; read 01
61+
jmp increment ; read 10
62+
jmp update ; read 11
63+
64+
; 01 state
65+
jmp increment ; read 00
66+
jmp update ; read 01
67+
jmp update ; read 10
68+
jmp decrement ; read 11
69+
70+
; 10 state
71+
jmp decrement ; read 00
72+
jmp update ; read 01
73+
jmp update ; read 10
74+
jmp increment ; read 11
75+
76+
; to reduce code size, the last 2 states are implemented in place and become the
77+
; target for the other jumps
78+
79+
; 11 state
80+
jmp update ; read 00
81+
jmp increment ; read 01
82+
decrement:
83+
; note: the target of this instruction must be the next address, so that
84+
; the effect of the instruction does not depend on the value of Y. The
85+
; same is true for the "jmp y--" below. Basically "jmp y--, <next addr>"
86+
; is just a pure "decrement y" instruction, with no other side effects
87+
jmp y--, update ; read 10
88+
89+
; this is where the main loop starts
90+
.wrap_target
91+
update:
92+
mov isr, y ; read 11
93+
push noblock
94+
95+
sample_pins:
96+
; we shift into ISR the last state of the 2 input pins (now in OSR) and
97+
; the new state of the 2 pins, thus producing the 4 bit target for the
98+
; computed jump into the correct action for this state. Both the PUSH
99+
; above and the OUT below zero out the other bits in ISR
100+
out isr, 2
101+
in pins, 2
102+
103+
; save the state in the OSR, so that we can use ISR for other purposes
104+
mov osr, isr
105+
; jump to the correct state machine action
106+
mov pc, isr
107+
108+
; the PIO does not have a increment instruction, so to do that we do a
109+
; negate, decrement, negate sequence
110+
increment:
111+
mov y, ~y
112+
jmp y--, increment_cont
113+
increment_cont:
114+
mov y, ~y
115+
.wrap ; the .wrap here avoids one jump instruction and saves a cycle too
116+
"""
117+
)
118+
119+
_zero_y = adafruit_pioasm.assemble("set y 0")
120+
121+
122+
class IncrementalEncoder:
123+
"""
124+
IncrementalEncoder determines the relative rotational position based on two series of
125+
pulses. It assumes that the encoder’s common pin(s) are connected to ground,and enables
126+
pull-ups on pin_a and pin_b.
127+
128+
Create an IncrementalEncoder object associated with the given pins. It tracks the
129+
positional state of an incremental rotary encoder (also known as a quadrature encoder.)
130+
Position is relative to the position when the object is constructed.
131+
"""
132+
133+
def __init__(
134+
self, pin_a: microcontroller.Pin, pin_b: microcontroller.Pin, divisor: int = 4
135+
):
136+
"""Create an incremental encoder on pin_a and the next higher pin
137+
138+
Always operates in "x4" mode (one count per quadrature edge)
139+
140+
Assumes but does not check that pin_b is one above pin_a."""
141+
if pin_b is not None and pin_b.id != pin_a.id + 1:
142+
raise ValueError("pin_b must be None or one higher than pin_a")
143+
144+
try:
145+
self._sm = StateMachine(
146+
_program.assembled,
147+
frequency=0,
148+
init=_zero_y,
149+
first_in_pin=pin_a,
150+
in_pin_count=2,
151+
pull_in_pin_up=0x3,
152+
auto_push=True,
153+
push_threshold=32,
154+
in_shift_right=False,
155+
**_program.pio_kwargs,
156+
)
157+
except RuntimeError as e:
158+
if "(error -13)" in e.args[0]:
159+
raise RuntimeError(
160+
"This feature requires a rules file to allow access to PIO. See "
161+
"https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/"
162+
"using-neopixels-on-the-pi-5#updating-permissions-3189429"
163+
) from e
164+
raise
165+
self._buffer = array.array("i", [0] * _n_read)
166+
self.divisor = divisor
167+
self._position = 0
168+
169+
def deinit(self):
170+
"""Deinitializes the IncrementalEncoder and releases any hardware resources for reuse."""
171+
self._sm.deinit()
172+
173+
def __enter__(self) -> IncrementalEncoder:
174+
"""No-op used by Context Managers."""
175+
return self
176+
177+
def __exit__(self, _type, _value, _traceback):
178+
"""
179+
Automatically deinitializes when exiting a context. See
180+
:ref:`lifetime-and-contextmanagers` for more info.
181+
"""
182+
self.deinit()
183+
184+
@property
185+
def position(self):
186+
"""The current position in terms of pulses. The number of pulses per rotation is defined
187+
by the specific hardware and by the divisor."""
188+
self._sm.readinto(self._buffer) # read N stale values + 1 fresh value
189+
raw_position = self._buffer[-1]
190+
delta = int((raw_position - self._position * self.divisor) / self.divisor)
191+
self._position += delta
192+
return self._position
Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
# SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
"""
5+
`rotaryio` - Support for reading rotation sensors
6+
===========================================================
7+
See `CircuitPython:rotaryio` in CircuitPython for more details.
8+
9+
Generic Threading/DigitalIO implementation for Linux
10+
11+
* Author(s): Melissa LeBlanc-Williams
12+
"""
13+
14+
from __future__ import annotations
15+
import threading
16+
import microcontroller
17+
import digitalio
18+
19+
# Define the state transition table for the quadrature encoder
20+
transitions = [
21+
0, # 00 -> 00 no movement
22+
-1, # 00 -> 01 3/4 ccw (11 detent) or 1/4 ccw (00 at detent)
23+
+1, # 00 -> 10 3/4 cw or 1/4 cw
24+
0, # 00 -> 11 non-Gray-code transition
25+
+1, # 01 -> 00 2/4 or 4/4 cw
26+
0, # 01 -> 01 no movement
27+
0, # 01 -> 10 non-Gray-code transition
28+
-1, # 01 -> 11 4/4 or 2/4 ccw
29+
-1, # 10 -> 00 2/4 or 4/4 ccw
30+
0, # 10 -> 01 non-Gray-code transition
31+
0, # 10 -> 10 no movement
32+
+1, # 10 -> 11 4/4 or 2/4 cw
33+
0, # 11 -> 00 non-Gray-code transition
34+
+1, # 11 -> 01 1/4 or 3/4 cw
35+
-1, # 11 -> 10 1/4 or 3/4 ccw
36+
0, # 11 -> 11 no movement
37+
]
38+
39+
40+
class IncrementalEncoder:
41+
"""
42+
IncrementalEncoder determines the relative rotational position based on two series of
43+
pulses. It assumes that the encoder’s common pin(s) are connected to ground,and enables
44+
pull-ups on pin_a and pin_b.
45+
46+
Create an IncrementalEncoder object associated with the given pins. It tracks the
47+
positional state of an incremental rotary encoder (also known as a quadrature encoder.)
48+
Position is relative to the position when the object is constructed.
49+
"""
50+
51+
def __init__(
52+
self, pin_a: microcontroller.Pin, pin_b: microcontroller.Pin, divisor: int = 4
53+
):
54+
"""
55+
Create an IncrementalEncoder object associated with the given pins. It tracks the
56+
positional state of an incremental rotary encoder (also known as a quadrature encoder.)
57+
Position is relative to the position when the object is constructed.
58+
59+
:param microcontroller.Pin pin_a: The first pin connected to the encoder.
60+
:param microcontroller.Pin pin_b: The second pin connected to the encoder.
61+
:param int divisor: The number of pulses per encoder step. Default is 4.
62+
"""
63+
self._pin_a = digitalio.DigitalInOut(pin_a)
64+
self._pin_a.switch_to_input(pull=digitalio.Pull.UP)
65+
self._pin_b = digitalio.DigitalInOut(pin_b)
66+
self._pin_b.switch_to_input(pull=digitalio.Pull.UP)
67+
self._position = 0
68+
self._last_state = 0
69+
self._divisor = divisor
70+
self._sub_count = 0
71+
self._poll_thread = threading.Thread(target=self._polling_loop, daemon=True)
72+
self._poll_thread.start()
73+
74+
def deinit(self):
75+
"""Deinitializes the IncrementalEncoder and releases any hardware resources for reuse."""
76+
self._pin_a.deinit()
77+
self._pin_b.deinit()
78+
if self._poll_thread.is_alive():
79+
self._poll_thread.join()
80+
81+
def __enter__(self) -> IncrementalEncoder:
82+
"""No-op used by Context Managers."""
83+
return self
84+
85+
def __exit__(self, _type, _value, _traceback):
86+
"""
87+
Automatically deinitializes when exiting a context. See
88+
:ref:`lifetime-and-contextmanagers` for more info.
89+
"""
90+
self.deinit()
91+
92+
@property
93+
def divisor(self) -> int:
94+
"""The divisor of the quadrature signal. Use 1 for encoders without detents, or encoders
95+
with 4 detents per cycle. Use 2 for encoders with 2 detents per cycle. Use 4 for encoders
96+
with 1 detent per cycle."""
97+
return self._divisor
98+
99+
@divisor.setter
100+
def divisor(self, value: int):
101+
self._divisor = value
102+
103+
@property
104+
def position(self) -> int:
105+
"""The current position in terms of pulses. The number of pulses per rotation is defined
106+
by the specific hardware and by the divisor."""
107+
return self._position
108+
109+
@position.setter
110+
def position(self, value: int):
111+
self._position = value
112+
113+
def _get_pin_state(self) -> int:
114+
"""Returns the current state of the pins."""
115+
return self._pin_a.value << 1 | self._pin_b.value
116+
117+
def _polling_loop(self):
118+
while True:
119+
self._poll_encoder()
120+
121+
def _poll_encoder(self):
122+
# Check the state of the pins
123+
# if either pin has changed, update the state
124+
new_state = self._get_pin_state()
125+
if new_state != self._last_state:
126+
self._state_update(new_state)
127+
self._last_state = new_state
128+
129+
def _state_update(self, new_state: int):
130+
new_state &= 3
131+
index = self._last_state << 2 | new_state
132+
sub_increment = transitions[index]
133+
self._sub_count += sub_increment
134+
if self._sub_count >= self._divisor:
135+
self._position += 1
136+
self._sub_count = 0
137+
elif self._sub_count <= -self._divisor:
138+
self._position -= 1
139+
self._sub_count = 0

0 commit comments

Comments
 (0)