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