ESP32 PCNT and xQueue with 500 nanosecond TICKPERIOD trigger?


I am trying to make a highspeed counter with the ESP32. I am using the espressif PCNT example described in the Espressif PCNT function. (can’t post a link, new user)

The PCNT function says it can count pulses with a pulse duration longer than 12.5 ns using this function my pulses are 20 ns long. I got that to work but I need to “bin” the count’s every 500 ns for 400 microsecond blocks, 800 separate bins. Then do some data analysis on the data. So I need to trigger the xQueueReceive function every 500 ns. I realize this is really fast and will tax the CPU greatly and might have some errors but I am not doing anything else with this processor and want to test this and see what the error is before I decide to use a different chip with a faster processor. Maybe a Teensy 4.0.

res = xQueueReceive(pcnt_evt_queue, &evt, 1 / portTICK_PERIOD_MS);

The fastest I can make the xQueueReceive is 1 millisecond, portTICKPERIOD_MS is too slow. Is there a way to tap into the ESP.cycleclock() to use the 240 MHz clock ticks? or change the portTICK_PERIOD_MS to a different period?


When feeding the queue correctly you shouldn’t have to fiddle around with its receive timeout. Just push the data into it assumingely in an ISR and receive it in a high prio task to process it. You’ll see if the ESP32 can handle it sustained at the desired rate checking the queue push return codes indicating a potential queue overflow.
When considering performance the FreeRTOS stream or message buffer feature might help because it’s faster.