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