How do I measure frequency using MCU timers? (STM32F0) - Page 1
Forum section: electronics / general

author: technix
author_rank: Super Contributor
timestamp: August 04, 2019, 05:04:48 pm
content: Two of my projects need to measure frequency of some clock signal and act upon it:* M107v1 USB Color Sensor: it uses a STM32F042F6P6 to measure the frequency output of TCS3200 color sensor relative to its own 8MHz ceramic resonator (which also provides its own 48MHz system clock and USB clock over a PLL,) then turn it into USB HID packets for host computer to consume.* M177v4 Raspberry Pi GPS HAT: it uses a STM32F030F4P6 to measure the nominally 32768Hz frequency output of DS3231 TCXO RTC relative to PPS pulse from a GPS module, then in turn trim the RTC clock with that.How to do that? I do know it needs chaining two clocks up together, but how?

author: thinkfat
author_rank: Unknown
timestamp: August 04, 2019, 05:19:02 pm
content: There are variants of the STM32F0 which can discipline their internal RTC with an external pps pulse. You could just do that and suppress the external RTC entirely. Or is hold-over with GPS loss so critical that you need an external, tcxo controlled RTC?EDIT TO ADD:Regarding frequency measurement, you could use one timer in external clock mode to count edges from the color sensor output, and another timer to provide a capture event. The timers in the STM32F0xx can also be used to measure pulse width of an external signal, if that's precise enough for you.Updated:I didn't find STM32F0xx that would sync against a PPS signal, only 50Hz or 60Hz, which is ideal for applications where you have a mains input somewhere. Not for GPS though.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: August 04, 2019, 10:42:49 pm
content: Quote from: technix on August 04, 2019, 05:04:48 pmThis is unclear, as you mention 2 projects, but then give examples that appear to already do what you ask ?Can you ask a better question ?Do you already have M177v4 Raspberry Pi GPS HAT & M107v1 USB Color Sensor both connected to a Pi ?

author: technix
author_rank: Super Contributor
timestamp: August 05, 2019, 03:59:54 pm
content: Quote from: PCB.Wiz on August 04, 2019, 10:42:49 pmThese two projects are irrelevant, however in both projects a frequency measurement mechanism with a 20-pin STM32F0 is needed. Those are the goals of the projects, but at their cores the frequency measurement mechanism has yet to be implemented.Quote from: thinkfat on August 04, 2019, 05:19:02 pmThe RTC is not for the STM32 to use, instead it is for the Pi as a stratum 1 NTP server. The STM32 trims the TCXO against the GPS when it gained a fix, so the Pi can have a second PPS source when GPS is lost.Quote from: thinkfat on August 04, 2019, 05:19:02 pmAny pointer on implementing that edge counter + capture event structure?

author: PCB.Wiz
author_rank: Super Contributor
timestamp: August 05, 2019, 07:27:54 pm
content: Quote from: technix on August 05, 2019, 03:59:54 pmWhat frequency range do you plan to measure, and with what precision and measurement rate ?If you know the frequency under test (like the 32kHz example you quote) you can use a simple capture timer, to count cycles.eg In another thread, it was shown 61 seconds of 32.768kHz resolves to 0.5ppm, and a 16b timer is enough to capture the expected deviations, as youknowthe MSBs are stable.If you want a more generic Frequency Counter, (Reciprocal Counter) then you may need two capture systems, one forTime, and one forCyclesBoth channels capture over some whole number of Fi cycles, (on the same edge) and you then determineFrequency = Cycles/TimeAbove some MHz limit, you will need an external prescalerThis has a better dynamic range on parts with a one-shot Dual Chan arm->capture design, which is rare on MCUs, but can be created in parts with Configurable Logic cells.

author: thinkfat
author_rank: Unknown
timestamp: August 05, 2019, 07:51:25 pm
content: Quote from: technix on August 05, 2019, 03:59:54 pmOK, so the DS3231 will not interface with the I2C of the Pi, only providing a PPS signal? Otherwise - you will need to arbitrate accesses between the micro and the Pi to the DS3231, because it only has one control interface.Also, the possibilities to "trim" the TCXO are not very extensive, it seems there is only an "Aging Offset" register that you can use, signed 8 bit value, LSB worth 0.1ppm of change, so at max you can trim +12.7ppm / - 12.8ppm.Quote from: technix on August 05, 2019, 03:59:54 pmWell, just have a look at the register manual. For example, for the PPS sync, you could use e.g. TIM1, configure the counter into free-running auto-reload mode, connect each PPS signal to a TIM1_CHx input and configure the two linked capture/compare stages to capture the counter at each rising edge of the PPS pulse. Then, subtract the two captured counter values. If the result changes, you have a drift and you can use that information to trim the RTC.

author: MosherIV
author_rank: Super Contributor
timestamp: August 05, 2019, 08:15:43 pm
content: HiGenerally speaking, there are 2 methods for measuring freq1. Gated period method. Already described by most people, set up 1 timer say to 1 secound, use 2nd counter to count the edges of signal until 1st timer triggers. The count is the freq2. Recipricle method. Set up 1 very high speed clock to a counter, start and stop the counter on the rising edge of the input signal. Divide 1 by the count x hs clock period.

author: PCB.Wiz
author_rank: Super Contributor
timestamp: August 05, 2019, 08:37:50 pm
content: Quote from: MosherIV on August 05, 2019, 08:15:43 pmClose, the more general Reciprocal counter captures time over some whole number of Fi cycles, usually larger than some nominal 'gate time'.If you capture the SysCLK rather than start & stop timers, you can use multiple results to improve precision, asyou do not drop any SysCLKsMeasuring over a single cycle, loses precision at high frequency inputs, whilst a design that measures whole cycles in say (>200ms) will give N digits/second. independent of FiAs an example, let's measure 33Hz with a 10MHz SysCLK and a (>200ms) gate time.That will capture 7 whole cycles, in actually 212.1212121ms, the 10M SysCLK capture is (mostly) 2121212, and the reported frequency is10M*7/2121212 = 33.000001885HzRarely, (actually 4 out of every 33 captures) it will capture10M*7/(2121212+1) = 32.99998632Hz, and the average will be 33.0000HzThe next step possible, the other way is10M*7/(2121212-1) = 33.00001744Hz, for a Counter LSB of 0.471ppm

author: technix
author_rank: Super Contributor
timestamp: August 06, 2019, 03:49:33 am
content: Quote from: PCB.Wiz on August 05, 2019, 07:27:54 pmFor the GPS HAT, it is 32768Hz nominal against 1Hz PPS pulse.For the color sensor, is is somewhere around 100-600kHz against either a 8MHz ceramic resonator or a 48MHz internal clock PLL'd from said ceramic resonator.Quote from: thinkfat on August 05, 2019, 07:51:25 pmDS3231 will interface with the Pi over I2C. And I am arbitrating it.Quote from: thinkfat on August 05, 2019, 07:51:25 pmI don't need extensive trimming anyway.Quote from: thinkfat on August 05, 2019, 07:51:25 pmThanks.