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