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