]> sigrok.org Git - libsigrokdecode.git/blob - decoders/onewire/onewire.py
0f9db4dac19dbfd81f487b15eb490adbf66fc1d0
[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_TRANSFER = 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         ['Transfer', 'Transfer 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   = 'ROM COMMAND'
65         self.net_event   = 'NONE'
66         self.net_cnt     = 0
67         self.net_search  = "P"
68         self.net_data_p  = 0x0
69         self.net_data_n  = 0x0
70         self.net_data    = 0x0
71         # Transport layer variables
72         self.trn_state   = 'WAIT FOR EVENT'
73         self.trn_event   = 'NONE'
74
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')
79
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]])
84
85     def report(self):
86         pass
87
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))
91
92             # Data link layer
93
94             # Clear events.
95             self.lnk_event = "NONE"
96             # State machine.
97             if self.lnk_state == 'WAIT FOR FALLING EDGE':
98                 # The start of a cycle is a falling edge.
99                 if (owr == 0):
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.
114                 if (owr == 1):
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']])
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"
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]])
143             else:
144                 raise Exception('Invalid lnk_state: %d' % self.lnk_state)
145
146             # Network layer
147             
148             # Clear events.
149             self.net_event = "RESET"
150             # State machine.
151             if (self.lnk_event == "RESET"):
152                 self.net_state = "ROM COMMAND"
153                 self.net_search = "P"
154                 self.net_cnt    = 0
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):
162                             # READ ROM
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):
166                             # READ ROM TODO
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):
170                             # SKIP ROM
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):
174                             # MATCH ROM
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):
178                             # SEARCH ROM
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):
182                             # OVERDRIVE SKIP ROM
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):
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"
222                         else:
223                             # unsupported commands
224                             self.net_state = "UNDEFINED"
225                 elif (self.net_state == "UNDEFINED"):
226                     pass
227                 else:
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)
231
232
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
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
264             return (1)
265         else:
266             return (0)