1 # SPDX-FileCopyrightText: 2025 Melissa LeBlanc-Williams for Adafruit Industries
3 # SPDX-License-Identifier: MIT
5 `rotaryio` - Support for reading rotation sensors
6 ===========================================================
7 See `CircuitPython:rotaryio` in CircuitPython for more details.
9 Raspberry Pi PIO implementation
11 * Author(s): Melissa LeBlanc-Williams
14 from __future__ import annotations
15 import microcontroller
18 import adafruit_pioasm
19 from adafruit_rp1pio import StateMachine
21 raise("adafruit_pioasm and adafruit_rp1pio are required for this module")
24 _program = adafruit_pioasm.Program("""
26 ; Copyright (c) 2023 Raspberry Pi (Trading) Ltd.
28 ; SPDX-License-Identifier: BSD-3-Clause
30 .pio_version 0 // only requires PIO version 0
32 .program quadrature_encoder
34 ; the code must be loaded at address 0, because it uses computed jumps
38 ; the code works by running a loop that continuously shifts the 2 phase pins into
39 ; ISR and looks at the lower 4 bits to do a computed jump to an instruction that
40 ; does the proper "do nothing" | "increment" | "decrement" action for that pin
41 ; state change (or no change)
43 ; ISR holds the last state of the 2 pins during most of the code. The Y register
44 ; keeps the current encoder count and is incremented / decremented according to
47 ; the program keeps trying to write the current count to the RX FIFO without
48 ; blocking. To read the current count, the user code must drain the FIFO first
49 ; and wait for a fresh sample (takes ~4 SM cycles on average). The worst case
50 ; sampling loop takes 10 cycles, so this program is able to read step rates up
51 ; to sysclk / 10 (e.g., sysclk 125MHz, max step rate = 12.5 Msteps/sec)
55 jmp decrement ; read 01
56 jmp increment ; read 10
60 jmp increment ; read 00
63 jmp decrement ; read 11
66 jmp decrement ; read 00
69 jmp increment ; read 11
71 ; to reduce code size, the last 2 states are implemented in place and become the
72 ; target for the other jumps
76 jmp increment ; read 01
78 ; note: the target of this instruction must be the next address, so that
79 ; the effect of the instruction does not depend on the value of Y. The
80 ; same is true for the "jmp y--" below. Basically "jmp y--, <next addr>"
81 ; is just a pure "decrement y" instruction, with no other side effects
82 jmp y--, update ; read 10
84 ; this is where the main loop starts
91 ; we shift into ISR the last state of the 2 input pins (now in OSR) and
92 ; the new state of the 2 pins, thus producing the 4 bit target for the
93 ; computed jump into the correct action for this state. Both the PUSH
94 ; above and the OUT below zero out the other bits in ISR
98 ; save the state in the OSR, so that we can use ISR for other purposes
100 ; jump to the correct state machine action
103 ; the PIO does not have a increment instruction, so to do that we do a
104 ; negate, decrement, negate sequence
107 jmp y--, increment_cont
110 .wrap ; the .wrap here avoids one jump instruction and saves a cycle too
113 _zero_y = adafruit_pioasm.assemble("set y 0")
115 class IncrementalEncoder:
117 IncrementalEncoder determines the relative rotational position based on two series of
118 pulses. It assumes that the encoder’s common pin(s) are connected to ground,and enables
119 pull-ups on pin_a and pin_b.
121 Create an IncrementalEncoder object associated with the given pins. It tracks the
122 positional state of an incremental rotary encoder (also known as a quadrature encoder.)
123 Position is relative to the position when the object is constructed.
127 self, pin_a: microcontroller.Pin, pin_b: microcontroller.Pin, divisor: int = 4
129 """Create an incremental encoder on pin_a and the next higher pin
131 Always operates in "x4" mode (one count per quadrature edge)
133 Assumes but does not check that pin_b is one above pin_a."""
134 #if pin_b is not None and pin_b.id != pin_a.id + 1:
135 # raise ValueError("pin_b must be None or one higher than pin_a")
138 self._sm = StateMachine(
147 in_shift_right=False,
148 **_program.pio_kwargs
150 except RuntimeError as e:
151 if "(error -13)" in e.args[0]:
153 "This feature requires a rules file to allow access to PIO. See "
154 "https://learn.adafruit.com/circuitpython-on-raspberrypi-linux/using-neopixels-on-the-pi-5#updating-permissions-3189429"
157 self._buffer = array.array('i',[0] * _n_read)
158 self.divisor = divisor
162 """Deinitializes the IncrementalEncoder and releases any hardware resources for reuse."""
165 def __enter__(self) -> IncrementalEncoder:
166 """No-op used by Context Managers."""
169 def __exit__(self, _type, _value, _traceback):
171 Automatically deinitializes when exiting a context. See
172 :ref:`lifetime-and-contextmanagers` for more info.
178 self._sm.readinto(self._buffer) # read N stale values + 1 fresh value
179 raw_position = self._buffer[-1]
180 delta = int((raw_position - self._position * self.divisor) / self.divisor)
181 self._position += delta
182 return self._position
185 if __name__ == '__main__':
187 # D17/D18 on header pins 11/12
188 # GND on header pin 6/9
189 # +5V on header pins 2/4
190 q = IncrementalEncoder(board.D17)
191 old_position = q.position
193 position = q.position
194 if position != old_position:
195 delta = position - old_position
196 print(f"{position:8d} {delta=}")
197 old_position = position