CRC (循环冗余校验)
简介
CRC模块用于CRC的计算与控制,主要用来检测或校验传输错误或存储完整性,在数字电路中根据循环移位或异或完成。
基于CRC的技术常用于验证数据传输或存储的完整性,根据功能安全标准的规定,这些技术提供了验证FLASH完整性的方法,CRC计算单元有助于在运行期间计算软件结果,并将该结果与设定校验值进行比较;
功能概述
支持任意字节的CRC计算和校验;
支持CRC-8、CRC-16、CRC-32校验;
可编程初始值配置;
支持对输入数据字节交换、大小端转换、字节内反转;
支持CRC计算值按照寄存器配置是否反转,是否按照异或处理;
CRC采用单通道串行计算和多项式软件配置;
输入数据处理
输入数据字节交换、字节内反转, 针对两种配置有四种输入方式,分别为直接输入、字节反转、字节内bit反转、字节反转后再字节内bit反转
![]()
输出数据处理
输出数据结果异或、输出按位反转,针对输出主要有四种模式,不做处理输出、异或输出、按位反转输出、按位反转后异或输出
![]()
配置流程说明
初始化配置仅配置模式,仅配置初始化参数,初始化配置完成后对模块进行使能 CRC_enableModule ,然后直接对输入数据寄存器进行写入,CRC模块会自动进行CRC计算。
CRC检验输入支持两种输入方式和结果读取
FIFO数据寄存器软件输入:
CRC模块支持FIFO数据寄存器软件输入,用户可以通过软件往FIFO寄存器中写入数据,CRC模块会自动进行CRC计算。 根据CRC计算模式,CRC提供三个输入数据函数,用户可以根据自己的需要选择使用。
CRC_calculate32():CRC32模式下计算CRC_calculate16():CRC16模式下计算CRC_calculate8():CRC8模式下计算
DMA通路输入:
CRC模块支持DMA通路输入,用户可以通过DMA通路将数据写入CRC模块,CRC模块会自动进行CRC计算; DMA输入模式,在初始化配置后不需要单独配置DMA使能功能,CRC模块没有对应的功能使能,只需要使用DMA搬运将需要运算的参数进行输入即可。
CRC结果读取
CRC数据运算结果只有一个,用户可以通过
CRC_getResult()读取CRC计算结果。
应用示例
CRC-32/MPEG-2计算示例: CRC/CRC_calculate 。
API Reference
Header File
Functions
-
void CRC_enableModule(void)
Enable CRC module.
-
void CRC_disableModule(void)
Disable CRC module.
-
void CRC_config(CRC_ConfigParams *config)
Configure CRC module.
- 参数:
config – pointer to CRC configuration structure
-
DRV_Status CRC_calculate32(uint32_t *pInputData, uint32_t crcNum, uint32_t *pCrcRes, uint32_t timeout)
CRC calculate with poll mode with 32bit width.
- 参数:
pInputData – pointer to data that is ready to be calculated
crcNum – the number of data in bytes
pCrcRes – pointer to result
timeout – timeout in cycle
- 返回:
DRV_Status
DRV_ERROR: CRC configuration error
DRV_TIMEOUT: CRC calculate timeout
DRV_OK: success
-
DRV_Status CRC_calculate16(uint16_t *pInputData, uint32_t crcNum, uint16_t *pCrcRes, uint32_t timeout)
Calculates the CRC-16 checksum of a given input data.
备注
This function assumes that the CRC module is already initialized and configured.
- 参数:
pInputData – Pointer to the input data.
crcNum – Number of bytes in the input data.
pCrcRes – Pointer to store the calculated CRC-16 checksum.
timeout – Timeout in cycles.
- 返回:
DRV_Status DRV_OK if the calculation is successful, DRV_ERROR if the CRC kernel is not selected, and DRV_TIMEOUT if the calculation times out.
-
DRV_Status CRC_calculate8(uint8_t *pInputData, uint32_t crcNum, uint8_t *pCrcRes, uint32_t timeout)
Calculates the CRC-8 checksum of a given input data.
备注
This function assumes that the CRC module is already initialized and configured.
- 参数:
pInputData – Pointer to the input data.
crcNum – Number of bytes in the input data.
pCrcRes – Pointer to store the calculated CRC-8 checksum.
timeout – Timeout in cycles.
- 返回:
DRV_Status DRV_OK if the calculation is successful, DRV_ERROR if the CRC kernel is not selected, and DRV_TIMEOUT if the calculation times out.
-
void CRC_setDataLength(uint32_t length)
Set input data number to be calculated.
- 参数:
length – the data number in 8bits/16bits/32bits depends on CRC mode selection
-
uint32_t CRC_getDataLength(void)
Get current input data that is not calculated.
- 返回:
uint32_t the data number in 8bits/16bits/32bits depends on CRC mode selection
-
uint32_t CRC_getResult(void)
Get calculated results.
- 返回:
uint32_t the calculated results
-
void CRC_setCheckValue(uint32_t value)
Set the check value for CRC calculation.
This function sets the check value that will be used for CRC calculation. The check value is compared with the calculated CRC result to determine if the calculated result matches the expected value.
- 参数:
value – The check value to be set.
-
bool CRC_isFIFOOverflow(void)
Determine if FIFO is overflow.
- 返回:
true FIFO is overflow
- 返回:
false FIFO is not overflow
-
void CRC_enableInterrupt(uint32_t ITmasks)
Enable CRC interrupt.
- 参数:
ITmasks – CRC interrupt status masks
-
void CRC_disableInterrupt(uint32_t ITmasks)
Disable CRC interrupt.
- 参数:
ITmasks – CRC interrupt status masks
-
void CRC_clearInterrupt(uint32_t ITmasks)
Clear CRC interrupt.
- 参数:
ITmasks – CRC interrupt masks
-
void CRC_forceInterrupt(uint32_t ITmasks)
force CRC interrupt
- 参数:
ITmasks – CRC interrupt masks
-
uint32_t CRC_getInterruptStatusRawAll(void)
Get raw CRC interrupt status.
-
uint32_t CRC_getInterruptStatusAll(void)
Get CRC interrupt status.
-
void CRC_setInterruptMask(uint32_t ITmasks)
Mask CRC interrupt status.
- 参数:
ITmasks – CRC interrupt status masks
-
void CRC_clearInterruptMask(uint32_t ITmasks)
Unmask CRC interrupt status.
- 参数:
ITmasks – CRC interrupt status masks
Structures
Macros
-
CRC_INT_BUS_ERR
Bus error Interrupt.
-
CRC_INT_LENGTH_ERR
length error Interrupt
-
CRC_INT_CHECK_ERR
crc check result error Interrupt
-
CRC_FIFO_TRHD_MAX