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