]> sigrok.org Git - libsigrokdecode.git/blame - decoders/lin/pd.py
Rename values that shadow built-in keywords
[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
44
45 def __init__(self):
546733a2
UH
46 a = dict()
47 a[LinFsm.State.WaitForBreak] = (LinFsm.State.Sync,)
48 a[LinFsm.State.Sync] = (LinFsm.State.Pid,)
49 a[LinFsm.State.Pid] = (LinFsm.State.Data,)
50 a[LinFsm.State.Data] = (LinFsm.State.Data, LinFsm.State.Checksum)
51 a[LinFsm.State.Checksum] = (LinFsm.State.WaitForBreak,)
52 a[LinFsm.State.Error] = (LinFsm.State.Sync,)
53 self.allowed_state = a
fe9160d5 54
55 self.state = None
56 self.reset()
57
58class Decoder(srd.Decoder):
59 api_version = 3
60 id = 'lin'
61 name = 'LIN'
62 longname = 'Local Interconnect Network'
63 desc = 'Local Interconnect Network (LIN) protocol.'
64 license = 'gplv2+'
65 inputs = ['uart']
66 outputs = ['lin']
67 options = (
68 {'id': 'version', 'desc': 'Protocol version', 'default': 2, 'values': (1, 2)},
69 )
70 annotations = (
71 ('data', 'LIN data'),
72 ('control', 'Protocol info'),
73 ('error', 'Error descriptions'),
74 ('inline_error', 'Protocol violations and errors'),
75 )
76 annotation_rows = (
77 ('data', 'Data', (0, 1, 3)),
78 ('error', 'Error', (2,)),
79 )
80
81 def __init__(self):
82 self.reset()
83
84 def reset(self):
85 self.fsm = LinFsm()
86 self.lin_header = []
87 self.lin_rsp = []
88 self.lin_version = None
89 self.out_ann = None
90 self.ss_block = None
91 self.es_block = None
92
93 def start(self):
94 self.out_ann = self.register(srd.OUTPUT_ANN)
95 self.lin_version = self.options['version']
96
97 def putx(self, data):
98 self.put(self.ss_block, self.es_block, self.out_ann, data)
99
100 def wipe_break_null_byte(self, value):
101 # Upon a break condition a null byte is received which must be ignored.
546733a2 102 if self.fsm.state not in (LinFsm.State.WaitForBreak, LinFsm.State.Error):
fe9160d5 103 if len(self.lin_rsp):
104 value = self.lin_rsp.pop()[2]
105 else:
106 self.lin_header.pop()
107
108 if value != 0:
109 self.fsm.transit(LinFsm.State.Error)
546733a2 110 self.handle_error(None)
fe9160d5 111 return False
112
113 return True
114
115 def handle_wait_for_break(self, value):
116 self.wipe_break_null_byte(value)
117
118 def handle_break(self, value):
546733a2 119 if self.fsm.state not in (LinFsm.State.WaitForBreak, LinFsm.State.Error):
fe9160d5 120 if self.wipe_break_null_byte(value):
121 self.fsm.transit(LinFsm.State.Checksum)
122 self.handle_checksum()
123
124 self.fsm.reset()
125 self.fsm.transit(LinFsm.State.Sync)
126
740a49d4 127 self.putx([1, ['Break condition', 'Break', 'Brk', 'B']])
fe9160d5 128
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))
132
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))
136
137 def handle_data(self, value):
138 self.lin_rsp.append((self.ss_block, self.es_block, value))
139
140 def handle_checksum(self):
141 sync = self.lin_header.pop(0) if len(self.lin_header) else None
142
143 self.put(sync[0], sync[1], self.out_ann, [0, ['Sync', 'S']])
144
145 if sync[2] != 0x55:
146 self.put(sync[0], sync[1], self.out_ann,
147 [2, ['Sync is not 0x55', 'Not 0x55', '!= 0x55']])
148
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
151
152 if pid:
16d2031b 153 id_ = pid[2] & 0x3F
fe9160d5 154 parity = pid[2] >> 6
155
156 expected_parity = self.calc_parity(pid[2])
157 parity_valid = parity == expected_parity
158
159 if not parity_valid:
160 self.put(pid[0], pid[1], self.out_ann, [2, ['P != %d' % expected_parity]])
161
162 ann_class = 0 if parity_valid else 3
163 self.put(pid[0], pid[1], self.out_ann, [ann_class, [
16d2031b
RJ
164 'ID: %02X Parity: %d (%s)' % (id_, parity, 'ok' if parity_valid else 'bad'),
165 'ID: 0x%02X' % id_, 'I: %d' % id_
fe9160d5 166 ]])
167
168 if len(self.lin_rsp):
169 checksum_valid = self.checksum_is_valid(pid[2], self.lin_rsp, checksum[2])
170
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]]])
173
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']])
177
178 if not checksum_valid:
179 self.put(checksum[0], checksum[1], self.out_ann, [2, ['Checksum invalid']])
180 else:
181 pass # No response.
182
183 self.lin_header.clear()
184 self.lin_rsp.clear()
185
546733a2 186 def handle_error(self, dummy):
fe9160d5 187 self.putx([3, ['Error', 'Err', 'E']])
188
189 def checksum_is_valid(self, pid, data, checksum):
190 if self.lin_version == 2:
16d2031b 191 id_ = pid & 0x3F
fe9160d5 192
16d2031b 193 if id_ != 60 and id_ != 61:
fe9160d5 194 checksum += pid
195
196 for d in data:
197 checksum += d[2]
198
199 carry_bits = int(checksum / 256)
200 checksum += carry_bits
201
202 return checksum & 0xFF == 0xFF
203
204 @staticmethod
205 def calc_parity(pid):
16d2031b 206 id_ = [((pid & 0x3F) >> i) & 1 for i in range(8)]
fe9160d5 207
16d2031b
RJ
208 p0 = id_[0] ^ id_[1] ^ id_[2] ^ id_[4]
209 p1 = not (id_[1] ^ id_[3] ^ id_[4] ^ id_[5])
fe9160d5 210
211 return (p0 << 0) | (p1 << 1)
212
213 def decode(self, ss, es, data):
214 ptype, rxtx, pdata = data
215
216 self.ss_block, self.es_block = ss, es
217
218 # Ignore all UART packets except the actual data packets or BREAK.
219 if ptype == 'BREAK':
220 self.handle_break(pdata)
221 if ptype != 'DATA':
222 return
223
224 # We're only interested in the byte value (not individual bits).
225 pdata = pdata[0]
226
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.
232
546733a2
UH
233 handler = getattr(self, 'handle_%s' % self.fsm.state.lower())
234 handler(pdata)