Skip to content

Instantly share code, notes, and snippets.

@artizirk
Last active December 4, 2020 10:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save artizirk/7aa194192ff7bdff85fa1a890ee8655d to your computer and use it in GitHub Desktop.
Save artizirk/7aa194192ff7bdff85fa1a890ee8655d to your computer and use it in GitHub Desktop.
def _slow_crc(data, seed=0xFFFF_FFFF, polynomial=0x04C11DB7):
"""
STM32 CRC that is actually CRC-32/MPEG2 but input data is read as litle-endian (not big-endian like MPEG)
"""
crc = seed
# Pad data if needed
pad_len = len(data) % 4
if pad_len > 0:
words = array.array('I', data[:-pad_len])
words.frombytes(data[-pad_len:] + b'\xff' * (4 - pad_len))
else:
words = array.array('I', data)
# CRC
for word in words:
crc ^= word
for i in range(32):
if crc & 0x80000000:
crc = (crc << 1) ^ polynomial
else:
crc = (crc << 1)
crc &= 0xFFFF_FFFF
return crc
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment