]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
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 | import sigrokdecode as srd | |
22 | ||
23 | ''' | |
24 | OUTPUT_PYTHON format: | |
25 | ||
26 | Packet: | |
27 | [<ptype>, <pdata>] | |
28 | ||
29 | <ptype>, <pdata>: | |
30 | - 'DATA', [<channel>, <value>] | |
31 | ||
32 | <channel>: 'L' or 'R' | |
33 | <value>: integer | |
34 | ''' | |
35 | ||
36 | class SamplerateError(Exception): | |
37 | pass | |
38 | ||
39 | class Decoder(srd.Decoder): | |
40 | api_version = 2 | |
41 | id = 'i2s' | |
42 | name = 'I²S' | |
43 | longname = 'Integrated Interchip Sound' | |
44 | desc = 'Serial bus for connecting digital audio devices.' | |
45 | license = 'gplv2+' | |
46 | inputs = ['logic'] | |
47 | outputs = ['i2s'] | |
48 | channels = ( | |
49 | {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'}, | |
50 | {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'}, | |
51 | {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'}, | |
52 | ) | |
53 | annotations = ( | |
54 | ('left', 'Left channel'), | |
55 | ('right', 'Right channel'), | |
56 | ('warnings', 'Warnings'), | |
57 | ) | |
58 | binary = ( | |
59 | ('wav', 'WAV file'), | |
60 | ) | |
61 | ||
62 | def __init__(self, **kwargs): | |
63 | self.samplerate = None | |
64 | self.oldsck = 1 | |
65 | self.oldws = 1 | |
66 | self.bitcount = 0 | |
67 | self.data = 0 | |
68 | self.samplesreceived = 0 | |
69 | self.first_sample = None | |
70 | self.ss_block = None | |
71 | self.wordlength = -1 | |
72 | self.wrote_wav_header = False | |
73 | ||
74 | def start(self): | |
75 | self.out_python = self.register(srd.OUTPUT_PYTHON) | |
76 | self.out_binary = self.register(srd.OUTPUT_BINARY) | |
77 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
78 | ||
79 | def metadata(self, key, value): | |
80 | if key == srd.SRD_CONF_SAMPLERATE: | |
81 | self.samplerate = value | |
82 | ||
83 | def putpb(self, data): | |
84 | self.put(self.ss_block, self.samplenum, self.out_python, data) | |
85 | ||
86 | def putbin(self, data): | |
87 | self.put(self.ss_block, self.samplenum, self.out_binary, data) | |
88 | ||
89 | def putb(self, data): | |
90 | self.put(self.ss_block, self.samplenum, self.out_ann, data) | |
91 | ||
92 | def report(self): | |
93 | ||
94 | # Calculate the sample rate. | |
95 | samplerate = '?' | |
96 | if self.ss_block is not None and \ | |
97 | self.first_sample is not None and \ | |
98 | self.ss_block > self.first_sample: | |
99 | samplerate = '%d' % (self.samplesreceived * | |
100 | self.samplerate / (self.ss_block - | |
101 | self.first_sample)) | |
102 | ||
103 | return 'I²S: %d %d-bit samples received at %sHz' % \ | |
104 | (self.samplesreceived, self.wordlength, samplerate) | |
105 | ||
106 | def wav_header(self): | |
107 | # Chunk descriptor | |
108 | h = b'RIFF' | |
109 | h += b'\x24\x80\x00\x00' # Chunk size (2084) | |
110 | h += b'WAVE' | |
111 | # Fmt subchunk | |
112 | h += b'fmt ' | |
113 | h += b'\x10\x00\x00\x00' # Subchunk size (16 bytes) | |
114 | h += b'\x01\x00' # Audio format (0x0001 == PCM) | |
115 | h += b'\x02\x00' # Number of channels (2) | |
116 | h += b'\x80\x3e\x00\x00' # Samplerate (16000) | |
117 | h += b'\x00\x7d\x00\x00' # Byterate (32000) | |
118 | h += b'\x04\x00' # Blockalign (4) | |
119 | h += b'\x10\x00' # Bits per sample (16) | |
120 | # Data subchunk | |
121 | h += b'data' | |
122 | h += b'\xff\xff\x00\x00' # Subchunk size (65535 bytes) TODO | |
123 | return h | |
124 | ||
125 | def wav_sample(self, sample): | |
126 | # TODO: This currently assumes U32 samples, and converts to S16. | |
127 | s = sample >> 16 | |
128 | if s >= 0x8000: | |
129 | s -= 0x10000 | |
130 | lo, hi = s & 0xff, (s >> 8) & 0xff | |
131 | return bytes([lo, hi]) | |
132 | ||
133 | def decode(self, ss, es, data): | |
134 | if not self.samplerate: | |
135 | raise SamplerateError('Cannot decode without samplerate.') | |
136 | for self.samplenum, (sck, ws, sd) in data: | |
137 | ||
138 | # Ignore sample if the bit clock hasn't changed. | |
139 | if sck == self.oldsck: | |
140 | continue | |
141 | ||
142 | self.oldsck = sck | |
143 | if sck == 0: # Ignore the falling clock edge. | |
144 | continue | |
145 | ||
146 | self.data = (self.data << 1) | sd | |
147 | self.bitcount += 1 | |
148 | ||
149 | # This was not the LSB unless WS has flipped. | |
150 | if ws == self.oldws: | |
151 | continue | |
152 | ||
153 | # Only submit the sample, if we received the beginning of it. | |
154 | if self.ss_block is not None: | |
155 | ||
156 | if not self.wrote_wav_header: | |
157 | self.put(0, 0, self.out_binary, [0, self.wav_header()]) | |
158 | self.wrote_wav_header = True | |
159 | ||
160 | self.samplesreceived += 1 | |
161 | ||
162 | idx = 0 if self.oldws else 1 | |
163 | c1 = 'Left channel' if self.oldws else 'Right channel' | |
164 | c2 = 'Left' if self.oldws else 'Right' | |
165 | c3 = 'L' if self.oldws else 'R' | |
166 | v = '%08x' % self.data | |
167 | self.putpb(['DATA', [c3, self.data]]) | |
168 | self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v), | |
169 | '%s: %s' % (c3, v), c3]]) | |
170 | self.putbin([0, self.wav_sample(self.data)]) | |
171 | ||
172 | # Check that the data word was the correct length. | |
173 | if self.wordlength != -1 and self.wordlength != self.bitcount: | |
174 | self.putb([2, ['Received %d-bit word, expected %d-bit ' | |
175 | 'word' % (self.bitcount, self.wordlength)]]) | |
176 | ||
177 | self.wordlength = self.bitcount | |
178 | ||
179 | # Reset decoder state. | |
180 | self.data = 0 | |
181 | self.bitcount = 0 | |
182 | self.ss_block = self.samplenum | |
183 | ||
184 | # Save the first sample position. | |
185 | if self.first_sample is None: | |
186 | self.first_sample = self.samplenum | |
187 | ||
188 | self.oldws = ws |