Hello,

attached patch fixes wrong computation of CRC sequence stuff bits for CAN FD messages.

Let me know if there is any issue merging this in.

Thanks,
Karel
From 51641461e991bc83c684ef21887e56c2bc535a23 Mon Sep 17 00:00:00 2001
From: Karel Gardas <karel@functional.vision>
Date: Wed, 3 Aug 2022 20:45:32 +0200
Subject: [PATCH] can: fix to not compute stuff bits inside the CAN FD CRC
 sequence

Standard tells it clearly that CAN FD CRC sequence contains fixed
stuff bits on specified positions, hence common logic of computing
stuff bits is not used for the CAN FD CRC sequence message part.
---
 decoders/can/pd.py | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)

diff --git a/decoders/can/pd.py b/decoders/can/pd.py
index fcd13e6..fac215f 100644
--- a/decoders/can/pd.py
+++ b/decoders/can/pd.py
@@ -154,7 +154,7 @@ def get_sample_point(self, bitnum):
         samplenum += self.sample_point
         return int(samplenum)
 
-    def is_stuff_bit(self):
+    def is_stuff_bit(self, bitnum):
         # CAN uses NRZ encoding and bit stuffing.
         # After 5 identical bits, a stuff bit of opposite value is added.
         # But not in the CRC delimiter, ACK, and end of frame fields.
@@ -163,6 +163,12 @@ def is_stuff_bit(self):
         last_6_bits = self.rawbits[-6:]
         if last_6_bits not in ([0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 0]):
             return False
+        # in case of CAN FD CRC sequence contains fixed stuff-bits
+        # hence they are not added by transmitter not removed by receiver
+        # they are fixed part of CRC sequence hence we should not remove
+        # them here by marking them as stuff-bits.
+        if bitnum > self.last_databit and self.fd:
+            return False
 
         # Stuff bit. Keep it in self.rawbits, but drop it from self.bits.
         self.bits.pop() # Drop last bit.
@@ -439,7 +445,7 @@ def handle_bit(self, can_rx):
                 self.set_fast_bitrate()
 
         # If this is a stuff bit, remove it from self.bits and ignore it.
-        if self.is_stuff_bit():
+        if self.is_stuff_bit(bitnum):
             self.putx([15, [str(can_rx)]])
             self.curbit += 1 # Increase self.curbit (bitnum is not affected).
             return
-- 
2.35.1

_______________________________________________
sigrok-devel mailing list
sigrok-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sigrok-devel

Reply via email to