From bdc0ec57c50103aaa956a1d4b0250f2c33658b9d Mon Sep 17 00:00:00 2001 From: Robin Krens Date: Sat, 10 Feb 2024 12:14:52 +0100 Subject: [PATCH] flasher.py: format data packet --- flasher.py | 45 +++++++++++++++++++++++++++++++++------------ 1 file changed, 33 insertions(+), 12 deletions(-) diff --git a/flasher.py b/flasher.py index 7a1463c..1ffe9ae 100644 --- a/flasher.py +++ b/flasher.py @@ -9,6 +9,10 @@ BAU_CMD = 0x34 SIG_CMD = 0x3A ARE_CMD = 0x3B +LOW_PULSE = 0x00 +GENERIC_CODE = 0x55 +BOOT_CODE = 0xC3 + TESTID = [ "0xF0", "0xF1", "0xF2", "0xF3", "0xE4", "0xE5", "0xE6", "0xE7", @@ -16,38 +20,55 @@ TESTID = [ "0xCC", "0xCD", "0xCE", "0xCF" ] +def calc_sum(data): + data_len = len(data) + lnh = data_len + 1 & 0xFF00 + lnl = data_len + 1 & 0x00FF + res = lnh + lnl + for i in range(data_len): + if isinstance(data[i], str): + res += int(data[i], 16) + else: + res += ord(data[i]) + res = ~(res) & 0xFF # two's complement + return (lnh, lnl, res) + + +# format of data packet is [SOD|LNH|LNL|COM|byte_data|SUM|ETX] def format_command(cmd, data): - COM_LEN = len(data) SOD = 0x01 COM = cmd - LNH = COM_LEN + 1 & 0xFF00 - LNL = COM_LEN + 1 & 0x00FF - calcsum = LNH + LNL if isinstance(data, str): byte_data = bytes(data.encode('utf-8')) else: byte_data = bytes([int(x, 16) for x in data]) - for i in range(COM_LEN): - if isinstance(data[i], str): - calcsum += int(data[i], 16) - else: - calcsum += ord(data[i]) - SUM = ~(calcsum) & 0xFF # two's complement + LNH, LNL, SUM = calc_sum(data) ETX = 0x03 fmt_header = '= 1024): + raise Exception(f'Data packet too large, data length is {DATA_LEN} (>1024)') + LNH, LNL, SUM = calc_sum(data) + DAT = bytes([int(x, 16) for x in data]) + RES = 0x13 ETX = 0x03 + fmt_header = '