]> sigrok.org Git - libsigrokdecode.git/blob - decoders/onewire_link/onewire_link.py
d6aec2c2aae23ee6da8fe6b3d704318c97a21190
[libsigrokdecode.git] / decoders / onewire_link / onewire_link.py
1 ##
2 ## This file is part of the sigrok 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 # 1-Wire protocol decoder (link layer)
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'
29     longname = '1-Wire serial communication bus (link layer)'
30     desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
31     license = 'gplv2+'
32     inputs = ['logic']
33     outputs = ['onewire_link']
34     probes = [
35         {'id': 'owr', 'name': 'OWR', 'desc': '1-Wire bus'},
36     ]
37     optional_probes = [
38         {'id': 'pwr', 'name': 'PWR', 'desc': '1-Wire power'},
39     ]
40     options = {
41         'overdrive': ['Overdrive', 1],
42         # Time options (specified in number of samplerate periods):
43         'cnt_normal_bit': ['Normal mode sample bit time', 0],
44         'cnt_normal_slot': ['Normal mode data slot time', 0],
45         'cnt_normal_presence': ['Normal mode sample presence time', 0],
46         'cnt_normal_reset': ['Normal mode reset time', 0],
47         'cnt_overdrive_bit': ['Overdrive mode sample bit time', 0],
48         'cnt_overdrive_slot': ['Overdrive mode data slot time', 0],
49         'cnt_overdrive_presence': ['Overdrive mode sample presence time', 0],
50         'cnt_overdrive_reset': ['Overdrive mode reset time', 0],
51     }
52     annotations = [
53         ['Link', 'Link layer events (reset, presence, bit slots)'],
54     ]
55
56     def __init__(self, **kwargs):
57         self.samplenum = 0
58         # Link layer variables
59         self.state = 'WAIT FOR FALLING EDGE'
60         self.present = 0
61         self.bit = 0
62         self.bit_cnt = 0
63         self.command = 0
64         self.overdrive = 0
65         # Event timing variables
66         self.fall = 0
67         self.rise = 0
68
69     def start(self, metadata):
70         self.out_proto = self.add(srd.OUTPUT_PROTO, 'onewire_link')
71         self.out_ann = self.add(srd.OUTPUT_ANN, 'onewire_link')
72
73         self.samplerate = metadata['samplerate']
74
75         # Check if samplerate is appropriate.
76         if self.options['overdrive']:
77             self.put(0, 0, self.out_ann, [0,
78                 ['NOTE: Sample rate checks assume overdrive mode.']])
79             if self.samplerate < 2000000:
80                 self.put(0, 0, self.out_ann, [0,
81                     ['ERROR: Sampling rate is too low. Must be above 2MHz ' +
82                      'for proper overdrive mode decoding.']])
83             elif self.samplerate < 5000000:
84                 self.put(0, 0, self.out_ann, [0,
85                   ['WARNING: Sampling rate is suggested to be above 5MHz ' +
86                    'for proper overdrive mode decoding.']])
87         else:
88             self.put(0, 0, self.out_ann, [0,
89                 ['NOTE: Sample rate checks assume normal mode only.']])
90             if self.samplerate < 400000:
91                 self.put(0, 0, self.out_ann, [0,
92                     ['ERROR: Sampling rate is too low. Must be above ' +
93                      '400kHz for proper normal mode decoding.']])
94             elif (self.samplerate < 1000000):
95                 self.put(0, 0, self.out_ann, [0,
96                     ['WARNING: Sampling rate is suggested to be above ' +
97                      '1MHz for proper normal mode decoding.']])
98
99         # The default 1-Wire time base is 30us. This is used to calculate
100         # sampling times.
101         samplerate = float(self.samplerate)
102         if self.options['cnt_normal_bit']:
103             self.cnt_normal_bit = self.options['cnt_normal_bit']
104         else:
105             self.cnt_normal_bit = int(samplerate * 0.000015) - 1 # 15ns
106         if self.options['cnt_normal_slot']:
107             self.cnt_normal_slot = self.options['cnt_normal_slot']
108         else:
109             self.cnt_normal_slot = int(samplerate * 0.000060) - 1 # 60ns
110         if self.options['cnt_normal_presence']:
111             self.cnt_normal_presence = self.options['cnt_normal_presence']
112         else:
113             self.cnt_normal_presence = int(samplerate * 0.000075) - 1 # 75ns
114         if self.options['cnt_normal_reset']:
115             self.cnt_normal_reset = self.options['cnt_normal_reset']
116         else:
117             self.cnt_normal_reset = int(samplerate * 0.000480) - 1 # 480ns
118         if self.options['cnt_overdrive_bit']:
119             self.cnt_overdrive_bit = self.options['cnt_overdrive_bit']
120         else:
121             self.cnt_overdrive_bit = int(samplerate * 0.000002) - 1 # 2ns
122         if self.options['cnt_overdrive_slot']:
123             self.cnt_overdrive_slot = self.options['cnt_overdrive_slot']
124         else:
125             self.cnt_overdrive_slot = int(samplerate * 0.0000073) - 1 # 6ns+1.3ns
126         if self.options['cnt_overdrive_presence']:
127             self.cnt_overdrive_presence = self.options['cnt_overdrive_presence']
128         else:
129             self.cnt_overdrive_presence = int(samplerate * 0.000010) - 1 # 10ns
130         if self.options['cnt_overdrive_reset']:
131             self.cnt_overdrive_reset = self.options['cnt_overdrive_reset']
132         else:
133             self.cnt_overdrive_reset = int(samplerate * 0.000048) - 1 # 48ns
134
135         # Organize values into lists.
136         self.cnt_bit = [self.cnt_normal_bit, self.cnt_overdrive_bit]
137         self.cnt_presence = [self.cnt_normal_presence, self.cnt_overdrive_presence]
138         self.cnt_reset = [self.cnt_normal_reset, self.cnt_overdrive_reset]
139         self.cnt_slot = [self.cnt_normal_slot, self.cnt_overdrive_slot]
140
141         # Check if sample times are in the allowed range.
142
143         time_min = float(self.cnt_normal_bit) / self.samplerate
144         time_max = float(self.cnt_normal_bit + 1) / self.samplerate
145         if (time_min < 0.000005) or (time_max > 0.000015):
146             self.put(0, 0, self.out_ann, [0,
147                 ['WARNING: The normal mode data sample time interval ' +
148                  '(%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).'
149                  % (time_min * 1000000, time_max * 1000000)]])
150
151         time_min = float(self.cnt_normal_presence) / self.samplerate
152         time_max = float(self.cnt_normal_presence + 1) / self.samplerate
153         if (time_min < 0.0000681) or (time_max > 0.000075):
154             self.put(0, 0, self.out_ann, [0,
155                 ['WARNING: The normal mode presence sample time interval ' +
156                  '(%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).'
157                  % (time_min * 1000000, time_max * 1000000)]])
158
159         time_min = float(self.cnt_overdrive_bit) / self.samplerate
160         time_max = float(self.cnt_overdrive_bit + 1) / self.samplerate
161         if (time_min < 0.000001) or (time_max > 0.000002):
162             self.put(0, 0, self.out_ann, [0,
163                 ['WARNING: 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.put(0, 0, self.out_ann, [0,
171                 ['WARNING: The overdrive mode presence sample time interval ' +
172                  '(%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).'
173                  % (time_min*1000000, time_max*1000000)]])
174
175     def report(self):
176         pass
177
178     def decode(self, ss, es, data):
179         for (self.samplenum, (owr, pwr)) in data:
180             # State machine.
181             if self.state == 'WAIT FOR FALLING EDGE':
182                 # The start of a cycle is a falling edge.
183                 if owr == 0:
184                     # Save the sample number for the falling edge.
185                     self.fall = self.samplenum
186                     # Go to waiting for sample time.
187                     self.state = 'WAIT FOR DATA SAMPLE'
188             elif self.state == 'WAIT FOR DATA SAMPLE':
189                 # Sample data bit.
190                 t = self.samplenum - self.fall
191                 if t == self.cnt_bit[self.overdrive]:
192                     self.bit = owr
193                     self.state = 'WAIT FOR DATA SLOT END'
194             elif self.state == 'WAIT FOR DATA SLOT END':
195                 # A data slot ends in a recovery period, otherwise, this is
196                 # probably a reset.
197                 t = self.samplenum - self.fall
198                 if t == self.cnt_slot[self.overdrive]:
199                     if owr:
200                         self.put(self.fall, self.samplenum, self.out_ann,
201                                  [0, ['BIT: %01x' % self.bit]])
202                         self.put(self.fall, self.samplenum, self.out_proto,
203                                  ['BIT', self.bit])
204
205                         # Checking the first command to see if overdrive mode
206                         # should be entered.
207                         if self.bit_cnt <= 8:
208                             self.command |= (self.bit << self.bit_cnt)
209                         elif self.bit_cnt == 8 and self.command in [0x3c, 0x69]:
210                             self.put(self.fall, self.cnt_bit[self.overdrive],
211                                      self.out_ann,
212                                      [0, ['ENTER OVERDRIVE MODE']])
213                         # Increment the bit counter.
214                         self.bit_cnt += 1
215                         # Wait for next slot.
216                         self.state = 'WAIT FOR FALLING EDGE'
217                     else:
218                         # This seems to be a reset slot, wait for its end.
219                         self.state = 'WAIT FOR RISING EDGE'
220             elif self.state == 'WAIT FOR RISING EDGE':
221                 # The end of a cycle is a rising edge.
222                 if owr:
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 falling edge.
227                         self.rise = self.samplenum
228                         self.state = 'WAIT FOR PRESENCE DETECT'
229                         # Exit overdrive mode.
230                         if self.overdrive:
231                             self.put(self.fall, self.cnt_bit[self.overdrive],
232                                      self.out_ann, [0, ['EXIT OVERDRIVE MODE']])
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 falling edge.
239                         self.rise = self.samplenum
240                         self.state = "WAIT FOR PRESENCE DETECT"
241                     # Otherwise this is assumed to be a data bit.
242                     else:
243                         self.state = "WAIT FOR FALLING EDGE"
244             elif self.state == 'WAIT FOR PRESENCE DETECT':
245                 # Sample presence status.
246                 t = self.samplenum - self.rise
247                 if t == self.cnt_presence[self.overdrive]:
248                     self.present = owr
249                     self.state = 'WAIT FOR RESET SLOT END'
250             elif self.state == 'WAIT FOR RESET SLOT END':
251                 # A reset slot ends in a long recovery period
252                 t = self.samplenum - self.rise
253                 if t == self.cnt_reset[self.overdrive]:
254                     if owr:
255                         self.put(self.fall, self.samplenum, self.out_ann,
256                                  [0, ['RESET/PRESENCE: %s'
257                                  % ('False' if self.present else 'True')]])
258                         self.put(self.fall, self.samplenum, self.out_proto,
259                                  ['RESET/PRESENCE', not self.present])
260                         # Wait for next slot.
261                         self.state = 'WAIT FOR FALLING EDGE'
262                     else:
263                         # This seems to be a reset slot, wait for its end.
264                         self.state = 'WAIT FOR RISING EDGE'
265             else:
266                 raise Exception('Invalid state: %s' % self.state)