Assume I connect a microcontroller board (like an STM32F4) to a PC via USB. Is there anything in the USB communication protocol that contains host clock data? In other words, can I sync the microcontroller to the PC clock just by hooking up to USB?

If that's not possible, could you suggest some clever minimal-effort way to get the clock info from PC to the STM32F4?

I guess I could always write some software to run on the PC in the background and provide that info to the STM32F4, and I'll do that if I must, but I'd like to avoid that.

I could also use a network shield and have the STM32F4 query the PC over the network, but I'd like to keep cost and complexity down by not using any extra shields.

I could also have the STM32F4 run its own clock base, but that's not an option - it must be synced to this particular PC down to the second, even if the PC itself is out of sync with NTP.

By the way the Synchronisation is on the Microsecond level.


I found this for Arduino Board : http://playground.arduino.cc/Code/DateTime

  • 1
    \$\begingroup\$ Down to the second or down to the microsecond? Two very different problems... \$\endgroup\$
    – Dave Tweed
    Jun 1, 2016 at 11:39
  • \$\begingroup\$ Down to the Microsecond :) or at least 10 Microseconds \$\endgroup\$ Jun 1, 2016 at 11:42
  • \$\begingroup\$ @PeterJ I am using Debian . \$\endgroup\$ Jun 1, 2016 at 12:51
  • \$\begingroup\$ The Linux get time calls have had microsecond resolution for ages, although I'm not sure to what degree they use high-resolution counters in the hardware (would guess they do they've been around quite a while now). \$\endgroup\$
    – PeterJ
    Jun 1, 2016 at 12:56
  • 1
    \$\begingroup\$ @PeterJ So how to implement ? \$\endgroup\$ Jun 1, 2016 at 12:59

3 Answers 3


I think microsecond, or even 10 microsecond accuracy between host PC and a microcontroller over USB will be very hard.

Even with a program running on the host PC with access to a 1µs accurate clock, there is no guarantee when your program will be able to write down the USB port to the STM32F4. It could easily be 250µs or more after the host program gets the time before the STM32F4 receives the time.

So you are going to have to implement an algorithm which works out the errors, and corrects them.

There is an internet Network Time Protocol (NTP) which uses an algorithm to synchronise time, but is only accurate to milliseconds.

There is also the Precision Time Protocol (PTP) which aims for microsecond precision, so this is the one to understand.

As you want to do all of this in software, you'll need to find an implementation which is understandable enough that you can extract the portions you need.

I searched the web for "Precision Time Protocol over USB" and found a bunch of potentially useful articles and application notes. I suggest you look at them and come back with specific questions.

I would be surprised if you find any implementations of PTP for STM32.

An alternative approach might be to use GPS time signals. With a GPS attached to the PC, and another to the STM32F4, you may be able to measure how far from GPS the host PC, based on a shred time base.

  • \$\begingroup\$ I tried to implement The PTP thanks to a Python script to send time over usb to stm32 and same from stm32 to the pc through usb then calculate the delay but can't get it to work :/ \$\endgroup\$ Jun 2, 2016 at 9:05
  • \$\begingroup\$ @YoussefKamoun - can you divide your implementation into pieces and ask smaller questions about the problems you are having? We might be able to help, or possibly better might be stackoverflow. \$\endgroup\$
    – gbulmer
    Jun 3, 2016 at 1:06

Actually I believe the USB spec also has a provision for Interrupt Transfers that it has a maximum latency of 1ms. Assuming your PC was the host and your STM32F4 was the device, I would think the driver on the PC could access the realtime clock, initiate an Interrupt Transfer with the current host time. Upon receipt the STM32F4 would compare the host time with its current device time, and respond to the host with the time difference.

Fundamentally you will need to keep track of 4 values:

  • Host Time
  • Transport time to device
  • Device Time
  • Transport time back to host

Depending on which side you want to be "smarter" the host can track these and send the expected device time to the device. Or the device side can track these times. My thinking is the host side tracking them would be most accurate. Ideally if USB controller chips had hardware time stamping you could get to low nanosecond levels of synchronization. With software I think you will be able to achieve microsecond or below.


The answers to your questions are of course in the USB spec. Read it.

Briefly, the USB host sends a frame sync message at accurate regular intervals. This is 1 ms for low and full speed USB. I vaguely remember it is faster, like 250 µs on high speed USB. Again, read the spec.

These frame sync messages have quite accurate timing requirements, and can be used to get relative time in the device. There is nothing in USB that requires transferring absolute time from the host to a device.

  • 3
    \$\begingroup\$ But there's no guarantee that the USB host controller's clock is in any way synchronized to the real-time clock that software running on the PC has access to, is there? \$\endgroup\$
    – Dave Tweed
    Jun 1, 2016 at 11:57
  • \$\begingroup\$ I saw in the arduino case,you can do it : playground.arduino.cc/Code/DateTime \$\endgroup\$ Jun 1, 2016 at 11:59
  • \$\begingroup\$ @Dave: Most likely both are ultimately derived from the same crystal oscillator. However, I don't know of anything that requires that. The frame sync timing requirements are quite accurate, but that's as far as the USB spec goes. So yes, there can be drift between the frame syncs and the host's real time clock. \$\endgroup\$ Jun 1, 2016 at 12:27
  • 2
    \$\begingroup\$ On a system with NTP or other access to an external reference, the real time clock will be slewed by corrections, which there is no reason to expect the USB bus operations to follow. \$\endgroup\$ Jun 1, 2016 at 13:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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