| 123456789101112131415161718192021222324252627282930313233 |
- #!/usr/bin/env python3
- """Independent CRC-16/CCITT-FALSE using a frozen nibble table.
- This table is not generated from tools/generate_project.py or
- prod2u_simple_protocol.py. Used to cross-check golden vectors.
- """
- from __future__ import annotations
- # High-nibble table for poly 0x1021, init applied by the caller loop.
- _NIBBLE = (
- 0x0000, 0x1021, 0x2042, 0x3063, 0x4084, 0x50A5, 0x60C6, 0x70E7,
- 0x8108, 0x9129, 0xA14A, 0xB16B, 0xC18C, 0xD1AD, 0xE1CE, 0xF1EF,
- )
- def crc16_ccitt_false_nibble(data: bytes | bytearray) -> int:
- crc = 0xFFFF
- for value in data:
- crc ^= (value & 0xFF) << 8
- crc = ((crc << 4) & 0xFFFF) ^ _NIBBLE[(crc >> 12) & 0x0F]
- crc = ((crc << 4) & 0xFFFF) ^ _NIBBLE[(crc >> 12) & 0x0F]
- return crc
- def frame_hex(frame_type: int, payload: bytes) -> str:
- body = bytes((frame_type, len(payload))) + payload
- crc = crc16_ccitt_false_nibble(body)
- return (b"\xA5\x5A" + body + crc.to_bytes(2, "little")).hex().upper()
- SET_ALL_6400_ALL_ZERO = "A55A0120" + ("00" * 32) + "0D8C"
- GET_TEMPERATURE = "A55A02006D7B"
|