cancel
Showing results for 
Search instead for 
Did you mean: 

Unable to understand how the CRC calculation is done in the SPI module

arnold_w
Senior
Posted on March 22, 2016 at 15:11

I am working with the Discovery Development Board and I am trying to understand how the SPI with CRC enabled works. I had a look at Application NoteAN4187 (www.st.com/st-web-ui/static/active/en/resource/technical/document/application_note/DM00068pdf) and on page 7 they have an example that I was able to verify in software:

#define POLYRETURN uint8_t
#define DATATYPE uint8_t
#define MSB_MASK 0x80
#define LOWER 0 /* lower limit */
#define STEP 1 /* step size */
#define CRCUPPER sizeof(DATATYPE)*8 /* CRC software upper limit */
#define CRC_SHIFT 1
POLYRETURN CrcSoftwareFunc(POLYRETURN Initial_Crc, DATATYPE Input_Data, POLYRETURN POLY)
{
uint8_t bindex = 0;
POLYRETURN Crc = 0;
/* Initial XOR operation with the previous Crc value */
Crc = Initial_Crc ^ Input_Data;
/* The CRC algorithm routine */
for (bindex = LOWER; bindex < CRCUPPER; bindex = bindex + STEP)
{
if (Crc & MSB_MASK)
{
Crc = (Crc << CRC_SHIFT) ^ POLY;
}
else
{
Crc = (Crc << CRC_SHIFT);
}
}
return Crc;
}
uint8_t Initial_Crc = 0xFF;
uint8_t Input_Data = 0xC1;
uint8_t POLY = 0xCB;
uint8_t Crc;
Crc = CrcSoftwareFunc(Initial_Crc, Input_Data, POLY);

When I run the code above, I get the CRC 0x4C, exactly as in the example on page 7. However, the SPI module can't use the polynomial above because the Cube specifies the following: * CRC Polynomial CRCPolynomial should respect this format :Xa+Xb+...+Xc. where a,b,c are the powers of the polynomial and shouldbe less than 8.

* This parameter hasautomatically changed after your last modification.

So, I change my polynomial to 0x07 in both the code above and in my SPI module. I then call the function above with Input_Data = 0xC1 and compare the result with what the SPI module sends after I transmit 0xC1 on the MOSI line and the results don't match (0xBA vs 0x49). What am I doing wrong, why don't the CRC:s match?
1 REPLY 1
arnold_w
Senior
Posted on March 22, 2016 at 15:38

I found the solution, the SPI module seems to use Initial_Crc = 0 (not 0xFF).