请告诉我问题是什么,数组的CRC在分部分计数时不收敛,代码如下
uint8_t arr[] =
{
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB,
0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC,
0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD
};
uint32_t crc_check_sum = 0;
crc_check_sum = CRC_Calc(arr, sizeof(arr), DEFAULT_INIT_VALUE_ENABLE);
printf("________CRC_Calc: 0x%x\r\n", crc_check_sum);
uint8_t arr_1[] = {0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA};
uint8_t arr_2[] = {0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB, 0xBB};
uint8_t arr_3[] = {0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC, 0xCC};
uint8_t arr_4[] = {0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD, 0xDD};
uint32_t div_crc = 0;
uint32_t temp_crc = 0;
div_crc = CRC_TEST_Calc(arr_1, sizeof(arr_1), 0xFFFFFFFFU);
temp_crc = div_crc;
div_crc = CRC_TEST_Calc(arr_2, sizeof(arr_2), temp_crc);
temp_crc = div_crc;
div_crc = CRC_TEST_Calc(arr_3, sizeof(arr_3), temp_crc);
temp_crc = div_crc;
div_crc = CRC_TEST_Calc(arr_4, sizeof(arr_4), temp_crc);
temp_crc = div_crc;
printf("temp_crc: 0x%x\r\n", temp_crc);
temp_crc = ~div_crc;
printf("~temp_crc: 0x%x\r\n", temp_crc);
功能实现:
void CRC_TEST_Init(uint32_t initValue)
{
crc_hcrc.Instance = CRC;
__HAL_RCC_CRC_CLK_ENABLE();
crc_hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_ENABLE;
crc_hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_DISABLE;
crc_hcrc.Init.InitValue = initValue;
crc_hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_BYTE;
crc_hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_ENABLE;
crc_hcrc.Init.CRCLength = CRC_POLYLENGTH_32B;
crc_hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
if (HAL_CRC_Init(&crc_hcrc) != HAL_OK)
{
Error_Handler();
}
}
uint32_t CRC_TEST_Calc(uint8_t * data, uint32_t size, uint32_t initValue)
{
CRC_TEST_Init(initValue);
CRPT_CksumStart();
uint32_t res = HAL_CRC_Calculate(&crc_hcrc, (uint32_t *)data, size);
return res;
}
uint32_t CRC_Calc(uint8_t * data, uint32_t size, uint32_t initValue)
{
CRC_TEST_Init(initValue);
CRPT_CksumStart();
uint32_t res = HAL_CRC_Calculate(&crc_hcrc, (uint32_t *)data, size);
return ~res;
}
首先,在初始化计算时给你不同的初始值。在第一种情况下
DEFAULT_INIT_VALUE_ENABLE
。在第二个 -0xFFFFFFFFU
。该宏
DEFAULT_INIT_VALUE_ENABLE
定义在stm32f7xx_hal_crc.h中(你可能有不同的系列,但这并不改变本质)如下:其次,在计算数组 1、2 和 3 的 crc 时禁用输出位反转。仅对阵列 4 启用它。