CRC – trouble understanding bit reversal
I’ve trawled through several CRC-related threads without finding an explanation of this: How to properly set up bit reversal.
Normally, I use CRC-16 XMODEM, which uses the 0x1021 polynomial and no bit reversal. This works perfectly fine when I use this code:
void xmodemCalcCRC (uint8_t Data, uint16_t *CRCVal)
{
CRC->POL = 0x1021; // CRC polynomial
CRC->INIT = *CRCVal; // Previous CRC result
CRC->CR = CRC_CR_POLYSIZE_0; // 16-bit polynomial
*(__IO uint8_t*)(&CRC->DR) = Data; // Feed an 8-bit value into the algorithm
*CRCVal = CRC->DR; // Read the result
}The reason I set the polynomial, the initial value and configuration every time is to accommodate different CRC methods in the same application.
Now, in a current project (using an STM32L433 microcontroller) I need to use CRC-16 Kermit as well. According to crccalc.com, Kermit CRC uses bit reversal on input and output. This is where my trouble begins: When bit reversing, I don’t get the correct result.
The only thing I change in my code is the bit reversal configuration bits:
void kermitCalcCRC (uint8_t Data, uint16_t *CRCVal)
{
CRC->POL = 0x1021;
CRC->INIT = *CRCVal;
CRC->CR = CRC_CR_REV_OUT | CRC_CR_REV_IN_0 | CRC_CR_POLYSIZE_0;
*(__IO uint8_t*)(&CRC->DR) = Data;
*CRCVal = CRC->DR;
}As I feed the CRC unit one byte at a time, I have set the REV_IN[0] bit, as this should do the input bit reversal by byte. But the result I get is incorrect. As an example, the text string “00 6 0 0 0 3 0 “ gives me the CRC result 0xF3BF, where I expected 0xAFEA.
I have tried adding the CRC RESET bit, and also shuffled the order of operations. This has made no difference whatsoever.
I must be missing something, but what?
As a fallback, I have code for doing CRC-16 Kermit calculations without using the CRC unit. But I would really like to get this to work, as I believe it is faster as well as flash space saving.
