I have the implementation for calculating CRC16 with function and I want to do it using HAL_CRC_Calculate but cant get the same results.
My function is:
uint16_t CalcCRC(const uint8_t* const buffer, const uint8_t u8length)
{
uint32_t temp;
uint32_t temp2;
uint32_t flag;
temp = 0xFFFF;
for (uint8_t i = 0; i < u8length; i++)
{
temp = temp ^ buffer[i];
for (uint8_t j = 1; j <= 8; j++)
{
flag = temp & 0x0001;
temp >>=1;
if (flag)
temp ^= 0xA001;
}
}
// Reverse byte order.
temp2 = temp >> 8;
temp = (temp << 8) | temp2;
temp &= 0xFFFF;
return (uint16_t)temp;
}
And HAL initialization:
hcrc.Instance = CRC;
hcrc.Init.DefaultPolynomialUse = DEFAULT_POLYNOMIAL_DISABLE;
hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_DISABLE;
hcrc.Init.GeneratingPolynomial = 0xA001;
hcrc.Init.CRCLength = CRC_POLYLENGTH_16B;
hcrc.Init.InitValue = 0xffff;
hcrc.Init.InputDataInversionMode = CRC_INPUTDATA_INVERSION_BYTE;
hcrc.Init.OutputDataInversionMode = CRC_OUTPUTDATA_INVERSION_ENABLE;
hcrc.InputDataFormat = CRC_INPUTDATA_FORMAT_BYTES;
when I use it like this i get different results.
uint16_t u16crc_1 = HAL_CRC_Calculate(&hcrc, (uint32_t*)struct->buffer, struct->bufferSize);
uint16_t u16crc = CalcCRC(struct->buffer, struct->bufferSize);
Minimal example:
uint8_t buffer[6] = {25,15,0,7,0,1};
uint16_t u16crc_hal = HAL_CRC_Calculate(&hcrc, (uint32_t*)buffer, 6);
uint16_t u16crc = CalcCRC(buffer, 6);
And then I get:
u16crc_hal = 3094
u16crc = 9746
hcrc.Init.DefaultInitValueUse = DEFAULT_INIT_VALUE_ENABLEHAL_CRC_Calculatethat there are 6uint32_ts wherebufferpoints. You don't have that. You have 6uint8_ts. Try:_Alignas(uint32_t) uint8_t buffer[2 * sizeof(uint32_t)] = {25,15,0,7,0,1};HAL_CRC_Calculate(&hcrc, (uint32_t*)buffer, 2);