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