Skip to main content
Pilous Droip
Senior
July 3, 2018
Question

Python and CRC8 verification

  • July 3, 2018
  • 2 replies
  • 2941 views
Posted on July 03, 2018 at 10:11

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 #crc
This topic has been closed for replies.

2 replies

Tesla DeLorean
Guru
July 3, 2018
Posted on July 03, 2018 at 11:34

The 0xF8 value looks reasonable, show how you are computing on the STM32

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Tesla DeLorean
Guru
July 3, 2018
Posted on July 03, 2018 at 11:39

These might be computationally more efficient

def AddToCRC(b, crc):

     crc ^= b

     for i in xrange(8):

          odd = (crc & 1) == 1

          crc >>= 1

          if (odd):

               crc ^= 0x8C

     return crc

def AddToCRC(b, crc):

     crc ^= b

     for i in xrange(8):

          if ((crc & 1) == 1):

               crc = (crc >> 1) ^ 0x8C

          else

               crc >>= 1

     return crc
Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Pilous Droip
Senior
July 3, 2018
Posted on July 03, 2018 at 13:41

Okay. I used your python modification, and the result is the same. But I think that problem is with polynomial:

STM use: 0x04C11DB7

Is It Posible?

Tesla DeLorean
Guru
July 3, 2018
Posted on July 03, 2018 at 14:29

You don't state which STM32 you're using.

You don't show how you initialize the CRC peripheral and the polynomial selected.

Initialization value will also be important.

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..
Pilous Droip
Senior
July 3, 2018
Posted on July 03, 2018 at 14:42

Now I try only enable pheripherals:

LL_AHB1_GRP1_EnableClock(LL_AHB1_GRP1_PERIPH_CRC);�?

And polynomial is used default. I try changed it, but it was without changes.

So I enable clock for CRC and call my test function:

uint8_t TestDATA[] = {0, 0xAB, 0xFF};
uint8_t returnCRC = 0;
CRC__Calculate8(TestDATA, 3, 1, &returnCRC);�?�?�?