28
28
#include "shared-bindings/countio/Counter.h"
29
29
#include "common-hal/microcontroller/Pin.h"
30
30
31
+ #include "bindings/espidf/__init__.h"
32
+
31
33
#include "py/runtime.h"
32
34
33
35
#include "driver/gpio.h"
@@ -36,25 +38,26 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
36
38
const mcu_pin_obj_t * pin , countio_edge_t edge , digitalio_pull_t pull ) {
37
39
claim_pin (pin );
38
40
39
- // Prepare configuration for the PCNT unit
40
- pcnt_config_t pcnt_config = {
41
- // Set PCNT input signal and control GPIOs
42
- .pulse_gpio_num = pin -> number ,
43
- .ctrl_gpio_num = PCNT_PIN_NOT_USED ,
44
- .channel = PCNT_CHANNEL_0 ,
45
- // What to do on the rising / falling edge of pulse input?
46
- // If EDGE_RISE_AND_FALL, both modeswill do PCNT_COUNT_INC.
47
- .pos_mode = (edge == EDGE_FALL ) ? PCNT_COUNT_DIS : PCNT_COUNT_INC , // Count up unless only fall
48
- .neg_mode = (edge == EDGE_RISE ) ? PCNT_COUNT_DIS : PCNT_COUNT_INC , // Count up unless only rise
41
+ pcnt_unit_config_t unit_config = {
42
+ // Set counter limit
43
+ .low_limit = -1 ,
44
+ .high_limit = INT16_MAX
49
45
};
46
+ // The pulse count driver automatically counts roll overs.
47
+ unit_config .flags .accum_count = true;
50
48
51
- // Initialize PCNT unit
52
- const int8_t unit = peripherals_pcnt_init (& pcnt_config );
53
- if (unit == -1 ) {
54
- mp_raise_RuntimeError (MP_ERROR_TEXT ("All PCNT units in use" ));
55
- }
49
+ // initialize PCNT
50
+ CHECK_ESP_RESULT (pcnt_new_unit (& unit_config , & self -> unit ));
56
51
57
52
self -> pin = pin -> number ;
53
+ pcnt_chan_config_t channel_config = {
54
+ .edge_gpio_num = self -> pin ,
55
+ .level_gpio_num = -1
56
+ };
57
+ CHECK_ESP_RESULT (pcnt_new_channel (self -> unit , & channel_config , & self -> channel ));
58
+ pcnt_channel_edge_action_t pos = (edge == EDGE_RISE || edge == EDGE_RISE_AND_FALL ) ? PCNT_CHANNEL_EDGE_ACTION_INCREASE : PCNT_CHANNEL_EDGE_ACTION_HOLD ;
59
+ pcnt_channel_edge_action_t neg = (edge == EDGE_FALL || edge == EDGE_RISE_AND_FALL ) ? PCNT_CHANNEL_EDGE_ACTION_INCREASE : PCNT_CHANNEL_EDGE_ACTION_HOLD ;
60
+ pcnt_channel_set_edge_action (self -> channel , pos , neg );
58
61
59
62
gpio_pullup_dis (pin -> number );
60
63
gpio_pulldown_dis (pin -> number );
@@ -64,29 +67,34 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
64
67
gpio_pulldown_en (pin -> number );
65
68
}
66
69
67
- self -> unit = (pcnt_unit_t )unit ;
70
+
71
+ pcnt_unit_enable (self -> unit );
72
+ pcnt_unit_start (self -> unit );
68
73
}
69
74
70
75
bool common_hal_countio_counter_deinited (countio_counter_obj_t * self ) {
71
- return self -> unit == PCNT_UNIT_MAX ;
76
+ return self -> unit == NULL ;
72
77
}
73
78
74
79
void common_hal_countio_counter_deinit (countio_counter_obj_t * self ) {
75
80
if (common_hal_countio_counter_deinited (self )) {
76
81
return ;
77
82
}
83
+ pcnt_unit_disable (self -> unit );
84
+ pcnt_del_channel (self -> channel );
78
85
reset_pin_number (self -> pin );
79
- peripherals_pcnt_deinit (& self -> unit );
86
+ pcnt_del_unit (self -> unit );
87
+ self -> unit = NULL ;
80
88
}
81
89
82
90
mp_int_t common_hal_countio_counter_get_count (countio_counter_obj_t * self ) {
83
- int16_t count ;
84
- pcnt_get_counter_value (self -> unit , & count );
91
+ int count ;
92
+ pcnt_unit_get_count (self -> unit , & count );
85
93
return count + self -> count ;
86
94
}
87
95
88
96
void common_hal_countio_counter_set_count (countio_counter_obj_t * self ,
89
97
mp_int_t new_count ) {
90
98
self -> count = new_count ;
91
- pcnt_counter_clear (self -> unit );
99
+ pcnt_unit_clear_count (self -> unit );
92
100
}
0 commit comments