Clock synchronisation between two F303K8 or F411RE

23 Jun 2016

Hi,

I try today to synchronize to card, F303K8. So there is the idea:

Two F303K8 are listening one a GPIO from an external signal ( PPS from a GPS module) so each second a rising edge comes from the GPS to each F303K8. The idea is when each F303K8 detect a rising edge, they start a Ticker that call a function at a fixed time (here each second (1 000 000 µs) because I need to disconnect the PPS signal from the two card as they will move underwater and there is no GPS signal underwater). At this point we have to Ticker, one on each F303K8 that start a t0 for example, exactly synchronous. But as the time pass, after a few iterations, the two Ticker does not start exactly at the same time.

I tested that this way:

The function called by the ticker create a rising edge on a GPIO, wait 100ms, then create a falling edge. So I have a pulse width of 100ms each second on each F303K8. The goal is that this pulse width will rise and fall EXACTLY at the same time on each card without any connections between them.

Here is a picture of what I can see on my oscilloscope: http://i.imgur.com/vaDzx4k.png

At the beginning C1 and C2 pulse are in sync with the CLK (which is my PPS signal) but when I start creating my pulse with my Ticker instead of waiting a rising edge on CLK, here is what append on C2'

I don't know how to fix it. I tried to start two PWM with the same params on each card to see if it was my code that is bad, but it is the same, I can see that the delay between PWM on first card and the second card is not static in the time.

Do you have any idea where it comes from ? And if I need maybe some card with more precise qwartz ? Or there is a solution to fix this ?

28 Jun 2016

What you are seeing is the clock error of the CPU crystal. The accuracy will be measured in ppm (Parts per million) and a typical part will be rated at 50 to 100ppm.

100ppm gives a possible error of up to 100us per second. Since you have two crystals each with a possible error you could in theory see a relative errors between two units of twice this size.

In practice most crystals will be more accurate than their rating and two identical crystals at the same temperature will tend to have the same errors so I'd guess you're probably getting a drift of 10-20 us per second between the two.

How can you improve this?

First of all you need to answer the question of how close do the clocks need to be? There is no such thing as perfect synchronization but if you put a good atomic clock in both systems then the drift would be well under a nanosecond per year. Obviously using atomic clocks is going to be large, power hungry and expensive, it becomes a trade off, given that perfect is impossible how good do you need it to be?

The next step up from a normal quartz crystal is a TCXO (Temperature compensated oscillator), this is a normal crystal with some circuitry to tune it and compensate for temperature effects. They are typically in the 2-10ppm range.

An OCXO (oven compensated oscillator) solves the temperature problems by heating the crystal up so that it's always at exactly the same temperature. They can get you down to 0.1 - 1 ppm.

Both a TCXO and OCXO would require you to physically change the clock on the F303K8.

There are two options I can think of to make this work without physical changes to the boards.

1) Measure the clock error.

Get each board to measure the time in us between two PPS signals from the GPS or even better measure the time taken for 100 PPS pulses. In theory you should see 1,000,000us per PPS, in practice you'll measure a different number because the CPU clock is running at a slightly incorrect speed. You can now calculate the CPU clock error and adjust your ticker period accordingly. Make sure that the GPS has a good position fix before you start this process, it's PPS isn't valid until it has a position. I've seen the PPS jump by a few ms about 10 seconds after a GPS starts outputting positions so play safe and give it plenty of time to lock on accurately.

2) Trust the GPS.

Use two GPS systems, one in each unit. In order for a GPS to calculate a position it needs to constantly calculate the error in it's own clock. If it then loses GPS lock it will assume the last error it calculated is still correct until it starts to see GPS again. This results in a very accurate clock initially but it will slowly drift off as the clock error changes from the calculated value.

If you use the PPS output of two GPS receivers they will be perfectly in sync (within a few ns) once they have a good position lock and are calculating locations. Once they lose GPS they will start to drift but by a lot less than a normal crystal. Assuming their temperature doesn't change significantly they should be at least as good as a reasonable quality TCXO. How well they stay in sync will depend on the GPS engine used, some will be better than others.

This has the added advantage that you don't need to start with the two systems connected to each other.

Also keep in mind with either of these options that vibration and temperature changes can change the clock frequency so you need to minimize them as much as possible.