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