]> sigrok.org Git - libsigrokdecode.git/blame - decoders/sae_j1850_vpw/pd.py
sae_j1850_vpw: drop the part which duplicates the timing decoder
[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
0e15a126 28class Ann:
0e15a126 29 ANN_RAW, ANN_SOF, ANN_IFS, ANN_DATA, \
fcf7bf48 30 ANN_PACKET = range(5)
0e15a126 31
3bd25dea 32class Decoder(srd.Decoder):
41e0dd7a
GS
33 api_version = 3
34 id = 'sae_j1850_vpw'
35 name = 'SAE J1850 VPW'
36 longname = 'SAE J1850 VPW.'
37 desc = 'SAE J1850 Variable Pulse Width 1x and 4x.'
3bd25dea
AS
38 license = 'gplv2+'
39 inputs = ['logic']
41e0dd7a
GS
40 outputs = []
41 tags = ['Automotive']
3bd25dea
AS
42 channels = (
43 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
44 )
45 annotations = (
3bd25dea
AS
46 ('raw', 'Raw'),
47 ('sof', 'SOF'),
48 ('ifs', 'EOF/IFS'),
49 ('data', 'Data'),
50 ('packet', 'Packet'),
51 )
52 annotation_rows = (
0e15a126 53 ('raws', 'Raws', (Ann.ANN_RAW, Ann.ANN_SOF, Ann.ANN_IFS,)),
fcf7bf48
GS
54 ('bytes', 'Bytes', (Ann.ANN_DATA,)),
55 ('packets', 'Packets', (Ann.ANN_PACKET,)),
3bd25dea
AS
56 )
57
41e0dd7a
GS
58 def __init__(self):
59 self.reset()
60
61 def reset(self):
3bd25dea
AS
62 self.state = 'IDLE'
63 self.samplerate = None
3bd25dea
AS
64 self.byte = 0 # the byte offset in the packet
65 self.mode = 0 # for by packet decode
66 self.data = 0 # the current byte
67 self.datastart = 0 # sample number this byte started at
68 self.csa = 0 # track the last byte seperately to retrospectively add the CS marker
69 self.csb = 0
41e0dd7a 70 self.count = 0 # which bit number we are up to
3bd25dea 71 self.active = 0 # which logic level is considered active
41e0dd7a
GS
72
73 # vpw timings. ideal, min and max tollerances.
3bd25dea 74 # From SAE J1850 1995 rev section 23.406
41e0dd7a 75
3bd25dea
AS
76 self.sof = 200
77 self.sofl = 164
78 self.sofh = 245 # 240 by the spec, 245 so a 60us 4x sample will pass
79 self.long = 128
80 self.longl = 97
81 self.longh = 170 # 164 by the spec but 170 for low sample rate tolerance.
82 self.short = 64
83 self.shortl = 24 # 35 by the spec, 24 to allow down to 6us as measured in practice for 4x @ 1mhz sampling
84 self.shorth = 97
85 self.ifs = 240
86 self.spd = 1 # set to 4 when a 4x SOF is detected (VPW high speed frame)
41e0dd7a
GS
87
88 def handle_bit(self, ss, es, b):
3bd25dea 89 self.data |= (b << 7-self.count) # MSB-first
0e15a126 90 self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ["%d" % b]])
3bd25dea 91 if self.count == 0:
41e0dd7a 92 self.datastart = ss
3bd25dea
AS
93 if self.count == 7:
94 self.csa = self.datastart # for CS
95 self.csb = self.samplenum # for CS
0e15a126 96 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_DATA, ["%02X" % self.data]])
3bd25dea
AS
97 # add protocol parsing here
98 if self.byte == 0:
0e15a126 99 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Priority','Prio','P']])
3bd25dea 100 elif self.byte == 1:
0e15a126 101 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Destination','Dest','D']])
3bd25dea 102 elif self.byte == 2:
0e15a126 103 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Source','Src','S']])
3bd25dea 104 elif self.byte == 3:
0e15a126 105 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Mode','M']])
41e0dd7a 106 self.mode = self.data
3bd25dea 107 elif self.mode == 1 and self.byte == 4: # mode 1 payload
0e15a126 108 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Pid','P']])
41e0dd7a 109
3bd25dea
AS
110 # prepare for next byte
111 self.count = -1
112 self.data = 0
113 self.byte = self.byte + 1 # track packet offset
114 self.count = self.count + 1
41e0dd7a 115
3bd25dea
AS
116 def metadata(self, key, value):
117 if key == srd.SRD_CONF_SAMPLERATE:
118 self.samplerate = value
119
120 def start(self):
121 self.out_ann = self.register(srd.OUTPUT_ANN)
122
41e0dd7a 123 def decode(self):
3bd25dea
AS
124 if not self.samplerate:
125 raise SamplerateError('Cannot decode without samplerate.')
126
41e0dd7a
GS
127 self.wait({0: 'e'})
128 es = self.samplenum
129 while True:
130 ss = es
131 pin, = self.wait({0: 'e'})
132 es = self.samplenum
133
134 samples = es - ss
41e0dd7a
GS
135 t = timeuf(samples / self.samplerate)
136 if self.state == 'IDLE': # detect and set speed from the size of sof
137 if pin == self.active and t in range(self.sofl , self.sofh):
0e15a126 138 self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ['1X SOF', 'S1', 'S']])
41e0dd7a
GS
139 self.spd = 1
140 self.data = 0
141 self.count = 0
142 self.state = 'DATA'
143 elif pin == self.active and t in range(int(self.sofl / 4) , int(self.sofh / 4)):
0e15a126 144 self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ['4X SOF', 'S4', '4']])
41e0dd7a
GS
145 self.spd = 4
146 self.data = 0
147 self.count = 0
148 self.state = 'DATA'
149
150 elif self.state == 'DATA':
151 if t >= int(self.ifs / self.spd):
152 self.state = 'IDLE'
0e15a126
GS
153 self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ["EOF/IFS", "E"]]) # EOF=239-280 IFS=281+
154 self.put(self.csa, self.csb, self.out_ann, [Ann.ANN_PACKET, ['Checksum','CS','C']]) # retrospective print of CS
41e0dd7a
GS
155 self.byte = 0 # reset packet offset
156 elif t in range(int(self.shortl / self.spd), int(self.shorth / self.spd)):
157 if pin == self.active:
158 self.handle_bit(ss, es, 1)
159 else:
160 self.handle_bit(ss, es, 0)
161 elif t in range(int(self.longl / self.spd), int(self.longh / self.spd)):
162 if pin == self.active:
163 self.handle_bit(ss, es, 0)
164 else:
165 self.handle_bit(ss, es, 1)