]>
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 | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
9cfb16e8 IJ |
18 | ## |
19 | ||
9cfb16e8 IJ |
20 | import sigrokdecode as srd |
21 | ||
21cda951 UH |
22 | class SamplerateError(Exception): |
23 | pass | |
24 | ||
9cfb16e8 | 25 | class Decoder(srd.Decoder): |
d14e8033 | 26 | api_version = 3 |
9cfb16e8 IJ |
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'] | |
6a15597a | 34 | channels = ( |
3f302d51 | 35 | {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire signal line'}, |
da9bcbd9 | 36 | ) |
6a15597a | 37 | optional_channels = ( |
3f302d51 | 38 | {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power supply pin'}, |
da9bcbd9 | 39 | ) |
84c1c0b5 BV |
40 | options = ( |
41 | {'id': 'overdrive', | |
b0918d40 | 42 | 'desc': 'Overdrive mode', 'default': 'no', 'values': ('yes', 'no')}, |
497f196d | 43 | # Time options (specified in microseconds): |
84c1c0b5 | 44 | {'id': 'cnt_normal_bit', |
b0918d40 | 45 | 'desc': 'Normal mode sample bit time (μs)', 'default': 15}, |
84c1c0b5 | 46 | {'id': 'cnt_normal_slot', |
b0918d40 | 47 | 'desc': 'Normal mode data slot time (μs)', 'default': 60}, |
84c1c0b5 | 48 | {'id': 'cnt_normal_presence', |
b0918d40 | 49 | 'desc': 'Normal mode sample presence time (μs)', 'default': 75}, |
84c1c0b5 | 50 | {'id': 'cnt_normal_reset', |
b0918d40 | 51 | 'desc': 'Normal mode reset time (μs)', 'default': 480}, |
84c1c0b5 | 52 | {'id': 'cnt_overdrive_bit', |
b0918d40 | 53 | 'desc': 'Overdrive mode sample bit time (μs)', 'default': 2}, |
84c1c0b5 | 54 | {'id': 'cnt_overdrive_slot', |
b0918d40 | 55 | 'desc': 'Overdrive mode data slot time (μs)', 'default': 7.3}, |
84c1c0b5 | 56 | {'id': 'cnt_overdrive_presence', |
b0918d40 | 57 | 'desc': 'Overdrive mode sample presence time (μs)', 'default': 10}, |
84c1c0b5 | 58 | {'id': 'cnt_overdrive_reset', |
b0918d40 | 59 | 'desc': 'Overdrive mode reset time (μs)', 'default': 48}, |
84c1c0b5 | 60 | ) |
da9bcbd9 BV |
61 | annotations = ( |
62 | ('bit', 'Bit'), | |
63 | ('warnings', 'Warnings'), | |
64 | ('reset', 'Reset'), | |
65 | ('presence', 'Presence'), | |
66 | ('overdrive', 'Overdrive mode notifications'), | |
67 | ) | |
8e11415b UH |
68 | annotation_rows = ( |
69 | ('bits', 'Bits', (0, 2, 3)), | |
70 | ('info', 'Info', (4,)), | |
71 | ('warnings', 'Warnings', (1,)), | |
72 | ) | |
9cfb16e8 | 73 | |
ddeb9b32 UH |
74 | def putm(self, data): |
75 | self.put(0, 0, self.out_ann, data) | |
76 | ||
77 | def putpb(self, data): | |
c515eed7 | 78 | self.put(self.fall, self.samplenum, self.out_python, data) |
ddeb9b32 UH |
79 | |
80 | def putb(self, data): | |
81 | self.put(self.fall, self.samplenum, self.out_ann, data) | |
82 | ||
83 | def putx(self, data): | |
84 | self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, data) | |
85 | ||
6f507573 UH |
86 | def putfr(self, data): |
87 | self.put(self.fall, self.rise, self.out_ann, data) | |
88 | ||
89 | def putprs(self, data): | |
c515eed7 | 90 | self.put(self.rise, self.samplenum, self.out_python, data) |
6f507573 UH |
91 | |
92 | def putrs(self, data): | |
93 | self.put(self.rise, self.samplenum, self.out_ann, data) | |
94 | ||
92b7b49f | 95 | def __init__(self): |
f372d597 | 96 | self.samplerate = None |
e7720d6c | 97 | self.state = 'WAIT FOR FALLING EDGE' |
9cfb16e8 | 98 | self.present = 0 |
e7720d6c | 99 | self.bit = 0 |
99f5f3b5 IJ |
100 | self.bit_cnt = 0 |
101 | self.command = 0 | |
9cfb16e8 | 102 | self.overdrive = 0 |
e7720d6c UH |
103 | self.fall = 0 |
104 | self.rise = 0 | |
9cfb16e8 | 105 | |
f372d597 | 106 | def start(self): |
c515eed7 | 107 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
be465111 | 108 | self.out_ann = self.register(srd.OUTPUT_ANN) |
9cfb16e8 | 109 | |
d14e8033 UH |
110 | self.initial_pins = [1, 1] |
111 | ||
3085037b | 112 | def checks(self): |
e7720d6c | 113 | # Check if samplerate is appropriate. |
01520fa9 | 114 | if self.options['overdrive'] == 'yes': |
e7720d6c | 115 | if self.samplerate < 2000000: |
ddeb9b32 UH |
116 | self.putm([1, ['Sampling rate is too low. Must be above ' + |
117 | '2MHz for proper overdrive mode decoding.']]) | |
e7720d6c | 118 | elif self.samplerate < 5000000: |
ddeb9b32 UH |
119 | self.putm([1, ['Sampling rate is suggested to be above 5MHz ' + |
120 | 'for proper overdrive mode decoding.']]) | |
9cfb16e8 | 121 | else: |
e7720d6c | 122 | if self.samplerate < 400000: |
ddeb9b32 UH |
123 | self.putm([1, ['Sampling rate is too low. Must be above ' + |
124 | '400kHz for proper normal mode decoding.']]) | |
3085037b | 125 | elif self.samplerate < 1000000: |
ddeb9b32 UH |
126 | self.putm([1, ['Sampling rate is suggested to be above ' + |
127 | '1MHz for proper normal mode decoding.']]) | |
9cfb16e8 | 128 | |
e7720d6c UH |
129 | # Check if sample times are in the allowed range. |
130 | ||
131 | time_min = float(self.cnt_normal_bit) / self.samplerate | |
132 | time_max = float(self.cnt_normal_bit + 1) / self.samplerate | |
133 | if (time_min < 0.000005) or (time_max > 0.000015): | |
ddeb9b32 | 134 | self.putm([1, ['The normal mode data sample time interval ' + |
e7720d6c UH |
135 | '(%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).' |
136 | % (time_min * 1000000, time_max * 1000000)]]) | |
137 | ||
138 | time_min = float(self.cnt_normal_presence) / self.samplerate | |
139 | time_max = float(self.cnt_normal_presence + 1) / self.samplerate | |
140 | if (time_min < 0.0000681) or (time_max > 0.000075): | |
ddeb9b32 | 141 | self.putm([1, ['The normal mode presence sample time interval ' + |
e7720d6c UH |
142 | '(%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).' |
143 | % (time_min * 1000000, time_max * 1000000)]]) | |
144 | ||
145 | time_min = float(self.cnt_overdrive_bit) / self.samplerate | |
146 | time_max = float(self.cnt_overdrive_bit + 1) / self.samplerate | |
147 | if (time_min < 0.000001) or (time_max > 0.000002): | |
ddeb9b32 | 148 | self.putm([1, ['The overdrive mode data sample time interval ' + |
e7720d6c UH |
149 | '(%2.1fus-%2.1fus) should be inside (1.0us, 2.0us).' |
150 | % (time_min * 1000000, time_max * 1000000)]]) | |
151 | ||
152 | time_min = float(self.cnt_overdrive_presence) / self.samplerate | |
153 | time_max = float(self.cnt_overdrive_presence + 1) / self.samplerate | |
154 | if (time_min < 0.0000073) or (time_max > 0.000010): | |
ddeb9b32 | 155 | self.putm([1, ['The overdrive mode presence sample time interval ' + |
e7720d6c | 156 | '(%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).' |
35b380b1 | 157 | % (time_min * 1000000, time_max * 1000000)]]) |
9cfb16e8 | 158 | |
3085037b UH |
159 | |
160 | def metadata(self, key, value): | |
161 | if key != srd.SRD_CONF_SAMPLERATE: | |
162 | return | |
163 | self.samplerate = value | |
164 | ||
165 | # The default 1-Wire time base is 30us. This is used to calculate | |
166 | # sampling times. | |
167 | samplerate = float(self.samplerate) | |
168 | ||
169 | x = float(self.options['cnt_normal_bit']) / 1000000.0 | |
170 | self.cnt_normal_bit = int(samplerate * x) - 1 | |
171 | x = float(self.options['cnt_normal_slot']) / 1000000.0 | |
172 | self.cnt_normal_slot = int(samplerate * x) - 1 | |
173 | x = float(self.options['cnt_normal_presence']) / 1000000.0 | |
174 | self.cnt_normal_presence = int(samplerate * x) - 1 | |
175 | x = float(self.options['cnt_normal_reset']) / 1000000.0 | |
176 | self.cnt_normal_reset = int(samplerate * x) - 1 | |
177 | x = float(self.options['cnt_overdrive_bit']) / 1000000.0 | |
178 | self.cnt_overdrive_bit = int(samplerate * x) - 1 | |
179 | x = float(self.options['cnt_overdrive_slot']) / 1000000.0 | |
180 | self.cnt_overdrive_slot = int(samplerate * x) - 1 | |
181 | x = float(self.options['cnt_overdrive_presence']) / 1000000.0 | |
182 | self.cnt_overdrive_presence = int(samplerate * x) - 1 | |
183 | x = float(self.options['cnt_overdrive_reset']) / 1000000.0 | |
184 | self.cnt_overdrive_reset = int(samplerate * x) - 1 | |
185 | ||
186 | # Organize values into lists. | |
187 | self.cnt_bit = [self.cnt_normal_bit, self.cnt_overdrive_bit] | |
188 | self.cnt_presence = [self.cnt_normal_presence, self.cnt_overdrive_presence] | |
189 | self.cnt_reset = [self.cnt_normal_reset, self.cnt_overdrive_reset] | |
190 | self.cnt_slot = [self.cnt_normal_slot, self.cnt_overdrive_slot] | |
191 | ||
d14e8033 | 192 | def decode(self): |
21cda951 UH |
193 | if not self.samplerate: |
194 | raise SamplerateError('Cannot decode without samplerate.') | |
d14e8033 UH |
195 | self.checks() |
196 | while True: | |
9cfb16e8 IJ |
197 | # State machine. |
198 | if self.state == 'WAIT FOR FALLING EDGE': | |
d14e8033 UH |
199 | # The start of a cycle is a falling edge on OWR. |
200 | self.wait({0: 'f'}) | |
48b59746 UH |
201 | # Save the sample number for the falling edge. |
202 | self.fall = self.samplenum | |
48b59746 | 203 | self.state = 'WAIT FOR DATA SAMPLE' |
9cfb16e8 | 204 | elif self.state == 'WAIT FOR DATA SAMPLE': |
e7720d6c | 205 | # Sample data bit. |
d14e8033 UH |
206 | t = self.fall + self.cnt_bit[self.overdrive] |
207 | self.bit, pwr = self.wait({'skip': t - self.samplenum}) | |
208 | self.state = 'WAIT FOR DATA SLOT END' | |
671cc300 | 209 | elif self.state == 'WAIT FOR DATA SLOT END': |
e7720d6c UH |
210 | # A data slot ends in a recovery period, otherwise, this is |
211 | # probably a reset. | |
d14e8033 UH |
212 | t = self.fall + self.cnt_slot[self.overdrive] |
213 | owr, pwr = self.wait({'skip': t - self.samplenum}) | |
48b59746 UH |
214 | |
215 | if owr == 0: | |
216 | # This seems to be a reset slot, wait for its end. | |
217 | self.state = 'WAIT FOR RISING EDGE' | |
218 | continue | |
219 | ||
4e980c20 | 220 | self.putb([0, ['Bit: %d' % self.bit, '%d' % self.bit]]) |
ddeb9b32 | 221 | self.putpb(['BIT', self.bit]) |
e7720d6c | 222 | |
48b59746 UH |
223 | # Checking the first command to see if overdrive mode |
224 | # should be entered. | |
225 | if self.bit_cnt <= 8: | |
226 | self.command |= (self.bit << self.bit_cnt) | |
227 | elif self.bit_cnt == 8 and self.command in [0x3c, 0x69]: | |
4e980c20 | 228 | self.putx([4, ['Entering overdrive mode', 'Overdrive on']]) |
48b59746 UH |
229 | # Increment the bit counter. |
230 | self.bit_cnt += 1 | |
231 | # Wait for next slot. | |
232 | self.state = 'WAIT FOR FALLING EDGE' | |
9cfb16e8 IJ |
233 | elif self.state == 'WAIT FOR RISING EDGE': |
234 | # The end of a cycle is a rising edge. | |
d14e8033 | 235 | self.wait({0: 'r'}) |
48b59746 UH |
236 | |
237 | # Check if this was a reset cycle. | |
238 | t = self.samplenum - self.fall | |
239 | if t > self.cnt_normal_reset: | |
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 | # Exit overdrive mode. | |
245 | if self.overdrive: | |
4e980c20 | 246 | self.putx([4, ['Exiting overdrive mode', 'Overdrive off']]) |
48b59746 UH |
247 | self.overdrive = 0 |
248 | # Clear command bit counter and data register. | |
249 | self.bit_cnt = 0 | |
250 | self.command = 0 | |
251 | elif (t > self.cnt_overdrive_reset) and self.overdrive: | |
015dc33a | 252 | # Save the sample number for the rising edge. |
48b59746 | 253 | self.rise = self.samplenum |
4e980c20 | 254 | self.putfr([2, ['Reset', 'Rst', 'R']]) |
21b39043 | 255 | self.state = 'WAIT FOR PRESENCE DETECT' |
48b59746 UH |
256 | # Otherwise this is assumed to be a data bit. |
257 | else: | |
21b39043 | 258 | self.state = 'WAIT FOR FALLING EDGE' |
9cfb16e8 | 259 | elif self.state == 'WAIT FOR PRESENCE DETECT': |
e7720d6c | 260 | # Sample presence status. |
d14e8033 UH |
261 | t = self.rise + self.cnt_presence[self.overdrive] |
262 | owr, pwr = self.wait({'skip': t - self.samplenum}) | |
263 | self.present = owr | |
264 | self.state = 'WAIT FOR RESET SLOT END' | |
671cc300 | 265 | elif self.state == 'WAIT FOR RESET SLOT END': |
48b59746 | 266 | # A reset slot ends in a long recovery period. |
d14e8033 UH |
267 | t = self.rise + self.cnt_reset[self.overdrive] |
268 | owr, pwr = self.wait({'skip': t - self.samplenum}) | |
48b59746 UH |
269 | |
270 | if owr == 0: | |
271 | # This seems to be a reset slot, wait for its end. | |
272 | self.state = 'WAIT FOR RISING EDGE' | |
273 | continue | |
274 | ||
ddeb9b32 | 275 | p = 'false' if self.present else 'true' |
4e980c20 | 276 | self.putrs([3, ['Presence: %s' % p, 'Presence', 'Pres', 'P']]) |
6f507573 | 277 | self.putprs(['RESET/PRESENCE', not self.present]) |
ddeb9b32 | 278 | |
48b59746 UH |
279 | # Wait for next slot. |
280 | self.state = 'WAIT FOR FALLING EDGE' |