]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2c/pd.py
i2c: Convert to PD API version 3.
[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
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
0588ed70 21# TODO: Look into arbitration, collision detection, clock synchronisation, etc.
9e95e4d8 22# TODO: Implement support for 10bit slave addresses.
0588ed70
UH
23# TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0).
24# TODO: Implement support for detecting various bus errors.
23fb2e12 25
677d597b 26import sigrokdecode as srd
b2c19614 27
f1428c4c 28'''
c515eed7 29OUTPUT_PYTHON format:
f1428c4c 30
bf69977d
UH
31Packet:
32[<ptype>, <pdata>]
f1428c4c 33
bf69977d 34<ptype>:
f1428c4c
UH
35 - 'START' (START condition)
36 - 'START REPEAT' (Repeated START condition)
37 - 'ADDRESS READ' (Slave address, read)
38 - 'ADDRESS WRITE' (Slave address, write)
39 - 'DATA READ' (Data, read)
40 - 'DATA WRITE' (Data, write)
41 - 'STOP' (STOP condition)
42 - 'ACK' (ACK bit)
43 - 'NACK' (NACK bit)
bf69977d 44 - 'BITS' (<pdata>: list of data/address bits and their ss/es numbers)
f1428c4c 45
bf69977d 46<pdata> is the data or address byte associated with the 'ADDRESS*' and 'DATA*'
f1428c4c
UH
47command. Slave addresses do not include bit 0 (the READ/WRITE indication bit).
48For example, a slave address field could be 0x51 (instead of 0xa2).
bf69977d 49For 'START', 'START REPEAT', 'STOP', 'ACK', and 'NACK' <pdata> is None.
f1428c4c
UH
50'''
51
d94ff143 52# CMD: [annotation-type-index, long annotation, short annotation]
1541976f 53proto = {
d94ff143
UH
54 'START': [0, 'Start', 'S'],
55 'START REPEAT': [1, 'Start repeat', 'Sr'],
56 'STOP': [2, 'Stop', 'P'],
57 'ACK': [3, 'ACK', 'A'],
58 'NACK': [4, 'NACK', 'N'],
de038c47
UH
59 'BIT': [5, 'Bit', 'B'],
60 'ADDRESS READ': [6, 'Address read', 'AR'],
61 'ADDRESS WRITE': [7, 'Address write', 'AW'],
62 'DATA READ': [8, 'Data read', 'DR'],
63 'DATA WRITE': [9, 'Data write', 'DW'],
15969949 64}
e5080882 65
d2bdb130
BV
66class SamplerateError(Exception):
67 pass
68
677d597b 69class Decoder(srd.Decoder):
592f355b 70 api_version = 3
67e847fd 71 id = 'i2c'
ab4aa33c 72 name = 'I²C'
9a12a6e7 73 longname = 'Inter-Integrated Circuit'
a465436e 74 desc = 'Two-wire, multi-master, serial bus.'
f39d2404
UH
75 license = 'gplv2+'
76 inputs = ['logic']
77 outputs = ['i2c']
6a15597a 78 channels = (
bc5f5a43
BV
79 {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'},
80 {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'},
da9bcbd9 81 )
84c1c0b5
BV
82 options = (
83 {'id': 'address_format', 'desc': 'Displayed slave address format',
84 'default': 'shifted', 'values': ('shifted', 'unshifted')},
85 )
da9bcbd9
BV
86 annotations = (
87 ('start', 'Start condition'),
88 ('repeat-start', 'Repeat start condition'),
89 ('stop', 'Stop condition'),
90 ('ack', 'ACK'),
91 ('nack', 'NACK'),
92 ('bit', 'Data/address bit'),
93 ('address-read', 'Address read'),
94 ('address-write', 'Address write'),
95 ('data-read', 'Data read'),
96 ('data-write', 'Data write'),
97 ('warnings', 'Human-readable warnings'),
98 )
de038c47
UH
99 annotation_rows = (
100 ('bits', 'Bits', (5,)),
101 ('addr-data', 'Address/Data', (0, 1, 2, 3, 4, 6, 7, 8, 9)),
102 ('warnings', 'Warnings', (10,)),
103 )
a929afa6 104 binary = (
5cb2cb02
BV
105 ('address-read', 'Address read'),
106 ('address-write', 'Address write'),
107 ('data-read', 'Data read'),
108 ('data-write', 'Data write'),
a929afa6 109 )
0588ed70 110
92b7b49f 111 def __init__(self):
8d2a9636 112 self.samplerate = None
486b19ce 113 self.ss = self.es = self.ss_byte = -1
f39d2404
UH
114 self.bitcount = 0
115 self.databyte = 0
116 self.wr = -1
5dd9af5b 117 self.is_repeat_start = 0
2b716038 118 self.state = 'FIND START'
8d2a9636
BV
119 self.pdu_start = None
120 self.pdu_bits = 0
de038c47 121 self.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
592f355b
UH
134 # Assume that the initial SCL/SDA pin state is high (logic 1).
135 # This is a good default, since both pins have pullups as per spec.
136 self.initial_pins = [1, 1]
137
d94ff143 138 def putx(self, data):
de038c47 139 self.put(self.ss, self.es, self.out_ann, data)
d94ff143
UH
140
141 def putp(self, data):
de038c47 142 self.put(self.ss, self.es, self.out_python, data)
d94ff143 143
a929afa6 144 def putb(self, data):
de038c47 145 self.put(self.ss, self.es, self.out_binary, data)
a929afa6 146
592f355b 147 def handle_start(self, pins):
de038c47 148 self.ss, self.es = self.samplenum, self.samplenum
8d2a9636
BV
149 self.pdu_start = self.samplenum
150 self.pdu_bits = 0
c4975078 151 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START'
d94ff143
UH
152 self.putp([cmd, None])
153 self.putx([proto[cmd][0], proto[cmd][1:]])
2b716038 154 self.state = 'FIND ADDRESS'
7b86f0bc 155 self.bitcount = self.databyte = 0
5dd9af5b 156 self.is_repeat_start = 1
7b86f0bc 157 self.wr = -1
de038c47 158 self.bits = []
7b86f0bc 159
c4975078 160 # Gather 8 bits of data plus the ACK/NACK bit.
592f355b
UH
161 def handle_address_or_data(self, pins):
162 scl, sda = pins
163 self.pdu_bits += 1
164
7b86f0bc
UH
165 # Address and data are transmitted MSB-first.
166 self.databyte <<= 1
167 self.databyte |= sda
168
de038c47 169 # Remember the start of the first data/address bit.
c4975078 170 if self.bitcount == 0:
486b19ce 171 self.ss_byte = self.samplenum
de038c47
UH
172
173 # Store individual bits and their start/end samplenumbers.
174 # In the list, index 0 represents the LSB (I²C transmits MSB-first).
175 self.bits.insert(0, [sda, self.samplenum, self.samplenum])
176 if self.bitcount > 0:
177 self.bits[1][2] = self.samplenum
178 if self.bitcount == 7:
179 self.bitwidth = self.bits[1][2] - self.bits[2][2]
180 self.bits[0][2] += self.bitwidth
c4975078 181
7b86f0bc 182 # Return if we haven't collected all 8 + 1 bits, yet.
de038c47
UH
183 if self.bitcount < 7:
184 self.bitcount += 1
eb7082c9 185 return
7b86f0bc 186
d94ff143 187 d = self.databyte
2b716038 188 if self.state == 'FIND ADDRESS':
7b86f0bc 189 # The READ/WRITE bit is only in address bytes, not data bytes.
bf1c3f4d 190 self.wr = 0 if (self.databyte & 1) else 1
d94ff143
UH
191 if self.options['address_format'] == 'shifted':
192 d = d >> 1
15969949 193
a929afa6 194 bin_class = -1
2b716038 195 if self.state == 'FIND ADDRESS' and self.wr == 1:
a2d2aff2 196 cmd = 'ADDRESS WRITE'
a929afa6 197 bin_class = 1
2b716038 198 elif self.state == 'FIND ADDRESS' and self.wr == 0:
a2d2aff2 199 cmd = 'ADDRESS READ'
a929afa6 200 bin_class = 0
2b716038 201 elif self.state == 'FIND DATA' and self.wr == 1:
a2d2aff2 202 cmd = 'DATA WRITE'
a929afa6 203 bin_class = 3
2b716038 204 elif self.state == 'FIND DATA' and self.wr == 0:
a2d2aff2 205 cmd = 'DATA READ'
a929afa6 206 bin_class = 2
eb7082c9 207
486b19ce 208 self.ss, self.es = self.ss_byte, self.samplenum + self.bitwidth
de038c47
UH
209
210 self.putp(['BITS', self.bits])
d94ff143 211 self.putp([cmd, d])
de038c47 212
2824e811 213 self.putb([bin_class, bytes([d])])
7b86f0bc 214
de038c47
UH
215 for bit in self.bits:
216 self.put(bit[1], bit[2], self.out_ann, [5, ['%d' % bit[0]]])
217
218 if cmd.startswith('ADDRESS'):
219 self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth
220 w = ['Write', 'Wr', 'W'] if self.wr else ['Read', 'Rd', 'R']
221 self.putx([proto[cmd][0], w])
486b19ce 222 self.ss, self.es = self.ss_byte, self.samplenum
de038c47
UH
223
224 self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d),
225 '%s: %02X' % (proto[cmd][2], d), '%02X' % d]])
226
1b75abfd 227 # Done with this packet.
1b75abfd 228 self.bitcount = self.databyte = 0
de038c47 229 self.bits = []
2b716038 230 self.state = 'FIND ACK'
7b86f0bc 231
592f355b
UH
232 def get_ack(self, pins):
233 scl, sda = pins
de038c47 234 self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth
d94ff143
UH
235 cmd = 'NACK' if (sda == 1) else 'ACK'
236 self.putp([cmd, None])
237 self.putx([proto[cmd][0], proto[cmd][1:]])
1b75abfd
BV
238 # There could be multiple data bytes in a row, so either find
239 # another data byte or a STOP condition next.
2b716038 240 self.state = 'FIND DATA'
7b86f0bc 241
592f355b 242 def handle_stop(self, pins):
8d2a9636
BV
243 # Meta bitrate
244 elapsed = 1 / float(self.samplerate) * (self.samplenum - self.pdu_start + 1)
245 bitrate = int(1 / elapsed * self.pdu_bits)
486b19ce 246 self.put(self.ss_byte, self.samplenum, self.out_bitrate, bitrate)
8d2a9636 247
d94ff143 248 cmd = 'STOP'
de038c47 249 self.ss, self.es = self.samplenum, self.samplenum
d94ff143
UH
250 self.putp([cmd, None])
251 self.putx([proto[cmd][0], proto[cmd][1:]])
2b716038 252 self.state = 'FIND START'
5dd9af5b 253 self.is_repeat_start = 0
7b86f0bc 254 self.wr = -1
de038c47 255 self.bits = []
7b86f0bc 256
592f355b 257 def decode(self):
d2bdb130 258 if not self.samplerate:
21cda951 259 raise SamplerateError('Cannot decode without samplerate.')
2fcd7c22 260
592f355b 261 self.wait({})
8d2a9636 262
592f355b 263 while True:
7b86f0bc 264 # State machine.
2b716038 265 if self.state == 'FIND START':
592f355b
UH
266 # Wait for a START condition (S): SCL = high, SDA = falling.
267 self.handle_start(self.wait({0: 'h', 1: 'f'}))
2b716038 268 elif self.state == 'FIND ADDRESS':
592f355b
UH
269 # Wait for a data bit: SCL = rising.
270 self.handle_address_or_data(self.wait({0: 'r'}))
2b716038 271 elif self.state == 'FIND DATA':
592f355b
UH
272 # Wait for any of the following conditions (or combinations):
273 # a) Data sampling of receiver: SCL = rising, and/or
274 # b) START condition (S): SCL = high, SDA = falling, and/or
275 # c) STOP condition (P): SCL = high, SDA = rising
276 conds = [{0: 'r'}, {0: 'h', 1: 'f'}, {0: 'h', 1: 'r'}]
277 pins = self.wait(conds[:]) # TODO
278
279 # Check which of the condition(s) matched and handle them.
280 if self.matched[0]:
281 self.handle_address_or_data(pins)
282 elif self.matched[1]:
283 self.handle_start(pins)
284 elif self.matched[2]:
285 self.handle_stop(pins)
2b716038 286 elif self.state == 'FIND ACK':
592f355b
UH
287 # Wait for a data/ack bit: SCL = rising.
288 self.get_ack(self.wait({0: 'r'}))