Skip to content

Commit 2ae1e7d

Browse files
committed
WIPrp2040: Implement IncrementalEncoder
Any two consecutive pins can be used for an IncrementalEncoder Testing performed: Put a synthesized (few hundred counts per second) quadrature signal into GP2/3 and read the encoder out. Performed filesystem operations at the same time to stress test it. The reasons for not using common_hal_rp2pio_statemachine_readinto are commented on.
1 parent 2ab13d6 commit 2ae1e7d

File tree

7 files changed

+257
-1
lines changed

7 files changed

+257
-1
lines changed

ports/raspberrypi/bindings/rp2pio/StateMachine.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,4 +71,6 @@ bool common_hal_rp2pio_statemachine_get_rxstall(rp2pio_statemachine_obj_t* self)
7171
void common_hal_rp2pio_statemachine_clear_rxfifo(rp2pio_statemachine_obj_t *self);
7272
size_t common_hal_rp2pio_statemachine_get_in_waiting(rp2pio_statemachine_obj_t *self);
7373

74+
void common_hal_rp2pio_statemachine_set_interrupt_handler(rp2pio_statemachine_obj_t *self, void(*handler)(void*), void *arg, int mask);
75+
7476
#endif // MICROPY_INCLUDED_RASPBERRYPI_BINDINGS_RP2PIO_STATEMACHINE_H
Lines changed: 170 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,170 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#include "py/runtime.h"
28+
29+
#include <hardware/regs/pio.h>
30+
#include "common-hal/rotaryio/IncrementalEncoder.h"
31+
#include "bindings/rp2pio/__init__.h"
32+
#include "bindings/rp2pio/StateMachine.h"
33+
34+
STATIC const uint16_t encoder[] = {
35+
// again:
36+
// in pins, 2
37+
0x4002,
38+
// mov x, isr
39+
0xa026,
40+
// jmp x!=y, push_data
41+
0x00a5,
42+
// mov isr, null
43+
0xa0c3,
44+
// jmp again
45+
0x0000,
46+
// push_data:
47+
// push
48+
0x8020,
49+
// mov y, x
50+
0xa041,
51+
};
52+
53+
STATIC const uint16_t encoder_init[] = {
54+
// set y, 31
55+
0xe05f,
56+
};
57+
58+
STATIC void incrementalencoder_interrupt_handler(void *self_in);
59+
60+
void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t* self,
61+
const mcu_pin_obj_t* pin_a, const mcu_pin_obj_t* pin_b) {
62+
mp_obj_t pins[] = {MP_OBJ_FROM_PTR(pin_a), MP_OBJ_FROM_PTR(pin_b)};
63+
if (!common_hal_rp2pio_pins_are_sequential(2, pins)) {
64+
mp_raise_RuntimeError(translate("Pins must be sequential"));
65+
}
66+
67+
self->position = 0;
68+
self->quarter_count = 0;
69+
70+
common_hal_rp2pio_statemachine_construct(&self->state_machine,
71+
encoder, MP_ARRAY_SIZE(encoder),
72+
1000000,
73+
encoder_init, MP_ARRAY_SIZE(encoder_init), // init
74+
NULL, 1, 0, 0xffffffff, // out pin
75+
pin_a, 2, // in pins
76+
3, 0, // in pulls
77+
NULL, 0, 0, 0x1f, // set pins
78+
NULL, 0, 0, 0x1f, // sideset pins
79+
true, // exclusive pin use
80+
false, 32, false, // out settings
81+
false, // Wait for txstall
82+
false, 32, false); // in settings
83+
84+
common_hal_rp2pio_statemachine_run(&self->state_machine, encoder_init, MP_ARRAY_SIZE(encoder_init));
85+
86+
// We're guaranteed by the init code that some output will be available promptly
87+
uint8_t state;
88+
common_hal_rp2pio_statemachine_readinto(&self->state_machine, &state, 1, 1);
89+
// Top two bits of self->last_state don't matter, because they'll be gone as soon as
90+
// interrupt handler is called.
91+
self->last_state = state & 3;
92+
93+
common_hal_rp2pio_statemachine_set_interrupt_handler(&self->state_machine, incrementalencoder_interrupt_handler, self, PIO_IRQ0_INTF_SM0_RXNEMPTY_BITS);
94+
}
95+
96+
bool common_hal_rotaryio_incrementalencoder_deinited(rotaryio_incrementalencoder_obj_t* self) {
97+
return common_hal_rp2pio_statemachine_deinited(&self->state_machine);
98+
}
99+
100+
void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_obj_t* self) {
101+
if (common_hal_rotaryio_incrementalencoder_deinited(self)) {
102+
return;
103+
}
104+
common_hal_rp2pio_statemachine_deinit(&self->state_machine);
105+
}
106+
107+
mp_int_t common_hal_rotaryio_incrementalencoder_get_position(rotaryio_incrementalencoder_obj_t* self) {
108+
return self->position;
109+
}
110+
111+
void common_hal_rotaryio_incrementalencoder_set_position(rotaryio_incrementalencoder_obj_t* self,
112+
mp_int_t new_position) {
113+
self->position = new_position;
114+
}
115+
116+
STATIC void incrementalencoder_interrupt_handler(void *self_in) {
117+
rotaryio_incrementalencoder_obj_t* self = self_in;
118+
// This table also works for detent both at 11 and 00
119+
// For 11 at detent:
120+
// Turning cw: 11->01->00->10->11
121+
// Turning ccw: 11->10->00->01->11
122+
// For 00 at detent:
123+
// Turning cw: 00->10->11->10->00
124+
// Turning ccw: 00->01->11->10->00
125+
126+
// index table by state <oldA><oldB><newA><newB>
127+
#define BAD 7
128+
static const int8_t transitions[16] = {
129+
0, // 00 -> 00 no movement
130+
-1, // 00 -> 01 3/4 ccw (11 detent) or 1/4 ccw (00 at detent)
131+
+1, // 00 -> 10 3/4 cw or 1/4 cw
132+
BAD, // 00 -> 11 non-Gray-code transition
133+
+1, // 01 -> 00 2/4 or 4/4 cw
134+
0, // 01 -> 01 no movement
135+
BAD, // 01 -> 10 non-Gray-code transition
136+
-1, // 01 -> 11 4/4 or 2/4 ccw
137+
-1, // 10 -> 00 2/4 or 4/4 ccw
138+
BAD, // 10 -> 01 non-Gray-code transition
139+
0, // 10 -> 10 no movement
140+
+1, // 10 -> 11 4/4 or 2/4 cw
141+
BAD, // 11 -> 00 non-Gray-code transition
142+
+1, // 11 -> 01 1/4 or 3/4 cw
143+
-1, // 11 -> 10 1/4 or 3/4 ccw
144+
0, // 11 -> 11 no movement
145+
};
146+
147+
while (common_hal_rp2pio_statemachine_get_in_waiting(&self->state_machine)) {
148+
// Bypass all the logic of StateMachine.c:_transfer, we need something
149+
// very simple and fast for an interrupt!
150+
uint8_t new = self->state_machine.pio->rxf[self->state_machine.state_machine];
151+
152+
// Shift the old AB bits to the "old" position, and set the new AB bits.
153+
self->last_state = (self->last_state & 0x3) << 2 | (new & 0x3);
154+
155+
int8_t quarter_incr = transitions[self->last_state];
156+
if (quarter_incr == BAD) {
157+
// Missed a transition. We don't know which way we're going, so do nothing.
158+
return;
159+
}
160+
161+
self->quarter_count += quarter_incr;
162+
if (self->quarter_count >= 4) {
163+
self->position += 1;
164+
self->quarter_count = 0;
165+
} else if (self->quarter_count <= -4) {
166+
self->position -= 1;
167+
self->quarter_count = 0;
168+
}
169+
}
170+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/*
2+
* This file is part of the MicroPython project, http://micropython.org/
3+
*
4+
* The MIT License (MIT)
5+
*
6+
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
7+
*
8+
* Permission is hereby granted, free of charge, to any person obtaining a copy
9+
* of this software and associated documentation files (the "Software"), to deal
10+
* in the Software without restriction, including without limitation the rights
11+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
12+
* copies of the Software, and to permit persons to whom the Software is
13+
* furnished to do so, subject to the following conditions:
14+
*
15+
* The above copyright notice and this permission notice shall be included in
16+
* all copies or substantial portions of the Software.
17+
*
18+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
19+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
20+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
21+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
22+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
23+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
24+
* THE SOFTWARE.
25+
*/
26+
27+
#pragma once
28+
29+
#include "common-hal/rp2pio/StateMachine.h"
30+
#include "common-hal/microcontroller/Pin.h"
31+
32+
#include "py/obj.h"
33+
34+
typedef struct {
35+
mp_obj_base_t base;
36+
rp2pio_statemachine_obj_t state_machine;
37+
uint8_t last_state:4; // <old A><old B><new A><new B>
38+
int8_t quarter_count:4; // count intermediate transitions between detents
39+
mp_int_t position;
40+
} rotaryio_incrementalencoder_obj_t;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
// No rotaryio module functions.

ports/raspberrypi/common-hal/rotaryio/__init__.h

Whitespace-only changes.

ports/raspberrypi/common-hal/rp2pio/StateMachine.c

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "bindings/rp2pio/StateMachine.h"
2828

2929
#include "common-hal/microcontroller/__init__.h"
30+
#include "shared-bindings/microcontroller/__init__.h"
3031
#include "shared-bindings/microcontroller/Pin.h"
3132

3233
#include "src/rp2040/hardware_regs/include/hardware/platform_defs.h"
@@ -52,6 +53,11 @@ STATIC uint32_t _current_pins[NUM_PIOS];
5253
STATIC uint32_t _current_sm_pins[NUM_PIOS][NUM_PIO_STATE_MACHINES];
5354

5455
STATIC PIO pio_instances[2] = {pio0, pio1};
56+
typedef void (*interrupt_handler_type)(void*);
57+
STATIC interrupt_handler_type _interrupt_handler[NUM_PIOS][NUM_PIO_STATE_MACHINES];
58+
STATIC void *_interrupt_arg[NUM_PIOS][NUM_PIO_STATE_MACHINES];
59+
60+
STATIC void rp2pio_statemachine_interrupt_handler(void);
5561

5662
static void rp2pio_statemachine_set_pull(uint32_t pull_pin_up, uint32_t pull_pin_down, uint32_t pins_we_use) {
5763
for (int i=0; i<TOTAL_GPIO_COUNT; i++) {
@@ -100,6 +106,7 @@ void _reset_statemachine(PIO pio, uint8_t sm, bool leave_pins) {
100106
}
101107
}
102108
_current_sm_pins[pio_index][sm] = 0;
109+
pio->inte0 &= ~((PIO_IRQ0_INTF_SM0_RXNEMPTY_BITS | PIO_IRQ0_INTF_SM0_TXNFULL_BITS | PIO_IRQ0_INTF_SM0_BITS) << sm);
103110
pio_sm_unclaim(pio, sm);
104111
}
105112

@@ -535,6 +542,10 @@ void common_hal_rp2pio_statemachine_set_frequency(rp2pio_statemachine_obj_t* sel
535542
void rp2pio_statemachine_deinit(rp2pio_statemachine_obj_t *self, bool leave_pins) {
536543
uint8_t sm = self->state_machine;
537544
uint8_t pio_index = pio_get_index(self->pio);
545+
common_hal_mcu_disable_interrupts();
546+
_interrupt_arg[pio_index][sm] = NULL;
547+
_interrupt_handler[pio_index][sm] = NULL;
548+
common_hal_mcu_enable_interrupts();
538549
_never_reset[pio_index][sm] = false;
539550
_reset_statemachine(self->pio, sm, leave_pins);
540551
self->state_machine = NUM_PIO_STATE_MACHINES;
@@ -757,3 +768,34 @@ size_t common_hal_rp2pio_statemachine_get_in_waiting(rp2pio_statemachine_obj_t *
757768
uint8_t level = pio_sm_get_rx_fifo_level(self->pio, self->state_machine);
758769
return level;
759770
}
771+
772+
void common_hal_rp2pio_statemachine_set_interrupt_handler(rp2pio_statemachine_obj_t *self, void(*handler)(void*), void *arg, int mask) {
773+
uint8_t pio_index = pio_get_index(self->pio);
774+
uint8_t sm = self->state_machine;
775+
776+
common_hal_mcu_disable_interrupts();
777+
uint32_t inte = self->pio->inte0;
778+
inte &= ~((PIO_IRQ0_INTF_SM0_RXNEMPTY_BITS | PIO_IRQ0_INTF_SM0_TXNFULL_BITS | PIO_IRQ0_INTF_SM0_BITS) << sm);
779+
inte |= (mask << sm);
780+
self->pio->inte0 = inte;
781+
_interrupt_arg[pio_index][sm] = arg;
782+
_interrupt_handler[pio_index][sm] = handler;
783+
irq_set_exclusive_handler(PIO0_IRQ_0 + 2 * pio_index, rp2pio_statemachine_interrupt_handler);
784+
irq_set_enabled(PIO0_IRQ_0 + 2 * pio_index, true);
785+
common_hal_mcu_enable_interrupts();
786+
}
787+
788+
STATIC void rp2pio_statemachine_interrupt_handler(void) {
789+
for (size_t pio_index = 0; pio_index < NUM_PIOS; pio_index++) {
790+
PIO pio = pio_instances[pio_index];
791+
for (size_t sm = 0; sm < NUM_PIO_STATE_MACHINES; sm++) {
792+
if (!_interrupt_handler[pio_index][sm]) {
793+
continue;
794+
}
795+
uint32_t intf = (PIO_IRQ0_INTF_SM0_RXNEMPTY_BITS | PIO_IRQ0_INTF_SM0_TXNFULL_BITS | PIO_IRQ0_INTF_SM0_BITS) << sm;
796+
if (pio->ints0 & intf) {
797+
_interrupt_handler[pio_index][sm](_interrupt_arg[pio_index][sm]);
798+
}
799+
}
800+
}
801+
}

ports/raspberrypi/mpconfigport.mk

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@ CIRCUITPY_FULL_BUILD = 1
2828
CIRCUITPY_BITOPS = 1
2929
CIRCUITPY_PWMIO = 1
3030
CIRCUITPY_RGBMATRIX = 1
31+
CIRCUITPY_ROTARYIO = 1
3132

3233
# Things that need to be implemented.
34+
# (Remember to remove any comment when changing a 0 to a 1, make gets confused otherwise)
3335
CIRCUITPY_FREQUENCYIO = 0 # Use PWM interally
3436
CIRCUITPY_I2CPERIPHERAL = 0
3537
CIRCUITPY_NVM = 1
3638
CIRCUITPY_PULSEIO = 0 # Use PIO interally
37-
CIRCUITPY_ROTARYIO = 0 # Use PIO interally
3839
CIRCUITPY_WATCHDOG = 1
3940

4041
# Audio via PWM

0 commit comments

Comments
 (0)