2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
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
23 import sigrokdecode as srd
25 # Annotation feed formats
30 class Decoder(srd.Decoder):
35 desc = '1-Wire bus and MicroLan'
40 {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire bus'},
43 {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power'},
46 'overdrive' : ['Overdrive', 1],
47 'cnt_normal_bit' : ['Time (in samplerate periods) for normal mode sample bit' , 0],
48 'cnt_normal_presence' : ['Time (in samplerate periods) for normal mode sample presence', 0],
49 'cnt_normal_reset' : ['Time (in samplerate periods) for normal mode reset' , 0],
50 'cnt_overdrive_bit' : ['Time (in samplerate periods) for overdrive mode sample bit' , 0],
51 'cnt_overdrive_presence': ['Time (in samplerate periods) for overdrive mode sample presence', 0],
52 'cnt_overdrive_reset' : ['Time (in samplerate periods) for overdrive mode reset' , 0],
55 ['Link', 'Link layer events (reset, presence, bit slots)'],
56 ['Network', 'Network layer events (device addressing)'],
57 ['Transport', 'Transport layer events'],
60 def __init__(self, **kwargs):
63 # Link layer variables
64 self.lnk_state = 'WAIT FOR FALLING EDGE'
65 self.lnk_event = 'NONE'
68 self.lnk_overdrive = 0
69 # Event timing variables
74 # Network layer variables
75 self.net_state = 'IDLE'
81 self.net_rom = 0x0000000000000000
83 def start(self, metadata):
84 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire')
85 self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire')
87 # check if samplerate is appropriate
88 self.samplerate = metadata['samplerate']
89 if (self.options['overdrive']):
90 self.put(0, 0, self.out_ann, [ANN_LINK, ['NOTE: Sample rate checks assume overdrive mode.']])
91 if (self.samplerate < 2000000):
92 self.put(0, 0, self.out_ann, [ANN_LINK, ['ERROR: Sampling rate is too low must be above 2MHz for proper overdrive mode decoding.']])
93 elif (self.samplerate < 5000000):
94 self.put(0, 0, self.out_ann, [ANN_LINK, ['WARNING: Sampling rate is suggested to be above 5MHz for proper overdrive mode decoding.']])
96 self.put(0, 0, self.out_ann, [ANN_LINK, ['NOTE: Sample rate checks assume normal mode only.']])
97 if (self.samplerate < 400000):
98 self.put(0, 0, self.out_ann, [ANN_LINK, ['ERROR: Sampling rate is too low must be above 400kHz for proper normal mode decoding.']])
99 elif (self.samplerate < 1000000):
100 self.put(0, 0, self.out_ann, [ANN_LINK, ['WARNING: Sampling rate is suggested to be above 1MHz for proper normal mode decoding.']])
102 # The default 1-Wire time base is 30us, this is used to calculate sampling times.
103 if (self.options['cnt_normal_bit']): self.cnt_normal_bit = self.options['cnt_normal_bit']
104 else: self.cnt_normal_bit = int(float(self.samplerate) * 0.000015) - 1 # 15ns
105 if (self.options['cnt_normal_presence']): self.cnt_normal_presence = self.options['cnt_normal_presence']
106 else: self.cnt_normal_presence = int(float(self.samplerate) * 0.000075) - 1 # 75ns
107 if (self.options['cnt_normal_reset']): self.cnt_normal_reset = self.options['cnt_normal_reset']
108 else: self.cnt_normal_reset = int(float(self.samplerate) * 0.000480) - 1 # 480ns
109 if (self.options['cnt_overdrive_bit']): self.cnt_overdrive_bit = self.options['cnt_overdrive_bit']
110 else: self.cnt_overdrive_bit = int(float(self.samplerate) * 0.000002) - 1 # 2ns
111 if (self.options['cnt_overdrive_presence']): self.cnt_overdrive_presence = self.options['cnt_overdrive_presence']
112 else: self.cnt_overdrive_presence = int(float(self.samplerate) * 0.000010) - 1 # 10ns
113 if (self.options['cnt_overdrive_reset']): self.cnt_overdrive_reset = self.options['cnt_overdrive_reset']
114 else: self.cnt_overdrive_reset = int(float(self.samplerate) * 0.000048) - 1 # 48ns
116 # Check if sample times are in the allowed range
117 time_min = float(self.cnt_normal_bit ) / self.samplerate
118 time_max = float(self.cnt_normal_bit+1) / self.samplerate
119 if ( (time_min < 0.000005) or (time_max > 0.000015) ) :
120 self.put(0, 0, self.out_ann, [ANN_LINK, ['WARNING: The normal mode data sample time interval (%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).' % (time_min*1000000, time_max*1000000)]])
121 time_min = float(self.cnt_normal_presence ) / self.samplerate
122 time_max = float(self.cnt_normal_presence+1) / self.samplerate
123 if ( (time_min < 0.0000681) or (time_max > 0.000075) ) :
124 self.put(0, 0, self.out_ann, [ANN_LINK, ['WARNING: The normal mode presence sample time interval (%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).' % (time_min*1000000, time_max*1000000)]])
125 time_min = float(self.cnt_overdrive_bit ) / self.samplerate
126 time_max = float(self.cnt_overdrive_bit+1) / self.samplerate
127 if ( (time_min < 0.000001) or (time_max > 0.000002) ) :
128 self.put(0, 0, self.out_ann, [ANN_LINK, ['WARNING: The overdrive mode data sample time interval (%2.1fus-%2.1fus) should be inside (1.0us, 2.0us).' % (time_min*1000000, time_max*1000000)]])
129 time_min = float(self.cnt_overdrive_presence ) / self.samplerate
130 time_max = float(self.cnt_overdrive_presence+1) / self.samplerate
131 if ( (time_min < 0.0000073) or (time_max > 0.000010) ) :
132 self.put(0, 0, self.out_ann, [ANN_LINK, ['WARNING: The overdrive mode presence sample time interval (%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).' % (time_min*1000000, time_max*1000000)]])
137 def decode(self, ss, es, data):
138 for (self.samplenum, (owr, pwr)) in data:
143 self.lnk_event = "NONE"
145 if self.lnk_state == 'WAIT FOR FALLING EDGE':
146 # The start of a cycle is a falling edge.
148 # Save the sample number for the falling edge.
149 self.lnk_fall = self.samplenum
150 # Go to waiting for sample time
151 self.lnk_state = 'WAIT FOR DATA SAMPLE'
152 elif self.lnk_state == 'WAIT FOR DATA SAMPLE':
154 if (self.lnk_overdrive): cnt = self.cnt_overdrive_bit
155 else : cnt = self.cnt_normal_bit
156 if (self.samplenum - self.lnk_fall == cnt):
157 self.lnk_bit = owr & 0x1
158 self.lnk_event = "DATA BIT"
159 if (self.lnk_bit): self.lnk_state = 'WAIT FOR FALLING EDGE'
160 else : self.lnk_state = 'WAIT FOR RISING EDGE'
161 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK, ['BIT: %01x' % self.lnk_bit]])
162 elif self.lnk_state == 'WAIT FOR RISING EDGE':
163 # The end of a cycle is a rising edge.
165 # Check if this was a reset cycle
166 if (self.samplenum - self.lnk_fall > self.cnt_normal_reset):
167 # Save the sample number for the falling edge.
168 self.lnk_rise = self.samplenum
169 # Send a reset event to the next protocol layer.
170 self.lnk_event = "RESET"
171 self.lnk_state = "WAIT FOR PRESENCE DETECT"
172 self.put(self.lnk_fall, self.samplenum, self.out_proto, ['RESET'])
173 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK , ['RESET']])
174 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK , ['RESET']])
176 self.lnk_fall = self.samplenum
177 elif ((self.samplenum - self.lnk_fall > self.cnt_overdrive_reset) and (self.lnk_overdrive)):
178 # Save the sample number for the falling edge.
179 self.lnk_rise = self.samplenum
180 # Send a reset event to the next protocol layer.
181 self.lnk_event = "RESET"
182 self.lnk_state = "WAIT FOR PRESENCE DETECT"
183 self.put(self.lnk_fall, self.samplenum, self.out_proto, ['RESET OVERDRIVE'])
184 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK , ['RESET OVERDRIVE']])
185 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK , ['RESET OVERDRIVE']])
187 self.lnk_fall = self.samplenum
188 # Otherwise this is assumed to be a data bit.
190 self.lnk_state = "WAIT FOR FALLING EDGE"
191 elif self.lnk_state == 'WAIT FOR PRESENCE DETECT':
192 # Sample presence status
193 if (self.lnk_overdrive): cnt = self.cnt_overdrive_presence
194 else : cnt = self.cnt_normal_presence
195 if (self.samplenum - self.lnk_rise == cnt):
196 self.lnk_present = owr & 0x1
197 # Save the sample number for the falling edge.
198 if not (self.lnk_present) : self.lnk_fall = self.samplenum
199 # create presence detect event
200 #self.lnk_event = "PRESENCE DETECT"
201 if (self.lnk_present) : self.lnk_state = 'WAIT FOR FALLING EDGE'
202 else : self.lnk_state = 'WAIT FOR RISING EDGE'
203 present_str = "False" if self.lnk_present else "True"
204 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK , ['PRESENCE: ' + present_str]])
205 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['PRESENCE: ' + present_str]])
207 raise Exception('Invalid lnk_state: %d' % self.lnk_state)
212 if (self.lnk_event == "RESET"):
213 self.net_state = "COMMAND"
214 self.net_search = "P"
216 elif (self.net_state == "IDLE"):
218 elif (self.net_state == "COMMAND"):
219 # Receiving and decoding a ROM command
220 if (self.onewire_collect(8)):
221 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: 0x%02x' % self.net_data]])
222 if (self.net_data == 0x33):
224 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM\'']])
225 self.net_state = "GET ROM"
226 elif (self.net_data == 0x0f):
227 # CONDITIONAL READ ROM
228 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'CONDITIONAL READ ROM\'']])
229 self.net_state = "GET ROM"
230 elif (self.net_data == 0xcc):
232 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SKIP ROM\'']])
233 self.net_state = "TRANSPORT"
234 elif (self.net_data == 0x55):
236 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'MATCH ROM\'']])
237 self.net_state = "GET ROM"
238 elif (self.net_data == 0xf0):
240 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SEARCH ROM\'']])
241 self.net_state = "SEARCH ROM"
242 elif (self.net_data == 0xec):
243 # CONDITIONAL SEARCH ROM
244 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'CONDITIONAL SEARCH ROM\'']])
245 self.net_state = "SEARCH ROM"
246 elif (self.net_data == 0x3c):
248 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE SKIP ROM\'']])
249 self.lnk_overdrive = 1
250 self.net_state = "TRANSPORT"
251 elif (self.net_data == 0x69):
252 # OVERDRIVE MATCH ROM
253 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE MATCH ROM\'']])
254 self.lnk_overdrive = 1
255 self.net_state = "GET ROM"
256 elif (self.net_state == "GET ROM"):
257 # A 64 bit device address is selected
258 # family code (1B) + serial number (6B) + CRC (1B)
259 if (self.onewire_collect(64)):
260 self.net_rom = self.net_data & 0xffffffffffffffff
261 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_rom]])
262 self.net_state = "TRANSPORT"
263 elif (self.net_state == "SEARCH ROM"):
264 # A 64 bit device address is searched for
265 # family code (1B) + serial number (6B) + CRC (1B)
266 if (self.onewire_search(64)):
267 self.net_rom = self.net_data & 0xffffffffffffffff
268 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_rom]])
269 self.net_state = "TRANSPORT"
270 elif (self.net_state == "TRANSPORT"):
271 # The transport layer is handled in byte sized units
272 if (self.onewire_collect(8)):
273 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_NETWORK , ['TRANSPORT: 0x%02x' % self.net_data]])
274 self.put(self.net_beg, self.net_end, self.out_ann, [ANN_TRANSPORT, ['TRANSPORT: 0x%02x' % self.net_data]])
275 self.put(self.net_beg, self.net_end, self.out_proto, ['transfer', self.net_data])
276 # TODO: Sending translort layer data to 1-Wire device models
278 raise Exception('Invalid net_state: %s' % self.net_state)
281 # Link/Network layer data collector
282 def onewire_collect (self, length):
283 if (self.lnk_event == "DATA BIT"):
284 # Storing the sampe this sequence begins with
285 if (self.net_cnt == 1):
286 self.net_beg = self.samplenum
287 self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
288 self.net_cnt = self.net_cnt + 1
289 # Storing the sampe this sequence ends with
290 # In case the full length of the sequence is received, return 1
291 if (self.net_cnt == length):
292 self.net_end = self.samplenum
293 self.net_data = self.net_data & ((1<<length)-1)
301 # Link/Network layer search collector
302 def onewire_search (self, length):
303 if (self.lnk_event == "DATA BIT"):
304 # Storing the sampe this sequence begins with
305 if ((self.net_cnt == 0) and (self.net_search == "P")):
306 self.net_beg = self.samplenum
307 # Master receives an original address bit
308 if (self.net_search == "P"):
309 self.net_data_p = self.net_data_p & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
310 self.net_search = "N"
311 # Master receives a complemented address bit
312 elif (self.net_search == "N"):
313 self.net_data_n = self.net_data_n & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
314 self.net_search = "D"
315 # Master transmits an address bit
316 elif (self.net_search == "D"):
317 self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
318 self.net_search = "P"
319 self.net_cnt = self.net_cnt + 1
320 # Storing the sampe this sequence ends with
321 # In case the full length of the sequence is received, return 1
322 if (self.net_cnt == length):
323 self.net_end = self.samplenum
324 self.net_data_p = self.net_data_p & ((1<<length)-1)
325 self.net_data_n = self.net_data_n & ((1<<length)-1)
326 self.net_data = self.net_data & ((1<<length)-1)
327 self.net_search = "P"