]> sigrok.org Git - libsigrokdecode.git/blame - decoders/sae_j1850_vpw/pd.py
sae_j1850_vpw: update for API v3, adjust to recent requirements
[libsigrokdecode.git] / decoders / sae_j1850_vpw / pd.py
CommitLineData
3bd25dea
AS
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
41e0dd7a 17## along with this program; if not, see <http://www.gnu.org/licenses/>.
3bd25dea
AS
18##
19
20import sigrokdecode as srd
21
22class SamplerateError(Exception):
23 pass
24
25def timeuf(t):
26 return int (t * 1000.0 * 1000.0)
27
28def normalize_time(t):
29 if t >= 1.0:
30 return '%d s' % t
31 elif t >= 0.001:
32 return '%d ms' % (t * 1000.0)
33 elif t >= 0.000001:
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)
37 else:
38 return '%f' % t
39
40class Decoder(srd.Decoder):
41e0dd7a
GS
41 api_version = 3
42 id = 'sae_j1850_vpw'
43 name = 'SAE J1850 VPW'
44 longname = 'SAE J1850 VPW.'
45 desc = 'SAE J1850 Variable Pulse Width 1x and 4x.'
3bd25dea
AS
46 license = 'gplv2+'
47 inputs = ['logic']
41e0dd7a
GS
48 outputs = []
49 tags = ['Automotive']
3bd25dea
AS
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 = (
41e0dd7a
GS
62 ('packets', 'Packets', (5,)),
63 ('bytes', 'Bytes', (4,)),
64 ('raws', 'Raws', (1,2,3,)),
65 ('times', 'Times', (0,)),
3bd25dea
AS
66 )
67
41e0dd7a
GS
68 def __init__(self):
69 self.reset()
70
71 def reset(self):
3bd25dea
AS
72 self.state = 'IDLE'
73 self.samplerate = None
3bd25dea
AS
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
79 self.csb = 0
41e0dd7a 80 self.count = 0 # which bit number we are up to
3bd25dea 81 self.active = 0 # which logic level is considered active
41e0dd7a
GS
82
83 # vpw timings. ideal, min and max tollerances.
3bd25dea 84 # From SAE J1850 1995 rev section 23.406
41e0dd7a 85
3bd25dea
AS
86 self.sof = 200
87 self.sofl = 164
88 self.sofh = 245 # 240 by the spec, 245 so a 60us 4x sample will pass
89 self.long = 128
90 self.longl = 97
91 self.longh = 170 # 164 by the spec but 170 for low sample rate tolerance.
92 self.short = 64
93 self.shortl = 24 # 35 by the spec, 24 to allow down to 6us as measured in practice for 4x @ 1mhz sampling
94 self.shorth = 97
95 self.ifs = 240
96 self.spd = 1 # set to 4 when a 4x SOF is detected (VPW high speed frame)
41e0dd7a
GS
97
98 def handle_bit(self, ss, es, b):
3bd25dea 99 self.data |= (b << 7-self.count) # MSB-first
41e0dd7a 100 self.put(ss, es, self.out_ann, [1, ["%d" % b]])
3bd25dea 101 if self.count == 0:
41e0dd7a 102 self.datastart = ss
3bd25dea
AS
103 if self.count == 7:
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
108 if self.byte == 0:
109 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Priority','Prio','P']])
110 elif self.byte == 1:
111 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Destination','Dest','D']])
112 elif self.byte == 2:
113 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Source','Src','S']])
114 elif self.byte == 3:
115 self.put(self.datastart, self.samplenum, self.out_ann, [5, ['Mode','M']])
41e0dd7a 116 self.mode = self.data
3bd25dea
AS
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']])
41e0dd7a 119
3bd25dea
AS
120 # prepare for next byte
121 self.count = -1
122 self.data = 0
123 self.byte = self.byte + 1 # track packet offset
124 self.count = self.count + 1
41e0dd7a 125
3bd25dea
AS
126 def metadata(self, key, value):
127 if key == srd.SRD_CONF_SAMPLERATE:
128 self.samplerate = value
129
130 def start(self):
131 self.out_ann = self.register(srd.OUTPUT_ANN)
132
41e0dd7a 133 def decode(self):
3bd25dea
AS
134 if not self.samplerate:
135 raise SamplerateError('Cannot decode without samplerate.')
136
41e0dd7a
GS
137 self.wait({0: 'e'})
138 es = self.samplenum
139 while True:
140 ss = es
141 pin, = self.wait({0: 'e'})
142 es = self.samplenum
143
144 samples = es - ss
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']])
151 self.spd = 1
152 self.data = 0
153 self.count = 0
154 self.state = 'DATA'
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']])
157 self.spd = 4
158 self.data = 0
159 self.count = 0
160 self.state = 'DATA'
161
162 elif self.state == 'DATA':
163 if t >= int(self.ifs / self.spd):
164 self.state = 'IDLE'
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)
171 else:
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)
176 else:
177 self.handle_bit(ss, es, 1)