I'm using 3 STM32F401RE to synchronize their clocks. My desired accuracy is of 1ms and even lesser like 0.1ms. This device is connected to PC via USB port.

Question 1 :

So I would like to know if I can synchronize each one of them with the PC Clock then they would be synchronized?

Question 2 :

Right now I am using RTC and the clock start counting since power up. Can I for example configure it to start counting with a command (for example with a python script activate it?)

  • \$\begingroup\$ What is your desired accuracy? \$\endgroup\$ – Bence Kaulics Apr 12 '16 at 9:33
  • \$\begingroup\$ My desired accuracy is of 1ms and even lesser like 0.1ms. \$\endgroup\$ – Youssef Kamoun Apr 12 '16 at 9:35
  • \$\begingroup\$ 0.1ms over which period of time? You have a register in STM32 to adjust RTC. Other way could be using GPS disciplined clock, instead. \$\endgroup\$ – Marko Buršič Apr 12 '16 at 9:47
  • \$\begingroup\$ about the RTC in STM32,it is configured and works great,but I have three STM32 and since RTC starts since power on ,then they won't have the same clock= not sycnhronized,that is why i want either to activate manually the Clock through a python script for example or synchronize the clock of the Pc with the one in the STM32 . \$\endgroup\$ – Youssef Kamoun Apr 12 '16 at 9:49
  • 1
    \$\begingroup\$ Even after you will synchronize with your phyton script, they will start to drift away. So question still persists: 0.1ms over which period of time (1s, 1min)? You hve to make a temperature oven for RTC XTAL and then use RTC output from MCU, use the precise period/frequency counter to calibrate each MCU board. \$\endgroup\$ – Marko Buršič Apr 12 '16 at 10:02

I suggest you the following reading about Using the hardware real-time clock (RTC) in STM32 F0, F2, F3, F4 and L1 series of MCUs. Synchronizing the RTC part starts at page 19.

Just to highlight the basics from the document:

The RTC calendar can be synchronized to a more precise clock, “remote clock”, using the RTC shift feature. After reading the RTC sub-second field, a calculation of the precise offset between the time being maintained by the remote clock and the RTC can be made. The RTC can be adjusted by removing this offset with a fine adjustment using the shift register control.

enter image description here

Correcting the RTC calendar time

If the RTC clock is advanced compared to the remote clock by n fractions of seconds, the offset value must be written in SUBFS, which will be added to the synchronous prescaler’s counter. As this counter counts down, this operation effectively subtracts from (delays) the clock by:

Delay (seconds) = SUBFS / (PREDIV_S + 1)

If the RTC is delayed compared to the remote clock by n fractions of seconds, the offset value can effectively be added to the clock (advancing the clock) when the ADD1S function is used in conjunction with SUBFS, effectively advancing the clock by:

Advance (seconds) = (1 - (SUBFS / (PREDIV_S + 1))).

  • \$\begingroup\$ Thanks , I will give it a try and tell you about the outcome ! :) \$\endgroup\$ – Youssef Kamoun Apr 12 '16 at 9:57
  • \$\begingroup\$ I think if you want to generalise the answer not just to STM uC but for all uC, then something like a GPS clock will be a right approach. \$\endgroup\$ – AlphaGoku Apr 12 '16 at 10:15
  • \$\begingroup\$ en.wikipedia.org/wiki/Clock_synchronization \$\endgroup\$ – AlphaGoku Apr 12 '16 at 10:17
  • \$\begingroup\$ Sorry, forgot this one. Site has already got a general answer. \$\endgroup\$ – Bence Kaulics Apr 12 '16 at 10:17
  • \$\begingroup\$ I don't want to use Any External Clock or GPS,so how to Implement this Delay(seconds) or Advance(seconds)? \$\endgroup\$ – Youssef Kamoun Apr 12 '16 at 11:56

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.