67 pcnt_unit_config_t unit_config = {
68 .low_limit = INT16_MIN,
69 .high_limit = INT16_MAX,
70 .flags = {.accum_count =
true},
72 esp_err_t error = pcnt_new_unit(&unit_config, &this->
pcnt_unit);
73 if (error != ESP_OK) {
74 ESP_LOGE(TAG,
"Creating PCNT unit failed: %s", esp_err_to_name(error));
78 pcnt_chan_config_t chan_config = {
79 .edge_gpio_num = this->pin->
get_pin(),
83 if (error != ESP_OK) {
84 ESP_LOGE(TAG,
"Creating PCNT channel failed: %s", esp_err_to_name(error));
88 pcnt_channel_edge_action_t rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
89 pcnt_channel_edge_action_t falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
92 rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
95 rising = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
98 rising = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
103 falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
106 falling = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
109 falling = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
113 error = pcnt_channel_set_edge_action(this->
pcnt_channel, rising, falling);
114 if (error != ESP_OK) {
115 ESP_LOGE(TAG,
"Setting PCNT edge action failed: %s", esp_err_to_name(error));
120 uint32_t max_glitch_ns = PCNT_LL_MAX_GLITCH_WIDTH * 1000u / ((uint32_t) esp_clk_apb_freq() / 1000000u);
121 pcnt_glitch_filter_config_t filter_config = {
122 .max_glitch_ns = std::min(this->
filter_us * 1000u, max_glitch_ns),
124 error = pcnt_unit_set_glitch_filter(this->
pcnt_unit, &filter_config);
125 if (error != ESP_OK) {
126 ESP_LOGE(TAG,
"Setting PCNT glitch filter failed: %s", esp_err_to_name(error));
131 error = pcnt_unit_add_watch_point(this->
pcnt_unit, INT16_MIN);
132 if (error != ESP_OK) {
133 ESP_LOGE(TAG,
"Adding PCNT low limit watch point failed: %s", esp_err_to_name(error));
136 error = pcnt_unit_add_watch_point(this->
pcnt_unit, INT16_MAX);
137 if (error != ESP_OK) {
138 ESP_LOGE(TAG,
"Adding PCNT high limit watch point failed: %s", esp_err_to_name(error));
142 error = pcnt_unit_enable(this->
pcnt_unit);
143 if (error != ESP_OK) {
144 ESP_LOGE(TAG,
"Enabling PCNT unit failed: %s", esp_err_to_name(error));
147 error = pcnt_unit_clear_count(this->
pcnt_unit);
148 if (error != ESP_OK) {
149 ESP_LOGE(TAG,
"Clearing PCNT unit failed: %s", esp_err_to_name(error));
152 error = pcnt_unit_start(this->
pcnt_unit);
153 if (error != ESP_OK) {
154 ESP_LOGE(TAG,
"Starting PCNT unit failed: %s", esp_err_to_name(error));