@@ -35,15 +35,24 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
3535 claim_pin (pin_a );
3636 claim_pin (pin_b );
3737
38+ // This configuration counts on all edges of the quadrature signal: Channel 0
39+ // counts on rising and falling edges of channel A, with the direction set by the
40+ // polarity of channel B. Channel 1 does likewise, counting edges of channel B according
41+ // to the polarity of channel A. A little pencil work suffices to show that this
42+ // counts correctly on all 8 correct quadrature state transitions.
43+ //
44+ // These routines also implicitly configure the weak internal pull-ups, as expected
45+ // in CircuitPython.
46+
3847 // Prepare configuration for the PCNT unit
3948 pcnt_config_t pcnt_config_channel_0 = {
4049 // Set PCNT input signal and control GPIOs
4150 .pulse_gpio_num = pin_a -> number ,
4251 .ctrl_gpio_num = pin_b -> number ,
4352 .channel = PCNT_CHANNEL_0 ,
4453 // What to do on the positive / negative edge of pulse input?
45- .pos_mode = PCNT_COUNT_DEC , // Count up on the positive edge
46- .neg_mode = PCNT_COUNT_INC , // Keep the counter value on the negative edge
54+ .pos_mode = PCNT_COUNT_DEC , // Count down on the positive edge
55+ .neg_mode = PCNT_COUNT_INC , // Count up on negative edge
4756 // What to do when control input is low or high?
4857 .lctrl_mode = PCNT_MODE_REVERSE , // Reverse counting direction if low
4958 .hctrl_mode = PCNT_MODE_KEEP , // Keep the primary counter mode if high
@@ -61,8 +70,8 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
6170 .ctrl_gpio_num = pin_a -> number ,
6271 .channel = PCNT_CHANNEL_1 ,
6372 // What to do on the positive / negative edge of pulse input?
64- .pos_mode = PCNT_COUNT_DEC , // Count up on the positive edge
65- .neg_mode = PCNT_COUNT_INC , // Keep the counter value on the negative edge
73+ .pos_mode = PCNT_COUNT_DEC , // Count down on the positive edge
74+ .neg_mode = PCNT_COUNT_INC , // Count up on negative edge
6675 // What to do when control input is low or high?
6776 .lctrl_mode = PCNT_MODE_KEEP , // Keep the primary counter mode if low
6877 .hctrl_mode = PCNT_MODE_REVERSE , // Reverse counting direction if high
@@ -88,6 +97,7 @@ void common_hal_rotaryio_incrementalencoder_deinit(rotaryio_incrementalencoder_o
8897 reset_pin_number (self -> pin_a );
8998 reset_pin_number (self -> pin_b );
9099 peripherals_pcnt_deinit (& self -> unit );
100+ self -> unit = PCNT_UNIT_MAX ;
91101}
92102
93103mp_int_t common_hal_rotaryio_incrementalencoder_get_position (rotaryio_incrementalencoder_obj_t * self ) {
0 commit comments