]> sigrok.org Git - libsigrokdecode.git/blame - decoders/onewire/onewire.py
decoder onewire: some annotation cleanup
[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
0bd62eb1
IJ
26ANN_LINK = 0
27ANN_NETWORK = 1
28ANN_TRANSFER = 2
51990c45
IJ
29
30class Decoder(srd.Decoder):
31 api_version = 1
32 id = 'onewire'
33 name = '1-Wire'
34 longname = ''
35 desc = '1-Wire bus and MicroLan'
36 license = 'gplv2+'
37 inputs = ['logic']
38 outputs = ['onewire']
39 probes = [
40 {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire bus'},
41 ]
42 optional_probes = [
43 {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power'},
44 ]
45 options = {
46 'overdrive': ['Overdrive', 0],
47 }
48 annotations = [
0bd62eb1
IJ
49 ['Link', 'Link layer events (reset, presence, bit slots)'],
50 ['Network', 'Network layer events (device addressing)'],
51 ['Transfer', 'Transfer layer events'],
51990c45
IJ
52 ]
53
51990c45
IJ
54 def __init__(self, **kwargs):
55 # Common variables
56 self.samplenum = 0
57 # Link layer variables
af623785
IJ
58 self.lnk_state = 'WAIT FOR FALLING EDGE'
59 self.lnk_event = 'NONE'
60 self.lnk_fall = 0
61 self.lnk_present = 0
62 self.lnk_bit = 0
51990c45 63 # Network layer variables
d2b6e141 64 self.net_state = 'ROM COMMAND'
af623785
IJ
65 self.net_event = 'NONE'
66 self.net_cnt = 0
d2b6e141
IJ
67 self.net_search = "P"
68 self.net_data_p = 0x0
69 self.net_data_n = 0x0
70 self.net_data = 0x0
51990c45 71 # Transport layer variables
af623785
IJ
72 self.trn_state = 'WAIT FOR EVENT'
73 self.trn_event = 'NONE'
51990c45
IJ
74
75 def start(self, metadata):
76 self.samplerate = metadata['samplerate']
77 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire')
4fe36ec3 78 self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire')
51990c45
IJ
79
80 # The width of the 1-Wire time base (30us) in number of samples.
81 # TODO: optimize this value
af623785 82 self.time_base = float(self.samplerate) * float(0.000030)
0bd62eb1 83 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK, ['time_base = %d' % self.time_base]])
51990c45
IJ
84
85 def report(self):
86 pass
87
51990c45 88 def decode(self, ss, es, data):
af623785
IJ
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))
51990c45 91
51990c45 92 # Data link layer
39a0219a
IJ
93
94 # Clear events.
af623785 95 self.lnk_event = "NONE"
39a0219a 96 # State machine.
51990c45
IJ
97 if self.lnk_state == 'WAIT FOR FALLING EDGE':
98 # The start of a cycle is a falling edge.
39a0219a
IJ
99 if (owr == 0):
100 # Save the sample number for the falling edge.
101 self.lnk_fall = self.samplenum
51990c45 102 # Go to waiting for sample time
39a0219a
IJ
103 self.lnk_state = 'WAIT FOR DATA SAMPLE'
104 elif self.lnk_state == 'WAIT FOR DATA SAMPLE':
51990c45 105 # Data should be sample one 'time unit' after a falling edge
d2b6e141 106 if (self.samplenum - self.lnk_fall == 0.5*self.time_base):
51990c45 107 self.lnk_bit = owr & 0x1
39a0219a
IJ
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'
0bd62eb1 111 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK, ['BIT: %01x' % self.lnk_bit]])
51990c45
IJ
112 elif self.lnk_state == 'WAIT FOR RISING EDGE':
113 # The end of a cycle is a rising edge.
39a0219a 114 if (owr == 1):
af623785 115 # A reset cycle is longer than 8T.
39a0219a
IJ
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
af623785 119 # Send a reset event to the next protocol layer.
39a0219a
IJ
120 self.lnk_event = "RESET"
121 self.lnk_state = "WAIT FOR PRESENCE DETECT"
0bd62eb1
IJ
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']])
af623785
IJ
125 # Reset the timer.
126 self.lnk_fall = self.samplenum
127 # Otherwise this is assumed to be a data bit.
128 else :
129 self.lnk_state = "WAIT FOR FALLING EDGE"
39a0219a
IJ
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):
af623785 133 self.lnk_present = owr & 0x1
cf0f9df0
IJ
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
af623785 137 #self.lnk_event = "PRESENCE DETECT"
cf0f9df0
IJ
138 if (self.lnk_present) : self.lnk_state = 'WAIT FOR FALLING EDGE'
139 else : self.lnk_state = 'WAIT FOR RISING EDGE'
0bd62eb1
IJ
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]])
39a0219a 143 else:
0631e33d 144 raise Exception('Invalid lnk_state: %d' % self.lnk_state)
39a0219a
IJ
145
146 # Network layer
147
148 # Clear events.
149 self.net_event = "RESET"
150 # State machine.
4fe36ec3 151 if (self.lnk_event == "RESET"):
d2b6e141
IJ
152 self.net_state = "ROM COMMAND"
153 self.net_search = "P"
154 self.net_cnt = 0
4fe36ec3 155 elif (self.lnk_event == "DATA BIT"):
d2b6e141
IJ
156 if (self.net_state == "ROM COMMAND"):
157 if (self.collect_data(8)):
158# self.put(self.lnk_fall, self.samplenum,
0bd62eb1
IJ
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]])
de2f5328 161 if (self.net_data == 0x33):
d2b6e141 162 # READ ROM
0bd62eb1 163 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM\'']])
d2b6e141 164 self.net_state = "ADDRESS"
de2f5328
IJ
165 elif (self.net_data == 0x0f):
166 # READ ROM TODO
0bd62eb1 167 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM ???\'']])
de2f5328 168 self.net_state = "ADDRESS"
d2b6e141
IJ
169 elif (self.net_data == 0xcc):
170 # SKIP ROM
0bd62eb1 171 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SKIP ROM\'']])
d2b6e141
IJ
172 self.net_state = "CONTROL COMMAND"
173 elif (self.net_data == 0x55):
174 # MATCH ROM
0bd62eb1 175 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'MATCH ROM\'']])
d2b6e141
IJ
176 self.net_state = "ADDRESS"
177 elif (self.net_data == 0xf0):
178 # SEARCH ROM
0bd62eb1 179 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SEARCH ROM\'']])
d2b6e141
IJ
180 self.net_state = "SEARCH"
181 elif (self.net_data == 0x3c):
182 # OVERDRIVE SKIP ROM
0bd62eb1 183 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE SKIP ROM\'']])
d2b6e141
IJ
184 self.net_state = "CONTROL COMMAND"
185 elif (self.net_data == 0x69):
186 # OVERDRIVE MATCH ROM
0bd62eb1 187 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE MATCH ROM\'']])
d2b6e141
IJ
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)):
de2f5328 192 self.net_address = self.net_data & 0xffffffffffffffff
0bd62eb1 193 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_address]])
d2b6e141
IJ
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)):
de2f5328 198 self.net_address = self.net_data & 0xffffffffffffffff
0bd62eb1 199 self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_address]])
d2b6e141
IJ
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,
0bd62eb1 206 [ANN_TRANSFER, ['TRANSFER: FUNCTION COMMAND: 0x' + hex(self.net_data)]])
de2f5328 207 if (self.net_data == 0x48):
d2b6e141
IJ
208 # COPY SCRATCHPAD
209 self.net_state = "TODO"
210 elif (self.net_data == 0x4e):
211 # WRITE SCRATCHPAD
212 self.net_state = "TODO"
213 elif (self.net_data == 0xbe):
214 # READ SCRATCHPAD
215 self.net_state = "TODO"
216 elif (self.net_data == 0xb8):
217 # RECALL E2
218 self.net_state = "TODO"
219 elif (self.net_data == 0xb4):
220 # READ POWER SUPPLY
221 self.net_state = "TODO"
de2f5328
IJ
222 else:
223 # unsupported commands
224 self.net_state = "UNDEFINED"
225 elif (self.net_state == "UNDEFINED"):
226 pass
39a0219a 227 else:
d2b6e141
IJ
228 raise Exception('Invalid net_state: %s' % self.net_state)
229 elif (self.lnk_event != "NONE"):
af623785 230 raise Exception('Invalid lnk_event: %s' % self.lnk_event)
51990c45 231
51990c45 232
d2b6e141
IJ
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)
240 self.net_cnt = 0
d2b6e141
IJ
241 return (1)
242 else:
243 return (0)
244
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"
263 self.net_cnt = 0
d2b6e141
IJ
264 return (1)
265 else:
266 return (0)