]> sigrok.org Git - libsigrokdecode.git/blob - decoders/sae_j1850_vpw/pd.py
srd: add TODO comment on the SIGROKDECODE_DIR env var's motivation
[libsigrokdecode.git] / decoders / sae_j1850_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, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21
22 class SamplerateError(Exception):
23     pass
24
25 def timeuf(t):
26     return int (t * 1000.0 * 1000.0)
27
28 class Ann:
29     ANN_RAW, ANN_SOF, ANN_IFS, ANN_DATA, \
30     ANN_PACKET = range(5)
31
32 class Decoder(srd.Decoder):
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.'
38     license = 'gplv2+'
39     inputs = ['logic']
40     outputs = []
41     tags = ['Automotive']
42     channels = (
43         {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
44     )
45     annotations = (
46         ('raw', 'Raw'),
47         ('sof', 'SOF'),
48         ('ifs', 'EOF/IFS'),
49         ('data', 'Data'),
50         ('packet', 'Packet'),
51     )
52     annotation_rows = (
53         ('raws', 'Raws', (Ann.ANN_RAW, Ann.ANN_SOF, Ann.ANN_IFS,)),
54         ('bytes', 'Bytes', (Ann.ANN_DATA,)),
55         ('packets', 'Packets', (Ann.ANN_PACKET,)),
56     )
57
58     def __init__(self):
59         self.reset()
60
61     def reset(self):
62         self.state = 'IDLE'
63         self.samplerate = None
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
70         self.count = 0     # which bit number we are up to
71         self.active = 0    # which logic level is considered active
72
73         # vpw timings. ideal, min and max tollerances.
74         # From SAE J1850 1995 rev section 23.406
75
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)
87
88     def handle_bit(self, ss, es, b):
89         self.data |= (b << 7-self.count) # MSB-first
90         self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ["%d" % b]])
91         if self.count == 0:
92             self.datastart = ss
93         if self.count == 7:
94             self.csa = self.datastart # for CS
95             self.csb = self.samplenum # for CS
96             self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_DATA, ["%02X" % self.data]])
97             # add protocol parsing here
98             if self.byte == 0:
99                 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Priority','Prio','P']])
100             elif self.byte == 1:
101                 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Destination','Dest','D']])
102             elif self.byte == 2:
103                 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Source','Src','S']])
104             elif self.byte == 3:
105                 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Mode','M']])
106                 self.mode = self.data
107             elif self.mode == 1 and self.byte == 4: # mode 1 payload
108                 self.put(self.datastart, self.samplenum, self.out_ann, [Ann.ANN_PACKET, ['Pid','P']])
109
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
115
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
123     def decode(self):
124         if not self.samplerate:
125             raise SamplerateError('Cannot decode without samplerate.')
126
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
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):
138                     self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ['1X SOF', 'S1', 'S']])
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)):
144                     self.put(ss, es, self.out_ann, [Ann.ANN_RAW, ['4X SOF', 'S4', '4']])
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'
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
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)