A method for protecting timestamps of embedded systems
By optimizing the timestamp protection and synchronization module of the embedded system, the instability of timestamps caused by step-by-step time synchronization and floating-point arithmetic anomalies is solved, realizing the continuity and accuracy of timestamps, which is suitable for high-reliability embedded systems.
Patent Information
- Authority / Receiving Office
- CN · China
- Patent Type
- Applications(China)
- Current Assignee / Owner
- MILKY WAY ELECTRONICS EQUIP FACTORY SHANXI PROVINCE
- Filing Date
- 2026-01-22
- Publication Date
- 2026-05-29
AI Technical Summary
Existing embedded systems suffer from timestamp errors due to step-by-step modification strategies, floating-point arithmetic anomalies, and timestamp instability caused by the intertwining of the time synchronization module and business data processing logic during timestamp calibration.
By optimizing the floating-point arithmetic function of the timestamp protection time synchronization module, configuring communication link timeout parameters, setting up a multi-level alarm mechanism, realizing the unified conversion of timestamp data format and measurement form, and performing time calibration under the dual time synchronization trigger conditions of deviation threshold and continuous judgment number.
It improves the stability and reliability of timestamps, avoids errors caused by time synchronization interruptions and format differences, ensures the continuity and accuracy of timestamps, and meets the operational requirements of highly reliable embedded systems.
Smart Images

Figure CN122111177A_ABST
Abstract
Description
Technical Field
[0001] This invention relates to the field of embedded system technology, and in particular to a method for protecting timestamps in embedded systems. Background Technology
[0002] In the operation of embedded systems, the accuracy and stability of timestamps are crucial to ensuring the correctness of business data processing. Currently, embedded systems typically calibrate their local time by interacting with external time synchronization devices. However, existing time synchronization methods suffer from the following technical problems: First, traditional time synchronization methods typically employ a step-by-step modification strategy when changing the system time: first modifying the date field (year, month, day), then modifying the time field (hour, minute, second). During this process, if the business data processing software obtains the timestamp in an intermediate state where the date has been updated but the time has not, it will receive an abnormal time value with the hours, minutes, and seconds reset to zero, leading to timestamp errors and data parsing chaos.
[0003] Second, some embedded compilers have known defects in floating-point arithmetic, where floating-point calculations can result in abnormally zero values under certain conditions. Time synchronization modules often rely on floating-point arithmetic when calculating time differences, and this defect can cause the calculated time deviation to be zero. Consequently, the system incorrectly assumes that the local time is perfectly synchronized with the time from an external time synchronization device, thus suppressing necessary time synchronization operations. As the local clock gradually deviates from the standard time due to crystal oscillator drift, the system's time base becomes inaccurate and cannot be automatically recovered.
[0004] Third, in many existing systems, time synchronization operations are closely intertwined with business data processing logic. Frequent time synchronization judgments and executions directly interfere with the business data processing software's acquisition of timestamps, affecting the continuity and stability of timestamp output, making it difficult to meet the requirements of a new generation of highly reliable data unpacking systems for independent control and stable operation.
[0005] To address the aforementioned issues, there is an urgent need for a protection method that can fundamentally enhance the stability and reliability of timestamps in embedded systems. Summary of the Invention
[0006] The purpose of this invention is to provide a method for protecting timestamps in embedded systems, so as to solve the technical problems of timestamp abnormalities, mistime calibration and poor stability caused by step-by-step time calibration, compiler operation defects and coupling between the time calibration module and the business data processing module in the prior art.
[0007] To achieve the above objectives, the present invention provides a method for protecting timestamps in an embedded system, which is executed by the timestamp protection and synchronization module in the embedded system, and includes the following steps: S1, Optimize the floating-point arithmetic function of the compilation tool used by the timestamp protection time synchronization module to eliminate the problem of floating-point arithmetic results abnormally returning to zero; S2, the timestamp protection time synchronization module creates a time synchronization data receiving communication link based on the preset configuration, and configures the time synchronization data receiving timeout parameters of the communication link; S3, the timestamp protection time synchronization module obtains time synchronization data sent by the external time synchronization device through the communication link. If the time synchronization data is not obtained within the time limit specified by the timeout parameter, an alarm mechanism is triggered. If it continues to fail to obtain the data, the alarm mechanism is repeatedly triggered according to the preset cycle, and the stability of the device's local clock is maintained in accordance with the preset index. If the data is successfully obtained, proceed to step S3. S4, the time stamp protection synchronization module parses the time synchronization data, converts it into a format consistent with the local time, and then compares the difference with the local time; S5, the timestamp protection time synchronization module sets a time deviation threshold. If the comparison difference is less than the threshold, the local time state is maintained and no calibration operation is performed. If the comparison difference is greater than the threshold, the deviation information is recorded and reported. When the comparison difference is greater than the threshold for a preset number of consecutive times, local time calibration is performed based on the time synchronization data. S6. During the calibration process, the timestamp protection time synchronization module converts both the time statistics data and the local time into a unified time measurement format. After calculating the absolute value of the deviation between the two, it synchronously updates the date and time fields of the local time to complete the time calibration.
[0008] Preferably, the optimization of the floating-point arithmetic function of the compiler tool in step S1 includes updating the compiler tool version, patching arithmetic logic vulnerabilities, or replacing at least one of the high-precision floating-point arithmetic libraries.
[0009] Preferably, the communication link in step S2 is any one of a network socket link, a serial communication link, or a wireless communication link.
[0010] Preferably, the alarm mechanism in step S3 includes at least one of log reporting, audible and visual alerts, or sending alarm notifications to a preset management terminal.
[0011] Preferably, the preset number of times in step S5 is no less than 3 times, which can be flexibly configured according to the application scenario of the embedded system.
[0012] Preferably, in step S5, the time deviation threshold can be manually adjusted by the user through the system configuration interface, and the range of the time deviation threshold can be flexibly set according to the actual application scenario of the embedded system.
[0013] Preferably, the unified time measurement format in step S6 is any one of seconds, milliseconds, or microseconds counted from the same time epoch, and the synchronous update includes a one-time complete overwrite of all time fields of year, month, day, hour, minute, and second.
[0014] Compared with the prior art, the present invention has the following significant advantages: First, this invention manages the time synchronization process uniformly through a timestamp-protected time synchronization module, configures communication link timeout parameters, avoids embedded systems from getting stuck in waiting due to time synchronization interruptions, and sets up a multi-level alarm mechanism, which significantly improves the perceptibility of maintenance personnel to time synchronization anomalies such as time synchronization interruptions and signal delays, making it easier to troubleshoot and solve problems in a timely manner.
[0015] Secondly, this invention achieves unified format conversion during the time synchronization data parsing stage and unified conversion of measurement form during the calibration stage. The dual conversion mechanism effectively eliminates comparison errors caused by differences in format and unit, and improves the accuracy of time synchronization judgment of the timestamp protection time synchronization module.
[0016] Third, the present invention sets a dual time synchronization trigger condition of deviation threshold and number of consecutive judgments, which not only avoids invalid calibration caused by a single instantaneous deviation, but also responds in a timely manner to continuous time deviations, thus balancing time synchronization accuracy and system operating load.
[0017] Fourth, this invention adopts a one-time synchronous update method for the time field, which effectively avoids the abnormal intermediate state of the timestamp caused by step-by-step updates and ensures the continuity of the timestamp.
[0018] Fifth, this invention optimizes the compiler's floating-point arithmetic function, eliminates the problem of abnormal zeroing during arithmetic, ensures the accuracy of deviation calculation results, avoids unnecessary repeated calibration from the root, and further improves the stability of timestamps. Attached Figure Description
[0019] One or more embodiments are illustrated by way of example with reference numerals in the accompanying drawings. These illustrations do not constitute a limitation on the embodiments. Elements with the same reference numerals in the drawings are denoted as similar elements. Unless otherwise stated, the figures in the drawings are not to be limited by scale.
[0020] Figure 1 This is a flowchart illustrating the execution process of the timestamp protection time synchronization module. Detailed Implementation
[0021] The specific embodiments of the present invention will now be described in detail with reference to the accompanying drawings. It should be understood that the specific embodiments described herein are for illustrative purposes only and are not intended to limit the scope of protection of the present invention.
[0022] This invention uses an embedded data unpacking device applied in high-reliability scenarios (such as industrial control and communication infrastructure) as an example to specifically illustrate the implementation of the invention. (Refer to...) Figure 1As shown in the flowchart, after the system powers on, it first loads and runs the timestamp protection and time synchronization module, which has been optimized by the compiler tools. The floating-point arithmetic function of the compiler tools used by this timestamp protection and time synchronization module has been optimized by linking a high-precision mathematical arithmetic library to ensure the accuracy of floating-point arithmetic results and eliminate the problem of abnormal zeroing.
[0023] During initialization, the timestamp-protected time synchronization module reads preset network configuration parameters from its configuration file. Based on these parameters, the module calls the operating system's network application programming interface (API) to create a network socket based on the User Datagram Protocol (UDP). This network socket is bound to the local network interface and the recognized port 123 of the Network Time Protocol (NTP). Subsequently, the module explicitly configures the data reception timeout parameter of the network socket to 2 seconds using the network socket option setting function.
[0024] After configuration, the timestamp-protected time synchronization module enters its main service loop. This module continuously attempts to receive time synchronization data conforming to the time synchronization server protocol format from the preset time synchronization server address by calling the network socket receive function. If no valid time synchronization data is received within a 2-second timeout period, the timetamp-protected time synchronization module immediately triggers its alarm mechanism. Specifically, it records a "warning" log entry in the system's persistent log storage area (such as flash memory), with the content identified as "Time synchronization server time synchronization data reception timeout". If time synchronization data continues to be lost, the timetamp-protected time synchronization module will repeatedly trigger this log alarm at a preset 1-minute cycle.
[0025] If the time synchronization server continues to lose time data, the timestamp protection time synchronization module will switch to autonomous timekeeping mode. In this mode, the timestamp protection time synchronization module stops network time synchronization attempts and instead relies on the onboard high-stability clock source to maintain timing. In this embodiment, a temperature-compensated crystal oscillator is used as the clock source. The timestamp protection time synchronization module runs a sophisticated clock maintenance algorithm, which dynamically compensates and corrects the accumulated time based on pre-stored clock source characteristic parameters (such as frequency deviation values). In this way, it can be ensured that the daily accumulated drift of the device's local clock is strictly controlled within ±0.1 seconds when there is no time synchronization server, so that it can still provide the system with a compliant time reference during the interruption of time synchronization server time data reception.
[0026] Once the timestamp protection and time synchronization module successfully receives valid time synchronization data from the time server, it enters the time information processing flow. First, the module parses the received time synchronization data according to the network time protocol format specification, extracting a high-precision timestamp based on Coordinated Universal Time (UTC) from its specific fields. Then, the module converts this timestamp into the internal time representation format used by the embedded system kernel for subsequent comparison and calculation. In this embodiment, the embedded system kernel's internal time uses the "Unix timestamp" format, which represents the number of seconds elapsed since January 1, 1970, 00:00:00 UTC.
[0027] Subsequently, the timestamp protection time synchronization module performs time comparison. It calculates the difference between the Unix timestamp obtained from the time synchronization data parsing and conversion by the time synchronization server and the current local Unix timestamp. In this embodiment, the preset time deviation threshold is 0.050 seconds. If the absolute value of the calculated time difference is less than 0.050 seconds, the timestamp protection time synchronization module determines that the current time synchronization status is good and does not perform any calibration operation. If the time difference is greater than or equal to 0.050 seconds (for example, a measured deviation of 0.200 seconds), the timestamp protection time synchronization module not only records detailed deviation information (such as "time deviation: 0.200 seconds") and reports it to the system log, but also starts an internal counter to record this "threshold exceeded" event. According to the configuration of this embodiment, only when the comparison difference is greater than the 0.050-second threshold after three consecutive successful receptions and comparisons will the timestamp protection time synchronization module finally determine that the time synchronization trigger condition is met and prepare to perform forced time calibration.
[0028] After determining that time synchronization will be performed, the timestamp-protected time synchronization module enters the precise calibration process. First, the timestamp-protected time synchronization module converts the parsed time from the time service server and the system's current local time into a unified high-precision time measurement format. In this embodiment, accumulated seconds are used, which is the number of seconds accumulated since a specific epoch (e.g., Coordinated Universal Time January 1, 1970, 00:00:00) (usually expressed as a floating-point number or a high-precision fixed-point number). Through this conversion, time that might have been expressed in a separate "year, month, day, hour, minute, second" format is normalized into a single, continuous numerical quantity, thereby eliminating unit differences and format ambiguities, laying the foundation for subsequent precise calculations.
[0029] Under a unified accumulated second format, the timestamp protection time synchronization module invokes optimized floating-point arithmetic functions to accurately calculate the absolute value of the deviation between the accumulated second value of the time synchronization server and the accumulated second value of the local time. After the calculation is completed, the timestamp protection time synchronization module performs the final calibration operation. Through the system call interface provided by the operating system with atomic guarantees, the corrected accurate time value, including all time parameters such as "year, month, day, hour, minute, and second," is written into the system clock data structure all at once and completely. This operation achieves synchronous and atomic updates of all fields of the system time. This process is completed instantaneously, and the system time jumps directly from the old value to the new value, avoiding the intermediate abnormal states caused by the step-by-step operation of "changing the date first and then changing the time" in traditional methods, thereby eliminating the risk of business timestamp errors during time synchronization.
[0030] Finally, it should be noted that the above embodiments are only used to illustrate the technical solutions of the present invention and are not intended to limit it. Although the embodiments of the present invention have been described in detail, those skilled in the art should understand that modifications or equivalent substitutions to the technical solutions of the present invention do not depart from the spirit and scope of the technical solutions of the present invention, and all such modifications or substitutions should be covered within the scope of protection of the claims of the present invention.
Claims
1. A method for protecting timestamps in an embedded system, characterized in that, The execution based on the timestamp protection time synchronization module in the embedded system includes the following steps: Step S1: Optimize the floating-point arithmetic function of the compilation tool used by the timestamp protection time synchronization module to eliminate the problem of abnormal zeroing of floating-point arithmetic results; Step S2: The timestamp protection time synchronization module creates a time synchronization data receiving communication link based on a preset configuration, and configures the time synchronization data receiving timeout parameter of the communication link; Step S3: The timestamp protection time synchronization module obtains the time synchronization data sent by the external time synchronization device through the communication link. If the time synchronization data is not obtained within the time limit specified by the timeout parameter, an alarm mechanism is triggered. If the data is not obtained continuously, the alarm mechanism is repeatedly triggered according to a preset cycle, and the stability of the device's local clock is maintained in accordance with the preset index. If the data is successfully obtained, proceed to step S3. Step S4: The timestamp protection time synchronization module parses the time synchronization data, converts it into a format consistent with the local time, and compares the difference with the local time to obtain the comparison difference. Step S5: The timestamp protection time synchronization module sets a time deviation threshold. If the comparison difference is less than the time deviation threshold, the local time state is maintained and no calibration operation is performed. If the comparison difference is greater than the time deviation threshold, the deviation information is recorded and reported. When the comparison difference is greater than the time deviation threshold for a preset number of consecutive times, local time calibration is performed based on the time synchronization data. Step S6: During the calibration process, the timestamp protection time synchronization module converts both the time statistics data and the local time into a unified time measurement format, calculates the absolute value of the deviation between the two, and synchronously updates the date and time fields of the local time to complete the time calibration.
2. The method for protecting timestamps in an embedded system according to claim 1, characterized in that, The optimization of the floating-point arithmetic function of the compiler tool in step S1 includes updating the compiler tool version, patching arithmetic logic vulnerabilities, or replacing at least one of the high-precision floating-point arithmetic libraries.
3. The method for protecting timestamps in an embedded system according to claim 1, characterized in that, The communication link mentioned in step S2 can be any one of a network socket link, a serial communication link, or a wireless communication link.
4. The method for protecting timestamps in an embedded system according to claim 1, characterized in that, The alarm mechanism in step S3 includes at least one of log reporting, audible and visual alerts, or sending alarm notifications to a preset management terminal.
5. The method for protecting timestamps in an embedded system according to claim 1, characterized in that, The preset number of times in step S5 is no less than 3 times.
6. The method for protecting timestamps in an embedded system according to claim 1, characterized in that, In step S5, the time deviation threshold can be manually adjusted by the user through the system configuration interface.
7. The method for protecting timestamps in an embedded system according to claim 1, characterized in that, The unified time measurement format in step S6 is any one of the following: seconds, milliseconds, or microseconds counted from the same time epoch. The synchronous update includes a one-time complete overwrite of all time fields, including year, month, day, hour, minute, and second.