2019-09-30 05:04 AM
Hi,
I have (for example) a byte array with the size of 6. Now I want to calculate its crc32 value. The first 4 bytes are no problem - I put them in as word, but what about the last 2 bytes?
Thanks for the Help in advance.
BR
Nico
2019-09-30 05:08 AM
The HW CRC on the part operates on 32-bit at a time. To handle odd bytes you must either use consistent padding bytes or work the smaller amounts in SW.
2019-09-30 07:50 AM
uint32_t CrcCCITTBytes(const uint8_t * data, uint32_t size);
uint32_t CrcCCITTBytes(const uint8_t * data, uint32_t size) {
uint32_t i;
CRC->CR = CRC_CR_RESET;
while(CRC->CR & CRC_CR_RESET); // avoiding the not-reset-fast-enough bug
i = size % 4;
switch(i) {
case 0:
break;
case 1:
CRC->DR = 0xFFFFFFB9;
CRC->DR = 0xAF644900
| (__RBIT(*(uint32_t*)(uintptr_t)&data[0]) >> 24)
;
break;
case 2:
CRC->DR = 0xFFFFB950;
CRC->DR = 0x64490000
| (__RBIT(*(uint32_t*)(uintptr_t)&data[0]) >> 16)
;
break;
case 3:
CRC->DR = 0xFFB9509B;
CRC->DR = 0x49000000
| (__RBIT(*(uint32_t*)(uintptr_t)&data[0]) >> 8)
;
break;
}
for (; i < size; i += 4) {
CRC->DR = __RBIT(*(uint32_t*)(uintptr_t)&data[i]);
}
return __RBIT(CRC->DR) ^ 0xFFFFFFFF;
}
This is to demonstrate how to
> use consistent padding bytes
I started with one, two or three extra 0xFF, calculated CRC from just them, and then added the "data to change CRC to whatever I want" (and I wanted 0xFFFFFFFF) as per Clive's recipe (last post in that thread). That's how those constants above came to be. The rest is input and output bit reversal as per CCITT-32, and the final xor.
JW