Skip to content

Commit caed4c4

Browse files
committed
Slight tweaks to encoder PIO
1 parent 65446cb commit caed4c4

File tree

4 files changed

+45
-39
lines changed

4 files changed

+45
-39
lines changed

include/encoder2.pio.h

+27-25
Original file line numberDiff line numberDiff line change
@@ -20,38 +20,38 @@
2020

2121
static const uint16_t encoder2_program_instructions[] = {
2222
0x0b10, // 0: jmp 16 [11]
23-
0x0315, // 1: jmp 21 [3]
24-
0x0318, // 2: jmp 24 [3]
23+
0x0315, // 1: jmp 21 [3]
24+
0x0318, // 2: jmp 24 [3]
2525
0x0b11, // 3: jmp 17 [11]
26-
0x0318, // 4: jmp 24 [3]
26+
0x0318, // 4: jmp 24 [3]
2727
0x0b10, // 5: jmp 16 [11]
2828
0x0b11, // 6: jmp 17 [11]
29-
0x0315, // 7: jmp 21 [3]
30-
0x0315, // 8: jmp 21 [3]
29+
0x0315, // 7: jmp 21 [3]
30+
0x0315, // 8: jmp 21 [3]
3131
0x0b11, // 9: jmp 17 [11]
3232
0x0b10, // 10: jmp 16 [11]
33-
0x0318, // 11: jmp 24 [3]
33+
0x0318, // 11: jmp 24 [3]
3434
0x0b11, // 12: jmp 17 [11]
35-
0x0318, // 13: jmp 24 [3]
36-
0x0315, // 14: jmp 21 [3]
35+
0x0318, // 13: jmp 24 [3]
36+
0x0315, // 14: jmp 21 [3]
3737
0x0b10, // 15: jmp 16 [11]
3838
// .wrap_target
39-
0x0052, // 16: jmp x--, 18
40-
0xe020, // 17: set x, 0
41-
0x4002, // 18: in pins, 2
42-
0xa0e6, // 19: mov osr, isr
43-
0x60a4, // 20: out pc, 4
44-
0x0079, // 21: jmp !y, 25
45-
0xa04a, // 22: mov y, !y
46-
0x0019, // 23: jmp 25
47-
0x0076, // 24: jmp !y, 22
48-
0xa0e6, // 25: mov osr, isr
49-
0xa0c9, // 26: mov isr, !x
50-
0x4041, // 27: in y, 1
51-
0x8000, // 28: push noblock
52-
0x60c2, // 29: out isr, 2
53-
0xa0eb, // 30: mov osr, !null
54-
0x603f, // 31: out x, 31
39+
0x0052, // 16: jmp x--, 18
40+
0xe020, // 17: set x, 0
41+
0x4002, // 18: in pins, 2
42+
0xa0e6, // 19: mov osr, isr
43+
0x60a4, // 20: out pc, 4
44+
0x0079, // 21: jmp !y, 25
45+
0xa04a, // 22: mov y, !y
46+
0x0019, // 23: jmp 25
47+
0x0076, // 24: jmp !y, 22
48+
0xa0e6, // 25: mov osr, isr
49+
0xa0c9, // 26: mov isr, !x
50+
0x4041, // 27: in y, 1
51+
0x8000, // 28: push noblock
52+
0x60c2, // 29: out isr, 2
53+
0xa0eb, // 30: mov osr, !null
54+
0x603f, // 31: out x, 31
5555
// .wrap
5656
};
5757

@@ -69,6 +69,9 @@ static inline pio_sm_config encoder2_program_get_default_config(uint offset) {
6969
}
7070

7171
static inline void encoder2_program_init(PIO pio, uint sm, uint offset, uint base_pin) {
72+
pio_gpio_init(pio, base_pin);
73+
pio_gpio_init(pio, base_pin+1);
74+
pio_sm_set_consecutive_pindirs(pio, sm, base_pin, 2, false);
7275
pio_sm_config c = encoder2_program_get_default_config(offset);
7376
sm_config_set_in_pins(&c, base_pin);
7477
sm_config_set_in_shift(&c, false, false, 32);
@@ -79,4 +82,3 @@ static inline void encoder2_program_init(PIO pio, uint sm, uint offset, uint bas
7982
}
8083

8184
#endif
82-

platformio.ini

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[env]
2-
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#088a074ba98e091f9abc48db91b99bedf908332b
2+
platform = https://github.com/maxgerhardt/platform-raspberrypi.git#39b90392af50585e429941bd2561a91949a2ba46
33
framework = arduino
44
board_build.core = earlephilhower
55
board_build.filesystem_size = 0.5m

src/encoder.cpp

+10-10
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,15 @@ static PIOProgram encoderProgram(&encoder2_program);
4040
* Initialize PIO program that measures encoder period.
4141
*
4242
* Returns true on sucess.
43-
*
43+
*
4444
*****************************************************************/
4545

4646
bool Encoder::init(const int pin) {
4747
last_sample_time = millis();
4848

4949
this->pin = pin;
5050

51-
if (!encoderProgram.prepare(&PioInstance, &StateMachineIdx, &offset)) {
51+
if (!encoderProgram.prepare(&PioInstance, &StateMachineIdx, &offset, pin, 2)) {
5252
return false;
5353
}
5454

@@ -60,7 +60,7 @@ bool Encoder::init(const int pin) {
6060
* Encoder::enable()
6161
* Enable the encoder.
6262
*
63-
*
63+
*
6464
*****************************************************************/
6565

6666
void Encoder::enable() {
@@ -75,7 +75,7 @@ void Encoder::enable() {
7575
* Encoder::disable()
7676
* Disable the encoder.
7777
*
78-
*
78+
*
7979
*****************************************************************/
8080

8181
void Encoder::disable() {
@@ -90,14 +90,14 @@ void Encoder::disable() {
9090
* Encoder::update()
9191
* Get the latest encoder period(s) from the PIO if available
9292
* store the latest, and updated the tick count.
93-
*
93+
*
9494
* Returns number of samples that were retrieved.
95-
*
95+
*
9696
*****************************************************************/
9797

9898
uint Encoder::getFraction(const uint count) const {
9999
return count & (samples_to_average() - 1);
100-
}
100+
}
101101

102102
uint Encoder::getWholeNumber(const uint count) const {
103103
return count >> samples_to_average_shift;
@@ -147,7 +147,7 @@ int Encoder::update() {
147147
++period;
148148
period_fraction = getFraction(period_fraction);
149149
}
150-
period_queue[sample_index++] = raw_rx_fifo;
150+
period_queue[sample_index++] = raw_rx_fifo;
151151
++sample_count;
152152
if(sample_index == samples_to_average())
153153
sample_index = 0;
@@ -191,7 +191,7 @@ void Encoder::setSamplesToAverage(const int n) {
191191
* Return the period calculated by the PIO in the following format:
192192
* 31 1 0
193193
* | Period in 16-cycle ticks | dir |
194-
*
194+
*
195195
* This is the same format return by the PIO.
196196
* (Note: PIO calculated value may be overidden if a relatively
197197
* large amount of time has passed since the last sample from the PIO).
@@ -228,7 +228,7 @@ uint Encoder::getPeriod() {
228228
//If PIO value is correct, the amount of time that has passed since the last sample
229229
//should be less than or equal to period calculated by the PIO.
230230
//Otherwise, motor may have slowed way down; use the time passed for the current period length.
231-
231+
232232
//If time since last sample is significantly longer than the last period returned by the PIO,
233233
//use the time passed instead.
234234

src/encoder2.pio

+7-3
Original file line numberDiff line numberDiff line change
@@ -29,14 +29,14 @@
2929
.program encoder2
3030
.origin 0
3131

32-
.define PUBLIC VERSION 102
32+
.define PUBLIC VERSION 102
3333
.define PUBLIC CYCLES_PER_COUNT 16
3434

3535
; This PIO program monitors the encoder inputs to measure the period
3636
; between encoder pulses. At the start of a period, a 31-bit counter is
3737
; initialized to its maximum value (0x7FFFFFFF) and is decremented every 16 cycles
3838
; until the start of the next period. The period start/end is determined
39-
; by the transition of the encoder signals from one state to another (e.g., 01 -> 11 or 01 -> 00)
39+
; by the transition of the encoder signals from one state to another (e.g., 01 -> 11 or 01 -> 00)
4040

4141
; Every period, the total count is passed to the robot code through the RX FIFO.
4242
; The upper 31 bits is the count. It represents the number of 16-cycle intervals
@@ -117,11 +117,15 @@ send_period:
117117
push noblock ;send period/direction to robot code
118118
out isr,2 ;restore last state to isr
119119
mov osr,~null ;set all bits in osr
120-
out x, 31 ;set 31 bits of x register
120+
out x, 31 ;set 31 bits of x register
121121
.wrap
122122

123123
% c-sdk {
124124
static inline void encoder2_program_init(PIO pio, uint sm, uint offset, uint base_pin) {
125+
pio_gpio_init(pio, base_pin);
126+
pio_gpio_init(pio, base_pin+1);
127+
pio_sm_set_consecutive_pindirs(pio, sm, base_pin, 2, false);
128+
125129
pio_sm_config c = encoder2_program_get_default_config(offset);
126130

127131
sm_config_set_in_pins(&c, base_pin);

0 commit comments

Comments
 (0)