2021-02-10 16:25:25 +00:00
|
|
|
; --------------------------------------------------
|
|
|
|
; Quadrature Encoder reader using PIO
|
|
|
|
; by Christopher (@ZodiusInfuser) Parrott
|
|
|
|
; --------------------------------------------------
|
|
|
|
;
|
2022-04-13 20:12:44 +01:00
|
|
|
; Watches any two pins (i.e. do not need to be
|
|
|
|
; consecutive) for when their state changes, and
|
|
|
|
; pushes that new state along with the old state,
|
|
|
|
; and time since the last change.
|
2021-02-10 16:25:25 +00:00
|
|
|
;
|
|
|
|
; - X is used for storing the last state
|
2022-04-13 20:12:44 +01:00
|
|
|
; - Y is used as a general scratch register
|
|
|
|
; and for storing the current state
|
2021-02-10 16:25:25 +00:00
|
|
|
; - OSR is used for storing the state-change timer
|
|
|
|
;
|
2022-04-13 20:12:44 +01:00
|
|
|
; After data is pushed into the system, a long delay
|
|
|
|
; takes place as a form of switch debounce to deal
|
|
|
|
; with rotary encoder dials. This is currently set
|
|
|
|
; to 500 cycles, but can be changed using the debounce
|
|
|
|
; constants below, as well as adjusting the frequency
|
|
|
|
; the PIO state machine runs at. E.g. a freq_divider
|
|
|
|
; of 250 gives a 1ms debounce.
|
2021-02-10 16:25:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
; Debounce Constants
|
|
|
|
; --------------------------------------------------
|
|
|
|
.define SET_CYCLES 10
|
|
|
|
.define ITERATIONS 30
|
2022-04-13 20:12:44 +01:00
|
|
|
.define JMP_CYCLES 16
|
2021-02-10 16:25:25 +00:00
|
|
|
.define public ENC_DEBOUNCE_CYCLES (SET_CYCLES + (JMP_CYCLES * ITERATIONS))
|
|
|
|
|
2022-04-13 20:12:44 +01:00
|
|
|
; Ensure that ENC_DEBOUNCE_CYCLES is a multiple of the
|
|
|
|
; number of cycles the wrap takes, which is currently
|
|
|
|
; 10 cycles, otherwise timing may be inaccurate
|
2021-02-10 16:25:25 +00:00
|
|
|
|
|
|
|
|
|
|
|
; Encoder Program
|
|
|
|
; --------------------------------------------------
|
|
|
|
.program encoder
|
|
|
|
|
|
|
|
.wrap_target
|
|
|
|
loop:
|
2022-04-13 20:12:44 +01:00
|
|
|
; Copy the state-change timer from OSR,
|
|
|
|
; decrement it, and save it back
|
2021-02-10 16:25:25 +00:00
|
|
|
mov y, osr
|
|
|
|
jmp y-- osr_dec
|
|
|
|
osr_dec:
|
|
|
|
mov osr, y
|
|
|
|
; takes 3 cycles
|
|
|
|
|
2022-04-13 20:12:44 +01:00
|
|
|
; Read the state of both encoder pins and check
|
|
|
|
; if they are different from the last state
|
2021-02-10 16:25:25 +00:00
|
|
|
jmp pin encA_was_high
|
|
|
|
mov isr, null
|
|
|
|
jmp read_encB
|
|
|
|
encA_was_high:
|
|
|
|
set y, 1
|
|
|
|
mov isr, y
|
|
|
|
read_encB:
|
|
|
|
in pins, 1
|
|
|
|
mov y, isr
|
|
|
|
jmp x!=y state_changed [1]
|
|
|
|
; takes 7 cycles on both paths
|
|
|
|
.wrap
|
|
|
|
|
|
|
|
state_changed:
|
2022-04-13 20:12:44 +01:00
|
|
|
; Put the last state and the timer value into
|
|
|
|
; ISR alongside the current state, and push that
|
|
|
|
; state to the system. Then override the last
|
|
|
|
; state with the current state
|
2021-02-10 16:25:25 +00:00
|
|
|
in x, 2
|
2022-04-13 20:12:44 +01:00
|
|
|
mov x, ~osr ; invert the timer value to give
|
|
|
|
; a sensible value to the system
|
2021-02-10 16:25:25 +00:00
|
|
|
in x, 28
|
|
|
|
push noblock ; this also clears isr
|
|
|
|
mov x, y
|
|
|
|
|
|
|
|
; Perform a delay to debounce switch inputs
|
|
|
|
set y, (ITERATIONS - 1) [SET_CYCLES - 1]
|
|
|
|
debounce_loop:
|
|
|
|
jmp y-- debounce_loop [JMP_CYCLES - 1]
|
|
|
|
|
2022-04-13 20:12:44 +01:00
|
|
|
; Initialise the timer, as an inverse, and decrement
|
|
|
|
; it to account for the time this setup takes
|
2021-02-10 16:25:25 +00:00
|
|
|
mov y, ~null
|
|
|
|
jmp y-- y_dec
|
|
|
|
y_dec:
|
|
|
|
mov osr, y
|
|
|
|
jmp loop [1]
|
|
|
|
;takes 10 cycles, not counting whatever the debounce adds
|
|
|
|
|
|
|
|
|
|
|
|
; Initialisation Code
|
|
|
|
; --------------------------------------------------
|
|
|
|
% c-sdk {
|
|
|
|
#include "hardware/clocks.h"
|
|
|
|
|
|
|
|
static const uint8_t ENC_LOOP_CYCLES = encoder_wrap - encoder_wrap_target;
|
|
|
|
|
2022-04-13 20:12:44 +01:00
|
|
|
// The time that the debounce takes, as the number of wrap loops that the debounce is equivalent to
|
2021-02-10 16:25:25 +00:00
|
|
|
static const uint8_t ENC_DEBOUNCE_TIME = ENC_DEBOUNCE_CYCLES / ENC_LOOP_CYCLES;
|
|
|
|
%}
|