ESP32 guru meditation error core 1 panic'ed

Hello all,
I am trying to read 50Hz pulses on GPIO34 of my ESP32.
When trying to print the results on the serial, I am getting some errors like the name of this topic, and after that I am able to see the results being printed out normally.
Anyone any ideas?

int FC_PIN = 34; // Pin for frequency measurement

void Get_Cycle_Length(void* arrow) {
  for (;;) {
    volatile int64_t start_time, end_time, cycle_duration;
    float frequency = 0.0;

    noInterrupts();
    
    // Wait for the signal to go LOW (start of cycle)
    while (digitalRead(FC_PIN) != LOW) { }
    
    // Wait for the LOW phase to end (HIGH phase begins)
    while (digitalRead(FC_PIN) == LOW) { }

    // Capture start time at the beginning of the HIGH phase
    start_time = esp_timer_get_time();
    
    // Wait for the HIGH phase to end
    while (digitalRead(FC_PIN) == HIGH) { }

    // Wait for the LOW phase to end again (completing one full cycle)
    while (digitalRead(FC_PIN) == LOW) { }

    // Capture end time at the beginning of the next HIGH phase
    end_time = esp_timer_get_time();
    
    // Calculate cycle duration in microseconds
    cycle_duration = end_time - start_time;

    interrupts();
    
    // Calculate frequency in Hz (1,000,000 microseconds = 1 second)
    if (cycle_duration > 0) {
      frequency = 1000000.0 / cycle_duration;
    }

    // Print the frequency on the Serial monitor
    Serial.print("Frequency: ");
    Serial.print(frequency, 2);  // Display frequency with 2 decimal places
    Serial.println(" Hz");

    delay(50); // Update every 500ms
  
  }
}


void setup() {
  // Initialize serial communication
  Serial.begin(115200);
  
  // Setup the pin for input
  pinMode(FC_PIN, INPUT);
  
  // Allow system to stabilize
  delay(10);

  // Create a FreeRTOS task for frequency measurement
  xTaskCreate(Get_Cycle_Length, "Cycle_Length", 8192, NULL, 4, NULL);
}

void loop() {
  // Nothing in loop, task handles everything
}

Thanks!

It's a really bad idea to have blocking code with interrupts disabled on an ESP32. You might be better off trying the input capture feature of the chip's Motor Control Pulse Width Modulator (MCPWM) peripheral.

ESP32 has a hardware pulse counter. Below is the example which counts pulses on pin 34 for 1 second and then print result.

#include <Arduino.h>
#include "driver/gpio.h"
#include "driver/pcnt.h"


#define FC_PIN 34


void setup() {
  
  Serial.begin(115200);
  pinMode(FC_PIN,INPUT);

}


unsigned int 
count_pulses(unsigned int pin, unsigned int duration) {

  
  int16_t count;
  pcnt_config_t cfg;

  memset(&cfg, 0, sizeof(cfg));

  cfg.pulse_gpio_num = pin;
  cfg.ctrl_gpio_num = -1;
  cfg.channel = PCNT_CHANNEL_0;
  cfg.unit = PCNT_UNIT_0;
  cfg.pos_mode = PCNT_COUNT_INC;
  cfg.neg_mode = PCNT_COUNT_DIS;

  pcnt_unit_config(&cfg);
  pcnt_counter_pause(PCNT_UNIT_0);
  pcnt_counter_clear(PCNT_UNIT_0);

  pcnt_counter_resume(PCNT_UNIT_0);
  delay(duration);
  pcnt_counter_pause(PCNT_UNIT_0);

  pcnt_get_counter_value(PCNT_UNIT_0, &count);

  return (unsigned int)count;
}


void loop() {

    Serial.printf("%u pulses per second\r\n",count_pulses(FC_PIN,1000));
    delay(3000);
}