STM32 GPSDO

What are you developing?
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

Re: STM32 GPSDO

Post by AndrewBCN »

dannyf wrote: Sun May 30, 2021 10:29 pm you can build a LPF from a rc network -> he has the lpf on the output from the phase comparator.
No, that circuit is a ns-resolution TIC, not a lpf. Things are a little bit more complicated than a superficial look in Lars' design, you should try to read his code, but I admit that is rather challenging. He clearly has not one, but two low-pass filters implemented in software.
dannyf wrote:
AndrewBCN wrote:Likewise, it's very difficult to come up with a simpler design than mine, if you use the FLL control method. :mrgreen:
I have done a few, from a pic16f based one to LM4F120-based. I may just pick it up - I'm thinking about a ATtiny85 or ATmega328p, given how inexpensive the arduino boards are.
Good luck with that, but I think you'll quickly realize how much brilliant engineering and long hours of work went into Lars' minimalistic design, making it almost impossible to surpass. :geek:
Last edited by AndrewBCN on Mon May 31, 2021 11:34 am, edited 1 time in total.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: STM32 GPSDO

Post by dannyf »

His design uses ADC to see if the phase is stable. ADC typically moves 7 to 8 digits. So he will likely need to filter that out. More work if he used the native analogRead().

The chip has input capture so hopefully he used that instead of the external int or pin change int you often see.

To be fair I will need to read his code.
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

Re: STM32 GPSDO

Post by AndrewBCN »

dannyf wrote: Mon May 31, 2021 11:11 am His design uses ADC to see if the phase is stable.
Not exactly. The AVR ADC input is used to measure the voltage of the 1ns resolution TIC circuit (which you had mistaken for a lpf). That's the reason I call his control loop a "hybrid digital/analog PLL".
These ADC readings are then filtered and further processed in software to generate the OCXO control voltage.
dannyf wrote: ...
To be fair I will need to read his code.
Obviously, if you want to understand how Lars' DIY GPSDO works, you have to read the source code, and thankfully, it's written in C and well commented.
mlundin
Posts: 94
Joined: Wed Nov 04, 2020 1:20 pm
Answers: 6
Location: Sweden

Re: STM32 GPSDO

Post by mlundin »

The TIC circuit, HC4046 plus D1, R1 and C1 measures the phase relation between the oscillator divided down to 1MHz and the PPS1. The output voltage over C! that is then read by the ADC is an increasing (not really linear, more exponentially increasing so the initial slope is more or less linear) function of the time from the PPS to the next 1MHz clock pulse, so indeed it measures the phase.
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

Re: STM32 GPSDO

Post by AndrewBCN »

mlundin wrote: Mon May 31, 2021 12:34 pm The TIC circuit, HC4046 plus D1, R1 and C1 measures the phase relation between the oscillator divided down to 1MHz and the PPS1. The output voltage over C! that is then read by the ADC is an increasing (not really linear, more exponentially increasing so the initial slope is more or less linear) function of the time from the PPS to the next 1MHz clock pulse, so indeed it measures the phase.
Yes, the 1ns resolution TIC circuit outputs once per second a voltage that is more or less proportional to the phase difference between the OCXO 10MHz (divided by 10) and the GPS 1PPS, and that voltage is read by the MCU and then further lp filtered/processed to calculate the duty cycle of the twin-PWM generated Vctl that controls the OCXO frequency.
That's roughly the idea of the hybrid analog/digital PLL in Lar's brilliant design.
Last edited by AndrewBCN on Mon May 31, 2021 2:19 pm, edited 1 time in total.
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: STM32 GPSDO

Post by dannyf »

I spent sometime on his code.

what I like:
1. he indeed used input capture for the 1pps signal. I think a lot of people made the mistake of using external interrupt or pcint for that. not excusable on a chip with input capture.
2. rounding is implemented;
3. his use of two low-reso pwms to compose a his reso pwm is clever - reminds me of the 24-bit adc based voltage reference from Linear.
4. the use of a two stage filter is nice.

what i don't like:
1. lots of floating point math: he certainly could have done without;
2. ram usage is significant due to the arrays: tough to fit that to a smaller chip;
3. he used the stock analogRead() and inside of an isr: the alternative is to run the analog read continuously and smooth it out in the isr. or he can ste up the adc to be triggered by the capture event.

I don't like the general structure of the code - very intertwined and not as modular.
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

Re: STM32 GPSDO

Post by AndrewBCN »

dannyf wrote: Mon May 31, 2021 1:43 pm I spent sometime on his code.
You probably spent more time than I did. I browsed through it weeks ago and very early on noticed that Lars' code is totally inseparable from his hardware design. Also it's definitely not my style.
dannyf wrote: what I like:
1. he indeed used input capture for the 1pps signal. I think a lot of people made the mistake of using external interrupt or pcint for that. not excusable on a chip with input capture.
2. rounding is implemented;
3. his use of two low-reso pwms to compose a his reso pwm is clever - reminds me of the 24-bit adc based voltage reference from Linear.
4. the use of a two stage filter is nice.
...
I don't like the general structure of the code - very intertwined and not as modular.
Well, unfortunately Lars passed away a few years ago, and nobody has really worked on his code since. I think Lars did his best to fit a ton of functionality in the limited flash and ram of the 16-bit AVR MCU and when he got it all working, he decided not to touch the code anymore, a wise decision imho.

I can safely say I have borrowed exactly zero lines of code from Lars's GPSDO for my STM32 GPSDO firmware. :lol:
dannyf
Posts: 447
Joined: Sat Jul 04, 2020 7:46 pm

Re: STM32 GPSDO

Post by dannyf »

Rough sketch, on a PIC32:
//Basic operations:
// 1. 32-bit time base (TMR2/3): Timer 2/3 form a free-running 32-bit timebase for capturing and for 1pps generation (optional for debugging);
// 2. 32-bit input capture (IC1): Input capture 1 used to capture the 1pps, from which frequency measurements are taken;
// 3. 1pps generation(OC2, optional): OC2 in 32-bit mode used to generate a 1pps, for debugging and potential phase comparison with 1pps;
// 4. 16-bit PWM generation (OC1): used to drive the LO
// 5. oscillator to be disciplined is used to drive the mcu;
I can get it to cycle accurate - measuring its own crystal oscillator - to show that it works. I also got it to track a 1pps signal from a 12F675.

the use of 32-bit timer as the time base for capturing made life much easier, as you don't need to deal with atomicity.

the guts of the code:

Code: Select all

		if (pps_arrived) {				//new data as arrived
			pps_arrived = 0;			//reset the flag
			ferr = freq - F_TGT;		//capture the error term
			ferr = avg_exp(ferr);		//calculate the average error term
			dc1  = ferr2dc16(ferr);		//convert freq error to duty cycle (16bit)
			pwm1_setdc(dc1);			//update pwm1 duty cycle

fairly straight forward. the tricky part is that I did the math in terms of frequency error (measured frequency - the target frequency). the implementation is fairly minimalist - 20-ish files in the project and the main() has 50 lines of code + comments. the main loop has the few lines above + 4 lines for uart display. porting it to a blue pill would be fairly easy.

the next step is to hook up a real 1pps source to this thing and see how it goes.
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

STM32 GPSDO Version v0.04a pushed to GitHub

Post by AndrewBCN »

I have just pushed Version v0.04a to GitHub, this is the first version in the 0.04 series. In this series, I intend to improve the general operation of the GPSDO, implement auto-calibration and holdover operating modes, improve internal status reporting, and experiment with various low-pass filter values in the frequency locked loop (FLL).
I have implemented the 10,000s ring buffer and it's testing right now.
Also testing: I increased the size of the HardwareSerial TX and RX buffers from their default of 64 to 256 characters.
Finally, I am testing setting the navigation mode of the u-blox Neo-M8 receiver to "stationary" (I believe the default is "portable"). I configured the receiver manually (using u-center under Windows 10), but intend to implement the UBX command to add this in the STM32 GPSDO firmware as a build option.
In summary, for versions 0.04[a to z] in the coming weeks: no major code rewrite but many improvements in various areas and an exploration of various features and code optimizations.
AndrewBCN
Posts: 105
Joined: Sun Apr 25, 2021 3:50 pm
Answers: 1
Location: Strasbourg, France

Re: STM32 GPSDO

Post by AndrewBCN »

dannyf wrote: Mon May 31, 2021 7:45 pm Rough sketch, on a PIC32:
...
Hi dannyf,
There is at least one, possibly two serious conceptual mistakes in your rough sketch, as you'll immediately realize once you have implemented and tested it. However, I would appreciate it if you could please create a new topic in the "Off topic" section (since it's not STM32 code but PIC32 code) and move your last post there, if you want to discuss your draft code further. Thank you for your help in keeping this thread on the topic of the STM32 GPSDO project.
Post Reply

Return to “Projects”