]>
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. | |
23fb2e12 UH |
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. | |
23fb2e12 UH |
71 | |
72 | # | |
7ce7775c BV |
73 | # I2C protocol output format: |
74 | # | |
75 | # The protocol output consists of a (Python) list of I2C "packets", each of | |
76 | # which is of the form | |
77 | # | |
eb7082c9 | 78 | # [<i2c_command>, <data>, <ack_bit>] |
7ce7775c | 79 | # |
eb7082c9 | 80 | # <i2c_command> is one of: |
7ce7775c BV |
81 | # - 'START' (START condition) |
82 | # - 'START_REPEAT' (Repeated START) | |
eb7082c9 UH |
83 | # - 'ADDRESS_READ' (Slave address, read) |
84 | # - 'ADDRESS_WRITE' (Slave address, write) | |
7ce7775c BV |
85 | # - 'DATA_READ' (Data, read) |
86 | # - 'DATA_WRITE' (Data, write) | |
87 | # - 'STOP' (STOP condition) | |
23fb2e12 | 88 | # |
eb7082c9 | 89 | # <data> is the data or address byte associated with the ADDRESS_* and DATA_* |
7ce7775c | 90 | # command. For START, START_REPEAT and STOP, this is None. |
23fb2e12 | 91 | # |
eb7082c9 | 92 | # <ack_bit> is either 'ACK' or 'NACK', but may also be None. |
23fb2e12 UH |
93 | # |
94 | ||
677d597b | 95 | import sigrokdecode as srd |
b2c19614 | 96 | |
eb7082c9 | 97 | # Annotation feed formats |
7ce7775c BV |
98 | ANN_SHIFTED = 0 |
99 | ANN_SHIFTED_SHORT = 1 | |
100 | ANN_RAW = 2 | |
101 | ||
eb7082c9 | 102 | # Values are verbose and short annotation, respectively. |
15969949 | 103 | protocol = { |
eb7082c9 UH |
104 | 'START': ['START', 'S'], |
105 | 'START_REPEAT': ['START REPEAT', 'Sr'], | |
106 | 'STOP': ['STOP', 'P'], | |
107 | 'ACK': ['ACK', 'A'], | |
108 | 'NACK': ['NACK', 'N'], | |
109 | 'ADDRESS_READ': ['ADDRESS READ', 'AR'], | |
110 | 'ADDRESS_WRITE': ['ADDRESS WRITE', 'AW'], | |
111 | 'DATA_READ': ['DATA READ', 'DR'], | |
112 | 'DATA_WRITE': ['DATA WRITE', 'DW'], | |
15969949 | 113 | } |
e5080882 | 114 | |
400f9ae7 UH |
115 | # States |
116 | FIND_START = 0 | |
117 | FIND_ADDRESS = 1 | |
118 | FIND_DATA = 2 | |
119 | ||
677d597b | 120 | class Decoder(srd.Decoder): |
67e847fd | 121 | id = 'i2c' |
f39d2404 | 122 | name = 'I2C' |
9a12a6e7 | 123 | longname = 'Inter-Integrated Circuit' |
f39d2404 UH |
124 | desc = 'I2C is a two-wire, multi-master, serial bus.' |
125 | longdesc = '...' | |
126 | author = 'Uwe Hermann' | |
127 | email = 'uwe@hermann-uwe.de' | |
128 | license = 'gplv2+' | |
129 | inputs = ['logic'] | |
130 | outputs = ['i2c'] | |
bc5f5a43 BV |
131 | probes = [ |
132 | {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'}, | |
133 | {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'}, | |
134 | ] | |
f39d2404 UH |
135 | options = { |
136 | 'address-space': ['Address space (in bits)', 7], | |
ad2dc0de | 137 | } |
e97b6ef5 | 138 | annotations = [ |
15969949 | 139 | # ANN_SHIFTED |
eb7082c9 UH |
140 | ['7-bit shifted hex', |
141 | 'Read/write bit shifted out from the 8-bit I2C slave address'], | |
15969949 | 142 | # ANN_SHIFTED_SHORT |
eb7082c9 UH |
143 | ['7-bit shifted hex (short)', |
144 | 'Read/write bit shifted out from the 8-bit I2C slave address'], | |
15969949 | 145 | # ANN_RAW |
eb7082c9 | 146 | ['Raw hex', 'Unaltered raw data'], |
15969949 | 147 | ] |
0588ed70 | 148 | |
3643fc3f | 149 | def __init__(self, **kwargs): |
bc5f5a43 | 150 | self.samplecnt = 0 |
f39d2404 UH |
151 | self.bitcount = 0 |
152 | self.databyte = 0 | |
153 | self.wr = -1 | |
154 | self.startsample = -1 | |
5dd9af5b | 155 | self.is_repeat_start = 0 |
400f9ae7 | 156 | self.state = FIND_START |
f39d2404 UH |
157 | self.oldscl = None |
158 | self.oldsda = None | |
159 | ||
3643fc3f | 160 | def start(self, metadata): |
56202222 UH |
161 | self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2c') |
162 | self.out_ann = self.add(srd.OUTPUT_ANN, 'i2c') | |
3643fc3f | 163 | |
f39d2404 UH |
164 | def report(self): |
165 | pass | |
166 | ||
7b86f0bc | 167 | def is_start_condition(self, scl, sda): |
eb7082c9 | 168 | # START condition (S): SDA = falling, SCL = high |
7b86f0bc UH |
169 | if (self.oldsda == 1 and sda == 0) and scl == 1: |
170 | return True | |
171 | return False | |
172 | ||
173 | def is_data_bit(self, scl, sda): | |
eb7082c9 | 174 | # Data sampling of receiver: SCL = rising |
7b86f0bc UH |
175 | if self.oldscl == 0 and scl == 1: |
176 | return True | |
177 | return False | |
178 | ||
179 | def is_stop_condition(self, scl, sda): | |
eb7082c9 | 180 | # STOP condition (P): SDA = rising, SCL = high |
7b86f0bc UH |
181 | if (self.oldsda == 0 and sda == 1) and scl == 1: |
182 | return True | |
183 | return False | |
184 | ||
e5080882 | 185 | def found_start(self, scl, sda): |
bf1c3f4d | 186 | cmd = 'START_REPEAT' if (self.is_repeat_start == 1) else 'START' |
eb7082c9 UH |
187 | |
188 | self.put(self.out_proto, [cmd, None, None]) | |
189 | self.put(self.out_ann, [ANN_SHIFTED, [protocol[cmd][0]]]) | |
190 | self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol[cmd][1]]]) | |
e5080882 | 191 | |
400f9ae7 | 192 | self.state = FIND_ADDRESS |
7b86f0bc | 193 | self.bitcount = self.databyte = 0 |
5dd9af5b | 194 | self.is_repeat_start = 1 |
7b86f0bc | 195 | self.wr = -1 |
7b86f0bc | 196 | |
e5080882 | 197 | def found_address_or_data(self, scl, sda): |
eb7082c9 | 198 | # Gather 8 bits of data plus the ACK/NACK bit. |
7b86f0bc UH |
199 | |
200 | if self.startsample == -1: | |
eb7082c9 | 201 | # TODO: Should be samplenum, as received from the feed. |
bc5f5a43 | 202 | self.startsample = self.samplecnt |
7b86f0bc UH |
203 | self.bitcount += 1 |
204 | ||
205 | # Address and data are transmitted MSB-first. | |
206 | self.databyte <<= 1 | |
207 | self.databyte |= sda | |
208 | ||
209 | # Return if we haven't collected all 8 + 1 bits, yet. | |
210 | if self.bitcount != 9: | |
eb7082c9 | 211 | return |
7b86f0bc | 212 | |
eb7082c9 UH |
213 | # Send raw output annotation before we start shifting out |
214 | # read/write and ack/nack bits. | |
215 | self.put(self.out_ann, [ANN_RAW, ['0x%.2x' % self.databyte]]) | |
15969949 | 216 | |
7b86f0bc UH |
217 | # We received 8 address/data bits and the ACK/NACK bit. |
218 | self.databyte >>= 1 # Shift out unwanted ACK/NACK bit here. | |
219 | ||
400f9ae7 | 220 | if self.state == FIND_ADDRESS: |
7b86f0bc | 221 | # The READ/WRITE bit is only in address bytes, not data bytes. |
bf1c3f4d | 222 | self.wr = 0 if (self.databyte & 1) else 1 |
84b81f1d | 223 | d = self.databyte >> 1 |
400f9ae7 | 224 | elif self.state == FIND_DATA: |
7b86f0bc UH |
225 | d = self.databyte |
226 | else: | |
227 | # TODO: Error? | |
228 | pass | |
229 | ||
eb7082c9 | 230 | # Last bit that came in was the ACK/NACK bit (1 = NACK). |
bf1c3f4d | 231 | ack_bit = 'NACK' if (sda == 1) else 'ACK' |
15969949 | 232 | |
400f9ae7 | 233 | if self.state == FIND_ADDRESS and self.wr == 1: |
15969949 | 234 | cmd = 'ADDRESS_WRITE' |
400f9ae7 | 235 | elif self.state == FIND_ADDRESS and self.wr == 0: |
15969949 | 236 | cmd = 'ADDRESS_READ' |
400f9ae7 | 237 | elif self.state == FIND_DATA and self.wr == 1: |
15969949 | 238 | cmd = 'DATA_WRITE' |
400f9ae7 | 239 | elif self.state == FIND_DATA and self.wr == 0: |
15969949 | 240 | cmd = 'DATA_READ' |
eb7082c9 UH |
241 | |
242 | self.put(self.out_proto, [cmd, d, ack_bit]) | |
957da073 UH |
243 | self.put(self.out_ann, [ANN_SHIFTED, |
244 | [protocol[cmd][0], '0x%02x' % d, protocol[ack_bit][0]]]) | |
245 | self.put(self.out_ann, [ANN_SHIFTED_SHORT, | |
246 | [protocol[cmd][1], '0x%02x' % d, protocol[ack_bit][1]]]) | |
7b86f0bc | 247 | |
7b86f0bc UH |
248 | self.bitcount = self.databyte = 0 |
249 | self.startsample = -1 | |
250 | ||
400f9ae7 UH |
251 | if self.state == FIND_ADDRESS: |
252 | self.state = FIND_DATA | |
253 | elif self.state == FIND_DATA: | |
7b86f0bc UH |
254 | # There could be multiple data bytes in a row. |
255 | # So, either find a STOP condition or another data byte next. | |
256 | pass | |
257 | ||
e5080882 | 258 | def found_stop(self, scl, sda): |
eb7082c9 UH |
259 | self.put(self.out_proto, ['STOP', None, None]) |
260 | self.put(self.out_ann, [ANN_SHIFTED, [protocol['STOP'][0]]]) | |
261 | self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol['STOP'][1]]]) | |
7b86f0bc | 262 | |
400f9ae7 | 263 | self.state = FIND_START |
5dd9af5b | 264 | self.is_repeat_start = 0 |
7b86f0bc UH |
265 | self.wr = -1 |
266 | ||
1aef2f93 | 267 | def put(self, output_id, data): |
eb7082c9 UH |
268 | # Inject sample range into the call up to sigrok. |
269 | # TODO: 0-0 sample range for now. | |
bc5f5a43 | 270 | super(Decoder, self).put(0, 0, output_id, data) |
1aef2f93 | 271 | |
2b9837d9 | 272 | def decode(self, ss, es, data): |
bc5f5a43 BV |
273 | for samplenum, (scl, sda) in data: |
274 | self.samplecnt += 1 | |
f39d2404 UH |
275 | |
276 | # First sample: Save SCL/SDA value. | |
277 | if self.oldscl == None: | |
bc5f5a43 BV |
278 | self.oldscl = scl |
279 | self.oldsda = sda | |
ad2dc0de | 280 | continue |
0588ed70 | 281 | |
f39d2404 UH |
282 | # TODO: Wait until the bus is idle (SDA = SCL = 1) first? |
283 | ||
7b86f0bc | 284 | # State machine. |
400f9ae7 | 285 | if self.state == FIND_START: |
7b86f0bc | 286 | if self.is_start_condition(scl, sda): |
e5080882 | 287 | self.found_start(scl, sda) |
400f9ae7 | 288 | elif self.state == FIND_ADDRESS: |
7b86f0bc | 289 | if self.is_data_bit(scl, sda): |
e5080882 | 290 | self.found_address_or_data(scl, sda) |
400f9ae7 | 291 | elif self.state == FIND_DATA: |
7b86f0bc | 292 | if self.is_data_bit(scl, sda): |
e5080882 | 293 | self.found_address_or_data(scl, sda) |
7b86f0bc | 294 | elif self.is_start_condition(scl, sda): |
e5080882 | 295 | self.found_start(scl, sda) |
7b86f0bc | 296 | elif self.is_stop_condition(scl, sda): |
e5080882 | 297 | self.found_stop(scl, sda) |
7b86f0bc UH |
298 | else: |
299 | # TODO: Error? | |
300 | pass | |
f39d2404 UH |
301 | |
302 | # Save current SDA/SCL values for the next round. | |
303 | self.oldscl = scl | |
304 | self.oldsda = sda | |
305 |