Time synchronization method
Through the electrical connection of the debugging port between the slave device and the master device, using the leading pulse and interrupt mode, combined with the software algorithm, high-precision time synchronization is achieved, which solves the problem of existing technology's dependence on dedicated hardware and is suitable for multi-device debugging. Microsecond-level time synchronization reduces costs and improves efficiency.
Patent Information
- Application Number
- CN202410948421.0
- Authority / Receiving Office
- CN · China
- Patent Type
- Patents(China)
- Current Assignee / Owner
- Filing Date
- 2024-07-16
- Publication Date
- 2024-09-06
- Estimated Expiration
- 2044-07-16
AI Technical Summary
Existing high-precision time synchronization solutions rely on dedicated hardware, which is costly and inefficient, and cannot achieve microsecond-level high-precision time synchronization between multiple devices.
Through the electrical connection of the debugging port between the slave device and the master device, the leading pulse data is used to enter the interrupt mode, the frame start data is captured, the response frame data is generated, and the software algorithm is implemented based on the pre-stored interrupt response delay value and hardware time data. Time synchronization, no additional hardware configuration required.
It achieves 10us-level high-precision time alignment across devices, adapts to high- and low-performance devices, reduces costs, and improves product development and verification efficiency.
Smart Images

Figure CN118487697B
Abstract
Description
Technical Field
[0001] The invention relates to the technical field of distributed interconnected equipment, and in particular to a time synchronization method. Background Art
[0002] With the advent of the era of the Internet of Everything, the demand for high-precision time synchronization between multiple devices is increasing, especially the development and debugging of multi-terminal fusion scenarios requires stable microsecond-level high-precision time synchronization. Existing high-precision time synchronization solutions usually rely on dedicated hardware. Different types of dedicated hardware need to be provided for different types of devices, which is costly and inefficient. Summary of the invention
[0003] The time synchronization method provided by the embodiment of the invention performs time synchronization when a slave device and a master device are electrically connected via a debug port, thereby ensuring time accuracy without configuring additional hardware.
[0004] According to one aspect of the present application, a time synchronization method is provided, which is applied to a slave device, wherein the slave device is electrically connected to a master device via a debug port, and the method comprises: in response to receiving a leading pulse data sent by the master device, entering an interrupt mode to prepare for receiving frame start data; obtaining capture time data of the frame start data by the slave device, and calculating the leading edge time of the frame start data based on the capture time data and the period of the frame start data; upon receiving message data, generating response frame data, sending the response frame data to the master device, and recording the current second time data of the slave device; upon receiving frame termination data, based on the The message data is used to obtain the first time data of the master device; the slave device time synchronization result is generated according to the pre-stored interrupt response delay value, the leading edge time, the first time data and the second time data; wherein the pre-stored interrupt response delay value is obtained in the following manner: when the first pin of the slave device receives the leading edge of the preset signal data, the leading edge of the preset signal data is output to the second pin of the slave device; the data of the first pin and the second pin are captured by an analyzer to obtain the first time difference between the leading edge of the preset signal data reaching the first pin and the second pin; and the first time difference is used as the interrupt response delay value.
[0005] According to another aspect of the present application, a time synchronization method is also provided, which is applied to a master device, and the master device and the slave device are electrically connected via a debug port. The method includes: sending a leading pulse data to the slave device to make the slave device enter an interrupt mode; collecting the current first time data of the master device, generating frame start data, message data and frame end data based on the first time data, and after sending the frame start data to the slave device without interruption, sending the message data and the frame end data to the slave device bit by bit in sequence, so that the slave device generates a response frame data and a slave device time synchronization result based on the message data and the frame end data respectively; when receiving the response frame data sent by the slave device, recording the master device time synchronization result.
[0006] According to another aspect of the present application, an electronic device is provided, comprising: a memory, a processor, and a computer program stored in the memory, wherein the processor implements the above method when executing the computer program.
[0007] According to another aspect of the present application, a computer-readable storage medium is provided, wherein a computer program is stored in the computer-readable storage medium, and the computer program implements the above method when executed by a processor.
[0008] The time synchronization method provided by the embodiment of the present invention enters the interrupt mode by receiving the leading pulse data sent by the master device to ensure that the frame start data that is about to arrive can be captured in a timely and stable manner, further ensuring the high-precision time synchronization effect, and calculating the leading edge time of the frame start data based on the capture time data and the period of the frame start data. After that, the message data is received, the response frame data is generated, and the response frame data is sent to the master device, and the current second time data of the slave device is recorded. When the frame end data is received, the first time data of the master device is obtained based on the message data, and the slave device time synchronization result is generated according to the pre-stored interrupt response delay value, the leading edge time obtained based on the software, and the first time data and the second time data obtained based on the hardware. The present invention performs time synchronization calculation based on the combination of software and hardware, thereby realizing high-precision time synchronization across devices. The method can be implemented when the slave device and the master device are electrically connected through the debug port, and the existing device does not need to be modified in hardware, and no additional hardware configuration is required, and can adapt to various high-performance and low-performance devices. BRIEF DESCRIPTION OF THE DRAWINGS
[0009] In order to more clearly illustrate the embodiments of the present invention or the technical solutions in the prior art, the following briefly introduces the drawings required for use in the embodiments or the prior art descriptions. Obviously, the drawings described below are only some embodiments of the present invention, and for ordinary technicians in this field, other embodiments can be obtained based on these drawings without creative work.
[0010] Figure 1 It is a flow chart of a time synchronization method according to an embodiment of the present invention;
[0011] Figure 2 This is a schematic diagram of the main process of high-precision time synchronization created by the present invention;
[0012] Figure 3 It is a schematic diagram of hardware connection created by the present invention;
[0013] Figure 4 It is a schematic diagram of a basic signal unit of a communication frame created by the present invention;
[0014] Figure 5 It is a schematic diagram of the communication frame timing created by the present invention;
[0015] Figure 6 It is a schematic diagram of key time points in the time synchronization process created by the present invention;
[0016] Figure 7 It is a schematic diagram of measuring the software response delay of the interrupt capture signal created by the present invention;
[0017] Figure 8 It is a structural schematic diagram of the electronic device created by the present invention;
[0018] Fig. 9 It is a schematic diagram of the synchronization result created by the present invention. DETAILED DESCRIPTION
[0019] The embodiments of the present invention will be described in more detail below with reference to the accompanying drawings. Although certain embodiments of the present invention are shown in the accompanying drawings, it should be understood that the present invention can be implemented in various forms and should not be construed as being limited to the embodiments described herein, which are instead provided to provide a more thorough and complete understanding of the present invention. It should be understood that the drawings and embodiments of the present invention are only for exemplary purposes and are not intended to limit the scope of protection of the present invention.
[0020] The current high-precision time synchronization solutions on the market, such as PTP (Precision Time Protocol) / gPTP (Generalized Precision Time Protocol), rely on the hard protocol support of the MAC (Media Access Control) layer, GNSS (Global Navigation Satellite System) timing relies on GPS (Global Positioning System) / Beidou chips, and the most common NTP (Network Time Protocol) also relies on at least general Ethernet access devices. However, many terminal devices do not have WIFI (Wireless Fidelity), but only BT (Bluetooth), but the BT environment has large interference and large transmission delay jitter. At the same time, it does not have a transmission capability similar to UDP (User Datagram Protocol), and it is impossible to use solutions similar to NTP (Network Time Protocol) / PTP to achieve low-power consumption and high-precision time synchronization. That is, there is currently no universal technical solution to achieve microsecond-level high-precision time synchronization of multiple devices.
[0021] Based on this, the present invention provides a time synchronization method, which does not require the use of special hardware, and the existing equipment does not need hardware modification. It uses the debugging interface of each device itself, only needs single-line connection, and the master device and the slave device complete information interaction through the ordinary GPIO port through the custom timing frame, and combines the software algorithm to achieve high-precision time synchronization at the ten microsecond level, wherein the real-time guarantee of the interrupt response of different devices is achieved through the leading pulse string, and the local time self-correction algorithm can be supported at the same time, and the real-time automatic deviation correction can be achieved with extremely low senseless overhead, maintaining extremely high timekeeping accuracy at any time. The method can achieve 10us-level high-precision time alignment across devices, has extremely strong universality, can adapt to various types of high-performance and low-performance devices, and has strong compatibility of the solution. It will bring great efficiency improvement, especially for product development, verification, and problem analysis, and is also suitable for time synchronization scenarios between various types of devices. The method solves the current industry's high-precision time synchronization method's dependence on dedicated hardware, and is universal for various types of high- and low-performance devices, with extremely low senseless overhead. In particular, it will bring great efficiency improvement for product development, verification, and problem analysis, and is also suitable for high-precision time synchronization scenarios between various types of devices.
[0022] A time synchronization method is provided in an embodiment of the present invention. The method is applied to a slave device, wherein the slave device is electrically connected to a master device via a debug port. Based on the method, the time of the slave device can be synchronized with the time of the master device based on the time of the master device. Figure 1 is a flowchart of a time synchronization method according to an embodiment of the present application. Figure 1 The method steps involved are described.
[0023] Step S101, in response to receiving the leading pulse data sent by the master device, entering the interrupt mode to prepare for receiving the frame start data.
[0024] In an embodiment of the present invention, the debug port may be a general debug port, which is used as a general-purpose input / output (GPIO) to realize the connection between the master device and the slave device. GPIO is a common functional module in microcontrollers and processors, which allows the device to interact with the outside world through digital signals. The GPIO port can be configured as input or output, so that it can read external signals or send signals to external devices.
[0025] The leading pulse data is used to make the slave device enter the interrupt mode, so that it is in a state of preparing to receive the frame start data subsequently sent by the master device, ensuring the timeliness and stability of the slave device end interrupt capture of the next synchronization signal (that is, the first edge of the frame start data, such as the first rising edge or the first falling edge).
[0026] In the actual device environment, almost all systems have power saving management measures, such as cpuidle (a power management framework in the Linux kernel) of Linux (a widely used open source operating system kernel), and CPU (Central Processing Unit) has multiple C-state states (CPU idlestates, sleep state). The CPU is not always active, but will frequently enter WFI (Wait for Interrupt), PC (Personal Sleep State) as needed. Computer, personal computer) and other different levels of CPU stop running states, and in the PC state, the time for the CPU to resume operation and respond to the interrupt will be significantly increased, and the soft control of these states by cpuidle, such as avoiding ping-pong and other strategies, will cause the interrupt response delay to not only be further increased but also fluctuate greatly. In a specific embodiment, the measured delay peak and fluctuation can reach 5~10ms, and the leading pulse can inform the slave device to prepare in advance. Multiple pulse strings can ensure that the slave device can respond to at least one leading pulse in any state to capture this leading notification action, and then temporarily turn off power saving to ensure timely and stable capture of the upcoming synchronization signal, and resume power saving after receiving this frame of data.
[0027] The slave device software responds to the leading pulse signal interrupt, and immediately turns off the power saving measures (for example, cpuidle on Linux) after capturing the first leading pulse signal, and prepares for receiving the synchronization signal, that is, enters the interrupt mode.
[0028] In interrupt mode, when an event occurs, the hardware will pause the currently running program and execute a predefined code, which is called an interrupt service routine (ISR). After processing the interrupt event, the processor will return to the state before the interruption and continue to execute the interrupted task.
[0029] It should be noted that for the GPIO interrupt mode, specifically, when the level state on the GPIO pin changes in a certain way, an interrupt will be triggered. This change may be a rising edge, a falling edge, or any level change. The following are several common interrupt trigger conditions: Rising edge interrupt: When the level of the GPIO pin changes from low to high, an interrupt is triggered. Falling edge interrupt: When the level of the GPIO pin changes from high to low, an interrupt is triggered. Dual edge interrupt: Whether it is a rising edge or a falling edge, as long as the level state changes, an interrupt will be triggered. Level interrupt: When the GPIO remains at a high level or a low level for more than a certain period of time, an interrupt is triggered.
[0030] In the embodiment of the present invention, the interrupt trigger condition can be selected according to actual needs. Considering that, for example, when a rising edge interrupt is used, the state needs to be modified to a low level before the next interrupt, in order to avoid frequent state modification, the trigger condition of a double edge interrupt can be preferably used.
[0031] In the embodiment of the present invention, the communication frame can be customized and consists of three parts: a leading pulse (i.e., leading pulse data), a frame start and end signal (i.e., frame start data and frame end data), and a PDU (Protocol Data Unit) message (i.e., message data). The leading pulse is composed of a pulse unit with a period of 6ms and a duty cycle of 50%, for example, and the rest is composed of basic signal units. The basic signal unit is defined as a bit, and there are four types of bits: start, data 0, data 1, and stop. See Figure 4 The schematic diagram of the basic signal unit of the communication frame shown in FIG.
[0032] start is the frame start signal (10ms high + 10ms low); stop is the frame end signal (10ms high + 30ms low); data0 is the bit data 0 in the PDU (1.5ms high + 0.5ms low); data 1 is the bit data 1 in the PDU (1.5ms low + 0.5ms high). The above specific data (period and duty cycle) are the data used in this implementation example and can be changed and adjusted as needed.
[0033] See also Figure 5 The communication frame timing diagram shown in the figure takes the SYNC_SET communication frame initiated by the master device as an example. The frame timing includes 5 leading pulses, frame start signal, PDU data message, and frame end signal in sequence. Among them. The number of leading pulses can be, for example, 6, 7, etc., customized according to actual requirements. The PDU data message is optional, and the frame that does not contain the PDU data message is an empty frame. The leading pulse and the frame start and end signal both use pulses with a longer period of 10ms. This design can adapt to the situation where low-performance devices cannot generate PDU data with shorter period pulses. Therefore, the SYNC_SET frame design can not include PDU data. At this time, the sampling time needs to be transmitted to the slave device end through other channels (for example, PDU data can be sent via Bluetooth or WIFI). At this time, the SYNC_SET empty frame is only used to collect the synchronization time point, and the time is calculated and set after receiving the synchronization point time of the host end through other channels in the future.
[0034] Step S102, obtaining capture time data of the frame start data from the device, and calculating the leading edge time of the frame start data based on the capture time data and the period of the frame start data.
[0035] In an embodiment of the present invention, the capture time data of the frame start data from the slave device is the moment when the complete frame start data is received from the slave device. After the slave device receives the frame start signal completely through an interrupt response, the current time of the slave device system is collected, that is, the capture time data of the frame start data from the slave device is obtained. The period of the frame start data is subtracted from the capture time data to obtain the leading edge time of the frame start data. Specifically, the time point of the leading edge time of the frame start signal is obtained by subtracting the high level and low level duration of the actually received start frame signal, that is, the slave device end time when the software receives the synchronization signal through an interrupt. This time is Figure 6 For example, see ST4 in Figure 4 From A in the figure, we can see that the period of the frame start signal is 20ms. After obtaining the capture time data, subtract 20ms to get the leading edge time.
[0036] It should be noted that if each point at which reception starts and ends is directly collected and the time consumption is obtained by subtracting each point, memory will be occupied. Based on the steps of the embodiment of the present invention, the memory occupation can be reduced.
[0037] Step S103, upon receiving the message data, generating response frame data, sending the response frame data to the master device, and recording the current second time data of the slave device.
[0038] In the embodiment of the present invention, response frame data, namely, ACK (Acknowledgement Frame) frame is generated, see Figure 5 The communication frame timing diagram shown in the figure shows that, in order to enhance the universality of the device, the ACK frame adopts an empty frame design to notify the master device of successful synchronization. It does not carry any other data to save overhead (data can also be carried in the ACK frame as needed). After completing the ACK frame transmission, the slave device collects the current system time of the slave device, that is, the second time data. This time data is Figure 6 ST5 in.
[0039] Step S104: upon receiving the frame termination data, obtaining the first time data of the master device based on the message data.
[0040] In the embodiment of the present invention, the message data, that is, the PDU data frame carries the first time data of the master device, which is the current system time collected by the master device, accurate to ns (nanoseconds), corresponding to Figure 6 MT1 in. The master device generates a custom message data and immediately sends a synchronization signal. It should be noted that the acquisition of the system time to the sending of the synchronization signal cannot be interrupted or suspended midway, thereby ensuring high accuracy of time synchronization. When the frame termination data is received, the first time data of the master device is obtained based on the message data.
[0041] Step S105, generating a slave device time synchronization result according to a pre-stored interrupt response delay value, the leading edge time, the first time data and the second time data; wherein the pre-stored interrupt response delay value is obtained in the following manner: when the leading edge of the preset signal data is received at the first pin of the slave device, the leading edge of the preset signal data is output to the second pin of the slave device; using an analyzer to capture the data of the first pin and the second pin to obtain a first time difference between the leading edge of the preset signal data reaching the first pin and the second pin; and using the first time difference as the interrupt response delay value.
[0042] In the embodiment of the present invention, the interrupt response delay value is a value obtained in advance by instrument testing, which can be stored in advance and directly obtained when used.
[0043] The interrupt response delay values on different CPUs and operating systems are different, so it is necessary to measure the delay data from the device itself. The interrupt response delay value cannot be measured from the device and must be tested by an instrument (such as a logic analyzer). It is a preset value in the slave device. In order to ensure that the interrupt response delay value can be accurately measured and thus ensure the accuracy of time synchronization, the pre-stored interrupt response delay value is obtained by the following method:
[0044] After the slave device software responds to the interrupt (i.e. responds to the signal from the other end), the received signal level is immediately and synchronously output to another debug port GPIO pin (ninety-nine percent of debug ports have more than two signal / data pins) in the ISR hard interrupt function, that is, when the leading edge of the preset signal data is received at the first pin of the slave device, the leading edge of the preset signal data is output to the second pin of the slave device; by simultaneously capturing the signals of the two pins with an analyzer, the delay between the signal reaching the CPU and the software responding to the interrupt signal can be measured, that is, the data of the first pin and the second pin are captured by the analyzer, and the first time difference between the leading edge of the preset signal data reaching the first pin and the second pin is obtained, and the first time difference is used as the interrupt response delay value.
[0045] like Figure 7 The interrupt capture signal software response delay measurement diagram is shown in the figure. The measurement result of the device in the figure is 51us. After multiple measurements to observe the fluctuations, the actual fluctuations are small. The maximum fluctuation of the test device in the figure is within 30us, and 90% is concentrated in the range of 45~55us (that is, within 10us fluctuations). This fluctuation directly determines the accuracy of this synchronization. So far, the interrupt response delay value of this measurement device has been measured, which corresponds to Figure 6 T4-T3 is 50us.
[0046] According to the pre-stored interrupt response delay value, the leading edge time, the first time data and the second time data, the time corresponding to the slave device end and the master device end is calculated, that is, the slave device time synchronization result is generated.
[0047] The time synchronization method provided by the embodiment of the present invention enters the interrupt mode by receiving the leading pulse data sent by the master device to ensure that the frame start data that is about to arrive can be captured in a timely and stable manner, further ensuring the high-precision time synchronization effect, and calculating the leading edge time of the frame start data based on the capture time data and the period of the frame start data. After that, the message data is received, the response frame data is generated, and the response frame data is sent to the master device, and the current second time data of the slave device is recorded. When the frame end data is received, the first time data of the master device is obtained based on the message data, and the slave device time synchronization result is generated according to the pre-stored interrupt response delay value, the leading edge time obtained based on the software, and the first time data and the second time data obtained based on the hardware. The present invention performs time synchronization calculation based on the combination of software and hardware, thereby realizing high-precision time synchronization across devices. The method can be implemented when the slave device and the master device are electrically connected through the debug port, and the existing device does not need to be modified in hardware, and no additional hardware configuration is required, and can adapt to various high-performance and low-performance devices.
[0048] Considering that the previous receiving steps are all in the hard interrupt context environment, they cannot be blocked and the time consumption needs to be as short as possible. If they are postponed to the worker thread, they can sleep. Therefore, in a possible implementation, sending the response frame data to the master device includes: determining a designated thread in the working thread; and using the designated thread to send the response frame data to the master device.
[0049] In the embodiment of the present invention, after the message data integrity check is passed, the response frame data is generated, and the response frame data is postponed to the worker thread (work thread) for sending, so as to adapt to different timing delay schemes and block-type security.
[0050] If the PDU message data integrity check fails, the slave device does not send an ACK response frame and directly waits for the next synchronization data.
[0051] It should be noted that it takes time to send the ACK response frame. If it is done in a hard interrupt, it will be blocked. Therefore, it is done in a thread and waits for the CPU to be idle to execute.
[0052] In one possible implementation, generating a slave device time synchronization result based on the interrupt response delay value, the leading edge time, the first time data, and the second time data includes: calculating a second time difference between the second time data and the leading edge time; summing the first time data, the second time difference, and the interrupt response delay value to obtain a target synchronization time; and using the target synchronization time as the time synchronization result corresponding to the second time data.
[0053] In the embodiment of the present invention, Tn represents natural time, MTn represents master device time, and STn represents slave device time. Figure 6 As shown, the master device sampling time point T1 corresponds to the master device time MT1, that is, the first time data, the synchronization signal is sent from the master device end at T2, where the synchronization signal (that is, the synchronization time point) is the first rising edge of the frame start data, the synchronization signal arrives at the slave device end at T3, and the slave device end software responds to the synchronization signal interrupt time T4 (corresponding to the slave device time ST4, the collection mark in the ISR hard interrupt program), after the slave end sends ACK, the master device time MT5 at this time is calculated at T5 (slave device time ST5) and set to the slave device. The time-consuming disassembly is as follows:
[0054] T2-T1 is the time difference between the sampling system time at the master device end and the time when the synchronization signal is sent. It can be measured, but it is generally ignored within 1us. T3-T2 is the time it takes for the physical synchronization signal to be transmitted from the master device end to the slave device end. This time depends on the line impedance and capacitive reactance characteristics and the influence of the line length on the signal establishment time. This item can be determined by specific measurements of the equipment. In conventional short-distance debugging, this time is measured at the 1us level, which is the time for wire transmission and can be ignored.
[0055] T4-T3 is the time taken for the signal to reach the GPIO pin of the slave device and for the slave device software to respond to the interrupt. The processing and measurement of this time consumption affects the accuracy of time synchronization. Based on the leading pulse data, the response of the synchronization signal interrupt is guaranteed to be timely and stable. Then, this time consumption is measured by combining software and hardware, that is, the interrupt response delay value. When the signal reaches the slave device, it only reaches the hardware logic circuit at first, and then the CPU enters the interrupt mode. The CPU then jumps to the address of the interrupt function according to the interrupt signal and executes the function.
[0056] T5-T4 is the time taken by the slave device to process the synchronization frame (ie, generate response frame data and send response frame data), which can be obtained according to ST5-ST4.
[0057] That is, the slave device needs to calculate MT5 at T5 (corresponding to ST5) based on ST4 and MT1 obtained according to the above disassembly time consumption, and then modify the slave device time to MT5 to complete the time synchronization. The calculation formula is as follows:
[0058] MT5 = MT1 + (ST5 - ST4) + (T4 - T3) + (T3 - T2) + (T2 - T1);
[0059] Ignoring T2-T1 and T3-T2, MT5 = MT1 + (ST5 - ST4) + (T4 - T3); T4-T3, i.e., the interrupt response delay, is usually around tens of us, which varies depending on the CPU. Based on this formula, we can derive MT5 with 10us accuracy.
[0060] In a possible implementation, the slave device is electrically connected to the master device via a debug port, including: a first designated pin of the slave device is electrically connected to a second designated pin of the master device; wherein the first designated pin is configured as a GPIO output state, and the second designated pin is configured as a GPIO input state.
[0061] In the embodiments of the present invention, see Figure 3 As shown in the hardware connection diagram of the invention, almost all devices have different forms of debug ports, and most devices (more than 99%) have uart debug ports, which can usually be used as general GPIO ports and have interrupt capabilities. Of course, other types of debug ports can also be used as long as they have the functions of general GPIO ports. Only one pin in the debug port needs to be used, and the entire solution adopts a custom timing single-line communication method, which can achieve synchronization function without affecting the output of the debug port log, and does not require any modification to the device hardware.
[0062] In a possible implementation, the method further includes: calculating a third time difference between the leading edge time and the interrupt response delay value; and using the third time difference as a synchronization time corresponding to the first time data.
[0063] In the embodiment of the present invention, the third time difference between the leading edge time and the interrupt response delay value is referred to as Figure 6 , the first edge time is T4, the interrupt response delay value is T3, and the difference between the two is T3. When T2 is negligible, T3 is equivalent to MT1, that is, the third time difference is used as the synchronization time corresponding to the first time data.
[0064] Based on this, in addition to completing high-precision time synchronization, the embodiment of the present invention can also calculate the clock deviation of the devices at both ends in ppm, as follows Fig. 9 The synchronization result diagram is shown, where M is the time on the master device at the synchronization signal moment, S is the time on the slave device at the synchronization signal moment, elapse is the interval between two measurements, drift is the drift of the slave device clock relative to the master device clock between the two measurements, and finally the unit is ppm commonly used in the industry for measurement and display.
[0065] For specific calculation methods, refer to Figure 6 , where the absolute time offset of the slave device relative to the master device is offset = MT5 -ST5, and the last synchronization related variable is marked with _last. The relevant parameter formula is as follows:
[0066] Interval: MT1 - MT1_last;
[0067] Drift: (ST4 - (ST4_last + offset)) - (MT1 - MT1_last);
[0068] ppm: ((ST4 - (ST4_last + offset)) – (MT1 – MT1_last)) / (MT1 – MT1_last).
[0069] Combine the following Figure 2 The main process diagram of high-precision time synchronization shown in the figure illustrates the specific implementation of this method, which mainly includes the following steps:
[0070] S001: The master device initializes the communication port and prepares to initiate synchronization. The general debug port is used as a normal GPIO function to achieve the connection. The master device initializes the GPIO to the output state and only uses one pin.
[0071] S002: Initialize the communication port of the slave device and wait for receiving data. Connect the general debug port as a normal GPIO function. Initialize the GPIO of the slave device to the input state, turn on the corresponding interrupt response, and configure it to the edge interrupt mode, using only one pin.
[0072] S003: The master device sends a leading pulse string through the GPIO pin in S001. In this embodiment, the leading pulse string is 5 pulses with a period of 6ms and a duty cycle of 50% (adjustable according to the situation). This is one of the key links in the invention to achieve high precision, and is used to ensure the timeliness and stability of the next synchronization signal (corresponding to the first rising edge of the frame start signal, hereinafter referred to as the synchronization signal) captured by the other end interrupt. Because in the actual device environment, almost all systems have power saving management measures, such as cpuidle in Linux, and the CPU has multiple C-state states. The CPU is not always in an active state, but will frequently enter WFI, PC, etc. as needed. Different levels of CPU shutdown states, especially in the PC state, the time it takes for the CPU to resume operation and respond to interrupts will be significantly increased. In addition, the soft control of these states by cpuidle, such as avoiding ping-pong strategies, will cause the interrupt response delay to not only increase further but also fluctuate greatly. In this implementation example environment, the measured delay peak and fluctuation can reach 5~10ms. The leading pulse can inform the other end to prepare in advance. Multiple pulse trains can ensure that the other end can respond to at least one leading pulse in any state to capture this leading notification action, and then temporarily turn off power saving to ensure timely and stable capture of the upcoming synchronization signal, and resume power saving after receiving this frame of data;
[0073] S004: The slave device software responds to the preamble pulse signal interrupt, and immediately turns off the power saving measures (e.g., cpuidle on Linux) after capturing the first preamble pulse signal, and prepares for receiving the synchronization signal;
[0074] S005: The master device collects the current system time MT1 (refer to the attached Figure 6 ), accurate to ns, generate a custom PDU data frame, and immediately send a synchronization signal. This is also an important step to achieve high precision. The key point is that the acquisition of system time to the sending of synchronization signals cannot be interrupted or suspended in the middle, that is, atomicity must be guaranteed, and interrupt protection must be turned off to ensure that the host sampling time and the sending of synchronization signals are consistent (not absolutely simultaneous, but there is a small fixed time delay, but this time is negligible within 1us on most systems. If the low-performance device takes a long time, this delay can be entered in the later calculation). The subsequent part of the frame start signal is sent by the timer according to the defined timing delay. The frame start signal is attached Figure 4 ;
[0075] S006: After the slave device receives the frame start signal completely through the interrupt response, the current time of the slave device system is collected, and then the high level and low level duration of the actually received start frame signal are subtracted to obtain the time point of the frame start signal first edge time, that is, the slave device end time when the software receives the synchronization signal through the interrupt, corresponding to the attached Figure 6 ST4 in;
[0076] S007: The master device sends the PDU data frame and the frame end signal bit by bit in sequence. The PDU frame data carries the master device synchronization point time collected in S005. After the sending is completed, it enters the receiving state and waits for the arrival of the slave device ACK frame (in this real-time embodiment, the timeout is set to 0.5s, which can be adjusted);
[0077] S008: The slave device receives the PDU data frame, and determines the data validity in real time during the receiving process. When the end frame signal is received normally and the PDU data verification is complete, the master device synchronization point time in the PDU is taken out;
[0078] S009: If the slave device passes the PDU data integrity check in S008, an ACK response frame is generated and postponed to the worker thread for sending (because the previous receiving steps are all in the hard interrupt context environment, it cannot be blocked, and the time consumption needs to be as short as possible. If it is postponed to the worker thread, it can sleep to adapt to different timing delay schemes and block-type security). The response frame can use an empty frame or carry information as needed. In this implementation example, an empty frame is used to save overhead;
[0079] S010: After completing the sending of the ACK frame, the slave device collects the current system time ST5 of the slave device, and calculates and sets the new time MT5 in combination with the synchronization signal time ST4, the master device synchronization point time MT1 and the link delay time. Figure 5 ,The detailed calculation method is shown in the time synchronization calculation section below;
[0080] S011: The master device receives the ACK response frame and records the synchronization result as required. The synchronization is completed.
[0081] S012: If the slave device fails the PDU data integrity check in S008, the slave device does not send an ACK response frame and directly waits for the next synchronization data;
[0082] S013: After S012 occurs, the master device will have an ACK response timeout. At this time, it decides whether to immediately re-initiate synchronization based on the retry parameters set by the user. If necessary, it returns to S003 and repeats the above synchronization process.
[0083] The invention provides a time synchronization method. The method realizes 10us-level high-precision time synchronization through a universal debugging port, without relying on dedicated hardware, without hardware modification and zero cost; realizes high-precision time synchronization through common GPIO and a single data line with a custom frame timing; realizes accurate capture of synchronization signals by the opposite end through a leading pulse design; realizes simplification and easy operation of interrupt response delay measurement through a combination of software and hardware; realizes 10us-level high-precision time synchronization through common GPIO port communication through a combination of software and hardware; and can automatically measure the relative clock deviation between master and slave devices while realizing time synchronization.
[0084] The embodiment of the invention also provides a time synchronization method, which is applied to a master device, wherein the master device and the slave device are electrically connected via a debug port, and the method comprises:
[0085] Sending a leading pulse data to the slave device to make the slave device enter an interrupt mode; collecting the current first time data of the master device, generating frame start data, message data and frame end data based on the first time data, and after sending the frame start data to the slave device without interruption, sending the message data and the frame end data to the slave device bit by bit in sequence, so that the slave device generates a response frame data and a slave device time synchronization result based on the message data and the frame end data respectively; upon receiving the response frame data sent by the slave device, recording the master device time synchronization result.
[0086] In an embodiment of the present invention, the debug port may be a general debug port, which is used as a general-purpose input / output (GPIO) to realize the connection between the master device and the slave device. GPIO is a common functional module in microcontrollers and processors, which allows the device to interact with the outside world through digital signals. The GPIO port can be configured as input or output, so that it can read external signals or send signals to external devices.
[0087] The leading pulse data is used to make the slave device enter the interrupt mode, so that it is in a state of preparing to receive the frame start data subsequently sent by the master device, ensuring the timeliness and stability of the slave device end interrupt capture of the next synchronization signal (i.e., the first rising edge of the frame start data).
[0088] In the actual device environment, almost all systems have power saving management measures, such as cpuidle (a power management framework in the Linux kernel) of Linux (a widely used open source operating system kernel). The CPU (Central Processing Unit, Chinese for central processing unit) has multiple C-state states (CPU idle states, sleep state). The CPU is not always in an active state, but will frequently enter different levels of CPU stop states such as WFI and PC (Personal Computer) as needed. In the PC state, the time for the CPU to resume operation and respond to interrupts will be significantly increased. In addition, the soft control of these states by cpuidle, such as avoiding ping-pong and other strategies, will cause the interrupt response delay to not only be further increased but also fluctuate greatly. In a specific embodiment, the measured delay peak and fluctuation can reach 5~10ms, and the leading pulse can inform the slave device to prepare in advance. Multiple pulse strings can ensure that the slave device can respond to at least one leading pulse in any state to capture this leading notification action, and then temporarily turn off power saving to ensure timely and stable capture of the upcoming synchronization signal, and resume power saving after receiving this frame of data.
[0089] The slave device software responds to the leading pulse signal interrupt, and immediately turns off the power saving measures (for example, cpuidle on Linux) after capturing the first leading pulse signal, and prepares for receiving the synchronization signal, that is, enters the interrupt mode.
[0090] In interrupt mode, when an event occurs, the hardware will pause the currently running program and execute a predefined code, which is called an interrupt service routine (ISR). After processing the interrupt event, the processor will return to the state before the interruption and continue to execute the interrupted task.
[0091] It should be noted that for the GPIO interrupt mode, specifically, when the level state on the GPIO pin changes in a certain way, an interrupt will be triggered. This change may be a rising edge, a falling edge, or any level change. The following are several common interrupt trigger conditions: Rising edge interrupt: When the level of the GPIO pin changes from low to high, an interrupt is triggered. Falling edge interrupt: When the level of the GPIO pin changes from high to low, an interrupt is triggered. Dual edge interrupt: Whether it is a rising edge or a falling edge, as long as the level state changes, an interrupt will be triggered. Level interrupt: When the GPIO remains at a high level or a low level for more than a certain period of time, an interrupt is triggered.
[0092] In the embodiment of the present invention, the interrupt trigger condition can be selected according to actual needs. Considering that, for example, when a rising edge interrupt is used, the state needs to be modified to a low level before the next interrupt, in order to avoid frequent state modification, the trigger condition of a double edge interrupt can be preferably used.
[0093] In the embodiment of the present invention, the communication frame can be customized and consists of three parts: the leading pulse (i.e. the leading pulse data), the frame start and end signals (i.e. the frame start data and the frame end data), and the PDU (Protocol Data Unit) message (i.e. the message data). The leading pulse is composed of a pulse unit with a period of 6ms and a duty cycle of 50%, and the rest is composed of basic signal units. The basic signal unit is defined as a bit, and there are four types of bits: start, data 0, data1, and stop. See Figure 4 The schematic diagram of the basic signal unit of the communication frame shown in FIG.
[0094] start is the frame start signal (10ms high + 10ms low); stop is the frame end signal (10ms high + 30ms low); data0 is the bit data 0 in the PDU (1.5ms high + 0.5ms low); data 1 is the bit data 1 in the PDU (1.5ms low + 0.5ms high). The above specific data (period and duty cycle) are the data used in this implementation example and can be changed and adjusted as needed.
[0095] See also Figure 5 The communication frame timing diagram shown in the figure takes the SYNC_SET communication frame initiated by the master device as an example. The frame timing includes 5 leading pulses, frame start signal, PDU data message, and frame end signal in sequence. Among them. PDU data message is optional, and the frame without PDU data message is an empty frame. The leading pulse and frame start and end signals both use pulses with a longer period of 10ms. This design can adapt to the situation where low-performance devices cannot generate PDU data with shorter period pulses. Therefore, the SYNC_SET frame design can exclude PDU data. At this time, the sampling time needs to be transmitted to the slave device through other channels (for example, PDU data can be sent via Bluetooth or WIFI). At this time, the SYNC_SET empty frame is only used to collect the synchronization time point, and the time is calculated and set after receiving the synchronization point time of the host through other channels.
[0096] Response frame data, that is, ACK (Acknowledgement Frame) frame, see Figure 5The communication frame timing diagram shown in the figure shows that, in order to enhance the universality of the device, the ACK frame adopts an empty frame design to notify the master device of successful synchronization. It does not carry any other data to save overhead (data can also be carried in the ACK frame as needed). After completing the ACK frame transmission, the slave device collects the current system time of the slave device, that is, the second time data. This time data is Figure 6 ST5 in.
[0097] In the embodiment of the present invention, the message data, that is, the PDU data frame carries the first time data of the master device, which is the current system time collected by the master device, accurate to ns (nanoseconds), corresponding to Figure 6 MT1 in. The master device generates custom message data and immediately sends a synchronization signal. It should be noted that the acquisition of system time to the issuance of synchronization signals cannot be interrupted or suspended midway, thereby ensuring high accuracy of time synchronization. It should be noted that the PUD data frame is synchronously generated when the acquisition time MT1 is collected, and the generation time is very fast and can be ignored. The PUD data frame can be an empty frame so that the slave device can record the time of the frame start signal.
[0098] The embodiment of the present invention can calculate time synchronization based on the combination of software and hardware, thereby realizing high-precision time synchronization across devices. The method can be realized when the slave device and the master device are electrically connected through the debug port, and the existing device does not need to be modified in hardware, and no additional hardware configuration is required, and can be adapted to various high-performance and low-performance devices.
[0099] In one possible implementation, after the frame start data is sent to the slave device, the message data and the frame end data are sent to the slave device bit by bit in sequence without interruption, including: performing an interrupt protection operation on generating the frame start data, message data and frame end data based on the first time data and sending the frame start data to the slave device.
[0100] In the embodiment of the present invention, disabling interrupt protection is a mechanism in a computer system, which is mainly used to prevent interference from external interrupts when executing certain key operations. When the processor receives an interrupt request, it will pause the currently executed instruction and turn to handle the interrupt event. By disabling the interrupt protection operation, it can be ensured that the generation of the frame start data, message data and frame end data based on the first time data and the sending of the frame start data to the slave device are not interrupted in the middle, ensuring atomicity.
[0101] In a possible implementation manner, the method further includes: if the response frame data is not received within a specified time period, recording a timeout result, and resending the leading pulse data to the slave device.
[0102] In the embodiment of the present invention, if the response frame data times out, it can be determined whether to immediately re-initiate synchronization based on the retry parameters set by the user. If necessary, the timeout result is recorded and the leading pulse data is re-sent to the slave device.
[0103] The embodiments of the present invention also provide a non-transitory machine-readable medium storing a computer program, wherein the computer program, when executed by a processor of a computer, is used to cause the computer to execute the method of the embodiments of the present invention.
[0104] The embodiments of the present invention further provide a computer program product, including a computer program, wherein the computer program, when executed by a processor of a computer, is used to cause the computer to execute the method of the embodiments of the present invention.
[0105] The embodiment of the invention also provides an electronic device, comprising: at least one processor; and a memory connected to the at least one processor in communication. The memory stores a computer program that can be executed by the at least one processor, and the computer program is used to enable the electronic device to perform the method of the embodiment of the invention when executed by the at least one processor.
[0106] refer to Figure 8 , a block diagram of an electronic device that can be used as a server or client of an embodiment of the invention will now be described, which is an example of hardware devices that can be applied to various aspects of the invention. The electronic device is intended to represent various forms of digital electronic computer devices, such as laptop computers, desktop computers, workbenches, personal digital assistants, servers, blade servers, mainframe computers, and other suitable computers. The electronic device can also represent various forms of mobile devices, such as personal digital processing, cellular phones, smart phones, wearable devices, and other similar computing devices. The components shown herein, their connections and relationships, and their functions are merely examples and are not intended to limit the implementation of the invention described and / or required herein.
[0107] like Figure 8 As shown, the electronic device includes a computing unit 801, which can perform various appropriate actions and processes according to a computer program stored in a read-only memory (ROM) 802 or a computer program loaded from a storage unit 808 to a random access memory (RAM) 803. In the RAM 803, various programs and data required for the operation of the electronic device can also be stored. The computing unit 801, the ROM 802, and the RAM 803 are connected to each other via a bus 804. An input / output (I / O) interface 805 is also connected to the bus 804.
[0108] Multiple components in the electronic device are connected to the I / O interface 805, including: an input unit 806, an output unit 807, a storage unit 808, and a communication unit 809. The input unit 806 can be any type of device that can input information to the electronic device, and the input unit 806 can receive input digital or character information, and generate key signal input related to user settings and / or function control of the electronic device. The output unit 807 can be any type of device that can present information, and can include but is not limited to a display, a speaker, a video / audio output terminal, a vibrator, and / or a printer. The storage unit 808 can include but is not limited to a disk, an optical disk. The communication unit 809 allows the electronic device to exchange information / data with other devices through a computer network such as the Internet and / or various telecommunication networks, and can include but is not limited to a modem, a network card, an infrared communication device, and / or a wireless communication transceiver, such as a Bluetooth device, a WiFi device, a WiMax device, a cellular communication device, and / or the like.
[0109] The computing unit 801 may be a variety of general and / or special processing components with processing and computing capabilities. Some examples of the computing unit 801 include, but are not limited to, a CPU, a graphics processing unit (GPU), various dedicated artificial intelligence (AI) computing units, various computing units running machine learning model algorithms, digital signal processors (DSPs), and any appropriate processors, controllers, microcontrollers, etc. The computing unit 801 performs the various methods and processes described above. For example, in some embodiments, the method embodiments created by the present invention may be implemented as a computer program, which is tangibly contained in a machine-readable medium, such as a storage unit 808. In some embodiments, part or all of the computer program may be loaded and / or installed on an electronic device via ROM 802 and / or a communication unit 809. In some embodiments, the computing unit 801 may be configured to perform the above-described method in any other appropriate manner (e.g., by means of firmware).
[0110] The computer program for implementing the method of the present invention can be written in any combination of one or more programming languages. These computer programs can be provided to a processor or controller of a general-purpose computer, a special-purpose computer, or other programmable data processing device, so that the computer program, when executed by the processor or controller, enables the functions / operations specified in the flow chart and / or block diagram to be implemented. The computer program can be executed entirely on the machine, partially on the machine, partially on the machine as a stand-alone software package and partially on a remote machine, or entirely on a remote machine or server.
[0111] In the context of the present invention, a machine-readable medium may be a tangible medium that may contain or store a program for use by or in conjunction with an instruction execution system, device, or apparatus. A machine-readable medium may be a machine-readable signal medium or a machine-readable storage medium. A machine-readable signal medium may include, but is not limited to, electronic, magnetic, optical, electromagnetic, or infrared systems, devices, or equipment, or any suitable combination of the foregoing. More specific examples of machine-readable storage media may include electrical connections based on one or more lines, portable computer disks, hard disks, random access memories (RAM), read-only memories (ROM), erasable programmable read-only memories (EPROM or flash memory), optical fibers, portable compact disk read-only memories (CD-ROMs), optical storage devices, magnetic storage devices, or any suitable combination of the foregoing.
[0112] It should be noted that the term "including" and its variations used in the embodiments of the present invention are open inclusions, that is, "including but not limited to". The term "based on" means "based at least in part on". The term "one embodiment" means "at least one embodiment"; the term "another embodiment" means "at least one other embodiment"; the term "some embodiments" means "at least some embodiments". The modifications of "one" and "multiple" mentioned in the embodiments of the present invention are illustrative and not restrictive. Those skilled in the art should understand that unless otherwise clearly indicated in the context, it should be understood as "one or more".
[0113] The user information (including but not limited to user device information, user personal information, etc.) and data (including but not limited to data used for analysis, stored data, displayed data, etc.) involved in the embodiments of the present invention are all information and data authorized by the user or fully authorized by all parties, and the collection, use and processing of relevant data must comply with the relevant laws, regulations and standards of relevant countries and regions, and provide corresponding operation entrances for users to choose to authorize or refuse.
[0114] The various steps described in the method implementation methods provided by the embodiments of the present invention can be performed in different orders and / or in parallel. In addition, the method implementation methods may include additional steps and / or omit the steps shown. The scope of protection of the present invention is not limited in this respect.
[0115] The term "embodiment" in this specification refers to specific features, structures or characteristics described in conjunction with the embodiment that can be included in at least one embodiment of the invention. The appearance of this phrase in various places in the specification does not necessarily mean the same embodiment, nor does it mean that it is mutually exclusive with other embodiments and is independent or optional. The various embodiments in this specification are described in a related manner, and the same and similar parts between the various embodiments refer to each other. In particular, for the device, equipment, and system embodiments, since they are basically similar to the method embodiments, the description is relatively simple, and the relevant parts refer to the partial description of the method embodiment.
[0116] The above-described embodiments only express several implementation methods of the present invention, and the descriptions thereof are relatively specific and detailed, but they cannot be understood as limiting the scope of protection. It should be pointed out that, for a person of ordinary skill in the art, several modifications and improvements can be made without departing from the concept of the present invention, and these all belong to the scope of protection of the present invention. Therefore, the scope of protection of the present invention shall be subject to the attached claims.
Claims
1. A time synchronization method, applied to a slave device, wherein the slave device is electrically connected to a master device via a debug port, the method comprising: In response to receiving the leading pulse data sent by the master device, entering the interrupt mode to prepare for receiving the frame start data; Obtaining capture time data of the frame start data from the device, and calculating the leading edge time of the frame start data based on the capture time data and the period of the frame start data; When receiving the message data, generating response frame data, sending the response frame data to the master device, and recording the current second time data of the slave device; When the frame termination data is received, obtaining the first time data of the master device based on the message data; Generate a slave device time synchronization result according to a pre-stored interrupt response delay value, the leading edge time, the first time data, and the second time data; wherein the pre-stored interrupt response delay value is obtained by: Based on receiving the leading edge of the preset signal data at the first pin of the slave device, outputting the leading edge of the preset signal data to the second pin of the slave device; Using an analyzer to capture data from the first pin and the second pin, obtaining a first time difference between a leading edge of the preset signal data and the first pin; The first time difference is used as the interrupt response delay value.
2. The method according to claim 1, wherein: Sending the response frame data to the master device includes: Determine the specified thread among the worker threads; The response frame data is sent to the master device using the designated thread.
3. The method according to claim 1, wherein: Generating a slave device time synchronization result according to the interrupt response delay value, the leading edge time, the first time data, and the second time data includes: Calculate a second time difference between the second time data and the leading edge time; Summing the first time data, the second time difference and the interrupt response delay value to obtain a target synchronization time; The target synchronization time is used as the time synchronization result corresponding to the second time data.
4. The method according to claim 1, wherein: The slave device is electrically connected to the master device via a debug port, including: The first designated pin of the slave device is electrically connected to the second designated pin of the master device; wherein the first designated pin is configured as a GPIO output state, and the second designated pin is configured as a GPIO input state.
5. The method according to any one of claims 1 to 4, wherein: Also includes: Calculate a third time difference between the leading edge time and the interrupt response delay value; The third time difference is used as the synchronization time corresponding to the first time data.
6. A time synchronization method, applied to a master device, wherein the master device is electrically connected to a slave device via a debug port, the method comprising: Sending a leading pulse data to the slave device to make the slave device enter an interrupt mode; Collect the current first time data of the master device, generate frame start data, message data and frame end data based on the first time data, send the frame start data to the slave device without interruption, and then send the message data and the frame end data to the slave device in sequence bit by bit, so that the slave device generates response frame data and slave device time synchronization result based on the message data and the frame end data respectively; wherein the slave device time synchronization result is generated by the slave device according to the pre-stored interrupt response delay value, the leading edge time, the first time data and the second time data; wherein the leading edge time is generated by the slave device based on the capture time of the frame start data by the slave device The second time data is calculated by the period of the frame start data and the frame start data; the second time data is recorded by the slave device when it receives the message data, generates the response frame data, and sends the response frame data to the master device; the pre-stored interrupt response delay value is obtained by the slave device in the following manner: when the first pin of the slave device receives the leading edge of the preset signal data, the leading edge of the preset signal data is output to the second pin of the slave device; the data of the first pin and the second pin are captured by an analyzer to obtain the first time difference between the leading edge of the preset signal data reaching the first pin and the second pin; the first time difference is used as the interrupt response delay value; When receiving the response frame data sent by the slave device, the master device time synchronization result is recorded.
7. The method according to claim 6, wherein: After sending the frame start data to the slave device, sending the message data and the frame end data to the slave device bit by bit in sequence without interruption includes: The generation of frame start data, message data and frame end data based on the first time data and the sending of the frame start data to the slave device are performed, and an interrupt protection operation is performed.
8. The method according to claim 6 or 7, wherein: Also includes: If the response frame data is not received within the specified time, the timeout result is recorded and the leading pulse data is resent to the slave device.
9. An electronic device, comprising a memory, a processor, and a computer program stored in the memory, wherein the processor implements the method according to any one of claims 1 to 8 when executing the computer program.
10. A computer-readable storage medium, wherein a computer program is stored in the computer-readable storage medium, and when the computer program is executed by a processor, the method according to any one of claims 1 to 8 is implemented.
