2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012 Iztok Jeras <iztok.jeras@gmail.com>
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.
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.
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
21 import sigrokdecode as srd
23 class SamplerateError(Exception):
26 class Decoder(srd.Decoder):
29 name = '1-Wire link layer'
30 longname = '1-Wire serial communication bus (link layer)'
31 desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
34 outputs = ['onewire_link']
36 {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire signal line'},
39 {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power supply pin'},
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},
64 ('warnings', 'Warnings'),
66 ('presence', 'Presence'),
67 ('overdrive', 'Overdrive mode notifications'),
70 ('bits', 'Bits', (0, 2, 3)),
71 ('info', 'Info', (4,)),
72 ('warnings', 'Warnings', (1,)),
76 self.put(0, 0, self.out_ann, data)
78 def putpb(self, data):
79 self.put(self.fall, self.samplenum, self.out_python, data)
82 self.put(self.fall, self.samplenum, self.out_ann, data)
85 self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, data)
87 def putfr(self, data):
88 self.put(self.fall, self.rise, self.out_ann, data)
90 def putprs(self, data):
91 self.put(self.rise, self.samplenum, self.out_python, data)
93 def putrs(self, data):
94 self.put(self.rise, self.samplenum, self.out_ann, data)
96 def __init__(self, **kwargs):
97 self.samplerate = None
99 self.state = 'WAIT FOR FALLING EDGE'
109 self.out_python = self.register(srd.OUTPUT_PYTHON)
110 self.out_ann = self.register(srd.OUTPUT_ANN)
112 def metadata(self, key, value):
113 if key != srd.SRD_CONF_SAMPLERATE:
115 self.samplerate = value
117 # Check if samplerate is appropriate.
118 if self.options['overdrive'] == 'yes':
119 if self.samplerate < 2000000:
120 self.putm([1, ['Sampling rate is too low. Must be above ' +
121 '2MHz for proper overdrive mode decoding.']])
122 elif self.samplerate < 5000000:
123 self.putm([1, ['Sampling rate is suggested to be above 5MHz ' +
124 'for proper overdrive mode decoding.']])
126 if self.samplerate < 400000:
127 self.putm([1, ['Sampling rate is too low. Must be above ' +
128 '400kHz for proper normal mode decoding.']])
129 elif (self.samplerate < 1000000):
130 self.putm([1, ['Sampling rate is suggested to be above ' +
131 '1MHz for proper normal mode decoding.']])
133 # The default 1-Wire time base is 30us. This is used to calculate
135 samplerate = float(self.samplerate)
137 x = float(self.options['cnt_normal_bit']) / 1000000.0
138 self.cnt_normal_bit = int(samplerate * x) - 1
139 x = float(self.options['cnt_normal_slot']) / 1000000.0
140 self.cnt_normal_slot = int(samplerate * x) - 1
141 x = float(self.options['cnt_normal_presence']) / 1000000.0
142 self.cnt_normal_presence = int(samplerate * x) - 1
143 x = float(self.options['cnt_normal_reset']) / 1000000.0
144 self.cnt_normal_reset = int(samplerate * x) - 1
145 x = float(self.options['cnt_overdrive_bit']) / 1000000.0
146 self.cnt_overdrive_bit = int(samplerate * x) - 1
147 x = float(self.options['cnt_overdrive_slot']) / 1000000.0
148 self.cnt_overdrive_slot = int(samplerate * x) - 1
149 x = float(self.options['cnt_overdrive_presence']) / 1000000.0
150 self.cnt_overdrive_presence = int(samplerate * x) - 1
151 x = float(self.options['cnt_overdrive_reset']) / 1000000.0
152 self.cnt_overdrive_reset = int(samplerate * x) - 1
154 # Organize values into lists.
155 self.cnt_bit = [self.cnt_normal_bit, self.cnt_overdrive_bit]
156 self.cnt_presence = [self.cnt_normal_presence, self.cnt_overdrive_presence]
157 self.cnt_reset = [self.cnt_normal_reset, self.cnt_overdrive_reset]
158 self.cnt_slot = [self.cnt_normal_slot, self.cnt_overdrive_slot]
160 # Check if sample times are in the allowed range.
162 time_min = float(self.cnt_normal_bit) / self.samplerate
163 time_max = float(self.cnt_normal_bit + 1) / self.samplerate
164 if (time_min < 0.000005) or (time_max > 0.000015):
165 self.putm([1, ['The normal mode data sample time interval ' +
166 '(%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).'
167 % (time_min * 1000000, time_max * 1000000)]])
169 time_min = float(self.cnt_normal_presence) / self.samplerate
170 time_max = float(self.cnt_normal_presence + 1) / self.samplerate
171 if (time_min < 0.0000681) or (time_max > 0.000075):
172 self.putm([1, ['The normal mode presence sample time interval ' +
173 '(%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).'
174 % (time_min * 1000000, time_max * 1000000)]])
176 time_min = float(self.cnt_overdrive_bit) / self.samplerate
177 time_max = float(self.cnt_overdrive_bit + 1) / self.samplerate
178 if (time_min < 0.000001) or (time_max > 0.000002):
179 self.putm([1, ['The overdrive mode data sample time interval ' +
180 '(%2.1fus-%2.1fus) should be inside (1.0us, 2.0us).'
181 % (time_min * 1000000, time_max * 1000000)]])
183 time_min = float(self.cnt_overdrive_presence) / self.samplerate
184 time_max = float(self.cnt_overdrive_presence + 1) / self.samplerate
185 if (time_min < 0.0000073) or (time_max > 0.000010):
186 self.putm([1, ['The overdrive mode presence sample time interval ' +
187 '(%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).'
188 % (time_min * 1000000, time_max * 1000000)]])
190 def decode(self, ss, es, data):
191 if not self.samplerate:
192 raise SamplerateError('Cannot decode without samplerate.')
193 for (self.samplenum, (owr, pwr)) in data:
195 if self.state == 'WAIT FOR FALLING EDGE':
196 # The start of a cycle is a falling edge.
199 # Save the sample number for the falling edge.
200 self.fall = self.samplenum
201 # Go to waiting for sample time.
202 self.state = 'WAIT FOR DATA SAMPLE'
203 elif self.state == 'WAIT FOR DATA SAMPLE':
205 t = self.samplenum - self.fall
206 if t == self.cnt_bit[self.overdrive]:
208 self.state = 'WAIT FOR DATA SLOT END'
209 elif self.state == 'WAIT FOR DATA SLOT END':
210 # A data slot ends in a recovery period, otherwise, this is
212 t = self.samplenum - self.fall
213 if t != self.cnt_slot[self.overdrive]:
217 # This seems to be a reset slot, wait for its end.
218 self.state = 'WAIT FOR RISING EDGE'
221 self.putb([0, ['Bit: %d' % self.bit, '%d' % self.bit]])
222 self.putpb(['BIT', self.bit])
224 # Checking the first command to see if overdrive mode
226 if self.bit_cnt <= 8:
227 self.command |= (self.bit << self.bit_cnt)
228 elif self.bit_cnt == 8 and self.command in [0x3c, 0x69]:
229 self.putx([4, ['Entering overdrive mode', 'Overdrive on']])
230 # Increment the bit counter.
232 # Wait for next slot.
233 self.state = 'WAIT FOR FALLING EDGE'
234 elif self.state == 'WAIT FOR RISING EDGE':
235 # The end of a cycle is a rising edge.
239 # Check if this was a reset cycle.
240 t = self.samplenum - self.fall
241 if t > self.cnt_normal_reset:
242 # Save the sample number for the rising edge.
243 self.rise = self.samplenum
244 self.putfr([2, ['Reset', 'Rst', 'R']])
245 self.state = 'WAIT FOR PRESENCE DETECT'
246 # Exit overdrive mode.
248 self.putx([4, ['Exiting overdrive mode', 'Overdrive off']])
250 # Clear command bit counter and data register.
253 elif (t > self.cnt_overdrive_reset) and self.overdrive:
254 # Save the sample number for the rising edge.
255 self.rise = self.samplenum
256 self.putfr([2, ['Reset', 'Rst', 'R']])
257 self.state = 'WAIT FOR PRESENCE DETECT'
258 # Otherwise this is assumed to be a data bit.
260 self.state = 'WAIT FOR FALLING EDGE'
261 elif self.state == 'WAIT FOR PRESENCE DETECT':
262 # Sample presence status.
263 t = self.samplenum - self.rise
264 if t == self.cnt_presence[self.overdrive]:
266 self.state = 'WAIT FOR RESET SLOT END'
267 elif self.state == 'WAIT FOR RESET SLOT END':
268 # A reset slot ends in a long recovery period.
269 t = self.samplenum - self.rise
270 if t != self.cnt_reset[self.overdrive]:
274 # This seems to be a reset slot, wait for its end.
275 self.state = 'WAIT FOR RISING EDGE'
278 p = 'false' if self.present else 'true'
279 self.putrs([3, ['Presence: %s' % p, 'Presence', 'Pres', 'P']])
280 self.putprs(['RESET/PRESENCE', not self.present])
282 # Wait for next slot.
283 self.state = 'WAIT FOR FALLING EDGE'