From b1ba421c37c425ea2b686f270bc127169c38a8b4 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Sat, 19 Dec 2020 22:48:12 +0100 Subject: [PATCH 1/3] initial support for CAN checksums --- scopeprotocols/CANDecoder.cpp | 26 ++++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) diff --git a/scopeprotocols/CANDecoder.cpp b/scopeprotocols/CANDecoder.cpp index 89f5bf39..6042df81 100644 --- a/scopeprotocols/CANDecoder.cpp +++ b/scopeprotocols/CANDecoder.cpp @@ -126,6 +126,12 @@ void CANDecoder::Refresh() int frame_bytes_left = 0; int32_t frame_id = 0; char tmp[128]; + + // CRC (http://esd.cs.ucr.edu/webres/can20.pdf page 13) + const uint16_t crc_poly = 0x4599; + uint16_t crc = 0; + + for(size_t i = 0; i < len; i++) { bool v = diff->m_samples[i]; @@ -218,6 +224,18 @@ void CANDecoder::Refresh() current_field |= 1; nbit ++; + + + if (state != STATE_CRC){ + uint16_t crc_bit_14 = (crc >> 14) & 0x1; + uint16_t crc_nxt = sampled_value ^ crc_bit_14; + crc = crc << 1; + + if (crc_nxt){ + crc = crc ^ crc_poly; + } + } + switch(state) { //Wait for at least 7 bit times low @@ -245,6 +263,7 @@ void CANDecoder::Refresh() tblockstart = off; nbit = 0; + crc = 0; current_field = 0; state = STATE_ID; break; @@ -416,10 +435,13 @@ void CANDecoder::Refresh() //CRC is 15 bits long if(nbit == 15) { - //TODO: actually check the CRC + + bool crc_ok = current_field == (crc & 0x7fff); + auto type = crc_ok ? CANSymbol::TYPE_CRC_OK : CANSymbol::TYPE_CRC_BAD; + cap->m_offsets.push_back(tblockstart); cap->m_durations.push_back(end - tblockstart); - cap->m_samples.push_back(CANSymbol(CANSymbol::TYPE_CRC_OK, current_field)); + cap->m_samples.push_back(CANSymbol(type, current_field)); state = STATE_CRC_DELIM; } From c15bc13ac6c3fd66ce6447d5e42f4163699ab1c2 Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Sat, 19 Dec 2020 22:54:38 +0100 Subject: [PATCH 2/3] fix formatting --- scopeprotocols/CANDecoder.cpp | 32 ++++++++++++++------------------ 1 file changed, 14 insertions(+), 18 deletions(-) diff --git a/scopeprotocols/CANDecoder.cpp b/scopeprotocols/CANDecoder.cpp index 6042df81..4b57da35 100644 --- a/scopeprotocols/CANDecoder.cpp +++ b/scopeprotocols/CANDecoder.cpp @@ -127,10 +127,9 @@ void CANDecoder::Refresh() int32_t frame_id = 0; char tmp[128]; - // CRC (http://esd.cs.ucr.edu/webres/can20.pdf page 13) - const uint16_t crc_poly = 0x4599; - uint16_t crc = 0; - + // CRC (http://esd.cs.ucr.edu/webres/can20.pdf page 13) + const uint16_t crc_poly = 0x4599; + uint16_t crc = 0; for(size_t i = 0; i < len; i++) { @@ -224,17 +223,15 @@ void CANDecoder::Refresh() current_field |= 1; nbit ++; + if (state != STATE_CRC){ + uint16_t crc_bit_14 = (crc >> 14) & 0x1; + uint16_t crc_nxt = sampled_value ^ crc_bit_14; + crc = crc << 1; - - if (state != STATE_CRC){ - uint16_t crc_bit_14 = (crc >> 14) & 0x1; - uint16_t crc_nxt = sampled_value ^ crc_bit_14; - crc = crc << 1; - - if (crc_nxt){ - crc = crc ^ crc_poly; - } - } + if (crc_nxt){ + crc = crc ^ crc_poly; + } + } switch(state) { @@ -263,7 +260,7 @@ void CANDecoder::Refresh() tblockstart = off; nbit = 0; - crc = 0; + crc = 0; current_field = 0; state = STATE_ID; break; @@ -435,9 +432,8 @@ void CANDecoder::Refresh() //CRC is 15 bits long if(nbit == 15) { - - bool crc_ok = current_field == (crc & 0x7fff); - auto type = crc_ok ? CANSymbol::TYPE_CRC_OK : CANSymbol::TYPE_CRC_BAD; + bool crc_ok = current_field == (crc & 0x7fff); + auto type = crc_ok ? CANSymbol::TYPE_CRC_OK : CANSymbol::TYPE_CRC_BAD; cap->m_offsets.push_back(tblockstart); cap->m_durations.push_back(end - tblockstart); From da5dd5a79cd24726ba9da4a9c6356e0b0b4c2dcc Mon Sep 17 00:00:00 2001 From: Willem Melching Date: Sat, 19 Dec 2020 23:17:59 +0100 Subject: [PATCH 3/3] add parentheses --- scopeprotocols/CANDecoder.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/scopeprotocols/CANDecoder.cpp b/scopeprotocols/CANDecoder.cpp index 4b57da35..858e95cc 100644 --- a/scopeprotocols/CANDecoder.cpp +++ b/scopeprotocols/CANDecoder.cpp @@ -432,7 +432,7 @@ void CANDecoder::Refresh() //CRC is 15 bits long if(nbit == 15) { - bool crc_ok = current_field == (crc & 0x7fff); + bool crc_ok = (current_field == (crc & 0x7fff)); auto type = crc_ok ? CANSymbol::TYPE_CRC_OK : CANSymbol::TYPE_CRC_BAD; cap->m_offsets.push_back(tblockstart);