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