]> sigrok.org Git - libsigrokdecode.git/blobdiff - decoders/can/pd.py
can: implement bit rate switch support for CAN-FD frames
[libsigrokdecode.git] / decoders / can / pd.py
index 292f51b650e97a7789809737a14899740e1043a2..3ab82b02c054dea02f0c5b4b7c0fbac0564f2917 100644 (file)
@@ -23,6 +23,9 @@ import sigrokdecode as srd
 class SamplerateError(Exception):
     pass
 
+def dlc2len(dlc):
+    return [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64][dlc]
+
 class Decoder(srd.Decoder):
     api_version = 3
     id = 'can'
@@ -70,9 +73,6 @@ class Decoder(srd.Decoder):
     def __init__(self):
         self.reset()
 
-    def dlc2len(self, dlc):
-        return [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64][dlc]
-
     def reset(self):
         self.samplerate = None
         self.reset_variables()
@@ -80,6 +80,16 @@ class Decoder(srd.Decoder):
     def start(self):
         self.out_ann = self.register(srd.OUTPUT_ANN)
 
+    def set_bit_rate(self, bitrate):
+        self.bit_width = float(self.samplerate) / float(bitrate)
+        self.sample_point = (self.bit_width / 100.0) * self.options['sample_point']
+
+    def set_nominal_bitrate(self):
+        self.set_bit_rate(self.options['nominal_bitrate'])
+
+    def set_fast_bitrate(self):
+        self.set_bit_rate(self.options['fast_bitrate'])
+
     def metadata(self, key, value):
         if key == srd.SRD_CONF_SAMPLERATE:
             self.samplerate = value
@@ -173,7 +183,7 @@ class Decoder(srd.Decoder):
         if bitnum == (self.last_databit + 1):
             self.ss_block = self.samplenum
             if self.fd:
-                if self.dlc2len(self.dlc) < 16:
+                if dlc2len(self.dlc) < 16:
                     self.crc_len = 27 # 17 + SBC + stuff bits
                 else:
                     self.crc_len = 32 # 21 + SBC + stuff bits
@@ -183,12 +193,12 @@ class Decoder(srd.Decoder):
         # CRC sequence (15 bits, 17 bits or 21 bits)
         elif bitnum == (self.last_databit + self.crc_len):
             if self.fd:
-                if self.dlc2len(self.dlc) < 16:
+                if dlc2len(self.dlc) < 16:
                     crc_type = "CRC-17"
                 else:
                     crc_type = "CRC-21"
             else:
-                crc_type = "CRC" # TODO: CRC-15 (will break existing tests)
+                crc_type = "CRC-15"
 
             x = self.last_databit + 1
             crc_bits = self.bits[x:x + self.crc_len + 1]
@@ -205,6 +215,9 @@ class Decoder(srd.Decoder):
             if can_rx != 1:
                 self.putx([16, ['CRC delimiter must be a recessive bit']])
 
+            if self.fd:
+                self.set_nominal_bitrate()
+
         # ACK slot bit (dominant: ACK, recessive: NACK)
         elif bitnum == (self.last_databit + self.crc_len + 2):
             ack = 'ACK' if can_rx == 0 else 'NACK'
@@ -277,7 +290,7 @@ class Decoder(srd.Decoder):
             self.dlc = int(''.join(str(d) for d in self.bits[self.dlc_start:self.dlc_start + 4]), 2)
             self.putb([10, ['Data length code: %d' % self.dlc,
                             'DLC: %d' % self.dlc, 'DLC']])
-            self.last_databit = self.dlc_start + 3 + (self.dlc2len(self.dlc) * 8)
+            self.last_databit = self.dlc_start + 3 + (dlc2len(self.dlc) * 8)
             if self.dlc > 8 and not self.fd:
                 self.putb([16, ['Data length code (DLC) > 8 is not allowed']])
 
@@ -289,7 +302,7 @@ class Decoder(srd.Decoder):
         # The bits within a data byte are transferred MSB-first.
         elif bitnum == self.last_databit:
             self.ss_databytebits.append(self.samplenum) # Last databyte bit.
-            for i in range(self.dlc2len(self.dlc)):
+            for i in range(dlc2len(self.dlc)):
                 x = self.dlc_start + 4 + (8 * i)
                 b = int(''.join(str(d) for d in self.bits[x:x + 8]), 2)
                 ss = self.ss_databytebits[i * 8]
@@ -377,7 +390,7 @@ class Decoder(srd.Decoder):
             self.dlc = int(''.join(str(d) for d in self.bits[self.dlc_start:self.dlc_start + 4]), 2)
             self.putb([10, ['Data length code: %d' % self.dlc,
                             'DLC: %d' % self.dlc, 'DLC']])
-            self.last_databit = self.dlc_start + 3 + (self.dlc2len(self.dlc) * 8)
+            self.last_databit = self.dlc_start + 3 + (dlc2len(self.dlc) * 8)
 
         # Remember all databyte bits, except the very last one.
         elif bitnum in range(self.dlc_start + 4, self.last_databit):
@@ -387,7 +400,7 @@ class Decoder(srd.Decoder):
         # The bits within a data byte are transferred MSB-first.
         elif bitnum == self.last_databit:
             self.ss_databytebits.append(self.samplenum) # Last databyte bit.
-            for i in range(self.dlc2len(self.dlc)):
+            for i in range(dlc2len(self.dlc)):
                 x = self.dlc_start + 4 + (8 * i)
                 b = int(''.join(str(d) for d in self.bits[x:x + 8]), 2)
                 ss = self.ss_databytebits[i * 8]
@@ -408,6 +421,12 @@ class Decoder(srd.Decoder):
         # Get the index of the current CAN frame bit (without stuff bits).
         bitnum = len(self.bits) - 1
 
+        if self.fd and can_rx:
+            if bitnum == 16 and self.frame_type == 'standard' \
+                    or bitnum == 35 and self.frame_type == 'extended':
+                self.dom_edge_seen(force=True)
+                self.set_fast_bitrate()
+
         # If this is a stuff bit, remove it from self.bits and ignore it.
         if self.is_stuff_bit():
             self.putx([15, [str(can_rx)]])