]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2c/pd.py
i2c: Drop obsolete 10bit slave address TODO.
[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
d2bdb130
BV
64class SamplerateError(Exception):
65 pass
66
677d597b 67class Decoder(srd.Decoder):
592f355b 68 api_version = 3
67e847fd 69 id = 'i2c'
ab4aa33c 70 name = 'I²C'
9a12a6e7 71 longname = 'Inter-Integrated Circuit'
a465436e 72 desc = 'Two-wire, multi-master, serial bus.'
f39d2404
UH
73 license = 'gplv2+'
74 inputs = ['logic']
75 outputs = ['i2c']
6a15597a 76 channels = (
bc5f5a43
BV
77 {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'},
78 {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'},
da9bcbd9 79 )
84c1c0b5
BV
80 options = (
81 {'id': 'address_format', 'desc': 'Displayed slave address format',
82 'default': 'shifted', 'values': ('shifted', 'unshifted')},
83 )
da9bcbd9
BV
84 annotations = (
85 ('start', 'Start condition'),
86 ('repeat-start', 'Repeat start condition'),
87 ('stop', 'Stop condition'),
88 ('ack', 'ACK'),
89 ('nack', 'NACK'),
90 ('bit', 'Data/address bit'),
91 ('address-read', 'Address read'),
92 ('address-write', 'Address write'),
93 ('data-read', 'Data read'),
94 ('data-write', 'Data write'),
95 ('warnings', 'Human-readable warnings'),
96 )
de038c47
UH
97 annotation_rows = (
98 ('bits', 'Bits', (5,)),
99 ('addr-data', 'Address/Data', (0, 1, 2, 3, 4, 6, 7, 8, 9)),
100 ('warnings', 'Warnings', (10,)),
101 )
a929afa6 102 binary = (
5cb2cb02
BV
103 ('address-read', 'Address read'),
104 ('address-write', 'Address write'),
105 ('data-read', 'Data read'),
106 ('data-write', 'Data write'),
a929afa6 107 )
0588ed70 108
92b7b49f 109 def __init__(self):
10aeb8ea
GS
110 self.reset()
111
112 def reset(self):
8d2a9636 113 self.samplerate = None
486b19ce 114 self.ss = self.es = self.ss_byte = -1
f39d2404
UH
115 self.bitcount = 0
116 self.databyte = 0
117 self.wr = -1
5dd9af5b 118 self.is_repeat_start = 0
2b716038 119 self.state = 'FIND START'
8d2a9636
BV
120 self.pdu_start = None
121 self.pdu_bits = 0
de038c47 122 self.bits = []
8d2a9636
BV
123
124 def metadata(self, key, value):
125 if key == srd.SRD_CONF_SAMPLERATE:
126 self.samplerate = value
f39d2404 127
8915b346 128 def start(self):
c515eed7 129 self.out_python = self.register(srd.OUTPUT_PYTHON)
8d2a9636 130 self.out_ann = self.register(srd.OUTPUT_ANN)
be6733ca 131 self.out_binary = self.register(srd.OUTPUT_BINARY)
8d2a9636
BV
132 self.out_bitrate = self.register(srd.OUTPUT_META,
133 meta=(int, 'Bitrate', 'Bitrate from Start bit to Stop bit'))
3643fc3f 134
d94ff143 135 def putx(self, data):
de038c47 136 self.put(self.ss, self.es, self.out_ann, data)
d94ff143
UH
137
138 def putp(self, data):
de038c47 139 self.put(self.ss, self.es, self.out_python, data)
d94ff143 140
a929afa6 141 def putb(self, data):
de038c47 142 self.put(self.ss, self.es, self.out_binary, data)
a929afa6 143
592f355b 144 def handle_start(self, pins):
de038c47 145 self.ss, self.es = self.samplenum, self.samplenum
8d2a9636
BV
146 self.pdu_start = self.samplenum
147 self.pdu_bits = 0
c4975078 148 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START'
d94ff143
UH
149 self.putp([cmd, None])
150 self.putx([proto[cmd][0], proto[cmd][1:]])
2b716038 151 self.state = 'FIND ADDRESS'
7b86f0bc 152 self.bitcount = self.databyte = 0
5dd9af5b 153 self.is_repeat_start = 1
7b86f0bc 154 self.wr = -1
de038c47 155 self.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).
172 self.bits.insert(0, [sda, self.samplenum, self.samplenum])
173 if self.bitcount > 0:
174 self.bits[1][2] = self.samplenum
175 if self.bitcount == 7:
176 self.bitwidth = self.bits[1][2] - self.bits[2][2]
177 self.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':
7b86f0bc 186 # The READ/WRITE bit is only in address bytes, not data bytes.
bf1c3f4d 187 self.wr = 0 if (self.databyte & 1) else 1
d94ff143
UH
188 if self.options['address_format'] == 'shifted':
189 d = d >> 1
15969949 190
a929afa6 191 bin_class = -1
2b716038 192 if self.state == 'FIND ADDRESS' and self.wr == 1:
a2d2aff2 193 cmd = 'ADDRESS WRITE'
a929afa6 194 bin_class = 1
2b716038 195 elif self.state == 'FIND ADDRESS' and self.wr == 0:
a2d2aff2 196 cmd = 'ADDRESS READ'
a929afa6 197 bin_class = 0
2b716038 198 elif self.state == 'FIND DATA' and self.wr == 1:
a2d2aff2 199 cmd = 'DATA WRITE'
a929afa6 200 bin_class = 3
2b716038 201 elif self.state == 'FIND DATA' and self.wr == 0:
a2d2aff2 202 cmd = 'DATA READ'
a929afa6 203 bin_class = 2
eb7082c9 204
486b19ce 205 self.ss, self.es = self.ss_byte, self.samplenum + self.bitwidth
de038c47
UH
206
207 self.putp(['BITS', self.bits])
d94ff143 208 self.putp([cmd, d])
de038c47 209
2824e811 210 self.putb([bin_class, bytes([d])])
7b86f0bc 211
de038c47
UH
212 for bit in self.bits:
213 self.put(bit[1], bit[2], self.out_ann, [5, ['%d' % bit[0]]])
214
215 if cmd.startswith('ADDRESS'):
216 self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth
217 w = ['Write', 'Wr', 'W'] if self.wr else ['Read', 'Rd', 'R']
218 self.putx([proto[cmd][0], w])
486b19ce 219 self.ss, self.es = self.ss_byte, self.samplenum
de038c47
UH
220
221 self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d),
222 '%s: %02X' % (proto[cmd][2], d), '%02X' % d]])
223
1b75abfd 224 # Done with this packet.
1b75abfd 225 self.bitcount = self.databyte = 0
de038c47 226 self.bits = []
2b716038 227 self.state = 'FIND ACK'
7b86f0bc 228
592f355b
UH
229 def get_ack(self, pins):
230 scl, sda = pins
de038c47 231 self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth
d94ff143
UH
232 cmd = 'NACK' if (sda == 1) else 'ACK'
233 self.putp([cmd, None])
234 self.putx([proto[cmd][0], proto[cmd][1:]])
1b75abfd
BV
235 # There could be multiple data bytes in a row, so either find
236 # another data byte or a STOP condition next.
2b716038 237 self.state = 'FIND DATA'
7b86f0bc 238
592f355b 239 def handle_stop(self, pins):
8d2a9636
BV
240 # Meta bitrate
241 elapsed = 1 / float(self.samplerate) * (self.samplenum - self.pdu_start + 1)
242 bitrate = int(1 / elapsed * self.pdu_bits)
486b19ce 243 self.put(self.ss_byte, self.samplenum, self.out_bitrate, bitrate)
8d2a9636 244
d94ff143 245 cmd = 'STOP'
de038c47 246 self.ss, self.es = self.samplenum, self.samplenum
d94ff143
UH
247 self.putp([cmd, None])
248 self.putx([proto[cmd][0], proto[cmd][1:]])
2b716038 249 self.state = 'FIND START'
5dd9af5b 250 self.is_repeat_start = 0
7b86f0bc 251 self.wr = -1
de038c47 252 self.bits = []
7b86f0bc 253
592f355b 254 def decode(self):
d2bdb130 255 if not self.samplerate:
21cda951 256 raise SamplerateError('Cannot decode without samplerate.')
2fcd7c22 257
592f355b 258 while True:
7b86f0bc 259 # State machine.
2b716038 260 if self.state == 'FIND START':
592f355b
UH
261 # Wait for a START condition (S): SCL = high, SDA = falling.
262 self.handle_start(self.wait({0: 'h', 1: 'f'}))
2b716038 263 elif self.state == 'FIND ADDRESS':
592f355b
UH
264 # Wait for a data bit: SCL = rising.
265 self.handle_address_or_data(self.wait({0: 'r'}))
2b716038 266 elif self.state == 'FIND DATA':
592f355b
UH
267 # Wait for any of the following conditions (or combinations):
268 # a) Data sampling of receiver: SCL = rising, and/or
269 # b) START condition (S): SCL = high, SDA = falling, and/or
270 # c) STOP condition (P): SCL = high, SDA = rising
fcd5d23a 271 pins = self.wait([{0: 'r'}, {0: 'h', 1: 'f'}, {0: 'h', 1: 'r'}])
592f355b
UH
272
273 # Check which of the condition(s) matched and handle them.
274 if self.matched[0]:
275 self.handle_address_or_data(pins)
276 elif self.matched[1]:
277 self.handle_start(pins)
278 elif self.matched[2]:
279 self.handle_stop(pins)
2b716038 280 elif self.state == 'FIND ACK':
592f355b
UH
281 # Wait for a data/ack bit: SCL = rising.
282 self.get_ack(self.wait({0: 'r'}))