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 = 'ROM COMMAND'
65 self.net_event = 'NONE'
71 # Transport layer variables
72 self.trn_state = 'WAIT FOR EVENT'
73 self.trn_event = 'NONE'
75 def start(self, metadata):
76 self.samplerate = metadata['samplerate']
77 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire')
78 self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire')
80 # The width of the 1-Wire time base (30us) in number of samples.
81 # TODO: optimize this value
82 self.time_base = float(self.samplerate) * float(0.000030)
83 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK, ['time_base = %d' % self.time_base]])
88 def decode(self, ss, es, data):
89 for (self.samplenum, (owr, pwr)) in data:
90 # print ("DEBUG: sample = %d, owr = %d, pwr = %d, lnk_fall = %d, lnk_state = %s" % (self.samplenum, owr, pwr, self.lnk_fall, self.lnk_state))
95 self.lnk_event = "NONE"
97 if self.lnk_state == 'WAIT FOR FALLING EDGE':
98 # The start of a cycle is a falling edge.
100 # Save the sample number for the falling edge.
101 self.lnk_fall = self.samplenum
102 # Go to waiting for sample time
103 self.lnk_state = 'WAIT FOR DATA SAMPLE'
104 elif self.lnk_state == 'WAIT FOR DATA SAMPLE':
105 # Data should be sample one 'time unit' after a falling edge
106 if (self.samplenum - self.lnk_fall == 0.5*self.time_base):
107 self.lnk_bit = owr & 0x1
108 self.lnk_event = "DATA BIT"
109 if (self.lnk_bit) : self.lnk_state = 'WAIT FOR FALLING EDGE'
110 else : self.lnk_state = 'WAIT FOR RISING EDGE'
111 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK, ['BIT: %01x' % self.lnk_bit]])
112 elif self.lnk_state == 'WAIT FOR RISING EDGE':
113 # The end of a cycle is a rising edge.
115 # A reset cycle is longer than 8T.
116 if (self.samplenum - self.lnk_fall > 8*self.time_base):
117 # Save the sample number for the falling edge.
118 self.lnk_rise = self.samplenum
119 # Send a reset event to the next protocol layer.
120 self.lnk_event = "RESET"
121 self.lnk_state = "WAIT FOR PRESENCE DETECT"
122 self.put(self.lnk_fall, self.samplenum, self.out_proto, ['RESET'])
123 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK , ['RESET']])
124 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['RESET']])
126 self.lnk_fall = self.samplenum
127 # Otherwise this is assumed to be a data bit.
129 self.lnk_state = "WAIT FOR FALLING EDGE"
130 elif self.lnk_state == 'WAIT FOR PRESENCE DETECT':
131 # Data should be sample one 'time unit' after a falling edge
132 if (self.samplenum - self.lnk_rise == 2.5*self.time_base):
133 self.lnk_present = owr & 0x1
134 # Save the sample number for the falling edge.
135 if not (self.lnk_present) : self.lnk_fall = self.samplenum
136 # create presence detect event
137 #self.lnk_event = "PRESENCE DETECT"
138 if (self.lnk_present) : self.lnk_state = 'WAIT FOR FALLING EDGE'
139 else : self.lnk_state = 'WAIT FOR RISING EDGE'
140 present_str = "False" if self.lnk_present else "True"
141 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK , ['PRESENCE: ' + present_str]])
142 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['PRESENCE: ' + present_str]])
144 raise Exception('Invalid lnk_state: %d' % self.lnk_state)
149 self.net_event = "RESET"
151 if (self.lnk_event == "RESET"):
152 self.net_state = "ROM COMMAND"
153 self.net_search = "P"
155 elif (self.lnk_event == "DATA BIT"):
156 if (self.net_state == "ROM COMMAND"):
157 if (self.collect_data(8)):
158 # self.put(self.lnk_fall, self.samplenum,
159 # self.out_proto, ['ROM COMMAND', self.net_data])
160 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: 0x%02x' % self.net_data]])
161 if (self.net_data == 0x33):
163 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM\'']])
164 self.net_state = "ADDRESS"
165 elif (self.net_data == 0x0f):
167 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM ???\'']])
168 self.net_state = "ADDRESS"
169 elif (self.net_data == 0xcc):
171 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SKIP ROM\'']])
172 self.net_state = "CONTROL COMMAND"
173 elif (self.net_data == 0x55):
175 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'MATCH ROM\'']])
176 self.net_state = "ADDRESS"
177 elif (self.net_data == 0xf0):
179 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SEARCH ROM\'']])
180 self.net_state = "SEARCH"
181 elif (self.net_data == 0x3c):
183 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE SKIP ROM\'']])
184 self.net_state = "CONTROL 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 = "ADDRESS"
189 elif (self.net_state == "ADDRESS"):
190 # family code (1B) + serial number (6B) + CRC (1B)
191 if (self.collect_data((1+6+1)*8)):
192 self.net_address = self.net_data & 0xffffffffffffffff
193 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_address]])
194 self.net_state = "CONTROL COMMAND"
195 elif (self.net_state == "SEARCH"):
196 # family code (1B) + serial number (6B) + CRC (1B)
197 if (self.collect_search((1+6+1)*8)):
198 self.net_address = self.net_data & 0xffffffffffffffff
199 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_address]])
200 self.net_state = "CONTROL COMMAND"
201 elif (self.net_state == "CONTROL COMMAND"):
202 if (self.collect_data(8)):
203 # self.put(self.lnk_fall, self.samplenum,
204 # self.out_proto, ['LNK: COMMAND', self.net_data])
205 self.put(self.lnk_fall, self.samplenum, self.out_ann,
206 [ANN_TRANSFER, ['TRANSFER: FUNCTION COMMAND: 0x' + hex(self.net_data)]])
207 if (self.net_data == 0x48):
209 self.net_state = "TODO"
210 elif (self.net_data == 0x4e):
212 self.net_state = "TODO"
213 elif (self.net_data == 0xbe):
215 self.net_state = "TODO"
216 elif (self.net_data == 0xb8):
218 self.net_state = "TODO"
219 elif (self.net_data == 0xb4):
221 self.net_state = "TODO"
223 # unsupported commands
224 self.net_state = "UNDEFINED"
225 elif (self.net_state == "UNDEFINED"):
228 raise Exception('Invalid net_state: %s' % self.net_state)
229 elif (self.lnk_event != "NONE"):
230 raise Exception('Invalid lnk_event: %s' % self.lnk_event)
233 # Link/Network layer data collector
234 def collect_data (self, length):
235 #print ("DEBUG: BIT=%d t0=%d t+=%d" % (self.lnk_bit, self.lnk_fall, self.samplenum))
236 self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
237 self.net_cnt = self.net_cnt + 1
238 if (self.net_cnt == length):
239 self.net_data = self.net_data & ((1<<length)-1)
245 # Link/Network layer search collector
246 def collect_search (self, length):
247 #print ("DEBUG: SEARCH=%s BIT=%d t0=%d t+=%d" % (self.net_search, self.lnk_bit, self.lnk_fall, self.samplenum))
248 if (self.net_search == "P"):
249 self.net_data_p = self.net_data_p & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
250 self.net_search = "N"
251 elif (self.net_search == "N"):
252 self.net_data_n = self.net_data_n & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
253 self.net_search = "D"
254 elif (self.net_search == "D"):
255 self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
256 self.net_search = "P"
257 self.net_cnt = self.net_cnt + 1
258 if (self.net_cnt == length):
259 self.net_data_p = self.net_data_p & ((1<<length)-1)
260 self.net_data_n = self.net_data_n & ((1<<length)-1)
261 self.net_data = self.net_data & ((1<<length)-1)
262 self.net_search = "P"