]> sigrok.org Git - libsigrokdecode.git/blame - decoders/onewire_link/pd.py
Change PD options to be a tuple of dictionaries.
[libsigrokdecode.git] / decoders / onewire_link / pd.py
CommitLineData
9cfb16e8 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
9cfb16e8
IJ
3##
4## Copyright (C) 2012 Iztok Jeras <iztok.jeras@gmail.com>
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
9cfb16e8
IJ
21import sigrokdecode as srd
22
23class Decoder(srd.Decoder):
24 api_version = 1
25 id = 'onewire_link'
26 name = '1-Wire link layer'
e7720d6c 27 longname = '1-Wire serial communication bus (link layer)'
9cfb16e8
IJ
28 desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
29 license = 'gplv2+'
30 inputs = ['logic']
31 outputs = ['onewire_link']
32 probes = [
3f302d51 33 {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire signal line'},
9cfb16e8
IJ
34 ]
35 optional_probes = [
3f302d51 36 {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power supply pin'},
9cfb16e8 37 ]
84c1c0b5
BV
38 options = (
39 {'id': 'overdrive',
40 'desc': 'Overdrive mode',
41 'default': 'no'},
497f196d 42 # Time options (specified in microseconds):
84c1c0b5
BV
43 {'id': 'cnt_normal_bit',
44 'desc': 'Normal mode sample bit time (μs)',
45 'default': 15},
46 {'id': 'cnt_normal_slot',
47 'desc': 'Normal mode data slot time (μs)',
48 'default': 60},
49 {'id': 'cnt_normal_presence',
50 'desc': 'Normal mode sample presence time (μs)',
51 'default': 75},
52 {'id': 'cnt_normal_reset',
53 'desc': 'Normal mode reset time (μs)',
54 'default': 480},
55 {'id': 'cnt_overdrive_bit',
56 'desc': 'Overdrive mode sample bit time (μs)',
57 'default': 2},
58 {'id': 'cnt_overdrive_slot',
59 'desc': 'Overdrive mode data slot time (μs)',
60 'default': 7.3},
61 {'id': 'cnt_overdrive_presence',
62 'desc': 'Overdrive mode sample presence time (μs)',
63 'default': 10},
64 {'id': 'cnt_overdrive_reset',
65 'desc': 'Overdrive mode reset time (μs)',
66 'default': 48},
67 )
9cfb16e8 68 annotations = [
938aa7fb
UH
69 ['bit', 'Bit'],
70 ['warnings', 'Warnings'],
6f507573
UH
71 ['reset', 'Reset'],
72 ['presence', 'Presence'],
938aa7fb 73 ['overdrive', 'Overdrive mode notifications'],
9cfb16e8 74 ]
8e11415b
UH
75 annotation_rows = (
76 ('bits', 'Bits', (0, 2, 3)),
77 ('info', 'Info', (4,)),
78 ('warnings', 'Warnings', (1,)),
79 )
9cfb16e8 80
ddeb9b32
UH
81 def putm(self, data):
82 self.put(0, 0, self.out_ann, data)
83
84 def putpb(self, data):
c515eed7 85 self.put(self.fall, self.samplenum, self.out_python, data)
ddeb9b32
UH
86
87 def putb(self, data):
88 self.put(self.fall, self.samplenum, self.out_ann, data)
89
90 def putx(self, data):
91 self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, data)
92
6f507573
UH
93 def putfr(self, data):
94 self.put(self.fall, self.rise, self.out_ann, data)
95
96 def putprs(self, data):
c515eed7 97 self.put(self.rise, self.samplenum, self.out_python, data)
6f507573
UH
98
99 def putrs(self, data):
100 self.put(self.rise, self.samplenum, self.out_ann, data)
101
9cfb16e8 102 def __init__(self, **kwargs):
f372d597 103 self.samplerate = None
9cfb16e8 104 self.samplenum = 0
e7720d6c 105 self.state = 'WAIT FOR FALLING EDGE'
9cfb16e8 106 self.present = 0
e7720d6c 107 self.bit = 0
99f5f3b5
IJ
108 self.bit_cnt = 0
109 self.command = 0
9cfb16e8 110 self.overdrive = 0
e7720d6c
UH
111 self.fall = 0
112 self.rise = 0
9cfb16e8 113
f372d597 114 def start(self):
c515eed7 115 self.out_python = self.register(srd.OUTPUT_PYTHON)
be465111 116 self.out_ann = self.register(srd.OUTPUT_ANN)
9cfb16e8 117
f372d597
BV
118 def metadata(self, key, value):
119 if key != srd.SRD_CONF_SAMPLERATE:
120 return
121 self.samplerate = value
e7720d6c
UH
122
123 # Check if samplerate is appropriate.
01520fa9 124 if self.options['overdrive'] == 'yes':
e7720d6c 125 if self.samplerate < 2000000:
ddeb9b32
UH
126 self.putm([1, ['Sampling rate is too low. Must be above ' +
127 '2MHz for proper overdrive mode decoding.']])
e7720d6c 128 elif self.samplerate < 5000000:
ddeb9b32
UH
129 self.putm([1, ['Sampling rate is suggested to be above 5MHz ' +
130 'for proper overdrive mode decoding.']])
9cfb16e8 131 else:
e7720d6c 132 if self.samplerate < 400000:
ddeb9b32
UH
133 self.putm([1, ['Sampling rate is too low. Must be above ' +
134 '400kHz for proper normal mode decoding.']])
9cfb16e8 135 elif (self.samplerate < 1000000):
ddeb9b32
UH
136 self.putm([1, ['Sampling rate is suggested to be above ' +
137 '1MHz for proper normal mode decoding.']])
9cfb16e8 138
e7720d6c
UH
139 # The default 1-Wire time base is 30us. This is used to calculate
140 # sampling times.
141 samplerate = float(self.samplerate)
497f196d
UH
142
143 x = float(self.options['cnt_normal_bit']) / 1000000.0
144 self.cnt_normal_bit = int(samplerate * x) - 1
145 x = float(self.options['cnt_normal_slot']) / 1000000.0
146 self.cnt_normal_slot = int(samplerate * x) - 1
147 x = float(self.options['cnt_normal_presence']) / 1000000.0
148 self.cnt_normal_presence = int(samplerate * x) - 1
149 x = float(self.options['cnt_normal_reset']) / 1000000.0
150 self.cnt_normal_reset = int(samplerate * x) - 1
151 x = float(self.options['cnt_overdrive_bit']) / 1000000.0
152 self.cnt_overdrive_bit = int(samplerate * x) - 1
153 x = float(self.options['cnt_overdrive_slot']) / 1000000.0
154 self.cnt_overdrive_slot = int(samplerate * x) - 1
155 x = float(self.options['cnt_overdrive_presence']) / 1000000.0
156 self.cnt_overdrive_presence = int(samplerate * x) - 1
157 x = float(self.options['cnt_overdrive_reset']) / 1000000.0
158 self.cnt_overdrive_reset = int(samplerate * x) - 1
9cfb16e8 159
e7720d6c
UH
160 # Organize values into lists.
161 self.cnt_bit = [self.cnt_normal_bit, self.cnt_overdrive_bit]
9cfb16e8 162 self.cnt_presence = [self.cnt_normal_presence, self.cnt_overdrive_presence]
e7720d6c
UH
163 self.cnt_reset = [self.cnt_normal_reset, self.cnt_overdrive_reset]
164 self.cnt_slot = [self.cnt_normal_slot, self.cnt_overdrive_slot]
165
166 # Check if sample times are in the allowed range.
167
168 time_min = float(self.cnt_normal_bit) / self.samplerate
169 time_max = float(self.cnt_normal_bit + 1) / self.samplerate
170 if (time_min < 0.000005) or (time_max > 0.000015):
ddeb9b32 171 self.putm([1, ['The normal mode data sample time interval ' +
e7720d6c
UH
172 '(%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).'
173 % (time_min * 1000000, time_max * 1000000)]])
174
175 time_min = float(self.cnt_normal_presence) / self.samplerate
176 time_max = float(self.cnt_normal_presence + 1) / self.samplerate
177 if (time_min < 0.0000681) or (time_max > 0.000075):
ddeb9b32 178 self.putm([1, ['The normal mode presence sample time interval ' +
e7720d6c
UH
179 '(%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).'
180 % (time_min * 1000000, time_max * 1000000)]])
181
182 time_min = float(self.cnt_overdrive_bit) / self.samplerate
183 time_max = float(self.cnt_overdrive_bit + 1) / self.samplerate
184 if (time_min < 0.000001) or (time_max > 0.000002):
ddeb9b32 185 self.putm([1, ['The overdrive mode data sample time interval ' +
e7720d6c
UH
186 '(%2.1fus-%2.1fus) should be inside (1.0us, 2.0us).'
187 % (time_min * 1000000, time_max * 1000000)]])
188
189 time_min = float(self.cnt_overdrive_presence) / self.samplerate
190 time_max = float(self.cnt_overdrive_presence + 1) / self.samplerate
191 if (time_min < 0.0000073) or (time_max > 0.000010):
ddeb9b32 192 self.putm([1, ['The overdrive mode presence sample time interval ' +
e7720d6c
UH
193 '(%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).'
194 % (time_min*1000000, time_max*1000000)]])
9cfb16e8 195
9cfb16e8 196 def decode(self, ss, es, data):
f372d597
BV
197 if self.samplerate is None:
198 raise Exception("Cannot decode without samplerate.")
9cfb16e8 199 for (self.samplenum, (owr, pwr)) in data:
9cfb16e8
IJ
200 # State machine.
201 if self.state == 'WAIT FOR FALLING EDGE':
202 # The start of a cycle is a falling edge.
48b59746
UH
203 if owr != 0:
204 continue
205 # Save the sample number for the falling edge.
206 self.fall = self.samplenum
207 # Go to waiting for sample time.
208 self.state = 'WAIT FOR DATA SAMPLE'
9cfb16e8 209 elif self.state == 'WAIT FOR DATA SAMPLE':
e7720d6c
UH
210 # Sample data bit.
211 t = self.samplenum - self.fall
212 if t == self.cnt_bit[self.overdrive]:
213 self.bit = owr
671cc300
IJ
214 self.state = 'WAIT FOR DATA SLOT END'
215 elif self.state == 'WAIT FOR DATA SLOT END':
e7720d6c
UH
216 # A data slot ends in a recovery period, otherwise, this is
217 # probably a reset.
218 t = self.samplenum - self.fall
48b59746
UH
219 if t != self.cnt_slot[self.overdrive]:
220 continue
221
222 if owr == 0:
223 # This seems to be a reset slot, wait for its end.
224 self.state = 'WAIT FOR RISING EDGE'
225 continue
226
4e980c20 227 self.putb([0, ['Bit: %d' % self.bit, '%d' % self.bit]])
ddeb9b32 228 self.putpb(['BIT', self.bit])
e7720d6c 229
48b59746
UH
230 # Checking the first command to see if overdrive mode
231 # should be entered.
232 if self.bit_cnt <= 8:
233 self.command |= (self.bit << self.bit_cnt)
234 elif self.bit_cnt == 8 and self.command in [0x3c, 0x69]:
4e980c20 235 self.putx([4, ['Entering overdrive mode', 'Overdrive on']])
48b59746
UH
236 # Increment the bit counter.
237 self.bit_cnt += 1
238 # Wait for next slot.
239 self.state = 'WAIT FOR FALLING EDGE'
9cfb16e8
IJ
240 elif self.state == 'WAIT FOR RISING EDGE':
241 # The end of a cycle is a rising edge.
48b59746
UH
242 if owr != 1:
243 continue
244
245 # Check if this was a reset cycle.
246 t = self.samplenum - self.fall
247 if t > self.cnt_normal_reset:
015dc33a 248 # Save the sample number for the rising edge.
48b59746 249 self.rise = self.samplenum
4e980c20 250 self.putfr([2, ['Reset', 'Rst', 'R']])
48b59746
UH
251 self.state = 'WAIT FOR PRESENCE DETECT'
252 # Exit overdrive mode.
253 if self.overdrive:
4e980c20 254 self.putx([4, ['Exiting overdrive mode', 'Overdrive off']])
48b59746
UH
255 self.overdrive = 0
256 # Clear command bit counter and data register.
257 self.bit_cnt = 0
258 self.command = 0
259 elif (t > self.cnt_overdrive_reset) and self.overdrive:
015dc33a 260 # Save the sample number for the rising edge.
48b59746 261 self.rise = self.samplenum
4e980c20 262 self.putfr([2, ['Reset', 'Rst', 'R']])
48b59746
UH
263 self.state = "WAIT FOR PRESENCE DETECT"
264 # Otherwise this is assumed to be a data bit.
265 else:
266 self.state = "WAIT FOR FALLING EDGE"
9cfb16e8 267 elif self.state == 'WAIT FOR PRESENCE DETECT':
e7720d6c
UH
268 # Sample presence status.
269 t = self.samplenum - self.rise
270 if t == self.cnt_presence[self.overdrive]:
671cc300
IJ
271 self.present = owr
272 self.state = 'WAIT FOR RESET SLOT END'
273 elif self.state == 'WAIT FOR RESET SLOT END':
48b59746 274 # A reset slot ends in a long recovery period.
e7720d6c 275 t = self.samplenum - self.rise
48b59746
UH
276 if t != self.cnt_reset[self.overdrive]:
277 continue
278
279 if owr == 0:
280 # This seems to be a reset slot, wait for its end.
281 self.state = 'WAIT FOR RISING EDGE'
282 continue
283
ddeb9b32 284 p = 'false' if self.present else 'true'
4e980c20 285 self.putrs([3, ['Presence: %s' % p, 'Presence', 'Pres', 'P']])
6f507573 286 self.putprs(['RESET/PRESENCE', not self.present])
ddeb9b32 287
48b59746
UH
288 # Wait for next slot.
289 self.state = 'WAIT FOR FALLING EDGE'
9cfb16e8 290 else:
e7720d6c 291 raise Exception('Invalid state: %s' % self.state)