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 # 1-Wire protocol decoder (link layer)
23 import sigrokdecode as srd
25 class Decoder(srd.Decoder):
28 name = '1-Wire link layer'
29 longname = '1-Wire serial communication bus (link layer)'
30 desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
33 outputs = ['onewire_link']
35 {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire signal line'},
38 {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power supply pin'},
41 'overdrive': ['Overdrive mode', 'no'],
42 # Time options (specified in microseconds):
43 'cnt_normal_bit': ['Normal mode sample bit time (us)', 15],
44 'cnt_normal_slot': ['Normal mode data slot time (us)', 60],
45 'cnt_normal_presence': ['Normal mode sample presence time (us)', 75],
46 'cnt_normal_reset': ['Normal mode reset time (us)', 480],
47 'cnt_overdrive_bit': ['Overdrive mode sample bit time (us)', 2],
48 # 'cnt_overdrive_slot': ['Overdrive mode data slot time (us)', 7.3],
49 'cnt_overdrive_slot': ['Overdrive mode data slot time (us)', 7],
50 'cnt_overdrive_presence': ['Overdrive mode sample presence time (us)', 10],
51 'cnt_overdrive_reset': ['Overdrive mode reset time (us)', 48],
55 ['warnings', 'Warnings'],
57 ['presence', 'Presence'],
58 ['overdrive', 'Overdrive mode notifications'],
62 self.put(0, 0, self.out_ann, data)
64 def putpb(self, data):
65 self.put(self.fall, self.samplenum, self.out_proto, data)
68 self.put(self.fall, self.samplenum, self.out_ann, data)
71 self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, data)
73 def putfr(self, data):
74 self.put(self.fall, self.rise, self.out_ann, data)
76 def putprs(self, data):
77 self.put(self.rise, self.samplenum, self.out_proto, data)
79 def putrs(self, data):
80 self.put(self.rise, self.samplenum, self.out_ann, data)
82 def __init__(self, **kwargs):
84 self.state = 'WAIT FOR FALLING EDGE'
93 def start(self, metadata):
94 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire_link')
95 self.out_ann = self.add(srd.OUTPUT_ANN, 'onewire_link')
97 self.samplerate = metadata['samplerate']
99 # Check if samplerate is appropriate.
100 if self.options['overdrive'] == 'yes':
101 if self.samplerate < 2000000:
102 self.putm([1, ['Sampling rate is too low. Must be above ' +
103 '2MHz for proper overdrive mode decoding.']])
104 elif self.samplerate < 5000000:
105 self.putm([1, ['Sampling rate is suggested to be above 5MHz ' +
106 'for proper overdrive mode decoding.']])
108 if self.samplerate < 400000:
109 self.putm([1, ['Sampling rate is too low. Must be above ' +
110 '400kHz for proper normal mode decoding.']])
111 elif (self.samplerate < 1000000):
112 self.putm([1, ['Sampling rate is suggested to be above ' +
113 '1MHz for proper normal mode decoding.']])
115 # The default 1-Wire time base is 30us. This is used to calculate
117 samplerate = float(self.samplerate)
119 x = float(self.options['cnt_normal_bit']) / 1000000.0
120 self.cnt_normal_bit = int(samplerate * x) - 1
121 x = float(self.options['cnt_normal_slot']) / 1000000.0
122 self.cnt_normal_slot = int(samplerate * x) - 1
123 x = float(self.options['cnt_normal_presence']) / 1000000.0
124 self.cnt_normal_presence = int(samplerate * x) - 1
125 x = float(self.options['cnt_normal_reset']) / 1000000.0
126 self.cnt_normal_reset = int(samplerate * x) - 1
127 x = float(self.options['cnt_overdrive_bit']) / 1000000.0
128 self.cnt_overdrive_bit = int(samplerate * x) - 1
129 x = float(self.options['cnt_overdrive_slot']) / 1000000.0
130 self.cnt_overdrive_slot = int(samplerate * x) - 1
131 x = float(self.options['cnt_overdrive_presence']) / 1000000.0
132 self.cnt_overdrive_presence = int(samplerate * x) - 1
133 x = float(self.options['cnt_overdrive_reset']) / 1000000.0
134 self.cnt_overdrive_reset = int(samplerate * x) - 1
136 # Organize values into lists.
137 self.cnt_bit = [self.cnt_normal_bit, self.cnt_overdrive_bit]
138 self.cnt_presence = [self.cnt_normal_presence, self.cnt_overdrive_presence]
139 self.cnt_reset = [self.cnt_normal_reset, self.cnt_overdrive_reset]
140 self.cnt_slot = [self.cnt_normal_slot, self.cnt_overdrive_slot]
142 # Check if sample times are in the allowed range.
144 time_min = float(self.cnt_normal_bit) / self.samplerate
145 time_max = float(self.cnt_normal_bit + 1) / self.samplerate
146 if (time_min < 0.000005) or (time_max > 0.000015):
147 self.putm([1, ['The normal mode data sample time interval ' +
148 '(%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).'
149 % (time_min * 1000000, time_max * 1000000)]])
151 time_min = float(self.cnt_normal_presence) / self.samplerate
152 time_max = float(self.cnt_normal_presence + 1) / self.samplerate
153 if (time_min < 0.0000681) or (time_max > 0.000075):
154 self.putm([1, ['The normal mode presence sample time interval ' +
155 '(%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).'
156 % (time_min * 1000000, time_max * 1000000)]])
158 time_min = float(self.cnt_overdrive_bit) / self.samplerate
159 time_max = float(self.cnt_overdrive_bit + 1) / self.samplerate
160 if (time_min < 0.000001) or (time_max > 0.000002):
161 self.putm([1, ['The overdrive mode data sample time interval ' +
162 '(%2.1fus-%2.1fus) should be inside (1.0us, 2.0us).'
163 % (time_min * 1000000, time_max * 1000000)]])
165 time_min = float(self.cnt_overdrive_presence) / self.samplerate
166 time_max = float(self.cnt_overdrive_presence + 1) / self.samplerate
167 if (time_min < 0.0000073) or (time_max > 0.000010):
168 self.putm([1, ['The overdrive mode presence sample time interval ' +
169 '(%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).'
170 % (time_min*1000000, time_max*1000000)]])
175 def decode(self, ss, es, data):
176 for (self.samplenum, (owr, pwr)) in data:
178 if self.state == 'WAIT FOR FALLING EDGE':
179 # The start of a cycle is a falling edge.
182 # Save the sample number for the falling edge.
183 self.fall = self.samplenum
184 # Go to waiting for sample time.
185 self.state = 'WAIT FOR DATA SAMPLE'
186 elif self.state == 'WAIT FOR DATA SAMPLE':
188 t = self.samplenum - self.fall
189 if t == self.cnt_bit[self.overdrive]:
191 self.state = 'WAIT FOR DATA SLOT END'
192 elif self.state == 'WAIT FOR DATA SLOT END':
193 # A data slot ends in a recovery period, otherwise, this is
195 t = self.samplenum - self.fall
196 if t != self.cnt_slot[self.overdrive]:
200 # This seems to be a reset slot, wait for its end.
201 self.state = 'WAIT FOR RISING EDGE'
204 self.putb([0, ['Bit: %d' % self.bit, '%d' % self.bit]])
205 self.putpb(['BIT', self.bit])
207 # Checking the first command to see if overdrive mode
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.putx([4, ['Entering overdrive mode', 'Overdrive on']])
213 # Increment the bit counter.
215 # Wait for next slot.
216 self.state = 'WAIT FOR FALLING EDGE'
217 elif self.state == 'WAIT FOR RISING EDGE':
218 # The end of a cycle is a rising edge.
222 # Check if this was a reset cycle.
223 t = self.samplenum - self.fall
224 if t > self.cnt_normal_reset:
225 # Save the sample number for the rising edge.
226 self.rise = self.samplenum
227 self.putfr([2, ['Reset', 'Rst', 'R']])
228 self.state = 'WAIT FOR PRESENCE DETECT'
229 # Exit overdrive mode.
231 self.putx([4, ['Exiting overdrive mode', 'Overdrive off']])
233 # Clear command bit counter and data register.
236 elif (t > self.cnt_overdrive_reset) and self.overdrive:
237 # Save the sample number for the rising edge.
238 self.rise = self.samplenum
239 self.putfr([2, ['Reset', 'Rst', 'R']])
240 self.state = "WAIT FOR PRESENCE DETECT"
241 # Otherwise this is assumed to be a data bit.
243 self.state = "WAIT FOR FALLING EDGE"
244 elif self.state == 'WAIT FOR PRESENCE DETECT':
245 # Sample presence status.
246 t = self.samplenum - self.rise
247 if t == self.cnt_presence[self.overdrive]:
249 self.state = 'WAIT FOR RESET SLOT END'
250 elif self.state == 'WAIT FOR RESET SLOT END':
251 # A reset slot ends in a long recovery period.
252 t = self.samplenum - self.rise
253 if t != self.cnt_reset[self.overdrive]:
257 # This seems to be a reset slot, wait for its end.
258 self.state = 'WAIT FOR RISING EDGE'
261 p = 'false' if self.present else 'true'
262 self.putrs([3, ['Presence: %s' % p, 'Presence', 'Pres', 'P']])
263 self.putprs(['RESET/PRESENCE', not self.present])
265 # Wait for next slot.
266 self.state = 'WAIT FOR FALLING EDGE'
268 raise Exception('Invalid state: %s' % self.state)