Crc

文档信息(Document Information)

版本历史(Version History)

日期(Date)

作者(Author)

版本(Version)

状态(Status)

说明(Description)

2024/12/5

qinmei.chen

V0.1

发布(Release)

首次发布(First release)

2025/04/04

qinmei.chen

V1.0

发布(Release)

正式发布(Official release)

参考文档(Reference Document)

编号(Number)

分类(Classification)

标题(Title)

版本(Version)

1

Autosar

AUTOSAR_CP_SWS_CRCLibrary.pdf

R23-11

术语与简写(Terms and Abbreviations)

术语(Term)

None

简写(Abbreviation)

简写(Abbreviation)

全称(Full name)

解释(Explanation)

CRC

Cyclic Redundancy Check

循环冗余校验

ALU

Arithmetic Logic Unit

算术逻辑单元

简介(Introduction)

CRC模块主要实现了基于输入数据计算一组校验码,以检查数据在传输过程中是否发生了变化或传输错误。

The CRC module primarily implements the calculation of a set of checksums based on input data to verify whether the data has been altered or corrupted during transmission.

假设D是要被校验的数据,已转为n bit的二进制表示。例如

Let D be a bitwise representation of data with a total number of n bit, i.e.

D = (dn-1, dn-2, dn-3, …, d1, d0),其中d0到dn-1是二进制的位数。

D = (dn-1, dn-2, dn-3, …, d1, d0), where d0 to dn-1 are binary bits.

使用某些规则生成新的二进制序列R,假设k位,r0到rk-1是二进制值0或1。

Generate a new binary sequence R according to specific rules, where each value from r₀ to rₖ₋₁ is a binary digit (0 or 1).

冗余码R被附加到原始数据二进制序列上,成为n+k位数据的二进制表示:

The redundant code R is appended to the original data binary sequence, forming an n+k-bit binary representation of the data:

C=(D,R)=(dn-1,dn-2,dn-3,…,d1,d0,rk-1,…,r2,r1,r0,)。

C = (D, R) = (dn-1, dn-2, dn-3, …, d1, d0, rk-1, …, r2, r1, r0).

将C除以k阶多项式,得到(k-1)阶余数r(x),那么与r(x)对应的二进制码r就是CRC码。

When C is divided by a k-th order polynomial, a (k-1)-th order remainder r(x) is obtained, and the binary code r corresponding to r(x) is the CRC code.

被分割的k阶多项式是生成器。

The k-th order polynomial used for division is the generator.

CRCL模块提供三种算法处理机制:

The CRCL module provides three algorithm processing mechanisms:

  • 查表计算方法:速度快,需要占用较大的ROM。

    Look-up table calculation method: Fast in speed but requires a large amount of ROM.

  • 运行时计算方法:速度慢,占用ROM少。

    Runtime calculation method: Slow in speed but with low ROM usage.​

  • 硬件计算方法:快速,但需要硬件支持。

    Hardware calculation method: Fast but requires hardware support.

功能描述(Functional Description)

特性(Features)

CRC算法简介(Introduction to CRC Algorithms)

下述是几种标准的CRC校验生成多项式:

The following are several standard CRC check generator polynomials:

  • CRC 8bit SAE J1850:

\(G(x)=x^8+x^4+x^3+x^2+1\)

  • CRC 8bit based on 0x2F:

\(G(x)=x^8+x^5+x^3+x^2+x+1\)

  • CRC 16bit CCITT-FALSE:

\(G(x)=x^{16}+x^{12}+x^5+1\)

  • CRC 16bit based on 0x8005:

\(G(x)=x^{16}+x^{15}+x^2+1\)

  • CRC 32bit Ethernet IEEE-802.3:

\(G(x)=x^{32}+x^{26}+x^{23}+x^{22}+x^{16}+x^{12}+x^{11}+x^{10}+x^8+x^7+x^5+x^4+x^2+x+1\)

  • CRC 32bit based on 0xF4ACFB13:

\(G(x)=x^{32}+x^{31}+x^{30}+x^{29}+x^{28}+x^{25}+x^{23}+x^{21}+x^{19}+x^{18}+x^{15}+x^{14}+x^{13}+x^{12}+x^{11}+x^9+x^8+x^4+x+1\)

  • CRC 64bit ECMA:

\[\begin{split}\begin{align} G(x) = &x^{64}+x^{62}+x^{57}+x^{55}+x^{54}+x^{53}+x^{52}+x^{47}+x^{46}+x^{40}+x^{39}+x^{38} \\ &+ x^{37}+x^{35}+x^{33}+x^{32}+x^{31}+x^{29}+x^{27}+x^{24}+x^{23}+x^{22}+x^{21}+x^{19} \\ &+x^{17}+x^{13}+x^{12}+x^{10}+x^9+x^7+x^4+x+1 \end{align}\end{split}\]

CRC校验的原理就是将需要校验的数据与按规则产生的数据进行异或运算,得到的余数即为校验值。进行异或的方式与实际数据传输时,高位先传还是低位先传有关,若异或从数据的高位开始,为顺序异或,若异或从数据的低位开始,则为反序异或,两种异或方式,即使对应同一个生成多项式,计算出来的结果也不相同。

The principle of CRC (Cyclic Redundancy Check) is to perform an ​XOR operation​ between the data to be verified and a ​predefined polynomial, where the resulting ​remainder​ serves as the checksum value. The method of XOR operation depends on the ​data transmission order:

​Forward XOR (顺序异或)​: Processing starts from the ​most significant bit (MSB)​​ of the data.

​Reverse XOR (反序异或)​: Processing starts from the ​least significant bit (LSB)​​ of the data.

Even with the ​same generator polynomial​, these two methods yield ​different results​ due to opposing computational directions

CRC标准参数模型(CRC Standard Parameter Model)

CRC8、CRC16、CRC32、CRC64所要用到的标准参数如下:

The standard parameters to be used for CRC8, CRC16, CRC32, and CRC64 are as follows:

参数名(Parameter Name)

解释(Explanation)

CRC宽度(CRC Width)

CRC计算结果的宽度

The result data width of the CRC calculation

多项式(Polynomial)

用于CRC算法的生成多项式

The generator polynomial which is used for the CRC algorithm

初始值(Initial Value)

CRC算法开始时寄存器初始化的预置值

The start condition for the CRC algorithm

输入数据反转(Input Data Reverse)

定义了在参与CRC计算之前,每个输入字节是否需要进行位反转

Defines whether the bits of each input byte are reflected before being processed

输出数据反转(Output Data Reverse)

定义了CRC计算结果是否需要按位反转

Defines whether the bits of the CRC result are reflected

异或值(XOR Value)

该值将与寄存器中的最终值进行运算,再将异或结果作为返回值

This Value is XORed to the final register value before the value is returned as the official checksum

检查值(Check Value)

这是作为验证CRC算法的一种较弱的方法,当输入ASCII字符串”123456789”时,该值作为校验值

This field is a check value that can be used as a weak validator of implementations of the algorithm. When the input ASCII string is “123456789”, this value serves as the check value

CRC功能实现(CRC Function Implementation)

CRC功能实现分为三种方式:直接计算法、查表法、硬件实现法,分别对应配置项CrcxMode中的取值CRC_RUNTIME、CRC_TABLE和CRC_HARDWARE ,x代表CRC位宽,可为8、8H2F、16、16ARC、32、32P4、64。

The implementation of CRC functions is divided into three methods: direct calculation, look-up table, and hardware implementation, which correspond to the values CRC_RUNTIME, CRC_TABLE, and CRC_HARDWARE in the configuration item CrcxMode respectively. Here, “x” represents the CRC bit width, which can be 8, 8H2F, 16, 16ARC, 32, 32P4, or 64.

集成(Integration)

文件列表(File list)

静态文件(Static files)

文件(File)

描述(Description)

Crc.h

CRC模块头文件,包含了API函数的扩展声明并定义了端口的数据结构。

CRC module header file, which contains extended declarations of API functions and defines the data structure of ports.

Crc.c

CRC模块源文件,包含了API函数的实现。

CRC module source file, which contains the implementation of API functions.

Crc_MemMap.h

CRC的内存映射定义。

Memory mapping definition of CRC.

动态文件(Dynamic file)

文件(File)

描述(Description)

Crc_cfg.h

定义CRC模块预编译时用到的配置参数。

Define the configuration parameters used during the pre-compilation of the CRC module.

错误处理(Error handling)

None

接口描述(Interface Description)

类型定义(Type definition)

None

提供的服务(Provided services)

Crc_GetVersionInfo

void Crc_GetVersionInfo(Std_VersionInfoType *versionInfo)

This service returns the version information of this module.

Sync/Async

TRUE

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[out]

versionInfo

Pointer to where to store the version

Return type

void

Crc_CalculateCRC8

uint8 Crc_CalculateCRC8(const uint8 *Crc_DataPtr, uint32 Crc_Length, uint8 Crc_StartValue8, boolean Crc_IsFirstCall)

This service makes a CRC8 calculation on Crc_Length data bytes, with SAE J1850 parameters.

Sync/Async

TRUE

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[in]

Crc_DataPtr

Pointer to start address of data block to be calculated.

[in]

Crc_Length

Length of data block to be calculated in bytes.

[in]

Crc_StartValue8

Start value when the algorithm starts.

[in]

Crc_IsFirstCall

TRUE: First call in a sequence or individual CRC calculation; start from initial value, ignore Crc_StartValue8. FALSE:Subsequent call in a call sequence; Crc_StartValue8 is interpreted to be the return value of the previous function call.

Return type

uint8

Crc_CalculateCRC8H2F

uint8 Crc_CalculateCRC8H2F(const uint8 *Crc_DataPtr, uint32 Crc_Length, uint8 Crc_StartValue8H2F, boolean Crc_IsFirstCall)

This service makes a CRC8 calculation with the Polynomial 0x2F on Crc_Length.

Sync/Async

TRUE

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[in]

Crc_DataPtr

Pointer to start address of data block to be calculated.

[in]

Crc_Length

Length of data block to be calculated in bytes.

[in]

Crc_StartValue8H2F

Start value when the algorithm starts.

[in]

Crc_IsFirstCall

TRUE: First call in a sequence or individual CRC calculation; start from initial value, ignore Crc_StartValue8H2F. FALSE:Subsequent call in a call sequence; Crc_StartValue8H2F is interpreted to be the return value of the previous function call.

Return type

uint8

Crc_CalculateCRC16

uint16 Crc_CalculateCRC16(const uint8 *Crc_DataPtr, uint32 Crc_Length, uint16 Crc_StartValue16, boolean Crc_IsFirstCall)

This service makes a CRC16 calculation on Crc_Length data bytes.

Sync/Async

TRUE

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[in]

Crc_DataPtr

Pointer to start address of data block to be calculated.

[in]

Crc_Length

Length of data block to be calculated in bytes.

[in]

Crc_StartValue16

Start value when the algorithm starts.

[in]

Crc_IsFirstCall

TRUE: First call in a sequence or individual CRC calculation; start from initial value, ignore Crc_StartValue16. FALSE:Subsequent call in a call sequence; Crc_StartValue16 is interpreted to be the return value of the previous function call.

Return type

uint16

Crc_CalculateCRC16ARC

uint16 Crc_CalculateCRC16ARC(const uint8 *Crc_DataPtr, uint32 Crc_Length, uint16 Crc_StartValue16, boolean Crc_IsFirstCall)

This service makes a CRC16 calculation on Crc_Length data bytes, using the polynomial 0x8005.

Sync/Async

TRUE

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[in]

Crc_DataPtr

Pointer to start address of data block to be calculated.

[in]

Crc_Length

Length of data block to be calculated in bytes.

[in]

Crc_StartValue16

Start value when the algorithm starts.

[in]

Crc_IsFirstCall

TRUE: First call in a sequence or individual CRC calculation; start from initial value, ignore Crc_StartValue16. FALSE:Subsequent call in a call sequence; Crc_StartValue16 is interpreted to be the return value of the previous function call.

Return type

uint16

Crc_CalculateCRC32

uint32 Crc_CalculateCRC32(const uint8 *Crc_DataPtr, uint32 Crc_Length, uint32 Crc_StartValue32, boolean Crc_IsFirstCall)

This service makes a CRC32 calculation on Crc_Length data bytes.

Sync/Async

TRUE

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[in]

Crc_DataPtr

Pointer to start address of data block to be calculated.

[in]

Crc_Length

Length of data block to be calculated in bytes.

[in]

Crc_StartValue32

Start value when the algorithm starts.

[in]

Crc_IsFirstCall

TRUE: First call in a sequence or individual CRC calculation; start from initial value, ignore Crc_StartValue32. FALSE:Subsequent call in a call sequence; Crc_StartValue32 is interpreted to be the return value of the previous function call.

Return type

uint32

Crc_CalculateCRC32P4

uint32 Crc_CalculateCRC32P4(const uint8 *Crc_DataPtr, uint32 Crc_Length, uint32 Crc_StartValue32, boolean Crc_IsFirstCall)

This service makes a CRC32 calculation on Crc_Length data bytes.

Sync/Async

TRUE

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[in]

Crc_DataPtr

Pointer to start address of data block to be calculated.

[in]

Crc_Length

Length of data block to be calculated in bytes.

[in]

Crc_StartValue32

Start value when the algorithm starts.

[in]

Crc_IsFirstCall

TRUE: First call in a sequence or individual CRC calculation; start from initial value, ignore Crc_StartValue32. FALSE:Subsequent call in a call sequence; Crc_StartValue32 is interpreted to be the return value of the previous function call.

Return type

uint32

Crc_CalculateCRC64

uint64 Crc_CalculateCRC64(const uint8 *Crc_DataPtr, uint64 Crc_Length, uint64 Crc_StartValue64, boolean Crc_IsFirstCall)

This service makes a CRC64 calculation on Crc_Length data bytes.

Sync/Async

TRUE

Reentrancy

Non Reentrant

Parameters

Dir

Name

Description

[in]

Crc_DataPtr

Pointer to start address of data block to be calculated.

[in]

Crc_Length

Length of data block to be calculated in bytes.

[in]

Crc_StartValue64

Start value when the algorithm starts.

[in]

Crc_IsFirstCall

TRUE: First call in a sequence or individual CRC calculation; start from initial value, ignore Crc_StartValue64. FALSE:Subsequent call in a call sequence; Crc_StartValue64 is interpreted to be the return value of the previous function call.

Return type

uint64

配置(Configuration)

Crc8Mode

开启Crc8的支持,可选方法包括CRC_TABLE、CRC_RUNTIME和CRC_HARDWARE

Enable support for Crc8, optional methods include CRC_TABLE, CRC_RUNTIME, and CRC_HARDWARE

Crc8H2FMode

开启Crc8H2F的支持,可选方法包括CRC_TABLE、CRC_RUNTIME和CRC_HARDWARE

Enable support for Crc8H2F, optional methods include CRC_TABLE, CRC_RUNTIME, and CRC_HARDWARE

Crc16Mode

开启Crc16的支持,可选方法包括CRC_TABLE、CRC_RUNTIME和CRC_HARDWARE

Enable support for Crc16, optional methods include CRC_TABLE, CRC_RUNTIME, and CRC_HARDWARE

Crc16ARCMode

开启Crc16ARC的支持,可选方法包括CRC_TABLE、CRC_RUNTIME和CRC_HARDWARE

Enable support for Crc16ARC, optional methods include CRC_TABLE, CRC_RUNTIME, and CRC_HARDWARE

Crc32Mode

开启Crc32的支持,可选方法包括CRC_TABLE、CRC_RUNTIME和CRC_HARDWARE

Enable support for Crc32, optional methods include CRC_TABLE, CRC_RUNTIME, and CRC_HARDWARE

Crc32P4Mode

开启Crc32P4的支持,可选方法包括CRC_TABLE、CRC_RUNTIME和CRC_HARDWARE

Enable support for Crc32P4, optional methods include CRC_TABLE, CRC_RUNTIME, and CRC_HARDWARE

Crc64Mode

开启Crc64的支持,可选方法包括CRC_TABLE、CRC_RUNTIME和CRC_HARDWARE

Enable support for Crc64, optional methods include CRC_TABLE, CRC_RUNTIME, and CRC_HARDWARE

CRC容器配置图

CRC容器配置图