X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=decoders%2Fcan%2Fpd.py;h=3dbadc090313cc424feaf13051cf724e82c29108;hb=79f5cbcfe332be55fb08d31618c086ee9282eb3d;hp=8c417fd3f3901fe454f6e9b591d5d8cf1f317201;hpb=6c890c087168b929a3017613b6ebfb7822a4f0fa;p=libsigrokdecode.git diff --git a/decoders/can/pd.py b/decoders/can/pd.py index 8c417fd..3dbadc0 100644 --- a/decoders/can/pd.py +++ b/decoders/can/pd.py @@ -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' @@ -37,11 +40,12 @@ class Decoder(srd.Decoder): {'id': 'can_rx', 'name': 'CAN RX', 'desc': 'CAN bus line'}, ) options = ( - {'id': 'bitrate', 'desc': 'Bitrate (bits/s)', 'default': 1000000}, + {'id': 'nominal_bitrate', 'desc': 'Nominal bitrate (bits/s)', 'default': 1000000}, + {'id': 'fast_bitrate', 'desc': 'Fast bitrate (bits/s)', 'default': 2000000}, {'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'), @@ -57,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 = ( @@ -69,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() @@ -79,10 +80,20 @@ 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 - self.bit_width = float(self.samplerate) / float(self.options['bitrate']) + self.bit_width = float(self.samplerate) / float(self.options['nominal_bitrate']) self.sample_point = (self.bit_width / 100.0) * self.options['sample_point'] # Generic helper for CAN bit annotations. @@ -129,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. @@ -171,9 +178,8 @@ class Decoder(srd.Decoder): # Remember start of CRC sequence (see below). 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 +189,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: - crc_type = "CRC-17" - else: - crc_type = "CRC-21" + 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 +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' @@ -234,23 +243,21 @@ class Decoder(srd.Decoder): # Returns True if the frame ended (EOF), False otherwise. def decode_standard_frame(self, can_rx, bitnum): - # Bit 14: FDF (Flexible Data Format) - # Has to be sent dominant when FD frame, has to be sent recessive when classic CAN frame. + # Bit 14: FDF (Flexible data format) + # Has to be sent dominant when FD frame, has to be sent recessive + # when classic CAN frame. if bitnum == 14: self.fd = True if can_rx else False - if self.fd: - self.putx([7, ['Flexible Data Format: %d' % can_rx, - 'FDF: %d' % can_rx, - 'FDF']]) + self.putx([7, ['Flexible data format: %d' % can_rx, + 'FDF: %d' % can_rx, 'FDF']]) else: self.putx([7, ['Reserved bit 0: %d' % can_rx, - 'RB0: %d' % can_rx, - 'RB0']]) + 'RB0: %d' % can_rx, 'RB0']]) - # SRR Substitute Remote Request if self.fd: - self.put12([8, ['Substitute Remote Request', 'SRR']]) + # Bit 12: Substitute remote request (SRR) bit + self.put12([8, ['Substitute remote request', 'SRR']]) self.dlc_start = 18 else: # Bit 12: Remote transmission request (RTR) bit @@ -261,17 +268,14 @@ class Decoder(srd.Decoder): 'RTR: %s frame' % rtr, 'RTR']]) self.dlc_start = 15 - if bitnum == 15: - if self.fd: - self.putx([7, ['Reserved: %d' % can_rx, 'R0: %d' % can_rx, 'R0']]) + if bitnum == 15 and self.fd: + self.putx([7, ['Reserved: %d' % can_rx, 'R0: %d' % can_rx, 'R0']]) - if bitnum == 16: - if self.fd: - self.putx([7, ['Bit rate switch: %d' % can_rx, 'BRS: %d' % can_rx, 'BRS']]) + if bitnum == 16 and self.fd: + self.putx([7, ['Bit rate switch: %d' % can_rx, 'BRS: %d' % can_rx, 'BRS']]) - if bitnum == 17: - if self.fd: - self.putx([7, ['Error state indicator: %d' % can_rx, 'ESI: %d' % can_rx, 'ESI']]) + if bitnum == 17 and self.fd: + self.putx([7, ['Error state indicator: %d' % can_rx, 'ESI: %d' % can_rx, 'ESI']]) # Remember start of DLC (see below). elif bitnum == self.dlc_start: @@ -281,8 +285,8 @@ class Decoder(srd.Decoder): elif bitnum == self.dlc_start + 3: 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) + 'DLC: %d' % self.dlc, 'DLC']]) + 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']]) @@ -294,7 +298,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] @@ -350,12 +354,10 @@ class Decoder(srd.Decoder): # Bit 33: RB1 (reserved bit) elif bitnum == 33: self.fd = True if can_rx else False - if self.fd: self.dlc_start = 37 - self.putx([7, ['Flexible Data Format: %d' % can_rx, + self.putx([7, ['Flexible data format: %d' % can_rx, 'FDF: %d' % can_rx, 'FDF']]) - self.put32([7, ['Reserved bit 1: %d' % self.rtr, 'RB1: %d' % self.rtr, 'RB1']]) else: @@ -384,7 +386,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): @@ -394,7 +396,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] @@ -415,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)]]) @@ -490,4 +498,3 @@ class Decoder(srd.Decoder): self.dom_edge_seen() if self.matched[0]: self.handle_bit(can_rx) - self.bit_sampled()