]>
Commit | Line | Data |
---|---|---|
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 | ||
e7720d6c | 21 | # 1-Wire protocol decoder (link layer) |
9cfb16e8 IJ |
22 | |
23 | import sigrokdecode as srd | |
24 | ||
25 | class Decoder(srd.Decoder): | |
26 | api_version = 1 | |
27 | id = 'onewire_link' | |
28 | name = '1-Wire link layer' | |
e7720d6c | 29 | longname = '1-Wire serial communication bus (link layer)' |
9cfb16e8 IJ |
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 = { | |
e7720d6c UH |
41 | 'overdrive': ['Overdrive', 1], |
42 | # Time options (specified in number of samplerate periods): | |
43 | 'cnt_normal_bit': ['Normal mode sample bit time', 0], | |
44 | 'cnt_normal_slot': ['Normal mode data slot time', 0], | |
45 | 'cnt_normal_presence': ['Normal mode sample presence time', 0], | |
46 | 'cnt_normal_reset': ['Normal mode reset time', 0], | |
47 | 'cnt_overdrive_bit': ['Overdrive mode sample bit time', 0], | |
48 | 'cnt_overdrive_slot': ['Overdrive mode data slot time', 0], | |
49 | 'cnt_overdrive_presence': ['Overdrive mode sample presence time', 0], | |
50 | 'cnt_overdrive_reset': ['Overdrive mode reset time', 0], | |
9cfb16e8 IJ |
51 | } |
52 | annotations = [ | |
53 | ['Link', 'Link layer events (reset, presence, bit slots)'], | |
54 | ] | |
55 | ||
56 | def __init__(self, **kwargs): | |
9cfb16e8 IJ |
57 | self.samplenum = 0 |
58 | # Link layer variables | |
e7720d6c | 59 | self.state = 'WAIT FOR FALLING EDGE' |
9cfb16e8 | 60 | self.present = 0 |
e7720d6c | 61 | self.bit = 0 |
99f5f3b5 IJ |
62 | self.bit_cnt = 0 |
63 | self.command = 0 | |
9cfb16e8 | 64 | self.overdrive = 0 |
9cfb16e8 | 65 | # Event timing variables |
e7720d6c UH |
66 | self.fall = 0 |
67 | self.rise = 0 | |
9cfb16e8 IJ |
68 | |
69 | def start(self, metadata): | |
70 | self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire_link') | |
e7720d6c | 71 | self.out_ann = self.add(srd.OUTPUT_ANN, 'onewire_link') |
9cfb16e8 | 72 | |
9cfb16e8 | 73 | self.samplerate = metadata['samplerate'] |
e7720d6c UH |
74 | |
75 | # Check if samplerate is appropriate. | |
76 | if self.options['overdrive']: | |
9cfb16e8 | 77 | self.put(0, 0, self.out_ann, [0, |
e7720d6c UH |
78 | ['NOTE: Sample rate checks assume overdrive mode.']]) |
79 | if self.samplerate < 2000000: | |
9cfb16e8 | 80 | self.put(0, 0, self.out_ann, [0, |
e7720d6c UH |
81 | ['ERROR: Sampling rate is too low. Must be above 2MHz ' + |
82 | 'for proper overdrive mode decoding.']]) | |
83 | elif self.samplerate < 5000000: | |
9cfb16e8 | 84 | self.put(0, 0, self.out_ann, [0, |
e7720d6c UH |
85 | ['WARNING: Sampling rate is suggested to be above 5MHz ' + |
86 | 'for proper overdrive mode decoding.']]) | |
9cfb16e8 IJ |
87 | else: |
88 | self.put(0, 0, self.out_ann, [0, | |
e7720d6c UH |
89 | ['NOTE: Sample rate checks assume normal mode only.']]) |
90 | if self.samplerate < 400000: | |
9cfb16e8 | 91 | self.put(0, 0, self.out_ann, [0, |
e7720d6c UH |
92 | ['ERROR: Sampling rate is too low. Must be above ' + |
93 | '400kHz for proper normal mode decoding.']]) | |
9cfb16e8 IJ |
94 | elif (self.samplerate < 1000000): |
95 | self.put(0, 0, self.out_ann, [0, | |
e7720d6c UH |
96 | ['WARNING: Sampling rate is suggested to be above ' + |
97 | '1MHz for proper normal mode decoding.']]) | |
9cfb16e8 | 98 | |
e7720d6c UH |
99 | # The default 1-Wire time base is 30us. This is used to calculate |
100 | # sampling times. | |
101 | samplerate = float(self.samplerate) | |
102 | if self.options['cnt_normal_bit']: | |
9cfb16e8 IJ |
103 | self.cnt_normal_bit = self.options['cnt_normal_bit'] |
104 | else: | |
e7720d6c UH |
105 | self.cnt_normal_bit = int(samplerate * 0.000015) - 1 # 15ns |
106 | if self.options['cnt_normal_slot']: | |
671cc300 IJ |
107 | self.cnt_normal_slot = self.options['cnt_normal_slot'] |
108 | else: | |
e7720d6c UH |
109 | self.cnt_normal_slot = int(samplerate * 0.000060) - 1 # 60ns |
110 | if self.options['cnt_normal_presence']: | |
9cfb16e8 IJ |
111 | self.cnt_normal_presence = self.options['cnt_normal_presence'] |
112 | else: | |
e7720d6c UH |
113 | self.cnt_normal_presence = int(samplerate * 0.000075) - 1 # 75ns |
114 | if self.options['cnt_normal_reset']: | |
9cfb16e8 IJ |
115 | self.cnt_normal_reset = self.options['cnt_normal_reset'] |
116 | else: | |
e7720d6c UH |
117 | self.cnt_normal_reset = int(samplerate * 0.000480) - 1 # 480ns |
118 | if self.options['cnt_overdrive_bit']: | |
9cfb16e8 IJ |
119 | self.cnt_overdrive_bit = self.options['cnt_overdrive_bit'] |
120 | else: | |
e7720d6c UH |
121 | self.cnt_overdrive_bit = int(samplerate * 0.000002) - 1 # 2ns |
122 | if self.options['cnt_overdrive_slot']: | |
671cc300 IJ |
123 | self.cnt_overdrive_slot = self.options['cnt_overdrive_slot'] |
124 | else: | |
e7720d6c UH |
125 | self.cnt_overdrive_slot = int(samplerate * 0.0000073) - 1 # 6ns+1.3ns |
126 | if self.options['cnt_overdrive_presence']: | |
9cfb16e8 IJ |
127 | self.cnt_overdrive_presence = self.options['cnt_overdrive_presence'] |
128 | else: | |
e7720d6c UH |
129 | self.cnt_overdrive_presence = int(samplerate * 0.000010) - 1 # 10ns |
130 | if self.options['cnt_overdrive_reset']: | |
9cfb16e8 IJ |
131 | self.cnt_overdrive_reset = self.options['cnt_overdrive_reset'] |
132 | else: | |
e7720d6c | 133 | self.cnt_overdrive_reset = int(samplerate * 0.000048) - 1 # 48ns |
9cfb16e8 | 134 | |
e7720d6c UH |
135 | # Organize values into lists. |
136 | self.cnt_bit = [self.cnt_normal_bit, self.cnt_overdrive_bit] | |
9cfb16e8 | 137 | self.cnt_presence = [self.cnt_normal_presence, self.cnt_overdrive_presence] |
e7720d6c UH |
138 | self.cnt_reset = [self.cnt_normal_reset, self.cnt_overdrive_reset] |
139 | self.cnt_slot = [self.cnt_normal_slot, self.cnt_overdrive_slot] | |
140 | ||
141 | # Check if sample times are in the allowed range. | |
142 | ||
143 | time_min = float(self.cnt_normal_bit) / self.samplerate | |
144 | time_max = float(self.cnt_normal_bit + 1) / self.samplerate | |
145 | if (time_min < 0.000005) or (time_max > 0.000015): | |
146 | self.put(0, 0, self.out_ann, [0, | |
147 | ['WARNING: The normal mode data sample time interval ' + | |
148 | '(%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).' | |
149 | % (time_min * 1000000, time_max * 1000000)]]) | |
150 | ||
151 | time_min = float(self.cnt_normal_presence) / self.samplerate | |
152 | time_max = float(self.cnt_normal_presence + 1) / self.samplerate | |
153 | if (time_min < 0.0000681) or (time_max > 0.000075): | |
154 | self.put(0, 0, self.out_ann, [0, | |
155 | ['WARNING: The normal mode presence sample time interval ' + | |
156 | '(%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).' | |
157 | % (time_min * 1000000, time_max * 1000000)]]) | |
158 | ||
159 | time_min = float(self.cnt_overdrive_bit) / self.samplerate | |
160 | time_max = float(self.cnt_overdrive_bit + 1) / self.samplerate | |
161 | if (time_min < 0.000001) or (time_max > 0.000002): | |
162 | self.put(0, 0, self.out_ann, [0, | |
163 | ['WARNING: The overdrive mode data sample time interval ' + | |
164 | '(%2.1fus-%2.1fus) should be inside (1.0us, 2.0us).' | |
165 | % (time_min * 1000000, time_max * 1000000)]]) | |
166 | ||
167 | time_min = float(self.cnt_overdrive_presence) / self.samplerate | |
168 | time_max = float(self.cnt_overdrive_presence + 1) / self.samplerate | |
169 | if (time_min < 0.0000073) or (time_max > 0.000010): | |
170 | self.put(0, 0, self.out_ann, [0, | |
171 | ['WARNING: The overdrive mode presence sample time interval ' + | |
172 | '(%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).' | |
173 | % (time_min*1000000, time_max*1000000)]]) | |
9cfb16e8 IJ |
174 | |
175 | def report(self): | |
176 | pass | |
177 | ||
178 | def decode(self, ss, es, data): | |
179 | for (self.samplenum, (owr, pwr)) in data: | |
9cfb16e8 IJ |
180 | # State machine. |
181 | if self.state == 'WAIT FOR FALLING EDGE': | |
182 | # The start of a cycle is a falling edge. | |
48b59746 UH |
183 | if owr != 0: |
184 | continue | |
185 | # Save the sample number for the falling edge. | |
186 | self.fall = self.samplenum | |
187 | # Go to waiting for sample time. | |
188 | self.state = 'WAIT FOR DATA SAMPLE' | |
9cfb16e8 | 189 | elif self.state == 'WAIT FOR DATA SAMPLE': |
e7720d6c UH |
190 | # Sample data bit. |
191 | t = self.samplenum - self.fall | |
192 | if t == self.cnt_bit[self.overdrive]: | |
193 | self.bit = owr | |
671cc300 IJ |
194 | self.state = 'WAIT FOR DATA SLOT END' |
195 | elif self.state == 'WAIT FOR DATA SLOT END': | |
e7720d6c UH |
196 | # A data slot ends in a recovery period, otherwise, this is |
197 | # probably a reset. | |
198 | t = self.samplenum - self.fall | |
48b59746 UH |
199 | if t != self.cnt_slot[self.overdrive]: |
200 | continue | |
201 | ||
202 | if owr == 0: | |
203 | # This seems to be a reset slot, wait for its end. | |
204 | self.state = 'WAIT FOR RISING EDGE' | |
205 | continue | |
206 | ||
207 | self.put(self.fall, self.samplenum, self.out_ann, | |
208 | [0, ['BIT: %01x' % self.bit]]) | |
209 | self.put(self.fall, self.samplenum, self.out_proto, | |
210 | ['BIT', self.bit]) | |
e7720d6c | 211 | |
48b59746 UH |
212 | # Checking the first command to see if overdrive mode |
213 | # should be entered. | |
214 | if self.bit_cnt <= 8: | |
215 | self.command |= (self.bit << self.bit_cnt) | |
216 | elif self.bit_cnt == 8 and self.command in [0x3c, 0x69]: | |
217 | self.put(self.fall, self.cnt_bit[self.overdrive], | |
218 | self.out_ann, [0, ['ENTER OVERDRIVE MODE']]) | |
219 | # Increment the bit counter. | |
220 | self.bit_cnt += 1 | |
221 | # Wait for next slot. | |
222 | self.state = 'WAIT FOR FALLING EDGE' | |
9cfb16e8 IJ |
223 | elif self.state == 'WAIT FOR RISING EDGE': |
224 | # The end of a cycle is a rising edge. | |
48b59746 UH |
225 | if owr != 1: |
226 | continue | |
227 | ||
228 | # Check if this was a reset cycle. | |
229 | t = self.samplenum - self.fall | |
230 | if t > self.cnt_normal_reset: | |
231 | # Save the sample number for the falling edge. | |
232 | self.rise = self.samplenum | |
233 | self.state = 'WAIT FOR PRESENCE DETECT' | |
234 | # Exit overdrive mode. | |
235 | if self.overdrive: | |
236 | self.put(self.fall, self.cnt_bit[self.overdrive], | |
237 | self.out_ann, [0, ['EXIT OVERDRIVE MODE']]) | |
238 | self.overdrive = 0 | |
239 | # Clear command bit counter and data register. | |
240 | self.bit_cnt = 0 | |
241 | self.command = 0 | |
242 | elif (t > self.cnt_overdrive_reset) and self.overdrive: | |
243 | # Save the sample number for the falling edge. | |
244 | self.rise = self.samplenum | |
245 | self.state = "WAIT FOR PRESENCE DETECT" | |
246 | # Otherwise this is assumed to be a data bit. | |
247 | else: | |
248 | self.state = "WAIT FOR FALLING EDGE" | |
9cfb16e8 | 249 | elif self.state == 'WAIT FOR PRESENCE DETECT': |
e7720d6c UH |
250 | # Sample presence status. |
251 | t = self.samplenum - self.rise | |
252 | if t == self.cnt_presence[self.overdrive]: | |
671cc300 IJ |
253 | self.present = owr |
254 | self.state = 'WAIT FOR RESET SLOT END' | |
255 | elif self.state == 'WAIT FOR RESET SLOT END': | |
48b59746 | 256 | # A reset slot ends in a long recovery period. |
e7720d6c | 257 | t = self.samplenum - self.rise |
48b59746 UH |
258 | if t != self.cnt_reset[self.overdrive]: |
259 | continue | |
260 | ||
261 | if owr == 0: | |
262 | # This seems to be a reset slot, wait for its end. | |
263 | self.state = 'WAIT FOR RISING EDGE' | |
264 | continue | |
265 | ||
266 | self.put(self.fall, self.samplenum, self.out_ann, | |
267 | [0, ['RESET/PRESENCE: %s' | |
268 | % ('False' if self.present else 'True')]]) | |
269 | self.put(self.fall, self.samplenum, self.out_proto, | |
270 | ['RESET/PRESENCE', not self.present]) | |
271 | # Wait for next slot. | |
272 | self.state = 'WAIT FOR FALLING EDGE' | |
9cfb16e8 | 273 | else: |
e7720d6c | 274 | raise Exception('Invalid state: %s' % self.state) |