From 9ba16e71e1dd35d33714f352fafe40f2cd37c749 Mon Sep 17 00:00:00 2001 From: BlackMark Date: Sat, 11 Apr 2020 10:34:40 +0200 Subject: [PATCH] Fix checksum discarding high-order byte of size field --- stk500v2/main.cpp | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/stk500v2/main.cpp b/stk500v2/main.cpp index c40c950..4832936 100644 --- a/stk500v2/main.cpp +++ b/stk500v2/main.cpp @@ -31,11 +31,10 @@ static inline bool receiveByte(uint8_t &data, uint16_t timeout = 1000) static inline uint8_t calcChecksum(const Message &msg) { uint8_t checksum = msg.start; - checksum ^= msg.number; - checksum ^= msg.size; - checksum ^= msg.token; - for (uint8_t i = 0; i < msg.size; ++i) - checksum ^= msg.body[i]; + + for (uint16_t i = 1; i < 5 + msg.size; ++i) { + checksum ^= *(reinterpret_cast(&msg) + i); + } return checksum; }