2828#include "shared-bindings/countio/Counter.h"
2929#include "common-hal/microcontroller/Pin.h"
3030
31+ #include "bindings/espidf/__init__.h"
32+
3133#include "py/runtime.h"
3234
3335#include "driver/gpio.h"
@@ -36,25 +38,26 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
3638 const mcu_pin_obj_t * pin , countio_edge_t edge , digitalio_pull_t pull ) {
3739 claim_pin (pin );
3840
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
4945 };
46+ // The pulse count driver automatically counts roll overs.
47+ unit_config .flags .accum_count = true;
5048
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 ));
5651
5752 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 );
5861
5962 gpio_pullup_dis (pin -> number );
6063 gpio_pulldown_dis (pin -> number );
@@ -64,29 +67,34 @@ void common_hal_countio_counter_construct(countio_counter_obj_t *self,
6467 gpio_pulldown_en (pin -> number );
6568 }
6669
67- self -> unit = (pcnt_unit_t )unit ;
70+
71+ pcnt_unit_enable (self -> unit );
72+ pcnt_unit_start (self -> unit );
6873}
6974
7075bool common_hal_countio_counter_deinited (countio_counter_obj_t * self ) {
71- return self -> unit == PCNT_UNIT_MAX ;
76+ return self -> unit == NULL ;
7277}
7378
7479void common_hal_countio_counter_deinit (countio_counter_obj_t * self ) {
7580 if (common_hal_countio_counter_deinited (self )) {
7681 return ;
7782 }
83+ pcnt_unit_disable (self -> unit );
84+ pcnt_del_channel (self -> channel );
7885 reset_pin_number (self -> pin );
79- peripherals_pcnt_deinit (& self -> unit );
86+ pcnt_del_unit (self -> unit );
87+ self -> unit = NULL ;
8088}
8189
8290mp_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 );
8593 return count + self -> count ;
8694}
8795
8896void common_hal_countio_counter_set_count (countio_counter_obj_t * self ,
8997 mp_int_t new_count ) {
9098 self -> count = new_count ;
91- pcnt_counter_clear (self -> unit );
99+ pcnt_unit_clear_count (self -> unit );
92100}
0 commit comments