]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/i2c/pd.py
s/out_proto/out_python/.
[libsigrokdecode.git] / decoders / i2c / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2010-2013 Uwe Hermann <uwe@hermann-uwe.de>
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# TODO: Look into arbitration, collision detection, clock synchronisation, etc.
22# TODO: Implement support for 10bit slave addresses.
23# TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0).
24# TODO: Implement support for detecting various bus errors.
25
26import sigrokdecode as srd
27
28'''
29OUTPUT_PYTHON format:
30
31I²C packet:
32[<cmd>, <data>]
33
34<cmd> is one of:
35 - 'START' (START condition)
36 - 'START REPEAT' (Repeated START condition)
37 - 'ADDRESS READ' (Slave address, read)
38 - 'ADDRESS WRITE' (Slave address, write)
39 - 'DATA READ' (Data, read)
40 - 'DATA WRITE' (Data, write)
41 - 'STOP' (STOP condition)
42 - 'ACK' (ACK bit)
43 - 'NACK' (NACK bit)
44
45<data> is the data or address byte associated with the 'ADDRESS*' and 'DATA*'
46command. Slave addresses do not include bit 0 (the READ/WRITE indication bit).
47For example, a slave address field could be 0x51 (instead of 0xa2).
48For 'START', 'START REPEAT', 'STOP', 'ACK', and 'NACK' <data> is None.
49'''
50
51# CMD: [annotation-type-index, long annotation, short annotation]
52proto = {
53 'START': [0, 'Start', 'S'],
54 'START REPEAT': [1, 'Start repeat', 'Sr'],
55 'STOP': [2, 'Stop', 'P'],
56 'ACK': [3, 'ACK', 'A'],
57 'NACK': [4, 'NACK', 'N'],
58 'ADDRESS READ': [5, 'Address read', 'AR'],
59 'ADDRESS WRITE': [6, 'Address write', 'AW'],
60 'DATA READ': [7, 'Data read', 'DR'],
61 'DATA WRITE': [8, 'Data write', 'DW'],
62}
63
64class Decoder(srd.Decoder):
65 api_version = 1
66 id = 'i2c'
67 name = 'I²C'
68 longname = 'Inter-Integrated Circuit'
69 desc = 'Two-wire, multi-master, serial bus.'
70 license = 'gplv2+'
71 inputs = ['logic']
72 outputs = ['i2c']
73 probes = [
74 {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'},
75 {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'},
76 ]
77 optional_probes = []
78 options = {
79 'address_format': ['Displayed slave address format', 'shifted'],
80 }
81 annotations = [
82 ['start', 'Start condition'],
83 ['repeat-start', 'Repeat start condition'],
84 ['stop', 'Stop condition'],
85 ['ack', 'ACK'],
86 ['nack', 'NACK'],
87 ['address-read', 'Address read'],
88 ['address-write', 'Address write'],
89 ['data-read', 'Data read'],
90 ['data-write', 'Data write'],
91 ['warnings', 'Human-readable warnings'],
92 ]
93 binary = (
94 ('address-read', 'Address read'),
95 ('address-write', 'Address write'),
96 ('data-read', 'Data read'),
97 ('data-write', 'Data write'),
98 )
99
100 def __init__(self, **kwargs):
101 self.samplerate = None
102 self.startsample = -1
103 self.samplenum = None
104 self.bitcount = 0
105 self.databyte = 0
106 self.wr = -1
107 self.is_repeat_start = 0
108 self.state = 'FIND START'
109 self.oldscl = 1
110 self.oldsda = 1
111 self.oldpins = [1, 1]
112 self.pdu_start = None
113 self.pdu_bits = 0
114
115 def metadata(self, key, value):
116 if key == srd.SRD_CONF_SAMPLERATE:
117 self.samplerate = value
118
119 def start(self):
120 self.out_python = self.register(srd.OUTPUT_PYTHON)
121 self.out_ann = self.register(srd.OUTPUT_ANN)
122 self.out_binary = self.register(srd.OUTPUT_BINARY)
123 self.out_bitrate = self.register(srd.OUTPUT_META,
124 meta=(int, 'Bitrate', 'Bitrate from Start bit to Stop bit'))
125
126 def putx(self, data):
127 self.put(self.startsample, self.samplenum, self.out_ann, data)
128
129 def putp(self, data):
130 self.put(self.startsample, self.samplenum, self.out_python, data)
131
132 def putb(self, data):
133 self.put(self.startsample, self.samplenum, self.out_binary, data)
134
135 def is_start_condition(self, scl, sda):
136 # START condition (S): SDA = falling, SCL = high
137 if (self.oldsda == 1 and sda == 0) and scl == 1:
138 return True
139 return False
140
141 def is_data_bit(self, scl, sda):
142 # Data sampling of receiver: SCL = rising
143 if self.oldscl == 0 and scl == 1:
144 return True
145 return False
146
147 def is_stop_condition(self, scl, sda):
148 # STOP condition (P): SDA = rising, SCL = high
149 if (self.oldsda == 0 and sda == 1) and scl == 1:
150 return True
151 return False
152
153 def found_start(self, scl, sda):
154 self.startsample = self.samplenum
155 self.pdu_start = self.samplenum
156 self.pdu_bits = 0
157 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START'
158 self.putp([cmd, None])
159 self.putx([proto[cmd][0], proto[cmd][1:]])
160 self.state = 'FIND ADDRESS'
161 self.bitcount = self.databyte = 0
162 self.is_repeat_start = 1
163 self.wr = -1
164
165 # Gather 8 bits of data plus the ACK/NACK bit.
166 def found_address_or_data(self, scl, sda):
167 # Address and data are transmitted MSB-first.
168 self.databyte <<= 1
169 self.databyte |= sda
170
171 if self.bitcount == 0:
172 self.startsample = self.samplenum
173
174 # Return if we haven't collected all 8 + 1 bits, yet.
175 self.bitcount += 1
176 if self.bitcount != 8:
177 return
178
179 # We triggered on the ACK/NACK bit, but won't report that until later.
180 self.startsample -= 1
181
182 d = self.databyte
183 if self.state == 'FIND ADDRESS':
184 # The READ/WRITE bit is only in address bytes, not data bytes.
185 self.wr = 0 if (self.databyte & 1) else 1
186 if self.options['address_format'] == 'shifted':
187 d = d >> 1
188
189 bin_class = -1
190 if self.state == 'FIND ADDRESS' and self.wr == 1:
191 cmd = 'ADDRESS WRITE'
192 bin_class = 1
193 elif self.state == 'FIND ADDRESS' and self.wr == 0:
194 cmd = 'ADDRESS READ'
195 bin_class = 0
196 elif self.state == 'FIND DATA' and self.wr == 1:
197 cmd = 'DATA WRITE'
198 bin_class = 3
199 elif self.state == 'FIND DATA' and self.wr == 0:
200 cmd = 'DATA READ'
201 bin_class = 2
202
203 self.putp([cmd, d])
204 self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d),
205 '%s: %02X' % (proto[cmd][2], d), '%02X' % d]])
206 self.putb((bin_class, bytes([d])))
207
208 # Done with this packet.
209 self.startsample = -1
210 self.bitcount = self.databyte = 0
211 self.state = 'FIND ACK'
212
213 def get_ack(self, scl, sda):
214 self.startsample = self.samplenum
215 cmd = 'NACK' if (sda == 1) else 'ACK'
216 self.putp([cmd, None])
217 self.putx([proto[cmd][0], proto[cmd][1:]])
218 # There could be multiple data bytes in a row, so either find
219 # another data byte or a STOP condition next.
220 self.state = 'FIND DATA'
221
222 def found_stop(self, scl, sda):
223 # Meta bitrate
224 elapsed = 1 / float(self.samplerate) * (self.samplenum - self.pdu_start + 1)
225 bitrate = int(1 / elapsed * self.pdu_bits)
226 self.put(self.startsample, self.samplenum, self.out_bitrate, bitrate)
227
228 self.startsample = self.samplenum
229 cmd = 'STOP'
230 self.putp([cmd, None])
231 self.putx([proto[cmd][0], proto[cmd][1:]])
232 self.state = 'FIND START'
233 self.is_repeat_start = 0
234 self.wr = -1
235
236 def decode(self, ss, es, data):
237 if self.samplerate is None:
238 raise Exception("Cannot decode without samplerate.")
239 for (self.samplenum, pins) in data:
240
241 # Ignore identical samples early on (for performance reasons).
242 if self.oldpins == pins:
243 continue
244 self.oldpins, (scl, sda) = pins, pins
245
246 self.pdu_bits += 1
247
248 # TODO: Wait until the bus is idle (SDA = SCL = 1) first?
249
250 # State machine.
251 if self.state == 'FIND START':
252 if self.is_start_condition(scl, sda):
253 self.found_start(scl, sda)
254 elif self.state == 'FIND ADDRESS':
255 if self.is_data_bit(scl, sda):
256 self.found_address_or_data(scl, sda)
257 elif self.state == 'FIND DATA':
258 if self.is_data_bit(scl, sda):
259 self.found_address_or_data(scl, sda)
260 elif self.is_start_condition(scl, sda):
261 self.found_start(scl, sda)
262 elif self.is_stop_condition(scl, sda):
263 self.found_stop(scl, sda)
264 elif self.state == 'FIND ACK':
265 if self.is_data_bit(scl, sda):
266 self.get_ack(scl, sda)
267 else:
268 raise Exception('Invalid state: %s' % self.state)
269
270 # Save current SDA/SCL values for the next round.
271 self.oldscl = scl
272 self.oldsda = sda
273