PicoLowLevel
Loading...
Searching...
No Matches
quadrature_encoder.pio.h
Go to the documentation of this file.
1// -------------------------------------------------- //
2// This file is autogenerated by pioasm; do not edit! //
3// -------------------------------------------------- //
4
5#pragma once
6
7#if !PICO_NO_HARDWARE
8#include "hardware/pio.h"
9#endif
10
11// ------------------ //
12// quadrature_encoder //
13// ------------------ //
14
15#define quadrature_encoder_wrap_target 15
16#define quadrature_encoder_wrap 28
17
18static const uint16_t quadrature_encoder_program_instructions[] = {
19 0x000f, // 0: jmp 15
20 0x000e, // 1: jmp 14
21 0x001a, // 2: jmp 26
22 0x000f, // 3: jmp 15
23 0x001a, // 4: jmp 26
24 0x000f, // 5: jmp 15
25 0x000f, // 6: jmp 15
26 0x000e, // 7: jmp 14
27 0x000e, // 8: jmp 14
28 0x000f, // 9: jmp 15
29 0x000f, // 10: jmp 15
30 0x001a, // 11: jmp 26
31 0x000f, // 12: jmp 15
32 0x001a, // 13: jmp 26
33 0x008f, // 14: jmp y--, 15
34 // .wrap_target
35 0xe020, // 15: set x, 0
36 0x8080, // 16: pull noblock
37 0xa027, // 17: mov x, osr
38 0xa0e6, // 18: mov osr, isr
39 0x0036, // 19: jmp !x, 22
40 0xa0c2, // 20: mov isr, y
41 0x8020, // 21: push block
42 0xa0c3, // 22: mov isr, null
43 0x40e2, // 23: in osr, 2
44 0x4002, // 24: in pins, 2
45 0xa0a6, // 25: mov pc, isr
46 0xa02a, // 26: mov x, !y
47 0x005c, // 27: jmp x--, 28
48 0xa049, // 28: mov y, !x
49 // .wrap
50};
51
52#if !PICO_NO_HARDWARE
53static const struct pio_program quadrature_encoder_program = {
54 .instructions = quadrature_encoder_program_instructions,
55 .length = 29,
56 .origin = 0,
57};
58
59static inline pio_sm_config quadrature_encoder_program_get_default_config(uint offset) {
60 pio_sm_config c = pio_get_default_sm_config();
61 sm_config_set_wrap(&c, offset + quadrature_encoder_wrap_target, offset + quadrature_encoder_wrap);
62 return c;
63}
64
65#include "hardware/clocks.h"
66#include "hardware/gpio.h"
67// max_step_rate is used to lower the clock of the state machine to save power
68// if the application doesn't require a very high sampling rate. Passing zero
69// will set the clock to the maximum, which gives a max step rate of around
70// 8.9 Msteps/sec at 125MHz
71static inline void quadrature_encoder_program_init(PIO pio, uint sm, uint offset, uint pin, int max_step_rate)
72{
73 pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false);
74 gpio_pull_up(pin);
75 gpio_pull_up(pin + 1);
76 pio_sm_config c = quadrature_encoder_program_get_default_config(offset);
77 sm_config_set_in_pins(&c, pin); // for WAIT, IN
78 sm_config_set_jmp_pin(&c, pin); // for JMP
79 // shift to left, autopull disabled
80 sm_config_set_in_shift(&c, false, false, 32);
81 // don't join FIFO's
82 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_NONE);
83 // passing "0" as the sample frequency,
84 if (max_step_rate == 0) {
85 sm_config_set_clkdiv(&c, 1.0);
86 } else {
87 // one state machine loop takes at most 14 cycles
88 float div = (float)clock_get_hz(clk_sys) / (14 * max_step_rate);
89 sm_config_set_clkdiv(&c, div);
90 }
91 pio_sm_init(pio, sm, offset, &c);
92 pio_sm_set_enabled(pio, sm, true);
93}
94// When requesting the current count we may have to wait a few cycles (average
95// ~11 sysclk cycles) for the state machine to reply. If we are reading multiple
96// encoders, we may request them all in one go and then fetch them all, thus
97// avoiding doing the wait multiple times. If we are reading just one encoder,
98// we can use the "get_count" function to request and wait
99static inline void quadrature_encoder_request_count(PIO pio, uint sm)
100{
101 pio->txf[sm] = 1;
102}
103static inline int32_t quadrature_encoder_fetch_count(PIO pio, uint sm)
104{
105 while (pio_sm_is_rx_fifo_empty(pio, sm));
106 return pio->rxf[sm];
107}
108static inline int32_t quadrature_encoder_get_count(PIO pio, uint sm)
109{
110 quadrature_encoder_request_count(pio, sm);
111 return quadrature_encoder_fetch_count(pio, sm);
112}
113
114#endif
#define quadrature_encoder_wrap
Definition quadrature_encoder.pio.h:16
#define quadrature_encoder_wrap_target
Definition quadrature_encoder.pio.h:15