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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 import sigrokdecode as srd
23 class SamplerateError(Exception):
27 return int (t * 1000.0 * 1000.0)
29 def normalize_time(t):
33 return '%d ms' % (t * 1000.0)
35 return '%d μs' % (t * 1000.0 * 1000.0)
36 elif t >= 0.000000001:
37 return '%d ns' % (t * 1000.0 * 1000.0 * 1000.0)
41 class Decoder(srd.Decoder):
45 longname = 'J1850 VPW Decoder'
46 desc = 'Decode J1850 VPW 1x and 4x'
51 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
62 ('packet', 'Packet', (5,)),
63 ('byte', 'Byte', (4,)),
64 ('raw', 'Raw', (1,2,3,)),
65 ('time', 'Time', (0,)),
68 def __init__(self, **kwargs):
70 self.samplerate = None
72 self.last_samplenum = None
73 self.byte = 0 # the byte offset in the packet
74 self.mode = 0 # for by packet decode
75 self.data = 0 # the current byte
76 self.datastart = 0 # sample number this byte started at
77 self.csa = 0 # track the last byte seperately to retrospectively add the CS marker
79 self.count = 0 # which bit number we are up to
80 self.active = 0 # which logic level is considered active
82 # vpw timings. ideal, min and max tollerances.
83 # From SAE J1850 1995 rev section 23.406
87 self.sofh = 245 # 240 by the spec, 245 so a 60us 4x sample will pass
90 self.longh = 170 # 164 by the spec but 170 for low sample rate tolerance.
92 self.shortl = 24 # 35 by the spec, 24 to allow down to 6us as measured in practice for 4x @ 1mhz sampling
95 self.spd = 1 # set to 4 when a 4x SOF is detected (VPW high speed frame)
97 def handle_bit(self, b):
98 self.data |= (b << 7-self.count) # MSB-first
99 self.put(self.last_samplenum, self.samplenum, self.out_ann, [1, ["%d" % b]])
101 self.datastart = self.last_samplenum
103 self.csa = self.datastart # for CS
104 self.csb = self.samplenum # for CS
105 self.put(self.datastart, self.samplenum, self.out_ann, [4, ["%02X" % self.data]])
106 # add protocol parsing here
108 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Priority','Prio','P']])
110 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Destination','Dest','D']])
112 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Source','Src','S']])
114 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Mode','M']])
116 elif self.mode == 1 and self.byte == 4: # mode 1 payload
117 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Pid','P']])
119 # prepare for next byte
122 self.byte = self.byte + 1 # track packet offset
123 self.count = self.count + 1
125 def metadata(self, key, value):
126 if key == srd.SRD_CONF_SAMPLERATE:
127 self.samplerate = value
130 self.out_ann = self.register(srd.OUTPUT_ANN)
132 def decode(self, ss, es, data):
133 if not self.samplerate:
134 raise SamplerateError('Cannot decode without samplerate.')
136 for (self.samplenum, (pin,)) in data:
137 # Ignore identical samples early on (for performance reasons).
138 if self.oldpin == pin:
141 if self.oldpin is None:
143 self.last_samplenum = self.samplenum
146 if self.oldpin != pin:
147 samples = self.samplenum - self.last_samplenum
148 txt=normalize_time(samples / self.samplerate)
149 self.put(self.last_samplenum, self.samplenum, self.out_ann, [0, [txt]])
150 t=timeuf(samples / self.samplerate)
151 if self.state == 'IDLE': # detect and set speed from the size of sof
152 if pin==self.active and t in range(self.sofl , self.sofh):
153 self.put(self.last_samplenum, self.samplenum, self.out_ann, [1, ['1X SOF', 'S1', 'S']])
158 elif pin==self.active and t in range(int(self.sofl/4) , int(self.sofh/4)):
159 self.put(self.last_samplenum, self.samplenum, self.out_ann, [1, ['4X SOF', 'S4', '4']])
165 elif self.state == 'DATA':
166 if t >= int(self.ifs/self.spd):
168 self.put(self.last_samplenum, self.samplenum, self.out_ann, [1, ["EOF/IFS", "E"]]) # EOF=239-280 IFS=281+
169 self.put(self.csa, self.csb, self.out_ann, [5, ['Checksum','CS','C']]) # retrospective print of CS
170 self.byte = 0 # reset packet offset
171 elif t in range(int(self.shortl/self.spd), int(self.shorth/self.spd)):
176 elif t in range(int(self.longl/self.spd), int(self.longh/self.spd)):
182 # Store data for next round.
183 self.last_samplenum = self.samplenum