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)
42 ANN_RAW, ANN_SOF, ANN_IFS, ANN_DATA, \
45 class Decoder(srd.Decoder):
48 name = 'SAE J1850 VPW'
49 longname = 'SAE J1850 VPW.'
50 desc = 'SAE J1850 Variable Pulse Width 1x and 4x.'
56 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
67 ('packets', 'Packets', (Ann.ANN_PACKET,)),
68 ('bytes', 'Bytes', (Ann.ANN_DATA,)),
69 ('raws', 'Raws', (Ann.ANN_RAW, Ann.ANN_SOF, Ann.ANN_IFS,)),
70 ('times', 'Times', (Ann.ANN_TIME,)),
78 self.samplerate = None
79 self.byte = 0 # the byte offset in the packet
80 self.mode = 0 # for by packet decode
81 self.data = 0 # the current byte
82 self.datastart = 0 # sample number this byte started at
83 self.csa = 0 # track the last byte seperately to retrospectively add the CS marker
85 self.count = 0 # which bit number we are up to
86 self.active = 0 # which logic level is considered active
88 # vpw timings. ideal, min and max tollerances.
89 # From SAE J1850 1995 rev section 23.406
93 self.sofh = 245 # 240 by the spec, 245 so a 60us 4x sample will pass
96 self.longh = 170 # 164 by the spec but 170 for low sample rate tolerance.
98 self.shortl = 24 # 35 by the spec, 24 to allow down to 6us as measured in practice for 4x @ 1mhz sampling
101 self.spd = 1 # set to 4 when a 4x SOF is detected (VPW high speed frame)
103 def handle_bit(self, ss, es, b):
104 self.data |= (b << 7-self.count) # MSB-first
105 self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ["%d" % b]])
109 self.csa = self.datastart # for CS
110 self.csb = self.samplenum # for CS
111 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_DATA, ["%02X" % self.data]])
112 # add protocol parsing here
114 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Priority','Prio','P']])
116 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Destination','Dest','D']])
118 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Source','Src','S']])
120 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Mode','M']])
121 self.mode = self.data
122 elif self.mode == 1 and self.byte == 4: # mode 1 payload
123 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Pid','P']])
125 # prepare for next byte
128 self.byte = self.byte + 1 # track packet offset
129 self.count = self.count + 1
131 def metadata(self, key, value):
132 if key == srd.SRD_CONF_SAMPLERATE:
133 self.samplerate = value
136 self.out_ann = self.register(srd.OUTPUT_ANN)
139 if not self.samplerate:
140 raise SamplerateError('Cannot decode without samplerate.')
146 pin, = self.wait({0: 'e'})
150 txt = normalize_time(samples / self.samplerate)
151 self.put(ss, es, self.out_ann, [Ann.ANN_TIME, [txt]])
152 t = timeuf(samples / self.samplerate)
153 if self.state == 'IDLE': # detect and set speed from the size of sof
154 if pin == self.active and t in range(self.sofl , self.sofh):
155 self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ['1X SOF', 'S1', 'S']])
160 elif pin == self.active and t in range(int(self.sofl / 4) , int(self.sofh / 4)):
161 self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ['4X SOF', 'S4', '4']])
167 elif self.state == 'DATA':
168 if t >= int(self.ifs / self.spd):
170 self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ["EOF/IFS", "E"]]) # EOF=239-280 IFS=281+
171 self.put(self.csa, self.csb, self.out_ann, [Ann.ANN_PACKET, ['Checksum','CS','C']]) # retrospective print of CS
172 self.byte = 0 # reset packet offset
173 elif t in range(int(self.shortl / self.spd), int(self.shorth / self.spd)):
174 if pin == self.active:
175 self.handle_bit(ss, es, 1)
177 self.handle_bit(ss, es, 0)
178 elif t in range(int(self.longl / self.spd), int(self.longh / self.spd)):
179 if pin == self.active:
180 self.handle_bit(ss, es, 0)
182 self.handle_bit(ss, es, 1)