2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2018 Stephan Thiele <stephan.thiele@mailbox.org>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
24 WaitForBreak = 'WAIT FOR BREAK'
31 def transit(self, target_state):
32 if not self._transition_allowed(target_state):
34 self.state = target_state
37 def _transition_allowed(self, target_state):
38 if target_state == LinFsm.State.Error:
40 return target_state in self.allowed_state[self.state]
43 self.state = LinFsm.State.WaitForBreak
46 self.allowed_state = dict()
47 self.allowed_state[LinFsm.State.WaitForBreak] = (LinFsm.State.Sync,)
48 self.allowed_state[LinFsm.State.Sync] = (LinFsm.State.Pid,)
49 self.allowed_state[LinFsm.State.Pid] = (LinFsm.State.Data,)
50 self.allowed_state[LinFsm.State.Data] = (LinFsm.State.Data, LinFsm.State.Checksum)
51 self.allowed_state[LinFsm.State.Checksum] = (LinFsm.State.WaitForBreak,)
52 self.allowed_state[LinFsm.State.Error] = (LinFsm.State.Sync,)
57 class Decoder(srd.Decoder):
61 longname = 'Local Interconnect Network'
62 desc = 'Local Interconnect Network (LIN) protocol.'
67 {'id': 'version', 'desc': 'Protocol version', 'default': 2, 'values': (1, 2)},
71 ('control', 'Protocol info'),
72 ('error', 'Error descriptions'),
73 ('inline_error', 'Protocol violations and errors'),
76 ('data', 'Data', (0, 1, 3)),
77 ('error', 'Error', (2,)),
87 self.lin_version = None
93 self.out_ann = self.register(srd.OUTPUT_ANN)
94 self.lin_version = self.options['version']
97 self.put(self.ss_block, self.es_block, self.out_ann, data)
99 def wipe_break_null_byte(self, value):
100 # Upon a break condition a null byte is received which must be ignored.
101 if self.fsm.state != LinFsm.State.WaitForBreak and self.fsm.state != LinFsm.State.Error:
102 if len(self.lin_rsp):
103 value = self.lin_rsp.pop()[2]
105 self.lin_header.pop()
108 self.fsm.transit(LinFsm.State.Error)
114 def handle_wait_for_break(self, value):
115 self.wipe_break_null_byte(value)
117 def handle_break(self, value):
118 if self.fsm.state != LinFsm.State.WaitForBreak and self.fsm.state != LinFsm.State.Error:
119 if self.wipe_break_null_byte(value):
120 self.fsm.transit(LinFsm.State.Checksum)
121 self.handle_checksum()
124 self.fsm.transit(LinFsm.State.Sync)
126 data = [1, ['Break condition', 'Break', 'Brk', 'B']]
127 self.put(self.ss_block, self.es_block, self.out_ann, data)
129 def handle_sync(self, value):
130 self.fsm.transit(LinFsm.State.Pid)
131 self.lin_header.append((self.ss_block, self.es_block, value))
133 def handle_pid(self, value):
134 self.fsm.transit(LinFsm.State.Data)
135 self.lin_header.append((self.ss_block, self.es_block, value))
137 def handle_data(self, value):
138 self.lin_rsp.append((self.ss_block, self.es_block, value))
140 def handle_checksum(self):
141 sync = self.lin_header.pop(0) if len(self.lin_header) else None
143 self.put(sync[0], sync[1], self.out_ann, [0, ['Sync', 'S']])
146 self.put(sync[0], sync[1], self.out_ann,
147 [2, ['Sync is not 0x55', 'Not 0x55', '!= 0x55']])
149 pid = self.lin_header.pop(0) if len(self.lin_header) else None
150 checksum = self.lin_rsp.pop() if len(self.lin_rsp) else None
156 expected_parity = self.calc_parity(pid[2])
157 parity_valid = parity == expected_parity
160 self.put(pid[0], pid[1], self.out_ann, [2, ['P != %d' % expected_parity]])
162 ann_class = 0 if parity_valid else 3
163 self.put(pid[0], pid[1], self.out_ann, [ann_class, [
164 'ID: %02X Parity: %d (%s)' % (id, parity, 'ok' if parity_valid else 'bad'),
165 'ID: 0x%02X' % id, 'I: %d' % id
168 if len(self.lin_rsp):
169 checksum_valid = self.checksum_is_valid(pid[2], self.lin_rsp, checksum[2])
171 for b in self.lin_rsp:
172 self.put(b[0], b[1], self.out_ann, [0, ['Data: 0x%02X' % b[2], 'D: 0x%02X' % b[2]]])
174 ann_class = 0 if checksum_valid else 3
175 self.put(checksum[0], checksum[1], self.out_ann,
176 [ann_class, ['Checksum: 0x%02X' % checksum[2], 'Checksum', 'Chk', 'C']])
178 if not checksum_valid:
179 self.put(checksum[0], checksum[1], self.out_ann, [2, ['Checksum invalid']])
183 self.lin_header.clear()
186 def handle_error(self):
187 self.putx([3, ['Error', 'Err', 'E']])
189 def checksum_is_valid(self, pid, data, checksum):
190 if self.lin_version == 2:
193 if id != 60 and id != 61:
199 carry_bits = int(checksum / 256)
200 checksum += carry_bits
202 return checksum & 0xFF == 0xFF
205 def calc_parity(pid):
206 id = [((pid & 0x3F) >> i) & 1 for i in range(8)]
208 p0 = id[0] ^ id[1] ^ id[2] ^ id[4]
209 p1 = not (id[1] ^ id[3] ^ id[4] ^ id[5])
211 return (p0 << 0) | (p1 << 1)
213 def decode(self, ss, es, data):
214 ptype, rxtx, pdata = data
216 self.ss_block, self.es_block = ss, es
218 # Ignore all UART packets except the actual data packets or BREAK.
220 self.handle_break(pdata)
224 # We're only interested in the byte value (not individual bits).
227 # Short LIN overview:
228 # - Message begins with a BREAK (0x00) for at least 13 bittimes.
229 # - Break is always followed by a SYNC byte (0x55).
230 # - Sync byte is followed by a PID byte (Protected Identifier).
231 # - PID byte is followed by 1 - 8 data bytes and a final checksum byte.
233 if self.fsm.state == LinFsm.State.Error: self.handle_error()
234 elif self.fsm.state == LinFsm.State.WaitForBreak: self.handle_wait_for_break(pdata)
235 elif self.fsm.state == LinFsm.State.Sync: self.handle_sync(pdata)
236 elif self.fsm.state == LinFsm.State.Pid: self.handle_pid(pdata)
237 elif self.fsm.state == LinFsm.State.Data: self.handle_data(pdata)
238 elif self.fsm.state == LinFsm.State.Checksum: self.handle_checksum()