]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2c/pd.py
i2c: Emit per-bit annotations/packets and define annotation rows.
[libsigrokdecode.git] / decoders / i2c / pd.py
CommitLineData
0588ed70 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
0588ed70 3##
de038c47 4## Copyright (C) 2010-2014 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
ab4aa33c 31I²C packet:
f1428c4c
UH
32[<cmd>, <data>]
33
34<cmd> is one of:
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)
de038c47 44 - 'BITS' (<data>: list of data/address bits and their ss/es numbers)
f1428c4c
UH
45
46<data> is the data or address byte associated with the 'ADDRESS*' and 'DATA*'
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).
49For 'START', 'START REPEAT', 'STOP', 'ACK', and 'NACK' <data> is None.
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
677d597b 66class Decoder(srd.Decoder):
a2c2afd9 67 api_version = 1
67e847fd 68 id = 'i2c'
ab4aa33c 69 name = 'I²C'
9a12a6e7 70 longname = 'Inter-Integrated Circuit'
a465436e 71 desc = 'Two-wire, multi-master, serial bus.'
f39d2404
UH
72 license = 'gplv2+'
73 inputs = ['logic']
74 outputs = ['i2c']
bc5f5a43
BV
75 probes = [
76 {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'},
77 {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'},
78 ]
b77614bc 79 optional_probes = []
f39d2404 80 options = {
d94ff143 81 'address_format': ['Displayed slave address format', 'shifted'],
ad2dc0de 82 }
e97b6ef5 83 annotations = [
fbd226c3
BV
84 ['start', 'Start condition'],
85 ['repeat-start', 'Repeat start condition'],
86 ['stop', 'Stop condition'],
87 ['ack', 'ACK'],
88 ['nack', 'NACK'],
de038c47 89 ['bit', 'Data/address bit'],
fbd226c3
BV
90 ['address-read', 'Address read'],
91 ['address-write', 'Address write'],
92 ['data-read', 'Data read'],
93 ['data-write', 'Data write'],
94 ['warnings', 'Human-readable warnings'],
15969949 95 ]
de038c47
UH
96 annotation_rows = (
97 ('bits', 'Bits', (5,)),
98 ('addr-data', 'Address/Data', (0, 1, 2, 3, 4, 6, 7, 8, 9)),
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
3643fc3f 108 def __init__(self, **kwargs):
8d2a9636 109 self.samplerate = None
de038c47 110 self.ss = self.es = self.byte_ss = -1
c4975078 111 self.samplenum = None
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'
de038c47 117 self.oldscl = self.oldsda = 1
d94ff143 118 self.oldpins = [1, 1]
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
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
7b86f0bc 143 def is_start_condition(self, scl, sda):
eb7082c9 144 # START condition (S): SDA = falling, SCL = high
7b86f0bc
UH
145 if (self.oldsda == 1 and sda == 0) and scl == 1:
146 return True
147 return False
148
149 def is_data_bit(self, scl, sda):
eb7082c9 150 # Data sampling of receiver: SCL = rising
7b86f0bc
UH
151 if self.oldscl == 0 and scl == 1:
152 return True
153 return False
154
155 def is_stop_condition(self, scl, sda):
eb7082c9 156 # STOP condition (P): SDA = rising, SCL = high
7b86f0bc
UH
157 if (self.oldsda == 0 and sda == 1) and scl == 1:
158 return True
159 return False
160
e5080882 161 def found_start(self, scl, sda):
de038c47 162 self.ss, self.es = self.samplenum, self.samplenum
8d2a9636
BV
163 self.pdu_start = self.samplenum
164 self.pdu_bits = 0
c4975078 165 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START'
d94ff143
UH
166 self.putp([cmd, None])
167 self.putx([proto[cmd][0], proto[cmd][1:]])
2b716038 168 self.state = 'FIND ADDRESS'
7b86f0bc 169 self.bitcount = self.databyte = 0
5dd9af5b 170 self.is_repeat_start = 1
7b86f0bc 171 self.wr = -1
de038c47 172 self.bits = []
7b86f0bc 173
c4975078 174 # Gather 8 bits of data plus the ACK/NACK bit.
e5080882 175 def found_address_or_data(self, scl, sda):
7b86f0bc
UH
176 # Address and data are transmitted MSB-first.
177 self.databyte <<= 1
178 self.databyte |= sda
179
de038c47 180 # Remember the start of the first data/address bit.
c4975078 181 if self.bitcount == 0:
de038c47
UH
182 self.byte_ss = self.samplenum
183
184 # Store individual bits and their start/end samplenumbers.
185 # In the list, index 0 represents the LSB (I²C transmits MSB-first).
186 self.bits.insert(0, [sda, self.samplenum, self.samplenum])
187 if self.bitcount > 0:
188 self.bits[1][2] = self.samplenum
189 if self.bitcount == 7:
190 self.bitwidth = self.bits[1][2] - self.bits[2][2]
191 self.bits[0][2] += self.bitwidth
c4975078 192
7b86f0bc 193 # Return if we haven't collected all 8 + 1 bits, yet.
de038c47
UH
194 if self.bitcount < 7:
195 self.bitcount += 1
eb7082c9 196 return
7b86f0bc 197
d94ff143 198 d = self.databyte
2b716038 199 if self.state == 'FIND ADDRESS':
7b86f0bc 200 # The READ/WRITE bit is only in address bytes, not data bytes.
bf1c3f4d 201 self.wr = 0 if (self.databyte & 1) else 1
d94ff143
UH
202 if self.options['address_format'] == 'shifted':
203 d = d >> 1
15969949 204
a929afa6 205 bin_class = -1
2b716038 206 if self.state == 'FIND ADDRESS' and self.wr == 1:
a2d2aff2 207 cmd = 'ADDRESS WRITE'
a929afa6 208 bin_class = 1
2b716038 209 elif self.state == 'FIND ADDRESS' and self.wr == 0:
a2d2aff2 210 cmd = 'ADDRESS READ'
a929afa6 211 bin_class = 0
2b716038 212 elif self.state == 'FIND DATA' and self.wr == 1:
a2d2aff2 213 cmd = 'DATA WRITE'
a929afa6 214 bin_class = 3
2b716038 215 elif self.state == 'FIND DATA' and self.wr == 0:
a2d2aff2 216 cmd = 'DATA READ'
a929afa6 217 bin_class = 2
eb7082c9 218
de038c47
UH
219 self.ss, self.es = self.byte_ss, self.samplenum + self.bitwidth
220
221 self.putp(['BITS', self.bits])
d94ff143 222 self.putp([cmd, d])
de038c47 223
a929afa6 224 self.putb((bin_class, bytes([d])))
7b86f0bc 225
de038c47
UH
226 for bit in self.bits:
227 self.put(bit[1], bit[2], self.out_ann, [5, ['%d' % bit[0]]])
228
229 if cmd.startswith('ADDRESS'):
230 self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth
231 w = ['Write', 'Wr', 'W'] if self.wr else ['Read', 'Rd', 'R']
232 self.putx([proto[cmd][0], w])
233 self.ss, self.es = self.byte_ss, self.samplenum
234
235 self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d),
236 '%s: %02X' % (proto[cmd][2], d), '%02X' % d]])
237
1b75abfd 238 # Done with this packet.
1b75abfd 239 self.bitcount = self.databyte = 0
de038c47 240 self.bits = []
2b716038 241 self.state = 'FIND ACK'
7b86f0bc 242
1b75abfd 243 def get_ack(self, scl, sda):
de038c47 244 self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth
d94ff143
UH
245 cmd = 'NACK' if (sda == 1) else 'ACK'
246 self.putp([cmd, None])
247 self.putx([proto[cmd][0], proto[cmd][1:]])
1b75abfd
BV
248 # There could be multiple data bytes in a row, so either find
249 # another data byte or a STOP condition next.
2b716038 250 self.state = 'FIND DATA'
7b86f0bc 251
e5080882 252 def found_stop(self, scl, sda):
8d2a9636
BV
253 # Meta bitrate
254 elapsed = 1 / float(self.samplerate) * (self.samplenum - self.pdu_start + 1)
255 bitrate = int(1 / elapsed * self.pdu_bits)
de038c47 256 self.put(self.byte_ss, self.samplenum, self.out_bitrate, bitrate)
8d2a9636 257
d94ff143 258 cmd = 'STOP'
de038c47 259 self.ss, self.es = self.samplenum, self.samplenum
d94ff143
UH
260 self.putp([cmd, None])
261 self.putx([proto[cmd][0], proto[cmd][1:]])
2b716038 262 self.state = 'FIND START'
5dd9af5b 263 self.is_repeat_start = 0
7b86f0bc 264 self.wr = -1
de038c47 265 self.bits = []
7b86f0bc 266
2b9837d9 267 def decode(self, ss, es, data):
8d2a9636
BV
268 if self.samplerate is None:
269 raise Exception("Cannot decode without samplerate.")
2fcd7c22
UH
270 for (self.samplenum, pins) in data:
271
272 # Ignore identical samples early on (for performance reasons).
273 if self.oldpins == pins:
274 continue
275 self.oldpins, (scl, sda) = pins, pins
f39d2404 276
8d2a9636
BV
277 self.pdu_bits += 1
278
7b86f0bc 279 # State machine.
2b716038 280 if self.state == 'FIND START':
7b86f0bc 281 if self.is_start_condition(scl, sda):
e5080882 282 self.found_start(scl, sda)
2b716038 283 elif self.state == 'FIND ADDRESS':
7b86f0bc 284 if self.is_data_bit(scl, sda):
e5080882 285 self.found_address_or_data(scl, sda)
2b716038 286 elif self.state == 'FIND DATA':
7b86f0bc 287 if self.is_data_bit(scl, sda):
e5080882 288 self.found_address_or_data(scl, sda)
7b86f0bc 289 elif self.is_start_condition(scl, sda):
e5080882 290 self.found_start(scl, sda)
7b86f0bc 291 elif self.is_stop_condition(scl, sda):
e5080882 292 self.found_stop(scl, sda)
2b716038 293 elif self.state == 'FIND ACK':
1b75abfd
BV
294 if self.is_data_bit(scl, sda):
295 self.get_ack(scl, sda)
7b86f0bc 296 else:
0eeeb544 297 raise Exception('Invalid state: %s' % self.state)
f39d2404
UH
298
299 # Save current SDA/SCL values for the next round.
de038c47 300 self.oldscl, self.oldsda = scl, sda
f39d2404 301