2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
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.
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.
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/>.
20 import sigrokdecode as srd
30 - 'DATA', [<channel>, <value>]
36 class Decoder(srd.Decoder):
40 longname = 'Integrated Interchip Sound'
41 desc = 'Serial bus for connecting digital audio devices.'
45 tags = ['Audio', 'PC']
47 {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'},
48 {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'},
49 {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'},
52 ('left', 'Left channel'),
53 ('right', 'Right channel'),
54 ('warnings', 'Warnings'),
64 self.samplerate = None
68 self.samplesreceived = 0
69 self.first_sample = None
72 self.wrote_wav_header = False
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)
79 def metadata(self, key, value):
80 if key == srd.SRD_CONF_SAMPLERATE:
81 self.samplerate = value
83 def putpb(self, data):
84 self.put(self.ss_block, self.samplenum, self.out_python, data)
86 def putbin(self, data):
87 self.put(self.ss_block, self.samplenum, self.out_binary, data)
90 self.put(self.ss_block, self.samplenum, self.out_ann, data)
93 # Calculate the sample rate.
95 if self.ss_block is not None and \
96 self.first_sample is not None and \
97 self.ss_block > self.first_sample and \
99 samplerate = '%d' % (self.samplesreceived *
100 self.samplerate / (self.ss_block -
103 return 'I²S: %d %d-bit samples received at %sHz' % \
104 (self.samplesreceived, self.wordlength, samplerate)
106 def wav_header(self):
109 h += b'\x24\x80\x00\x00' # Chunk size (2084)
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\xfa\x00\x00' # Byterate (64000)
118 h += b'\x04\x00' # Blockalign (4)
119 h += b'\x20\x00' # Bits per sample (32)
122 h += b'\xff\xff\xff\xff' # Subchunk size (4G bytes) TODO
125 def wav_sample(self, sample):
126 return struct.pack('<I', self.data)
130 # Wait for a rising edge on the SCK pin.
131 sck, ws, sd = self.wait({0: 'r'})
133 self.data = (self.data << 1) | sd
136 # This was not the LSB unless WS has flipped.
140 # Only submit the sample, if we received the beginning of it.
141 if self.ss_block is not None:
143 if not self.wrote_wav_header:
144 self.put(0, 0, self.out_binary, [0, self.wav_header()])
145 self.wrote_wav_header = True
147 self.samplesreceived += 1
149 sck = self.wait({0: 'f'})
151 idx = 0 if not self.oldws else 1
152 c1 = 'Left channel' if not self.oldws else 'Right channel'
153 c2 = 'Left' if not self.oldws else 'Right'
154 c3 = 'L' if not self.oldws else 'R'
155 v = '%08x' % self.data
156 self.putpb(['DATA', [c3, self.data]])
157 self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
158 '%s: %s' % (c3, v), c3]])
159 self.putbin([0, self.wav_sample(self.data)])
161 # Check that the data word was the correct length.
162 if self.wordlength != -1 and self.wordlength != self.bitcount:
163 self.putb([2, ['Received %d-bit word, expected %d-bit '
164 'word' % (self.bitcount, self.wordlength)]])
166 self.wordlength = self.bitcount
168 sck = self.wait({0: 'f'})
170 # Reset decoder state.
173 self.ss_block = self.samplenum
175 # Save the first sample position.
176 if self.first_sample is None:
177 self.first_sample = self.samplenum