]> sigrok.org Git - libsigrokdecode.git/blob - decoders/onewire/onewire.py
decoder onewire: added conditional network commands, some fixex to the transport...
[libsigrokdecode.git] / decoders / onewire / onewire.py
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
23 import sigrokdecode as srd
24
25 # Annotation feed formats
26 ANN_LINK      = 0
27 ANN_NETWORK   = 1
28 ANN_TRANSPORT = 2
29
30 class 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 = [
49         ['Link', 'Link layer events (reset, presence, bit slots)'],
50         ['Network', 'Network layer events (device addressing)'],
51         ['Transport', 'Transport layer events'],
52     ]
53
54     def __init__(self, **kwargs):
55         # Common variables
56         self.samplenum = 0
57         # Link layer variables
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
63         # Network layer variables
64         self.net_state   = 'IDLE'
65         self.net_cnt     = 0
66         self.net_search  = "P"
67         self.net_data_p  = 0x0
68         self.net_data_n  = 0x0
69         self.net_data    = 0x0
70         self.net_rom     = 0x0000000000000000
71         # Transport layer variables
72         self.trn_state   = 'IDLE'
73
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')
78
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]])
83
84     def report(self):
85         pass
86
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))
90
91             # Data link layer
92
93             # Clear events.
94             self.lnk_event = "NONE"
95             # State machine.
96             if self.lnk_state == 'WAIT FOR FALLING EDGE':
97                 # The start of a cycle is a falling edge.
98                 if (owr == 0):
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.
113                 if (owr == 1):
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']])
124                         # Reset the timer.
125                         self.lnk_fall = self.samplenum
126                     # Otherwise this is assumed to be a data bit.
127                     else :
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]])
142             else:
143                 raise Exception('Invalid lnk_state: %d' % self.lnk_state)
144
145             # Network layer
146             
147             # State machine.
148             if (self.lnk_event == "RESET"):
149                 self.net_state = "COMMAND"
150                 self.net_search = "P"
151                 self.net_cnt    = 0
152             elif (self.net_state == "IDLE"):
153                 pass
154             elif (self.net_state == "DONE"):
155                 self.trn_state = "COMMAND"
156                 self.net_state = "IDLE"
157             elif (self.net_state == "COMMAND"):
158                 if (self.collect_data(8)):
159 #                    self.put(self.lnk_fall, self.samplenum,
160 #                             self.out_proto, ['ROM COMMAND', self.net_data])
161                     self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: 0x%02x' % self.net_data]])
162                     if   (self.net_data == 0x33):
163                         # READ ROM
164                         self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM\'']])
165                         self.net_state = "GET ROM"
166                     elif (self.net_data == 0x0f):
167                         # CONDITIONAL READ ROM
168                         self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'CONDITIONAL READ ROM\'']])
169                         self.net_state = "GET ROM"
170                     elif (self.net_data == 0xcc):
171                         # SKIP ROM
172                         self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SKIP ROM\'']])
173                         self.net_state = "DONE"
174                     elif (self.net_data == 0x55):
175                         # MATCH ROM
176                         self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'MATCH ROM\'']])
177                         self.net_state = "GET ROM"
178                     elif (self.net_data == 0xf0):
179                         # SEARCH ROM
180                         self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SEARCH ROM\'']])
181                         self.net_state = "SEARCH ROM"
182                     elif (self.net_data == 0xec):
183                         # CONDITIONAL SEARCH ROM
184                         self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'CONDITIONAL SEARCH ROM\'']])
185                         self.net_state = "SEARCH ROM"
186                     elif (self.net_data == 0x3c):
187                         # OVERDRIVE SKIP ROM
188                         self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE SKIP ROM\'']])
189                         self.net_state = "DONE"
190                     elif (self.net_data == 0x69):
191                         # OVERDRIVE MATCH ROM
192                         self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE MATCH ROM\'']])
193                         self.net_state = "GET ROM"
194             elif (self.net_state == "GET ROM"):
195                 # family code (1B) + serial number (6B) + CRC (1B)
196                 if (self.collect_data(64)):
197                     self.net_rom = self.net_data & 0xffffffffffffffff
198                     self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_rom]])
199                     self.net_state = "DONE"
200             elif (self.net_state == "SEARCH ROM"):
201                 # family code (1B) + serial number (6B) + CRC (1B)
202                 if (self.collect_search(64)):
203                     self.net_rom = self.net_data & 0xffffffffffffffff
204                     self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_rom]])
205                     self.net_state = "DONE"
206             else:
207                 raise Exception('Invalid net_state: %s' % self.net_state)
208
209             # Transport layer
210             
211             # State machine.
212             if (self.lnk_event == "RESET"):
213                 self.trn_state = "IDLE"
214             elif (self.trn_state == "IDLE"):
215                 pass
216             elif (self.trn_state == "COMMAND"):
217                 if (self.collect_data(8)):
218 #                    self.put(self.lnk_fall, self.samplenum, self.out_proto, ['FUNCTION COMMAND', self.net_data])
219                     self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK  , ['FUNCTION COMMAND: 0x%02x' % self.net_data]])
220                     self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_TRANSPORT, ['FUNCTION COMMAND: 0x%02x' % self.net_data]])
221                     if   (self.net_data == 0x48):
222                         # COPY SCRATCHPAD
223                         self.trn_state = "TODO"
224                     elif (self.net_data == 0x4e):
225                         # WRITE SCRATCHPAD
226                         self.trn_state = "TODO"
227                     elif (self.net_data == 0xbe):
228                         # READ SCRATCHPAD
229                         self.trn_state = "TODO"
230                     elif (self.net_data == 0xb8):
231                         # RECALL E2
232                         self.trn_state = "TODO"
233                     elif (self.net_data == 0xb4):
234                         # READ POWER SUPPLY
235                         self.trn_state = "TODO"
236                     else:
237                         # unsupported commands
238                         self.trn_state = "TODO"
239             elif (self.trn_state == "TODO"):
240                 if (self.collect_data(8)):
241                     self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK  , ['TRANSPORT DATA: 0x%02x' % self.net_data]])
242                     self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_TRANSPORT, ['TRANSPORT DATA: 0x%02x' % self.net_data]])
243 #                self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK  , ['TODO unsupported transport state: %s' % self.trn_state]])
244 #                self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_TRANSPORT, ['TODO unsupported transport state: %s' % self.trn_state]])
245                 pass
246             else:
247                 raise Exception('Invalid trn_state: %s' % self.trn_state)
248
249     # Link/Network layer data collector
250     def collect_data (self, length):
251         if (self.lnk_event == "DATA BIT"):
252             #print ("DEBUG: BIT=%d t0=%d t+=%d" % (self.lnk_bit, self.lnk_fall, self.samplenum))
253             self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
254             self.net_cnt  = self.net_cnt + 1
255             if (self.net_cnt == length):
256                 self.net_data = self.net_data & ((1<<length)-1)
257                 self.net_cnt  = 0
258                 return (1)
259             else:
260                 return (0)
261         else:
262             return (0)
263
264     # Link/Network layer search collector
265     def collect_search (self, length):
266         if (self.lnk_event == "DATA BIT"):
267             #print ("DEBUG: SEARCH=%s BIT=%d t0=%d t+=%d" % (self.net_search, self.lnk_bit, self.lnk_fall, self.samplenum))
268             if   (self.net_search == "P"):
269               self.net_data_p = self.net_data_p & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
270               self.net_search = "N"
271             elif (self.net_search == "N"):
272               self.net_data_n = self.net_data_n & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
273               self.net_search = "D"
274             elif (self.net_search == "D"):
275               self.net_data   = self.net_data   & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
276               self.net_search = "P"
277               self.net_cnt    = self.net_cnt + 1
278             if (self.net_cnt == length):
279                 self.net_data_p = self.net_data_p & ((1<<length)-1)
280                 self.net_data_n = self.net_data_n & ((1<<length)-1)
281                 self.net_data   = self.net_data   & ((1<<length)-1)
282                 self.net_search = "P"
283                 self.net_cnt    = 0
284                 return (1)
285             else:
286                 return (0)
287         else:
288             return (0)