]> sigrok.org Git - libsigrokdecode.git/blob - decoders/i2s/pd.py
91801b4e98f9c0c6a62dcb8b92852907513f6092
[libsigrokdecode.git] / decoders / i2s / pd.py
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, see <http://www.gnu.org/licenses/>.
18 ##
19
20 import sigrokdecode as srd
21 import struct
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 = 3
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):
63         self.reset()
64
65     def reset(self):
66         self.samplerate = None
67         self.oldws = 1
68         self.bitcount = 0
69         self.data = 0
70         self.samplesreceived = 0
71         self.first_sample = None
72         self.ss_block = None
73         self.wordlength = -1
74         self.wrote_wav_header = False
75
76     def start(self):
77         self.out_python = self.register(srd.OUTPUT_PYTHON)
78         self.out_binary = self.register(srd.OUTPUT_BINARY)
79         self.out_ann = self.register(srd.OUTPUT_ANN)
80
81     def metadata(self, key, value):
82         if key == srd.SRD_CONF_SAMPLERATE:
83             self.samplerate = value
84
85     def putpb(self, data):
86         self.put(self.ss_block, self.samplenum, self.out_python, data)
87
88     def putbin(self, data):
89         self.put(self.ss_block, self.samplenum, self.out_binary, data)
90
91     def putb(self, data):
92         self.put(self.ss_block, self.samplenum, self.out_ann, data)
93
94     def report(self):
95
96         # Calculate the sample rate.
97         samplerate = '?'
98         if self.ss_block is not None and \
99             self.first_sample is not None and \
100             self.ss_block > self.first_sample:
101             samplerate = '%d' % (self.samplesreceived *
102                 self.samplerate / (self.ss_block -
103                 self.first_sample))
104
105         return 'I²S: %d %d-bit samples received at %sHz' % \
106             (self.samplesreceived, self.wordlength, samplerate)
107
108     def wav_header(self):
109         # Chunk descriptor
110         h  = b'RIFF'
111         h += b'\x24\x80\x00\x00' # Chunk size (2084)
112         h += b'WAVE'
113         # Fmt subchunk
114         h += b'fmt '
115         h += b'\x10\x00\x00\x00' # Subchunk size (16 bytes)
116         h += b'\x01\x00'         # Audio format (0x0001 == PCM)
117         h += b'\x02\x00'         # Number of channels (2)
118         h += b'\x80\x3e\x00\x00' # Samplerate (16000)
119         h += b'\x00\xfa\x00\x00' # Byterate (64000)
120         h += b'\x04\x00'         # Blockalign (4)
121         h += b'\x20\x00'         # Bits per sample (32)
122         # Data subchunk
123         h += b'data'
124         h += b'\xff\xff\xff\xff' # Subchunk size (4G bytes) TODO
125         return h
126
127     def wav_sample(self, sample):
128         return struct.pack('<I', self.data)
129
130     def decode(self):
131         if not self.samplerate:
132             raise SamplerateError('Cannot decode without samplerate.')
133         while True:
134             # Wait for a rising edge on the SCK pin.
135             sck, ws, sd = self.wait({0: 'r'})
136
137             self.data = (self.data << 1) | sd
138             self.bitcount += 1
139
140             # This was not the LSB unless WS has flipped.
141             if ws == self.oldws:
142                 continue
143
144             # Only submit the sample, if we received the beginning of it.
145             if self.ss_block is not None:
146
147                 if not self.wrote_wav_header:
148                     self.put(0, 0, self.out_binary, [0, self.wav_header()])
149                     self.wrote_wav_header = True
150
151                 self.samplesreceived += 1
152
153                 idx = 0 if self.oldws else 1
154                 c1 = 'Left channel' if self.oldws else 'Right channel'
155                 c2 = 'Left' if self.oldws else 'Right'
156                 c3 = 'L' if self.oldws else 'R'
157                 v = '%08x' % self.data
158                 self.putpb(['DATA', [c3, self.data]])
159                 self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
160                                  '%s: %s' % (c3, v), c3]])
161                 self.putbin([0, self.wav_sample(self.data)])
162
163                 # Check that the data word was the correct length.
164                 if self.wordlength != -1 and self.wordlength != self.bitcount:
165                     self.putb([2, ['Received %d-bit word, expected %d-bit '
166                                    'word' % (self.bitcount, self.wordlength)]])
167
168                 self.wordlength = self.bitcount
169
170             # Reset decoder state.
171             self.data = 0
172             self.bitcount = 0
173             self.ss_block = self.samplenum
174
175             # Save the first sample position.
176             if self.first_sample is None:
177                 self.first_sample = self.samplenum
178
179             self.oldws = ws