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