]>
Commit | Line | Data |
---|---|---|
702fa251 | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
702fa251 | 3 | ## |
e20f455c | 4 | ## Copyright (C) 2012-2013 Uwe Hermann <uwe@hermann-uwe.de> |
38b40330 | 5 | ## Copyright (C) 2019 Stephan Thiele <stephan.thiele@mailbox.org> |
702fa251 UH |
6 | ## |
7 | ## This program is free software; you can redistribute it and/or modify | |
8 | ## it under the terms of the GNU General Public License as published by | |
9 | ## the Free Software Foundation; either version 2 of the License, or | |
10 | ## (at your option) any later version. | |
11 | ## | |
12 | ## This program is distributed in the hope that it will be useful, | |
13 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ## GNU General Public License for more details. | |
16 | ## | |
17 | ## You should have received a copy of the GNU General Public License | |
4539e9ca | 18 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
702fa251 | 19 | ## |
702fa251 | 20 | |
ae3ed295 | 21 | from common.srdhelper import bitpack_msb |
702fa251 UH |
22 | import sigrokdecode as srd |
23 | ||
21cda951 UH |
24 | class SamplerateError(Exception): |
25 | pass | |
26 | ||
ad373029 UH |
27 | def dlc2len(dlc): |
28 | return [0, 1, 2, 3, 4, 5, 6, 7, 8, 12, 16, 20, 24, 32, 48, 64][dlc] | |
29 | ||
702fa251 | 30 | class Decoder(srd.Decoder): |
64d87119 | 31 | api_version = 3 |
702fa251 UH |
32 | id = 'can' |
33 | name = 'CAN' | |
9e1437a0 | 34 | longname = 'Controller Area Network' |
702fa251 UH |
35 | desc = 'Field bus protocol for distributed realtime control.' |
36 | license = 'gplv2+' | |
37 | inputs = ['logic'] | |
7ecd283c | 38 | outputs = ['can'] |
d6d8a8a4 | 39 | tags = ['Automotive'] |
6a15597a | 40 | channels = ( |
702fa251 | 41 | {'id': 'can_rx', 'name': 'CAN RX', 'desc': 'CAN bus line'}, |
da9bcbd9 | 42 | ) |
84c1c0b5 | 43 | options = ( |
3b593817 UH |
44 | {'id': 'nominal_bitrate', 'desc': 'Nominal bitrate (bits/s)', 'default': 1000000}, |
45 | {'id': 'fast_bitrate', 'desc': 'Fast bitrate (bits/s)', 'default': 2000000}, | |
b0918d40 | 46 | {'id': 'sample_point', 'desc': 'Sample point (%)', 'default': 70.0}, |
84c1c0b5 | 47 | ) |
da9bcbd9 | 48 | annotations = ( |
e144452b | 49 | ('data', 'Payload data'), |
da9bcbd9 BV |
50 | ('sof', 'Start of frame'), |
51 | ('eof', 'End of frame'), | |
52 | ('id', 'Identifier'), | |
53 | ('ext-id', 'Extended identifier'), | |
54 | ('full-id', 'Full identifier'), | |
55 | ('ide', 'Identifier extension bit'), | |
56 | ('reserved-bit', 'Reserved bit 0 and 1'), | |
57 | ('rtr', 'Remote transmission request'), | |
58 | ('srr', 'Substitute remote request'), | |
59 | ('dlc', 'Data length count'), | |
60 | ('crc-sequence', 'CRC sequence'), | |
61 | ('crc-delimiter', 'CRC delimiter'), | |
62 | ('ack-slot', 'ACK slot'), | |
63 | ('ack-delimiter', 'ACK delimiter'), | |
64 | ('stuff-bit', 'Stuff bit'), | |
e144452b | 65 | ('warning', 'Warning'), |
544038d9 | 66 | ('bit', 'Bit'), |
d4a28d0f UH |
67 | ) |
68 | annotation_rows = ( | |
544038d9 | 69 | ('bits', 'Bits', (15, 17)), |
2fac4493 UH |
70 | ('fields', 'Fields', tuple(range(15))), |
71 | ('warnings', 'Warnings', (16,)), | |
da9bcbd9 | 72 | ) |
702fa251 | 73 | |
92b7b49f | 74 | def __init__(self): |
10aeb8ea GS |
75 | self.reset() |
76 | ||
77 | def reset(self): | |
f372d597 | 78 | self.samplerate = None |
702fa251 UH |
79 | self.reset_variables() |
80 | ||
f372d597 | 81 | def start(self): |
be465111 | 82 | self.out_ann = self.register(srd.OUTPUT_ANN) |
7ecd283c | 83 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
702fa251 | 84 | |
8abd7aa3 ST |
85 | def set_bit_rate(self, bitrate): |
86 | self.bit_width = float(self.samplerate) / float(bitrate) | |
87 | self.sample_point = (self.bit_width / 100.0) * self.options['sample_point'] | |
88 | ||
89 | def set_nominal_bitrate(self): | |
90 | self.set_bit_rate(self.options['nominal_bitrate']) | |
91 | ||
92 | def set_fast_bitrate(self): | |
93 | self.set_bit_rate(self.options['fast_bitrate']) | |
94 | ||
f372d597 BV |
95 | def metadata(self, key, value): |
96 | if key == srd.SRD_CONF_SAMPLERATE: | |
97 | self.samplerate = value | |
2d9e1115 | 98 | self.bit_width = float(self.samplerate) / float(self.options['nominal_bitrate']) |
300f9194 | 99 | self.sample_point = (self.bit_width / 100.0) * self.options['sample_point'] |
702fa251 | 100 | |
4b1813b4 UH |
101 | # Generic helper for CAN bit annotations. |
102 | def putg(self, ss, es, data): | |
300f9194 | 103 | left, right = int(self.sample_point), int(self.bit_width - self.sample_point) |
4b1813b4 UH |
104 | self.put(ss - left, es + right, self.out_ann, data) |
105 | ||
106 | # Single-CAN-bit annotation using the current samplenum. | |
e20f455c | 107 | def putx(self, data): |
4b1813b4 UH |
108 | self.putg(self.samplenum, self.samplenum, data) |
109 | ||
110 | # Single-CAN-bit annotation using the samplenum of CAN bit 12. | |
111 | def put12(self, data): | |
112 | self.putg(self.ss_bit12, self.ss_bit12, data) | |
113 | ||
6c890c08 | 114 | # Single-CAN-bit annotation using the samplenum of CAN bit 32. |
115 | def put32(self, data): | |
116 | self.putg(self.ss_bit32, self.ss_bit32, data) | |
117 | ||
4b1813b4 UH |
118 | # Multi-CAN-bit annotation from self.ss_block to current samplenum. |
119 | def putb(self, data): | |
120 | self.putg(self.ss_block, self.samplenum, data) | |
e20f455c | 121 | |
ba8529ae | 122 | def putpy(self, data): |
7ecd283c KH |
123 | self.put(self.ss_packet, self.es_packet, self.out_python, data) |
124 | ||
702fa251 UH |
125 | def reset_variables(self): |
126 | self.state = 'IDLE' | |
127 | self.sof = self.frame_type = self.dlc = None | |
128 | self.rawbits = [] # All bits, including stuff bits | |
129 | self.bits = [] # Only actual CAN frame bits (no stuff bits) | |
130 | self.curbit = 0 # Current bit of CAN frame (bit 0 == SOF) | |
131 | self.last_databit = 999 # Positive value that bitnum+x will never match | |
4b1813b4 UH |
132 | self.ss_block = None |
133 | self.ss_bit12 = None | |
6c890c08 | 134 | self.ss_bit32 = None |
4b1813b4 | 135 | self.ss_databytebits = [] |
ba8529ae | 136 | self.frame_bytes = [] |
7ecd283c | 137 | self.rtr_type = None |
6c890c08 | 138 | self.fd = False |
139 | self.rtr = None | |
702fa251 | 140 | |
45a50880 GS |
141 | # Poor man's clock synchronization. Use signal edges which change to |
142 | # dominant state in rather simple ways. This naive approach is neither | |
143 | # aware of the SYNC phase's width nor the specific location of the edge, | |
144 | # but improves the decoder's reliability when the input signal's bitrate | |
145 | # does not exactly match the nominal rate. | |
146 | def dom_edge_seen(self, force = False): | |
147 | self.dom_edge_snum = self.samplenum | |
148 | self.dom_edge_bcount = self.curbit | |
149 | ||
64d87119 GS |
150 | # Determine the position of the next desired bit's sample point. |
151 | def get_sample_point(self, bitnum): | |
45a50880 | 152 | samplenum = self.dom_edge_snum |
e4eeaab3 GS |
153 | samplenum += self.bit_width * (bitnum - self.dom_edge_bcount) |
154 | samplenum += self.sample_point | |
155 | return int(samplenum) | |
702fa251 UH |
156 | |
157 | def is_stuff_bit(self): | |
158 | # CAN uses NRZ encoding and bit stuffing. | |
159 | # After 5 identical bits, a stuff bit of opposite value is added. | |
a0128522 | 160 | # But not in the CRC delimiter, ACK, and end of frame fields. |
cffb6592 | 161 | if len(self.bits) > self.last_databit + 17: |
a0128522 | 162 | return False |
702fa251 UH |
163 | last_6_bits = self.rawbits[-6:] |
164 | if last_6_bits not in ([0, 0, 0, 0, 0, 1], [1, 1, 1, 1, 1, 0]): | |
165 | return False | |
166 | ||
167 | # Stuff bit. Keep it in self.rawbits, but drop it from self.bits. | |
702fa251 UH |
168 | self.bits.pop() # Drop last bit. |
169 | return True | |
170 | ||
171 | def is_valid_crc(self, crc_bits): | |
172 | return True # TODO | |
173 | ||
174 | def decode_error_frame(self, bits): | |
175 | pass # TODO | |
176 | ||
177 | def decode_overload_frame(self, bits): | |
178 | pass # TODO | |
179 | ||
180 | # Both standard and extended frames end with CRC, CRC delimiter, ACK, | |
181 | # ACK delimiter, and EOF fields. Handle them in a common function. | |
182 | # Returns True if the frame ended (EOF), False otherwise. | |
183 | def decode_frame_end(self, can_rx, bitnum): | |
184 | ||
4b1813b4 UH |
185 | # Remember start of CRC sequence (see below). |
186 | if bitnum == (self.last_databit + 1): | |
187 | self.ss_block = self.samplenum | |
741dba78 | 188 | if self.fd: |
ad373029 | 189 | if dlc2len(self.dlc) < 16: |
741dba78 ST |
190 | self.crc_len = 27 # 17 + SBC + stuff bits |
191 | else: | |
fd41596a | 192 | self.crc_len = 32 # 21 + SBC + stuff bits |
741dba78 ST |
193 | else: |
194 | self.crc_len = 15 | |
195 | ||
196 | # CRC sequence (15 bits, 17 bits or 21 bits) | |
197 | elif bitnum == (self.last_databit + self.crc_len): | |
198 | if self.fd: | |
ad373029 | 199 | if dlc2len(self.dlc) < 16: |
3b593817 UH |
200 | crc_type = "CRC-17" |
201 | else: | |
202 | crc_type = "CRC-21" | |
741dba78 | 203 | else: |
9a76aa18 | 204 | crc_type = "CRC-15" |
741dba78 | 205 | |
702fa251 | 206 | x = self.last_databit + 1 |
741dba78 | 207 | crc_bits = self.bits[x:x + self.crc_len + 1] |
ae3ed295 | 208 | self.crc = bitpack_msb(crc_bits) |
741dba78 ST |
209 | self.putb([11, ['%s sequence: 0x%04x' % (crc_type, self.crc), |
210 | '%s: 0x%04x' % (crc_type, self.crc), '%s' % crc_type]]) | |
702fa251 | 211 | if not self.is_valid_crc(crc_bits): |
74c9bb3c | 212 | self.putb([16, ['CRC is invalid']]) |
702fa251 UH |
213 | |
214 | # CRC delimiter bit (recessive) | |
741dba78 | 215 | elif bitnum == (self.last_databit + self.crc_len + 1): |
74c9bb3c UH |
216 | self.putx([12, ['CRC delimiter: %d' % can_rx, |
217 | 'CRC d: %d' % can_rx, 'CRC d']]) | |
2fac4493 UH |
218 | if can_rx != 1: |
219 | self.putx([16, ['CRC delimiter must be a recessive bit']]) | |
702fa251 | 220 | |
8abd7aa3 ST |
221 | if self.fd: |
222 | self.set_nominal_bitrate() | |
223 | ||
702fa251 | 224 | # ACK slot bit (dominant: ACK, recessive: NACK) |
741dba78 | 225 | elif bitnum == (self.last_databit + self.crc_len + 2): |
702fa251 | 226 | ack = 'ACK' if can_rx == 0 else 'NACK' |
74c9bb3c | 227 | self.putx([13, ['ACK slot: %s' % ack, 'ACK s: %s' % ack, 'ACK s']]) |
702fa251 UH |
228 | |
229 | # ACK delimiter bit (recessive) | |
741dba78 | 230 | elif bitnum == (self.last_databit + self.crc_len + 3): |
74c9bb3c UH |
231 | self.putx([14, ['ACK delimiter: %d' % can_rx, |
232 | 'ACK d: %d' % can_rx, 'ACK d']]) | |
2fac4493 UH |
233 | if can_rx != 1: |
234 | self.putx([16, ['ACK delimiter must be a recessive bit']]) | |
702fa251 | 235 | |
4b1813b4 | 236 | # Remember start of EOF (see below). |
741dba78 | 237 | elif bitnum == (self.last_databit + self.crc_len + 4): |
4b1813b4 UH |
238 | self.ss_block = self.samplenum |
239 | ||
702fa251 | 240 | # End of frame (EOF), 7 recessive bits |
b177af15 | 241 | elif bitnum == (self.last_databit + self.crc_len + 10): |
74c9bb3c | 242 | self.putb([2, ['End of frame', 'EOF', 'E']]) |
2fac4493 UH |
243 | if self.rawbits[-7:] != [1, 1, 1, 1, 1, 1, 1]: |
244 | self.putb([16, ['End of frame (EOF) must be 7 recessive bits']]) | |
7ecd283c | 245 | self.es_packet = self.samplenum |
ba8529ae GS |
246 | py_data = tuple([self.frame_type, self.fullid, self.rtr_type, |
247 | self.dlc, self.frame_bytes]) | |
248 | self.putpy(py_data) | |
702fa251 UH |
249 | self.reset_variables() |
250 | return True | |
251 | ||
252 | return False | |
253 | ||
254 | # Returns True if the frame ended (EOF), False otherwise. | |
255 | def decode_standard_frame(self, can_rx, bitnum): | |
256 | ||
3b593817 UH |
257 | # Bit 14: FDF (Flexible data format) |
258 | # Has to be sent dominant when FD frame, has to be sent recessive | |
259 | # when classic CAN frame. | |
702fa251 | 260 | if bitnum == 14: |
38b40330 | 261 | self.fd = True if can_rx else False |
b177af15 | 262 | if self.fd: |
3b593817 UH |
263 | self.putx([7, ['Flexible data format: %d' % can_rx, |
264 | 'FDF: %d' % can_rx, 'FDF']]) | |
b177af15 ST |
265 | else: |
266 | self.putx([7, ['Reserved bit 0: %d' % can_rx, | |
3b593817 | 267 | 'RB0: %d' % can_rx, 'RB0']]) |
38b40330 | 268 | |
38b40330 | 269 | if self.fd: |
3b593817 UH |
270 | # Bit 12: Substitute remote request (SRR) bit |
271 | self.put12([8, ['Substitute remote request', 'SRR']]) | |
7f75d507 | 272 | self.dlc_start = 18 |
38b40330 ST |
273 | else: |
274 | # Bit 12: Remote transmission request (RTR) bit | |
275 | # Data frame: dominant, remote frame: recessive | |
276 | # Remote frames do not contain a data field. | |
ba8529ae GS |
277 | rtr = 'remote' if self.bits[12] == 1 else 'data' |
278 | self.put12([8, ['Remote transmission request: %s frame' % rtr, | |
279 | 'RTR: %s frame' % rtr, 'RTR']]) | |
280 | self.rtr_type = rtr | |
7f75d507 | 281 | self.dlc_start = 15 |
38b40330 | 282 | |
3b593817 UH |
283 | if bitnum == 15 and self.fd: |
284 | self.putx([7, ['Reserved: %d' % can_rx, 'R0: %d' % can_rx, 'R0']]) | |
7f75d507 | 285 | |
3b593817 UH |
286 | if bitnum == 16 and self.fd: |
287 | self.putx([7, ['Bit rate switch: %d' % can_rx, 'BRS: %d' % can_rx, 'BRS']]) | |
702fa251 | 288 | |
3b593817 UH |
289 | if bitnum == 17 and self.fd: |
290 | self.putx([7, ['Error state indicator: %d' % can_rx, 'ESI: %d' % can_rx, 'ESI']]) | |
4b1813b4 UH |
291 | |
292 | # Remember start of DLC (see below). | |
7f75d507 | 293 | elif bitnum == self.dlc_start: |
4b1813b4 | 294 | self.ss_block = self.samplenum |
702fa251 UH |
295 | |
296 | # Bits 15-18: Data length code (DLC), in number of bytes (0-8). | |
7f75d507 | 297 | elif bitnum == self.dlc_start + 3: |
ae3ed295 | 298 | self.dlc = bitpack_msb(self.bits[self.dlc_start:self.dlc_start + 4]) |
b177af15 | 299 | self.putb([10, ['Data length code: %d' % self.dlc, |
3b593817 | 300 | 'DLC: %d' % self.dlc, 'DLC']]) |
ad373029 | 301 | self.last_databit = self.dlc_start + 3 + (dlc2len(self.dlc) * 8) |
b177af15 ST |
302 | if self.dlc > 8 and not self.fd: |
303 | self.putb([16, ['Data length code (DLC) > 8 is not allowed']]) | |
702fa251 | 304 | |
4b1813b4 | 305 | # Remember all databyte bits, except the very last one. |
7f75d507 | 306 | elif bitnum in range(self.dlc_start + 4, self.last_databit): |
4b1813b4 UH |
307 | self.ss_databytebits.append(self.samplenum) |
308 | ||
702fa251 UH |
309 | # Bits 19-X: Data field (0-8 bytes, depending on DLC) |
310 | # The bits within a data byte are transferred MSB-first. | |
311 | elif bitnum == self.last_databit: | |
4b1813b4 | 312 | self.ss_databytebits.append(self.samplenum) # Last databyte bit. |
ad373029 | 313 | for i in range(dlc2len(self.dlc)): |
7f75d507 | 314 | x = self.dlc_start + 4 + (8 * i) |
ae3ed295 | 315 | b = bitpack_msb(self.bits[x:x + 8]) |
ba8529ae | 316 | self.frame_bytes.append(b) |
4b1813b4 UH |
317 | ss = self.ss_databytebits[i * 8] |
318 | es = self.ss_databytebits[((i + 1) * 8) - 1] | |
534ae912 UH |
319 | self.putg(ss, es, [0, ['Data byte %d: 0x%02x' % (i, b), |
320 | 'DB %d: 0x%02x' % (i, b), 'DB']]) | |
4b1813b4 | 321 | self.ss_databytebits = [] |
702fa251 UH |
322 | |
323 | elif bitnum > self.last_databit: | |
324 | return self.decode_frame_end(can_rx, bitnum) | |
325 | ||
326 | return False | |
327 | ||
328 | # Returns True if the frame ended (EOF), False otherwise. | |
329 | def decode_extended_frame(self, can_rx, bitnum): | |
330 | ||
4b1813b4 UH |
331 | # Remember start of EID (see below). |
332 | if bitnum == 14: | |
333 | self.ss_block = self.samplenum | |
655f8b16 | 334 | self.fd = False |
335 | self.dlc_start = 35 | |
4b1813b4 | 336 | |
702fa251 | 337 | # Bits 14-31: Extended identifier (EID[17..0]) |
4b1813b4 | 338 | elif bitnum == 31: |
ae3ed295 | 339 | self.eid = bitpack_msb(self.bits[14:]) |
534ae912 | 340 | s = '%d (0x%x)' % (self.eid, self.eid) |
74c9bb3c | 341 | self.putb([4, ['Extended Identifier: %s' % s, |
534ae912 | 342 | 'Extended ID: %s' % s, 'Extended ID', 'EID']]) |
702fa251 | 343 | |
bd7efe23 | 344 | self.fullid = self.ident << 18 | self.eid |
534ae912 | 345 | s = '%d (0x%x)' % (self.fullid, self.fullid) |
74c9bb3c | 346 | self.putb([5, ['Full Identifier: %s' % s, 'Full ID: %s' % s, |
534ae912 | 347 | 'Full ID', 'FID']]) |
702fa251 UH |
348 | |
349 | # Bit 12: Substitute remote request (SRR) bit | |
74c9bb3c | 350 | self.put12([9, ['Substitute remote request: %d' % self.bits[12], |
534ae912 | 351 | 'SRR: %d' % self.bits[12], 'SRR']]) |
702fa251 UH |
352 | |
353 | # Bit 32: Remote transmission request (RTR) bit | |
354 | # Data frame: dominant, remote frame: recessive | |
355 | # Remote frames do not contain a data field. | |
655f8b16 | 356 | |
357 | # Remember start of RTR (see below). | |
702fa251 | 358 | if bitnum == 32: |
6c890c08 | 359 | self.ss_bit32 = self.samplenum |
360 | self.rtr = can_rx | |
702fa251 | 361 | |
6c890c08 | 362 | if not self.fd: |
ba8529ae GS |
363 | rtr = 'remote' if can_rx == 1 else 'data' |
364 | self.putx([8, ['Remote transmission request: %s frame' % rtr, | |
365 | 'RTR: %s frame' % rtr, 'RTR']]) | |
366 | self.rtr_type = rtr | |
655f8b16 | 367 | |
702fa251 UH |
368 | # Bit 33: RB1 (reserved bit) |
369 | elif bitnum == 33: | |
655f8b16 | 370 | self.fd = True if can_rx else False |
655f8b16 | 371 | if self.fd: |
372 | self.dlc_start = 37 | |
3b593817 | 373 | self.putx([7, ['Flexible data format: %d' % can_rx, |
655f8b16 | 374 | 'FDF: %d' % can_rx, 'FDF']]) |
6c890c08 | 375 | self.put32([7, ['Reserved bit 1: %d' % self.rtr, |
376 | 'RB1: %d' % self.rtr, 'RB1']]) | |
655f8b16 | 377 | else: |
378 | self.putx([7, ['Reserved bit 1: %d' % can_rx, | |
379 | 'RB1: %d' % can_rx, 'RB1']]) | |
702fa251 UH |
380 | |
381 | # Bit 34: RB0 (reserved bit) | |
382 | elif bitnum == 34: | |
74c9bb3c | 383 | self.putx([7, ['Reserved bit 0: %d' % can_rx, |
534ae912 | 384 | 'RB0: %d' % can_rx, 'RB0']]) |
702fa251 | 385 | |
655f8b16 | 386 | elif bitnum == 35 and self.fd: |
387 | self.putx([7, ['Bit rate switch: %d' % can_rx, | |
388 | 'BRS: %d' % can_rx, 'BRS']]) | |
389 | ||
390 | elif bitnum == 36 and self.fd: | |
391 | self.putx([7, ['Error state indicator: %d' % can_rx, | |
392 | 'ESI: %d' % can_rx, 'ESI']]) | |
393 | ||
4b1813b4 | 394 | # Remember start of DLC (see below). |
655f8b16 | 395 | elif bitnum == self.dlc_start: |
4b1813b4 UH |
396 | self.ss_block = self.samplenum |
397 | ||
702fa251 | 398 | # Bits 35-38: Data length code (DLC), in number of bytes (0-8). |
655f8b16 | 399 | elif bitnum == self.dlc_start + 3: |
ae3ed295 | 400 | self.dlc = bitpack_msb(self.bits[self.dlc_start:self.dlc_start + 4]) |
b177af15 ST |
401 | self.putb([10, ['Data length code: %d' % self.dlc, |
402 | 'DLC: %d' % self.dlc, 'DLC']]) | |
ad373029 | 403 | self.last_databit = self.dlc_start + 3 + (dlc2len(self.dlc) * 8) |
702fa251 | 404 | |
4b1813b4 | 405 | # Remember all databyte bits, except the very last one. |
655f8b16 | 406 | elif bitnum in range(self.dlc_start + 4, self.last_databit): |
4b1813b4 UH |
407 | self.ss_databytebits.append(self.samplenum) |
408 | ||
702fa251 UH |
409 | # Bits 39-X: Data field (0-8 bytes, depending on DLC) |
410 | # The bits within a data byte are transferred MSB-first. | |
411 | elif bitnum == self.last_databit: | |
4b1813b4 | 412 | self.ss_databytebits.append(self.samplenum) # Last databyte bit. |
ad373029 | 413 | for i in range(dlc2len(self.dlc)): |
655f8b16 | 414 | x = self.dlc_start + 4 + (8 * i) |
ae3ed295 | 415 | b = bitpack_msb(self.bits[x:x + 8]) |
ba8529ae | 416 | self.frame_bytes.append(b) |
4b1813b4 UH |
417 | ss = self.ss_databytebits[i * 8] |
418 | es = self.ss_databytebits[((i + 1) * 8) - 1] | |
534ae912 UH |
419 | self.putg(ss, es, [0, ['Data byte %d: 0x%02x' % (i, b), |
420 | 'DB %d: 0x%02x' % (i, b), 'DB']]) | |
4b1813b4 | 421 | self.ss_databytebits = [] |
702fa251 UH |
422 | |
423 | elif bitnum > self.last_databit: | |
424 | return self.decode_frame_end(can_rx, bitnum) | |
425 | ||
426 | return False | |
427 | ||
428 | def handle_bit(self, can_rx): | |
429 | self.rawbits.append(can_rx) | |
430 | self.bits.append(can_rx) | |
431 | ||
432 | # Get the index of the current CAN frame bit (without stuff bits). | |
433 | bitnum = len(self.bits) - 1 | |
434 | ||
8abd7aa3 ST |
435 | if self.fd and can_rx: |
436 | if bitnum == 16 and self.frame_type == 'standard' \ | |
437 | or bitnum == 35 and self.frame_type == 'extended': | |
438 | self.dom_edge_seen(force=True) | |
439 | self.set_fast_bitrate() | |
440 | ||
702fa251 UH |
441 | # If this is a stuff bit, remove it from self.bits and ignore it. |
442 | if self.is_stuff_bit(): | |
544038d9 | 443 | self.putx([15, [str(can_rx)]]) |
702fa251 UH |
444 | self.curbit += 1 # Increase self.curbit (bitnum is not affected). |
445 | return | |
544038d9 UH |
446 | else: |
447 | self.putx([17, [str(can_rx)]]) | |
702fa251 UH |
448 | |
449 | # Bit 0: Start of frame (SOF) bit | |
450 | if bitnum == 0: | |
7ecd283c | 451 | self.ss_packet = self.samplenum |
2fac4493 UH |
452 | self.putx([1, ['Start of frame', 'SOF', 'S']]) |
453 | if can_rx != 0: | |
74c9bb3c | 454 | self.putx([16, ['Start of frame (SOF) must be a dominant bit']]) |
702fa251 | 455 | |
4b1813b4 UH |
456 | # Remember start of ID (see below). |
457 | elif bitnum == 1: | |
458 | self.ss_block = self.samplenum | |
459 | ||
702fa251 UH |
460 | # Bits 1-11: Identifier (ID[10..0]) |
461 | # The bits ID[10..4] must NOT be all recessive. | |
462 | elif bitnum == 11: | |
bd7efe23 GS |
463 | # BEWARE! Don't clobber the decoder's .id field which is |
464 | # part of its boiler plate! | |
ae3ed295 | 465 | self.ident = bitpack_msb(self.bits[1:]) |
bd7efe23 GS |
466 | self.fullid = self.ident |
467 | s = '%d (0x%x)' % (self.ident, self.ident), | |
74c9bb3c | 468 | self.putb([3, ['Identifier: %s' % s, 'ID: %s' % s, 'ID']]) |
bd7efe23 | 469 | if (self.ident & 0x7f0) == 0x7f0: |
2fac4493 | 470 | self.putb([16, ['Identifier bits 10..4 must not be all recessive']]) |
702fa251 UH |
471 | |
472 | # RTR or SRR bit, depending on frame type (gets handled later). | |
473 | elif bitnum == 12: | |
4b1813b4 UH |
474 | # self.putx([0, ['RTR/SRR: %d' % can_rx]]) # Debug only. |
475 | self.ss_bit12 = self.samplenum | |
702fa251 UH |
476 | |
477 | # Bit 13: Identifier extension (IDE) bit | |
478 | # Standard frame: dominant, extended frame: recessive | |
479 | elif bitnum == 13: | |
480 | ide = self.frame_type = 'standard' if can_rx == 0 else 'extended' | |
74c9bb3c | 481 | self.putx([6, ['Identifier extension bit: %s frame' % ide, |
534ae912 | 482 | 'IDE: %s frame' % ide, 'IDE']]) |
702fa251 UH |
483 | |
484 | # Bits 14-X: Frame-type dependent, passed to the resp. handlers. | |
485 | elif bitnum >= 14: | |
486 | if self.frame_type == 'standard': | |
487 | done = self.decode_standard_frame(can_rx, bitnum) | |
488 | else: | |
489 | done = self.decode_extended_frame(can_rx, bitnum) | |
490 | ||
491 | # The handlers return True if a frame ended (EOF). | |
492 | if done: | |
493 | return | |
494 | ||
495 | # After a frame there are 3 intermission bits (recessive). | |
496 | # After these bits, the bus is considered free. | |
497 | ||
498 | self.curbit += 1 | |
499 | ||
64d87119 | 500 | def decode(self): |
21cda951 UH |
501 | if not self.samplerate: |
502 | raise SamplerateError('Cannot decode without samplerate.') | |
702fa251 | 503 | |
64d87119 | 504 | while True: |
702fa251 UH |
505 | # State machine. |
506 | if self.state == 'IDLE': | |
507 | # Wait for a dominant state (logic 0) on the bus. | |
64d87119 | 508 | (can_rx,) = self.wait({0: 'l'}) |
702fa251 | 509 | self.sof = self.samplenum |
45a50880 | 510 | self.dom_edge_seen(force = True) |
702fa251 UH |
511 | self.state = 'GET BITS' |
512 | elif self.state == 'GET BITS': | |
513 | # Wait until we're in the correct bit/sampling position. | |
64d87119 | 514 | pos = self.get_sample_point(self.curbit) |
45a50880 GS |
515 | (can_rx,) = self.wait([{'skip': pos - self.samplenum}, {0: 'f'}]) |
516 | if self.matched[1]: | |
517 | self.dom_edge_seen() | |
518 | if self.matched[0]: | |
519 | self.handle_bit(can_rx) |