@@ -36,45 +36,41 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
3636 claim_pin (pin_b );
3737
3838 // Prepare configuration for the PCNT unit
39- pcnt_config_t pcnt_config = {
39+ pcnt_config_t pcnt_config_channel_0 = {
4040 // Set PCNT input signal and control GPIOs
4141 .pulse_gpio_num = pin_a -> number ,
4242 .ctrl_gpio_num = pin_b -> number ,
4343 .channel = PCNT_CHANNEL_0 ,
4444 // 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
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
4747 // What to do when control input is low or high?
4848 .lctrl_mode = PCNT_MODE_REVERSE , // Reverse counting direction if low
4949 .hctrl_mode = PCNT_MODE_KEEP , // Keep the primary counter mode if high
5050 };
5151
52- // Initialize PCNT unit
53- const int8_t unit = peripherals_pcnt_get_unit ( pcnt_config );
52+ // Allocate and initialize PCNT unit, CHANNEL_0.
53+ const int8_t unit = peripherals_pcnt_init ( & pcnt_config_channel_0 );
5454 if (unit == -1 ) {
5555 mp_raise_RuntimeError (translate ("All PCNT units in use" ));
5656 }
5757
58- pcnt_unit_config (& pcnt_config );
59-
60- pcnt_config .pulse_gpio_num = pin_b -> number ; // What was control is now signal
61- pcnt_config .ctrl_gpio_num = pin_a -> number ; // What was signal is now control
62- pcnt_config .channel = PCNT_CHANNEL_1 ;
63- // What to do on the positive / negative edge of pulse input?
64- pcnt_config .pos_mode = PCNT_COUNT_DEC ; // Count up on the positive edge
65- pcnt_config .neg_mode = PCNT_COUNT_INC ; // Keep the counter value on the negative edge
66- // What to do when control input is low or high?
67- pcnt_config .lctrl_mode = PCNT_MODE_KEEP ; // Keep the primary counter mode if low
68- pcnt_config .hctrl_mode = PCNT_MODE_REVERSE ; // Reverse counting direction if high
69-
70- pcnt_unit_config (& pcnt_config );
71-
72- // Initialize PCNT's counter
73- pcnt_counter_pause (pcnt_config .unit );
74- pcnt_counter_clear (pcnt_config .unit );
58+ pcnt_config_t pcnt_config_channel_1 = {
59+ // Set PCNT input signal and control GPIOs
60+ .pulse_gpio_num = pin_b -> number , // Pins are reversed from above
61+ .ctrl_gpio_num = pin_a -> number ,
62+ .channel = PCNT_CHANNEL_1 ,
63+ // 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
66+ // What to do when control input is low or high?
67+ .lctrl_mode = PCNT_MODE_KEEP , // Keep the primary counter mode if low
68+ .hctrl_mode = PCNT_MODE_REVERSE , // Reverse counting direction if high
69+ .unit = unit ,
70+ };
7571
76- // Everything is set up, now go to counting
77- pcnt_counter_resume ( pcnt_config . unit );
72+ // Reinitalize same unit, CHANNEL_1 with different parameters.
73+ peripherals_pcnt_reinit ( & pcnt_config_channel_1 );
7874
7975 self -> pin_a = pin_a -> number ;
8076 self -> pin_b = pin_b -> number ;
0 commit comments