fix nrf ISR; make direction consistent across ports; save code size

This commit is contained in:
Dan Halbert 2021-04-09 11:07:47 -04:00
parent 280aefffb7
commit 61e33a5619
5 changed files with 22 additions and 11 deletions

View File

@ -35,10 +35,10 @@ typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
uint8_t pin_a; uint8_t pin_a;
uint8_t pin_b; uint8_t pin_b;
uint8_t eic_channel_a:4; uint8_t eic_channel_a;
uint8_t eic_channel_b:4; uint8_t eic_channel_b;
uint8_t state:4; // <old A><old B> uint8_t state; // <old A><old B>
int8_t quarter_count:4; // count intermediate transitions between detents int8_t quarter_count; // count intermediate transitions between detents
mp_int_t position; mp_int_t position;
} rotaryio_incrementalencoder_obj_t; } rotaryio_incrementalencoder_obj_t;

View File

@ -41,9 +41,9 @@ static void _intr_handler(nrfx_gpiote_pin_t pin, nrf_gpiote_polarity_t action) {
return; return;
} }
// reads a state 0 .. 3 *in order*. uint8_t new_state =
uint8_t new_state = nrf_gpio_pin_read(self->pin_a); ((uint8_t) nrf_gpio_pin_read(self->pin_a) << 1) |
new_state = (new_state << 1) + (new_state ^ nrf_gpio_pin_read(self->pin_b)); (uint8_t) nrf_gpio_pin_read(self->pin_b);
shared_module_softencoder_state_update(self, new_state); shared_module_softencoder_state_update(self, new_state);
} }

View File

@ -35,8 +35,8 @@ typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
uint8_t pin_a; uint8_t pin_a;
uint8_t pin_b; uint8_t pin_b;
int8_t quarter_count : 4; uint8_t state; // <old A><old B>
uint8_t state : 4; int8_t quarter_count; // count intermediate transitions between detents
mp_int_t position; mp_int_t position;
} rotaryio_incrementalencoder_obj_t; } rotaryio_incrementalencoder_obj_t;

View File

@ -61,9 +61,12 @@ STATIC void incrementalencoder_interrupt_handler(void *self_in);
void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t *self, void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencoder_obj_t *self,
const mcu_pin_obj_t *pin_a, const mcu_pin_obj_t *pin_b) { const mcu_pin_obj_t *pin_a, const mcu_pin_obj_t *pin_b) {
mp_obj_t pins[] = {MP_OBJ_FROM_PTR(pin_a), MP_OBJ_FROM_PTR(pin_b)}; mp_obj_t pins[] = {MP_OBJ_FROM_PTR(pin_a), MP_OBJ_FROM_PTR(pin_b)};
// Start out with swapped to match behavior with other ports.
self->swapped = true;
if (!common_hal_rp2pio_pins_are_sequential(2, pins)) { if (!common_hal_rp2pio_pins_are_sequential(2, pins)) {
pins[0] = MP_OBJ_FROM_PTR(pin_b); pins[0] = MP_OBJ_FROM_PTR(pin_b);
pins[1] = MP_OBJ_FROM_PTR(pin_a); pins[1] = MP_OBJ_FROM_PTR(pin_a);
self->swapped = false;
if (!common_hal_rp2pio_pins_are_sequential(2, pins)) { if (!common_hal_rp2pio_pins_are_sequential(2, pins)) {
mp_raise_RuntimeError(translate("Pins must be sequential")); mp_raise_RuntimeError(translate("Pins must be sequential"));
} }
@ -115,6 +118,13 @@ STATIC void incrementalencoder_interrupt_handler(void *self_in) {
// Bypass all the logic of StateMachine.c:_transfer, we need something // Bypass all the logic of StateMachine.c:_transfer, we need something
// very simple and fast for an interrupt! // very simple and fast for an interrupt!
uint8_t new_state = self->state_machine.pio->rxf[self->state_machine.state_machine]; uint8_t new_state = self->state_machine.pio->rxf[self->state_machine.state_machine];
if (self->swapped) {
if (new_state == 0x1) {
new_state = 0x2;
} else if (new_state == 0x2) {
new_state = 0x1;
}
}
shared_module_softencoder_state_update(self, new_state); shared_module_softencoder_state_update(self, new_state);
} }
} }

View File

@ -34,7 +34,8 @@
typedef struct { typedef struct {
mp_obj_base_t base; mp_obj_base_t base;
rp2pio_statemachine_obj_t state_machine; rp2pio_statemachine_obj_t state_machine;
uint8_t state : 4; // <old A><old B><new A><new B> uint8_t state; // <old A><old B>
int8_t quarter_count : 4; // count intermediate transitions between detents int8_t quarter_count; // count intermediate transitions between detents
bool swapped; // Did the pins need to be swapped to be sequential?
mp_int_t position; mp_int_t position;
} rotaryio_incrementalencoder_obj_t; } rotaryio_incrementalencoder_obj_t;