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