[SOLVED] ESP8266, read 80 [MHz] counter

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
User avatar
wimpie
Posts: 14
Joined: Sun Jul 05, 2020 11:14 am
Location: The Netherlands

[SOLVED] ESP8266, read 80 [MHz] counter

Post by wimpie » Thu Oct 01, 2020 8:44 am

The intent is to build a device which monitors the mains frequency, which is about 50 [Hz] over here. The device should use less than 0.5 [W], which is a reason to use an ESP8266 microcontroller. When the sine wave is converted to a digital signal, the time it takes to receive N pulses needs to be determined. In article https://sub.nanona.fi/esp8266/timing-and-ticks.html a CPU register named CCOUNT is mentioned which is incremented each 12.5 [ns]. This counter would be a fine source to determine the elapsed time.

Searching for ways to access the counter from within microPython did not result in a solution. My question is therefore if it is possible to retrieve this counter in microPython (in an interrupt service routine (ISR)).
Last edited by wimpie on Thu Oct 01, 2020 8:04 pm, edited 1 time in total.

User avatar
Roberthh
Posts: 3667
Joined: Sat May 09, 2015 4:13 pm
Location: Rhineland, Europe

Re: ESP8266, read 80 [MHz] counter

Post by Roberthh » Thu Oct 01, 2020 4:52 pm

With Viper code you should be able to access the register directly, if it is memory mapped. But, sorry for that, I have a different concern:
Since on the ESP8266 with MicroPython the variation of ISR latency time is several 100 µs, looking for 12.5ns resolution is not needed. You can go as well for time.ticks_us(). Even that requires, that the bandwidth of the circuit which creates digital transition from the sine wave if in the order of 1 MHz, and that the jitter of that circuit is smaller than 1 µs.
What should work is counting the edges in the ISR and get the time with tick_us() when sufficient many edges have been seen. How long, depends on the precision needed and the ISR latency. Polling should give you a lower latency. For a precision of 1 mHz you would need a test time of 100 to 1000 seconds.


Interesting links about mains frequency monitoring:
https://gridradar.net/wide-area-monitoring-system.html
https://www.netzfrequenzmessung.de/

User avatar
wimpie
Posts: 14
Joined: Sun Jul 05, 2020 11:14 am
Location: The Netherlands

Re: ESP8266, read 80 [MHz] counter

Post by wimpie » Thu Oct 01, 2020 8:03 pm

I am not looking for a resolution of about 10 [ns]. Such a resolution is much, much better than I need. The target is to get an accuracy of the measured frequency of 10 [mHz]. This implies that the duration of one period must be measured with an accuracy of 4 [µs].

Given a variation of the ISR latency of about 100 [µs] (thanks for that figure) and assuming that the variations of the ISR latency are statistically independent, the minimum number of periods in one measurement must exceed 100 * √2 / 4 ≈ 37. Thus measuring the duration of 40 periods, which is about 2 [s], should give the required accuracy.
If the variation of the ISR latency is up to 400 [µs], measuring the duration of 141 periods, which is about 7 [s], should suffice.

Reading the most recent version of microPython documentation showed a method named utime.ticks_cpu(), which is new to me. A quick test showed that ticks_cpu is effectively the special register CCOUNT.

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: [SOLVED] ESP8266, read 80 [MHz] counter

Post by pythoncoder » Wed Oct 07, 2020 7:42 am

I would seriously consider a Pyboard. They use less than 0.5W and have vastly shorter interrupt latency which is highly consistent. In my experience ESPx interrupt latency is extremely variable. You can build real measuring instruments with Pyboards. See viewtopic.php?f=5&t=3990 and viewtopic.php?f=5&t=4159&p=23923 both based on Pyboard 1.1.
Peter Hinch
Index to my micropython libraries.

User avatar
rcolistete
Posts: 352
Joined: Thu Dec 31, 2015 3:12 pm
Location: Brazil
Contact:

Re: [SOLVED] ESP8266, read 80 [MHz] counter

Post by rcolistete » Fri Oct 23, 2020 2:13 am

Pyboard Lite v1.0 uses less energy. Both Pyboard v1.1 and Lite v1.0 can be underclocked to save energy :
https://store.micropython.org/pyb-features
My "MicroPython Samples". My "MicroPython Firmwares" with many options (double precision, ulab, etc).

Post Reply