]> sigrok.org Git - libsigrokdecode.git/blob - decoders/vpw/pd.py
a1dd8ec3b9d785529066e4d2dab1aebcd61d09a7
[libsigrokdecode.git] / decoders / vpw / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2016 Anthony Symons <antus@pcmhacking.net>
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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 import sigrokdecode as srd
22
23 class SamplerateError(Exception):
24     pass
25
26 def timeuf(t):
27     return int (t * 1000.0 * 1000.0)
28
29 def normalize_time(t):
30     if t >= 1.0:
31         return '%d s' % t
32     elif t >= 0.001:
33         return '%d ms' % (t * 1000.0)
34     elif t >= 0.000001:
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)
38     else:
39         return '%f' % t
40
41 class Decoder(srd.Decoder):
42     api_version = 2
43     id = 'vpw'
44     name = 'VPW'
45     longname = 'J1850 VPW Decoder'
46     desc = 'Decode J1850 VPW 1x and 4x'
47     license = 'gplv2+'
48     inputs = ['logic']
49     outputs = ['timing']
50     channels = (
51         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
52     )
53     annotations = (
54         ('time', 'Time'),
55         ('raw', 'Raw'),
56         ('sof', 'SOF'),
57         ('ifs', 'EOF/IFS'),
58         ('data', 'Data'),
59         ('packet', 'Packet'),
60     )
61     annotation_rows = (
62         ('packet', 'Packet', (5,)),
63         ('byte', 'Byte', (4,)),
64         ('raw', 'Raw', (1,2,3,)),
65         ('time', 'Time', (0,)),
66     )
67
68     def __init__(self, **kwargs):
69         self.state = 'IDLE'
70         self.samplerate = None
71         self.oldpin = 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
78         self.csb = 0
79         self.count = 0     # which bit number we are up to 
80         self.active = 0    # which logic level is considered active
81         
82         # vpw timings. ideal, min and max tollerances. 
83         # From SAE J1850 1995 rev section 23.406
84         
85         self.sof = 200
86         self.sofl = 164
87         self.sofh = 245  # 240 by the spec, 245 so a 60us 4x sample will pass
88         self.long = 128
89         self.longl = 97
90         self.longh = 170 # 164 by the spec but 170 for low sample rate tolerance.
91         self.short = 64
92         self.shortl = 24 # 35 by the spec, 24 to allow down to 6us as measured in practice for 4x @ 1mhz sampling
93         self.shorth = 97
94         self.ifs = 240
95         self.spd = 1     # set to 4 when a 4x SOF is detected (VPW high speed frame)
96   
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]])
100         if self.count == 0:
101             self.datastart = self.last_samplenum
102         if self.count == 7:
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
107             if self.byte == 0:
108                 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Priority','Prio','P']])
109             elif self.byte == 1:
110                 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Destination','Dest','D']])
111             elif self.byte == 2:
112                 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Source','Src','S']])
113             elif self.byte == 3:
114                 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Mode','M']])
115                 self.mode=self.data
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']])
118             
119             # prepare for next byte
120             self.count = -1
121             self.data = 0
122             self.byte = self.byte + 1 # track packet offset
123         self.count = self.count + 1
124     
125     def metadata(self, key, value):
126         if key == srd.SRD_CONF_SAMPLERATE:
127             self.samplerate = value
128
129     def start(self):
130         self.out_ann = self.register(srd.OUTPUT_ANN)
131
132     def decode(self, ss, es, data):
133         if not self.samplerate:
134             raise SamplerateError('Cannot decode without samplerate.')
135
136         for (self.samplenum, (pin,)) in data:
137             # Ignore identical samples early on (for performance reasons).
138             if self.oldpin == pin:
139                 continue
140
141             if self.oldpin is None:
142                 self.oldpin = pin
143                 self.last_samplenum = self.samplenum
144                 continue
145
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']])
154                         self.spd = 1
155                         self.data = 0
156                         self.count = 0
157                         self.state = 'DATA'
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']])
160                         self.spd = 4
161                         self.data = 0
162                         self.count = 0
163                         self.state = 'DATA'
164                         
165                 elif self.state == 'DATA':
166                     if t >= int(self.ifs/self.spd):
167                         self.state = 'IDLE'
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)):
172                         if pin==self.active:
173                             self.handle_bit(1)
174                         else:
175                             self.handle_bit(0)
176                     elif t in range(int(self.longl/self.spd), int(self.longh/self.spd)):
177                         if pin==self.active:
178                             self.handle_bit(0)
179                         else:
180                             self.handle_bit(1)
181
182                 # Store data for next round.
183                 self.last_samplenum = self.samplenum
184                 self.oldpin = pin