Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 9 of 9

Thread: Teensy 4.1 NTP server

  1. #1
    Junior Member
    Join Date
    Jan 2013
    Posts
    11

    Teensy 4.1 NTP server

    I ported the embedded NTP stratum 1 server that I originally wrote for the stm32f407. It uses the 1588 ethernet hardware for RX and TX timestamps. I'm also using the 1588 event input capture to measure the reference PPS in hardware.

    My first task was to get the clock synchronized with my reference GPS module's PPS. I'm using a PID controller to keep them in sync.




    Once I have the local clock synchronized, I can then start serving the time to clients. I compared the teensy's clock to my stm32mp1-based NTP stratum 1 server. The green and blue lines represent the request and response latency, and the purple line is the offset between the two clocks. This is a 23 microsecond round trip time and an offset of 570 nanoseconds. There's a small change in the graph at the end where I turned on adjustments for the published latency figured for the PHY (105ns TX delay, 350ns RX delay).



    This is better than my original platform of the stm32f407 (below), because the Teensy's crystal is much better.




    Code is here: https://github.com/ddrown/teensy-ntp

    And I'm using my own version of the Teensy lwip library, with 1588 timestamping support: https://github.com/ddrown/teensy41_ethernet

  2. #2
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    I wanted to use the teensy_loader_cli but the soft reboot option wasn't working for me. I didn't dig very far into why it wasn't working, but I put together a workaround. If you send the character "r" to the serial, the teensy reboots into the bootloader.

    Code:
    static void bootloader_poll() {
      if(Serial.available()) {
        char r = Serial.read();
        if(r == 'r') {
          Serial.println("rebooting to bootloader");
          delay(10);
          asm("bkpt #251"); // run bootloader
        }
      }
    }

  3. #3
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,532
    Wow, nice work on the NTPD server using GPS and 1088. I haven't quite figured out all the components, but I boldly reconfigured my sparkfun GPS to run at 115200 baud and emit ZDA sentences. (I did manage to screw that up by getting the GPS into binary mode, then i had to find the binary data that would put it back in ASCII/NMEA mode ... sigh). I think it's running on my T4.1 with GPS Tx to pin 0 and GPS PPS to pin 35.
    Code:
    Ethernet 1588 NTP Server
    ------------------------
    
    netif status changed: ip 192.168.1.19, mask 255.255.255.0, gw 192.168.1.1
    waiting for link
    enet link status: up
    S 56627443 3802693683
    81627251 0.000007680 0.000007680 0.000000000 8455 3802693684
    106627058 -0.000001510 0.000007700 0.166664958 7555 3802693685
    131626865 -0.000001107 0.000007720 0.750002265 7614 3802693686
    156626672 -0.000001016 0.000007720 0.800003231 7622 3802693687
    181626480 -0.000000960 0.000007710 0.593748987 7617 3802693688
    206626287 -0.000000855 0.000007710 0.607158124 7626 3802693689
    231626095 -0.000000803 0.000007707 0.875059485 7627 3802693690
    256625902 -0.000000710 0.000007707 0.888899207 7636 3802693691
    281625710 -0.000000668 0.000007704 1.199987769 7637 3802693692
    ...
      eticks       offset      drift       chisq   drift     secs
    3338400888 -0.000000000 0.000007958 1.893251061 7958 3802771912
    3388400490 0.000000006 0.000007958 1.517588139 7958 3802771914
    3463399893 0.000000013 0.000007958 1.293152332 7959 3802771917
    3538399296 0.000000018 0.000007958 1.258479357 7959 3802771920
    3613398699 0.000000022 0.000007958 1.313638568 7960 3802771923
    3688398102 0.000000025 0.000007958 1.611920714 7960 3802771926
    3813397108 -0.000000013 0.000007958 1.605589628 7956 3802771931
    3863396710 -0.000000004 0.000007958 1.631969333 7957 3802771933
    3963395915 -0.000000030 0.000007958 2.179639101 7954 3802771937
    ...
    I assume the 2nd column is offset between GPS clock and T41 clock, your first and third graphs above. How do i get data to reproduce your frequency offset plot. I have a GPS PPS sketch using the GPT timer that lets me measure T4.1 crystal drift for either 24MHz crystal or 32KHz RTC crystal, so I know what ppm to expect (-8.1 ppm).

    Should your ntpd sketch not respond to NTP queries? I have a simple (old NTP v1) ubuntu C program that does an ntp query, but I get no reply from T4.1. I can ping the T4.1, and with tcpdump I can see the UDP ntp query packet go out on the wire, but no response. I haven't tried adding the T4.1 as an NTP server to /etc/ntp.conf on my ubuntu box.

    Just an observation (not your problem), the sparkfun GPS is supposed to flash its LED with a good GPS lock, but with the T4.1 running the NTP/GPS server, it sometimes stops blinking for a while. The GPS is still emitting NMEA but it may be using its internal clock ? I actually had one mbed eval board (NUCLEO F446RE) that would never work with that GPS, because of high frequency interference maybe?? Any how it looks like the T4.1 running native ethernet affects GPS signal ???

    i don't have a way to test 1088 with other boxes.

    EDIT 1: i added the T41 host as a server to ntp.conf and that appears to be working
    Code:
    12:52:57.041744 IP 192.168.1.10.123 > 192.168.1.19.123: NTPv4, Client, length 48
    12:52:57.041991 IP 192.168.1.19.123 > 192.168.1.10.123: NTPv4, Server, length 48
    12:54:01.041860 IP 192.168.1.10.123 > 192.168.1.19.123: NTPv4, Client, length 48
    12:54:01.041984 IP 192.168.1.19.123 > 192.168.1.10.123: NTPv4, Server, length 48
    
    ntpq -p
         remote           refid      st t when poll reach   delay   offset  jitter
    ==============================================================================
     manitou         .CDMA.           1 u   37   64   17    0.493    2.413   0.132
    *192.168.1.19    .PPS.            1 u   37   64   17    0.200    2.423   0.182
    Using sntp 192.168.1.19 to query the T41 works
    2020-07-02 13:05:09.845665 (+0500) -0.001066 +/- 0.000726 192.168.1.19 s1 no-leap

    EDIT 2:

    offset of GPS clock to disciplined T4.1 clock (1088 ether clock, 25MHz)
    Click image for larger version. 

Name:	ntpd.png 
Views:	16 
Size:	80.8 KB 
ID:	20856

    Click image for larger version. 

Name:	ntpdrift.png 
Views:	13 
Size:	32.3 KB 
ID:	20857
    Last edited by manitou; 07-05-2020 at 11:24 AM. Reason: ntp plot

  4. #4
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Quote Originally Posted by manitou View Post
    Wow, nice work on the NTPD server using GPS and 1088. I haven't quite figured out all the components, but I boldly reconfigured my sparkfun GPS to run at 115200 baud and emit ZDA sentences. (I did manage to screw that up by getting the GPS into binary mode, then i had to find the binary data that would put it back in ASCII/NMEA mode ... sigh). I think it's running on my T4.1 with GPS Tx to pin 0 and GPS PPS to pin 35.
    Code:
    Ethernet 1588 NTP Server
    ------------------------
    
    netif status changed: ip 192.168.1.19, mask 255.255.255.0, gw 192.168.1.1
    waiting for link
    enet link status: up
    S 56627443 3802693683
    81627251 0.000007680 0.000007680 0.000000000 8455 3802693684
    106627058 -0.000001510 0.000007700 0.166664958 7555 3802693685
    131626865 -0.000001107 0.000007720 0.750002265 7614 3802693686
    156626672 -0.000001016 0.000007720 0.800003231 7622 3802693687
    181626480 -0.000000960 0.000007710 0.593748987 7617 3802693688
    206626287 -0.000000855 0.000007710 0.607158124 7626 3802693689
    231626095 -0.000000803 0.000007707 0.875059485 7627 3802693690
    256625902 -0.000000710 0.000007707 0.888899207 7636 3802693691
    281625710 -0.000000668 0.000007704 1.199987769 7637 3802693692
    ...
    I assume the 2nd column is offset between GPS clock and T41 clock, your first and third graphs above. How do i get data to reproduce your frequency offset plot.
    The columns from that output:
    • T41 raw counter value at PPS (25MHz)
    • seconds offset between GPS PPS and virtual T41 clock (which is the first graph). The third graph is the measurement over the network, which will combine that offset with the network measurement noise
    • frequency measurement in seconds per second. multiply by 1 million to get parts per million (the frequency offset plot)
    • chisq fit of frequency measurement
    • used frequency, in parts per billion (includes phase adjustments to reduce the offset in column 2)
    • seconds since 1900, NTP's timebase. Subtract by 2208988800 to get unix epoch time (seconds since 1970)



    Quote Originally Posted by manitou View Post
    I have a GPS PPS sketch using the GPT timer that lets me measure T4.1 crystal drift for either 24MHz crystal or 32KHz RTC crystal, so I know what ppm to expect (-8.1 ppm).

    Should your ntpd sketch not respond to NTP queries? I have a simple ubuntu C program that does an ntp query, but I get no reply from T4.1. I can ping the T4.1, and with tcpdump I can see the UDP ntp query packet go out on the wire, but no response. I haven't tried adding the T4.1 as an NTP server to /etc/ntp.conf on my ubuntu box.
    It's very picky about what NTP queries it will respond to.

    • NTP version must be either 3 or 4
    • NTP mode must be client (3)
    • UDP data length must be exactly 48 bytes long


    If you show your tcpdump -vv output, I can help with that.

    Quote Originally Posted by manitou View Post
    Just an observation (not your problem), the sparkfun GPS is supposed to flash its LED with a good GPS lock, but with the T4.1 running the NTP/GPS server, it sometimes stops blinking for a while. The GPS is still emitting NMEA but it may be using its internal clock ? I actually had one mbed eval board that would never work with that GPS, because of high frequency interference maybe?? Any how it looks like the T4.1 running native ethernet affects GPS signal ???
    All sorts of things can interfere with GPS signal. It's a very weak signal when it gets to your receiver. The satellites overhead are moving very quickly, so if your antenna can only see part of the sky, there will be times that the module will lose lock and stop blinking till a new satellite moves into view.

    For an example of noise, I have an SBC based on the rk3328 processor with USB3. When I plug something into that USB port, it makes a lot of RF noise around the GPS frequencies:



    Normally the noise is much lower:



    Quote Originally Posted by manitou View Post
    i don't have a way to test 1088 with other boxes.
    I'm using chrony with hardware timestamps turned on to test NTP on my other systems. Not every NIC supports them, but my Intel NICs do. To check if you have support (the SOF_TIMESTAMPING_TX_HARDWARE and SOF_TIMESTAMPING_RX_HARDWARE are important):

    Code:
    $ sudo ethtool -T em1
    Time stamping parameters for em1:
    Capabilities:
            hardware-transmit     (SOF_TIMESTAMPING_TX_HARDWARE)
            software-transmit     (SOF_TIMESTAMPING_TX_SOFTWARE)
            hardware-receive      (SOF_TIMESTAMPING_RX_HARDWARE)
            software-receive      (SOF_TIMESTAMPING_RX_SOFTWARE)
            software-system-clock (SOF_TIMESTAMPING_SOFTWARE)
            hardware-raw-clock    (SOF_TIMESTAMPING_RAW_HARDWARE)
    PTP Hardware Clock: 0
    Hardware Transmit Timestamp Modes:
            off                   (HWTSTAMP_TX_OFF)
            on                    (HWTSTAMP_TX_ON)
    Hardware Receive Filter Modes:
            none                  (HWTSTAMP_FILTER_NONE)
            all                   (HWTSTAMP_FILTER_ALL)
            ptpv1-l4-sync         (HWTSTAMP_FILTER_PTP_V1_L4_SYNC)
            ptpv1-l4-delay-req    (HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ)
            ptpv2-l4-sync         (HWTSTAMP_FILTER_PTP_V2_L4_SYNC)
            ptpv2-l4-delay-req    (HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ)
            ptpv2-l2-sync         (HWTSTAMP_FILTER_PTP_V2_L2_SYNC)
            ptpv2-l2-delay-req    (HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ)
            ptpv2-event           (HWTSTAMP_FILTER_PTP_V2_EVENT)
            ptpv2-sync            (HWTSTAMP_FILTER_PTP_V2_SYNC)
            ptpv2-delay-req       (HWTSTAMP_FILTER_PTP_V2_DELAY_REQ)

  5. #5
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    I setup a second teensy with an Adafruit GPS. I added the GPS_USES_RMC define to settings.h to support it. Originally I took the timestamp of the GPRMC message, but it would often be delayed into the next second, which caused the software to think the clock was off by 1s. So now it's taking the timestamp of the GPGGA message and uses that instead.



    I put that change into place at around 01-00:30, and you can see the difference it made on the yellow line in the graph. The two Teensies agree on the time pretty closely. The Archmax's measurement over the network has the most noise, and the clock "cheese" has a static offset of around 500ns.

  6. #6
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,532
    I also experimented with Adafruit ultimate GPS. I found how to enable ZDA sentences and changed baud to 115200, BUT when I cycle power on the Adafruit GPS, it reverts back to 9600, and ZDA is no longer enabled. Of course, after program upload it will retain the 115200/ZDA setting, so additional code would have to be added to NTP sketch to start in 9600 and then enable ZDA and set to 115200 , then Serial1.end() and then start up Serial1 again at 115200. Messy, plus I observed the same intermittent GPS lock with the T4.1 running the NTP sketch, and I never could get all those Serial1 settings to work (LAG errors or B errors). T4.1 and adafruit GPS does OK with simple GPS sketches. My GPS is just sitting in a bedroom and no external antenna. The adafruit has an option for an external antenna and that's why i decided to experiment with it. I'll probably order the external antenna.

    So were your adafruit tests at 9600 and no ZDA?

    I was getting sub-microsecond offsets (i added data to my post #3) with the sparkfun GPS, but there were spikes when GPS lock was lost. I looked with scope and ZDA message comes about 340 ms after PPS on sparkfun

  7. #7
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    Quote Originally Posted by manitou View Post
    I also experimented with Adafruit ultimate GPS. I found how to enable ZDA sentences and changed baud to 115200, BUT when I cycle power on the Adafruit GPS, it reverts back to 9600, and ZDA is no longer enabled. Of course, after program upload it will retain the 115200/ZDA setting, so additional code would have to be added to NTP sketch to start in 9600 and then enable ZDA and set to 115200 , then Serial1.end() and then start up Serial1 again at 115200. Messy, plus I observed the same intermittent GPS lock with the T4.1 running the NTP sketch, and I never could get all those Serial1 settings to work (LAG errors or B errors). T4.1 and adafruit GPS does OK with simple GPS sketches. My GPS is just sitting in a bedroom and no external antenna. The adafruit has an option for an external antenna and that's why i decided to experiment with it. I'll probably order the external antenna.

    So were your adafruit tests at 9600 and no ZDA?
    Yes, I'm using the firmware defaults for the adafruit GPS, which is 9600 and no ZDA message. I'm also using an external antenna on a windowsill. Without an external antenna, I get bad reception.

    Quote Originally Posted by manitou View Post
    I was getting sub-microsecond offsets (i added data to my post #3) with the sparkfun GPS, but there were spikes when GPS lock was lost. I looked with scope and ZDA message comes about 340 ms after PPS on sparkfun
    Yeah, 300ms ish is pretty typical. As long as it is within 950ms, this code will accept it.

    I haven't done anything special for hold-over while there's no signal, so I'd expect it to drift in those cases.

    My measurements are on a system that's also synchronized to GPS directly, so that improves the measurements as well.

  8. #8
    Junior Member
    Join Date
    Jan 2013
    Posts
    11
    The RMC message taking more than 1s would generate those lag error messages. The LAG messages can also be caused by no pps due to signal lock loss.

    I'm surprised you're getting B (bad time) though. Is your GPS module resetting itself somehow?

  9. #9
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    2,532
    Quote Originally Posted by ddrown View Post
    The RMC message taking more than 1s would generate those lag error messages. The LAG messages can also be caused by no pps due to signal lock loss.

    I'm surprised you're getting B (bad time) though. Is your GPS module resetting itself somehow?
    I think i only got B message in one experiment trying to get ZDA and 115200 work on adafruit GPS ... not to worry.

    I added an offset and drift plot to post #3
    Last edited by manitou; 07-04-2020 at 09:56 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •