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