]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2c/pd.py
All PDs: Name the files pd.py consistently.
[libsigrokdecode.git] / decoders / i2c / pd.py
CommitLineData
0588ed70
UH
1##
2## This file is part of the sigrok project.
3##
7b86f0bc 4## Copyright (C) 2010-2011 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.
24# TODO: Handle clock stretching.
25# TODO: Handle combined messages / repeated START.
26# TODO: Implement support for 7bit and 10bit slave addresses.
27# TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0).
28# TODO: Implement support for detecting various bus errors.
23fb2e12
UH
29# TODO: I2C address of slaves.
30# TODO: Handle multiple different I2C devices on same bus
31# -> we need to decode multiple protocols at the same time.
23fb2e12 32
677d597b 33import sigrokdecode as srd
b2c19614 34
eb7082c9 35# Annotation feed formats
e4f82268 36ANN_SHIFTED = 0
7ce7775c 37ANN_SHIFTED_SHORT = 1
e4f82268 38ANN_RAW = 2
7ce7775c 39
eb7082c9 40# Values are verbose and short annotation, respectively.
1541976f 41proto = {
eb7082c9 42 'START': ['START', 'S'],
a2d2aff2 43 'START REPEAT': ['START REPEAT', 'Sr'],
eb7082c9
UH
44 'STOP': ['STOP', 'P'],
45 'ACK': ['ACK', 'A'],
46 'NACK': ['NACK', 'N'],
a2d2aff2
UH
47 'ADDRESS READ': ['ADDRESS READ', 'AR'],
48 'ADDRESS WRITE': ['ADDRESS WRITE', 'AW'],
49 'DATA READ': ['DATA READ', 'DR'],
50 'DATA WRITE': ['DATA WRITE', 'DW'],
15969949 51}
e5080882 52
677d597b 53class Decoder(srd.Decoder):
a2c2afd9 54 api_version = 1
67e847fd 55 id = 'i2c'
f39d2404 56 name = 'I2C'
9a12a6e7 57 longname = 'Inter-Integrated Circuit'
a465436e 58 desc = 'Two-wire, multi-master, serial bus.'
f39d2404
UH
59 license = 'gplv2+'
60 inputs = ['logic']
61 outputs = ['i2c']
bc5f5a43
BV
62 probes = [
63 {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'},
64 {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'},
65 ]
b77614bc 66 optional_probes = []
f39d2404 67 options = {
ea90233e 68 'addressing': ['Slave addressing (in bits)', 7], # 7 or 10
ad2dc0de 69 }
e97b6ef5 70 annotations = [
15969949 71 # ANN_SHIFTED
eb7082c9
UH
72 ['7-bit shifted hex',
73 'Read/write bit shifted out from the 8-bit I2C slave address'],
15969949 74 # ANN_SHIFTED_SHORT
eb7082c9
UH
75 ['7-bit shifted hex (short)',
76 'Read/write bit shifted out from the 8-bit I2C slave address'],
15969949 77 # ANN_RAW
eb7082c9 78 ['Raw hex', 'Unaltered raw data'],
15969949 79 ]
0588ed70 80
3643fc3f 81 def __init__(self, **kwargs):
c4975078
BV
82 self.startsample = -1
83 self.samplenum = None
f39d2404
UH
84 self.bitcount = 0
85 self.databyte = 0
86 self.wr = -1
5dd9af5b 87 self.is_repeat_start = 0
2b716038 88 self.state = 'FIND START'
f39d2404
UH
89 self.oldscl = None
90 self.oldsda = None
2fcd7c22 91 self.oldpins = None
f39d2404 92
3643fc3f 93 def start(self, metadata):
56202222
UH
94 self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2c')
95 self.out_ann = self.add(srd.OUTPUT_ANN, 'i2c')
3643fc3f 96
decde15e
UH
97 def report(self):
98 pass
99
7b86f0bc 100 def is_start_condition(self, scl, sda):
eb7082c9 101 # START condition (S): SDA = falling, SCL = high
7b86f0bc
UH
102 if (self.oldsda == 1 and sda == 0) and scl == 1:
103 return True
104 return False
105
106 def is_data_bit(self, scl, sda):
eb7082c9 107 # Data sampling of receiver: SCL = rising
7b86f0bc
UH
108 if self.oldscl == 0 and scl == 1:
109 return True
110 return False
111
112 def is_stop_condition(self, scl, sda):
eb7082c9 113 # STOP condition (P): SDA = rising, SCL = high
7b86f0bc
UH
114 if (self.oldsda == 0 and sda == 1) and scl == 1:
115 return True
116 return False
117
e5080882 118 def found_start(self, scl, sda):
c4975078 119 self.startsample = self.samplenum
eb7082c9 120
c4975078 121 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START'
1b75abfd 122 self.put(self.out_proto, [cmd, None])
1541976f
UH
123 self.put(self.out_ann, [ANN_SHIFTED, [proto[cmd][0]]])
124 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [proto[cmd][1]]])
e5080882 125
2b716038 126 self.state = 'FIND ADDRESS'
7b86f0bc 127 self.bitcount = self.databyte = 0
5dd9af5b 128 self.is_repeat_start = 1
7b86f0bc 129 self.wr = -1
7b86f0bc 130
c4975078 131 # Gather 8 bits of data plus the ACK/NACK bit.
e5080882 132 def found_address_or_data(self, scl, sda):
7b86f0bc
UH
133 # Address and data are transmitted MSB-first.
134 self.databyte <<= 1
135 self.databyte |= sda
136
c4975078
BV
137 if self.bitcount == 0:
138 self.startsample = self.samplenum
139
7b86f0bc 140 # Return if we haven't collected all 8 + 1 bits, yet.
c4975078 141 self.bitcount += 1
1b75abfd 142 if self.bitcount != 8:
eb7082c9 143 return
7b86f0bc 144
1b75abfd
BV
145 # We triggered on the ACK/NACK bit, but won't report that until later.
146 self.startsample -= 1
147
eb7082c9 148 # Send raw output annotation before we start shifting out
1b75abfd 149 # read/write and ACK/NACK bits.
eb7082c9 150 self.put(self.out_ann, [ANN_RAW, ['0x%.2x' % self.databyte]])
15969949 151
2b716038 152 if self.state == 'FIND ADDRESS':
7b86f0bc 153 # The READ/WRITE bit is only in address bytes, not data bytes.
bf1c3f4d 154 self.wr = 0 if (self.databyte & 1) else 1
84b81f1d 155 d = self.databyte >> 1
2b716038 156 elif self.state == 'FIND DATA':
7b86f0bc 157 d = self.databyte
15969949 158
2b716038 159 if self.state == 'FIND ADDRESS' and self.wr == 1:
a2d2aff2 160 cmd = 'ADDRESS WRITE'
2b716038 161 elif self.state == 'FIND ADDRESS' and self.wr == 0:
a2d2aff2 162 cmd = 'ADDRESS READ'
2b716038 163 elif self.state == 'FIND DATA' and self.wr == 1:
a2d2aff2 164 cmd = 'DATA WRITE'
2b716038 165 elif self.state == 'FIND DATA' and self.wr == 0:
a2d2aff2 166 cmd = 'DATA READ'
eb7082c9 167
1b75abfd 168 self.put(self.out_proto, [cmd, d])
1541976f
UH
169 self.put(self.out_ann, [ANN_SHIFTED, [proto[cmd][0], '0x%02x' % d]])
170 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [proto[cmd][1], '0x%02x' % d]])
7b86f0bc 171
1b75abfd 172 # Done with this packet.
7b86f0bc 173 self.startsample = -1
1b75abfd 174 self.bitcount = self.databyte = 0
2b716038 175 self.state = 'FIND ACK'
7b86f0bc 176
1b75abfd
BV
177 def get_ack(self, scl, sda):
178 self.startsample = self.samplenum
179 ack_bit = 'NACK' if (sda == 1) else 'ACK'
180 self.put(self.out_proto, [ack_bit, None])
1541976f
UH
181 self.put(self.out_ann, [ANN_SHIFTED, [proto[ack_bit][0]]])
182 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [proto[ack_bit][1]]])
1b75abfd
BV
183 # There could be multiple data bytes in a row, so either find
184 # another data byte or a STOP condition next.
2b716038 185 self.state = 'FIND DATA'
7b86f0bc 186
e5080882 187 def found_stop(self, scl, sda):
c4975078 188 self.startsample = self.samplenum
1b75abfd 189 self.put(self.out_proto, ['STOP', None])
1541976f
UH
190 self.put(self.out_ann, [ANN_SHIFTED, [proto['STOP'][0]]])
191 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [proto['STOP'][1]]])
7b86f0bc 192
2b716038 193 self.state = 'FIND START'
5dd9af5b 194 self.is_repeat_start = 0
7b86f0bc
UH
195 self.wr = -1
196
1aef2f93 197 def put(self, output_id, data):
eb7082c9 198 # Inject sample range into the call up to sigrok.
c4975078 199 super(Decoder, self).put(self.startsample, self.samplenum, output_id, data)
1aef2f93 200
2b9837d9 201 def decode(self, ss, es, data):
2fcd7c22
UH
202 for (self.samplenum, pins) in data:
203
204 # Ignore identical samples early on (for performance reasons).
205 if self.oldpins == pins:
206 continue
207 self.oldpins, (scl, sda) = pins, pins
f39d2404
UH
208
209 # First sample: Save SCL/SDA value.
210 if self.oldscl == None:
bc5f5a43
BV
211 self.oldscl = scl
212 self.oldsda = sda
ad2dc0de 213 continue
0588ed70 214
f39d2404
UH
215 # TODO: Wait until the bus is idle (SDA = SCL = 1) first?
216
7b86f0bc 217 # State machine.
2b716038 218 if self.state == 'FIND START':
7b86f0bc 219 if self.is_start_condition(scl, sda):
e5080882 220 self.found_start(scl, sda)
2b716038 221 elif self.state == 'FIND ADDRESS':
7b86f0bc 222 if self.is_data_bit(scl, sda):
e5080882 223 self.found_address_or_data(scl, sda)
2b716038 224 elif self.state == 'FIND DATA':
7b86f0bc 225 if self.is_data_bit(scl, sda):
e5080882 226 self.found_address_or_data(scl, sda)
7b86f0bc 227 elif self.is_start_condition(scl, sda):
e5080882 228 self.found_start(scl, sda)
7b86f0bc 229 elif self.is_stop_condition(scl, sda):
e5080882 230 self.found_stop(scl, sda)
2b716038 231 elif self.state == 'FIND ACK':
1b75abfd
BV
232 if self.is_data_bit(scl, sda):
233 self.get_ack(scl, sda)
7b86f0bc 234 else:
decde15e 235 raise Exception('Invalid state %d' % self.STATE)
f39d2404
UH
236
237 # Save current SDA/SCL values for the next round.
238 self.oldscl = scl
239 self.oldsda = sda
240