From 49a6744e36c74d2c9ba29159e9b862a16b80a748 Mon Sep 17 00:00:00 2001 From: Josh Stewart Date: Mon, 18 Mar 2019 16:44:29 +1100 Subject: [PATCH] Complete and tested CRC32 calcs for pages --- speeduino/comms.h | 1 + speeduino/src/FastCRC/FastCRC.h | 4 +-- speeduino/src/FastCRC/FastCRCsw.cpp | 9 +++--- speeduino/utils.ino | 44 ++++++++++++++++++++--------- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/speeduino/comms.h b/speeduino/comms.h index 3d4a47db..11011f0d 100644 --- a/speeduino/comms.h +++ b/speeduino/comms.h @@ -51,5 +51,6 @@ void receiveCalibration(byte); void sendToothLog(bool); void testComm(); void commandButtons(); +byte getPageValue(byte, uint16_t); #endif // COMMS_H diff --git a/speeduino/src/FastCRC/FastCRC.h b/speeduino/src/FastCRC/FastCRC.h index 9ec9926f..ef8cff58 100755 --- a/speeduino/src/FastCRC/FastCRC.h +++ b/speeduino/src/FastCRC/FastCRC.h @@ -136,10 +136,10 @@ class FastCRC32 { public: FastCRC32(); - uint32_t crc32(const uint8_t *data, const uint16_t datalen); // Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3 + uint32_t crc32(const uint8_t *data, const uint16_t datalen, bool reflect=true); // Alias CRC-32/ADCCP, PKZIP, Ethernet, 802.3 uint32_t cksum(const uint8_t *data, const uint16_t datalen); // Alias CRC-32/POSIX - uint32_t crc32_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed + uint32_t crc32_upd(const uint8_t *data, uint16_t len, bool reflect=true); // Call for subsequent calculations with previous seed uint32_t cksum_upd(const uint8_t *data, uint16_t len); // Call for subsequent calculations with previous seed #if !CRC_SW uint32_t generic(const uint32_t polyom, const uint32_t seed, const uint32_t flags, const uint8_t *data, const uint16_t datalen); //Not available in non-hw-variant (not T3.x) diff --git a/speeduino/src/FastCRC/FastCRCsw.cpp b/speeduino/src/FastCRC/FastCRCsw.cpp index 1ff29f5f..1478cfaf 100755 --- a/speeduino/src/FastCRC/FastCRCsw.cpp +++ b/speeduino/src/FastCRC/FastCRCsw.cpp @@ -410,7 +410,7 @@ FastCRC32::FastCRC32(){} #define CRC_TABLE_CRC32 crc_table_crc32 #endif -uint32_t FastCRC32::crc32_upd(const uint8_t *data, uint16_t len) +uint32_t FastCRC32::crc32_upd(const uint8_t *data, uint16_t len, bool reflect) { uint32_t crc = seed; @@ -440,17 +440,18 @@ uint32_t FastCRC32::crc32_upd(const uint8_t *data, uint16_t len) crc = (crc >> 8) ^ pgm_read_dword(&CRC_TABLE_CRC32[(crc & 0xff) ^ *data++]); } - crc = ~crc; + if(reflect) { crc = ~crc; } + seed = crc; return crc; } -uint32_t FastCRC32::crc32(const uint8_t *data, const uint16_t datalen) +uint32_t FastCRC32::crc32(const uint8_t *data, const uint16_t datalen, bool reflect) { // poly=0x04c11db7 init=0xffffffff refin=true refout=true xorout=0xffffffff check=0xcbf43926 seed = 0xffffffff; - return crc32_upd(data, datalen); + return crc32_upd(data, datalen, reflect); } /** CKSUM diff --git a/speeduino/utils.ino b/speeduino/utils.ino index cad9d4ce..14a2fef1 100644 --- a/speeduino/utils.ino +++ b/speeduino/utils.ino @@ -69,13 +69,17 @@ uint32_t calculateCRC32(byte pageNo) switch(pageNo) { case veMapPage: + //Confirmed working raw_value = getPageValue(veMapPage, 0); - CRC32_val = CRC32.crc32(&raw_value, 1); + CRC32_val = CRC32.crc32(&raw_value, 1, false); for(uint16_t x=1; x< npage_size[veMapPage]; x++) + //for(uint16_t x=1; x< 288; x++) { raw_value = getPageValue(veMapPage, x); - CRC32_val = CRC32.crc32_upd(&raw_value, 1); + CRC32_val = CRC32.crc32_upd(&raw_value, 1, false); } + //Do a manual reflection of the CRC32 value + CRC32_val = ~CRC32_val; break; case veSetPage: @@ -85,13 +89,16 @@ uint32_t calculateCRC32(byte pageNo) break; case ignMapPage: + //Confirmed working raw_value = getPageValue(ignMapPage, 0); - CRC32_val = CRC32.crc32(&raw_value, 1); - for(uint16_t x=1; x< sizeof(ignitionTable); x++) + CRC32_val = CRC32.crc32(&raw_value, 1, false); + for(uint16_t x=1; x< npage_size[ignMapPage]; x++) { raw_value = getPageValue(ignMapPage, x); - CRC32_val = CRC32.crc32_upd(&raw_value, 1); + CRC32_val = CRC32.crc32_upd(&raw_value, 1, false); } + //Do a manual reflection of the CRC32 value + CRC32_val = ~CRC32_val; break; case ignSetPage: @@ -101,13 +108,16 @@ uint32_t calculateCRC32(byte pageNo) break; case afrMapPage: + //Confirmed working raw_value = getPageValue(afrMapPage, 0); - CRC32_val = CRC32.crc32(&raw_value, 1); - for(uint16_t x=1; x< sizeof(afrTable); x++) + CRC32_val = CRC32.crc32(&raw_value, 1, false); + for(uint16_t x=1; x< npage_size[afrMapPage]; x++) { raw_value = getPageValue(afrMapPage, x); - CRC32_val = CRC32.crc32_upd(&raw_value, 1); + CRC32_val = CRC32.crc32_upd(&raw_value, 1, false); } + //Do a manual reflection of the CRC32 value + CRC32_val = ~CRC32_val; break; case afrSetPage: @@ -117,23 +127,29 @@ uint32_t calculateCRC32(byte pageNo) break; case boostvvtPage: - raw_value = getPageValue(afrMapPage, 0); - CRC32_val = CRC32.crc32(&raw_value, 1); + //Confirmed working + raw_value = getPageValue(boostvvtPage, 0); + CRC32_val = CRC32.crc32(&raw_value, 1, false); for(uint16_t x=1; x< npage_size[boostvvtPage]; x++) { - raw_value = getPageValue(afrMapPage, x); - CRC32_val = CRC32.crc32_upd(&raw_value, 1); + raw_value = getPageValue(boostvvtPage, x); + CRC32_val = CRC32.crc32_upd(&raw_value, 1, false); } + //Do a manual reflection of the CRC32 value + CRC32_val = ~CRC32_val; break; case seqFuelPage: + //Confirmed working raw_value = getPageValue(seqFuelPage, 0); - CRC32_val = CRC32.crc32(&raw_value, 1); + CRC32_val = CRC32.crc32(&raw_value, 1, false); for(uint16_t x=1; x< npage_size[seqFuelPage]; x++) { raw_value = getPageValue(seqFuelPage, x); - CRC32_val = CRC32.crc32_upd(&raw_value, 1); + CRC32_val = CRC32.crc32_upd(&raw_value, 1, false); } + //Do a manual reflection of the CRC32 value + CRC32_val = ~CRC32_val; break; case canbusPage: