2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2016 Anthony Symons <antus@pcmhacking.net>
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
22 class SamplerateError(Exception):
26 return int (t * 1000.0 * 1000.0)
28 def normalize_time(t):
32 return '%d ms' % (t * 1000.0)
34 return '%d μs' % (t * 1000.0 * 1000.0)
35 elif t >= 0.000000001:
36 return '%d ns' % (t * 1000.0 * 1000.0 * 1000.0)
40 class Decoder(srd.Decoder):
43 name = 'SAE J1850 VPW'
44 longname = 'SAE J1850 VPW.'
45 desc = 'SAE J1850 Variable Pulse Width 1x and 4x.'
51 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
62 ('packets', 'Packets', (5,)),
63 ('bytes', 'Bytes', (4,)),
64 ('raws', 'Raws', (1,2,3,)),
65 ('times', 'Times', (0,)),
73 self.samplerate = None
74 self.byte = 0 # the byte offset in the packet
75 self.mode = 0 # for by packet decode
76 self.data = 0 # the current byte
77 self.datastart = 0 # sample number this byte started at
78 self.csa = 0 # track the last byte seperately to retrospectively add the CS marker
80 self.count = 0 # which bit number we are up to
81 self.active = 0 # which logic level is considered active
83 # vpw timings. ideal, min and max tollerances.
84 # From SAE J1850 1995 rev section 23.406
88 self.sofh = 245 # 240 by the spec, 245 so a 60us 4x sample will pass
91 self.longh = 170 # 164 by the spec but 170 for low sample rate tolerance.
93 self.shortl = 24 # 35 by the spec, 24 to allow down to 6us as measured in practice for 4x @ 1mhz sampling
96 self.spd = 1 # set to 4 when a 4x SOF is detected (VPW high speed frame)
98 def handle_bit(self, ss, es, b):
99 self.data |= (b << 7-self.count) # MSB-first
100 self.put(ss, es, self.out_ann, [1, ["%d" % b]])
104 self.csa = self.datastart # for CS
105 self.csb = self.samplenum # for CS
106 self.put(self.datastart, self.samplenum, self.out_ann, [4, ["%02X" % self.data]])
107 # add protocol parsing here
109 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Priority','Prio','P']])
111 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Destination','Dest','D']])
113 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Source','Src','S']])
115 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Mode','M']])
116 self.mode = self.data
117 elif self.mode == 1 and self.byte == 4: # mode 1 payload
118 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Pid','P']])
120 # prepare for next byte
123 self.byte = self.byte + 1 # track packet offset
124 self.count = self.count + 1
126 def metadata(self, key, value):
127 if key == srd.SRD_CONF_SAMPLERATE:
128 self.samplerate = value
131 self.out_ann = self.register(srd.OUTPUT_ANN)
134 if not self.samplerate:
135 raise SamplerateError('Cannot decode without samplerate.')
141 pin, = self.wait({0: 'e'})
145 txt = normalize_time(samples / self.samplerate)
146 self.put(ss, es, self.out_ann, [0, [txt]])
147 t = timeuf(samples / self.samplerate)
148 if self.state == 'IDLE': # detect and set speed from the size of sof
149 if pin == self.active and t in range(self.sofl , self.sofh):
150 self.put(ss, es, self.out_ann, [1, ['1X SOF', 'S1', 'S']])
155 elif pin == self.active and t in range(int(self.sofl / 4) , int(self.sofh / 4)):
156 self.put(ss, es, self.out_ann, [1, ['4X SOF', 'S4', '4']])
162 elif self.state == 'DATA':
163 if t >= int(self.ifs / self.spd):
165 self.put(ss, es, self.out_ann, [1, ["EOF/IFS", "E"]]) # EOF=239-280 IFS=281+
166 self.put(self.csa, self.csb, self.out_ann, [5, ['Checksum','CS','C']]) # retrospective print of CS
167 self.byte = 0 # reset packet offset
168 elif t in range(int(self.shortl / self.spd), int(self.shorth / self.spd)):
169 if pin == self.active:
170 self.handle_bit(ss, es, 1)
172 self.handle_bit(ss, es, 0)
173 elif t in range(int(self.longl / self.spd), int(self.longh / self.spd)):
174 if pin == self.active:
175 self.handle_bit(ss, es, 0)
177 self.handle_bit(ss, es, 1)