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
32 class Decoder(srd.Decoder):
37 desc = '1-Wire bus and MicroLan'
42 {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire bus'},
45 {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power'},
48 'overdrive': ['Overdrive', 0],
51 ['ASCII', 'Data bytes as ASCII characters'],
52 ['Decimal', 'Databytes as decimal, integer values'],
53 ['Hex', 'Data bytes in hex format'],
54 ['Octal', 'Data bytes as octal numbers'],
55 ['Bits', 'Data bytes in bit notation (sequence of 0/1 digits)'],
58 def __init__(self, **kwargs):
61 # Link layer variables
62 self.lnk_state = 'WAIT FOR FALLING EDGE'
63 self.lnk_event = 'NONE'
67 # Network layer variables
68 self.net_state = 'ROM COMMAND'
69 self.net_event = 'NONE'
75 # Transport layer variables
76 self.trn_state = 'WAIT FOR EVENT'
77 self.trn_event = 'NONE'
79 def start(self, metadata):
80 self.samplerate = metadata['samplerate']
81 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire')
82 self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire')
84 # The width of the 1-Wire time base (30us) in number of samples.
85 # TODO: optimize this value
86 self.time_base = float(self.samplerate) * float(0.000030)
87 print ("DEBUG: samplerate = %d, time_base = %d" % (self.samplerate, self.time_base))
92 def decode(self, ss, es, data):
93 for (self.samplenum, (owr, pwr)) in data:
94 # print ("DEBUG: sample = %d, owr = %d, pwr = %d, lnk_fall = %d, lnk_state = %s" % (self.samplenum, owr, pwr, self.lnk_fall, self.lnk_state))
99 self.lnk_event = "NONE"
101 if self.lnk_state == 'WAIT FOR FALLING EDGE':
102 # The start of a cycle is a falling edge.
104 # Save the sample number for the falling edge.
105 self.lnk_fall = self.samplenum
106 # Go to waiting for sample time
107 self.lnk_state = 'WAIT FOR DATA SAMPLE'
108 self.put(self.lnk_fall, self.samplenum, self.out_ann,
109 [ANN_DEC, ['LNK: NEGEDGE: ']])
110 elif self.lnk_state == 'WAIT FOR DATA SAMPLE':
111 # Data should be sample one 'time unit' after a falling edge
112 if (self.samplenum - self.lnk_fall == 0.5*self.time_base):
113 self.lnk_bit = owr & 0x1
114 self.lnk_event = "DATA BIT"
115 if (self.lnk_bit) : self.lnk_state = 'WAIT FOR FALLING EDGE'
116 else : self.lnk_state = 'WAIT FOR RISING EDGE'
117 self.put(self.lnk_fall, self.samplenum, self.out_ann,
118 [ANN_DEC, ['LNK: BIT: ' + str(self.lnk_bit)]])
119 elif self.lnk_state == 'WAIT FOR RISING EDGE':
120 # The end of a cycle is a rising edge.
122 # A reset cycle is longer than 8T.
123 if (self.samplenum - self.lnk_fall > 8*self.time_base):
124 # Save the sample number for the falling edge.
125 self.lnk_rise = self.samplenum
126 # Send a reset event to the next protocol layer.
127 self.lnk_event = "RESET"
128 self.lnk_state = "WAIT FOR PRESENCE DETECT"
129 self.put(self.lnk_fall, self.samplenum, self.out_proto,
131 self.put(self.lnk_fall, self.samplenum, self.out_ann,
132 [ANN_DEC, ['LNK: RESET: ']])
133 print ("DEBUG: RESET t0=%d t+=%d" % (self.lnk_fall, self.samplenum))
135 self.lnk_fall = self.samplenum
136 # Otherwise this is assumed to be a data bit.
138 self.lnk_state = "WAIT FOR FALLING EDGE"
139 elif self.lnk_state == 'WAIT FOR PRESENCE DETECT':
140 # Data should be sample one 'time unit' after a falling edge
141 if (self.samplenum - self.lnk_rise == 2.5*self.time_base):
142 self.lnk_present = owr & 0x1
143 # Save the sample number for the falling edge.
144 if not (self.lnk_present) : self.lnk_fall = self.samplenum
145 # create presence detect event
146 #self.lnk_event = "PRESENCE DETECT"
147 if (self.lnk_present) : self.lnk_state = 'WAIT FOR FALLING EDGE'
148 else : self.lnk_state = 'WAIT FOR RISING EDGE'
149 self.put(self.lnk_fall, self.samplenum, self.out_ann,
150 [ANN_DEC, ['LNK: PRESENCE: ' + str(self.lnk_present)]])
151 print ("DEBUG: PRESENCE=%d t0=%d t+=%d" % (self.lnk_present, self.lnk_fall, self.samplenum))
153 raise Exception('Invalid lnk_state: %d' % self.lnk_state)
158 self.net_event = "RESET"
160 if (self.lnk_event == "RESET"):
161 self.net_state = "ROM COMMAND"
162 self.net_search = "P"
164 elif (self.lnk_event == "DATA BIT"):
165 if (self.net_state == "ROM COMMAND"):
166 if (self.collect_data(8)):
167 # self.put(self.lnk_fall, self.samplenum,
168 # self.out_proto, ['LNK: COMMAND', self.net_data])
169 self.put(self.lnk_fall, self.samplenum, self.out_ann,
170 [ANN_DEC, ['NET: ROM COMMAND: 0x' + hex(self.net_data)]])
171 print ("DEBUG: ROM_COMMAND=0x%02x t0=%d t+=%d" % (self.net_data, self.lnk_fall, self.samplenum))
172 if (self.net_data in [0x33, 0x0f]):
174 self.net_state = "ADDRESS"
175 elif (self.net_data == 0xcc):
177 self.net_state = "CONTROL COMMAND"
178 elif (self.net_data == 0x55):
180 self.net_state = "ADDRESS"
181 elif (self.net_data == 0xf0):
183 self.net_state = "SEARCH"
184 elif (self.net_data == 0x3c):
186 self.net_state = "CONTROL COMMAND"
187 elif (self.net_data == 0x69):
188 # OVERDRIVE MATCH ROM
189 self.net_state = "ADDRESS"
190 elif (self.net_state == "ADDRESS"):
191 # family code (1B) + serial number (6B) + CRC (1B)
192 if (self.collect_data((1+6+1)*8)):
193 self.net_family_code = (self.net_data >> (( 0)*8)) & 0xff
194 self.net_serial_number = (self.net_data >> (( 1)*8)) & 0xffffffffffff
195 self.net_crc = (self.net_data >> ((6+1)*8)) & 0xff
196 print ("DEBUG: net_family_code =0x%001x" % (self.net_family_code ))
197 print ("DEBUG: net_serial_number=0x%012x" % (self.net_serial_number))
198 print ("DEBUG: net_crc =0x%001x" % (self.net_crc ))
199 self.net_state = "CONTROL COMMAND"
200 elif (self.net_state == "SEARCH"):
201 # family code (1B) + serial number (6B) + CRC (1B)
202 if (self.collect_search((1+6+1)*8)):
203 self.net_family_code = (self.net_data >> (( 0)*8)) & 0xff
204 self.net_serial_number = (self.net_data >> (( 1)*8)) & 0xffffffffffff
205 self.net_crc = (self.net_data >> ((6+1)*8)) & 0xff
206 print ("DEBUG: net_family_code =0x%001x" % (self.net_family_code ))
207 print ("DEBUG: net_serial_number=0x%012x" % (self.net_serial_number))
208 print ("DEBUG: net_crc =0x%001x" % (self.net_crc ))
209 self.net_state = "CONTROL COMMAND"
210 elif (self.net_state == "CONTROL COMMAND"):
211 if (self.collect_data(8)):
212 # self.put(self.lnk_fall, self.samplenum,
213 # self.out_proto, ['LNK: COMMAND', self.net_data])
214 self.put(self.lnk_fall, self.samplenum, self.out_ann,
215 [ANN_DEC, ['NET: FUNCTION COMMAND: 0x' + hex(self.net_data)]])
216 print ("DEBUG: FUNCTION_COMMAND=0x%02x t0=%d t+=%d" % (self.net_data, self.lnk_fall, self.samplenum))
217 if (self.net_data == 0x44):
218 # CONVERT TEMPERATURE
219 self.net_state = "TODO"
220 elif (self.net_data == 0x48):
222 self.net_state = "TODO"
223 elif (self.net_data == 0x4e):
225 self.net_state = "TODO"
226 elif (self.net_data == 0xbe):
228 self.net_state = "TODO"
229 elif (self.net_data == 0xb8):
231 self.net_state = "TODO"
232 elif (self.net_data == 0xb4):
234 self.net_state = "TODO"
236 raise Exception('Invalid net_state: %s' % self.net_state)
237 elif (self.lnk_event != "NONE"):
238 raise Exception('Invalid lnk_event: %s' % self.lnk_event)
241 # Link/Network layer data collector
242 def collect_data (self, length):
243 #print ("DEBUG: BIT=%d t0=%d t+=%d" % (self.lnk_bit, self.lnk_fall, self.samplenum))
244 self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
245 self.net_cnt = self.net_cnt + 1
246 if (self.net_cnt == length):
247 self.net_data = self.net_data & ((1<<length)-1)
249 print ("DEBUG: DATA=0x%0x t0=%d t+=%d" % (self.net_data, self.lnk_fall, self.samplenum))
254 # Link/Network layer search collector
255 def collect_search (self, length):
256 #print ("DEBUG: SEARCH=%s BIT=%d t0=%d t+=%d" % (self.net_search, self.lnk_bit, self.lnk_fall, self.samplenum))
257 if (self.net_search == "P"):
258 self.net_data_p = self.net_data_p & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
259 self.net_search = "N"
260 elif (self.net_search == "N"):
261 self.net_data_n = self.net_data_n & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
262 self.net_search = "D"
263 elif (self.net_search == "D"):
264 self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
265 self.net_search = "P"
266 self.net_cnt = self.net_cnt + 1
267 if (self.net_cnt == length):
268 self.net_data_p = self.net_data_p & ((1<<length)-1)
269 self.net_data_n = self.net_data_n & ((1<<length)-1)
270 self.net_data = self.net_data & ((1<<length)-1)
271 self.net_search = "P"
273 print ("DEBUG: SEARCH_P=0x%0x t0=%d t+=%d" % (self.net_data_p, self.lnk_fall, self.samplenum))
274 print ("DEBUG: SEARCH_N=0x%0x t0=%d t+=%d" % (self.net_data_n, self.lnk_fall, self.samplenum))
275 print ("DEBUG: SEARCH =0x%0x t0=%d t+=%d" % (self.net_data , self.lnk_fall, self.samplenum))