Research and Design of NTP Time Synchronization Server

Research and Design of NTP Time Synchronization Server

With the rapid development of information technology and network technology, network interconnection has penetrated into all walks of life in the national economy. And network time synchronization is also receiving more and more attention, especially in many areas of national security and national economy (such as national defense and military industry, telecommunications network, financial industry, transportation, e-commerce, and power systems). lack. With the development of embedded technology, the combination of embedded and network time synchronization technology undoubtedly has a good development prospect.

1. Scheme design
At present, there are many ways to implement network time service. This article uses a self-designed embedded system and implements the SNTP protocol on it. So as to complete the synchronization of the network time. Its system block diagram is shown as in Fig. l.

This system adopts the C/S model and is divided into two parts: the network timing synchronization server and the client. This article mainly studies the network timing server.
In the network timing synchronization server, the processor STM32f103 uses the internal RTC module combined with the calendar algorithm to give time information (year, month, day, hour, minute, and second), and then obtains the time information from the GPS, and corrects its own time, and finally builds one with the W5100 chip Time server. When the client sends a request to the server, the time information of the client can be unified synchronously and reach the ms-level accuracy. The SNTP application layer protocol needs to be implemented during network transmission. The SNTP protocol package is constructed in the design and the round-trip delay of the packet exchange can be calculated according to the synchronization algorithm.
This system uses ST company's STM32 series processor based on Cortex-M3 core. The Cortex-M3 core is a processor core specially designed for the design of high-performance, low-power, low-cost, real-time embedded application systems. While improving performance, it also increases the code density of the Thumb-2 instruction set. It also greatly improves the performance of the tightly coupled nested vector interrupt controller for interrupt response. All new functions have the industry's best power consumption level at the same time.
The firmware chip W5100 used in the realization of TCP/IP protocol stack is a firmware network chip launched by South Korea's WIZnet company. It integrates TCP/IP protocol stack, Ethernet MAC and PHY, and can support TCP, UDP, ICMP, IGMP, IPv4, ARP, PPPoE, Ethemet and other network protocols; support 4 independent Socket communications at the same time, the internal 16 K byte send/receive buffer can quickly exchange data, the maximum communication rate can reach 25Mbps. In addition, W5100 also embeds 10BaseT/100BaseTX Ethernet physical layer, can support automatic answer (full duplex/half duplex mode), and provides a variety of bus (two parallel buses and SPI bus) interface methods, which can easily communicate with Various MCU connections. The introduction of the W5100 device greatly simplifies the design of the hardware circuit, allowing the microcontroller to truly realize the single-chip access to the Internet without the support of the operating system.
2 SNTP protocol analysis
SNTP stands for Simple Network Time Protocol. It is a time synchronization protocol used at the end of a local area network subnet. It requires only one reliable synchronized clock source to exist during operation. It is a simplified version of the NTP protocol.
2.1 The synchronization principle of
SNTP The SNTP protocol mainly uses synchronization algorithms to exchange the time stamps of the time server and the client, thereby estimating the round-trip delay of data packets on the network, and then independently estimating the system clock deviation. The transmission model of its time synchronization principle is shown in Figure 2.

In Figure 2, T1 is the time when the client sends the query request (referenced to the client's time system), T2 is the time when the server receives the query request (referenced to the server time system), and T3 is the server's response time packet time (referenced to the server Time system for reference), T4 is the time when the client receives the time packet (take the client's time system as a reference), D1 is the time consumed for the request information to be transmitted online, and D2 is the time consumed for the reply information to be transmitted online. Assuming that the request and reply have the same propagation time on the Internet, that is: δ1=δ2, the following formula can be obtained:

In the formula, θ is the difference between the client time and the standard time, and δ is the time for the information to spread on the Internet. It can be seen that θ and δ are only related to the difference between T2 and T1 and the difference between T4 and T3, but have nothing to do with the difference between T2 and T3, that is, the final result has nothing to do with the time required by the server to process the request. According to this, the client (CLIENT) can adjust the local clock through the time difference 0 calculated by T1, T2, T3, and T4.
2.2 SNTP protocol format
SNTP messages are generally encapsulated in UDP messages, the UDP port number is 123, and the source port and destination port in the UDP header are the same. The SNTP message immediately follows the IP and UDP headers, and its protocol format is shown in Figure 3.

In Fig. 3, U is a jump indicator, which can warn about the impending seconds (seconds) inserted at the end of the last day of the month. VN represents the version number. Mode is the mode, and this field includes the following values:
O (reserved); 1 (symmetrical behavior); 3 (client); 4 (server); 5 (broadcast); 6 (NTP control information). Stratum is used for the overall identification of the local clock level. Poll represents a signed integer representing the maximum interval between consecutive messages. Precision represents a signed integer and represents the accuracy of the local clock. Root Delay is a signed fixed point serial number, which represents the total delay of the main reference source, such as a segmented point between 15 and 16 in a short time. Root Dispersion is an unsigned fixed point serial number that represents a normal error relative to the main reference source, such as a segmentation point between bits 15 and 16 in a short time.
Reference Identifier is to identify the special reference source. Originate Timestamp is to request the time to separate the client from the server, using a 64-bit time stamp (Timestamp) format. Receive Timestamp is the time when the request from the server arrives at the client. 64-bit time stamp (Timestamp) format is also used. Transmit Timestamp is the time to reply to the client to separate the server. Use 64-bit time stamp (Timestamp) format.
3 hardware design
Figure 4 shows the circuit diagram of the W5100 part, which shows the connection mode of W5100 and STM32 and its peripheral circuits.

W5100 and STM32 can communicate via SPI. SPI mode can be allowed by pulling up the SEN pin to a high level with a 10 kΩ resistor; because W5100 is in SPI slave mode, its SPI working clock is provided by STM32 in master mode. MISO and MOSI are used for SPI communication Two data lines, SCLK is the SPI clock pin; ***** is the chip selection pin, low level is active, mainly used to access the W5100 internal registers or memory by the MCU when the parallel bus is connected; INT is the interrupt output pin Pin, low level is active. When W5100 connects, disconnects, receives data, data transmission is completed, and communication timeout occurs on SOCKET port, this pin will output a signal to indicate MCU. The interrupt will be cleared when writing to the interrupt register IR or the interrupt register of the port, and all interrupts can be masked. The 5th, 6th, 8th and 9th pins of W5100 are Ethernet physical layer signal pins, which are used to connect to the RJ45 interface. Among them, the 5th and 6th pins are RXIP/RXlN signal pairs, which are used to receive signals from the medium. The 8th and 9th pins are the TXOP/TXON signal pair, which is used to send the differential data to the medium; the 66th pin is the connection LED indicator pin, the low level indicates that the 10/100Mbps connection is normal, and the connection It outputs low level when normal, and flashes in TX/RX state; the 72nd pin is the receiving status LED indicator pin, the low level indicates the current receiving data, the 73rd pin is the sending status LED indicator pin, low power Ping means that data is currently being sent. These LED indication pins should be connected to the corresponding LED indicator pins of RJ45 to indicate the connection status. Except the power supply pin and the clock pin, the other pins DO~D7, AO~A14 and WR~RD of W5100 can be left floating.
Figure 5 shows a schematic diagram of the connection between the GPS module and the STM32. The GPS receiving module adopts the GPS module M87GPS produced by HOLUX. The serial port output and input of the module are respectively connected to the input and output of STM32, and the second pulse PPS signal is connected to the IO port of the processor. When the second pulse (1PPS) is synchronized , The system will accurately transmit the standard UTC time to the processor STM32 through the serial port in real time.

4 SNTP server software design The
SNTP server software design can be divided into two parts: W5100 drive design and SNTP protocol software implementation. Its software flow chart is shown as in Fig. 6.

First, use the firmware library provided by ST to initialize the system configuration of STM32, configure the SPI interface to two-wire unidirectional full-duplex transmission, master mode, and transmit in the format of 8-bit data frame; at the same time, configure the RTC module to generate a second pulse , And then combine with the calendar algorithm to get its own system time, and then correct the system time through the GPS second pulse PPS. Then complete its basic settings, network information and port memory information by configuring W5100 public registers and port registers to make it a UDP server mode. After that, W5100 is in the state of *. Once the SOCKET port of W5100 has an interrupt event, W5100 will trigger the external interrupt of STM32. If STM32 detects the change of SoekRecvflag, it will immediately start the analysis of SNTP protocol.
After receiving the SNTP protocol packet, you can record the time T2 of the received message, then parse the time stamp T1 from the message, and then encapsulate T1 and T2 into a new message for sending, and record a sending time when sending. T3.
5 Conclusion
This article builds a network server hardware platform based on STM32 and W5100, and implements SNTP synchronization time messages on it. After testing, the system runs stably and can synchronize the clock of the client PC. The system can effectively solve the time asynchronous problem in industrial control and other fields.

Guess you like

Origin blog.csdn.net/weixin_44990608/article/details/107492230