]> sigrok.org Git - libsigrokdecode.git/blame - decoders/lin/pd.py
ac97/lin: Remove some unneeded code snippets.
[libsigrokdecode.git] / decoders / lin / pd.py
CommitLineData
fe9160d5 1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2018 Stephan Thiele <stephan.thiele@mailbox.org>
5##
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.
10##
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.
15##
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/>.
18##
19
20import sigrokdecode as srd
21
22class LinFsm:
23 class State:
546733a2 24 WaitForBreak = 'WAIT_FOR_BREAK'
fe9160d5 25 Sync = 'SYNC'
26 Pid = 'PID'
27 Data = 'DATA'
28 Checksum = 'CHECKSUM'
29 Error = 'ERROR'
30
31 def transit(self, target_state):
32 if not self._transition_allowed(target_state):
33 return False
34 self.state = target_state
35 return True
36
37 def _transition_allowed(self, target_state):
38 if target_state == LinFsm.State.Error:
39 return True
b1e3616d 40 return target_state in self.allowed_state[self.state]
fe9160d5 41
42 def reset(self):
43 self.state = LinFsm.State.WaitForBreak
d871cc2d 44 self.uart_idle_count = 0
fe9160d5 45
46 def __init__(self):
546733a2
UH
47 a = dict()
48 a[LinFsm.State.WaitForBreak] = (LinFsm.State.Sync,)
49 a[LinFsm.State.Sync] = (LinFsm.State.Pid,)
50 a[LinFsm.State.Pid] = (LinFsm.State.Data,)
51 a[LinFsm.State.Data] = (LinFsm.State.Data, LinFsm.State.Checksum)
52 a[LinFsm.State.Checksum] = (LinFsm.State.WaitForBreak,)
53 a[LinFsm.State.Error] = (LinFsm.State.Sync,)
54 self.allowed_state = a
fe9160d5 55
56 self.state = None
d871cc2d 57 self.uart_idle_count = 0
fe9160d5 58 self.reset()
59
60class Decoder(srd.Decoder):
61 api_version = 3
62 id = 'lin'
63 name = 'LIN'
64 longname = 'Local Interconnect Network'
65 desc = 'Local Interconnect Network (LIN) protocol.'
66 license = 'gplv2+'
67 inputs = ['uart']
6cbba91f 68 outputs = []
d6d8a8a4 69 tags = ['Automotive']
fe9160d5 70 options = (
71 {'id': 'version', 'desc': 'Protocol version', 'default': 2, 'values': (1, 2)},
72 )
73 annotations = (
74 ('data', 'LIN data'),
75 ('control', 'Protocol info'),
76 ('error', 'Error descriptions'),
77 ('inline_error', 'Protocol violations and errors'),
78 )
79 annotation_rows = (
80 ('data', 'Data', (0, 1, 3)),
81 ('error', 'Error', (2,)),
82 )
83
84 def __init__(self):
85 self.reset()
86
87 def reset(self):
88 self.fsm = LinFsm()
89 self.lin_header = []
90 self.lin_rsp = []
91 self.lin_version = None
fe9160d5 92 self.ss_block = None
93 self.es_block = None
94
95 def start(self):
96 self.out_ann = self.register(srd.OUTPUT_ANN)
97 self.lin_version = self.options['version']
98
99 def putx(self, data):
100 self.put(self.ss_block, self.es_block, self.out_ann, data)
101
102 def wipe_break_null_byte(self, value):
103 # Upon a break condition a null byte is received which must be ignored.
546733a2 104 if self.fsm.state not in (LinFsm.State.WaitForBreak, LinFsm.State.Error):
fe9160d5 105 if len(self.lin_rsp):
106 value = self.lin_rsp.pop()[2]
107 else:
108 self.lin_header.pop()
109
110 if value != 0:
111 self.fsm.transit(LinFsm.State.Error)
546733a2 112 self.handle_error(None)
fe9160d5 113 return False
114
115 return True
116
d871cc2d
ST
117 def handle_uart_idle(self):
118 if self.fsm.state not in (LinFsm.State.WaitForBreak, LinFsm.State.Error):
119 self.fsm.uart_idle_count += 1
120
121 if self.fsm.uart_idle_count == 2:
122 self.fsm.transit(LinFsm.State.Checksum)
123 self.handle_checksum()
124 self.fsm.reset()
125
fe9160d5 126 def handle_wait_for_break(self, value):
127 self.wipe_break_null_byte(value)
128
129 def handle_break(self, value):
546733a2 130 if self.fsm.state not in (LinFsm.State.WaitForBreak, LinFsm.State.Error):
fe9160d5 131 if self.wipe_break_null_byte(value):
132 self.fsm.transit(LinFsm.State.Checksum)
133 self.handle_checksum()
134
135 self.fsm.reset()
136 self.fsm.transit(LinFsm.State.Sync)
137
740a49d4 138 self.putx([1, ['Break condition', 'Break', 'Brk', 'B']])
fe9160d5 139
140 def handle_sync(self, value):
141 self.fsm.transit(LinFsm.State.Pid)
142 self.lin_header.append((self.ss_block, self.es_block, value))
143
144 def handle_pid(self, value):
145 self.fsm.transit(LinFsm.State.Data)
146 self.lin_header.append((self.ss_block, self.es_block, value))
147
148 def handle_data(self, value):
149 self.lin_rsp.append((self.ss_block, self.es_block, value))
150
151 def handle_checksum(self):
152 sync = self.lin_header.pop(0) if len(self.lin_header) else None
153
154 self.put(sync[0], sync[1], self.out_ann, [0, ['Sync', 'S']])
155
156 if sync[2] != 0x55:
157 self.put(sync[0], sync[1], self.out_ann,
158 [2, ['Sync is not 0x55', 'Not 0x55', '!= 0x55']])
159
160 pid = self.lin_header.pop(0) if len(self.lin_header) else None
161 checksum = self.lin_rsp.pop() if len(self.lin_rsp) else None
162
163 if pid:
16d2031b 164 id_ = pid[2] & 0x3F
fe9160d5 165 parity = pid[2] >> 6
166
167 expected_parity = self.calc_parity(pid[2])
168 parity_valid = parity == expected_parity
169
170 if not parity_valid:
171 self.put(pid[0], pid[1], self.out_ann, [2, ['P != %d' % expected_parity]])
172
173 ann_class = 0 if parity_valid else 3
174 self.put(pid[0], pid[1], self.out_ann, [ann_class, [
16d2031b
RJ
175 'ID: %02X Parity: %d (%s)' % (id_, parity, 'ok' if parity_valid else 'bad'),
176 'ID: 0x%02X' % id_, 'I: %d' % id_
fe9160d5 177 ]])
178
179 if len(self.lin_rsp):
180 checksum_valid = self.checksum_is_valid(pid[2], self.lin_rsp, checksum[2])
181
182 for b in self.lin_rsp:
183 self.put(b[0], b[1], self.out_ann, [0, ['Data: 0x%02X' % b[2], 'D: 0x%02X' % b[2]]])
184
185 ann_class = 0 if checksum_valid else 3
186 self.put(checksum[0], checksum[1], self.out_ann,
187 [ann_class, ['Checksum: 0x%02X' % checksum[2], 'Checksum', 'Chk', 'C']])
188
189 if not checksum_valid:
190 self.put(checksum[0], checksum[1], self.out_ann, [2, ['Checksum invalid']])
191 else:
192 pass # No response.
193
194 self.lin_header.clear()
195 self.lin_rsp.clear()
196
546733a2 197 def handle_error(self, dummy):
fe9160d5 198 self.putx([3, ['Error', 'Err', 'E']])
199
200 def checksum_is_valid(self, pid, data, checksum):
201 if self.lin_version == 2:
16d2031b 202 id_ = pid & 0x3F
fe9160d5 203
16d2031b 204 if id_ != 60 and id_ != 61:
fe9160d5 205 checksum += pid
206
207 for d in data:
208 checksum += d[2]
209
210 carry_bits = int(checksum / 256)
211 checksum += carry_bits
212
213 return checksum & 0xFF == 0xFF
214
215 @staticmethod
216 def calc_parity(pid):
16d2031b 217 id_ = [((pid & 0x3F) >> i) & 1 for i in range(8)]
fe9160d5 218
16d2031b
RJ
219 p0 = id_[0] ^ id_[1] ^ id_[2] ^ id_[4]
220 p1 = not (id_[1] ^ id_[3] ^ id_[4] ^ id_[5])
fe9160d5 221
222 return (p0 << 0) | (p1 << 1)
223
224 def decode(self, ss, es, data):
225 ptype, rxtx, pdata = data
226
227 self.ss_block, self.es_block = ss, es
228
229 # Ignore all UART packets except the actual data packets or BREAK.
d871cc2d
ST
230 if ptype == 'IDLE':
231 self.handle_uart_idle()
fe9160d5 232 if ptype == 'BREAK':
233 self.handle_break(pdata)
234 if ptype != 'DATA':
235 return
236
237 # We're only interested in the byte value (not individual bits).
238 pdata = pdata[0]
239
240 # Short LIN overview:
241 # - Message begins with a BREAK (0x00) for at least 13 bittimes.
242 # - Break is always followed by a SYNC byte (0x55).
243 # - Sync byte is followed by a PID byte (Protected Identifier).
244 # - PID byte is followed by 1 - 8 data bytes and a final checksum byte.
245
546733a2
UH
246 handler = getattr(self, 'handle_%s' % self.fsm.state.lower())
247 handler(pdata)