]> sigrok.org Git - libsigrokdecode.git/blob - decoders/onewire_link/onewire_link.py
onewire: updated documentation
[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 link layer protocol decoder
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'
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         'cnt_normal_bit'        : ['Time (in samplerate periods) for normal mode sample bit'     , 0],
43         'cnt_normal_slot'       : ['Time (in samplerate periods) for normal mode data slot'      , 0],
44         'cnt_normal_presence'   : ['Time (in samplerate periods) for normal mode sample presence', 0],
45         'cnt_normal_reset'      : ['Time (in samplerate periods) for normal mode reset'          , 0],
46         'cnt_overdrive_bit'     : ['Time (in samplerate periods) for overdrive mode sample bit'     , 0],
47         'cnt_overdrive_slot'    : ['Time (in samplerate periods) for overdrive mode data slot'      , 0],
48         'cnt_overdrive_presence': ['Time (in samplerate periods) for overdrive mode sample presence', 0],
49         'cnt_overdrive_reset'   : ['Time (in samplerate periods) for overdrive mode reset'          , 0],
50     }
51     annotations = [
52         ['Link', 'Link layer events (reset, presence, bit slots)'],
53     ]
54
55     def __init__(self, **kwargs):
56         # Common variables
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         # check if samplerate is appropriate
74         self.samplerate = metadata['samplerate']
75         if (self.options['overdrive']):
76             self.put(0, 0, self.out_ann, [0,
77               ['NOTE: Sample rate checks assume overdrive mode.']])
78             if   (self.samplerate < 2000000):
79                 self.put(0, 0, self.out_ann, [0,
80                   ['ERROR: Sampling rate is too low must be above 2MHz for proper overdrive mode decoding.']])
81             elif (self.samplerate < 5000000):
82                 self.put(0, 0, self.out_ann, [0,
83                   ['WARNING: Sampling rate is suggested to be above 5MHz for proper overdrive mode decoding.']])
84         else:
85             self.put(0, 0, self.out_ann, [0,
86               ['NOTE: Sample rate checks assume normal mode only.']])
87             if   (self.samplerate <  400000):
88                 self.put(0, 0, self.out_ann, [0,
89                   ['ERROR: Sampling rate is too low must be above 400kHz for proper normal mode decoding.']])
90             elif (self.samplerate < 1000000):
91                 self.put(0, 0, self.out_ann, [0,
92                   ['WARNING: Sampling rate is suggested to be above 1MHz for proper normal mode decoding.']])
93
94         # The default 1-Wire time base is 30us, this is used to calculate sampling times.
95         if (self.options['cnt_normal_bit']):
96             self.cnt_normal_bit = self.options['cnt_normal_bit']
97         else:
98             self.cnt_normal_bit = int(float(self.samplerate) * 0.000015) - 1 # 15ns
99         if (self.options['cnt_normal_slot']):
100             self.cnt_normal_slot = self.options['cnt_normal_slot']
101         else:
102             self.cnt_normal_slot = int(float(self.samplerate) * 0.000060) - 1 # 60ns
103         if (self.options['cnt_normal_presence']):
104             self.cnt_normal_presence = self.options['cnt_normal_presence']
105         else:
106             self.cnt_normal_presence = int(float(self.samplerate) * 0.000075) - 1 # 75ns
107         if (self.options['cnt_normal_reset']):
108             self.cnt_normal_reset = self.options['cnt_normal_reset']
109         else:
110             self.cnt_normal_reset = int(float(self.samplerate) * 0.000480) - 1 # 480ns
111         if (self.options['cnt_overdrive_bit']):
112             self.cnt_overdrive_bit = self.options['cnt_overdrive_bit']
113         else:
114             self.cnt_overdrive_bit = int(float(self.samplerate) * 0.000002) - 1 # 2ns
115         if (self.options['cnt_overdrive_slot']):
116             self.cnt_overdrive_slot = self.options['cnt_overdrive_slot']
117         else:
118             self.cnt_overdrive_slot = int(float(self.samplerate) * 0.0000073) - 1 # 6ns+1.3ns
119         if (self.options['cnt_overdrive_presence']):
120             self.cnt_overdrive_presence = self.options['cnt_overdrive_presence']
121         else:
122             self.cnt_overdrive_presence = int(float(self.samplerate) * 0.000010) - 1 # 10ns
123         if (self.options['cnt_overdrive_reset']):
124             self.cnt_overdrive_reset = self.options['cnt_overdrive_reset']
125         else:
126             self.cnt_overdrive_reset = int(float(self.samplerate) * 0.000048) - 1 # 48ns
127
128         # organize values into lists
129         self.cnt_bit      = [self.cnt_normal_bit     , self.cnt_overdrive_bit     ]
130         self.cnt_presence = [self.cnt_normal_presence, self.cnt_overdrive_presence]
131         self.cnt_reset    = [self.cnt_normal_reset   , self.cnt_overdrive_reset   ]
132         self.cnt_slot     = [self.cnt_normal_slot    , self.cnt_overdrive_slot    ]
133
134         # Check if sample times are in the allowed range
135         time_min = float(self.cnt_normal_bit  ) / self.samplerate
136         time_max = float(self.cnt_normal_bit+1) / self.samplerate
137         if ( (time_min < 0.000005) or (time_max > 0.000015) ) :
138            self.put(0, 0, self.out_ann, [0,
139              ['WARNING: The normal mode data sample time interval (%2.1fus-%2.1fus) should be inside (5.0us, 15.0us).'
140                % (time_min*1000000, time_max*1000000)]])
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.put(0, 0, self.out_ann, [0,
145              ['WARNING: The normal mode presence sample time interval (%2.1fus-%2.1fus) should be inside (68.1us, 75.0us).'
146                % (time_min*1000000, time_max*1000000)]])
147         time_min = float(self.cnt_overdrive_bit  ) / self.samplerate
148         time_max = float(self.cnt_overdrive_bit+1) / self.samplerate
149         if ( (time_min < 0.000001) or (time_max > 0.000002) ) :
150            self.put(0, 0, self.out_ann, [0,
151              ['WARNING: The overdrive mode data sample time interval (%2.1fus-%2.1fus) should be inside (1.0us, 2.0us).'
152                % (time_min*1000000, time_max*1000000)]])
153         time_min = float(self.cnt_overdrive_presence  ) / self.samplerate
154         time_max = float(self.cnt_overdrive_presence+1) / self.samplerate
155         if ( (time_min < 0.0000073) or (time_max > 0.000010) ) :
156            self.put(0, 0, self.out_ann, [0,
157              ['WARNING: The overdrive mode presence sample time interval (%2.1fus-%2.1fus) should be inside (7.3us, 10.0us).'
158                % (time_min*1000000, time_max*1000000)]])
159
160     def report(self):
161         pass
162
163     def decode(self, ss, es, data):
164         for (self.samplenum, (owr, pwr)) in data:
165
166             # State machine.
167             if self.state == 'WAIT FOR FALLING EDGE':
168                 # The start of a cycle is a falling edge.
169                 if (owr == 0):
170                     # Save the sample number for the falling edge.
171                     self.fall = self.samplenum
172                     # Go to waiting for sample time
173                     self.state = 'WAIT FOR DATA SAMPLE'
174             elif self.state == 'WAIT FOR DATA SAMPLE':
175                 # Sample data bit
176                 if (self.samplenum - self.fall == self.cnt_bit[self.overdrive]):
177                     self.bit  = owr
178                     self.state = 'WAIT FOR DATA SLOT END'
179             elif self.state == 'WAIT FOR DATA SLOT END':
180                 # A data slot ends in a recovery period, otherwise, this is probably a reset
181                 if (self.samplenum - self.fall == self.cnt_slot[self.overdrive]):
182                     if (owr):
183                         self.put(self.fall, self.samplenum, self.out_ann, [0, ['BIT: %01x' % self.bit]])
184                         self.put(self.fall, self.samplenum, self.out_proto, ['BIT', self.bit])
185                         # Checking the first command to see if overdrive mode should be entered
186                         if   (self.bit_cnt <= 8):
187                             self.command = self.command | (self.bit << self.bit_cnt)
188                         elif (self.bit_cnt == 8):
189                             if (self.command in [0x3c, 0x69]):
190                                 self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, [0, ['ENTER OVERDRIVE MODE']])
191                         # Incrementing the bit counter
192                         self.bit_cnt += 1
193                         # Wait for next slot
194                         self.state = 'WAIT FOR FALLING EDGE'
195                     else:
196                         # This seems to be a reset slot, wait for its end
197                         self.state = 'WAIT FOR RISING EDGE'
198             elif self.state == 'WAIT FOR RISING EDGE':
199                 # The end of a cycle is a rising edge.
200                 if (owr):
201                     # Check if this was a reset cycle
202                     if (self.samplenum - self.fall > self.cnt_normal_reset):
203                         # Save the sample number for the falling edge.
204                         self.rise = self.samplenum
205                         self.state = "WAIT FOR PRESENCE DETECT"
206                         # Exit overdrive mode
207                         if (self.overdrive):
208                             self.put(self.fall, self.cnt_bit[self.overdrive], self.out_ann, [0, ['EXIT OVERDRIVE MODE']])
209                             self.overdrive = 0
210                         # Clear command bit counter and data register
211                         self.bit_cnt = 0
212                         self.command = 0
213                     elif ((self.samplenum - self.fall > self.cnt_overdrive_reset) and (self.overdrive)):
214                         # Save the sample number for the falling edge.
215                         self.rise = self.samplenum
216                         self.state = "WAIT FOR PRESENCE DETECT"
217                     # Otherwise this is assumed to be a data bit.
218                     else :
219                         self.state = "WAIT FOR FALLING EDGE"
220             elif self.state == 'WAIT FOR PRESENCE DETECT':
221                 # Sample presence status
222                 if (self.samplenum - self.rise == self.cnt_presence[self.overdrive]):
223                     self.present = owr
224                     self.state = 'WAIT FOR RESET SLOT END'
225             elif self.state == 'WAIT FOR RESET SLOT END':
226                 # A reset slot ends in a long recovery period
227                 if (self.samplenum - self.rise == self.cnt_reset[self.overdrive]):
228                     if (owr):
229                         self.put(self.fall, self.samplenum, self.out_ann, [0, ['RESET/PRESENCE: %s' % ('False' if self.present else 'True')]])
230                         self.put(self.fall, self.samplenum, self.out_proto, ['RESET/PRESENCE', not self.present])
231                         # Wait for next slot
232                         self.state = 'WAIT FOR FALLING EDGE'
233                     else:
234                         # This seems to be a reset slot, wait for its end
235                         self.state = 'WAIT FOR RISING EDGE'
236             else:
237                 raise Exception('Invalid state: %d' % self.state)