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