]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2c/i2c.py
srd: I2C: change format to have ACK/NACK bits as separate events
[libsigrokdecode.git] / decoders / i2c / i2c.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.
15969949 41protocol = {
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
400f9ae7
UH
53# States
54FIND_START = 0
55FIND_ADDRESS = 1
56FIND_DATA = 2
1b75abfd 57FIND_ACK = 3
400f9ae7 58
677d597b 59class Decoder(srd.Decoder):
a2c2afd9 60 api_version = 1
67e847fd 61 id = 'i2c'
f39d2404 62 name = 'I2C'
9a12a6e7 63 longname = 'Inter-Integrated Circuit'
f39d2404 64 desc = 'I2C is a two-wire, multi-master, serial bus.'
f39d2404
UH
65 license = 'gplv2+'
66 inputs = ['logic']
67 outputs = ['i2c']
bc5f5a43
BV
68 probes = [
69 {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'},
70 {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'},
71 ]
b77614bc 72 optional_probes = []
f39d2404 73 options = {
ea90233e 74 'addressing': ['Slave addressing (in bits)', 7], # 7 or 10
ad2dc0de 75 }
e97b6ef5 76 annotations = [
15969949 77 # ANN_SHIFTED
eb7082c9
UH
78 ['7-bit shifted hex',
79 'Read/write bit shifted out from the 8-bit I2C slave address'],
15969949 80 # ANN_SHIFTED_SHORT
eb7082c9
UH
81 ['7-bit shifted hex (short)',
82 'Read/write bit shifted out from the 8-bit I2C slave address'],
15969949 83 # ANN_RAW
eb7082c9 84 ['Raw hex', 'Unaltered raw data'],
15969949 85 ]
0588ed70 86
3643fc3f 87 def __init__(self, **kwargs):
c4975078
BV
88 self.startsample = -1
89 self.samplenum = None
f39d2404
UH
90 self.bitcount = 0
91 self.databyte = 0
92 self.wr = -1
5dd9af5b 93 self.is_repeat_start = 0
400f9ae7 94 self.state = FIND_START
f39d2404
UH
95 self.oldscl = None
96 self.oldsda = None
97
3643fc3f 98 def start(self, metadata):
56202222
UH
99 self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2c')
100 self.out_ann = self.add(srd.OUTPUT_ANN, 'i2c')
3643fc3f 101
decde15e
UH
102 def report(self):
103 pass
104
7b86f0bc 105 def is_start_condition(self, scl, sda):
eb7082c9 106 # START condition (S): SDA = falling, SCL = high
7b86f0bc
UH
107 if (self.oldsda == 1 and sda == 0) and scl == 1:
108 return True
109 return False
110
111 def is_data_bit(self, scl, sda):
eb7082c9 112 # Data sampling of receiver: SCL = rising
7b86f0bc
UH
113 if self.oldscl == 0 and scl == 1:
114 return True
115 return False
116
117 def is_stop_condition(self, scl, sda):
eb7082c9 118 # STOP condition (P): SDA = rising, SCL = high
7b86f0bc
UH
119 if (self.oldsda == 0 and sda == 1) and scl == 1:
120 return True
121 return False
122
e5080882 123 def found_start(self, scl, sda):
c4975078 124 self.startsample = self.samplenum
eb7082c9 125
c4975078 126 cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START'
1b75abfd 127 self.put(self.out_proto, [cmd, None])
eb7082c9
UH
128 self.put(self.out_ann, [ANN_SHIFTED, [protocol[cmd][0]]])
129 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol[cmd][1]]])
e5080882 130
400f9ae7 131 self.state = FIND_ADDRESS
7b86f0bc 132 self.bitcount = self.databyte = 0
5dd9af5b 133 self.is_repeat_start = 1
7b86f0bc 134 self.wr = -1
7b86f0bc 135
c4975078 136 # Gather 8 bits of data plus the ACK/NACK bit.
e5080882 137 def found_address_or_data(self, scl, sda):
7b86f0bc
UH
138 # Address and data are transmitted MSB-first.
139 self.databyte <<= 1
140 self.databyte |= sda
141
c4975078
BV
142 if self.bitcount == 0:
143 self.startsample = self.samplenum
144
7b86f0bc 145 # Return if we haven't collected all 8 + 1 bits, yet.
c4975078 146 self.bitcount += 1
1b75abfd 147 if self.bitcount != 8:
eb7082c9 148 return
7b86f0bc 149
1b75abfd
BV
150 # We triggered on the ACK/NACK bit, but won't report that until later.
151 self.startsample -= 1
152
eb7082c9 153 # Send raw output annotation before we start shifting out
1b75abfd 154 # read/write and ACK/NACK bits.
eb7082c9 155 self.put(self.out_ann, [ANN_RAW, ['0x%.2x' % self.databyte]])
15969949 156
400f9ae7 157 if self.state == FIND_ADDRESS:
7b86f0bc 158 # The READ/WRITE bit is only in address bytes, not data bytes.
bf1c3f4d 159 self.wr = 0 if (self.databyte & 1) else 1
84b81f1d 160 d = self.databyte >> 1
400f9ae7 161 elif self.state == FIND_DATA:
7b86f0bc 162 d = self.databyte
15969949 163
400f9ae7 164 if self.state == FIND_ADDRESS and self.wr == 1:
a2d2aff2 165 cmd = 'ADDRESS WRITE'
400f9ae7 166 elif self.state == FIND_ADDRESS and self.wr == 0:
a2d2aff2 167 cmd = 'ADDRESS READ'
400f9ae7 168 elif self.state == FIND_DATA and self.wr == 1:
a2d2aff2 169 cmd = 'DATA WRITE'
400f9ae7 170 elif self.state == FIND_DATA and self.wr == 0:
a2d2aff2 171 cmd = 'DATA READ'
eb7082c9 172
1b75abfd
BV
173 self.put(self.out_proto, [cmd, d])
174 self.put(self.out_ann, [ANN_SHIFTED, [protocol[cmd][0], '0x%02x' % d]])
175 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol[cmd][1], '0x%02x' % d]])
7b86f0bc 176
1b75abfd 177 # Done with this packet.
7b86f0bc 178 self.startsample = -1
1b75abfd
BV
179 self.bitcount = self.databyte = 0
180 self.state = FIND_ACK
7b86f0bc 181
1b75abfd
BV
182 def get_ack(self, scl, sda):
183 self.startsample = self.samplenum
184 ack_bit = 'NACK' if (sda == 1) else 'ACK'
185 self.put(self.out_proto, [ack_bit, None])
186 self.put(self.out_ann, [ANN_SHIFTED, [protocol[ack_bit][0]]])
187 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol[ack_bit][1]]])
188 # There could be multiple data bytes in a row, so either find
189 # another data byte or a STOP condition next.
190 self.state = FIND_DATA
7b86f0bc 191
e5080882 192 def found_stop(self, scl, sda):
c4975078 193 self.startsample = self.samplenum
1b75abfd 194 self.put(self.out_proto, ['STOP', None])
eb7082c9
UH
195 self.put(self.out_ann, [ANN_SHIFTED, [protocol['STOP'][0]]])
196 self.put(self.out_ann, [ANN_SHIFTED_SHORT, [protocol['STOP'][1]]])
7b86f0bc 197
400f9ae7 198 self.state = FIND_START
5dd9af5b 199 self.is_repeat_start = 0
7b86f0bc
UH
200 self.wr = -1
201
1aef2f93 202 def put(self, output_id, data):
eb7082c9 203 # Inject sample range into the call up to sigrok.
c4975078 204 super(Decoder, self).put(self.startsample, self.samplenum, output_id, data)
1aef2f93 205
2b9837d9 206 def decode(self, ss, es, data):
bc5f5a43 207 for samplenum, (scl, sda) in data:
c4975078 208 self.samplenum = samplenum
f39d2404
UH
209
210 # First sample: Save SCL/SDA value.
211 if self.oldscl == None:
bc5f5a43
BV
212 self.oldscl = scl
213 self.oldsda = sda
ad2dc0de 214 continue
0588ed70 215
f39d2404
UH
216 # TODO: Wait until the bus is idle (SDA = SCL = 1) first?
217
7b86f0bc 218 # State machine.
400f9ae7 219 if self.state == FIND_START:
7b86f0bc 220 if self.is_start_condition(scl, sda):
e5080882 221 self.found_start(scl, sda)
400f9ae7 222 elif self.state == FIND_ADDRESS:
7b86f0bc 223 if self.is_data_bit(scl, sda):
e5080882 224 self.found_address_or_data(scl, sda)
400f9ae7 225 elif self.state == FIND_DATA:
7b86f0bc 226 if self.is_data_bit(scl, sda):
e5080882 227 self.found_address_or_data(scl, sda)
7b86f0bc 228 elif self.is_start_condition(scl, sda):
e5080882 229 self.found_start(scl, sda)
7b86f0bc 230 elif self.is_stop_condition(scl, sda):
e5080882 231 self.found_stop(scl, sda)
1b75abfd
BV
232 elif self.state == FIND_ACK:
233 if self.is_data_bit(scl, sda):
234 self.get_ack(scl, sda)
7b86f0bc 235 else:
decde15e 236 raise Exception('Invalid state %d' % self.STATE)
f39d2404
UH
237
238 # Save current SDA/SCL values for the next round.
239 self.oldscl = scl
240 self.oldsda = sda
241