UdonLibrary 1.0.0
機械システム研究部 C++ ライブラリ
読み取り中…
検索中…
一致する文字列を見つけられません
QuadratureEncoder.pio.hpp
[詳解]
1// -------------------------------------------------- //
2// This file is autogenerated by pioasm; do not edit! //
3// -------------------------------------------------- //
4
5#pragma once
6
7#if !PICO_NO_HARDWARE
8# include "pico/stdlib.h"
9# include "hardware/timer.h"
10# include "hardware/pio.h"
11# include "hardware/clocks.h"
12# include "hardware/gpio.h"
13#endif
14
15namespace Udon
16{
17 namespace Pio
18 {
19 namespace Encoder
20 {
21
22 // ------------------ //
23 // quadrature_encoder //
24 // ------------------ //
25
26#define quadrature_encoder_wrap_target 15
27#define quadrature_encoder_wrap 28
28
29 static const uint16_t quadrature_encoder_program_instructions[] = {
30 0x000f, // 0: jmp 15
31 0x000e, // 1: jmp 14
32 0x001a, // 2: jmp 26
33 0x000f, // 3: jmp 15
34 0x001a, // 4: jmp 26
35 0x000f, // 5: jmp 15
36 0x000f, // 6: jmp 15
37 0x000e, // 7: jmp 14
38 0x000e, // 8: jmp 14
39 0x000f, // 9: jmp 15
40 0x000f, // 10: jmp 15
41 0x001a, // 11: jmp 26
42 0x000f, // 12: jmp 15
43 0x001a, // 13: jmp 26
44 0x008f, // 14: jmp y--, 15
45 // .wrap_target
46 0xe020, // 15: set x, 0
47 0x8080, // 16: pull noblock
48 0xa027, // 17: mov x, osr
49 0xa0e6, // 18: mov osr, isr
50 0x0036, // 19: jmp !x, 22
51 0xa0c2, // 20: mov isr, y
52 0x8020, // 21: push block
53 0xa0c3, // 22: mov isr, null
54 0x40e2, // 23: in osr, 2
55 0x4002, // 24: in pins, 2
56 0xa0a6, // 25: mov pc, isr
57 0xa02a, // 26: mov x, !y
58 0x005c, // 27: jmp x--, 28
59 0xa049, // 28: mov y, !x
60 // .wrap
61 };
62
63#if !PICO_NO_HARDWARE
64 static const struct pio_program quadrature_encoder_program = {
65 .instructions = quadrature_encoder_program_instructions,
66 .length = 29,
67 .origin = 0,
68 };
69
70 static inline pio_sm_config quadrature_encoder_program_get_default_config(uint offset)
71 {
72 pio_sm_config c = pio_get_default_sm_config();
73 sm_config_set_wrap(&c, offset + quadrature_encoder_wrap_target, offset + quadrature_encoder_wrap);
74 return c;
75 }
76
77 // max_step_rate is used to lower the clock of the state machine to save power
78 // if the application doesn't require a very high sampling rate. Passing zero
79 // will set the clock to the maximum, which gives a max step rate of around
80 // 8.9 Msteps/sec at 125MHz
81 static inline void quadrature_encoder_program_init(PIO pio, uint sm, uint offset, uint pin, int max_step_rate)
82 {
83 pio_sm_set_consecutive_pindirs(pio, sm, pin, 2, false);
84 gpio_pull_down(pin);
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); // for WAIT, IN
88 sm_config_set_jmp_pin(&c, pin); // for JMP
89 // shift to left, autopull disabled
90 sm_config_set_in_shift(&c, false, false, 32);
91 // don't join FIFO's
92 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_NONE);
93 // passing "0" as the sample frequency,
94 if (max_step_rate == 0)
95 {
96 sm_config_set_clkdiv(&c, 1.0);
97 }
98 else
99 {
100 // one state machine loop takes at most 14 cycles
101 float div = (float)clock_get_hz(clk_sys) / (14 * max_step_rate);
102 sm_config_set_clkdiv(&c, div);
103 }
104 pio_sm_init(pio, sm, offset, &c);
105 pio_sm_set_enabled(pio, sm, true);
106 }
107 // When requesting the current count we may have to wait a few cycles (average
108 // ~11 sysclk cycles) for the state machine to reply. If we are reading multiple
109 // encoders, we may request them all in one go and then fetch them all, thus
110 // avoiding doing the wait multiple times. If we are reading just one encoder,
111 // we can use the "get_count" function to request and wait
112 static inline void quadrature_encoder_request_count(PIO pio, uint sm)
113 {
114 pio->txf[sm] = 1;
115 }
116 static inline int32_t quadrature_encoder_fetch_count(PIO pio, uint sm)
117 {
118 while (pio_sm_is_rx_fifo_empty(pio, sm))
119 tight_loop_contents();
120 return pio->rxf[sm];
121 }
122 static inline int32_t quadrature_encoder_get_count(PIO pio, uint sm)
123 {
124 quadrature_encoder_request_count(pio, sm);
125 return quadrature_encoder_fetch_count(pio, sm);
126 }
127 }
128 }
129}
130
131# undef quadrature_encoder_wrap_target
132# undef quadrature_encoder_wrap
133
134#endif
#define quadrature_encoder_wrap
Definition QuadratureEncoder.pio.hpp:27
#define quadrature_encoder_wrap_target
Definition QuadratureEncoder.pio.hpp:26
Definition Bit.hpp:12