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