X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=decoders%2Fcan%2Fpd.py;h=3dbadc090313cc424feaf13051cf724e82c29108;hb=499bf266989634a02e33e5361a720f853934fe03;hp=0b001278a1905e9f27042d168e9a43fc2026645a;hpb=ad37302991cd5690bd1eb4db4abcad206fae42ec;p=libsigrokdecode.git diff --git a/decoders/can/pd.py b/decoders/can/pd.py index 0b00127..3dbadc0 100644 --- a/decoders/can/pd.py +++ b/decoders/can/pd.py @@ -45,7 +45,7 @@ class Decoder(srd.Decoder): {'id': 'sample_point', 'desc': 'Sample point (%)', 'default': 70.0}, ) annotations = ( - ('data', 'CAN payload data'), + ('data', 'Payload data'), ('sof', 'Start of frame'), ('eof', 'End of frame'), ('id', 'Identifier'), @@ -61,7 +61,7 @@ class Decoder(srd.Decoder): ('ack-slot', 'ACK slot'), ('ack-delimiter', 'ACK delimiter'), ('stuff-bit', 'Stuff bit'), - ('warnings', 'Human-readable warnings'), + ('warning', 'Warning'), ('bit', 'Bit'), ) annotation_rows = ( @@ -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 @@ -130,16 +140,12 @@ class Decoder(srd.Decoder): self.dom_edge_snum = self.samplenum self.dom_edge_bcount = self.curbit - def bit_sampled(self): - # EMPTY - pass - # Determine the position of the next desired bit's sample point. def get_sample_point(self, bitnum): samplenum = self.dom_edge_snum - samplenum += int(self.bit_width * (bitnum - self.dom_edge_bcount)) - samplenum += int(self.sample_point) - return samplenum + samplenum += self.bit_width * (bitnum - self.dom_edge_bcount) + samplenum += self.sample_point + return int(samplenum) def is_stuff_bit(self): # CAN uses NRZ encoding and bit stuffing. @@ -188,7 +194,7 @@ class Decoder(srd.Decoder): 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 +211,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' @@ -408,6 +417,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)]]) @@ -483,4 +498,3 @@ class Decoder(srd.Decoder): self.dom_edge_seen() if self.matched[0]: self.handle_bit(can_rx) - self.bit_sampled()