]>
Commit | Line | Data |
---|---|---|
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 | # 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 | |
41 | # Data bit sampling: SCL = rising | |
42 | # STOP condition (P): SDA = rising, SCL = high | |
43 | # | |
44 | # All data bytes on SDA are exactly 8 bits long (transmitted MSB-first). | |
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). | |
50 | # After those 7 bits, a data direction bit is sent. If the bit is low that | |
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. | |
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. | |
70 | ||
71 | """ | |
72 | I2C protocol output format: | |
73 | ||
74 | The protocol output consists of a (Python) list of I2C "packets", each of | |
75 | which 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_* | |
89 | command. For START, START_REPEAT and STOP, this is None. | |
90 | ||
91 | _ack_bit_ is either 'ACK' or 'NACK', but may also be None. | |
92 | """ | |
93 | ||
94 | import sigrokdecode as srd | |
95 | ||
96 | # Annotation feed formats | |
97 | ANN_SHIFTED = 0 | |
98 | ANN_SHIFTED_SHORT = 1 | |
99 | ANN_RAW = 2 | |
100 | ||
101 | # Values are verbose and short annotation, respectively. | |
102 | protocol = { | |
103 | 'START': ['START', 'S'], | |
104 | 'START REPEAT': ['START REPEAT', 'Sr'], | |
105 | 'STOP': ['STOP', 'P'], | |
106 | 'ACK': ['ACK', 'A'], | |
107 | 'NACK': ['NACK', 'N'], | |
108 | 'ADDRESS READ': ['ADDRESS READ', 'AR'], | |
109 | 'ADDRESS WRITE': ['ADDRESS WRITE', 'AW'], | |
110 | 'DATA READ': ['DATA READ', 'DR'], | |
111 | 'DATA WRITE': ['DATA WRITE', 'DW'], | |
112 | } | |
113 | ||
114 | # States | |
115 | FIND_START = 0 | |
116 | FIND_ADDRESS = 1 | |
117 | FIND_DATA = 2 | |
118 | ||
119 | class Decoder(srd.Decoder): | |
120 | id = 'i2c' | |
121 | name = 'I2C' | |
122 | longname = 'Inter-Integrated Circuit' | |
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'] | |
130 | probes = [ | |
131 | {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'}, | |
132 | {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'}, | |
133 | ] | |
134 | options = { | |
135 | 'addressing': ['Slave addressing (in bits)', 7], # 7 or 10 | |
136 | } | |
137 | annotations = [ | |
138 | # ANN_SHIFTED | |
139 | ['7-bit shifted hex', | |
140 | 'Read/write bit shifted out from the 8-bit I2C slave address'], | |
141 | # ANN_SHIFTED_SHORT | |
142 | ['7-bit shifted hex (short)', | |
143 | 'Read/write bit shifted out from the 8-bit I2C slave address'], | |
144 | # ANN_RAW | |
145 | ['Raw hex', 'Unaltered raw data'], | |
146 | ] | |
147 | ||
148 | def __init__(self, **kwargs): | |
149 | self.samplecnt = 0 | |
150 | self.bitcount = 0 | |
151 | self.databyte = 0 | |
152 | self.wr = -1 | |
153 | self.startsample = -1 | |
154 | self.is_repeat_start = 0 | |
155 | self.state = FIND_START | |
156 | self.oldscl = None | |
157 | self.oldsda = None | |
158 | ||
159 | # Set protocol decoder option defaults. | |
160 | self.addressing = Decoder.options['addressing'][1] | |
161 | ||
162 | def start(self, metadata): | |
163 | self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2c') | |
164 | self.out_ann = self.add(srd.OUTPUT_ANN, 'i2c') | |
165 | ||
166 | def report(self): | |
167 | pass | |
168 | ||
169 | def is_start_condition(self, scl, sda): | |
170 | # START condition (S): SDA = falling, SCL = high | |
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): | |
176 | # Data sampling of receiver: SCL = rising | |
177 | if self.oldscl == 0 and scl == 1: | |
178 | return True | |
179 | return False | |
180 | ||
181 | def is_stop_condition(self, scl, sda): | |
182 | # STOP condition (P): SDA = rising, SCL = high | |
183 | if (self.oldsda == 0 and sda == 1) and scl == 1: | |
184 | return True | |
185 | return False | |
186 | ||
187 | def found_start(self, scl, sda): | |
188 | cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START' | |
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]]]) | |
193 | ||
194 | self.state = FIND_ADDRESS | |
195 | self.bitcount = self.databyte = 0 | |
196 | self.is_repeat_start = 1 | |
197 | self.wr = -1 | |
198 | ||
199 | def found_address_or_data(self, scl, sda): | |
200 | # Gather 8 bits of data plus the ACK/NACK bit. | |
201 | ||
202 | if self.startsample == -1: | |
203 | # TODO: Should be samplenum, as received from the feed. | |
204 | self.startsample = self.samplecnt | |
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: | |
213 | return | |
214 | ||
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]]) | |
218 | ||
219 | # We received 8 address/data bits and the ACK/NACK bit. | |
220 | self.databyte >>= 1 # Shift out unwanted ACK/NACK bit here. | |
221 | ||
222 | if self.state == FIND_ADDRESS: | |
223 | # The READ/WRITE bit is only in address bytes, not data bytes. | |
224 | self.wr = 0 if (self.databyte & 1) else 1 | |
225 | d = self.databyte >> 1 | |
226 | elif self.state == FIND_DATA: | |
227 | d = self.databyte | |
228 | else: | |
229 | # TODO: Error? | |
230 | pass | |
231 | ||
232 | # Last bit that came in was the ACK/NACK bit (1 = NACK). | |
233 | ack_bit = 'NACK' if (sda == 1) else 'ACK' | |
234 | ||
235 | if self.state == FIND_ADDRESS and self.wr == 1: | |
236 | cmd = 'ADDRESS WRITE' | |
237 | elif self.state == FIND_ADDRESS and self.wr == 0: | |
238 | cmd = 'ADDRESS READ' | |
239 | elif self.state == FIND_DATA and self.wr == 1: | |
240 | cmd = 'DATA WRITE' | |
241 | elif self.state == FIND_DATA and self.wr == 0: | |
242 | cmd = 'DATA READ' | |
243 | ||
244 | self.put(self.out_proto, [cmd, d, ack_bit]) | |
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]]]) | |
249 | ||
250 | self.bitcount = self.databyte = 0 | |
251 | self.startsample = -1 | |
252 | ||
253 | if self.state == FIND_ADDRESS: | |
254 | self.state = FIND_DATA | |
255 | elif self.state == FIND_DATA: | |
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 | ||
260 | def found_stop(self, scl, sda): | |
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]]]) | |
264 | ||
265 | self.state = FIND_START | |
266 | self.is_repeat_start = 0 | |
267 | self.wr = -1 | |
268 | ||
269 | def put(self, output_id, data): | |
270 | # Inject sample range into the call up to sigrok. | |
271 | # TODO: 0-0 sample range for now. | |
272 | super(Decoder, self).put(0, 0, output_id, data) | |
273 | ||
274 | def decode(self, ss, es, data): | |
275 | for samplenum, (scl, sda) in data: | |
276 | self.samplecnt += 1 | |
277 | ||
278 | # First sample: Save SCL/SDA value. | |
279 | if self.oldscl == None: | |
280 | self.oldscl = scl | |
281 | self.oldsda = sda | |
282 | continue | |
283 | ||
284 | # TODO: Wait until the bus is idle (SDA = SCL = 1) first? | |
285 | ||
286 | # State machine. | |
287 | if self.state == FIND_START: | |
288 | if self.is_start_condition(scl, sda): | |
289 | self.found_start(scl, sda) | |
290 | elif self.state == FIND_ADDRESS: | |
291 | if self.is_data_bit(scl, sda): | |
292 | self.found_address_or_data(scl, sda) | |
293 | elif self.state == FIND_DATA: | |
294 | if self.is_data_bit(scl, sda): | |
295 | self.found_address_or_data(scl, sda) | |
296 | elif self.is_start_condition(scl, sda): | |
297 | self.found_start(scl, sda) | |
298 | elif self.is_stop_condition(scl, sda): | |
299 | self.found_stop(scl, sda) | |
300 | else: | |
301 | # TODO: Error? | |
302 | pass | |
303 | ||
304 | # Save current SDA/SCL values for the next round. | |
305 | self.oldscl = scl | |
306 | self.oldsda = sda | |
307 |