CanTsyn

文档信息 Document Information

版本历史 Version History

日期(Date)

作者(Author)

版本(Version)

状态(Status)

说明(Description)

2025/3/2

xiongfei.shi

V0.1

发布(Release)

首次发布(First release)

2025/04/04

xiongfei.shi

V1.0

发布(Release)

正式发布(Official release)

参考文档 References

编号(Number)

分类(Classification)

标题(Title)

版本(Version)

1

Autosar

AUTOSAR_CP_SWS_TimeSyncOverCAN.pdf

R23-11

2

Autosar

AUTOSAR_CP_SWS_SynchronizedTimeBaseManager.pdf

R23-11

术语与简写 Terms and Abbreviations

术语 Terms

术语(Terms)

解释(Explanation)

Global Time Domain

全局时间域(Global Time Domain)

Global Time Master

全域时间主控(Global Time Master)

Debounce Time

同一PDU两条发送消息的最小间隔(Minimum Interval between Two Transmissions of the Same PDU)

SYNC message

时间同步消息(Time synchronization message)

FUP message

后续消息(Subsequent Message)

简写 Abbreviations

简写(Abbreviation)

全称(Full name)

解释(Explanation)

<Bus>TSyn

A bus specific Time Synchronization module

特定总线时间同步模块

CanTSyn

Time Synchronization module for CAN

CAN 时间同步模块

CRC

Cyclic Redundancy Checksum

循环冗余检验

DET

Default Error Tracer

默认错误跟踪器

DLC

Data Length Code

数据长度代码

StbM

Synchronized Time-Base Manager

同步的时间基管理

TS

Time Slave

时间从属

Timesync

Time Synchronization

时间同步

简介 Introduction

CanTSyn 在 AutoSAR 软件层级架构如下图, 其所属于时间同步栈。

The software layer architecture of StbM in AutoSAR is shown in the figure below. It belongs to the time synchronization stack.

CanTSyn在AUTOSAR中的位置(Position of CanTSyn in AUTOSAR))

CanTSyn 在 AUTOSAR 中的位置 (Position of CanTSyn in AUTOSAR)

本文中描述CanTSyn,StbM负责管理时间域,给CanTSyn提供接口用来更新同步时间,给其他用户提供接口用来获取/通知同步时间。CanTSyn负责Can总线上的时间同步。

As described in this document, CanTSyn works with StbM. StbM is responsible for managing the time domain, providing an interface for CanTSyn to update the synchronized time, and providing interfaces for other users to get/notify the synchronized time. CanTSyn is responsible for time synchronization over the CAN bus.

功能描述 Functional Description

CanTSyn模块和StbM模块息息相关,StbM模块提供了时间同步的功能和时钟实例,但不负责在各个总线内的时间分发任务。那么CanTSyn模块就处理了在CAN总线发放时间信息的任务。

The CanTSyn module is closely related to the StbM module. The StbM module provides time synchronization function and clock instances but is not responsible for the task of time distribution on individual buses. Therefore, the CanTSyn module handles the task of distributing time information on the CAN bus.

仅仅通过广播的方式把时间信息从Time Masters发送到Time Slaves会导致时间不精准,这是因为CAN总线传输仲裁机制以及BSW的延迟。

Simply broadcasting time information from Time Masters to Time Slaves may lead to inaccuracies due to the CAN bus arbitration mechanism and BSW latency.

具体实现上我们通过以下的两步算法来尽可能消除这样的延迟:

In practice, this latency is minimized by implementing the following two-step algorithm:

第一步:发送方(Time Master)首先记录当前的同步时间(SYNC)以及本地时间(T0VLT)并在第一个广播信息(所谓的SYNC信息)里,把同步好的时间的秒部分(SYNCSEC)作为内容发送。

Step 1: The sender (Time Master) first records the current synchronized time (SYNC) and the local time (T0VLT), and in the first broadcast message (the SYNC message), sends the seconds part of the synchronized time (SYNCSEC).

发送方在收到 “CAN transmit confirmation”时记录时间戳来得到信息实际发送的时间点(T1VLT)。接收方(Time Slave)收到信息 “CAN receive indication”时记录时间戳来检测信息实际收到的时间点(T2VLT)。

The sender records the timestamp Upon receiving “CAN transmit confirmation” to get the actual transmission time of the message (T1VLT). The receiver (Time Slave) records the timestamp upon receiving “CAN receive indication” to detect the actual time the message was received (T2VLT).

第二步:在第二个同步信息(所谓的FUP(follow-up)信息)里,发送方发送T4作为内容,T4为SYNC消息准备发送和实际发送的时间差(T1VLT-T0VLT)加上T0SYNCNS(同步时间的纳秒部分)(T4=T0SYNCNS+(T1VLT-T0VLT))。对于发送方来说,此时T0SYNCSEC+T4就为同步时间。Time Slave现在从SYNC和FUP消息里获取了足够的信息,再加上先前记录的时间戳T2VLT,就可以确定更加确切的时间信息。接收方将(T0SYNCSEC+T4)和T2VLT时间对信息传给StbM, StbM即可计算同步时间,完成一次Time Master和Time Slave之间的时间同步。

Step 2: In the second synchronization message (the FUP, or follow-up, message), the sender transmits T4 as the content. T4 is the difference between the intended and actual transmission time of the SYNC message (T1VLT - T0VLT), plus the nanoseconds part of the synchronized time (T0SYNCNS). Thus, T4 = T0SYNCNS + (T1VLT - T0VLT). For the sender, the synchronized time at this point is T0SYNCSEC + T4. Time Slave now has obtained sufficient information from the SYNC and FUP messages, and along with its previously recorded timestamp T2VLT, it can determine a more precise time. The receiver passes the time pair information, (T0SYNCSEC + T4) and T2VLT, to StbM. StbM can then calculate the synchronized time, completing the time synchronization cycle between Time Master and Time Slave.

特性 Features

消息格式 Message Format

CanTSyn的协议格式即支持标准CAN模式也支持扩展CAN模式。

The protocol format of CanTSyn supports both standard CAN and extended CAN modes.

时间同步主站 Time Synchronization Master

CanTSyn支持配置为时间主站,将时间同步给同网络下的其他时间从站。

CanTSyn can be configured as a Time Master to synchronize time with other Time Slaves on the same network.

时间同步从站 Time Synchronization Slave

CanTSyn支持配置为时间从站,接受同网络下的时间主机的同步消息,将自己的时间与主站同步。

CanTSyn can be configured as a Time Slave to receive synchronization messages from the Time Master on the same network and synchronize its own time with the master’s time.

偏差 Deviation

1.不再支持偏移时间的同步,配置中删除CanTSynGlobalTimeOfsDataIDList配置项和CanTSynGlobalTimeOfnsDataIDList配置项

1.Synchronization of offset time is no longer supported; the CanTSynGlobalTimeOfsDataIDList and CanTSynGlobalTimeOfnsDataIDList configuration items have been removed.

2.不支持CAN的硬件时间戳

2.CAN hardware timestamps are not supported.

3.不支持时间验证功能

3.Time verification function is not supported

4.不支持数据完整性检查功能(ICV)

4.The data integrity check function (ICV) is not supported.

扩展 Extension

None

集成 Integration

文件列表 File List

静态文件 Static Files

文件(File)

描述(Description)

CanTSyn.h

包含需要使用的宏定义,类型定义,配置结构体声明,外部函数声明(Contains macro definitions, type definitions, configuration structure declarations, and external function declarations that need to be used)

CanTSyn_Cbk.h

包含需要使用的宏定义,类型定义,配置结构体声明,外部回调函数声明(Contains macro definitions, type definitions, configuration structure declarations, and external callback function declarations that need to be used)

CanTSyn_Internal.h

包含需要使用的内部宏定义,内部类型定义,外部数据声明和外部函数声明(Contains internal macro definitions, internal type definitions, external data declarations, and external function declarations that need to be used)

CanTSyn.c

包含需要使用的宏定义,内部变量,内部函数,全局函数(Contains macro definitions, internal variables, internal functions, and global functions that need to be used)

动态文件 Dynamic Files

文件(File)

描述(Description)

CanTSyn_MemMap.h

CanTSyn模块的内存映射(Memory mapping of the CanTrcv module)

CanTSyn_Cfg.h

定义CanTSyn模块预编译时用到的配置参数(Defines configuration parameters used in the pre-compilation of the CanTSyn module)

CanTSyn_Cfg.c

CanTSyn Pre-Compile 配置源文件(Pre-Compile configuration source file of CanTSyn)

CanTSyn_PBcfg.c

CanTSyn Post-Build 配置源文件(Post-Build configuration source file of CanTSyn)

错误处理 Error Handling

开发错误 Development Errors

Error code

Value[hex]

Description

CANTSYN_E_INVALID_PDUID

0x01

API service called with wrong PDU or SDU

CANTSYN_E_UNINIT

0x02

API service used in un-initialized state

CANTSYN_E_NULL_POINTER

0x03

A pointer is NULL

CANTSYN_E_INIT_FAILED

0x04

CanTSyn initialization failed

CANTSYN_E_PARAM

0x05

API called with invalid parameter

CANTSYN_E_INV_CTRL_IDX

0x06

Invalid Controller index

CANTSYN_E_INVALID_PARTITION

0xF0

CanTSyn accessed by wrong partition

CANTSYN_E_ALREADY_INITIALIZED

0xF1

CanTSyn has initialized

产品错误 Product Errors

None

运行时错误 Runtime Errors

None

接口描述 Interface Description

类型定义 Type Definitions

Type Name

Type

Description

CanTSyn_ConfigType

struct

This is the base type for the configuration of the Time Synchronization over CAN.

CanTSyn_TransmissionModeType

enum

Handles the enabling and disabling of the transmission mode.

提供的服务 Services

CanTSyn_Init

void CanTSyn_Init(const CanTSyn_ConfigType *configPtr)

This function initializes the Time Synchronization over CAN.

Sync/Async

Synchronous

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[in]

configPtr

Pointer to selected configuration structure

Return type

void

CanTSyn_GetVersionInfo

void CanTSyn_GetVersionInfo(Std_VersionInfoType *versioninfoPtr)

This service returns the version information of this module.

Sync/Async

Synchronous

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[out]

versioninfoPtr

See Std_VersionInfoType

Return type

void

CanTSyn_SetTransmissionMode

void CanTSyn_SetTransmissionMode(uint8 CtrlIdx, CanTSyn_TransmissionModeType Mode)

This API is used to turn on and off the TX capabilities of the CanTSyn.

Sync/Async

Synchronous

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[in]

CtrlIdx

Index of the CAN channel

[in]

Mode

CANTSYN_TX_OFF CANTSYN_TX_ON

Return type

void

类型定义 Type Definitions

None

提供的服务 Services

CanTSyn_RxIndication

void CanTSyn_RxIndication(PduIdType RxPduId, const PduInfoType *PduInfoPtr)

Indication of a received PDU from a lower layer communication interface module.

Sync/Async

Synchronous

Reentrancy

Reentrant(Reentrant for different PduIds. Non reentrant for the same PduId.)

Parameters

Dir

Name

Description

[in]

RxPduId

ID of the received PDU.

[in]

PduInfoPtr

Contains the length (SduLength) of the received PDU, a pointer to a buffer (SduDataPtr) containing the PDU, and the MetaData related to this PDU.

Return type

void

CanTSyn_TxConfirmation

void CanTSyn_TxConfirmation(PduIdType TxPduId, Std_ReturnType result)

The lower layer communication interface module confirms the transmission of a PDU, or the failure to transmit a PDU.

Sync/Async

Synchronous

Reentrancy

Reentrant(Reentrant for different PduIds. Non reentrant for the same PduId.)

Parameters

Dir

Name

Description

[in]

TxPduId

ID of the PDU that has been transmitted.

[in]

result

E_OK: The PDU was transmitted. E_NOT_OK: Transmission of the PDU failed.

Return type

void

类型定义 Type Definitions

Type Name

Type

Description

CanTSyn_MasterTimeInfoType

struct

store the time information for a time master

CanTSyn_SlaveTimeInfoType

struct

store the time information for a time slave

CanTSyn_GlobalTimeMasterType

struct

define configuration of the global time master

CanTSyn_GlobalTimeSlaveType

struct

define configuration of the global time slave

CanTSyn_DataIDListElementType

struct

define DataIDList messages

CanTSyn_GlobalTimeDomainType

struct

define CanTSyn GlobalTimeDomain

CanTSyn_InitStatusType

enum

define CanTSyn module initiation status

CanTSyn_MasterStatusType

enum

define CanTSyn master operation status

CanTSyn_SlaveStatusType

enum

define CanTSyn Slave operation Status

配置函数 Configuration function

CanTSyn_MainFunction

void CanTSyn_MainFunction(void)

This function needs to be called periodically to perform the AUTOSAR CanTSyn activities.

Sync/Async

Asynchronous

Reentrancy

Reentrant (Reentrant for different partitions. Non reentrant for the same partition.)

Return type

void

依赖的服务 Applicable Services

强制接口 Compulsory interface

API Function

Header File

Description

StbM_GetCurrentTime

StbM.h

Returns a time value(Local Time Base derived from Global Time Base) in standard format.

StbM_GetTimeBaseUpdateCounter

StbM.h

Get the time base update counter value, that indicates a Time Base update to the Timesync Modules.

StbM_GetTimeBaseStatus

StbM.h

Returns detailed status information for a Synchronized(or Pure Local) Time Base.

StbM_GetCurrentVirtualLocalTime

StbM.h

Returns the Virtual Local Time of the referenced Time Base..

CanIf_Transmit

CanIf.h

Requests transmission of a PDU.

可选接口 Optional Interface

API Function

Header File

Description

Det_ReportError

Det.h

Service to report development errors.

Crc_CalculateCRC8H2F

Crc.h

Calculate CRC value.

GetApplicationID

Os.h

Get application ID.

配置接口 Configuration Interfaces

None

配置 Configuration

1.各CanTSynGlobalTimeDomain的CanTSynGlobalTimeDomainId是唯一的,不能出现相同。

1.CanTSynGlobalTimeDomainId for each CanTSynGlobalTimeDomain must be unique.

2.如果使能了CRC校验,那么相同CanTSynGlobalTimeDomainId的Master和Slave的CanTSynGlobalTimeSyncDataIDList、CanTSynGlobalTimeFupDataIDListElement配置项必须相同,否则校验不过。

2.If CRC checksum is enabled, CanTSynGlobalTimeSyncDataIDList and CanTSynGlobalTimeFupDataIDListElement configuration items must be identical for the Master and Slave within the same CanTSynGlobalTimeDomainId; otherwise, the check will fail.

CanTSyn_1

配置主函数周期。

Configure the main function period.

Master配置

Master Configuration

BSW部分

BSW Part

导入萃取文件前

Before Importing the Extraction File

注意:萃取文件更新步骤参考下文SWC部分

Note: For the steps to update the extraction file, refer to the SWC part below.

CanTSyn_2

右键新增一个时间域。

Right-click to add a new time domain.

CanTSyn_3

1.配置CanTSynGlobalTimeDomainId时间域的ID。(主从时间域ID必须一致才可进行同步,用于划分不同的同步局域网)

1.Configure the time domain ID CanTSynGlobalTimeDomainId. (The Master and Slave time domain IDs must be identical for synchronization. This is used to create separate synchronization networks.)

2.关联时间基。

2.Associate the time base.

3.关联CAN控制器。

3.Associate the CAN controller.

CanTSyn_3

1.设置CanTSynCyclicMsgResumeTime时间,指从立即发送恢复到循环发送所需要的时间。

1.Set CanTSynCyclicMsgResumeTime, which specifies the time required to transition from immediate transmission back to cyclic transmission.

2.设置CanTSynGlobalTimeDebounceTime,指SYNC报文和FU报文之间的时间间隔。

2.Set CanTSynGlobalTimeDebounceTime, which specifies the time interval between the SYNC and FUP messages.

3.CanTSynGlobalTimeTxCrcSecured可选择支持或不支持CRC校验。(CRC校验会校验报文中的sequenceID,DomainID等信息)此处选择不支持。注意:若选择支持,则需要配置CanTSynGlobalTimeFupDataIDList和CanTSynGlobalTimeSyncDataIDList。

3.Set CanTSynGlobalTimeTxCrcSecured to either enable or disable CRC checksum. (CRC checksum checks information in the message such as the sequence ID and Domain ID). Here, it is set to disabled. Note: If enabled, CanTSynGlobalTimeFupDataIDList and CanTSynGlobalTimeSyncDataIDList must be configured.

4.设置同步报文的发送周期CanTSynGlobalTimeTxPeriod。

4.Set the transmission period for synchronization messages CanTSynGlobalTimeTxPeriod.

导入萃取文件后

After Importing the Extraction File

完成更新萃取文件后,按照以下步骤生成RTE

After updating the extraction file, follow the steps below to generate the RTE.

CanTSyn_4

首先打开ECUC模块,在上图位置添加之前在SWC新增的应用组件。

First, open the ECUC module and add the application component previously created in the SWC, as shown in the figure above.

CanTSyn_5

右键ECUC模块,同步一下模块。

Right-click the ECUC module and synchronize the module.

CanTSyn_6

点金同步RTE/OS。

Click to synchronize RTE/OS.

CanTSyn_7
CanTSyn_8

生成RTE文件和APP文件。

Generate the RTE and APP files.

SWC部分

SWC Part

作为主时钟,需要将自己的本地时间基信息发送到总线上,在这之前需要设置主时钟关联的时间基。因此需要走SWC,通过StbM的服务组件设置时间基。

As a master clock, it needs to send its local time base information to the bus. Before doing so, the time base associated with the master clock must be set. Therefore, this is done through SWC by setting the time base via the StbM service component.

CanTSyn_9

如上图所示,在Application Component处右键新增一个应用组件。

As shown in the figure above, right-click on Application Component to add a new application component.

CanTSyn_10

对该APP组件进行命名。

Name this APP component.

CanTSyn_11

右键新增一个客户端Port。

Right-click to add a new client Port.

CanTSyn_12

选中CanTSyn关联的时间基。

Select the time base associated with CanTSyn.

CanTSyn_13

点击上图所示Runnable Entity List。

Click Runnable Entity List, as shown in the figure above.

CanTSyn_14

New一个Init Runnable。

Create a new Init Runnable.

CanTSyn_15
CanTSyn_16

选择Access Points中的SetGlobalTime。

Select SetGlobalTime from the Access Points.

CanTSyn_17
CanTSyn_18

如上图所示开启图形界面。

Open the graphical interface, as shown in the figure above.

CanTSyn_19

双击ECU_ComPosition。

Double-click ECU_Composition.

CanTSyn_20

实例化TSyn_App。

Instantiate TSyn_App.

CanTSyn_21

再次右键ECU_ComPosition打开图形化界面。

Right-click ECU_Composition again to open the graphical interface.

CanTSyn_22

在空白处右键选择连接模式。

Right-click in the empty area and select the connection mode.

CanTSyn_23

将StbM组件和TSynApp组件连接。

Connect the StbM component and the TSynApp component.

CanTSyn_24

双击SysTem进入映射界面。

Double-click System to enter the mapping interface.

CanTSyn_25

将ECU_Composition左键拖入ECU下,完成映射。

Drag ECU_Composition with the left mouse button under the ECU to complete the mapping.

CanTSyn_26

点击上方箭头处进行校验。在下方箭头处查看是否有报错。若无报错,则可按照下图进行萃取。

Click the arrow at the top to perform verification. Check the area indicated by the arrow below for any errors. If there are no errors, proceed with extraction as shown in the figure below.

CanTSyn_27

如上图所示,在ECU处右键创建萃取文件。

As shown in the figure above, right-click the ECU to create the extraction file.

CanTSyn_28

替换到原有工程下的萃取文件。

Replace the existing extraction file in the project directory.

Slave配置

Slave Configuration

CanTSyn_29

CanTSynGlobalTimeDomain配置与Master一致。区别在于需要设置的是CanTSynGlobalTimeSlave。

The CanTSynGlobalTimeDomain configuration is the same as that of the Master. The difference is that CanTSynGlobalTimeSlave needs to be configured.

CanTSyn_30

1.设置FU报文的接收超时。指在这个时间内必须收到FU报文,否则丢弃。

1.Set the reception timeout for FUP messages. This means that the FUP message must be received within this time; otherwise, it is discarded.

2.设置CanTSynGlobalTimeSequenceCounterJumpWidth,指报文中的序列号SequenceCounter允许跳变的最大值,即可接受的最大丢帧数。

2.Set CanTSynGlobalTimeSequenceCounterJumpWidth, which specifies the maximum allowed jump for SequenceCounter in the message, i.e., the maximum number of acceptable lost frames.

3.由于不设置CRC校验,因此CanTSynRxCrcValidated需要设置为CRC_IGNORED。

3.Since CRC checksum is not being used, CanTSynRxCrcValidated must be set to CRC_IGNORED.