]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2010-2016 Uwe Hermann <uwe@hermann-uwe.de> | |
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, see <http://www.gnu.org/licenses/>. | |
18 | ## | |
19 | ||
20 | # TODO: Look into arbitration, collision detection, clock synchronisation, etc. | |
21 | # TODO: Implement support for inverting SDA/SCL levels (0->1 and 1->0). | |
22 | # TODO: Implement support for detecting various bus errors. | |
23 | ||
24 | import sigrokdecode as srd | |
25 | ||
26 | ''' | |
27 | OUTPUT_PYTHON format: | |
28 | ||
29 | Packet: | |
30 | [<ptype>, <pdata>] | |
31 | ||
32 | <ptype>: | |
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) | |
42 | - 'BITS' (<pdata>: list of data/address bits and their ss/es numbers) | |
43 | ||
44 | <pdata> is the data or address byte associated with the 'ADDRESS*' and 'DATA*' | |
45 | command. Slave addresses do not include bit 0 (the READ/WRITE indication bit). | |
46 | For example, a slave address field could be 0x51 (instead of 0xa2). | |
47 | For 'START', 'START REPEAT', 'STOP', 'ACK', and 'NACK' <pdata> is None. | |
48 | ''' | |
49 | ||
50 | # CMD: [annotation-type-index, long annotation, short annotation] | |
51 | proto = { | |
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'], | |
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'], | |
62 | } | |
63 | ||
64 | class Decoder(srd.Decoder): | |
65 | api_version = 3 | |
66 | id = 'i2c' | |
67 | name = 'I²C' | |
68 | longname = 'Inter-Integrated Circuit' | |
69 | desc = 'Two-wire, multi-master, serial bus.' | |
70 | license = 'gplv2+' | |
71 | inputs = ['logic'] | |
72 | outputs = ['i2c'] | |
73 | tags = ['Embedded/industrial'] | |
74 | channels = ( | |
75 | {'id': 'scl', 'name': 'SCL', 'desc': 'Serial clock line'}, | |
76 | {'id': 'sda', 'name': 'SDA', 'desc': 'Serial data line'}, | |
77 | ) | |
78 | options = ( | |
79 | {'id': 'address_format', 'desc': 'Displayed slave address format', | |
80 | 'default': 'shifted', 'values': ('shifted', 'unshifted')}, | |
81 | ) | |
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 | ('warning', 'Warning'), | |
94 | ) | |
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 | ) | |
100 | binary = ( | |
101 | ('address-read', 'Address read'), | |
102 | ('address-write', 'Address write'), | |
103 | ('data-read', 'Data read'), | |
104 | ('data-write', 'Data write'), | |
105 | ) | |
106 | ||
107 | def __init__(self): | |
108 | self.reset() | |
109 | ||
110 | def reset(self): | |
111 | self.samplerate = None | |
112 | self.ss = self.es = self.ss_byte = -1 | |
113 | self.bitcount = 0 | |
114 | self.databyte = 0 | |
115 | self.wr = -1 | |
116 | self.is_repeat_start = 0 | |
117 | self.state = 'FIND START' | |
118 | self.pdu_start = None | |
119 | self.pdu_bits = 0 | |
120 | self.bits = [] | |
121 | ||
122 | def metadata(self, key, value): | |
123 | if key == srd.SRD_CONF_SAMPLERATE: | |
124 | self.samplerate = value | |
125 | ||
126 | def start(self): | |
127 | self.out_python = self.register(srd.OUTPUT_PYTHON) | |
128 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
129 | self.out_binary = self.register(srd.OUTPUT_BINARY) | |
130 | self.out_bitrate = self.register(srd.OUTPUT_META, | |
131 | meta=(int, 'Bitrate', 'Bitrate from Start bit to Stop bit')) | |
132 | ||
133 | def putx(self, data): | |
134 | self.put(self.ss, self.es, self.out_ann, data) | |
135 | ||
136 | def putp(self, data): | |
137 | self.put(self.ss, self.es, self.out_python, data) | |
138 | ||
139 | def putb(self, data): | |
140 | self.put(self.ss, self.es, self.out_binary, data) | |
141 | ||
142 | def handle_start(self, pins): | |
143 | self.ss, self.es = self.samplenum, self.samplenum | |
144 | self.pdu_start = self.samplenum | |
145 | self.pdu_bits = 0 | |
146 | cmd = 'START REPEAT' if (self.is_repeat_start == 1) else 'START' | |
147 | self.putp([cmd, None]) | |
148 | self.putx([proto[cmd][0], proto[cmd][1:]]) | |
149 | self.state = 'FIND ADDRESS' | |
150 | self.bitcount = self.databyte = 0 | |
151 | self.is_repeat_start = 1 | |
152 | self.wr = -1 | |
153 | self.bits = [] | |
154 | ||
155 | # Gather 8 bits of data plus the ACK/NACK bit. | |
156 | def handle_address_or_data(self, pins): | |
157 | scl, sda = pins | |
158 | self.pdu_bits += 1 | |
159 | ||
160 | # Address and data are transmitted MSB-first. | |
161 | self.databyte <<= 1 | |
162 | self.databyte |= sda | |
163 | ||
164 | # Remember the start of the first data/address bit. | |
165 | if self.bitcount == 0: | |
166 | self.ss_byte = self.samplenum | |
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 | |
176 | ||
177 | # Return if we haven't collected all 8 + 1 bits, yet. | |
178 | if self.bitcount < 7: | |
179 | self.bitcount += 1 | |
180 | return | |
181 | ||
182 | d = self.databyte | |
183 | if self.state == 'FIND ADDRESS': | |
184 | # The READ/WRITE bit is only in address bytes, not data bytes. | |
185 | self.wr = 0 if (self.databyte & 1) else 1 | |
186 | if self.options['address_format'] == 'shifted': | |
187 | d = d >> 1 | |
188 | ||
189 | bin_class = -1 | |
190 | if self.state == 'FIND ADDRESS' and self.wr == 1: | |
191 | cmd = 'ADDRESS WRITE' | |
192 | bin_class = 1 | |
193 | elif self.state == 'FIND ADDRESS' and self.wr == 0: | |
194 | cmd = 'ADDRESS READ' | |
195 | bin_class = 0 | |
196 | elif self.state == 'FIND DATA' and self.wr == 1: | |
197 | cmd = 'DATA WRITE' | |
198 | bin_class = 3 | |
199 | elif self.state == 'FIND DATA' and self.wr == 0: | |
200 | cmd = 'DATA READ' | |
201 | bin_class = 2 | |
202 | ||
203 | self.ss, self.es = self.ss_byte, self.samplenum + self.bitwidth | |
204 | ||
205 | self.putp(['BITS', self.bits]) | |
206 | self.putp([cmd, d]) | |
207 | ||
208 | self.putb([bin_class, bytes([d])]) | |
209 | ||
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]) | |
217 | self.ss, self.es = self.ss_byte, self.samplenum | |
218 | ||
219 | self.putx([proto[cmd][0], ['%s: %02X' % (proto[cmd][1], d), | |
220 | '%s: %02X' % (proto[cmd][2], d), '%02X' % d]]) | |
221 | ||
222 | # Done with this packet. | |
223 | self.bitcount = self.databyte = 0 | |
224 | self.bits = [] | |
225 | self.state = 'FIND ACK' | |
226 | ||
227 | def get_ack(self, pins): | |
228 | scl, sda = pins | |
229 | self.ss, self.es = self.samplenum, self.samplenum + self.bitwidth | |
230 | cmd = 'NACK' if (sda == 1) else 'ACK' | |
231 | self.putp([cmd, None]) | |
232 | self.putx([proto[cmd][0], proto[cmd][1:]]) | |
233 | # There could be multiple data bytes in a row, so either find | |
234 | # another data byte or a STOP condition next. | |
235 | self.state = 'FIND DATA' | |
236 | ||
237 | def handle_stop(self, pins): | |
238 | # Meta bitrate | |
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) | |
243 | ||
244 | cmd = 'STOP' | |
245 | self.ss, self.es = self.samplenum, self.samplenum | |
246 | self.putp([cmd, None]) | |
247 | self.putx([proto[cmd][0], proto[cmd][1:]]) | |
248 | self.state = 'FIND START' | |
249 | self.is_repeat_start = 0 | |
250 | self.wr = -1 | |
251 | self.bits = [] | |
252 | ||
253 | def decode(self): | |
254 | while True: | |
255 | # State machine. | |
256 | if self.state == 'FIND START': | |
257 | # Wait for a START condition (S): SCL = high, SDA = falling. | |
258 | self.handle_start(self.wait({0: 'h', 1: 'f'})) | |
259 | elif self.state == 'FIND ADDRESS': | |
260 | # Wait for a data bit: SCL = rising. | |
261 | self.handle_address_or_data(self.wait({0: 'r'})) | |
262 | elif self.state == 'FIND DATA': | |
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 | |
267 | pins = self.wait([{0: 'r'}, {0: 'h', 1: 'f'}, {0: 'h', 1: 'r'}]) | |
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) | |
276 | elif self.state == 'FIND ACK': | |
277 | # Wait for a data/ack bit: SCL = rising. | |
278 | self.get_ack(self.wait({0: 'r'})) |