Please help me understand this code

if you still have problems consider using the ESP32 PCNT (Pulse Counter)
each pulse counter has two inputs pulse count and level enable (which enables counting during either high or low levels)

code (EDIT: display frequency of test signal)

// ESP32 - 
// Arduio forum https://forum.arduino.cc/t/please-help-me-understand-this-code/1381333/9

// 1. measure pulse width and rising edge-to-edge time using GPIO 16 and 17
// 2. using PCNT count pulses on GPIO 4 while signal on GPIO 16 is high

// https://docs.espressif.com/projects/esp-idf/en/stable/esp32/api-reference/peripherals/pcnt.html

#include "driver/pulse_cnt.h"
#include "driver/gpio.h"
#include "esp_err.h"

// define input GPIO pins
#define PULSE_COUNT_PIN 4     // measure pulse count
#define PULSE_LEVEL_PIN_1 16  // measure pulse width and rising edge-to-edge time
#define PULSE_LEVEL_PIN_2 17

// PCNT pulse counter definitions
#define EXAMPLE_PCNT_HIGH_LIMIT 20000
#define EXAMPLE_PCNT_LOW_LIMIT -100

pcnt_unit_config_t unit_config = {
  .low_limit = EXAMPLE_PCNT_LOW_LIMIT,
  .high_limit = EXAMPLE_PCNT_HIGH_LIMIT,
  .flags = {
    .accum_count = 1,
  }
};
pcnt_unit_handle_t pcnt_unit = NULL;

pcnt_chan_config_t chan_config = {
  .edge_gpio_num = PULSE_COUNT_PIN,
  .level_gpio_num = PULSE_LEVEL_PIN_1,
};
pcnt_channel_handle_t pcnt_chan = NULL;

// timer to measure pulse width and rising edge-to-edge time
hw_timer_t *Timer1_Cfg = NULL;  // timer object

// pulse timing data
volatile uint64_t riseTime, period, width;
// rising edge interrupt calculate period uSec
void IRAM_ATTR rising() {
  //pcnt_unit_clear_count(pcnt_unit); // clear PCNT count
  uint64_t rise = timerReadMicros(Timer1_Cfg);
  period = rise - riseTime;
  riseTime = rise;
}

// falling edge interrupt calculate pulse width uSec
int pulse = 0;
void IRAM_ATTR falling() {
  width = timerReadMicros(Timer1_Cfg) - riseTime;
  pulse = 1;
}

void setup(void) {
  Serial.begin(115200);
  delay(2000);
  Serial.println("\n\nESP32 pulse counter using PCNT unit");

  Serial.println("setup timer and GPIO interrupts");
  if ((Timer1_Cfg = timerBegin(1000000)) == NULL)  // API 3.0 setup timer for 1uSec
    Serial.println("timerBegin Failed!!");
  Serial.print("timerBegin() OK frequenmcy ");
  Serial.println(timerGetFrequency(Timer1_Cfg));
  //  setup interrput routines - connect signal to pins 16 and 17
  attachInterrupt(digitalPinToGPIONumber(PULSE_LEVEL_PIN_1), rising, RISING);    // detect rising edge on pin 16
  attachInterrupt(digitalPinToGPIONumber(PULSE_LEVEL_PIN_2), falling, FALLING);  // detect falling edge on pin 17

  Serial.println("Setup PCNT (pulse Counter) ");
  Serial.println("install pcnt unit");
  ESP_ERROR_CHECK(pcnt_new_unit(&unit_config, &pcnt_unit));
  Serial.println("install pcnt channels");
  ESP_ERROR_CHECK(pcnt_new_channel(pcnt_unit, &chan_config, &pcnt_chan));
  Serial.println("set edge and level actions for pcnt channels");
  ESP_ERROR_CHECK(pcnt_channel_set_edge_action(pcnt_chan, PCNT_CHANNEL_EDGE_ACTION_HOLD, PCNT_CHANNEL_EDGE_ACTION_INCREASE));
  ESP_ERROR_CHECK(pcnt_channel_set_level_action(pcnt_chan, PCNT_CHANNEL_LEVEL_ACTION_KEEP, PCNT_CHANNEL_LEVEL_ACTION_HOLD));
  Serial.println("add watch point");
  ESP_ERROR_CHECK(pcnt_unit_add_watch_point(pcnt_unit, EXAMPLE_PCNT_HIGH_LIMIT));

  Serial.println("enable pcnt unit");
  ESP_ERROR_CHECK(pcnt_unit_enable(pcnt_unit));
  Serial.println("clear pcnt unit");
  ESP_ERROR_CHECK(pcnt_unit_clear_count(pcnt_unit));
  Serial.println("start pcnt unit");
  ESP_ERROR_CHECK(pcnt_unit_start(pcnt_unit));
}

// display pulse count on falling edge of GPIO 16
void loop() {
  if (pulse == 0) return;  // on pulse falling edge display results

  static int pulse_count_old = 0, pulse_count = 0;
  pcnt_unit_get_count(pcnt_unit, &pulse_count);  // read PCNT pulse count
  Serial.printf("test Pulse count: %d frequency %dHz\n", pulse_count - pulse_count_old
                       , ( pulse_count - pulse_count_old) * 10000);
  pulse_count_old = pulse_count;
  Serial.printf("period %llduSec width = %llduSec  frequency %.2fHz\n",
                period, width, 1000000.0 / period);
  pulse = 0;
}

signals (testing 100KHz pulse input)

  1. pulse frequency 1Hz duty cycle 0.01 (100uSec pulse) on GPIO 16 and 17 - yellow trace
  2. square wave for counting on GPIO 4 - tested with frequencies 100KHz to 20MHz - blue trace

the oscilloscope measures the pulse on GPIO 16 an 17 as width 99uSec which is reflected in results below

test results

GPIO 16/17 1Hz duty cycle 0.01 (100uSec) 

GPIO 4 100KHz test signal

test Pulse count: 10 frequency 100000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 10 frequency 100000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 10 frequency 100000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 10 frequency 100000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 10 frequency 100000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 10 frequency 100000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz

----------------------------------------------------------
GPIO 4 1MHz test signal

test Pulse count: 99 frequency 990000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 100 frequency 1000000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 99 frequency 990000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 99 frequency 990000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 99 frequency 990000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 99 frequency 990000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 100 frequency 1000000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz

-------------------------------------------------------------------
GPIO 4 2MHz test signal

test Pulse count: 198 frequency 1980000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 199 frequency 1990000Hz
period 999993uSec width = 97uSec  frequency 1.00Hz
test Pulse count: 198 frequency 1980000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 199 frequency 1990000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 198 frequency 1980000Hz

-------------------------------------------------------
GPIO 4 4MHz test signal

test Pulse count: 396 frequency 3960000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 396 frequency 3960000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 397 frequency 3970000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 397 frequency 3970000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 397 frequency 3970000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 396 frequency 3960000Hz

--------------------------------------------------------------------------
GPIO 4 10MHz test signal

test Pulse count: 992 frequency 9920000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 992 frequency 9920000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 992 frequency 9920000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 992 frequency 9920000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 992 frequency 9920000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 992 frequency 9920000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 992 frequency 9920000Hz

-----------------------------------------------------------------
GPIO 4 20MHz test signal

test Pulse count: 1984 frequency 19840000Hz
period 999992uSec width = 99uSec  frequency 1.00Hz
test Pulse count: 1984 frequency 19840000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 1984 frequency 19840000Hz
period 999993uSec width = 97uSec  frequency 1.00Hz
test Pulse count: 1984 frequency 19840000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 1984 frequency 19840000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 1983 frequency 19830000Hz

-----------------------------------------------------------------
GPIO 4 30MHz test signal

test Pulse count: 2976 frequency 29760000Hz
period 999992uSec width = 99uSec  frequency 1.00Hz
test Pulse count: 2975 frequency 29750000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 2975 frequency 29750000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 2975 frequency 29750000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 2975 frequency 29750000Hz
period 999992uSec width = 98uSec  frequency 1.00Hz
test Pulse count: 2975 frequency 29750000Hz
period 999993uSec width = 98uSec  frequency 1.00Hz