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