From f4a6ea896f711f68e898e69e21d74118a44465f2 Mon Sep 17 00:00:00 2001 From: jaseg Date: Mon, 27 Jan 2020 21:58:22 +0100 Subject: serial basically working --- gm_platform/fw/crctest.py | 79 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 gm_platform/fw/crctest.py (limited to 'gm_platform/fw/crctest.py') diff --git a/gm_platform/fw/crctest.py b/gm_platform/fw/crctest.py new file mode 100644 index 0000000..5c97be9 --- /dev/null +++ b/gm_platform/fw/crctest.py @@ -0,0 +1,79 @@ +custom_crc_table = {} + +def generate_crc32_table(_poly): + + global custom_crc_table + + for i in range(256): + c = i << 24 + + for j in range(8): + c = (c << 1) ^ _poly if (c & 0x80000000) else c << 1 + + custom_crc_table[i] = c & 0xffffffff + + +def crc32_stm(bytes_arr): + + length = len(bytes_arr) + crc = 0xffffffff + + k = 0 + while length >= 4: + + v = ((bytes_arr[k] << 24) & 0xFF000000) | ((bytes_arr[k+1] << 16) & 0xFF0000) | \ + ((bytes_arr[k+2] << 8) & 0xFF00) | (bytes_arr[k+3] & 0xFF) + + crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ v)] + crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ (v >> 8))] + crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ (v >> 16))] + crc = ((crc << 8) & 0xffffffff) ^ custom_crc_table[0xFF & ((crc >> 24) ^ (v >> 24))] + + k += 4 + length -= 4 + + if length > 0: + v = 0 + + for i in range(length): + v |= (bytes_arr[k+i] << 24-i*8) + + if length == 1: + v &= 0xFF000000 + + elif length == 2: + v &= 0xFFFF0000 + + elif length == 3: + v &= 0xFFFFFF00 + + crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v ) )]; + crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v >> 8) )]; + crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v >> 16) )]; + crc = (( crc << 8 ) & 0xffffffff) ^ custom_crc_table[0xFF & ( (crc >> 24) ^ (v >> 24) )]; + + return crc + +poly = 0x04C11DB7 +buf = bytes(reversed([1, 2, 3, 4])) + +generate_crc32_table(poly) +print(hex(crc32_stm(bytearray(buf)))) + +from crccheck import crc +import struct + +def rev_bits_in_word(w): + return sum( ((w>>i)&1) << (31-i) for i in range(32) ) + +import zlib +def crc32stm(inbytes): + crc32 = crc.Crc32.calc(inbytes)^0xffffffff + #crc32 = zlib.crc32(inbytes)^0xffffffff + crc32 = rev_bits_in_word(crc32) + return crc32 + +#data = [0x80,0x40,0xc0,0x20] +data = [0x00, 0, 0, 0x80, 0, 0, 0, 0x80] +print(hex(crc32stm(bytes(data)))) +print(hex(zlib.crc32(bytes([0, 0, 0, 1]))^0xffffffff)) -- cgit