TWI751466B - Time synchronization method and time synchronization system - Google Patents

Time synchronization method and time synchronization system Download PDF

Info

Publication number
TWI751466B
TWI751466B TW108147782A TW108147782A TWI751466B TW I751466 B TWI751466 B TW I751466B TW 108147782 A TW108147782 A TW 108147782A TW 108147782 A TW108147782 A TW 108147782A TW I751466 B TWI751466 B TW I751466B
Authority
TW
Taiwan
Prior art keywords
time information
host
time
data packet
test
Prior art date
Application number
TW108147782A
Other languages
Chinese (zh)
Other versions
TW202126089A (en
Inventor
許晉嘉
Original Assignee
宏碁股份有限公司
Priority date (The priority date is an assumption and is not a legal conclusion. Google has not performed a legal analysis and makes no representation as to the accuracy of the date listed.)
Filing date
Publication date
Application filed by 宏碁股份有限公司 filed Critical 宏碁股份有限公司
Priority to TW108147782A priority Critical patent/TWI751466B/en
Publication of TW202126089A publication Critical patent/TW202126089A/en
Application granted granted Critical
Publication of TWI751466B publication Critical patent/TWI751466B/en

Links

Images

Landscapes

  • Synchronisation In Digital Transmission Systems (AREA)

Abstract

A time synchronization method and a time synchronization system are provided. The method includes: establishing a time mapping relationship between a Controller Area Network (CAN) device in a CAN system under a test mode of the CAN device; reading CAN time information of at least one data packet from the CAN device under a normal mode of the CAN device; and recording host time information of the at least one data packet according to the time mapping information and the CAN time information.

Description

時間同步方法與時間同步系統Time Synchronization Method and Time Synchronization System

本發明是有關於一種時間同步技術,且特別是有關於一種時間同步方法與時間同步系統。The present invention relates to a time synchronization technology, and in particular, to a time synchronization method and a time synchronization system.

控制器區域網路(Controller Area Network, CAN)是一種匯流排標準,其可用於車載電子系統或其他產業的電子系統中,以使電子系統中的晶片及/或電子儀器相互通訊。CAN系統中的節點亦稱為CAN裝置(或CAN匯流排裝置)。多個CAN裝置之間可透過雙絞線彼此連接以進行通訊。A Controller Area Network (CAN) is a bus standard that can be used in automotive electronic systems or electronic systems in other industries to enable chips and/or electronic devices in the electronic systems to communicate with each other. Nodes in a CAN system are also referred to as CAN devices (or CAN bus devices). Multiple CAN devices can be connected to each other through twisted pairs for communication.

一般來說,當CAN裝置接收到資料封包時,此CAN裝置會根據裝置內建的時鐘對接收到的資料封包加上一個時間戳,並將此資料封包暫存於緩存器中。爾後,外接的電腦裝置可以經由通用序列匯流排(Universal Serial Bus, USB)或乙太網路等通訊協定來從此CAN裝置的緩存器中讀取資料封包與相關的封包接收時間等資訊。但是,在實務上,CAN裝置內建的時鐘往往與真實時間存在誤差,且不同CAN裝置的時鐘可能也彼此不同步,從而造成應用程式對資料封包的接收時間發生誤判。Generally speaking, when a CAN device receives a data packet, the CAN device adds a time stamp to the received data packet according to the built-in clock of the device, and temporarily stores the data packet in a register. Afterwards, the external computer device can read information such as data packets and related packet reception time from the register of the CAN device through communication protocols such as Universal Serial Bus (USB) or Ethernet. However, in practice, the built-in clock of a CAN device often has errors with the real time, and the clocks of different CAN devices may be out of sync with each other, resulting in misjudgment of the receiving time of the data packet by the application program.

本發明提供一種時間同步方法與時間同步系統,可將CAN裝置所接收的資料封包的接收時間與主機裝置的時間進行同步。The invention provides a time synchronization method and a time synchronization system, which can synchronize the reception time of the data packet received by the CAN device with the time of the host device.

本發明的實施例提供一種時間同步方法,其用於控制器區域網路(Controller Area Network, CAN)系統,所述時間同步方法包括:在所述CAN系統中的CAN裝置的測試模式下,建立所述CAN裝置與主機裝置之間的時間映射關係;在所述CAN裝置的正常模式下,從所述CAN裝置讀取至少一資料封包的CAN時間資訊;以及根據所述時間映射關係與所述CAN時間資訊,記錄所述至少一資料封包的主機時間資訊。An embodiment of the present invention provides a time synchronization method, which is used in a Controller Area Network (CAN) system. The time synchronization method includes: in a test mode of a CAN device in the CAN system, establishing the time mapping relationship between the CAN device and the host device; in the normal mode of the CAN device, read CAN time information of at least one data packet from the CAN device; and according to the time mapping relationship and the CAN time information, recording the host time information of the at least one data packet.

本發明的實施例另提供一種時間同步系統,其包括主機裝置與至少一CAN裝置。所述至少一CAN裝置包括第一CAN裝置並耦接至所述主機裝置。所述主機裝置用以在所述第一CAN裝置的測試模式下,建立所述第一CAN裝置與所述主機裝置之間的時間映射關係。所述主機裝置更用以在所述第一CAN裝置的正常模式下,從所述第一CAN裝置讀取至少一資料封包的CAN時間資訊。所述主機裝置更用以根據所述時間映射關係與所述CAN時間資訊,記錄所述至少一資料封包的主機時間資訊。An embodiment of the present invention further provides a time synchronization system, which includes a host device and at least one CAN device. The at least one CAN device includes a first CAN device and is coupled to the host device. The host device is used for establishing a time mapping relationship between the first CAN device and the host device in the test mode of the first CAN device. The host device is further configured to read CAN time information of at least one data packet from the first CAN device in the normal mode of the first CAN device. The host device is further configured to record host time information of the at least one data packet according to the time mapping relationship and the CAN time information.

基於上述,在CAN裝置的測試模式下,CAN裝置與主機裝置之間的時間映射關係可被建立。此外,在CAN裝置的正常模式下,至少一資料封包的CAN時間資訊可從CAN裝置中讀取。根據所述時間映射關係與所述CAN時間資訊,所述至少一資料封包的主機時間資訊可被記錄,使得所述至少一資料封包的接收時間與主機裝置的時間進行同步。Based on the above, in the test mode of the CAN device, the time mapping relationship between the CAN device and the host device can be established. In addition, in the normal mode of the CAN device, CAN time information of at least one data packet can be read from the CAN device. According to the time mapping relationship and the CAN time information, the host time information of the at least one data packet can be recorded, so that the reception time of the at least one data packet is synchronized with the time of the host device.

圖1是根據本發明的一實施例所繪示的時間同步系統的示意圖。請參照圖1,時間同步系統100包括控制器區域網路(Controller Area Network, CAN)系統10與主機裝置12。CAN系統10包括多個CAN裝置11(1)~11(n)。CAN裝置11(1)~11(n)裝置彼此之間可透過雙絞線連接以相互通訊。FIG. 1 is a schematic diagram of a time synchronization system according to an embodiment of the present invention. Please refer to FIG. 1 , the time synchronization system 100 includes a controller area network (CAN) system 10 and a host device 12 . The CAN system 10 includes a plurality of CAN devices 11(1) to 11(n). The CAN devices 11( 1 ) to 11(n) can be connected to each other through twisted pairs to communicate with each other.

CAN裝置11(1)~11(n)中的每一者可包括CAN控制器(例如微控制器)與CAN收發器。CAN控制器用以控制CAN裝置的運作,而CAN收發器則可用以從其他CAN裝置接收資料封包或發送資料封包至其他CAN裝置。Each of the CAN devices 11(1)-11(n) may include a CAN controller (eg, a microcontroller) and a CAN transceiver. The CAN controller is used to control the operation of the CAN device, and the CAN transceiver can be used to receive data packets from other CAN devices or send data packets to other CAN devices.

CAN裝置11(1)~11(n)中的每一者可連接至一或多個電子控制單元(Electronic Control Unit, ECU)。在一實施例中,是假設時間同步系統100與CAN系統10設置於車載系統中。因此,所述ECU可以是用以控制此車載系統中特定車用電子組件的控制器。此外,多個ECU可經由CAN系統10(例如CAN裝置11(1)~11(n))彼此通訊。然而,在另一實施例中,時間同步系統100與CAN系統10亦可以設置於其他類型的電子系統中,而不限於車載系統。Each of the CAN devices 11(1)-11(n) may be connected to one or more Electronic Control Units (ECUs). In one embodiment, it is assumed that the time synchronization system 100 and the CAN system 10 are installed in an in-vehicle system. Therefore, the ECU may be a controller for controlling specific vehicle electronic components in this vehicle system. Furthermore, multiple ECUs can communicate with each other via the CAN system 10 (eg, CAN devices 11(1)-11(n)). However, in another embodiment, the time synchronization system 100 and the CAN system 10 may also be installed in other types of electronic systems, not limited to the vehicle-mounted system.

主機裝置12耦接至CAN系統10。主機裝置12可經由通用序列匯流排(Universal Serial Bus, USB)或乙太網路等通訊協定來連接至CAN系統10中的第一CAN裝置並與第一CAN裝置通訊。例如,主機裝置12可為任意類型的電腦裝置或電腦主機。例如,主機裝置12可包括中央處理單元(CPU)、或是其他可程式化之一般用途或特殊用途的微處理器、數位訊號處理器(Digital Signal Processor, DSP)、可程式化控制器、特殊應用積體電路(Application Specific Integrated Circuits, ASIC)、可程式化邏輯裝置(Programmable Logic Device, PLD)或其他類似裝置或這些裝置的組合。The host device 12 is coupled to the CAN system 10 . The host device 12 can be connected to and communicate with the first CAN device in the CAN system 10 via a communication protocol such as Universal Serial Bus (USB) or Ethernet. For example, host device 12 may be any type of computer device or computer host. For example, the host device 12 may include a central processing unit (CPU), or other programmable general-purpose or special-purpose microprocessors, digital signal processors (DSPs), programmable controllers, special Application Specific Integrated Circuits (ASIC), Programmable Logic Device (PLD) or other similar devices or combinations of these devices.

在一實施例中,主機裝置12可在第一CAN裝置的測試模式下,建立第一CAN裝置與主機裝置12之間在時間上的映射關係(亦稱為時間映射關係)。此時間映射關係可用於將主機裝置12的時鐘與第一CAN裝置的時鐘同步。In one embodiment, the host device 12 may establish a time mapping relationship (also referred to as a time mapping relationship) between the first CAN device and the host device 12 in the test mode of the first CAN device. This time mapping relationship can be used to synchronize the clock of the host device 12 with the clock of the first CAN device.

在一實施例中,測試模式亦稱為回授(loopback)模式。當第一CAN裝置處於測試模式時,若第一CAN裝置接收到來自主機裝置12的封包(亦稱為測試封包),第一CAN裝置可直接將此測試封包回傳給主機裝置12,而不將此測試封包發送給CAN系統10中的其餘CAN裝置。In one embodiment, the test mode is also referred to as a loopback mode. When the first CAN device is in the test mode, if the first CAN device receives a packet (also referred to as a test packet) from the host device 12, the first CAN device can directly return the test packet to the host device 12 without This test packet is sent to the rest of the CAN devices in the CAN system 10 .

在一實施例中,主機裝置12可在第一CAN裝置的正常模式下,從第一CAN裝置讀取至少一封包(亦稱為資料封包)的CAN時間資訊。例如,當第一CAN裝置從CAN系統10中的其餘CAN裝置接收到一個資料封包時,第一CAN裝置可記錄其接收到此資料封包的時間(即CAN時間資訊)。換言之,一個資料封包的CAN時間資訊可反映第一CAN裝置接收到此資料封包的時間。第一CAN裝置可將所接收到的資料封包與相關的資訊(例如封包ID、封包內容及CAN時間資訊)儲存於其內部的緩存器。當第一CAN裝置處於正常模式時,主機裝置12可從第一CAN裝置的緩存器中讀取資料封包以及與此資料封包相關的資訊(例如所述CAN時間資訊)。In one embodiment, the host device 12 may read CAN time information of at least one packet (also referred to as a data packet) from the first CAN device in the normal mode of the first CAN device. For example, when the first CAN device receives a data packet from the remaining CAN devices in the CAN system 10, the first CAN device can record the time at which it received the data packet (ie, CAN time information). In other words, the CAN time information of a data packet can reflect the time when the first CAN device received the data packet. The first CAN device can store the received data packet and related information (eg, packet ID, packet content and CAN time information) in its internal register. When the first CAN device is in the normal mode, the host device 12 can read the data packet and information related to the data packet (eg, the CAN time information) from the register of the first CAN device.

在一實施例中,主機裝置12可根據所述時間映射關係與所述資料封包的CAN時間資訊,記錄所述資料封包的主機時間資訊。此主機時間資訊是指當第一CAN裝置接收到某一資料封包時,此時的主機裝置12的時鐘所指示的時間。換言之,在從第一CAN裝置讀取的某一資料封包的CAN時間資訊後,主機裝置12可根據所述時間映射關係將此資料封包的CAN時間資訊轉換為當第一CAN裝置接收到此資料封包時,此時的主機裝置12的時鐘所指示的時間(即主機時間資訊)。In one embodiment, the host device 12 may record the host time information of the data packet according to the time mapping relationship and the CAN time information of the data packet. The host time information refers to the time indicated by the clock of the host device 12 when the first CAN device receives a certain data packet. In other words, after reading the CAN time information of a data packet from the first CAN device, the host device 12 can convert the CAN time information of the data packet into when the first CAN device receives the data according to the time mapping relationship The time indicated by the clock of the host device 12 at this time (ie, the host time information) when the packet is being packaged.

在以下的實施例中,是以CAN裝置11(1)作為第一CAN裝置的範例。然而,在另一實施例中,第一CAN裝置亦可以是指CAN裝置11(2)~11(n)中的一或多者。In the following embodiments, the CAN device 11(1) is used as an example of the first CAN device. However, in another embodiment, the first CAN device may also refer to one or more of the CAN devices 11(2)˜11(n).

圖2是根據本發明的一實施例所繪示的建立第一CAN裝置與主機裝置之間的時間映射關係的示意圖。請參照圖1與圖2,在時間範圍21內,主機裝置12可將CAN裝置11(1)設定為測試模式(亦稱為第一測試模式)。在第一測試模式下(即時間範圍21內),主機裝置12可發送測試封包(亦稱為第一測試封包)PT(1)至CAN裝置11(1)。然後,主機裝置12可記錄測試封包PT(1)的主機時間資訊(亦稱為第一主機時間資訊)TP(1)與測試封包PT(1)的CAN時間資訊(亦稱為第一CAN時間資訊)TC(1)。FIG. 2 is a schematic diagram of establishing a time mapping relationship between a first CAN device and a host device according to an embodiment of the present invention. Referring to FIGS. 1 and 2 , within the time range 21 , the host device 12 may set the CAN device 11 ( 1 ) to a test mode (also referred to as a first test mode). In the first test mode (ie, within the time range 21 ), the host device 12 may send a test packet (also referred to as a first test packet) PT( 1 ) to the CAN device 11 ( 1 ). Then, the host device 12 may record the host time information (also referred to as the first host time information) TP(1) of the test packet PT(1) and the CAN time information (also referred to as the first CAN time) of the test packet PT(1). Information) TC(1).

在一實施例中,當送出測試封包PT(1)時,主機裝置12可記錄送出測試封包PT(1)時的主機時間資訊T1(Tx)。例如,主機時間資訊T1(Tx)可反映主機裝置12送出測試封包PT(1)的時間。當CAN裝置11(1)接收到測試封包PT(1)時,CAN裝置11(1)可記錄測試封包PT(1)的CAN時間資訊TC(1)。例如,CAN時間資訊TC(1)可反映CAN裝置11(1)接收到測試封包PT(1)的時間。當主機裝置12接收到CAN裝置11(1)回傳的測試封包PT(1)時,主機裝置12可記錄測試封包PT(1)的另一主機時間資訊T1(Rx)。例如,主機時間資訊T1(Rx)可反映主機裝置12接收到測試封包PT(1)的時間。主機裝置12可根據主機時間資訊T1(Tx)與T1(Rx)決定主機時間資訊TP(1)。例如,主機時間資訊TP(1)可等於(T1(Tx)+T1(Rx))/2。在一實施例中,記錄主機時間資訊TP(1)與CAN時間資訊TC(1)的操作類似於建立主機時間資訊TP(1)與CAN時間資訊TC(1)之間的映射關係。In one embodiment, when the test packet PT(1) is sent, the host device 12 may record the host time information T1(Tx) when the test packet PT(1) is sent. For example, the host time information T1(Tx) may reflect the time when the host device 12 sends the test packet PT(1). When the CAN device 11(1) receives the test packet PT(1), the CAN device 11(1) can record the CAN time information TC(1) of the test packet PT(1). For example, the CAN time information TC(1) may reflect the time when the CAN device 11(1) receives the test packet PT(1). When the host device 12 receives the test packet PT(1) returned by the CAN device 11(1), the host device 12 may record another host time information T1(Rx) of the test packet PT(1). For example, the host time information T1(Rx) may reflect the time when the host device 12 receives the test packet PT(1). The host device 12 may determine the host time information TP(1) according to the host time information T1(Tx) and T1(Rx). For example, the host time information TP(1) may be equal to (T1(Tx)+T1(Rx))/2. In one embodiment, the operation of recording the host time information TP(1) and the CAN time information TC(1) is similar to establishing a mapping relationship between the host time information TP(1) and the CAN time information TC(1).

在記錄主機時間資訊TP(1)與CAN時間資訊TC(1)後,在時間範圍22內,主機裝置12可控制CAN裝置11(1)從第一測試模式切換至正常模式。在CAN裝置11(1)的正常模式下(即時間範圍22內),主機裝置12可從CAN裝置11(1)讀取至少一資料封包的CAN時間資訊。After recording the host time information TP(1) and the CAN time information TC(1), within the time range 22, the host device 12 can control the CAN device 11(1) to switch from the first test mode to the normal mode. In the normal mode of the CAN device 11(1) (ie, within the time range 22), the host device 12 can read CAN time information of at least one data packet from the CAN device 11(1).

須注意的是,在圖2的實施例中,是以讀取資料封包PN(k)的CAN時間資訊TC(k)作為範例。其中,資料封包PN(k)是暫存在CAN裝置11(1)的緩存器的資料封包,且CAN時間資訊TC(k)可反映CAN裝置11(1)接收到資料封包PN(k)的時間。主機裝置12可記錄資料封包PN(k)的CAN時間資訊TC(k)。資料封包PN(k)的數目可以是一或多個,本發明不加以限制。It should be noted that, in the embodiment of FIG. 2 , the CAN time information TC(k) of the data packet PN(k) is read as an example. The data packet PN(k) is a data packet temporarily stored in the register of the CAN device 11(1), and the CAN time information TC(k) can reflect the time when the CAN device 11(1) received the data packet PN(k) . The host device 12 may record the CAN time information TC(k) of the data packet PN(k). The number of data packets PN(k) can be one or more, which is not limited in the present invention.

在記錄資料封包PN(k)的CAN時間資訊TC(k)後,在時間範圍23內,主機裝置12可將CAN裝置11(1)再次設定為測試模式(亦稱為第二測試模式)。在第二測試模式下(即時間範圍23內),主機裝置12可發送測試封包(亦稱為第二測試封包)PT(2)至CAN裝置11(1)。然後,主機裝置12可記錄測試封包PT(2)的主機時間資訊(亦稱為第二主機時間資訊)TP(2)與測試封包PT(2)的CAN時間資訊(亦稱為第二CAN時間資訊)TC(2)。After recording the CAN time information TC(k) of the data packet PN(k), within the time range 23, the host device 12 can set the CAN device 11(1) to the test mode (also referred to as the second test mode) again. In the second test mode (ie, within the time range 23), the host device 12 may send a test packet (also referred to as a second test packet) PT(2) to the CAN device 11(1). Then, the host device 12 may record the host time information (also referred to as the second host time information) TP(2) of the test packet PT(2) and the CAN time information (also referred to as the second CAN time) of the test packet PT(2). Information) TC(2).

在一實施例中,當送出測試封包PT(2)時,主機裝置12可記錄送出測試封包PT(2)時的主機時間資訊T2(Tx)。例如,主機時間資訊T2(Tx)可反映主機裝置12送出測試封包PT(2)的時間。當CAN裝置11(1)接收到測試封包PT(2)時,CAN裝置11(1)可記錄測試封包PT(2)的CAN時間資訊TC(2)。例如,CAN時間資訊TC(2)可反映CAN裝置11(1)接收到測試封包PT(2)的時間。當主機裝置12接收到CAN裝置11(1)回傳的測試封包PT(2)時,主機裝置12可記錄測試封包PT(2)的另一主機時間資訊T2(Rx)。例如,主機時間資訊T2(Rx)可反映主機裝置12接收到測試封包PT(2)的時間。主機裝置12可根據主機時間資訊T2(Tx)與T2(Rx)決定主機時間資訊TP(2)。例如,主機時間資訊TP(2)可等於(T2(Tx)+T2(Rx))/2。在一實施例中,記錄主機時間資訊TP(2)與CAN時間資訊TC(2)的操作類似於建立主機時間資訊TP(2)與CAN時間資訊TC(2)之間的映射關係。In one embodiment, when the test packet PT(2) is sent, the host device 12 may record the host time information T2(Tx) when the test packet PT(2) is sent. For example, the host time information T2(Tx) may reflect the time when the host device 12 sends the test packet PT(2). When the CAN device 11(1) receives the test packet PT(2), the CAN device 11(1) can record the CAN time information TC(2) of the test packet PT(2). For example, the CAN time information TC(2) can reflect the time when the CAN device 11(1) receives the test packet PT(2). When the host device 12 receives the test packet PT(2) returned by the CAN device 11(1), the host device 12 can record another host time information T2(Rx) of the test packet PT(2). For example, the host time information T2(Rx) may reflect the time when the host device 12 receives the test packet PT(2). The host device 12 can determine the host time information TP(2) according to the host time information T2(Tx) and T2(Rx). For example, the host time information TP(2) may be equal to (T2(Tx)+T2(Rx))/2. In one embodiment, the operation of recording the host time information TP(2) and the CAN time information TC(2) is similar to establishing a mapping relationship between the host time information TP(2) and the CAN time information TC(2).

圖3是根據本發明的一實施例所繪示的根據時間映射關係與CAN時間資訊,記錄資料封包的主機時間資訊的示意圖。請參照圖1至圖3,主機裝置12可根據主機時間資訊TP(1)、CAN時間資訊TC(1)、主機時間資訊TP(2)、CAN時間資訊TC(2)及資料封包PN(k)的CAN時間資訊TC(k)決定主機時間資訊TP(k)。主機時間資訊TP(k)可反映當CAN裝置11(1)接收到資料封包PN(k)時,此時的主機裝置12的時鐘所指示的時間。3 is a schematic diagram of recording host time information of a data packet according to a time mapping relationship and CAN time information according to an embodiment of the present invention. Referring to FIGS. 1 to 3 , the host device 12 can use the host time information TP( 1 ), the CAN time information TC( 1 ), the host time information TP( 2 ), the CAN time information TC( 2 ) and the data packet PN(k ) of the CAN time information TC(k) determines the host time information TP(k). The host time information TP(k) can reflect the time indicated by the clock of the host device 12 when the CAN device 11(1) receives the data packet PN(k).

在一實施例中,主機裝置12可根據主機時間資訊TP(1)、CAN時間資訊TC(1)、主機時間資訊TP(2)及CAN時間資訊TC(2),將CAN時間資訊TC(k)映射至主機時間資訊TP(k)。在一實施例中,主機裝置12可獲得CAN時間資訊TC(k)相較於CAN時間資訊TC(1)與TC(2)的至少其中之一的偏移時間資訊。然後,主機裝置12可根據主機時間資訊TP(1)、主機時間資訊TP(2)及此偏移時間資訊獲得主機時間資訊TP(k)。In one embodiment, the host device 12 can convert the CAN time information TC(k) according to the host time information TP(1), the CAN time information TC(1), the host time information TP(2) and the CAN time information TC(2). ) maps to the host time information TP(k). In one embodiment, the host device 12 can obtain the offset time information of the CAN time information TC(k) compared to at least one of the CAN time information TC(1) and TC(2). Then, the host device 12 can obtain the host time information TP(k) according to the host time information TP(1), the host time information TP(2) and the offset time information.

在一實施例中,主機裝置12可根據CAN時間資訊TC(1)、TC(2)及TC(k)獲得差值r(1)與r(2)。差值r(1)反映CAN時間資訊TC(1)與TC(k)之間的時間差(例如r(1)=TC(k)-TC(1))。差值r(2)反映CAN時間資訊TC(k)與TC(2)之間的時間差(例如r(2)=TC(2)-TC(k))。差值r(1)及/或r(2)可為所述偏移時間資訊。主機裝置12可根據差值r(1)與r(2)的比值(或者差值r(1)與(r(1)+r(2))的比值)來在主機時間資訊TP(1)與TP(2)之間決定主機時間資訊TP(k)。In one embodiment, the host device 12 can obtain the difference values r(1) and r(2) according to the CAN time information TC(1), TC(2) and TC(k). The difference r(1) reflects the time difference between the CAN time information TC(1) and TC(k) (for example, r(1)=TC(k)−TC(1)). The difference r(2) reflects the time difference between the CAN time information TC(k) and TC(2) (eg r(2)=TC(2)−TC(k)). The difference r(1) and/or r(2) may be the offset time information. The host device 12 may use the ratio of the difference r(1) to r(2) (or the ratio of the difference r(1) to (r(1)+r(2))) to calculate the host time information TP(1) The host time information TP(k) is determined between TP(2).

須注意的是,主機時間資訊TP(1)與所決定的主機時間資訊TP(k)之間具有差值r(3),且主機時間資訊TP(2)與所決定的主機時間資訊TP(k)之間具有差值r(4)。差值r(3)反映主機時間資訊TP(1)與TP(k)之間的時間差(例如r(3)=TP(k)-TP(1))。差值r(4)反映主機時間資訊TP(k)與TP(2)之間的時間差(例如r(4)=TP(2)-TP(k))。差值r(3)與r(4)之間的比值可相同(或實質相同)於差值r(1)與r(2)之間的比值。It should be noted that there is a difference r(3) between the host time information TP(1) and the determined host time information TP(k), and the host time information TP(2) and the determined host time information TP( There is a difference r(4) between k). The difference r(3) reflects the time difference between the host time information TP(1) and TP(k) (eg, r(3)=TP(k)−TP(1)). The difference r(4) reflects the time difference between the host time information TP(k) and TP(2) (eg, r(4)=TP(2)-TP(k)). The ratio between the differences r(3) and r(4) may be the same (or substantially the same) as the ratio between the differences r(1) and r(2).

在一實施例中,主機裝置12可獲得主機時間資訊TP(1)與TP(2)之間的時間差Td。主機裝置12可將時間差Td乘上(r(1)/(r(1)+r(2)))獲得差值r(3)。然後,主機裝置12可將主機時間資訊TP(1)加上差值r(3)以獲得主機時間資訊TP(k)。須注意的是,主機裝置12還可藉由其他的邏輯運算方法來獲得主機時間資訊TP(k),只要符合上述數值比例之規範即可。In one embodiment, the host device 12 can obtain the time difference Td between the host time information TP(1) and TP(2). The host device 12 may multiply the time difference Td by (r(1)/(r(1)+r(2))) to obtain the difference r(3). Then, the host device 12 may add the host time information TP(1) to the difference r(3) to obtain the host time information TP(k). It should be noted that the host device 12 can also obtain the host time information TP(k) through other logical operation methods, as long as the above specification of the numerical ratio is met.

在一實施例中,主機裝置12可將所獲得的資料封包PN(k)的主機時間資訊TP(k)藉由主機裝置12的顯示器呈現。藉此,主機裝置12的操作者可以根據主機裝置12的顯示器所呈現的訊息清楚得知資料封包PN(k)是在主機裝置12的時鐘所指示的時間點TP(k)被CAN裝置11(1)接收。此外,主機裝置12的應用程式可根據同步後的主機時間資訊TP(k)來對資料封包PN(k)進行處理。In one embodiment, the host device 12 may display the obtained host time information TP(k) of the data packet PN(k) on the display of the host device 12 . In this way, the operator of the host device 12 can clearly know that the data packet PN(k) was sent to the CAN device 11 ( 1) Receive. In addition, the application program of the host device 12 can process the data packet PN(k) according to the synchronized host time information TP(k).

圖4是根據本發明的一實施例所繪示的時間同步方法的流程圖。請參照圖4,在步驟S401中,在CAN系統中的CAN裝置的測試模式下,建立所述CAN裝置與主機裝置之間的時間映射關係。在步驟S402中,在所述CAN裝置的正常模式下,從所述CAN裝置讀取至少一資料封包的CAN時間資訊。在步驟S403中,根據所述時間映射關係與所述CAN時間資訊,記錄所述至少一資料封包的主機時間資訊。FIG. 4 is a flowchart of a time synchronization method according to an embodiment of the present invention. Referring to FIG. 4 , in step S401 , in the test mode of the CAN device in the CAN system, a time mapping relationship between the CAN device and the host device is established. In step S402, in the normal mode of the CAN device, read CAN time information of at least one data packet from the CAN device. In step S403, the host time information of the at least one data packet is recorded according to the time mapping relationship and the CAN time information.

圖5是根據本發明的一實施例所繪示的時間同步方法的流程圖。請參照圖5,在步驟S501中,將CAN裝置設定為測試模式。在步驟S502中,在所述CAN裝置中建立接收器排程(receiver thread)。在步驟S503中,從主機裝置送出測試封包並記錄封包送出時的時間。在步驟S504中,從主機裝置的接收器排程接收從所述CAN裝置返回的測試封包並記錄主機裝置與所述CAN裝置分別接收到此測試封包的時間。在步驟S505中,根據步驟S503與S504所記錄的時間記錄測試封包的主機時間資訊以及測試封包的CAN時間資訊。例如,圖5的流程與相關操作可於圖2中的時間範圍21與23內執行。FIG. 5 is a flowchart of a time synchronization method according to an embodiment of the present invention. Referring to FIG. 5 , in step S501 , the CAN device is set to a test mode. In step S502, a receiver thread is established in the CAN device. In step S503, the test packet is sent from the host device and the time when the packet is sent is recorded. In step S504, the receiver of the host device schedules to receive the test packet returned from the CAN device and records the time when the host device and the CAN device respectively receive the test packet. In step S505, the host time information of the test packet and the CAN time information of the test packet are recorded according to the times recorded in steps S503 and S504. For example, the process and related operations of FIG. 5 may be performed within time ranges 21 and 23 in FIG. 2 .

圖6是根據本發明的一實施例所繪示的時間同步方法的流程圖。請參照圖6,在步驟S601中,將CAN裝置設定為正常模式。在步驟S602中,建立一記錄檔。在步驟S603中,從所述CAN裝置讀取至少一資料封包以及與所述至少一資料封包相關的CAN資訊,並將這些資訊記載於所述記錄檔。例如,圖6的流程與相關操作可於圖2中的時間範圍22內執行。FIG. 6 is a flowchart of a time synchronization method according to an embodiment of the present invention. Referring to FIG. 6, in step S601, the CAN device is set to the normal mode. In step S602, a record file is created. In step S603, at least one data packet and CAN information related to the at least one data packet are read from the CAN device, and the information is recorded in the log file. For example, the flow and related operations of FIG. 6 may be performed within the time frame 22 of FIG. 2 .

圖7是根據本發明的一實施例所繪示的時間同步方法的流程圖。請參照圖7,在步驟S701中,開啟所述記錄檔。在步驟S702中,從所述記錄檔中讀取所述至少一資料封包的CAN資訊,包含CAN時間資訊。在步驟S703中,根據所建立的時間映射關係與所述CAN資訊記錄所述至少一資料封包的主機時間資訊。例如,圖7的步驟S703可參照圖3的實施例之說明,在此不重複贅述。FIG. 7 is a flowchart of a time synchronization method according to an embodiment of the present invention. Referring to FIG. 7, in step S701, the record file is opened. In step S702, the CAN information of the at least one data packet is read from the record file, including CAN time information. In step S703, the host time information of the at least one data packet is recorded according to the established time mapping relationship and the CAN information. For example, for step S703 in FIG. 7 , reference may be made to the description of the embodiment in FIG. 3 , and details are not repeated here.

然而,圖4至圖7中各步驟已詳細說明如上,在此便不再贅述。值得注意的是,圖4至圖7中各步驟可以實作為多個程式碼或是電路,本發明不加以限制。此外,圖4至圖7的方法可以搭配以上範例實施例使用,也可以單獨使用,本發明不加以限制。However, each step in FIG. 4 to FIG. 7 has been described in detail as above, and will not be repeated here. It should be noted that each step in FIG. 4 to FIG. 7 can be implemented as a plurality of codes or circuits, which is not limited by the present invention. In addition, the methods of FIG. 4 to FIG. 7 can be used in conjunction with the above exemplary embodiments, and can also be used alone, which is not limited by the present invention.

綜上所述,在CAN裝置的測試模式下,CAN裝置與主機裝置之間的時間映射關係可被建立。此外,在CAN裝置的正常模式下,至少一資料封包的CAN時間資訊可從CAN裝置中讀取。根據所述時間映射關係與所述CAN時間資訊,所述至少一資料封包的主機時間資訊可被記錄,使得所述至少一資料封包的接收時間與主機裝置的時間進行同步。藉此,可有效改善以往CAN裝置的時鐘與主機裝置的時鐘之間在時間上不同步而衍生的問題(例如主機裝置的應用程式對資料封包的接收時間及/或順序發生誤判)。To sum up, in the test mode of the CAN device, the time mapping relationship between the CAN device and the host device can be established. In addition, in the normal mode of the CAN device, CAN time information of at least one data packet can be read from the CAN device. According to the time mapping relationship and the CAN time information, the host time information of the at least one data packet can be recorded, so that the reception time of the at least one data packet is synchronized with the time of the host device. In this way, the problems caused by the time synchronization between the clock of the CAN device and the clock of the host device in the past can be effectively improved (for example, the application of the host device misjudged the receiving time and/or sequence of the data packets).

100:時間同步系統 10:控制器區域網路(CAN)系統 11(1)~11(n):CAN裝置 12:主機裝置 21、22、23:時間範圍 PT(1)、PT(2):測試封包 PN(k):資料封包 TP(1)、TP(2)、TP(k):主機時間資訊 TC(1)、TC(2)、TC(k):CAN時間資訊 r(1)、r(2)、r(3)、r(4):差值 S401~S403、S501~S505、S601~S603、S701~S703:步驟100: Time Synchronization System 10: Controller Area Network (CAN) system 11(1)~11(n): CAN device 12: Host device 21, 22, 23: time frame PT(1), PT(2): Test packets PN(k): data packet TP(1), TP(2), TP(k): host time information TC(1), TC(2), TC(k): CAN time information r(1), r(2), r(3), r(4): difference S401~S403, S501~S505, S601~S603, S701~S703: Steps

圖1是根據本發明的一實施例所繪示的時間同步系統的示意圖。 圖2是根據本發明的一實施例所繪示的建立第一CAN裝置與主機裝置之間的時間映射關係的示意圖。 圖3是根據本發明的一實施例所繪示的根據時間映射關係與CAN時間資訊,記錄資料封包的主機時間資訊的示意圖。 圖4是根據本發明的一實施例所繪示的時間同步方法的流程圖。 圖5是根據本發明的一實施例所繪示的時間同步方法的流程圖。 圖6是根據本發明的一實施例所繪示的時間同步方法的流程圖。 圖7是根據本發明的一實施例所繪示的時間同步方法的流程圖。FIG. 1 is a schematic diagram of a time synchronization system according to an embodiment of the present invention. FIG. 2 is a schematic diagram of establishing a time mapping relationship between a first CAN device and a host device according to an embodiment of the present invention. 3 is a schematic diagram of recording host time information of a data packet according to a time mapping relationship and CAN time information according to an embodiment of the present invention. FIG. 4 is a flowchart of a time synchronization method according to an embodiment of the present invention. FIG. 5 is a flowchart of a time synchronization method according to an embodiment of the present invention. FIG. 6 is a flowchart of a time synchronization method according to an embodiment of the present invention. FIG. 7 is a flowchart of a time synchronization method according to an embodiment of the present invention.

S401~S403:步驟S401~S403: Steps

Claims (8)

一種時間同步方法,用於一控制器區域網路(Controller Area Network,CAN)系統,該時間同步方法包括:將該CAN系統中的一CAN裝置設定為一第一測試模式;在該第一測試模式下,發送一第一測試封包至該CAN裝置並記錄該第一測試封包的一第一主機時間資訊與該第一測試封包的一第一CAN時間資訊;控制該CAN裝置從該第一測試模式切換至一正常模式;在該CAN裝置的該正常模式下,從該CAN裝置讀取至少一資料封包的CAN時間資訊;在從該CAN裝置讀取該至少一資料封包的該CAN時間資訊後,將該CAN裝置設定為一第二測試模式;在該第二測試模式下,發送一第二測試封包至該CAN裝置並記錄該第二測試封包的一第二主機時間資訊與該第二測試封包的一第二CAN時間資訊;以及根據該第一CAN時間資訊、該第一主機時間資訊、該第二CAN時間資訊、該第二主機時間資訊及該至少一資料封包的該CAN時間資訊,記錄該至少一資料封包的一主機時間資訊。 A time synchronization method for a controller area network (Controller Area Network, CAN) system, the time synchronization method comprising: setting a CAN device in the CAN system to a first test mode; In the mode, send a first test packet to the CAN device and record a first host time information of the first test packet and a first CAN time information of the first test packet; control the CAN device from the first test The mode is switched to a normal mode; in the normal mode of the CAN device, the CAN time information of at least one data packet is read from the CAN device; after reading the CAN time information of the at least one data packet from the CAN device , set the CAN device to a second test mode; in the second test mode, send a second test packet to the CAN device and record a second host time information of the second test packet and the second test a second CAN time information of the packet; and according to the first CAN time information, the first host time information, the second CAN time information, the second host time information and the CAN time information of the at least one data packet, A host time information of the at least one data packet is recorded. 如申請專利範圍第1項所述的時間同步方法,其中記錄該第一測試封包的該第一主機時間資訊的步驟包括:根據該主機裝置發送該第一測試封包的時間與該主機裝置接收經由該CAN裝置送回該主機裝置的該第一測試封包的時間決定 該第一主機時間資訊。 The time synchronization method of claim 1, wherein the step of recording the first host time information of the first test packet comprises: according to the time when the host device sends the first test packet and the time when the host device receives the first test packet via The timing of the CAN device sending the first test packet back to the host device is determined The first host time information. 如申請專利範圍第1項所述的時間同步方法,其中根據該第一CAN時間資訊、該第一主機時間資訊、該第二CAN時間資訊、該第二主機時間資訊及該至少一資料封包的該CAN時間資訊,記錄該至少一資料封包的該主機時間資訊的步驟包括:根據該第一主機時間資訊、該第一CAN時間資訊、該第二主機時間資訊及該第二CAN時間資訊,將該至少一資料封包的該CAN時間資訊映射至該至少一資料封包的該主機時間資訊。 The time synchronization method as described in claim 1, wherein according to the first CAN time information, the first host time information, the second CAN time information, the second host time information and the at least one data packet For the CAN time information, the step of recording the host time information of the at least one data packet includes: according to the first host time information, the first CAN time information, the second host time information and the second CAN time information, The CAN time information of the at least one data packet is mapped to the host time information of the at least one data packet. 如申請專利範圍第3項所述的時間同步方法,其中根據該第一主機時間資訊、該第一CAN時間資訊、該第二主機時間資訊及該第二CAN時間資訊,將該至少一資料封包的該CAN時間資訊映射至該至少一資料封包的該主機時間資訊的步驟包括:獲得該CAN時間資訊相較於該第一CAN時間資訊與該第二CAN時間資訊的至少其中之一的一偏移時間資訊;以及根據該第一主機時間資訊、該第二主機時間資訊及該偏移時間資訊獲得該至少一資料封包的該主機時間資訊。 The time synchronization method according to claim 3, wherein the at least one data packet is packaged according to the first host time information, the first CAN time information, the second host time information and the second CAN time information The step of mapping the CAN time information to the host time information of the at least one data packet includes: obtaining an offset of the CAN time information compared to at least one of the first CAN time information and the second CAN time information and obtaining the host time information of the at least one data packet according to the first host time information, the second host time information and the offset time information. 一種時間同步系統,包括:一主機裝置;以及至少一CAN裝置,包括一第一CAN裝置並耦接至該主機裝置,其中該主機裝置用以:將該第一CAN裝置設定為一第一測試模式; 在該第一測試模式下,發送一第一測試封包至該第一CAN裝置並記錄該第一測試封包的一第一主機時間資訊與該第一測試封包的一第一CAN時間資訊;控制該第一CAN裝置從該第一測試模式切換至一正常模式;在該第一CAN裝置的該正常模式下,從該第一CAN裝置讀取至少一資料封包的CAN時間資訊;在從該第一CAN裝置讀取該至少一資料封包的該CAN時間資訊後,將該第一CAN裝置設定為一第二測試模式;在該第二測試模式下,發送一第二測試封包至該第一CAN裝置並記錄該第二測試封包的一第二主機時間資訊與該第二測試封包的一第二CAN時間資訊;以及根據該第一CAN時間資訊、該第一主機時間資訊、該第二CAN時間資訊、該第二主機時間資訊及該至少一資料封包的該CAN時間資訊,記錄該至少一資料封包的一主機時間資訊。 A time synchronization system, comprising: a host device; and at least one CAN device, including a first CAN device and coupled to the host device, wherein the host device is used for: setting the first CAN device as a first test model; In the first test mode, send a first test packet to the first CAN device and record a first host time information of the first test packet and a first CAN time information of the first test packet; control the The first CAN device is switched from the first test mode to a normal mode; in the normal mode of the first CAN device, the CAN time information of at least one data packet is read from the first CAN device; After the CAN device reads the CAN time information of the at least one data packet, the first CAN device is set to a second test mode; in the second test mode, a second test packet is sent to the first CAN device and record a second host time information of the second test packet and a second CAN time information of the second test packet; and according to the first CAN time information, the first host time information, the second CAN time information , the second host time information and the CAN time information of the at least one data packet, record a host time information of the at least one data packet. 如申請專利範圍第5項所述的時間同步系統,其中該主機裝置記錄該第一測試封包的該第一主機時間資訊的操作包括:根據該主機裝置發送該第一測試封包的時間與該主機裝置接收經由該第一CAN裝置送回該主機裝置的該第一測試封包的時間決定該第一主機時間資訊。 The time synchronization system according to claim 5, wherein the operation of the host device recording the first host time information of the first test packet includes: according to the time when the host device sends the first test packet and the host The time at which the device receives the first test packet sent back to the host device via the first CAN device determines the first host time information. 如申請專利範圍第5項所述的時間同步系統,其中該主機裝置根據該第一CAN時間資訊、該第一主機時間資訊、該第二 CAN時間資訊、該第二主機時間資訊及該至少一資料封包的該CAN時間資訊,記錄該至少一資料封包的該主機時間資訊的操作包括:根據該第一主機時間資訊、該第一CAN時間資訊、該第二主機時間資訊及該第二CAN時間資訊,將該至少一資料封包的該CAN時間資訊映射至該至少一資料封包的該主機時間資訊。 The time synchronization system of claim 5, wherein the host device is based on the first CAN time information, the first host time information, the second CAN time information, the second host time information and the CAN time information of the at least one data packet, the operation of recording the host time information of the at least one data packet includes: according to the first host time information, the first CAN time information, the second host time information and the second CAN time information, and the CAN time information of the at least one data packet is mapped to the host time information of the at least one data packet. 如申請專利範圍第7項所述的時間同步系統,其中該主機裝置根據該第一主機時間資訊、該第一CAN時間資訊、該第二主機時間資訊及該第二CAN時間資訊,將該至少一資料封包的該CAN時間資訊映射至該至少一資料封包的該主機時間資訊的操作包括:獲得該CAN時間資訊相較於該第一CAN時間資訊與該第二CAN時間資訊的至少其中之一的一偏移時間資訊;以及根據該第一主機時間資訊、該第二主機時間資訊及該偏移時間資訊獲得該至少一資料封包的該主機時間資訊。 The time synchronization system of claim 7, wherein the host device according to the first host time information, the first CAN time information, the second host time information and the second CAN time information, the at least The operation of mapping the CAN time information of a data packet to the host time information of the at least one data packet includes: obtaining the CAN time information compared to at least one of the first CAN time information and the second CAN time information and obtain the host time information of the at least one data packet according to the first host time information, the second host time information and the offset time information.
TW108147782A 2019-12-26 2019-12-26 Time synchronization method and time synchronization system TWI751466B (en)

Priority Applications (1)

Application Number Priority Date Filing Date Title
TW108147782A TWI751466B (en) 2019-12-26 2019-12-26 Time synchronization method and time synchronization system

Applications Claiming Priority (1)

Application Number Priority Date Filing Date Title
TW108147782A TWI751466B (en) 2019-12-26 2019-12-26 Time synchronization method and time synchronization system

Publications (2)

Publication Number Publication Date
TW202126089A TW202126089A (en) 2021-07-01
TWI751466B true TWI751466B (en) 2022-01-01

Family

ID=77908859

Family Applications (1)

Application Number Title Priority Date Filing Date
TW108147782A TWI751466B (en) 2019-12-26 2019-12-26 Time synchronization method and time synchronization system

Country Status (1)

Country Link
TW (1) TWI751466B (en)

Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO1998054872A1 (en) * 1997-05-27 1998-12-03 Micro Motion, Inc. Deterministic serial bus communication system with 'controller area (can)' protocol
US8504864B2 (en) * 2010-12-01 2013-08-06 GM Global Technology Operations LLC Data sensor coordination using time synchronization in a multi-bus controller area network system
CN106603367A (en) * 2017-02-28 2017-04-26 北京艾利特科技有限公司 CAN bus communication method for time synchronization

Patent Citations (3)

* Cited by examiner, † Cited by third party
Publication number Priority date Publication date Assignee Title
WO1998054872A1 (en) * 1997-05-27 1998-12-03 Micro Motion, Inc. Deterministic serial bus communication system with 'controller area (can)' protocol
US8504864B2 (en) * 2010-12-01 2013-08-06 GM Global Technology Operations LLC Data sensor coordination using time synchronization in a multi-bus controller area network system
CN106603367A (en) * 2017-02-28 2017-04-26 北京艾利特科技有限公司 CAN bus communication method for time synchronization

Also Published As

Publication number Publication date
TW202126089A (en) 2021-07-01

Similar Documents

Publication Publication Date Title
US6845416B1 (en) System and method for interfacing a CAN device and a peripheral device
RU2606062C2 (en) Method and device for testing correct function of serial data transmission
CN101937253B (en) Method, device and system for clock synchronization
JP6469365B2 (en) Method for providing general-purpose interface and microcontroller having general-purpose interface
CN110213143B (en) 1553B bus IP core and monitoring system
JP6149150B2 (en) Timestamp correction in skewed multilane communication links
US20150134864A1 (en) Synchronisation of a system of distributed computers
KR20140140508A (en) Method for providing a generic interface and microcontroller with generic interface
KR20140140507A (en) Method for providing a generic interface and microcontroller with generic interface
CN112448874B (en) Vehicle-mounted communication device and time synchronization method thereof
JP4860620B2 (en) Low latency data packet reception and processing
JP2016535483A (en) Network interface
CN111917532A (en) Device and system for use in a data testing environment and method of device testing
TWI751466B (en) Time synchronization method and time synchronization system
US8837473B2 (en) Single timestamp engine for generating timing information for inclusion in packets complying with multiple networking protocols
US20180083800A1 (en) Aggregation Device, System, And Method Thereof
CN101300773A (en) Data interface and method of seeking synchronization
US7694176B2 (en) Fault-tolerant computer and method of controlling same
CN110892653A (en) Time stamping unit and communication control unit for subscriber stations of a communication network
CN113132042A (en) Time synchronization method and time synchronization system
CN104142902A (en) Adaptive interface for coupling FPGA modules
CN113704045A (en) Clock synchronization test method, system and chip
CN111930582A (en) System management bus detection platform, processor and system management bus detection method
US12063287B1 (en) Methods, systems, and computer readable media for determining an internal time of a time-sensitive networking (TSN) network card
US9081743B2 (en) Communication system and communicaton method