26#define quadrature_encoder_wrap_target 15
27#define quadrature_encoder_wrap 28
29 static const uint16_t quadrature_encoder_program_instructions[] = {
64 static const struct pio_program quadrature_encoder_program = {
65 .instructions = quadrature_encoder_program_instructions,
70 static inline pio_sm_config quadrature_encoder_program_get_default_config(uint offset)
72 pio_sm_config c = pio_get_default_sm_config();
81 static inline void quadrature_encoder_program_init(PIO pio, uint sm, uint offset, uint pin,
int max_step_rate)
83 pio_sm_set_consecutive_pindirs(pio, sm, pin, 2,
false);
85 gpio_pull_down(pin + 1);
86 pio_sm_config c = quadrature_encoder_program_get_default_config(offset);
87 sm_config_set_in_pins(&c, pin);
88 sm_config_set_jmp_pin(&c, pin);
90 sm_config_set_in_shift(&c,
false,
false, 32);
92 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_NONE);
94 if (max_step_rate == 0)
96 sm_config_set_clkdiv(&c, 1.0);
101 float div = (float)clock_get_hz(clk_sys) / (14 * max_step_rate);
102 sm_config_set_clkdiv(&c, div);
104 pio_sm_init(pio, sm, offset, &c);
105 pio_sm_set_enabled(pio, sm,
true);
112 static inline void quadrature_encoder_request_count(PIO pio, uint sm)
116 static inline int32_t quadrature_encoder_fetch_count(PIO pio, uint sm)
118 while (pio_sm_is_rx_fifo_empty(pio, sm))
119 tight_loop_contents();
122 static inline int32_t quadrature_encoder_get_count(PIO pio, uint sm)
124 quadrature_encoder_request_count(pio, sm);
125 return quadrature_encoder_fetch_count(pio, sm);