]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2c/pd.py
Implement OUTPUT_BINARY
[libsigrokdecode.git] / decoders / i2c / pd.py
CommitLineData
0588ed70 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
0588ed70 3##
d94ff143 4## Copyright (C) 2010-2013 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# I2C protocol decoder
0588ed70
UH
22
23# TODO: Look into arbitration, collision detection, clock synchronisation, etc.
9e95e4d8 24# TODO: Implement support for 10bit slave addresses.
0588ed70
UH
25# TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0).
26# TODO: Implement support for detecting various bus errors.
23fb2e12 27
677d597b 28import sigrokdecode as srd
b2c19614 29
f1428c4c
UH
30'''
31Protocol output format:
32
33I2C packet:
34[<cmd>, <data>]
35
36<cmd> is one of:
37 - 'START' (START condition)
38 - 'START REPEAT' (Repeated START condition)
39 - 'ADDRESS READ' (Slave address, read)
40 - 'ADDRESS WRITE' (Slave address, write)
41 - 'DATA READ' (Data, read)
42 - 'DATA WRITE' (Data, write)
43 - 'STOP' (STOP condition)
44 - 'ACK' (ACK bit)
45 - 'NACK' (NACK bit)
46
47<data> is the data or address byte associated with the 'ADDRESS*' and 'DATA*'
48command. Slave addresses do not include bit 0 (the READ/WRITE indication bit).
49For example, a slave address field could be 0x51 (instead of 0xa2).
50For 'START', 'START REPEAT', 'STOP', 'ACK', and 'NACK' <data> is None.
51'''
52
d94ff143 53# CMD: [annotation-type-index, long annotation, short annotation]
1541976f 54proto = {
d94ff143
UH
55 'START': [0, 'Start', 'S'],
56 'START REPEAT': [1, 'Start repeat', 'Sr'],
57 'STOP': [2, 'Stop', 'P'],
58 'ACK': [3, 'ACK', 'A'],
59 'NACK': [4, 'NACK', 'N'],
60 'ADDRESS READ': [5, 'Address read', 'AR'],
61 'ADDRESS WRITE': [6, 'Address write', 'AW'],
62 'DATA READ': [7, 'Data read', 'DR'],
63 'DATA WRITE': [8, 'Data write', 'DW'],
15969949 64}
e5080882 65
677d597b 66class Decoder(srd.Decoder):
a2c2afd9 67 api_version = 1
67e847fd 68 id = 'i2c'
f39d2404 69 name = 'I2C'
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 = [
d94ff143
UH
84 ['Start', 'Start condition'],
85 ['Repeat start', 'Repeat start condition'],
86 ['Stop', 'Stop condition'],
87 ['ACK', 'ACK'],
88 ['NACK', 'NACK'],
89 ['Address read', 'Address read'],
90 ['Address write', 'Address write'],
91 ['Data read', 'Data read'],
92 ['Data write', 'Data write'],
93 ['Warnings', 'Human-readable warnings'],
15969949 94 ]
0588ed70 95
3643fc3f 96 def __init__(self, **kwargs):
8d2a9636 97 self.samplerate = None
c4975078
BV
98 self.startsample = -1
99 self.samplenum = None
f39d2404
UH
100 self.bitcount = 0
101 self.databyte = 0
102 self.wr = -1
5dd9af5b 103 self.is_repeat_start = 0
2b716038 104 self.state = 'FIND START'
95097f31
UH
105 self.oldscl = 1
106 self.oldsda = 1
d94ff143 107 self.oldpins = [1, 1]
8d2a9636
BV
108 self.pdu_start = None
109 self.pdu_bits = 0
110
111 def metadata(self, key, value):
112 if key == srd.SRD_CONF_SAMPLERATE:
113 self.samplerate = value
f39d2404 114
8915b346 115 def start(self):
8d2a9636
BV
116 self.out_proto = self.register(srd.OUTPUT_PYTHON)
117 self.out_ann = self.register(srd.OUTPUT_ANN)
118 self.out_bitrate = self.register(srd.OUTPUT_META,
119 meta=(int, 'Bitrate', 'Bitrate from Start bit to Stop bit'))
3643fc3f 120
d94ff143
UH
121 def putx(self, data):
122 self.put(self.startsample, self.samplenum, self.out_ann, data)
123
124 def putp(self, data):
125 self.put(self.startsample, self.samplenum, self.out_proto, data)
126
7b86f0bc 127 def is_start_condition(self, scl, sda):
eb7082c9 128 # START condition (S): SDA = falling, SCL = high
7b86f0bc
UH
129 if (self.oldsda == 1 and sda == 0) and scl == 1:
130 return True
131 return False
132
133 def is_data_bit(self, scl, sda):
eb7082c9 134 # Data sampling of receiver: SCL = rising
7b86f0bc
UH
135 if self.oldscl == 0 and scl == 1:
136 return True
137 return False
138
139 def is_stop_condition(self, scl, sda):
eb7082c9 140 # STOP condition (P): SDA = rising, SCL = high
7b86f0bc
UH
141 if (self.oldsda == 0 and sda == 1) and scl == 1:
142 return True
143 return False
144
e5080882 145 def found_start(self, scl, sda):
c4975078 146 self.startsample = self.samplenum
8d2a9636
BV
147 self.pdu_start = self.samplenum
148 self.pdu_bits = 0
c4975078 149 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START'
d94ff143
UH
150 self.putp([cmd, None])
151 self.putx([proto[cmd][0], proto[cmd][1:]])
2b716038 152 self.state = 'FIND ADDRESS'
7b86f0bc 153 self.bitcount = self.databyte = 0
5dd9af5b 154 self.is_repeat_start = 1
7b86f0bc 155 self.wr = -1
7b86f0bc 156
c4975078 157 # Gather 8 bits of data plus the ACK/NACK bit.
e5080882 158 def found_address_or_data(self, scl, sda):
7b86f0bc
UH
159 # Address and data are transmitted MSB-first.
160 self.databyte <<= 1
161 self.databyte |= sda
162
c4975078
BV
163 if self.bitcount == 0:
164 self.startsample = self.samplenum
165
7b86f0bc 166 # Return if we haven't collected all 8 + 1 bits, yet.
c4975078 167 self.bitcount += 1
1b75abfd 168 if self.bitcount != 8:
eb7082c9 169 return
7b86f0bc 170
1b75abfd
BV
171 # We triggered on the ACK/NACK bit, but won't report that until later.
172 self.startsample -= 1
173
d94ff143 174 d = self.databyte
2b716038 175 if self.state == 'FIND ADDRESS':
7b86f0bc 176 # The READ/WRITE bit is only in address bytes, not data bytes.
bf1c3f4d 177 self.wr = 0 if (self.databyte & 1) else 1
d94ff143
UH
178 if self.options['address_format'] == 'shifted':
179 d = d >> 1
15969949 180
2b716038 181 if self.state == 'FIND ADDRESS' and self.wr == 1:
a2d2aff2 182 cmd = 'ADDRESS WRITE'
2b716038 183 elif self.state == 'FIND ADDRESS' and self.wr == 0:
a2d2aff2 184 cmd = 'ADDRESS READ'
2b716038 185 elif self.state == 'FIND DATA' and self.wr == 1:
a2d2aff2 186 cmd = 'DATA WRITE'
2b716038 187 elif self.state == 'FIND DATA' and self.wr == 0:
a2d2aff2 188 cmd = 'DATA READ'
eb7082c9 189
d94ff143
UH
190 self.putp([cmd, d])
191 self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d),
192 '%s: %02X' % (proto[cmd][2], d), '%02X' % d]])
7b86f0bc 193
1b75abfd 194 # Done with this packet.
7b86f0bc 195 self.startsample = -1
1b75abfd 196 self.bitcount = self.databyte = 0
2b716038 197 self.state = 'FIND ACK'
7b86f0bc 198
1b75abfd
BV
199 def get_ack(self, scl, sda):
200 self.startsample = self.samplenum
d94ff143
UH
201 cmd = 'NACK' if (sda == 1) else 'ACK'
202 self.putp([cmd, None])
203 self.putx([proto[cmd][0], proto[cmd][1:]])
1b75abfd
BV
204 # There could be multiple data bytes in a row, so either find
205 # another data byte or a STOP condition next.
2b716038 206 self.state = 'FIND DATA'
7b86f0bc 207
e5080882 208 def found_stop(self, scl, sda):
8d2a9636
BV
209 # Meta bitrate
210 elapsed = 1 / float(self.samplerate) * (self.samplenum - self.pdu_start + 1)
211 bitrate = int(1 / elapsed * self.pdu_bits)
212 self.put(self.startsample, self.samplenum, self.out_bitrate, bitrate)
213
c4975078 214 self.startsample = self.samplenum
d94ff143
UH
215 cmd = 'STOP'
216 self.putp([cmd, None])
217 self.putx([proto[cmd][0], proto[cmd][1:]])
2b716038 218 self.state = 'FIND START'
5dd9af5b 219 self.is_repeat_start = 0
7b86f0bc
UH
220 self.wr = -1
221
2b9837d9 222 def decode(self, ss, es, data):
8d2a9636
BV
223 if self.samplerate is None:
224 raise Exception("Cannot decode without samplerate.")
2fcd7c22
UH
225 for (self.samplenum, pins) in data:
226
227 # Ignore identical samples early on (for performance reasons).
228 if self.oldpins == pins:
229 continue
230 self.oldpins, (scl, sda) = pins, pins
f39d2404 231
8d2a9636
BV
232 self.pdu_bits += 1
233
f39d2404
UH
234 # TODO: Wait until the bus is idle (SDA = SCL = 1) first?
235
7b86f0bc 236 # State machine.
2b716038 237 if self.state == 'FIND START':
7b86f0bc 238 if self.is_start_condition(scl, sda):
e5080882 239 self.found_start(scl, sda)
2b716038 240 elif self.state == 'FIND ADDRESS':
7b86f0bc 241 if self.is_data_bit(scl, sda):
e5080882 242 self.found_address_or_data(scl, sda)
2b716038 243 elif self.state == 'FIND DATA':
7b86f0bc 244 if self.is_data_bit(scl, sda):
e5080882 245 self.found_address_or_data(scl, sda)
7b86f0bc 246 elif self.is_start_condition(scl, sda):
e5080882 247 self.found_start(scl, sda)
7b86f0bc 248 elif self.is_stop_condition(scl, sda):
e5080882 249 self.found_stop(scl, sda)
2b716038 250 elif self.state == 'FIND ACK':
1b75abfd
BV
251 if self.is_data_bit(scl, sda):
252 self.get_ack(scl, sda)
7b86f0bc 253 else:
0eeeb544 254 raise Exception('Invalid state: %s' % self.state)
f39d2404
UH
255
256 # Save current SDA/SCL values for the next round.
257 self.oldscl = scl
258 self.oldsda = sda
259