82 pcnt_unit_config_t unit_config = {
83 .low_limit = INT16_MIN,
84 .high_limit = INT16_MAX,
85 .flags = {.accum_count =
true},
87 esp_err_t error = pcnt_new_unit(&unit_config, &this->
pcnt_unit);
88 if (error != ESP_OK) {
89 ESP_LOGE(TAG,
"Creating PCNT unit failed: %s", esp_err_to_name(error));
93 pcnt_chan_config_t chan_config = {
94 .edge_gpio_num =
static_cast<gpio_num_t
>(this->pin->
get_pin()),
95 .level_gpio_num = GPIO_NUM_NC,
98 if (error != ESP_OK) {
99 ESP_LOGE(TAG,
"Creating PCNT channel failed: %s", esp_err_to_name(error));
103 pcnt_channel_edge_action_t rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
104 pcnt_channel_edge_action_t falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
107 rising = PCNT_CHANNEL_EDGE_ACTION_HOLD;
110 rising = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
113 rising = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
118 falling = PCNT_CHANNEL_EDGE_ACTION_HOLD;
121 falling = PCNT_CHANNEL_EDGE_ACTION_INCREASE;
124 falling = PCNT_CHANNEL_EDGE_ACTION_DECREASE;
128 error = pcnt_channel_set_edge_action(this->
pcnt_channel, rising, falling);
129 if (error != ESP_OK) {
130 ESP_LOGE(TAG,
"Setting PCNT edge action failed: %s", esp_err_to_name(error));
135 uint32_t max_glitch_ns = PCNT_LL_MAX_GLITCH_WIDTH * 1000u / ((
uint32_t) esp_clk_apb_freq() / 1000000u);
136 pcnt_glitch_filter_config_t filter_config = {
137 .max_glitch_ns = std::min(this->
filter_us * 1000u, max_glitch_ns),
139 error = pcnt_unit_set_glitch_filter(this->
pcnt_unit, &filter_config);
140 if (error != ESP_OK) {
141 ESP_LOGE(TAG,
"Setting PCNT glitch filter failed: %s", esp_err_to_name(error));
146 error = pcnt_unit_add_watch_point(this->
pcnt_unit, INT16_MIN);
147 if (error != ESP_OK) {
148 ESP_LOGE(TAG,
"Adding PCNT low limit watch point failed: %s", esp_err_to_name(error));
151 error = pcnt_unit_add_watch_point(this->
pcnt_unit, INT16_MAX);
152 if (error != ESP_OK) {
153 ESP_LOGE(TAG,
"Adding PCNT high limit watch point failed: %s", esp_err_to_name(error));
157 error = pcnt_unit_enable(this->
pcnt_unit);
158 if (error != ESP_OK) {
159 ESP_LOGE(TAG,
"Enabling PCNT unit failed: %s", esp_err_to_name(error));
162 error = pcnt_unit_clear_count(this->
pcnt_unit);
163 if (error != ESP_OK) {
164 ESP_LOGE(TAG,
"Clearing PCNT unit failed: %s", esp_err_to_name(error));
167 error = pcnt_unit_start(this->
pcnt_unit);
168 if (error != ESP_OK) {
169 ESP_LOGE(TAG,
"Starting PCNT unit failed: %s", esp_err_to_name(error));