How to calculator input pulse frequency (100kHz)

Hello,

I am working on a project where I want to calculate the frequency of an input signal (90-110 KHz). I used a Function Generator(FG) to generate a square wave. FG is connected to pin 2 of Teensy 3.1.

I wrote the following code and I am able to display the correct frequency on the serial terminal. However when the input frequency is higher than 20KHz, the teensy doesn't display the right frequency even at the fastest clock. How can I fix this?

I decided to use Teensy 4.1 for this instead of 3.1. When I tried the same program on a 4.1, it doesn't even read the right frequency for 1Hz. It never read the right frequency. Not sure why. I even tried the common frequency for 3.1 and 4.1, 24MHz. Still, the discrepancy persists.

Is the 4.1 sampling rate worse than 3.1?

I also tried the code I found outline, the same problem persists. Till 20 Hz, it's fine. After that it's distorted.

HIGH and LOW are telling me the status of the pulse. Ideally, it should alternate. It does on 3.1 for <20Hz frequency.

For 4.1 its never the case.

Did you forget something?

What do you see when using the teensy FreqCount library.
https://www.pjrc.com/teensy/td_libs_FreqCount.html

Input is on pin 13.

The Arduino UNO has an Input Capture Register on Timer1. It can capture the timer count when it gets a signal on the Input Capture pin. The timer can run at 16 MHz so measuring frequency at 100 kHz would be easy. Perhaps the Teensy processor has a similar hardware feature.