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