]>
Commit | Line | Data |
---|---|---|
9cfb16e8 | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
9cfb16e8 IJ |
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 = [ | |
3f302d51 | 35 | {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire signal line'}, |
9cfb16e8 IJ |
36 | ] |
37 | optional_probes = [ | |
3f302d51 | 38 | {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power supply pin'}, |
9cfb16e8 IJ |
39 | ] |
40 | options = { | |
01520fa9 | 41 | 'overdrive': ['Overdrive mode', 'no'], |
497f196d UH |
42 | # Time options (specified in microseconds): |
43 | 'cnt_normal_bit': ['Normal mode sample bit time (us)', 15], | |
44 | 'cnt_normal_slot': ['Normal mode data slot time (us)', 60], | |
45 | 'cnt_normal_presence': ['Normal mode sample presence time (us)', 75], | |
46 | 'cnt_normal_reset': ['Normal mode reset time (us)', 480], | |
47 | 'cnt_overdrive_bit': ['Overdrive mode sample bit time (us)', 2], | |
48 | # 'cnt_overdrive_slot': ['Overdrive mode data slot time (us)', 7.3], | |
49 | 'cnt_overdrive_slot': ['Overdrive mode data slot time (us)', 7], | |
50 | 'cnt_overdrive_presence': ['Overdrive mode sample presence time (us)', 10], | |
51 | 'cnt_overdrive_reset': ['Overdrive mode reset time (us)', 48], | |
9cfb16e8 IJ |
52 | } |
53 | annotations = [ | |
938aa7fb UH |
54 | ['bit', 'Bit'], |
55 | ['warnings', 'Warnings'], | |
6f507573 UH |
56 | ['reset', 'Reset'], |
57 | ['presence', 'Presence'], | |
938aa7fb | 58 | ['overdrive', 'Overdrive mode notifications'], |
9cfb16e8 IJ |
59 | ] |
60 | ||
ddeb9b32 UH |
61 | def putm(self, data): |
62 | self.put(0, 0, self.out_ann, data) | |
63 | ||
64 | def putpb(self, data): | |
65 | self.put(self.fall, self.samplenum, self.out_proto, data) | |
66 | ||
67 | def putb(self, data): | |
68 | self.put(self.fall, self.samplenum, self.out_ann, data) | |
69 | ||
70 | def putx(self, data): | |
71 | self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, data) | |
72 | ||
6f507573 UH |
73 | def putfr(self, data): |
74 | self.put(self.fall, self.rise, self.out_ann, data) | |
75 | ||
76 | def putprs(self, data): | |
77 | self.put(self.rise, self.samplenum, self.out_proto, data) | |
78 | ||
79 | def putrs(self, data): | |
80 | self.put(self.rise, self.samplenum, self.out_ann, data) | |
81 | ||
9cfb16e8 | 82 | def __init__(self, **kwargs): |
f372d597 | 83 | self.samplerate = None |
9cfb16e8 | 84 | self.samplenum = 0 |
e7720d6c | 85 | self.state = 'WAIT FOR FALLING EDGE' |
9cfb16e8 | 86 | self.present = 0 |
e7720d6c | 87 | self.bit = 0 |
99f5f3b5 IJ |
88 | self.bit_cnt = 0 |
89 | self.command = 0 | |
9cfb16e8 | 90 | self.overdrive = 0 |
e7720d6c UH |
91 | self.fall = 0 |
92 | self.rise = 0 | |
9cfb16e8 | 93 | |
f372d597 | 94 | def start(self): |
be465111 BV |
95 | self.out_proto = self.register(srd.OUTPUT_PYTHON) |
96 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
9cfb16e8 | 97 | |
f372d597 BV |
98 | def metadata(self, key, value): |
99 | if key != srd.SRD_CONF_SAMPLERATE: | |
100 | return | |
101 | self.samplerate = value | |
e7720d6c UH |
102 | |
103 | # Check if samplerate is appropriate. | |
01520fa9 | 104 | if self.options['overdrive'] == 'yes': |
e7720d6c | 105 | if self.samplerate < 2000000: |
ddeb9b32 UH |
106 | self.putm([1, ['Sampling rate is too low. Must be above ' + |
107 | '2MHz for proper overdrive mode decoding.']]) | |
e7720d6c | 108 | elif self.samplerate < 5000000: |
ddeb9b32 UH |
109 | self.putm([1, ['Sampling rate is suggested to be above 5MHz ' + |
110 | 'for proper overdrive mode decoding.']]) | |
9cfb16e8 | 111 | else: |
e7720d6c | 112 | if self.samplerate < 400000: |
ddeb9b32 UH |
113 | self.putm([1, ['Sampling rate is too low. Must be above ' + |
114 | '400kHz for proper normal mode decoding.']]) | |
9cfb16e8 | 115 | elif (self.samplerate < 1000000): |
ddeb9b32 UH |
116 | self.putm([1, ['Sampling rate is suggested to be above ' + |
117 | '1MHz for proper normal mode decoding.']]) | |
9cfb16e8 | 118 | |
e7720d6c UH |
119 | # The default 1-Wire time base is 30us. This is used to calculate |
120 | # sampling times. | |
121 | samplerate = float(self.samplerate) | |
497f196d UH |
122 | |
123 | x = float(self.options['cnt_normal_bit']) / 1000000.0 | |
124 | self.cnt_normal_bit = int(samplerate * x) - 1 | |
125 | x = float(self.options['cnt_normal_slot']) / 1000000.0 | |
126 | self.cnt_normal_slot = int(samplerate * x) - 1 | |
127 | x = float(self.options['cnt_normal_presence']) / 1000000.0 | |
128 | self.cnt_normal_presence = int(samplerate * x) - 1 | |
129 | x = float(self.options['cnt_normal_reset']) / 1000000.0 | |
130 | self.cnt_normal_reset = int(samplerate * x) - 1 | |
131 | x = float(self.options['cnt_overdrive_bit']) / 1000000.0 | |
132 | self.cnt_overdrive_bit = int(samplerate * x) - 1 | |
133 | x = float(self.options['cnt_overdrive_slot']) / 1000000.0 | |
134 | self.cnt_overdrive_slot = int(samplerate * x) - 1 | |
135 | x = float(self.options['cnt_overdrive_presence']) / 1000000.0 | |
136 | self.cnt_overdrive_presence = int(samplerate * x) - 1 | |
137 | x = float(self.options['cnt_overdrive_reset']) / 1000000.0 | |
138 | self.cnt_overdrive_reset = int(samplerate * x) - 1 | |
9cfb16e8 | 139 | |
e7720d6c UH |
140 | # Organize values into lists. |
141 | self.cnt_bit = [self.cnt_normal_bit, self.cnt_overdrive_bit] | |
9cfb16e8 | 142 | self.cnt_presence = [self.cnt_normal_presence, self.cnt_overdrive_presence] |
e7720d6c UH |
143 | self.cnt_reset = [self.cnt_normal_reset, self.cnt_overdrive_reset] |
144 | self.cnt_slot = [self.cnt_normal_slot, self.cnt_overdrive_slot] | |
145 | ||
146 | # Check if sample times are in the allowed range. | |
147 | ||
148 | time_min = float(self.cnt_normal_bit) / self.samplerate | |
149 | time_max = float(self.cnt_normal_bit + 1) / self.samplerate | |
150 | if (time_min < 0.000005) or (time_max > 0.000015): | |
ddeb9b32 | 151 | self.putm([1, ['The normal mode data sample time interval ' + |
e7720d6c UH |
152 | '(%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).' |
153 | % (time_min * 1000000, time_max * 1000000)]]) | |
154 | ||
155 | time_min = float(self.cnt_normal_presence) / self.samplerate | |
156 | time_max = float(self.cnt_normal_presence + 1) / self.samplerate | |
157 | if (time_min < 0.0000681) or (time_max > 0.000075): | |
ddeb9b32 | 158 | self.putm([1, ['The normal mode presence sample time interval ' + |
e7720d6c UH |
159 | '(%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).' |
160 | % (time_min * 1000000, time_max * 1000000)]]) | |
161 | ||
162 | time_min = float(self.cnt_overdrive_bit) / self.samplerate | |
163 | time_max = float(self.cnt_overdrive_bit + 1) / self.samplerate | |
164 | if (time_min < 0.000001) or (time_max > 0.000002): | |
ddeb9b32 | 165 | self.putm([1, ['The overdrive mode data sample time interval ' + |
e7720d6c UH |
166 | '(%2.1fus-%2.1fus) should be inside (1.0us, 2.0us).' |
167 | % (time_min * 1000000, time_max * 1000000)]]) | |
168 | ||
169 | time_min = float(self.cnt_overdrive_presence) / self.samplerate | |
170 | time_max = float(self.cnt_overdrive_presence + 1) / self.samplerate | |
171 | if (time_min < 0.0000073) or (time_max > 0.000010): | |
ddeb9b32 | 172 | self.putm([1, ['The overdrive mode presence sample time interval ' + |
e7720d6c UH |
173 | '(%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).' |
174 | % (time_min*1000000, time_max*1000000)]]) | |
9cfb16e8 | 175 | |
9cfb16e8 | 176 | def decode(self, ss, es, data): |
f372d597 BV |
177 | if self.samplerate is None: |
178 | raise Exception("Cannot decode without samplerate.") | |
9cfb16e8 | 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 | ||
4e980c20 | 207 | self.putb([0, ['Bit: %d' % self.bit, '%d' % self.bit]]) |
ddeb9b32 | 208 | self.putpb(['BIT', self.bit]) |
e7720d6c | 209 | |
48b59746 UH |
210 | # Checking the first command to see if overdrive mode |
211 | # should be entered. | |
212 | if self.bit_cnt <= 8: | |
213 | self.command |= (self.bit << self.bit_cnt) | |
214 | elif self.bit_cnt == 8 and self.command in [0x3c, 0x69]: | |
4e980c20 | 215 | self.putx([4, ['Entering overdrive mode', 'Overdrive on']]) |
48b59746 UH |
216 | # Increment the bit counter. |
217 | self.bit_cnt += 1 | |
218 | # Wait for next slot. | |
219 | self.state = 'WAIT FOR FALLING EDGE' | |
9cfb16e8 IJ |
220 | elif self.state == 'WAIT FOR RISING EDGE': |
221 | # The end of a cycle is a rising edge. | |
48b59746 UH |
222 | if owr != 1: |
223 | continue | |
224 | ||
225 | # Check if this was a reset cycle. | |
226 | t = self.samplenum - self.fall | |
227 | if t > self.cnt_normal_reset: | |
015dc33a | 228 | # Save the sample number for the rising edge. |
48b59746 | 229 | self.rise = self.samplenum |
4e980c20 | 230 | self.putfr([2, ['Reset', 'Rst', 'R']]) |
48b59746 UH |
231 | self.state = 'WAIT FOR PRESENCE DETECT' |
232 | # Exit overdrive mode. | |
233 | if self.overdrive: | |
4e980c20 | 234 | self.putx([4, ['Exiting overdrive mode', 'Overdrive off']]) |
48b59746 UH |
235 | self.overdrive = 0 |
236 | # Clear command bit counter and data register. | |
237 | self.bit_cnt = 0 | |
238 | self.command = 0 | |
239 | elif (t > self.cnt_overdrive_reset) and self.overdrive: | |
015dc33a | 240 | # Save the sample number for the rising edge. |
48b59746 | 241 | self.rise = self.samplenum |
4e980c20 | 242 | self.putfr([2, ['Reset', 'Rst', 'R']]) |
48b59746 UH |
243 | self.state = "WAIT FOR PRESENCE DETECT" |
244 | # Otherwise this is assumed to be a data bit. | |
245 | else: | |
246 | self.state = "WAIT FOR FALLING EDGE" | |
9cfb16e8 | 247 | elif self.state == 'WAIT FOR PRESENCE DETECT': |
e7720d6c UH |
248 | # Sample presence status. |
249 | t = self.samplenum - self.rise | |
250 | if t == self.cnt_presence[self.overdrive]: | |
671cc300 IJ |
251 | self.present = owr |
252 | self.state = 'WAIT FOR RESET SLOT END' | |
253 | elif self.state == 'WAIT FOR RESET SLOT END': | |
48b59746 | 254 | # A reset slot ends in a long recovery period. |
e7720d6c | 255 | t = self.samplenum - self.rise |
48b59746 UH |
256 | if t != self.cnt_reset[self.overdrive]: |
257 | continue | |
258 | ||
259 | if owr == 0: | |
260 | # This seems to be a reset slot, wait for its end. | |
261 | self.state = 'WAIT FOR RISING EDGE' | |
262 | continue | |
263 | ||
ddeb9b32 | 264 | p = 'false' if self.present else 'true' |
4e980c20 | 265 | self.putrs([3, ['Presence: %s' % p, 'Presence', 'Pres', 'P']]) |
6f507573 | 266 | self.putprs(['RESET/PRESENCE', not self.present]) |
ddeb9b32 | 267 | |
48b59746 UH |
268 | # Wait for next slot. |
269 | self.state = 'WAIT FOR FALLING EDGE' | |
9cfb16e8 | 270 | else: |
e7720d6c | 271 | raise Exception('Invalid state: %s' % self.state) |