summaryrefslogtreecommitdiff
path: root/gm_platform/fw/crctest.py
diff options
context:
space:
mode:
authorjaseg <git@jaseg.net>2020-01-27 21:58:22 +0100
committerjaseg <git@jaseg.net>2020-01-27 21:58:22 +0100
commitf4a6ea896f711f68e898e69e21d74118a44465f2 (patch)
tree48a8b14c2c452333cbc8fb9595d20ddad5c5348f /gm_platform/fw/crctest.py
parent966f104597275b29f41c06c4574d1bbe9ecde401 (diff)
downloadmaster-thesis-f4a6ea896f711f68e898e69e21d74118a44465f2.tar.gz
master-thesis-f4a6ea896f711f68e898e69e21d74118a44465f2.tar.bz2
master-thesis-f4a6ea896f711f68e898e69e21d74118a44465f2.zip
serial basically working
Diffstat (limited to 'gm_platform/fw/crctest.py')
-rw-r--r--gm_platform/fw/crctest.py79
1 files changed, 79 insertions, 0 deletions
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))