@@ -36,45 +36,41 @@ void common_hal_rotaryio_incrementalencoder_construct(rotaryio_incrementalencode
36
36
claim_pin (pin_b );
37
37
38
38
// Prepare configuration for the PCNT unit
39
- pcnt_config_t pcnt_config = {
39
+ pcnt_config_t pcnt_config_channel_0 = {
40
40
// Set PCNT input signal and control GPIOs
41
41
.pulse_gpio_num = pin_a -> number ,
42
42
.ctrl_gpio_num = pin_b -> number ,
43
43
.channel = PCNT_CHANNEL_0 ,
44
44
// 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
47
47
// What to do when control input is low or high?
48
48
.lctrl_mode = PCNT_MODE_REVERSE , // Reverse counting direction if low
49
49
.hctrl_mode = PCNT_MODE_KEEP , // Keep the primary counter mode if high
50
50
};
51
51
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 );
54
54
if (unit == -1 ) {
55
55
mp_raise_RuntimeError (translate ("All PCNT units in use" ));
56
56
}
57
57
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
+ };
75
71
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 );
78
74
79
75
self -> pin_a = pin_a -> number ;
80
76
self -> pin_b = pin_b -> number ;
0 commit comments