59 static pcnt_unit_t next_pcnt_unit = PCNT_UNIT_0;
60 static pcnt_channel_t next_pcnt_channel = PCNT_CHANNEL_0;
65 next_pcnt_unit = pcnt_unit_t(
int(next_pcnt_unit) + 1);
66 if (
int(next_pcnt_unit) >= PCNT_UNIT_0 + PCNT_UNIT_MAX) {
67 next_pcnt_unit = PCNT_UNIT_0;
68 next_pcnt_channel = pcnt_channel_t(
int(next_pcnt_channel) + 1);
72 " PCNT Unit Number: %u\n"
73 " PCNT Channel Number: %u",
76 pcnt_count_mode_t rising = PCNT_COUNT_DIS, falling = PCNT_COUNT_DIS;
79 rising = PCNT_COUNT_DIS;
82 rising = PCNT_COUNT_INC;
85 rising = PCNT_COUNT_DEC;
90 falling = PCNT_COUNT_DIS;
93 falling = PCNT_COUNT_INC;
96 falling = PCNT_COUNT_DEC;
100 pcnt_config_t pcnt_config = {
101 .pulse_gpio_num = this->pin->
get_pin(),
102 .ctrl_gpio_num = PCNT_PIN_NOT_USED,
103 .lctrl_mode = PCNT_MODE_KEEP,
104 .hctrl_mode = PCNT_MODE_KEEP,
112 esp_err_t error = pcnt_unit_config(&pcnt_config);
113 if (error != ESP_OK) {
114 ESP_LOGE(TAG,
"Configuring Pulse Counter failed: %s", esp_err_to_name(error));
119 uint16_t filter_val = std::min(
static_cast<unsigned int>(this->
filter_us * 80u), 1023u);
120 ESP_LOGCONFIG(TAG,
" Filter Value: %" PRIu32
"us (val=%u)", this->
filter_us, filter_val);
121 error = pcnt_set_filter_value(this->
pcnt_unit, filter_val);
122 if (error != ESP_OK) {
123 ESP_LOGE(TAG,
"Setting filter value failed: %s", esp_err_to_name(error));
126 error = pcnt_filter_enable(this->
pcnt_unit);
127 if (error != ESP_OK) {
128 ESP_LOGE(TAG,
"Enabling filter failed: %s", esp_err_to_name(error));
133 error = pcnt_counter_pause(this->
pcnt_unit);
134 if (error != ESP_OK) {
135 ESP_LOGE(TAG,
"Pausing pulse counter failed: %s", esp_err_to_name(error));
138 error = pcnt_counter_clear(this->
pcnt_unit);
139 if (error != ESP_OK) {
140 ESP_LOGE(TAG,
"Clearing pulse counter failed: %s", esp_err_to_name(error));
143 error = pcnt_counter_resume(this->
pcnt_unit);
144 if (error != ESP_OK) {
145 ESP_LOGE(TAG,
"Resuming pulse counter failed: %s", esp_err_to_name(error));