]> sigrok.org Git - libsigrokdecode.git/blame - decoders/onewire/onewire.py
onewire decoder: added search support
[libsigrokdecode.git] / decoders / onewire / onewire.py
CommitLineData
51990c45
IJ
1##
2## This file is part of the sigrok project.
3##
4## Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
5##
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.
10##
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.
15##
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
19##
20
21# 1-Wire protocol decoder
22
23import sigrokdecode as srd
24
25# Annotation feed formats
26ANN_ASCII = 0
27ANN_DEC = 1
28ANN_HEX = 2
29ANN_OCT = 3
30ANN_BITS = 4
31
32class Decoder(srd.Decoder):
33 api_version = 1
34 id = 'onewire'
35 name = '1-Wire'
36 longname = ''
37 desc = '1-Wire bus and MicroLan'
38 license = 'gplv2+'
39 inputs = ['logic']
40 outputs = ['onewire']
41 probes = [
42 {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire bus'},
43 ]
44 optional_probes = [
45 {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power'},
46 ]
47 options = {
48 'overdrive': ['Overdrive', 0],
49 }
50 annotations = [
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)'],
56 ]
57
51990c45
IJ
58 def __init__(self, **kwargs):
59 # Common variables
60 self.samplenum = 0
61 # Link layer variables
af623785
IJ
62 self.lnk_state = 'WAIT FOR FALLING EDGE'
63 self.lnk_event = 'NONE'
64 self.lnk_fall = 0
65 self.lnk_present = 0
66 self.lnk_bit = 0
51990c45 67 # Network layer variables
d2b6e141 68 self.net_state = 'ROM COMMAND'
af623785
IJ
69 self.net_event = 'NONE'
70 self.net_cnt = 0
d2b6e141
IJ
71 self.net_search = "P"
72 self.net_data_p = 0x0
73 self.net_data_n = 0x0
74 self.net_data = 0x0
51990c45 75 # Transport layer variables
af623785
IJ
76 self.trn_state = 'WAIT FOR EVENT'
77 self.trn_event = 'NONE'
51990c45
IJ
78
79 def start(self, metadata):
80 self.samplerate = metadata['samplerate']
81 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire')
4fe36ec3 82 self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire')
51990c45
IJ
83
84 # The width of the 1-Wire time base (30us) in number of samples.
85 # TODO: optimize this value
af623785
IJ
86 self.time_base = float(self.samplerate) * float(0.000030)
87 print ("DEBUG: samplerate = %d, time_base = %d" % (self.samplerate, self.time_base))
51990c45
IJ
88
89 def report(self):
90 pass
91
51990c45 92 def decode(self, ss, es, data):
af623785
IJ
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))
51990c45 95
51990c45 96 # Data link layer
39a0219a
IJ
97
98 # Clear events.
af623785 99 self.lnk_event = "NONE"
39a0219a 100 # State machine.
51990c45
IJ
101 if self.lnk_state == 'WAIT FOR FALLING EDGE':
102 # The start of a cycle is a falling edge.
39a0219a
IJ
103 if (owr == 0):
104 # Save the sample number for the falling edge.
105 self.lnk_fall = self.samplenum
51990c45 106 # Go to waiting for sample time
39a0219a 107 self.lnk_state = 'WAIT FOR DATA SAMPLE'
af623785
IJ
108 self.put(self.lnk_fall, self.samplenum, self.out_ann,
109 [ANN_DEC, ['LNK: NEGEDGE: ']])
39a0219a 110 elif self.lnk_state == 'WAIT FOR DATA SAMPLE':
51990c45 111 # Data should be sample one 'time unit' after a falling edge
d2b6e141 112 if (self.samplenum - self.lnk_fall == 0.5*self.time_base):
51990c45 113 self.lnk_bit = owr & 0x1
39a0219a
IJ
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'
af623785
IJ
117 self.put(self.lnk_fall, self.samplenum, self.out_ann,
118 [ANN_DEC, ['LNK: BIT: ' + str(self.lnk_bit)]])
51990c45
IJ
119 elif self.lnk_state == 'WAIT FOR RISING EDGE':
120 # The end of a cycle is a rising edge.
39a0219a 121 if (owr == 1):
af623785 122 # A reset cycle is longer than 8T.
39a0219a
IJ
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
af623785 126 # Send a reset event to the next protocol layer.
39a0219a
IJ
127 self.lnk_event = "RESET"
128 self.lnk_state = "WAIT FOR PRESENCE DETECT"
af623785
IJ
129 self.put(self.lnk_fall, self.samplenum, self.out_proto,
130 ['RESET'])
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))
134 # Reset the timer.
135 self.lnk_fall = self.samplenum
136 # Otherwise this is assumed to be a data bit.
137 else :
138 self.lnk_state = "WAIT FOR FALLING EDGE"
39a0219a
IJ
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):
af623785 142 self.lnk_present = owr & 0x1
cf0f9df0
IJ
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
af623785 146 #self.lnk_event = "PRESENCE DETECT"
cf0f9df0
IJ
147 if (self.lnk_present) : self.lnk_state = 'WAIT FOR FALLING EDGE'
148 else : self.lnk_state = 'WAIT FOR RISING EDGE'
af623785
IJ
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))
39a0219a 152 else:
0631e33d 153 raise Exception('Invalid lnk_state: %d' % self.lnk_state)
39a0219a
IJ
154
155 # Network layer
156
157 # Clear events.
158 self.net_event = "RESET"
159 # State machine.
4fe36ec3 160 if (self.lnk_event == "RESET"):
d2b6e141
IJ
161 self.net_state = "ROM COMMAND"
162 self.net_search = "P"
163 self.net_cnt = 0
4fe36ec3 164 elif (self.lnk_event == "DATA BIT"):
d2b6e141
IJ
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]):
173 # READ ROM
174 self.net_state = "ADDRESS"
175 elif (self.net_data == 0xcc):
176 # SKIP ROM
177 self.net_state = "CONTROL COMMAND"
178 elif (self.net_data == 0x55):
179 # MATCH ROM
180 self.net_state = "ADDRESS"
181 elif (self.net_data == 0xf0):
182 # SEARCH ROM
183 self.net_state = "SEARCH"
184 elif (self.net_data == 0x3c):
185 # OVERDRIVE SKIP ROM
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):
221 # COPY SCRATCHPAD
222 self.net_state = "TODO"
223 elif (self.net_data == 0x4e):
224 # WRITE SCRATCHPAD
225 self.net_state = "TODO"
226 elif (self.net_data == 0xbe):
227 # READ SCRATCHPAD
228 self.net_state = "TODO"
229 elif (self.net_data == 0xb8):
230 # RECALL E2
231 self.net_state = "TODO"
232 elif (self.net_data == 0xb4):
233 # READ POWER SUPPLY
234 self.net_state = "TODO"
39a0219a 235 else:
d2b6e141
IJ
236 raise Exception('Invalid net_state: %s' % self.net_state)
237 elif (self.lnk_event != "NONE"):
af623785 238 raise Exception('Invalid lnk_event: %s' % self.lnk_event)
51990c45 239
51990c45 240
d2b6e141
IJ
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)
248 self.net_cnt = 0
249 print ("DEBUG: DATA=0x%0x t0=%d t+=%d" % (self.net_data, self.lnk_fall, self.samplenum))
250 return (1)
251 else:
252 return (0)
253
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"
272 self.net_cnt = 0
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))
276 return (1)
277 else:
278 return (0)