0

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?)

Bence Kaulics
  • 6,353
  • 12
  • 33
  • 60
  • What is your desired accuracy? – Bence Kaulics Apr 12 '16 at 09:33
  • My desired accuracy is of 1ms and even lesser like 0.1ms. – Youssef Kamoun Apr 12 '16 at 09:35
  • 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. – Marko Buršič Apr 12 '16 at 09:47
  • 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 . – Youssef Kamoun Apr 12 '16 at 09:49
  • 1
    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. – Marko Buršič Apr 12 '16 at 10:02
  • over 1min for example – Youssef Kamoun Apr 12 '16 at 11:50
  • @MarkoBuršič I assume the challenge would be to synchronize the clocks (with the PC clock) to within 100µs in the first place. – JimmyB Apr 12 '16 at 12:14
  • For better (initial) accuracy, you'd probably be better off connecting the µCs with each other and have them sync to each other. That should work down to way below 1µs; and you avoid latencies and jitter introduced by USB communication. – JimmyB Apr 12 '16 at 12:17
  • @HannoBinder Yes that is the challenge. For the µCs ,i would like to do it with usb communications first even if it is not below 1µs then if I succeed ,i will go to the next step and try to synchronize them while connected for example to Wifi.But for now I want to synchronize them by USB. – Youssef Kamoun Apr 12 '16 at 12:21
  • Clock accuracy is measured in percent (or ppm). – Lundin Apr 13 '16 at 06:48

1 Answers1

1

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))).

Bence Kaulics
  • 6,353
  • 12
  • 33
  • 60
  • Thanks , I will give it a try and tell you about the outcome ! :) – Youssef Kamoun Apr 12 '16 at 09:57
  • 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. – AlphaGoku Apr 12 '16 at 10:15
  • https://en.wikipedia.org/wiki/Clock_synchronization – AlphaGoku Apr 12 '16 at 10:17
  • Sorry, forgot this [one](http://electronics.stackexchange.com/questions/77629/how-to-synchronize-two-microcontrollers-to-micro-second-accuracy). Site has already got a general answer. – Bence Kaulics Apr 12 '16 at 10:17
  • I don't want to use Any External Clock or GPS,so how to Implement this Delay(seconds) or Advance(seconds)? – Youssef Kamoun Apr 12 '16 at 11:56
  • One of your STM32F401RE should be the master clock, this will act as an external or remote clock to the other two. – Bence Kaulics Apr 12 '16 at 12:50
  • but they need to be wired or connected ? – Youssef Kamoun Apr 12 '16 at 12:52
  • If you use on STM as a master the other two clocks should know somehow about the master. Or if you use a PC as a master the STM clocks must know somehow the PC's clock. So yes, slaves should be connected to the master otherwise how would they know the offset between their clocks and the masters. Once you know the offset, the above mentioned registers can be used to adjust the clocks. For further, professional help try stmicroelectronics's [forum](https://my.st.com/public/STe2ecommunities/mcu/Lists/cortex_mx_stm32/AllItems.aspx). – Bence Kaulics Apr 12 '16 at 13:16
  • Yes normally the Pc is the master and STM is the slave,so that the STM synchronize its Time with the Master. – Youssef Kamoun Apr 12 '16 at 13:17