]>
Commit | Line | Data |
---|---|---|
2ab416c2 | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
2ab416c2 JH |
3 | ## |
4 | ## Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk> | |
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 | ||
21 | # I2S protocol decoder | |
22 | ||
23 | import sigrokdecode as srd | |
24 | ||
4adb11a9 UH |
25 | ''' |
26 | Protocol output format: | |
27 | ||
28 | Packet: | |
29 | [<ptype>, <pdata>] | |
30 | ||
31 | <ptype>, <pdata>: | |
32 | - 'DATA', [<channel>, <value>] | |
33 | ||
34 | <channel>: 'L' or 'R' | |
35 | <value>: integer | |
36 | ''' | |
37 | ||
2ab416c2 JH |
38 | class Decoder(srd.Decoder): |
39 | api_version = 1 | |
40 | id = 'i2s' | |
41 | name = 'I2S' | |
42 | longname = 'Integrated Interchip Sound' | |
a465436e | 43 | desc = 'Serial bus for connecting digital audio devices.' |
2ab416c2 JH |
44 | license = 'gplv2+' |
45 | inputs = ['logic'] | |
46 | outputs = ['i2s'] | |
47 | probes = [ | |
48 | {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'}, | |
49 | {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'}, | |
ee3e279c | 50 | {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'}, |
2ab416c2 | 51 | ] |
ee3e279c UH |
52 | optional_probes = [] |
53 | options = {} | |
2ab416c2 | 54 | annotations = [ |
29fc3625 UH |
55 | ['left', 'Left channel'], |
56 | ['right', 'Right channel'], | |
57 | ['warnings', 'Warnings'], | |
2ab416c2 JH |
58 | ] |
59 | ||
60 | def __init__(self, **kwargs): | |
f372d597 | 61 | self.samplerate = None |
2ab416c2 JH |
62 | self.oldsck = 1 |
63 | self.oldws = 1 | |
64 | self.bitcount = 0 | |
65 | self.data = 0 | |
66 | self.samplesreceived = 0 | |
780a5bee | 67 | self.first_sample = None |
2ab416c2 | 68 | self.start_sample = None |
abbbd2ba | 69 | self.wordlength = -1 |
2ab416c2 | 70 | |
f372d597 | 71 | def start(self): |
be465111 BV |
72 | self.out_proto = self.register(srd.OUTPUT_PYTHON) |
73 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
2ab416c2 | 74 | |
f372d597 BV |
75 | def metadata(self, key, value): |
76 | if key == srd.SRD_CONF_SAMPLERATE: | |
77 | self.samplerate = value | |
78 | ||
2b2b7c30 UH |
79 | def putpb(self, data): |
80 | self.put(self.start_sample, self.samplenum, self.out_proto, data) | |
81 | ||
82 | def putb(self, data): | |
83 | self.put(self.start_sample, self.samplenum, self.out_ann, data) | |
84 | ||
2ab416c2 | 85 | def report(self): |
780a5bee | 86 | |
ee3e279c | 87 | # Calculate the sample rate. |
780a5bee JH |
88 | samplerate = '?' |
89 | if self.start_sample != None and \ | |
90 | self.first_sample != None and \ | |
91 | self.start_sample > self.first_sample: | |
ee3e279c | 92 | samplerate = '%d' % (self.samplesreceived * |
780a5bee JH |
93 | self.samplerate / (self.start_sample - |
94 | self.first_sample)) | |
95 | ||
96 | return 'I2S: %d %d-bit samples received at %sHz' % \ | |
97 | (self.samplesreceived, self.wordlength, samplerate) | |
2ab416c2 JH |
98 | |
99 | def decode(self, ss, es, data): | |
f372d597 BV |
100 | if self.samplerate is None: |
101 | raise Exception("Cannot decode without samplerate.") | |
2b2b7c30 | 102 | for self.samplenum, (sck, ws, sd) in data: |
2ab416c2 JH |
103 | |
104 | # Ignore sample if the bit clock hasn't changed. | |
105 | if sck == self.oldsck: | |
106 | continue | |
107 | ||
108 | self.oldsck = sck | |
ee3e279c | 109 | if sck == 0: # Ignore the falling clock edge. |
2ab416c2 JH |
110 | continue |
111 | ||
112 | self.data = (self.data << 1) | sd | |
113 | self.bitcount += 1 | |
114 | ||
ee3e279c | 115 | # This was not the LSB unless WS has flipped. |
2ab416c2 JH |
116 | if ws == self.oldws: |
117 | continue | |
118 | ||
ee3e279c | 119 | # Only submit the sample, if we received the beginning of it. |
2ab416c2 JH |
120 | if self.start_sample != None: |
121 | self.samplesreceived += 1 | |
750a808a UH |
122 | |
123 | idx = 0 if self.oldws else 1 | |
124 | c1 = 'Left channel' if self.oldws else 'Right channel' | |
125 | c2 = 'Left' if self.oldws else 'Right' | |
126 | c3 = 'L' if self.oldws else 'R' | |
127 | v = '%08x' % self.data | |
128 | self.putpb(['DATA', [c3, self.data]]) | |
129 | self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v), | |
130 | '%s: %s' % (c3, v), c3]]) | |
abbbd2ba | 131 | |
ee3e279c | 132 | # Check that the data word was the correct length. |
abbbd2ba | 133 | if self.wordlength != -1 and self.wordlength != self.bitcount: |
2b2b7c30 UH |
134 | self.putb([2, ['Received %d-bit word, expected %d-bit ' |
135 | 'word' % (self.bitcount, self.wordlength)]]) | |
abbbd2ba JH |
136 | |
137 | self.wordlength = self.bitcount | |
2ab416c2 JH |
138 | |
139 | # Reset decoder state. | |
140 | self.data = 0 | |
141 | self.bitcount = 0 | |
2b2b7c30 | 142 | self.start_sample = self.samplenum |
780a5bee | 143 | |
ee3e279c | 144 | # Save the first sample position. |
780a5bee | 145 | if self.first_sample == None: |
2b2b7c30 | 146 | self.first_sample = self.samplenum |
780a5bee | 147 | |
2ab416c2 | 148 | self.oldws = ws |
ee3e279c | 149 |