openpilot/opendbc_repo/opendbc/car/crc.py
github-actions[bot] 1f7233cb98 sunnypilot v0.10.1
version: sunnypilot v0.10.1 (staging-tici)
date: 2025-10-13T01:35:37
master commit: 737a6c4236e843034680c951005b38d15815363f
2025-10-13 01:35:37 +00:00

31 lines
641 B
Python

def _gen_crc8_table(poly: int) -> list[int]:
table = []
for i in range(256):
crc = i
for _ in range(8):
if crc & 0x80:
crc = ((crc << 1) ^ poly) & 0xFF
else:
crc = (crc << 1) & 0xFF
table.append(crc)
return table
def _gen_crc16_table(poly: int) -> list[int]:
table = []
for i in range(256):
crc = i << 8
for _ in range(8):
if crc & 0x8000:
crc = ((crc << 1) ^ poly) & 0xFFFF
else:
crc = (crc << 1) & 0xFFFF
table.append(crc)
return table
CRC8H2F = _gen_crc8_table(0x2F)
CRC8J1850 = _gen_crc8_table(0x1D)
CRC16_XMODEM = _gen_crc16_table(0x1021)