]> sigrok.org Git - libsigrokdecode.git/blame - decoders/onewire_link/onewire_link.py
onewire: the split of the protocol into layers works now
[libsigrokdecode.git] / decoders / onewire_link / onewire_link.py
CommitLineData
9cfb16e8
IJ
1##
2## This file is part of the sigrok project.
3##
4## Copyright (C) 2012 Iztok Jeras <iztok.jeras@gmail.com>
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 link layer protocol decoder
22
23import sigrokdecode as srd
24
25class Decoder(srd.Decoder):
26 api_version = 1
27 id = 'onewire_link'
28 name = '1-Wire link layer'
29 longname = '1-Wire serial communication bus'
30 desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = ['onewire_link']
34 probes = [
35 {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire bus'},
36 ]
37 optional_probes = [
38 {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power'},
39 ]
40 options = {
41 'overdrive' : ['Overdrive', 1],
42 'cnt_normal_bit' : ['Time (in samplerate periods) for normal mode sample bit' , 0],
43 'cnt_normal_presence' : ['Time (in samplerate periods) for normal mode sample presence', 0],
44 'cnt_normal_reset' : ['Time (in samplerate periods) for normal mode reset' , 0],
45 'cnt_overdrive_bit' : ['Time (in samplerate periods) for overdrive mode sample bit' , 0],
46 'cnt_overdrive_presence': ['Time (in samplerate periods) for overdrive mode sample presence', 0],
47 'cnt_overdrive_reset' : ['Time (in samplerate periods) for overdrive mode reset' , 0],
48 }
49 annotations = [
50 ['Link', 'Link layer events (reset, presence, bit slots)'],
51 ]
52
53 def __init__(self, **kwargs):
54 # Common variables
55 self.samplenum = 0
56 # Link layer variables
57 self.state = 'WAIT FOR FALLING EDGE'
58 self.present = 0
59 self.bit = 0
99f5f3b5
IJ
60 self.bit_cnt = 0
61 self.command = 0
9cfb16e8 62 self.overdrive = 0
9cfb16e8
IJ
63 # Event timing variables
64 self.fall = 0
65 self.rise = 0
66
67 def start(self, metadata):
68 self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire_link')
69 self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire_link')
70
71 # check if samplerate is appropriate
72 self.samplerate = metadata['samplerate']
73 if (self.options['overdrive']):
74 self.put(0, 0, self.out_ann, [0,
75 ['NOTE: Sample rate checks assume overdrive mode.']])
76 if (self.samplerate < 2000000):
77 self.put(0, 0, self.out_ann, [0,
78 ['ERROR: Sampling rate is too low must be above 2MHz for proper overdrive mode decoding.']])
79 elif (self.samplerate < 5000000):
80 self.put(0, 0, self.out_ann, [0,
81 ['WARNING: Sampling rate is suggested to be above 5MHz for proper overdrive mode decoding.']])
82 else:
83 self.put(0, 0, self.out_ann, [0,
84 ['NOTE: Sample rate checks assume normal mode only.']])
85 if (self.samplerate < 400000):
86 self.put(0, 0, self.out_ann, [0,
87 ['ERROR: Sampling rate is too low must be above 400kHz for proper normal mode decoding.']])
88 elif (self.samplerate < 1000000):
89 self.put(0, 0, self.out_ann, [0,
90 ['WARNING: Sampling rate is suggested to be above 1MHz for proper normal mode decoding.']])
91
92 # The default 1-Wire time base is 30us, this is used to calculate sampling times.
93 if (self.options['cnt_normal_bit']):
94 self.cnt_normal_bit = self.options['cnt_normal_bit']
95 else:
96 self.cnt_normal_bit = int(float(self.samplerate) * 0.000015) - 1 # 15ns
97 if (self.options['cnt_normal_presence']):
98 self.cnt_normal_presence = self.options['cnt_normal_presence']
99 else:
100 self.cnt_normal_presence = int(float(self.samplerate) * 0.000075) - 1 # 75ns
101 if (self.options['cnt_normal_reset']):
102 self.cnt_normal_reset = self.options['cnt_normal_reset']
103 else:
104 self.cnt_normal_reset = int(float(self.samplerate) * 0.000480) - 1 # 480ns
105 if (self.options['cnt_overdrive_bit']):
106 self.cnt_overdrive_bit = self.options['cnt_overdrive_bit']
107 else:
108 self.cnt_overdrive_bit = int(float(self.samplerate) * 0.000002) - 1 # 2ns
109 if (self.options['cnt_overdrive_presence']):
110 self.cnt_overdrive_presence = self.options['cnt_overdrive_presence']
111 else:
112 self.cnt_overdrive_presence = int(float(self.samplerate) * 0.000010) - 1 # 10ns
113 if (self.options['cnt_overdrive_reset']):
114 self.cnt_overdrive_reset = self.options['cnt_overdrive_reset']
115 else:
116 self.cnt_overdrive_reset = int(float(self.samplerate) * 0.000048) - 1 # 48ns
117
118 # calculating the slot size
119 self.cnt_normal_slot = int(float(self.samplerate) * 0.000060) - 1 # 60ns
120 self.cnt_overdrive_slot = int(float(self.samplerate) * 0.000006) - 1 # 6ns
121
122 # organize values into lists
123 self.cnt_bit = [self.cnt_normal_bit , self.cnt_overdrive_bit ]
124 self.cnt_presence = [self.cnt_normal_presence, self.cnt_overdrive_presence]
125 self.cnt_reset = [self.cnt_normal_reset , self.cnt_overdrive_reset ]
126 self.cnt_slot = [self.cnt_normal_slot , self.cnt_overdrive_slot ]
127
128 # Check if sample times are in the allowed range
129 time_min = float(self.cnt_normal_bit ) / self.samplerate
130 time_max = float(self.cnt_normal_bit+1) / self.samplerate
131 if ( (time_min < 0.000005) or (time_max > 0.000015) ) :
132 self.put(0, 0, self.out_ann, [0,
133 ['WARNING: The normal mode data sample time interval (%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).'
134 % (time_min*1000000, time_max*1000000)]])
135 time_min = float(self.cnt_normal_presence ) / self.samplerate
136 time_max = float(self.cnt_normal_presence+1) / self.samplerate
137 if ( (time_min < 0.0000681) or (time_max > 0.000075) ) :
138 self.put(0, 0, self.out_ann, [0,
139 ['WARNING: The normal mode presence sample time interval (%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).'
140 % (time_min*1000000, time_max*1000000)]])
141 time_min = float(self.cnt_overdrive_bit ) / self.samplerate
142 time_max = float(self.cnt_overdrive_bit+1) / self.samplerate
143 if ( (time_min < 0.000001) or (time_max > 0.000002) ) :
144 self.put(0, 0, self.out_ann, [0,
145 ['WARNING: The overdrive mode data sample time interval (%2.1fus-%2.1fus) should be inside (1.0us, 2.0us).'
146 % (time_min*1000000, time_max*1000000)]])
147 time_min = float(self.cnt_overdrive_presence ) / self.samplerate
148 time_max = float(self.cnt_overdrive_presence+1) / self.samplerate
149 if ( (time_min < 0.0000073) or (time_max > 0.000010) ) :
150 self.put(0, 0, self.out_ann, [0,
151 ['WARNING: The overdrive mode presence sample time interval (%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).'
152 % (time_min*1000000, time_max*1000000)]])
153
154 def report(self):
155 pass
156
157 def decode(self, ss, es, data):
158 for (self.samplenum, (owr, pwr)) in data:
159
160 # State machine.
161 if self.state == 'WAIT FOR FALLING EDGE':
162 # The start of a cycle is a falling edge.
163 if (owr == 0):
164 # Save the sample number for the falling edge.
165 self.fall = self.samplenum
166 # Go to waiting for sample time
167 self.state = 'WAIT FOR DATA SAMPLE'
168 elif self.state == 'WAIT FOR DATA SAMPLE':
169 # Sample data bit
170 if (self.samplenum - self.fall == self.cnt_bit[self.overdrive]):
171 self.bit = owr & 0x1
172 if (self.bit): self.state = 'WAIT FOR FALLING EDGE'
173 else : self.state = 'WAIT FOR RISING EDGE'
174 self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, [0, ['BIT: %01x' % self.bit]])
99f5f3b5 175 self.put(self.fall, self.cnt_bit[self.overdrive], self.out_proto, ['BIT', self.bit])
9cfb16e8 176 # Checking the first command to see if overdrive mode should be entered
99f5f3b5
IJ
177 if (self.bit_cnt <= 8):
178 self.command = self.command | (self.bit << self.bit_cnt)
179 elif (self.bit_cnt == 8):
9cfb16e8
IJ
180 if (self.command in [0x3c, 0x69]):
181 self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, [0, ['ENTER OVERDRIVE MODE']])
182 # incrementing the bit counter
183 self.bit_cnt += 1
184 elif self.state == 'WAIT FOR RISING EDGE':
185 # The end of a cycle is a rising edge.
186 if (owr == 1):
187 # Check if this was a reset cycle
188 if (self.samplenum - self.fall > self.cnt_normal_reset):
189 # Save the sample number for the falling edge.
190 self.rise = self.samplenum
191 self.state = "WAIT FOR PRESENCE DETECT"
192 self.put(self.fall, self.rise, self.out_ann, [0, ['RESET']])
193 self.put(self.fall, self.rise, self.out_proto, ['RESET', 0])
194 # Reset the timer.
195 self.fall = self.samplenum
196 # Exit overdrive mode
197 self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, [0, ['EXIT OVERDRIVE MODE']])
198 self.overdrive = 0
99f5f3b5 199 self.bit_cnt = 0
9cfb16e8
IJ
200 self.command = 0
201 elif ((self.samplenum - self.fall > self.cnt_overdrive_reset) and (self.overdrive)):
202 # Save the sample number for the falling edge.
203 self.rise = self.samplenum
204 self.state = "WAIT FOR PRESENCE DETECT"
205 self.put(self.fall, self.rise, self.out_ann, [0, ['RESET']])
206 self.put(self.fall, self.rise, self.out_proto, ['RESET', 0])
207 # Reset the timer.
208 self.fall = self.samplenum
209 # Otherwise this is assumed to be a data bit.
210 else :
211 self.state = "WAIT FOR FALLING EDGE"
212 elif self.state == 'WAIT FOR PRESENCE DETECT':
213 # Sample presence status
214 if (self.samplenum - self.rise == self.cnt_presence[self.overdrive]):
215 self.present = owr & 0x1
216 # Save the sample number for the falling edge.
217 if not (self.present) : self.fall = self.samplenum
218 # create presence detect event
219 if (self.present) : self.state = 'WAIT FOR FALLING EDGE'
220 else : self.state = 'WAIT FOR RISING EDGE'
221 self.put(self.samplenum, 0, self.out_ann, [0, ['PRESENCE: ' + "False" if self.present else "True"]])
222 self.put(self.samplenum, 0, self.out_proto, ['PRESENCE', self.present])
223 else:
224 raise Exception('Invalid state: %d' % self.state)