]>
Commit | Line | Data |
---|---|---|
0588ed70 | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
0588ed70 | 3 | ## |
d94ff143 | 4 | ## Copyright (C) 2010-2013 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 | 33 | import sigrokdecode as srd |
b2c19614 | 34 | |
d94ff143 | 35 | # CMD: [annotation-type-index, long annotation, short annotation] |
1541976f | 36 | proto = { |
d94ff143 UH |
37 | 'START': [0, 'Start', 'S'], |
38 | 'START REPEAT': [1, 'Start repeat', 'Sr'], | |
39 | 'STOP': [2, 'Stop', 'P'], | |
40 | 'ACK': [3, 'ACK', 'A'], | |
41 | 'NACK': [4, 'NACK', 'N'], | |
42 | 'ADDRESS READ': [5, 'Address read', 'AR'], | |
43 | 'ADDRESS WRITE': [6, 'Address write', 'AW'], | |
44 | 'DATA READ': [7, 'Data read', 'DR'], | |
45 | 'DATA WRITE': [8, 'Data write', 'DW'], | |
15969949 | 46 | } |
e5080882 | 47 | |
677d597b | 48 | class Decoder(srd.Decoder): |
a2c2afd9 | 49 | api_version = 1 |
67e847fd | 50 | id = 'i2c' |
f39d2404 | 51 | name = 'I2C' |
9a12a6e7 | 52 | longname = 'Inter-Integrated Circuit' |
a465436e | 53 | desc = 'Two-wire, multi-master, serial bus.' |
f39d2404 UH |
54 | license = 'gplv2+' |
55 | inputs = ['logic'] | |
56 | outputs = ['i2c'] | |
bc5f5a43 BV |
57 | probes = [ |
58 | {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'}, | |
59 | {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'}, | |
60 | ] | |
b77614bc | 61 | optional_probes = [] |
f39d2404 | 62 | options = { |
ea90233e | 63 | 'addressing': ['Slave addressing (in bits)', 7], # 7 or 10 |
d94ff143 | 64 | 'address_format': ['Displayed slave address format', 'shifted'], |
ad2dc0de | 65 | } |
e97b6ef5 | 66 | annotations = [ |
d94ff143 UH |
67 | ['Start', 'Start condition'], |
68 | ['Repeat start', 'Repeat start condition'], | |
69 | ['Stop', 'Stop condition'], | |
70 | ['ACK', 'ACK'], | |
71 | ['NACK', 'NACK'], | |
72 | ['Address read', 'Address read'], | |
73 | ['Address write', 'Address write'], | |
74 | ['Data read', 'Data read'], | |
75 | ['Data write', 'Data write'], | |
76 | ['Warnings', 'Human-readable warnings'], | |
15969949 | 77 | ] |
0588ed70 | 78 | |
3643fc3f | 79 | def __init__(self, **kwargs): |
c4975078 BV |
80 | self.startsample = -1 |
81 | self.samplenum = None | |
f39d2404 UH |
82 | self.bitcount = 0 |
83 | self.databyte = 0 | |
84 | self.wr = -1 | |
5dd9af5b | 85 | self.is_repeat_start = 0 |
2b716038 | 86 | self.state = 'FIND START' |
95097f31 UH |
87 | self.oldscl = 1 |
88 | self.oldsda = 1 | |
d94ff143 | 89 | self.oldpins = [1, 1] |
f39d2404 | 90 | |
3643fc3f | 91 | def start(self, metadata): |
56202222 UH |
92 | self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2c') |
93 | self.out_ann = self.add(srd.OUTPUT_ANN, 'i2c') | |
3643fc3f | 94 | |
decde15e UH |
95 | def report(self): |
96 | pass | |
97 | ||
d94ff143 UH |
98 | def putx(self, data): |
99 | self.put(self.startsample, self.samplenum, self.out_ann, data) | |
100 | ||
101 | def putp(self, data): | |
102 | self.put(self.startsample, self.samplenum, self.out_proto, data) | |
103 | ||
7b86f0bc | 104 | def is_start_condition(self, scl, sda): |
eb7082c9 | 105 | # START condition (S): SDA = falling, SCL = high |
7b86f0bc UH |
106 | if (self.oldsda == 1 and sda == 0) and scl == 1: |
107 | return True | |
108 | return False | |
109 | ||
110 | def is_data_bit(self, scl, sda): | |
eb7082c9 | 111 | # Data sampling of receiver: SCL = rising |
7b86f0bc UH |
112 | if self.oldscl == 0 and scl == 1: |
113 | return True | |
114 | return False | |
115 | ||
116 | def is_stop_condition(self, scl, sda): | |
eb7082c9 | 117 | # STOP condition (P): SDA = rising, SCL = high |
7b86f0bc UH |
118 | if (self.oldsda == 0 and sda == 1) and scl == 1: |
119 | return True | |
120 | return False | |
121 | ||
e5080882 | 122 | def found_start(self, scl, sda): |
c4975078 | 123 | self.startsample = self.samplenum |
c4975078 | 124 | cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START' |
d94ff143 UH |
125 | self.putp([cmd, None]) |
126 | self.putx([proto[cmd][0], proto[cmd][1:]]) | |
2b716038 | 127 | self.state = 'FIND ADDRESS' |
7b86f0bc | 128 | self.bitcount = self.databyte = 0 |
5dd9af5b | 129 | self.is_repeat_start = 1 |
7b86f0bc | 130 | self.wr = -1 |
7b86f0bc | 131 | |
c4975078 | 132 | # Gather 8 bits of data plus the ACK/NACK bit. |
e5080882 | 133 | def found_address_or_data(self, scl, sda): |
7b86f0bc UH |
134 | # Address and data are transmitted MSB-first. |
135 | self.databyte <<= 1 | |
136 | self.databyte |= sda | |
137 | ||
c4975078 BV |
138 | if self.bitcount == 0: |
139 | self.startsample = self.samplenum | |
140 | ||
7b86f0bc | 141 | # Return if we haven't collected all 8 + 1 bits, yet. |
c4975078 | 142 | self.bitcount += 1 |
1b75abfd | 143 | if self.bitcount != 8: |
eb7082c9 | 144 | return |
7b86f0bc | 145 | |
1b75abfd BV |
146 | # We triggered on the ACK/NACK bit, but won't report that until later. |
147 | self.startsample -= 1 | |
148 | ||
d94ff143 | 149 | d = self.databyte |
2b716038 | 150 | if self.state == 'FIND ADDRESS': |
7b86f0bc | 151 | # The READ/WRITE bit is only in address bytes, not data bytes. |
bf1c3f4d | 152 | self.wr = 0 if (self.databyte & 1) else 1 |
d94ff143 UH |
153 | if self.options['address_format'] == 'shifted': |
154 | d = d >> 1 | |
15969949 | 155 | |
2b716038 | 156 | if self.state == 'FIND ADDRESS' and self.wr == 1: |
a2d2aff2 | 157 | cmd = 'ADDRESS WRITE' |
2b716038 | 158 | elif self.state == 'FIND ADDRESS' and self.wr == 0: |
a2d2aff2 | 159 | cmd = 'ADDRESS READ' |
2b716038 | 160 | elif self.state == 'FIND DATA' and self.wr == 1: |
a2d2aff2 | 161 | cmd = 'DATA WRITE' |
2b716038 | 162 | elif self.state == 'FIND DATA' and self.wr == 0: |
a2d2aff2 | 163 | cmd = 'DATA READ' |
eb7082c9 | 164 | |
d94ff143 UH |
165 | self.putp([cmd, d]) |
166 | self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d), | |
167 | '%s: %02X' % (proto[cmd][2], d), '%02X' % d]]) | |
7b86f0bc | 168 | |
1b75abfd | 169 | # Done with this packet. |
7b86f0bc | 170 | self.startsample = -1 |
1b75abfd | 171 | self.bitcount = self.databyte = 0 |
2b716038 | 172 | self.state = 'FIND ACK' |
7b86f0bc | 173 | |
1b75abfd BV |
174 | def get_ack(self, scl, sda): |
175 | self.startsample = self.samplenum | |
d94ff143 UH |
176 | cmd = 'NACK' if (sda == 1) else 'ACK' |
177 | self.putp([cmd, None]) | |
178 | self.putx([proto[cmd][0], proto[cmd][1:]]) | |
1b75abfd BV |
179 | # There could be multiple data bytes in a row, so either find |
180 | # another data byte or a STOP condition next. | |
2b716038 | 181 | self.state = 'FIND DATA' |
7b86f0bc | 182 | |
e5080882 | 183 | def found_stop(self, scl, sda): |
c4975078 | 184 | self.startsample = self.samplenum |
d94ff143 UH |
185 | cmd = 'STOP' |
186 | self.putp([cmd, None]) | |
187 | self.putx([proto[cmd][0], proto[cmd][1:]]) | |
2b716038 | 188 | self.state = 'FIND START' |
5dd9af5b | 189 | self.is_repeat_start = 0 |
7b86f0bc UH |
190 | self.wr = -1 |
191 | ||
2b9837d9 | 192 | def decode(self, ss, es, data): |
2fcd7c22 UH |
193 | for (self.samplenum, pins) in data: |
194 | ||
195 | # Ignore identical samples early on (for performance reasons). | |
196 | if self.oldpins == pins: | |
197 | continue | |
198 | self.oldpins, (scl, sda) = pins, pins | |
f39d2404 | 199 | |
f39d2404 UH |
200 | # TODO: Wait until the bus is idle (SDA = SCL = 1) first? |
201 | ||
7b86f0bc | 202 | # State machine. |
2b716038 | 203 | if self.state == 'FIND START': |
7b86f0bc | 204 | if self.is_start_condition(scl, sda): |
e5080882 | 205 | self.found_start(scl, sda) |
2b716038 | 206 | elif self.state == 'FIND ADDRESS': |
7b86f0bc | 207 | if self.is_data_bit(scl, sda): |
e5080882 | 208 | self.found_address_or_data(scl, sda) |
2b716038 | 209 | elif self.state == 'FIND DATA': |
7b86f0bc | 210 | if self.is_data_bit(scl, sda): |
e5080882 | 211 | self.found_address_or_data(scl, sda) |
7b86f0bc | 212 | elif self.is_start_condition(scl, sda): |
e5080882 | 213 | self.found_start(scl, sda) |
7b86f0bc | 214 | elif self.is_stop_condition(scl, sda): |
e5080882 | 215 | self.found_stop(scl, sda) |
2b716038 | 216 | elif self.state == 'FIND ACK': |
1b75abfd BV |
217 | if self.is_data_bit(scl, sda): |
218 | self.get_ack(scl, sda) | |
7b86f0bc | 219 | else: |
0eeeb544 | 220 | raise Exception('Invalid state: %s' % self.state) |
f39d2404 UH |
221 | |
222 | # Save current SDA/SCL values for the next round. | |
223 | self.oldscl = scl | |
224 | self.oldsda = sda | |
225 |