]>
Commit | Line | Data |
---|---|---|
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 | ||
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 | |
7b86f0bc | 42 | # Data bit sampling: SCL = rising |
0588ed70 UH |
43 | # STOP condition (P): SDA = rising, SCL = high |
44 | # | |
33e72c54 | 45 | # All data bytes on SDA are exactly 8 bits long (transmitted MSB-first). |
0588ed70 UH |
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). | |
33e72c54 | 51 | # After those 7 bits, a data direction bit is sent. If the bit is low that |
0588ed70 UH |
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 | ||
23fb2e12 UH |
69 | # |
70 | # I2C output format: | |
71 | # | |
72 | # The output consists of a (Python) list of I2C "packets", each of which | |
73 | # has an (implicit) index number (its index in the list). | |
74 | # Each packet consists of a Python dict with certain key/value pairs. | |
75 | # | |
76 | # TODO: Make this a list later instead of a dict? | |
77 | # | |
78 | # 'type': (string) | |
79 | # - 'S' (START condition) | |
80 | # - 'Sr' (Repeated START) | |
81 | # - 'AR' (Address, read) | |
82 | # - 'AW' (Address, write) | |
83 | # - 'DR' (Data, read) | |
84 | # - 'DW' (Data, write) | |
85 | # - 'P' (STOP condition) | |
86 | # 'range': (tuple of 2 integers, the min/max samplenumber of this range) | |
87 | # - (min, max) | |
88 | # - min/max can also be identical. | |
89 | # 'data': (actual data as integer ???) TODO: This can be very variable... | |
90 | # 'ann': (string; additional annotations / comments) | |
91 | # | |
92 | # Example output: | |
93 | # [{'type': 'S', 'range': (150, 160), 'data': None, 'ann': 'Foobar'}, | |
94 | # {'type': 'AW', 'range': (200, 300), 'data': 0x50, 'ann': 'Slave 4'}, | |
95 | # {'type': 'DW', 'range': (310, 370), 'data': 0x00, 'ann': 'Init cmd'}, | |
96 | # {'type': 'AR', 'range': (500, 560), 'data': 0x50, 'ann': 'Get stat'}, | |
97 | # {'type': 'DR', 'range': (580, 640), 'data': 0xfe, 'ann': 'OK'}, | |
98 | # {'type': 'P', 'range': (650, 660), 'data': None, 'ann': None}] | |
99 | # | |
100 | # Possible other events: | |
101 | # - Error event in case protocol looks broken: | |
102 | # [{'type': 'ERROR', 'range': (min, max), | |
ad2dc0de | 103 | # 'data': TODO, 'ann': 'This is not a Microchip 24XX64 EEPROM'}, |
23fb2e12 | 104 | # [{'type': 'ERROR', 'range': (min, max), |
ad2dc0de | 105 | # 'data': TODO, 'ann': 'TODO'}, |
23fb2e12 UH |
106 | # - TODO: Make list of possible errors accessible as metadata? |
107 | # | |
108 | # TODO: I2C address of slaves. | |
109 | # TODO: Handle multiple different I2C devices on same bus | |
110 | # -> we need to decode multiple protocols at the same time. | |
111 | # TODO: range: Always contiguous? Splitted ranges? Multiple per event? | |
112 | # | |
113 | ||
114 | # | |
115 | # I2C input format: | |
116 | # | |
117 | # signals: | |
118 | # [[id, channel, description], ...] # TODO | |
119 | # | |
120 | # Example: | |
121 | # {'id': 'SCL', 'ch': 5, 'desc': 'Serial clock line'} | |
122 | # {'id': 'SDA', 'ch': 7, 'desc': 'Serial data line'} | |
123 | # ... | |
124 | # | |
125 | # {'inbuf': [...], | |
126 | # 'signals': [{'SCL': }]} | |
127 | # | |
128 | ||
bc5f5a43 | 129 | import sigrokdecode |
b2c19614 | 130 | |
e5080882 BV |
131 | # symbols for i2c decoders up the stack |
132 | START = 1 | |
133 | START_REPEAT = 2 | |
134 | STOP = 3 | |
135 | ACK = 4 | |
136 | NACK = 5 | |
137 | ADDRESS_READ = 6 | |
138 | ADDRESS_WRITE = 7 | |
139 | DATA_READ = 8 | |
140 | DATA_WRITE = 9 | |
141 | ||
400f9ae7 UH |
142 | # States |
143 | FIND_START = 0 | |
144 | FIND_ADDRESS = 1 | |
145 | FIND_DATA = 2 | |
146 | ||
f39d2404 | 147 | |
bc5f5a43 | 148 | class Decoder(sigrokdecode.Decoder): |
67e847fd | 149 | id = 'i2c' |
f39d2404 UH |
150 | name = 'I2C' |
151 | longname = 'Inter-Integrated Circuit (I2C) bus' | |
152 | desc = 'I2C is a two-wire, multi-master, serial bus.' | |
153 | longdesc = '...' | |
154 | author = 'Uwe Hermann' | |
155 | email = 'uwe@hermann-uwe.de' | |
156 | license = 'gplv2+' | |
157 | inputs = ['logic'] | |
158 | outputs = ['i2c'] | |
bc5f5a43 BV |
159 | probes = [ |
160 | {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'}, | |
161 | {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'}, | |
162 | ] | |
f39d2404 UH |
163 | options = { |
164 | 'address-space': ['Address space (in bits)', 7], | |
ad2dc0de | 165 | } |
0588ed70 | 166 | |
3643fc3f | 167 | def __init__(self, **kwargs): |
e5080882 BV |
168 | self.output_protocol = None |
169 | self.output_annotation = None | |
bc5f5a43 | 170 | self.samplecnt = 0 |
f39d2404 UH |
171 | self.bitcount = 0 |
172 | self.databyte = 0 | |
173 | self.wr = -1 | |
174 | self.startsample = -1 | |
5dd9af5b | 175 | self.is_repeat_start = 0 |
400f9ae7 | 176 | self.state = FIND_START |
f39d2404 UH |
177 | self.oldscl = None |
178 | self.oldsda = None | |
179 | ||
3643fc3f | 180 | def start(self, metadata): |
e5080882 BV |
181 | self.output_protocol = self.output_new(2) |
182 | self.output_annotation = self.output_new(1) | |
3643fc3f | 183 | |
f39d2404 UH |
184 | def report(self): |
185 | pass | |
186 | ||
7b86f0bc | 187 | def is_start_condition(self, scl, sda): |
c4262fd6 | 188 | """START condition (S): SDA = falling, SCL = high""" |
7b86f0bc UH |
189 | if (self.oldsda == 1 and sda == 0) and scl == 1: |
190 | return True | |
191 | return False | |
192 | ||
193 | def is_data_bit(self, scl, sda): | |
c4262fd6 | 194 | """Data sampling of receiver: SCL = rising""" |
7b86f0bc UH |
195 | if self.oldscl == 0 and scl == 1: |
196 | return True | |
197 | return False | |
198 | ||
199 | def is_stop_condition(self, scl, sda): | |
c4262fd6 | 200 | """STOP condition (P): SDA = rising, SCL = high""" |
7b86f0bc UH |
201 | if (self.oldsda == 0 and sda == 1) and scl == 1: |
202 | return True | |
203 | return False | |
204 | ||
e5080882 BV |
205 | def found_start(self, scl, sda): |
206 | if self.is_repeat_start == 1: | |
207 | out_proto = [ START_REPEAT ] | |
208 | out_ann = [ "START REPEAT" ] | |
209 | else: | |
210 | out_proto = [ START ] | |
211 | out_ann = [ "START" ] | |
212 | self.put(self.output_protocol, out_proto) | |
213 | self.put(self.output_annotation, out_ann) | |
214 | ||
400f9ae7 | 215 | self.state = FIND_ADDRESS |
7b86f0bc | 216 | self.bitcount = self.databyte = 0 |
5dd9af5b | 217 | self.is_repeat_start = 1 |
7b86f0bc | 218 | self.wr = -1 |
7b86f0bc | 219 | |
e5080882 | 220 | def found_address_or_data(self, scl, sda): |
c4262fd6 | 221 | """Gather 8 bits of data plus the ACK/NACK bit.""" |
7b86f0bc UH |
222 | |
223 | if self.startsample == -1: | |
bc5f5a43 BV |
224 | # TODO: should be samplenum, as received from the feed |
225 | self.startsample = self.samplecnt | |
7b86f0bc UH |
226 | self.bitcount += 1 |
227 | ||
228 | # Address and data are transmitted MSB-first. | |
229 | self.databyte <<= 1 | |
230 | self.databyte |= sda | |
231 | ||
232 | # Return if we haven't collected all 8 + 1 bits, yet. | |
233 | if self.bitcount != 9: | |
234 | return [] | |
235 | ||
236 | # We received 8 address/data bits and the ACK/NACK bit. | |
237 | self.databyte >>= 1 # Shift out unwanted ACK/NACK bit here. | |
238 | ||
400f9ae7 | 239 | if self.state == FIND_ADDRESS: |
7b86f0bc UH |
240 | d = self.databyte & 0xfe |
241 | # The READ/WRITE bit is only in address bytes, not data bytes. | |
e9de9c90 | 242 | self.wr = 1 if (self.databyte & 1) else 0 |
400f9ae7 | 243 | elif self.state == FIND_DATA: |
7b86f0bc UH |
244 | d = self.databyte |
245 | else: | |
246 | # TODO: Error? | |
247 | pass | |
248 | ||
e5080882 BV |
249 | out_proto = [] |
250 | out_ann = [] | |
7b86f0bc | 251 | # TODO: Simplify. |
400f9ae7 | 252 | if self.state == FIND_ADDRESS and self.wr == 1: |
e5080882 BV |
253 | cmd = ADDRESS_WRITE |
254 | ann = 'ADDRESS WRITE' | |
400f9ae7 | 255 | elif self.state == FIND_ADDRESS and self.wr == 0: |
e5080882 BV |
256 | cmd = ADDRESS_READ |
257 | ann = 'ADDRESS READ' | |
400f9ae7 | 258 | elif self.state == FIND_DATA and self.wr == 1: |
e5080882 BV |
259 | cmd = DATA_WRITE |
260 | ann = 'DATA WRITE' | |
400f9ae7 | 261 | elif self.state == FIND_DATA and self.wr == 0: |
e5080882 BV |
262 | cmd = DATA_READ |
263 | ann = 'DATA READ' | |
264 | out_proto.append( [cmd, d] ) | |
265 | out_ann.append( ["%s" % ann, "0x%02x" % d] ) | |
266 | ||
267 | if sda == 1: | |
268 | out_proto.append( [NACK] ) | |
269 | out_ann.append( ["NACK"] ) | |
270 | else: | |
271 | out_proto.append( [ACK] ) | |
272 | out_ann.append( ["ACK"] ) | |
7b86f0bc | 273 | |
e5080882 BV |
274 | self.put(self.output_protocol, out_proto) |
275 | self.put(self.output_annotation, out_ann) | |
7b86f0bc | 276 | |
7b86f0bc UH |
277 | self.bitcount = self.databyte = 0 |
278 | self.startsample = -1 | |
279 | ||
400f9ae7 UH |
280 | if self.state == FIND_ADDRESS: |
281 | self.state = FIND_DATA | |
282 | elif self.state == FIND_DATA: | |
7b86f0bc UH |
283 | # There could be multiple data bytes in a row. |
284 | # So, either find a STOP condition or another data byte next. | |
285 | pass | |
286 | ||
e5080882 BV |
287 | def found_stop(self, scl, sda): |
288 | self.put(self.output_protocol, [ STOP ]) | |
289 | self.put(self.output_annotation, [ "STOP" ]) | |
7b86f0bc | 290 | |
400f9ae7 | 291 | self.state = FIND_START |
5dd9af5b | 292 | self.is_repeat_start = 0 |
7b86f0bc UH |
293 | self.wr = -1 |
294 | ||
1aef2f93 | 295 | def put(self, output_id, data): |
bc5f5a43 BV |
296 | # inject sample range into the call up to sigrok |
297 | super(Decoder, self).put(0, 0, output_id, data) | |
1aef2f93 BV |
298 | |
299 | def decode(self, timeoffset, duration, data): | |
bc5f5a43 BV |
300 | for samplenum, (scl, sda) in data: |
301 | self.samplecnt += 1 | |
f39d2404 UH |
302 | |
303 | # First sample: Save SCL/SDA value. | |
304 | if self.oldscl == None: | |
bc5f5a43 BV |
305 | self.oldscl = scl |
306 | self.oldsda = sda | |
ad2dc0de | 307 | continue |
0588ed70 | 308 | |
f39d2404 UH |
309 | # TODO: Wait until the bus is idle (SDA = SCL = 1) first? |
310 | ||
7b86f0bc | 311 | # State machine. |
400f9ae7 | 312 | if self.state == FIND_START: |
7b86f0bc | 313 | if self.is_start_condition(scl, sda): |
e5080882 | 314 | self.found_start(scl, sda) |
400f9ae7 | 315 | elif self.state == FIND_ADDRESS: |
7b86f0bc | 316 | if self.is_data_bit(scl, sda): |
e5080882 | 317 | self.found_address_or_data(scl, sda) |
400f9ae7 | 318 | elif self.state == FIND_DATA: |
7b86f0bc | 319 | if self.is_data_bit(scl, sda): |
e5080882 | 320 | self.found_address_or_data(scl, sda) |
7b86f0bc | 321 | elif self.is_start_condition(scl, sda): |
e5080882 | 322 | self.found_start(scl, sda) |
7b86f0bc | 323 | elif self.is_stop_condition(scl, sda): |
e5080882 | 324 | self.found_stop(scl, sda) |
7b86f0bc UH |
325 | else: |
326 | # TODO: Error? | |
327 | pass | |
f39d2404 UH |
328 | |
329 | # Save current SDA/SCL values for the next round. | |
330 | self.oldscl = scl | |
331 | self.oldsda = sda | |
332 |