blob: b2a0b82f282488e3ebb157aa46ab86b59f4e65a0 [file] [log] [blame]
Ravago Jonesb83957c2021-12-29 19:57:34 -08001;
2; Copyright (c) 2021 pmarques-dev @ github
3;
4; SPDX-License-Identifier: BSD-3-Clause
5;
6
7.program quadrature_encoder
8
9; this code must be loaded into address 0, but at 29 instructions, it probably
10; wouldn't be able to share space with other programs anyway
11.origin 0
12
13
14; the code works by running a loop that continuously shifts the 2 phase pins into
15; ISR and looks at the lower 4 bits to do a computed jump to an instruction that
16; does the proper "do nothing" | "increment" | "decrement" action for that pin
17; state change (or no change)
18
19; ISR holds the last state of the 2 pins during most of the code. The Y register
20; keeps the current encoder count and is incremented / decremented according to
21; the steps sampled
22
23; writing any non zero value to the TX FIFO makes the state machine push the
24; current count to RX FIFO between 6 to 18 clocks afterwards. The worst case
25; sampling loop takes 14 cycles, so this program is able to read step rates up
26; to sysclk / 14 (e.g., sysclk 125MHz, max step rate = 8.9 Msteps/sec)
27
28
29; 00 state
30 JMP update ; read 00
31 JMP decrement ; read 01
32 JMP increment ; read 10
33 JMP update ; read 11
34
35; 01 state
36 JMP increment ; read 00
37 JMP update ; read 01
38 JMP update ; read 10
39 JMP decrement ; read 11
40
41; 10 state
42 JMP decrement ; read 00
43 JMP update ; read 01
44 JMP update ; read 10
45 JMP increment ; read 11
46
47; to reduce code size, the last 2 states are implemented in place and become the
48; target for the other jumps
49
50; 11 state
51 JMP update ; read 00
52 JMP increment ; read 01
53decrement:
54 ; note: the target of this instruction must be the next address, so that
55 ; the effect of the instruction does not depend on the value of Y. The
56 ; same is true for the "JMP X--" below. Basically "JMP Y--, <next addr>"
57 ; is just a pure "decrement Y" instruction, with no other side effects
58 JMP Y--, update ; read 10
59
60 ; this is where the main loop starts
61.wrap_target
62update:
63 ; we start by checking the TX FIFO to see if the main code is asking for
64 ; the current count after the PULL noblock, OSR will have either 0 if
65 ; there was nothing or the value that was there
66 SET X, 0
67 PULL noblock
68
69 ; since there are not many free registers, and PULL is done into OSR, we
70 ; have to do some juggling to avoid losing the state information and
71 ; still place the values where we need them
72 MOV X, OSR
73 MOV OSR, ISR
74
75 ; the main code did not ask for the count, so just go to "sample_pins"
76 JMP !X, sample_pins
77
78 ; if it did ask for the count, then we push it
79 MOV ISR, Y ; we trash ISR, but we already have a copy in OSR
80 PUSH
81
82sample_pins:
83 ; we shift into ISR the last state of the 2 input pins (now in OSR) and
84 ; the new state of the 2 pins, thus producing the 4 bit target for the
85 ; computed jump into the correct action for this state
86 MOV ISR, NULL
87 IN OSR, 2
88 IN PINS, 2
89 MOV PC, ISR
90
91 ; the PIO does not have a increment instruction, so to do that we do a
92 ; negate, decrement, negate sequence
93increment:
94 MOV X, !Y
95 JMP X--, increment_cont
96increment_cont:
97 MOV Y, !X
98.wrap ; the .wrap here avoids one jump instruction and saves a cycle too
99
100
101
102% c-sdk {
103
104#include "hardware/clocks.h"
105#include "hardware/gpio.h"
106
107// max_step_rate is used to lower the clock of the state machine to save power
108// if the application doesn't require a very high sampling rate. Passing zero
109// will set the clock to the maximum, which gives a max step rate of around
110// 8.9 Msteps/sec at 125MHz
111
112static inline void quadrature_encoder_program_init(PIO pio, uint sm, uint offset, uint pin, int max_step_rate)
113{
114 pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false);
115 pio_gpio_init(pio, pin);
116 gpio_pull_up(pin);
117
118 pio_sm_config c = quadrature_encoder_program_get_default_config(offset);
119 sm_config_set_in_pins(&c, pin); // for WAIT, IN
120 sm_config_set_jmp_pin(&c, pin); // for JMP
121 // shift to left, autopull disabled
122 sm_config_set_in_shift(&c, false, false, 32);
123 // don't join FIFO's
124 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_NONE);
125
126 // passing "0" as the sample frequency,
127 if (max_step_rate == 0) {
128 sm_config_set_clkdiv(&c, 1.0);
129 } else {
130 // one state machine loop takes at most 14 cycles
131 float div = (float)clock_get_hz(clk_sys) / (14 * max_step_rate);
132 sm_config_set_clkdiv(&c, div);
133 }
134
135 pio_sm_init(pio, sm, offset, &c);
136 pio_sm_set_enabled(pio, sm, true);
137}
138
139
140// When requesting the current count we may have to wait a few cycles (average
141// ~11 sysclk cycles) for the state machine to reply. If we are reading multiple
142// encoders, we may request them all in one go and then fetch them all, thus
143// avoiding doing the wait multiple times. If we are reading just one encoder,
144// we can use the "get_count" function to request and wait
145
146static inline void quadrature_encoder_request_count(PIO pio, uint sm)
147{
148 pio->txf[sm] = 1;
149}
150
151static inline int32_t quadrature_encoder_fetch_count(PIO pio, uint sm)
152{
153 while (pio_sm_is_rx_fifo_empty(pio, sm))
154 tight_loop_contents();
155 return pio->rxf[sm];
156}
157
158static inline int32_t quadrature_encoder_get_count(PIO pio, uint sm)
159{
160 quadrature_encoder_request_count(pio, sm);
161 return quadrature_encoder_fetch_count(pio, sm);
162}
163
164%}
165