]>
Commit | Line | Data |
---|---|---|
0588ed70 | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
0588ed70 | 3 | ## |
592f355b | 4 | ## Copyright (C) 2010-2016 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 | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
0588ed70 UH |
18 | ## |
19 | ||
0588ed70 | 20 | # TODO: Look into arbitration, collision detection, clock synchronisation, etc. |
0588ed70 UH |
21 | # TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0). |
22 | # TODO: Implement support for detecting various bus errors. | |
23fb2e12 | 23 | |
647aba6a | 24 | from common.srdhelper import bitpack_msb |
677d597b | 25 | import sigrokdecode as srd |
b2c19614 | 26 | |
f1428c4c | 27 | ''' |
c515eed7 | 28 | OUTPUT_PYTHON format: |
f1428c4c | 29 | |
bf69977d UH |
30 | Packet: |
31 | [<ptype>, <pdata>] | |
f1428c4c | 32 | |
bf69977d | 33 | <ptype>: |
f1428c4c UH |
34 | - 'START' (START condition) |
35 | - 'START REPEAT' (Repeated START condition) | |
36 | - 'ADDRESS READ' (Slave address, read) | |
37 | - 'ADDRESS WRITE' (Slave address, write) | |
38 | - 'DATA READ' (Data, read) | |
39 | - 'DATA WRITE' (Data, write) | |
40 | - 'STOP' (STOP condition) | |
41 | - 'ACK' (ACK bit) | |
42 | - 'NACK' (NACK bit) | |
bf69977d | 43 | - 'BITS' (<pdata>: list of data/address bits and their ss/es numbers) |
f1428c4c | 44 | |
bf69977d | 45 | <pdata> is the data or address byte associated with the 'ADDRESS*' and 'DATA*' |
f1428c4c UH |
46 | command. Slave addresses do not include bit 0 (the READ/WRITE indication bit). |
47 | For example, a slave address field could be 0x51 (instead of 0xa2). | |
bf69977d | 48 | For 'START', 'START REPEAT', 'STOP', 'ACK', and 'NACK' <pdata> is None. |
f1428c4c UH |
49 | ''' |
50 | ||
d94ff143 | 51 | # CMD: [annotation-type-index, long annotation, short annotation] |
1541976f | 52 | proto = { |
d94ff143 UH |
53 | 'START': [0, 'Start', 'S'], |
54 | 'START REPEAT': [1, 'Start repeat', 'Sr'], | |
55 | 'STOP': [2, 'Stop', 'P'], | |
56 | 'ACK': [3, 'ACK', 'A'], | |
57 | 'NACK': [4, 'NACK', 'N'], | |
de038c47 UH |
58 | 'BIT': [5, 'Bit', 'B'], |
59 | 'ADDRESS READ': [6, 'Address read', 'AR'], | |
60 | 'ADDRESS WRITE': [7, 'Address write', 'AW'], | |
61 | 'DATA READ': [8, 'Data read', 'DR'], | |
62 | 'DATA WRITE': [9, 'Data write', 'DW'], | |
15969949 | 63 | } |
e5080882 | 64 | |
677d597b | 65 | class Decoder(srd.Decoder): |
592f355b | 66 | api_version = 3 |
67e847fd | 67 | id = 'i2c' |
ab4aa33c | 68 | name = 'I²C' |
9a12a6e7 | 69 | longname = 'Inter-Integrated Circuit' |
a465436e | 70 | desc = 'Two-wire, multi-master, serial bus.' |
f39d2404 UH |
71 | license = 'gplv2+' |
72 | inputs = ['logic'] | |
73 | outputs = ['i2c'] | |
d6d8a8a4 | 74 | tags = ['Embedded/industrial'] |
6a15597a | 75 | channels = ( |
bc5f5a43 BV |
76 | {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'}, |
77 | {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'}, | |
da9bcbd9 | 78 | ) |
84c1c0b5 BV |
79 | options = ( |
80 | {'id': 'address_format', 'desc': 'Displayed slave address format', | |
81 | 'default': 'shifted', 'values': ('shifted', 'unshifted')}, | |
82 | ) | |
da9bcbd9 BV |
83 | annotations = ( |
84 | ('start', 'Start condition'), | |
85 | ('repeat-start', 'Repeat start condition'), | |
86 | ('stop', 'Stop condition'), | |
87 | ('ack', 'ACK'), | |
88 | ('nack', 'NACK'), | |
89 | ('bit', 'Data/address bit'), | |
90 | ('address-read', 'Address read'), | |
91 | ('address-write', 'Address write'), | |
92 | ('data-read', 'Data read'), | |
93 | ('data-write', 'Data write'), | |
e144452b | 94 | ('warning', 'Warning'), |
da9bcbd9 | 95 | ) |
de038c47 UH |
96 | annotation_rows = ( |
97 | ('bits', 'Bits', (5,)), | |
e144452b | 98 | ('addr-data', 'Address/data', (0, 1, 2, 3, 4, 6, 7, 8, 9)), |
de038c47 UH |
99 | ('warnings', 'Warnings', (10,)), |
100 | ) | |
a929afa6 | 101 | binary = ( |
5cb2cb02 BV |
102 | ('address-read', 'Address read'), |
103 | ('address-write', 'Address write'), | |
104 | ('data-read', 'Data read'), | |
105 | ('data-write', 'Data write'), | |
a929afa6 | 106 | ) |
0588ed70 | 107 | |
92b7b49f | 108 | def __init__(self): |
10aeb8ea GS |
109 | self.reset() |
110 | ||
111 | def reset(self): | |
8d2a9636 | 112 | self.samplerate = None |
486b19ce | 113 | self.ss = self.es = self.ss_byte = -1 |
14ba515b | 114 | self.is_write = None |
4f139c28 | 115 | self.rem_addr_bytes = None |
14ba515b | 116 | self.is_repeat_start = False |
2b716038 | 117 | self.state = 'FIND START' |
8d2a9636 BV |
118 | self.pdu_start = None |
119 | self.pdu_bits = 0 | |
14ba515b | 120 | self.data_bits = [] |
8d2a9636 BV |
121 | |
122 | def metadata(self, key, value): | |
123 | if key == srd.SRD_CONF_SAMPLERATE: | |
124 | self.samplerate = value | |
f39d2404 | 125 | |
8915b346 | 126 | def start(self): |
c515eed7 | 127 | self.out_python = self.register(srd.OUTPUT_PYTHON) |
8d2a9636 | 128 | self.out_ann = self.register(srd.OUTPUT_ANN) |
be6733ca | 129 | self.out_binary = self.register(srd.OUTPUT_BINARY) |
8d2a9636 BV |
130 | self.out_bitrate = self.register(srd.OUTPUT_META, |
131 | meta=(int, 'Bitrate', 'Bitrate from Start bit to Stop bit')) | |
3643fc3f | 132 | |
d94ff143 | 133 | def putx(self, data): |
de038c47 | 134 | self.put(self.ss, self.es, self.out_ann, data) |
d94ff143 UH |
135 | |
136 | def putp(self, data): | |
de038c47 | 137 | self.put(self.ss, self.es, self.out_python, data) |
d94ff143 | 138 | |
a929afa6 | 139 | def putb(self, data): |
de038c47 | 140 | self.put(self.ss, self.es, self.out_binary, data) |
a929afa6 | 141 | |
592f355b | 142 | def handle_start(self, pins): |
de038c47 | 143 | self.ss, self.es = self.samplenum, self.samplenum |
8d2a9636 BV |
144 | self.pdu_start = self.samplenum |
145 | self.pdu_bits = 0 | |
14ba515b | 146 | cmd = 'START REPEAT' if self.is_repeat_start else 'START' |
d94ff143 UH |
147 | self.putp([cmd, None]) |
148 | self.putx([proto[cmd][0], proto[cmd][1:]]) | |
2b716038 | 149 | self.state = 'FIND ADDRESS' |
14ba515b GS |
150 | self.is_repeat_start = True |
151 | self.is_write = None | |
4f139c28 | 152 | self.rem_addr_bytes = None |
647aba6a | 153 | self.data_bits.clear() |
7b86f0bc | 154 | |
c4975078 | 155 | # Gather 8 bits of data plus the ACK/NACK bit. |
592f355b UH |
156 | def handle_address_or_data(self, pins): |
157 | scl, sda = pins | |
158 | self.pdu_bits += 1 | |
159 | ||
647aba6a GS |
160 | # Accumulate a byte's bits, including its start position. |
161 | # Accumulate individual bits and their start/end sample numbers | |
162 | # as we see them. Get the start sample number at the time when | |
163 | # the bit value gets sampled. Assume the start of the next bit | |
164 | # as the end sample number of the previous bit. Guess the last | |
165 | # bit's end sample number from the second last bit's width. | |
166 | # (gsi: Shouldn't falling SCL be the end of the bit value?) | |
167 | # Keep the bits in receive order (MSB first) during accumulation. | |
168 | if not self.data_bits: | |
486b19ce | 169 | self.ss_byte = self.samplenum |
647aba6a GS |
170 | if self.data_bits: |
171 | self.data_bits[-1][2] = self.samplenum | |
172 | self.data_bits.append([sda, self.samplenum, self.samplenum]) | |
173 | if len(self.data_bits) < 8: | |
eb7082c9 | 174 | return |
647aba6a GS |
175 | self.bitwidth = self.data_bits[-2][2] - self.data_bits[-3][2] |
176 | self.data_bits[-1][2] += self.bitwidth | |
7b86f0bc | 177 | |
647aba6a GS |
178 | # Get the byte value. Address and data are transmitted MSB-first. |
179 | d = bitpack_msb(self.data_bits, 0) | |
2b716038 | 180 | if self.state == 'FIND ADDRESS': |
4f139c28 GS |
181 | # The READ/WRITE bit is only in the first address byte, not |
182 | # in data bytes. Address bit pattern 0b1111_0xxx means that | |
183 | # this is a 10bit slave address, another byte follows. Get | |
184 | # the R/W direction and the address bytes count from the | |
185 | # first byte in the I2C transfer. | |
186 | addr_byte = d | |
187 | if self.rem_addr_bytes is None: | |
188 | if (addr_byte & 0xf8) == 0xf0: | |
189 | self.rem_addr_bytes = 2 | |
190 | self.slave_addr_7 = None | |
191 | self.slave_addr_10 = addr_byte & 0x06 | |
192 | self.slave_addr_10 <<= 7 | |
193 | else: | |
194 | self.rem_addr_bytes = 1 | |
195 | self.slave_addr_7 = addr_byte >> 1 | |
196 | self.slave_addr_10 = None | |
197 | is_seven = self.slave_addr_7 is not None | |
198 | if self.is_write is None: | |
199 | read_bit = bool(addr_byte & 1) | |
200 | shift_seven = self.options['address_format'] == 'shifted' | |
201 | if is_seven and shift_seven: | |
202 | d = d >> 1 | |
203 | self.is_write = False if read_bit else True | |
204 | else: | |
205 | self.slave_addr_10 |= addr_byte | |
15969949 | 206 | |
a929afa6 | 207 | bin_class = -1 |
14ba515b | 208 | if self.state == 'FIND ADDRESS' and self.is_write: |
a2d2aff2 | 209 | cmd = 'ADDRESS WRITE' |
a929afa6 | 210 | bin_class = 1 |
14ba515b | 211 | elif self.state == 'FIND ADDRESS' and not self.is_write: |
a2d2aff2 | 212 | cmd = 'ADDRESS READ' |
a929afa6 | 213 | bin_class = 0 |
14ba515b | 214 | elif self.state == 'FIND DATA' and self.is_write: |
a2d2aff2 | 215 | cmd = 'DATA WRITE' |
a929afa6 | 216 | bin_class = 3 |
14ba515b | 217 | elif self.state == 'FIND DATA' and not self.is_write: |
a2d2aff2 | 218 | cmd = 'DATA READ' |
a929afa6 | 219 | bin_class = 2 |
eb7082c9 | 220 | |
486b19ce | 221 | self.ss, self.es = self.ss_byte, self.samplenum + self.bitwidth |
de038c47 | 222 | |
647aba6a GS |
223 | # Reverse the list of bits to LSB first order before emitting |
224 | # annotations and passing bits to upper layers. This may be | |
225 | # unexpected because the protocol is MSB first, but it keeps | |
226 | # backwards compatibility. | |
227 | self.data_bits.reverse() | |
14ba515b | 228 | self.putp(['BITS', self.data_bits]) |
d94ff143 | 229 | self.putp([cmd, d]) |
de038c47 | 230 | |
2824e811 | 231 | self.putb([bin_class, bytes([d])]) |
7b86f0bc | 232 | |
14ba515b | 233 | for bit in self.data_bits: |
de038c47 UH |
234 | self.put(bit[1], bit[2], self.out_ann, [5, ['%d' % bit[0]]]) |
235 | ||
4f139c28 | 236 | if cmd.startswith('ADDRESS') and is_seven: |
de038c47 | 237 | self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth |
14ba515b | 238 | w = ['Write', 'Wr', 'W'] if self.is_write else ['Read', 'Rd', 'R'] |
de038c47 | 239 | self.putx([proto[cmd][0], w]) |
486b19ce | 240 | self.ss, self.es = self.ss_byte, self.samplenum |
de038c47 UH |
241 | |
242 | self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d), | |
243 | '%s: %02X' % (proto[cmd][2], d), '%02X' % d]]) | |
244 | ||
1b75abfd | 245 | # Done with this packet. |
647aba6a | 246 | self.data_bits.clear() |
2b716038 | 247 | self.state = 'FIND ACK' |
7b86f0bc | 248 | |
592f355b UH |
249 | def get_ack(self, pins): |
250 | scl, sda = pins | |
de038c47 | 251 | self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth |
d94ff143 UH |
252 | cmd = 'NACK' if (sda == 1) else 'ACK' |
253 | self.putp([cmd, None]) | |
254 | self.putx([proto[cmd][0], proto[cmd][1:]]) | |
4f139c28 GS |
255 | # Slave addresses can span one or two bytes, before data bytes |
256 | # follow. There can be an arbitrary number of data bytes. Stick | |
257 | # with getting more address bytes if applicable, or enter or | |
258 | # remain in the data phase of the transfer otherwise. | |
259 | if self.rem_addr_bytes: | |
260 | self.rem_addr_bytes -= 1 | |
261 | if self.rem_addr_bytes: | |
262 | self.state = 'FIND ADDRESS' | |
263 | else: | |
264 | self.state = 'FIND DATA' | |
7b86f0bc | 265 | |
592f355b | 266 | def handle_stop(self, pins): |
8d2a9636 | 267 | # Meta bitrate |
8accc30b GS |
268 | if self.samplerate: |
269 | elapsed = 1 / float(self.samplerate) * (self.samplenum - self.pdu_start + 1) | |
270 | bitrate = int(1 / elapsed * self.pdu_bits) | |
271 | self.put(self.ss_byte, self.samplenum, self.out_bitrate, bitrate) | |
8d2a9636 | 272 | |
d94ff143 | 273 | cmd = 'STOP' |
de038c47 | 274 | self.ss, self.es = self.samplenum, self.samplenum |
d94ff143 UH |
275 | self.putp([cmd, None]) |
276 | self.putx([proto[cmd][0], proto[cmd][1:]]) | |
2b716038 | 277 | self.state = 'FIND START' |
14ba515b GS |
278 | self.is_repeat_start = False |
279 | self.is_write = None | |
647aba6a | 280 | self.data_bits.clear() |
7b86f0bc | 281 | |
592f355b | 282 | def decode(self): |
592f355b | 283 | while True: |
7b86f0bc | 284 | # State machine. |
2b716038 | 285 | if self.state == 'FIND START': |
592f355b UH |
286 | # Wait for a START condition (S): SCL = high, SDA = falling. |
287 | self.handle_start(self.wait({0: 'h', 1: 'f'})) | |
2b716038 | 288 | elif self.state == 'FIND ADDRESS': |
592f355b UH |
289 | # Wait for a data bit: SCL = rising. |
290 | self.handle_address_or_data(self.wait({0: 'r'})) | |
2b716038 | 291 | elif self.state == 'FIND DATA': |
592f355b UH |
292 | # Wait for any of the following conditions (or combinations): |
293 | # a) Data sampling of receiver: SCL = rising, and/or | |
294 | # b) START condition (S): SCL = high, SDA = falling, and/or | |
295 | # c) STOP condition (P): SCL = high, SDA = rising | |
fcd5d23a | 296 | pins = self.wait([{0: 'r'}, {0: 'h', 1: 'f'}, {0: 'h', 1: 'r'}]) |
592f355b UH |
297 | |
298 | # Check which of the condition(s) matched and handle them. | |
299 | if self.matched[0]: | |
300 | self.handle_address_or_data(pins) | |
301 | elif self.matched[1]: | |
302 | self.handle_start(pins) | |
303 | elif self.matched[2]: | |
304 | self.handle_stop(pins) | |
2b716038 | 305 | elif self.state == 'FIND ACK': |
592f355b UH |
306 | # Wait for a data/ack bit: SCL = rising. |
307 | self.get_ack(self.wait({0: 'r'})) |