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