2 ## This file is part of the sigrok project.
4 ## Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
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
21 # 1-Wire protocol decoder
23 import sigrokdecode as srd
25 # Annotation feed formats
32 class Decoder(srd.Decoder):
37 desc = '1-Wire bus and MicroLan'
42 {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire bus'},
45 {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power'},
48 'overdrive': ['Overdrive', 0],
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)'],
59 self.put(self.startsample, self.samplenum - 1, self.out_ann, data)
61 def __init__(self, **kwargs):
64 # Link layer variables
65 self.lnk_state = 'WAIT FOR NEGEDGE'
66 self.lnk_event = 'NONE'
71 # Network layer variables
72 self.net_state = 'WAIT FOR EVENT'
73 self.net_event = 'NONE'
75 # Transport layer variables
76 self.trn_state = 'WAIT FOR EVENT'
77 self.trn_event = 'NONE'
84 def start(self, metadata):
85 self.samplerate = metadata['samplerate']
86 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire')
87 self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire')
89 # The width of the 1-Wire time base (30us) in number of samples.
90 # TODO: optimize this value
91 self.time_base = float(self.samplerate) / float(0.000030)
96 def decode(self, ss, es, data):
97 for (self.samplenum, (owr, pwr)) in data:
102 self.lnk_event = "RESET"
104 if self.lnk_state == 'WAIT FOR FALLING EDGE':
105 # The start of a cycle is a falling edge.
107 # Save the sample number for the falling edge.
108 self.lnk_fall = self.samplenum
109 # Go to waiting for sample time
110 self.lnk_state = 'WAIT FOR DATA SAMPLE'
111 elif self.lnk_state == 'WAIT FOR DATA SAMPLE':
112 # Data should be sample one 'time unit' after a falling edge
113 if (self.samplenum - self.lnk_fall == 1*self.time_base):
114 self.lnk_bit = owr & 0x1
115 self.lnk_event = "DATA BIT"
116 if (self.lnk_bit) : self.lnk_state = 'WAIT FOR FALLING EDGE'
117 else : self.lnk_state = 'WAIT FOR RISING EDGE'
118 elif self.lnk_state == 'WAIT FOR RISING EDGE':
119 # The end of a cycle is a rising edge.
121 # A reset cycle is longer than 8T
122 if (self.samplenum - self.lnk_fall > 8*self.time_base):
123 # Save the sample number for the falling edge.
124 self.lnk_rise = self.samplenum
125 # Send a reset event to the next protocol layer
126 self.lnk_event = "RESET"
127 self.lnk_state = "WAIT FOR PRESENCE DETECT"
128 elif self.lnk_state == 'WAIT FOR PRESENCE DETECT':
129 # Data should be sample one 'time unit' after a falling edge
130 if (self.samplenum - self.lnk_rise == 2.5*self.time_base):
131 self.lnk_bit = owr & 0x1
132 self.lnk_event = "PRESENCE DETECT"
133 if (self.lnk_bit) : self.lnk_state = 'WAIT FOR FALLING EDGE'
134 else : self.lnk_state = 'WAIT FOR RISING EDGE'
136 raise Exception('Invalid lnk_state: %s' % self.lnk_state)
141 self.net_event = "RESET"
143 if (self.lnk_event == "RESET"):
144 self.net_state = "WAIT FOR COMMAND"
147 elif (self.lnk_event == "DATA BIT"):
148 if (self.net_state == "WAIT FOR COMMAND"):
149 self.net_cnt = self.net_cnt + 1
150 self.net_cmd = (self.net_cmd << 1) & self.lnk_bit
151 if (self.lnk_cnt == 8):
152 self.put(self.startsample, self.samplenum,
153 self.out_proto, ['LNK: BYTE', self.lnk_byte])
154 self.put(self.startsample, self.samplenum, self.out_ann,
155 [ANN_DEC, ['LNK: BYTE: ' + self.lnk_byte]])
156 if (self.net_cmd == 0x33):
159 elif (self.net_cmd == 0x0f):
162 elif (self.net_cmd == 0xcc):
165 elif (self.net_cmd == 0x55):
168 elif (self.net_cmd == 0xf0):
171 elif (self.net_cmd == 0x3c):
174 elif (self.net_cmd == 0x69):
175 # OVERDRIVE MATCH ROM
178 if (self.net_state == "WAIT FOR ROM"):
182 raise Exception('Invalid net_state: %s' % self.net_state)
183 elif not (self.lnk_event == "NONE"):
184 raise Exception('Invalid net_event: %s' % self.net_event)
188 # if (self.samplenum == self.lnk_start + 8*self.time_base):
189 # self.put(self.startsample, self.samplenum - 1, self.out_proto, ['RESET'])