Last active
December 4, 2020 10:17
-
-
Save artizirk/7aa194192ff7bdff85fa1a890ee8655d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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