]> sigrok.org Git - libsigrokdecode.git/blob - decoders/onewire/onewire.py
onewire decoder: removed some debug code, preparations for the next protocol layer
[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_ASCII = 0
27 ANN_DEC = 1
28 ANN_HEX = 2
29 ANN_OCT = 3
30 ANN_BITS = 4
31
32 class 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
58     def __init__(self, **kwargs):
59         # Common variables
60         self.samplenum = 0
61         # Link layer variables
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
67         # Network layer variables
68         self.net_state   = 'ROM COMMAND'
69         self.net_event   = 'NONE'
70         self.net_cnt     = 0
71         self.net_search  = "P"
72         self.net_data_p  = 0x0
73         self.net_data_n  = 0x0
74         self.net_data    = 0x0
75         # Transport layer variables
76         self.trn_state   = 'WAIT FOR EVENT'
77         self.trn_event   = 'NONE'
78
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')
83
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))
88
89     def report(self):
90         pass
91
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))
95
96             # Data link layer
97
98             # Clear events.
99             self.lnk_event = "NONE"
100             # State machine.
101             if self.lnk_state == 'WAIT FOR FALLING EDGE':
102                 # The start of a cycle is a falling edge.
103                 if (owr == 0):
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.
121                 if (owr == 1):
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,
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"
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))
152             else:
153                 raise Exception('Invalid lnk_state: %d' % self.lnk_state)
154
155             # Network layer
156             
157             # Clear events.
158             self.net_event = "RESET"
159             # State machine.
160             if (self.lnk_event == "RESET"):
161                 self.net_state = "ROM COMMAND"
162                 self.net_search = "P"
163                 self.net_cnt    = 0
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 == 0x33):
173                             # READ ROM
174                             self.net_state = "ADDRESS"
175                         elif (self.net_data == 0x0f):
176                             # READ ROM TODO
177                             self.net_state = "ADDRESS"
178                         elif (self.net_data == 0xcc):
179                             # SKIP ROM
180                             self.net_state = "CONTROL COMMAND"
181                         elif (self.net_data == 0x55):
182                             # MATCH ROM
183                             self.net_state = "ADDRESS"
184                         elif (self.net_data == 0xf0):
185                             # SEARCH ROM
186                             self.net_state = "SEARCH"
187                         elif (self.net_data == 0x3c):
188                             # OVERDRIVE SKIP ROM
189                             self.net_state = "CONTROL COMMAND"
190                         elif (self.net_data == 0x69):
191                             # OVERDRIVE MATCH ROM
192                             self.net_state = "ADDRESS"
193                 elif (self.net_state == "ADDRESS"):
194                     # family code (1B) + serial number (6B) + CRC (1B)
195                     if (self.collect_data((1+6+1)*8)):
196                         self.net_address = self.net_data & 0xffffffffffffffff
197                         self.net_state = "CONTROL COMMAND"
198                 elif (self.net_state == "SEARCH"):
199                     # family code (1B) + serial number (6B) + CRC (1B)
200                     if (self.collect_search((1+6+1)*8)):
201                         self.net_address = self.net_data & 0xffffffffffffffff
202                         self.net_state = "CONTROL COMMAND"
203                 elif (self.net_state == "CONTROL COMMAND"):
204                     if (self.collect_data(8)):
205 #                        self.put(self.lnk_fall, self.samplenum,
206 #                                 self.out_proto, ['LNK: COMMAND', self.net_data])
207                         self.put(self.lnk_fall, self.samplenum, self.out_ann,
208                                  [ANN_DEC, ['NET: FUNCTION COMMAND: 0x' + hex(self.net_data)]])
209                         print ("DEBUG: FUNCTION_COMMAND=0x%02x t0=%d t+=%d" % (self.net_data, self.lnk_fall, self.samplenum))
210                         if   (self.net_data == 0x48):
211                             # COPY SCRATCHPAD
212                             self.net_state = "TODO"
213                         elif (self.net_data == 0x4e):
214                             # WRITE SCRATCHPAD
215                             self.net_state = "TODO"
216                         elif (self.net_data == 0xbe):
217                             # READ SCRATCHPAD
218                             self.net_state = "TODO"
219                         elif (self.net_data == 0xb8):
220                             # RECALL E2
221                             self.net_state = "TODO"
222                         elif (self.net_data == 0xb4):
223                             # READ POWER SUPPLY
224                             self.net_state = "TODO"
225                         else:
226                             # unsupported commands
227                             self.net_state = "UNDEFINED"
228                 elif (self.net_state == "UNDEFINED"):
229                     pass
230                 else:
231                     raise Exception('Invalid net_state: %s' % self.net_state)
232             elif (self.lnk_event != "NONE"):
233                 raise Exception('Invalid lnk_event: %s' % self.lnk_event)
234
235
236     # Link/Network layer data collector
237     def collect_data (self, length):
238         #print ("DEBUG: BIT=%d t0=%d t+=%d" % (self.lnk_bit, self.lnk_fall, self.samplenum))
239         self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
240         self.net_cnt  = self.net_cnt + 1
241         if (self.net_cnt == length):
242             self.net_data = self.net_data & ((1<<length)-1)
243             self.net_cnt  = 0
244             print ("DEBUG: DATA=0x%0x t0=%d t+=%d" % (self.net_data, self.lnk_fall, self.samplenum))
245             return (1)
246         else:
247             return (0)
248
249     # Link/Network layer search collector
250     def collect_search (self, length):
251         #print ("DEBUG: SEARCH=%s BIT=%d t0=%d t+=%d" % (self.net_search, self.lnk_bit, self.lnk_fall, self.samplenum))
252         if   (self.net_search == "P"):
253           self.net_data_p = self.net_data_p & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
254           self.net_search = "N"
255         elif (self.net_search == "N"):
256           self.net_data_n = self.net_data_n & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
257           self.net_search = "D"
258         elif (self.net_search == "D"):
259           self.net_data   = self.net_data   & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt)
260           self.net_search = "P"
261           self.net_cnt    = self.net_cnt + 1
262         if (self.net_cnt == length):
263             self.net_data_p = self.net_data_p & ((1<<length)-1)
264             self.net_data_n = self.net_data_n & ((1<<length)-1)
265             self.net_data   = self.net_data   & ((1<<length)-1)
266             self.net_search = "P"
267             self.net_cnt    = 0
268             print ("DEBUG: SEARCH_P=0x%0x t0=%d t+=%d" % (self.net_data_p, self.lnk_fall, self.samplenum))
269             print ("DEBUG: SEARCH_N=0x%0x t0=%d t+=%d" % (self.net_data_n, self.lnk_fall, self.samplenum))
270             print ("DEBUG: SEARCH  =0x%0x t0=%d t+=%d" % (self.net_data  , self.lnk_fall, self.samplenum))
271             return (1)
272         else:
273             return (0)