High Frequency counter in ESP8266 not using interrupt

Thread Starter

mar10

Joined Mar 23, 2019
68
In relation to my project as documented in the Wireless&RF section , I need to solve how to count a high speed set of pulses using an ESP8266 running on esp-open-rtos.
The article here already confirms that this is a true challenge and I am actually looking for another approach.
I hope that the ESP8266 has a way to use the counter that is normally used for a timer can also be triggered by a GPIO input instead of the CPU clock/divider.
My code would reset the counter, trigger the LC circuit to generate pulses every 3µs and after 100µs or whenever convenient I would read out the counter value. Depending on the position of the water meter dial, that counter value would vary between 5 and 30.
The key reason I cannot use an interrupt based approach is that handling an interrupt supposedly is going to take longer than the pulse distance and it would also be hogging the CPU (see article referenced above).

The challenge I have is that these counters are very badly documented and I am not sure if the ESP8266 can be programmed as proposed.
In the latter case I would probably introduce a hardware counter, but that feels like a pity if we could do it on the ESP8266.

Anyone knows if this plan is possible, and if so, some hints would be nice ;-)
PS. there might be a problem if this is not compatible with the I2S output, which I use to generate the LC trigger signal (V(n012) and V(n002)).

Figure 1: The objective is to tell how many pulses are at V(out) after a trigger, the time they take is not relevant.
Screenshot 2021-08-23 at 22.28.53.png
 

mckenney

Joined Nov 10, 2018
123
I'm not familiar with the ESP8266, but I have done this with other MCUs. Skimming the TRM (V1.7) I'm not optimistic.

The keywords here are "external clock source [for timer/counter]". It looks like FRC1 (PWM) always uses the system clock as its source. FRC2 (IR) is less explicit, but since it seems to be doing a 38kHz demodulation I suspect it also (always) uses the system clock.

If you're really running at 80MHz, that's 3*80=240 CPU clocks per pulse, which might be enough to take a GPIO interrupt and increment a counter variable. This would eat into your CPU budget, but it looks(?) like the pulses only appear sporadically in bursts, so maybe you can schedule around it.
 

Thread Starter

mar10

Joined Mar 23, 2019
68
Thanks mckenny,

You are confirming my suspicions and it seems like a pity that they did not cater for this scenario.
I will probably test how far I get, but am afraid it will be a border case, making the results unreliable.
I have my eye on a 74HC590 which would work fine at the cost of a lot of IO lines (6 or 7) to get it done.
 
You could use say a small micrcontroller to count the pulses and use SPI or I2C to read the count when needed. That would cut down on your IO usage. Maybe one other pin to tell the controller whether to count or not.
 

Thread Starter

mar10

Joined Mar 23, 2019
68
I think I can save IO lines even more by using the following trick on the 74HC590.

Considering it has a ripple carry output RCO, I would need 3 pins. One to read the RCO, one to reset and one that drives the clock of the counter in parallel with the LC circuit output. When it is time to read the value of the counter, I will trigger the clock as often till via the RCO it reports an interrupt. The amount of pulses needed for that is 256 minus the counter value. I have a lot of time until the next measurement is needed, like 50ms.

Still curious though if there is not a way to do the original plan with driving the counter of the ESP from GPIO...
 

Thread Starter

mar10

Joined Mar 23, 2019
68
@michael8, this is an analogue technique which sounds as an interesting idea. If we pursue it, I will discuss it in the wireless forum.

Still curious though if there is not a way to do the original plan with driving the counter of the ESP from GPIO...
 

geekoftheweek

Joined Oct 6, 2013
737
I did a little looking around out of curiosity. I've used the ESP8266 for a few projects, but never really looked into what exactly they can do. I mostly use them to convert WiFI to I2C and crunch numbers instead of doing floating point on PICs.

I found https://www.espressif.com/sites/default/files/documentation/esp8266-technical_reference_en.pdf which gives a pretty good idea of the possibilities. Long story short there isn't a way in hardware to directly tie an input to a counter like many other controllers. It's all going to have to happen in software (other than interrupts) which as you have found gets harder with faster signals.
 
Top