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', 0],
49 ['Link', 'Link layer events (reset, presence, bit slots)'],
50 ['Network', 'Network layer events (device addressing)'],
51 ['Transfer', 'Transfer layer events'],
54 def __init__(self, **kwargs):
57 # Link layer variables
58 self.lnk_state = 'WAIT FOR FALLING EDGE'
59 self.lnk_event = 'NONE'
63 # Network layer variables
64 self.net_state = 'IDLE'
70 self.net_rom = 0x0000000000000000
71 # Transport layer variables
72 self.trn_state = 'IDLE'
74 def start(self, metadata):
75 self.samplerate = metadata['samplerate']
76 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire')
77 self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire')
79 # The width of the 1-Wire time base (30us) in number of samples.
80 # TODO: optimize this value
81 self.time_base = float(self.samplerate) * float(0.000030)
82 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK, ['time_base = %d' % self.time_base]])
87 def decode(self, ss, es, data):
88 for (self.samplenum, (owr, pwr)) in data:
89 # print ("DEBUG: sample = %d, owr = %d, pwr = %d, lnk_fall = %d, lnk_state = %s" % (self.samplenum, owr, pwr, self.lnk_fall, self.lnk_state))
94 self.lnk_event = "NONE"
96 if self.lnk_state == 'WAIT FOR FALLING EDGE':
97 # The start of a cycle is a falling edge.
99 # Save the sample number for the falling edge.
100 self.lnk_fall = self.samplenum
101 # Go to waiting for sample time
102 self.lnk_state = 'WAIT FOR DATA SAMPLE'
103 elif self.lnk_state == 'WAIT FOR DATA SAMPLE':
104 # Data should be sample one 'time unit' after a falling edge
105 if (self.samplenum - self.lnk_fall == 0.5*self.time_base):
106 self.lnk_bit = owr & 0x1
107 self.lnk_event = "DATA BIT"
108 if (self.lnk_bit) : self.lnk_state = 'WAIT FOR FALLING EDGE'
109 else : self.lnk_state = 'WAIT FOR RISING EDGE'
110 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK, ['BIT: %01x' % self.lnk_bit]])
111 elif self.lnk_state == 'WAIT FOR RISING EDGE':
112 # The end of a cycle is a rising edge.
114 # A reset cycle is longer than 8T.
115 if (self.samplenum - self.lnk_fall > 8*self.time_base):
116 # Save the sample number for the falling edge.
117 self.lnk_rise = self.samplenum
118 # Send a reset event to the next protocol layer.
119 self.lnk_event = "RESET"
120 self.lnk_state = "WAIT FOR PRESENCE DETECT"
121 self.put(self.lnk_fall, self.samplenum, self.out_proto, ['RESET'])
122 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK , ['RESET']])
123 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['RESET']])
125 self.lnk_fall = self.samplenum
126 # Otherwise this is assumed to be a data bit.
128 self.lnk_state = "WAIT FOR FALLING EDGE"
129 elif self.lnk_state == 'WAIT FOR PRESENCE DETECT':
130 # Data should be sample one 'time unit' after a falling edge
131 if (self.samplenum - self.lnk_rise == 2.5*self.time_base):
132 self.lnk_present = owr & 0x1
133 # Save the sample number for the falling edge.
134 if not (self.lnk_present) : self.lnk_fall = self.samplenum
135 # create presence detect event
136 #self.lnk_event = "PRESENCE DETECT"
137 if (self.lnk_present) : self.lnk_state = 'WAIT FOR FALLING EDGE'
138 else : self.lnk_state = 'WAIT FOR RISING EDGE'
139 present_str = "False" if self.lnk_present else "True"
140 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK , ['PRESENCE: ' + present_str]])
141 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['PRESENCE: ' + present_str]])
143 raise Exception('Invalid lnk_state: %d' % self.lnk_state)
148 if (self.lnk_event == "RESET"):
149 self.net_state = "COMMAND"
150 self.net_search = "P"
152 elif (self.net_state == "IDLE"):
154 elif (self.net_state == "COMMAND"):
155 if (self.collect_data(8)):
156 # self.put(self.lnk_fall, self.samplenum,
157 # self.out_proto, ['ROM COMMAND', self.net_data])
158 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: 0x%02x' % self.net_data]])
159 if (self.net_data == 0x33):
161 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM\'']])
162 self.net_state = "GET ROM"
163 elif (self.net_data == 0x0f):
165 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM ???\'']])
166 self.net_state = "GET ROM"
167 elif (self.net_data == 0xcc):
169 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SKIP ROM\'']])
170 self.net_state = "IDLE"
171 self.trn_state = "COMMAND"
172 elif (self.net_data == 0x55):
174 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'MATCH ROM\'']])
175 self.net_state = "GET ROM"
176 elif (self.net_data == 0xf0):
178 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SEARCH ROM\'']])
179 self.net_state = "SEARCH ROM"
180 elif (self.net_data == 0x3c):
182 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE SKIP ROM\'']])
183 self.net_state = "IDLE"
184 self.trn_state = "COMMAND"
185 elif (self.net_data == 0x69):
186 # OVERDRIVE MATCH ROM
187 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE MATCH ROM\'']])
188 self.net_state = "GET ROM"
189 elif (self.net_state == "GET ROM"):
190 # family code (1B) + serial number (6B) + CRC (1B)
191 if (self.collect_data((1+6+1)*8)):
192 self.net_rom = self.net_data & 0xffffffffffffffff
193 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_rom]])
194 self.net_state = "IDLE"
195 self.trn_state = "COMMAND"
196 elif (self.net_state == "SEARCH ROM"):
197 # family code (1B) + serial number (6B) + CRC (1B)
198 if (self.collect_search((1+6+1)*8)):
199 self.net_rom = self.net_data & 0xffffffffffffffff
200 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_rom]])
201 self.net_state = "IDLE"
202 self.trn_state = "COMMAND"
204 raise Exception('Invalid net_state: %s' % self.net_state)
209 if (self.lnk_event == "RESET"):
210 self.trn_state = "IDLE"
211 elif (self.trn_state == "IDLE"):
213 elif (self.trn_state == "COMMAND"):
214 if (self.collect_data(8)):
215 # self.put(self.lnk_fall, self.samplenum, self.out_proto, ['FUNCTION COMMAND', self.net_data])
216 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK , ['FUNCTION COMMAND: 0x%02x' % self.net_data]])
217 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_TRANSFER, ['FUNCTION COMMAND: 0x%02x' % self.net_data]])
218 if (self.net_data == 0x48):
220 self.trn_state = "TODO"
221 elif (self.net_data == 0x4e):
223 self.trn_state = "TODO"
224 elif (self.net_data == 0xbe):
226 self.trn_state = "TODO"
227 elif (self.net_data == 0xb8):
229 self.trn_state = "TODO"
230 elif (self.net_data == 0xb4):
232 self.trn_state = "TODO"
234 # unsupported commands
235 self.trn_state = "TODO"
236 elif (self.trn_state == "TODO"):
237 # self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK , ['TODO unsupported transfer state: %s' % self.trn_state]])
238 # self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_TRANSFER, ['TODO unsupported transfer state: %s' % self.trn_state]])
241 raise Exception('Invalid trn_state: %s' % self.trn_state)
243 # Link/Network layer data collector
244 def collect_data (self, length):
245 if (self.lnk_event == "DATA BIT"):
246 #print ("DEBUG: BIT=%d t0=%d t+=%d" % (self.lnk_bit, self.lnk_fall, self.samplenum))
247 self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
248 self.net_cnt = self.net_cnt + 1
249 if (self.net_cnt == length):
250 self.net_data = self.net_data & ((1<<length)-1)
258 # Link/Network layer search collector
259 def collect_search (self, length):
260 if (self.lnk_event == "DATA BIT"):
261 #print ("DEBUG: SEARCH=%s BIT=%d t0=%d t+=%d" % (self.net_search, self.lnk_bit, self.lnk_fall, self.samplenum))
262 if (self.net_search == "P"):
263 self.net_data_p = self.net_data_p & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
264 self.net_search = "N"
265 elif (self.net_search == "N"):
266 self.net_data_n = self.net_data_n & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
267 self.net_search = "D"
268 elif (self.net_search == "D"):
269 self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
270 self.net_search = "P"
271 self.net_cnt = self.net_cnt + 1
272 if (self.net_cnt == length):
273 self.net_data_p = self.net_data_p & ((1<<length)-1)
274 self.net_data_n = self.net_data_n & ((1<<length)-1)
275 self.net_data = self.net_data & ((1<<length)-1)
276 self.net_search = "P"