]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/i2c.py
srd: Decoders: Remove author/email fields.
[libsigrokdecode.git] / decoders / i2c.py
... / ...
CommitLineData
1##
2## This file is part of the sigrok project.
3##
4## Copyright (C) 2010-2011 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#
22# I2C protocol decoder
23#
24
25#
26# The Inter-Integrated Circuit (I2C) bus is a bidirectional, multi-master
27# bus using two signals (SCL = serial clock line, SDA = serial data line).
28#
29# There can be many devices on the same bus. Each device can potentially be
30# master or slave (and that can change during runtime). Both slave and master
31# can potentially play the transmitter or receiver role (this can also
32# change at runtime).
33#
34# Possible maximum data rates:
35# - Standard mode: 100 kbit/s
36# - Fast mode: 400 kbit/s
37# - Fast-mode Plus: 1 Mbit/s
38# - High-speed mode: 3.4 Mbit/s
39#
40# START condition (S): SDA = falling, SCL = high
41# Repeated START condition (Sr): same as S
42# Data bit sampling: SCL = rising
43# STOP condition (P): SDA = rising, SCL = high
44#
45# All data bytes on SDA are exactly 8 bits long (transmitted MSB-first).
46# Each byte has to be followed by a 9th ACK/NACK bit. If that bit is low,
47# that indicates an ACK, if it's high that indicates a NACK.
48#
49# After the first START condition, a master sends the device address of the
50# slave it wants to talk to. Slave addresses are 7 bits long (MSB-first).
51# After those 7 bits, a data direction bit is sent. If the bit is low that
52# indicates a WRITE operation, if it's high that indicates a READ operation.
53#
54# Later an optional 10bit slave addressing scheme was added.
55#
56# Documentation:
57# http://www.nxp.com/acrobat/literature/9398/39340011.pdf (v2.1 spec)
58# http://www.nxp.com/acrobat/usermanuals/UM10204_3.pdf (v3 spec)
59# http://en.wikipedia.org/wiki/I2C
60#
61
62# TODO: Look into arbitration, collision detection, clock synchronisation, etc.
63# TODO: Handle clock stretching.
64# TODO: Handle combined messages / repeated START.
65# TODO: Implement support for 7bit and 10bit slave addresses.
66# TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0).
67# TODO: Implement support for detecting various bus errors.
68# TODO: I2C address of slaves.
69# TODO: Handle multiple different I2C devices on same bus
70# -> we need to decode multiple protocols at the same time.
71
72'''
73Protocol output format:
74
75I2C packet:
76[<i2c_command>, <data>, <ack_bit>]
77
78<i2c_command> is one of:
79 - 'START' (START condition)
80 - 'START REPEAT' (Repeated START)
81 - 'ADDRESS READ' (Address, read)
82 - 'ADDRESS WRITE' (Address, write)
83 - 'DATA READ' (Data, read)
84 - 'DATA WRITE' (Data, write)
85 - 'STOP' (STOP condition)
86
87<data> is the data or address byte associated with the 'ADDRESS*' and 'DATA*'
88command. For 'START', 'START REPEAT' and 'STOP', this is None.
89
90<ack_bit> is either 'ACK' or 'NACK', but may also be None.
91'''
92
93import sigrokdecode as srd
94
95# Annotation feed formats
96ANN_SHIFTED = 0
97ANN_SHIFTED_SHORT = 1
98ANN_RAW = 2
99
100# Values are verbose and short annotation, respectively.
101protocol = {
102 'START': ['START', 'S'],
103 'START REPEAT': ['START REPEAT', 'Sr'],
104 'STOP': ['STOP', 'P'],
105 'ACK': ['ACK', 'A'],
106 'NACK': ['NACK', 'N'],
107 'ADDRESS READ': ['ADDRESS READ', 'AR'],
108 'ADDRESS WRITE': ['ADDRESS WRITE', 'AW'],
109 'DATA READ': ['DATA READ', 'DR'],
110 'DATA WRITE': ['DATA WRITE', 'DW'],
111}
112
113# States
114FIND_START = 0
115FIND_ADDRESS = 1
116FIND_DATA = 2
117
118class Decoder(srd.Decoder):
119 id = 'i2c'
120 name = 'I2C'
121 longname = 'Inter-Integrated Circuit'
122 desc = 'I2C is a two-wire, multi-master, serial bus.'
123 longdesc = '...'
124 license = 'gplv2+'
125 inputs = ['logic']
126 outputs = ['i2c']
127 probes = [
128 {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'},
129 {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'},
130 ]
131 options = {
132 'addressing': ['Slave addressing (in bits)', 7], # 7 or 10
133 }
134 annotations = [
135 # ANN_SHIFTED
136 ['7-bit shifted hex',
137 'Read/write bit shifted out from the 8-bit I2C slave address'],
138 # ANN_SHIFTED_SHORT
139 ['7-bit shifted hex (short)',
140 'Read/write bit shifted out from the 8-bit I2C slave address'],
141 # ANN_RAW
142 ['Raw hex', 'Unaltered raw data'],
143 ]
144
145 def __init__(self, **kwargs):
146 self.samplecnt = 0
147 self.bitcount = 0
148 self.databyte = 0
149 self.wr = -1
150 self.startsample = -1
151 self.is_repeat_start = 0
152 self.state = FIND_START
153 self.oldscl = None
154 self.oldsda = None
155
156 # Set protocol decoder option defaults.
157 self.addressing = Decoder.options['addressing'][1]
158
159 def start(self, metadata):
160 self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2c')
161 self.out_ann = self.add(srd.OUTPUT_ANN, 'i2c')
162
163 def report(self):
164 pass
165
166 def is_start_condition(self, scl, sda):
167 # START condition (S): SDA = falling, SCL = high
168 if (self.oldsda == 1 and sda == 0) and scl == 1:
169 return True
170 return False
171
172 def is_data_bit(self, scl, sda):
173 # Data sampling of receiver: SCL = rising
174 if self.oldscl == 0 and scl == 1:
175 return True
176 return False
177
178 def is_stop_condition(self, scl, sda):
179 # STOP condition (P): SDA = rising, SCL = high
180 if (self.oldsda == 0 and sda == 1) and scl == 1:
181 return True
182 return False
183
184 def found_start(self, scl, sda):
185 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START'
186
187 self.put(self.out_proto, [cmd, None, None])
188 self.put(self.out_ann, [ANN_SHIFTED, [protocol[cmd][0]]])
189 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol[cmd][1]]])
190
191 self.state = FIND_ADDRESS
192 self.bitcount = self.databyte = 0
193 self.is_repeat_start = 1
194 self.wr = -1
195
196 def found_address_or_data(self, scl, sda):
197 # Gather 8 bits of data plus the ACK/NACK bit.
198
199 if self.startsample == -1:
200 # TODO: Should be samplenum, as received from the feed.
201 self.startsample = self.samplecnt
202 self.bitcount += 1
203
204 # Address and data are transmitted MSB-first.
205 self.databyte <<= 1
206 self.databyte |= sda
207
208 # Return if we haven't collected all 8 + 1 bits, yet.
209 if self.bitcount != 9:
210 return
211
212 # Send raw output annotation before we start shifting out
213 # read/write and ack/nack bits.
214 self.put(self.out_ann, [ANN_RAW, ['0x%.2x' % self.databyte]])
215
216 # We received 8 address/data bits and the ACK/NACK bit.
217 self.databyte >>= 1 # Shift out unwanted ACK/NACK bit here.
218
219 if self.state == FIND_ADDRESS:
220 # The READ/WRITE bit is only in address bytes, not data bytes.
221 self.wr = 0 if (self.databyte & 1) else 1
222 d = self.databyte >> 1
223 elif self.state == FIND_DATA:
224 d = self.databyte
225 else:
226 # TODO: Error?
227 pass
228
229 # Last bit that came in was the ACK/NACK bit (1 = NACK).
230 ack_bit = 'NACK' if (sda == 1) else 'ACK'
231
232 if self.state == FIND_ADDRESS and self.wr == 1:
233 cmd = 'ADDRESS WRITE'
234 elif self.state == FIND_ADDRESS and self.wr == 0:
235 cmd = 'ADDRESS READ'
236 elif self.state == FIND_DATA and self.wr == 1:
237 cmd = 'DATA WRITE'
238 elif self.state == FIND_DATA and self.wr == 0:
239 cmd = 'DATA READ'
240
241 self.put(self.out_proto, [cmd, d, ack_bit])
242 self.put(self.out_ann, [ANN_SHIFTED,
243 [protocol[cmd][0], '0x%02x' % d, protocol[ack_bit][0]]])
244 self.put(self.out_ann, [ANN_SHIFTED_SHORT,
245 [protocol[cmd][1], '0x%02x' % d, protocol[ack_bit][1]]])
246
247 self.bitcount = self.databyte = 0
248 self.startsample = -1
249
250 if self.state == FIND_ADDRESS:
251 self.state = FIND_DATA
252 elif self.state == FIND_DATA:
253 # There could be multiple data bytes in a row.
254 # So, either find a STOP condition or another data byte next.
255 pass
256
257 def found_stop(self, scl, sda):
258 self.put(self.out_proto, ['STOP', None, None])
259 self.put(self.out_ann, [ANN_SHIFTED, [protocol['STOP'][0]]])
260 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol['STOP'][1]]])
261
262 self.state = FIND_START
263 self.is_repeat_start = 0
264 self.wr = -1
265
266 def put(self, output_id, data):
267 # Inject sample range into the call up to sigrok.
268 # TODO: 0-0 sample range for now.
269 super(Decoder, self).put(0, 0, output_id, data)
270
271 def decode(self, ss, es, data):
272 for samplenum, (scl, sda) in data:
273 self.samplecnt += 1
274
275 # First sample: Save SCL/SDA value.
276 if self.oldscl == None:
277 self.oldscl = scl
278 self.oldsda = sda
279 continue
280
281 # TODO: Wait until the bus is idle (SDA = SCL = 1) first?
282
283 # State machine.
284 if self.state == FIND_START:
285 if self.is_start_condition(scl, sda):
286 self.found_start(scl, sda)
287 elif self.state == FIND_ADDRESS:
288 if self.is_data_bit(scl, sda):
289 self.found_address_or_data(scl, sda)
290 elif self.state == FIND_DATA:
291 if self.is_data_bit(scl, sda):
292 self.found_address_or_data(scl, sda)
293 elif self.is_start_condition(scl, sda):
294 self.found_start(scl, sda)
295 elif self.is_stop_condition(scl, sda):
296 self.found_stop(scl, sda)
297 else:
298 # TODO: Error?
299 pass
300
301 # Save current SDA/SCL values for the next round.
302 self.oldscl = scl
303 self.oldsda = sda
304