2018-07-03 01:11 AM
Helo friends,
I try calculate CRC8 in python and compare with CRC in STM And I have a problem.
My python CRC is return wrong value.
Python script:
def AddToCRC(b, crc):
b2 = b
for i in xrange(8):
odd = ((b2^crc) & 1) == 1
crc >>= 1
b2 >>= 1
if (odd):
crc ^= 0x8C
return crc�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
And I call this function here:
TestValue = [0, 0xAB, 0xFF]
check = 0
for i in TestValue:
check = AddToCRC(i, check)
print(hex(check))�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
And I get CRC8 in STM32F: 0x8B
And CRC from python script: 0xF8
Any idea, how to calculate CRC8 in python for STM32?
#crc8 #crc2018-07-04 12:26 AM
I modificated the function, and It works.
uint32_t CRC__Calculate8(uint8_t* Buffer, uint32_t BufferSize, const uint8_t reset, uint8_t *returnCRC)
{
uint32_t index;
/* Reset CRC data register if necessary */
if (reset) {
/* Reset generator */
LL_CRC_ResetCRCCalculationUnit(CRC);
}
/* Compute the CRC of Data Buffer array*/
for (index = 0; index < (BufferSize / 4); index++)
{
uint32_t d32 = (uint32_t)((Buffer[4 * index] << 24) | (Buffer[4 * index + 1] << 16) | (Buffer[4 * index + 2] << 8) | Buffer[4 * index + 3]);
LL_CRC_FeedData32(CRC, d32);
}
/* Last bytes specific handling */
if ((BufferSize % 4) != 0)
{
if (BufferSize % 4 == 1)
{
uint8_t d8 = Buffer[4 * index];
LL_CRC_FeedData8(CRC, d8);
}
if (BufferSize % 4 == 2)
{
uint16_t d16 = (uint16_t)((Buffer[4 * index ]<<8) | Buffer[4 * index + 1]);
LL_CRC_FeedData16(CRC, d16);
}
if (BufferSize % 4 == 3)
{
uint16_t d16 = (uint16_t)((Buffer[4 * index]<<8) | Buffer[4 * index + 1]);
uint8_t d8 = Buffer[4 * index + 2];
LL_CRC_FeedData16(CRC, d16);
LL_CRC_FeedData8(CRC, d8);
}
}
/* Return computed CRC value */
*returnCRC = (LL_CRC_ReadData8(CRC));
return ERR_CRC_OK;
}�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?�?
And call function:
uint8_t Test[] = {0x00, 0xAB, 0xFF, 0xFE, 0x01};
CRC__Calculate8(Test, 5, 1, &returnCRC);�?�?�?�?
Now It return: 0xF4
But if I want tocalc the CRC of 32bit value? Would I replace LL_CRC_ReadData8 with LL_CRC_ReadData32, and modife init as:
LL_CRC_SetPolynomialCoef(CRC, polynomial_left);
LL_CRC_SetPolynomialSize(CRC, LL_CRC_POLYLENGTH_32B);
LL_CRC_SetInitialData(CRC, 0);
LL_CRC_SetInputDataReverseMode(CRC, LL_CRC_INDATA_REVERSE_BYTE); // BYTE
LL_CRC_SetOutputDataReverseMode(CRC, LL_CRC_OUTDATA_REVERSE_BIT); // STM32 backassward�?�?�?�?�?
2018-07-04 06:53 AM
>>But if I want to calc the CRC of 32bit value?
You're going to have to be a lot more precise in your description.
The code supplied will provide an 8-bit CRC across a 4-byte, 32-bit word as provided. It should work consistently for any size buffer.
For a 32-bit polynomial CRC you're going to have to consider multiple factors related to how it shifts and how the data is presented to the feedback terms.
https://community.st.com/0D50X00009XkgXtSAJ