]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the sigrok project. | |
3 | ## | |
4 | ## Copyright (C) 2011-2012 Uwe Hermann <uwe@hermann-uwe.de> | |
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 protocol decoder | |
22 | ||
23 | import sigrokdecode as srd | |
24 | ||
25 | # Annotation feed formats | |
26 | ANN_LINK = 0 | |
27 | ANN_NETWORK = 1 | |
28 | ANN_TRANSFER = 2 | |
29 | ||
30 | class Decoder(srd.Decoder): | |
31 | api_version = 1 | |
32 | id = 'onewire' | |
33 | name = '1-Wire' | |
34 | longname = '' | |
35 | desc = '1-Wire bus and MicroLan' | |
36 | license = 'gplv2+' | |
37 | inputs = ['logic'] | |
38 | outputs = ['onewire'] | |
39 | probes = [ | |
40 | {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire bus'}, | |
41 | ] | |
42 | optional_probes = [ | |
43 | {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power'}, | |
44 | ] | |
45 | options = { | |
46 | 'overdrive': ['Overdrive', 0], | |
47 | } | |
48 | annotations = [ | |
49 | ['Link', 'Link layer events (reset, presence, bit slots)'], | |
50 | ['Network', 'Network layer events (device addressing)'], | |
51 | ['Transfer', 'Transfer layer events'], | |
52 | ] | |
53 | ||
54 | def __init__(self, **kwargs): | |
55 | # Common variables | |
56 | self.samplenum = 0 | |
57 | # Link layer variables | |
58 | self.lnk_state = 'WAIT FOR FALLING EDGE' | |
59 | self.lnk_event = 'NONE' | |
60 | self.lnk_fall = 0 | |
61 | self.lnk_present = 0 | |
62 | self.lnk_bit = 0 | |
63 | # Network layer variables | |
64 | self.net_state = 'IDLE' | |
65 | self.net_cnt = 0 | |
66 | self.net_search = "P" | |
67 | self.net_data_p = 0x0 | |
68 | self.net_data_n = 0x0 | |
69 | self.net_data = 0x0 | |
70 | self.net_rom = 0x0000000000000000 | |
71 | # Transport layer variables | |
72 | self.trn_state = 'IDLE' | |
73 | ||
74 | def start(self, metadata): | |
75 | self.samplerate = metadata['samplerate'] | |
76 | self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire') | |
77 | self.out_ann = self.add(srd.OUTPUT_ANN , 'onewire') | |
78 | ||
79 | # The width of the 1-Wire time base (30us) in number of samples. | |
80 | # TODO: optimize this value | |
81 | self.time_base = float(self.samplerate) * float(0.000030) | |
82 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK, ['time_base = %d' % self.time_base]]) | |
83 | ||
84 | def report(self): | |
85 | pass | |
86 | ||
87 | def decode(self, ss, es, data): | |
88 | for (self.samplenum, (owr, pwr)) in data: | |
89 | # print ("DEBUG: sample = %d, owr = %d, pwr = %d, lnk_fall = %d, lnk_state = %s" % (self.samplenum, owr, pwr, self.lnk_fall, self.lnk_state)) | |
90 | ||
91 | # Data link layer | |
92 | ||
93 | # Clear events. | |
94 | self.lnk_event = "NONE" | |
95 | # State machine. | |
96 | if self.lnk_state == 'WAIT FOR FALLING EDGE': | |
97 | # The start of a cycle is a falling edge. | |
98 | if (owr == 0): | |
99 | # Save the sample number for the falling edge. | |
100 | self.lnk_fall = self.samplenum | |
101 | # Go to waiting for sample time | |
102 | self.lnk_state = 'WAIT FOR DATA SAMPLE' | |
103 | elif self.lnk_state == 'WAIT FOR DATA SAMPLE': | |
104 | # Data should be sample one 'time unit' after a falling edge | |
105 | if (self.samplenum - self.lnk_fall == 0.5*self.time_base): | |
106 | self.lnk_bit = owr & 0x1 | |
107 | self.lnk_event = "DATA BIT" | |
108 | if (self.lnk_bit) : self.lnk_state = 'WAIT FOR FALLING EDGE' | |
109 | else : self.lnk_state = 'WAIT FOR RISING EDGE' | |
110 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK, ['BIT: %01x' % self.lnk_bit]]) | |
111 | elif self.lnk_state == 'WAIT FOR RISING EDGE': | |
112 | # The end of a cycle is a rising edge. | |
113 | if (owr == 1): | |
114 | # A reset cycle is longer than 8T. | |
115 | if (self.samplenum - self.lnk_fall > 8*self.time_base): | |
116 | # Save the sample number for the falling edge. | |
117 | self.lnk_rise = self.samplenum | |
118 | # Send a reset event to the next protocol layer. | |
119 | self.lnk_event = "RESET" | |
120 | self.lnk_state = "WAIT FOR PRESENCE DETECT" | |
121 | self.put(self.lnk_fall, self.samplenum, self.out_proto, ['RESET']) | |
122 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK , ['RESET']]) | |
123 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['RESET']]) | |
124 | # Reset the timer. | |
125 | self.lnk_fall = self.samplenum | |
126 | # Otherwise this is assumed to be a data bit. | |
127 | else : | |
128 | self.lnk_state = "WAIT FOR FALLING EDGE" | |
129 | elif self.lnk_state == 'WAIT FOR PRESENCE DETECT': | |
130 | # Data should be sample one 'time unit' after a falling edge | |
131 | if (self.samplenum - self.lnk_rise == 2.5*self.time_base): | |
132 | self.lnk_present = owr & 0x1 | |
133 | # Save the sample number for the falling edge. | |
134 | if not (self.lnk_present) : self.lnk_fall = self.samplenum | |
135 | # create presence detect event | |
136 | #self.lnk_event = "PRESENCE DETECT" | |
137 | if (self.lnk_present) : self.lnk_state = 'WAIT FOR FALLING EDGE' | |
138 | else : self.lnk_state = 'WAIT FOR RISING EDGE' | |
139 | present_str = "False" if self.lnk_present else "True" | |
140 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_LINK , ['PRESENCE: ' + present_str]]) | |
141 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['PRESENCE: ' + present_str]]) | |
142 | else: | |
143 | raise Exception('Invalid lnk_state: %d' % self.lnk_state) | |
144 | ||
145 | # Network layer | |
146 | ||
147 | # State machine. | |
148 | if (self.lnk_event == "RESET"): | |
149 | self.net_state = "COMMAND" | |
150 | self.net_search = "P" | |
151 | self.net_cnt = 0 | |
152 | elif (self.net_state == "IDLE"): | |
153 | pass | |
154 | elif (self.net_state == "COMMAND"): | |
155 | if (self.collect_data(8)): | |
156 | # self.put(self.lnk_fall, self.samplenum, | |
157 | # self.out_proto, ['ROM COMMAND', self.net_data]) | |
158 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: 0x%02x' % self.net_data]]) | |
159 | if (self.net_data == 0x33): | |
160 | # READ ROM | |
161 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM\'']]) | |
162 | self.net_state = "GET ROM" | |
163 | elif (self.net_data == 0x0f): | |
164 | # READ ROM TODO | |
165 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'READ ROM ???\'']]) | |
166 | self.net_state = "GET ROM" | |
167 | elif (self.net_data == 0xcc): | |
168 | # SKIP ROM | |
169 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SKIP ROM\'']]) | |
170 | self.net_state = "IDLE" | |
171 | self.trn_state = "COMMAND" | |
172 | elif (self.net_data == 0x55): | |
173 | # MATCH ROM | |
174 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'MATCH ROM\'']]) | |
175 | self.net_state = "GET ROM" | |
176 | elif (self.net_data == 0xf0): | |
177 | # SEARCH ROM | |
178 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'SEARCH ROM\'']]) | |
179 | self.net_state = "SEARCH ROM" | |
180 | elif (self.net_data == 0x3c): | |
181 | # OVERDRIVE SKIP ROM | |
182 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE SKIP ROM\'']]) | |
183 | self.net_state = "IDLE" | |
184 | self.trn_state = "COMMAND" | |
185 | elif (self.net_data == 0x69): | |
186 | # OVERDRIVE MATCH ROM | |
187 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM COMMAND: \'OVERDRIVE MATCH ROM\'']]) | |
188 | self.net_state = "GET ROM" | |
189 | elif (self.net_state == "GET ROM"): | |
190 | # family code (1B) + serial number (6B) + CRC (1B) | |
191 | if (self.collect_data((1+6+1)*8)): | |
192 | self.net_rom = self.net_data & 0xffffffffffffffff | |
193 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_rom]]) | |
194 | self.net_state = "IDLE" | |
195 | self.trn_state = "COMMAND" | |
196 | elif (self.net_state == "SEARCH ROM"): | |
197 | # family code (1B) + serial number (6B) + CRC (1B) | |
198 | if (self.collect_search((1+6+1)*8)): | |
199 | self.net_rom = self.net_data & 0xffffffffffffffff | |
200 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK, ['ROM: 0x%016x' % self.net_rom]]) | |
201 | self.net_state = "IDLE" | |
202 | self.trn_state = "COMMAND" | |
203 | else: | |
204 | raise Exception('Invalid net_state: %s' % self.net_state) | |
205 | ||
206 | # Transport layer | |
207 | ||
208 | # State machine. | |
209 | if (self.lnk_event == "RESET"): | |
210 | self.trn_state = "IDLE" | |
211 | elif (self.trn_state == "IDLE"): | |
212 | pass | |
213 | elif (self.trn_state == "COMMAND"): | |
214 | if (self.collect_data(8)): | |
215 | # self.put(self.lnk_fall, self.samplenum, self.out_proto, ['FUNCTION COMMAND', self.net_data]) | |
216 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK , ['FUNCTION COMMAND: 0x%02x' % self.net_data]]) | |
217 | self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_TRANSFER, ['FUNCTION COMMAND: 0x%02x' % self.net_data]]) | |
218 | if (self.net_data == 0x48): | |
219 | # COPY SCRATCHPAD | |
220 | self.trn_state = "TODO" | |
221 | elif (self.net_data == 0x4e): | |
222 | # WRITE SCRATCHPAD | |
223 | self.trn_state = "TODO" | |
224 | elif (self.net_data == 0xbe): | |
225 | # READ SCRATCHPAD | |
226 | self.trn_state = "TODO" | |
227 | elif (self.net_data == 0xb8): | |
228 | # RECALL E2 | |
229 | self.trn_state = "TODO" | |
230 | elif (self.net_data == 0xb4): | |
231 | # READ POWER SUPPLY | |
232 | self.trn_state = "TODO" | |
233 | else: | |
234 | # unsupported commands | |
235 | self.trn_state = "TODO" | |
236 | elif (self.trn_state == "TODO"): | |
237 | # self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_NETWORK , ['TODO unsupported transfer state: %s' % self.trn_state]]) | |
238 | # self.put(self.lnk_fall, self.samplenum, self.out_ann, [ANN_TRANSFER, ['TODO unsupported transfer state: %s' % self.trn_state]]) | |
239 | pass | |
240 | else: | |
241 | raise Exception('Invalid trn_state: %s' % self.trn_state) | |
242 | ||
243 | # Link/Network layer data collector | |
244 | def collect_data (self, length): | |
245 | if (self.lnk_event == "DATA BIT"): | |
246 | #print ("DEBUG: BIT=%d t0=%d t+=%d" % (self.lnk_bit, self.lnk_fall, self.samplenum)) | |
247 | self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt) | |
248 | self.net_cnt = self.net_cnt + 1 | |
249 | if (self.net_cnt == length): | |
250 | self.net_data = self.net_data & ((1<<length)-1) | |
251 | self.net_cnt = 0 | |
252 | return (1) | |
253 | else: | |
254 | return (0) | |
255 | else: | |
256 | return (0) | |
257 | ||
258 | # Link/Network layer search collector | |
259 | def collect_search (self, length): | |
260 | if (self.lnk_event == "DATA BIT"): | |
261 | #print ("DEBUG: SEARCH=%s BIT=%d t0=%d t+=%d" % (self.net_search, self.lnk_bit, self.lnk_fall, self.samplenum)) | |
262 | if (self.net_search == "P"): | |
263 | self.net_data_p = self.net_data_p & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt) | |
264 | self.net_search = "N" | |
265 | elif (self.net_search == "N"): | |
266 | self.net_data_n = self.net_data_n & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt) | |
267 | self.net_search = "D" | |
268 | elif (self.net_search == "D"): | |
269 | self.net_data = self.net_data & ~(1 << self.net_cnt) | (self.lnk_bit << self.net_cnt) | |
270 | self.net_search = "P" | |
271 | self.net_cnt = self.net_cnt + 1 | |
272 | if (self.net_cnt == length): | |
273 | self.net_data_p = self.net_data_p & ((1<<length)-1) | |
274 | self.net_data_n = self.net_data_n & ((1<<length)-1) | |
275 | self.net_data = self.net_data & ((1<<length)-1) | |
276 | self.net_search = "P" | |
277 | self.net_cnt = 0 | |
278 | return (1) | |
279 | else: | |
280 | return (0) | |
281 | else: | |
282 | return (0) |