]> sigrok.org Git - libsigrokdecode.git/blame_incremental - decoders/i2c/pd.py
Emit meta bitrate
[libsigrokdecode.git] / decoders / i2c / pd.py
... / ...
CommitLineData
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2010-2013 Uwe Hermann <uwe@hermann-uwe.de>
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
21# I2C protocol decoder
22
23# TODO: Look into arbitration, collision detection, clock synchronisation, etc.
24# TODO: Implement support for 10bit slave addresses.
25# TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0).
26# TODO: Implement support for detecting various bus errors.
27
28import sigrokdecode as srd
29
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
53# CMD: [annotation-type-index, long annotation, short annotation]
54proto = {
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'],
64}
65
66class Decoder(srd.Decoder):
67 api_version = 1
68 id = 'i2c'
69 name = 'I2C'
70 longname = 'Inter-Integrated Circuit'
71 desc = 'Two-wire, multi-master, serial bus.'
72 license = 'gplv2+'
73 inputs = ['logic']
74 outputs = ['i2c']
75 probes = [
76 {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'},
77 {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'},
78 ]
79 optional_probes = []
80 options = {
81 'address_format': ['Displayed slave address format', 'shifted'],
82 }
83 annotations = [
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'],
94 ]
95
96 def __init__(self, **kwargs):
97 self.samplerate = None
98 self.startsample = -1
99 self.samplenum = None
100 self.bitcount = 0
101 self.databyte = 0
102 self.wr = -1
103 self.is_repeat_start = 0
104 self.state = 'FIND START'
105 self.oldscl = 1
106 self.oldsda = 1
107 self.oldpins = [1, 1]
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
114
115 def start(self):
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'))
120
121 def report(self):
122 pass
123
124 def putx(self, data):
125 self.put(self.startsample, self.samplenum, self.out_ann, data)
126
127 def putp(self, data):
128 self.put(self.startsample, self.samplenum, self.out_proto, data)
129
130 def is_start_condition(self, scl, sda):
131 # START condition (S): SDA = falling, SCL = high
132 if (self.oldsda == 1 and sda == 0) and scl == 1:
133 return True
134 return False
135
136 def is_data_bit(self, scl, sda):
137 # Data sampling of receiver: SCL = rising
138 if self.oldscl == 0 and scl == 1:
139 return True
140 return False
141
142 def is_stop_condition(self, scl, sda):
143 # STOP condition (P): SDA = rising, SCL = high
144 if (self.oldsda == 0 and sda == 1) and scl == 1:
145 return True
146 return False
147
148 def found_start(self, scl, sda):
149 self.startsample = self.samplenum
150 self.pdu_start = self.samplenum
151 self.pdu_bits = 0
152 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START'
153 self.putp([cmd, None])
154 self.putx([proto[cmd][0], proto[cmd][1:]])
155 self.state = 'FIND ADDRESS'
156 self.bitcount = self.databyte = 0
157 self.is_repeat_start = 1
158 self.wr = -1
159
160 # Gather 8 bits of data plus the ACK/NACK bit.
161 def found_address_or_data(self, scl, sda):
162 # Address and data are transmitted MSB-first.
163 self.databyte <<= 1
164 self.databyte |= sda
165
166 if self.bitcount == 0:
167 self.startsample = self.samplenum
168
169 # Return if we haven't collected all 8 + 1 bits, yet.
170 self.bitcount += 1
171 if self.bitcount != 8:
172 return
173
174 # We triggered on the ACK/NACK bit, but won't report that until later.
175 self.startsample -= 1
176
177 d = self.databyte
178 if self.state == 'FIND ADDRESS':
179 # The READ/WRITE bit is only in address bytes, not data bytes.
180 self.wr = 0 if (self.databyte & 1) else 1
181 if self.options['address_format'] == 'shifted':
182 d = d >> 1
183
184 if self.state == 'FIND ADDRESS' and self.wr == 1:
185 cmd = 'ADDRESS WRITE'
186 elif self.state == 'FIND ADDRESS' and self.wr == 0:
187 cmd = 'ADDRESS READ'
188 elif self.state == 'FIND DATA' and self.wr == 1:
189 cmd = 'DATA WRITE'
190 elif self.state == 'FIND DATA' and self.wr == 0:
191 cmd = 'DATA READ'
192
193 self.putp([cmd, d])
194 self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d),
195 '%s: %02X' % (proto[cmd][2], d), '%02X' % d]])
196
197 # Done with this packet.
198 self.startsample = -1
199 self.bitcount = self.databyte = 0
200 self.state = 'FIND ACK'
201
202 def get_ack(self, scl, sda):
203 self.startsample = self.samplenum
204 cmd = 'NACK' if (sda == 1) else 'ACK'
205 self.putp([cmd, None])
206 self.putx([proto[cmd][0], proto[cmd][1:]])
207 # There could be multiple data bytes in a row, so either find
208 # another data byte or a STOP condition next.
209 self.state = 'FIND DATA'
210
211 def found_stop(self, scl, sda):
212 # Meta bitrate
213 elapsed = 1 / float(self.samplerate) * (self.samplenum - self.pdu_start + 1)
214 bitrate = int(1 / elapsed * self.pdu_bits)
215 self.put(self.startsample, self.samplenum, self.out_bitrate, bitrate)
216
217 self.startsample = self.samplenum
218 cmd = 'STOP'
219 self.putp([cmd, None])
220 self.putx([proto[cmd][0], proto[cmd][1:]])
221 self.state = 'FIND START'
222 self.is_repeat_start = 0
223 self.wr = -1
224
225 def decode(self, ss, es, data):
226 if self.samplerate is None:
227 raise Exception("Cannot decode without samplerate.")
228 for (self.samplenum, pins) in data:
229
230 # Ignore identical samples early on (for performance reasons).
231 if self.oldpins == pins:
232 continue
233 self.oldpins, (scl, sda) = pins, pins
234
235 self.pdu_bits += 1
236
237 # TODO: Wait until the bus is idle (SDA = SCL = 1) first?
238
239 # State machine.
240 if self.state == 'FIND START':
241 if self.is_start_condition(scl, sda):
242 self.found_start(scl, sda)
243 elif self.state == 'FIND ADDRESS':
244 if self.is_data_bit(scl, sda):
245 self.found_address_or_data(scl, sda)
246 elif self.state == 'FIND DATA':
247 if self.is_data_bit(scl, sda):
248 self.found_address_or_data(scl, sda)
249 elif self.is_start_condition(scl, sda):
250 self.found_start(scl, sda)
251 elif self.is_stop_condition(scl, sda):
252 self.found_stop(scl, sda)
253 elif self.state == 'FIND ACK':
254 if self.is_data_bit(scl, sda):
255 self.get_ack(scl, sda)
256 else:
257 raise Exception('Invalid state: %s' % self.state)
258
259 # Save current SDA/SCL values for the next round.
260 self.oldscl = scl
261 self.oldsda = sda
262