]> sigrok.org Git - libsigrokdecode.git/blame - decoders/i2s/pd.py
i2s: Add helper methods for annotation/proto output.
[libsigrokdecode.git] / decoders / i2s / pd.py
CommitLineData
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
23import sigrokdecode as srd
24
2ab416c2
JH
25class Decoder(srd.Decoder):
26 api_version = 1
27 id = 'i2s'
28 name = 'I2S'
29 longname = 'Integrated Interchip Sound'
a465436e 30 desc = 'Serial bus for connecting digital audio devices.'
2ab416c2
JH
31 license = 'gplv2+'
32 inputs = ['logic']
33 outputs = ['i2s']
34 probes = [
35 {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'},
36 {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'},
ee3e279c 37 {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'},
2ab416c2 38 ]
ee3e279c
UH
39 optional_probes = []
40 options = {}
2ab416c2 41 annotations = [
29fc3625
UH
42 ['left', 'Left channel'],
43 ['right', 'Right channel'],
44 ['warnings', 'Warnings'],
2ab416c2
JH
45 ]
46
47 def __init__(self, **kwargs):
48 self.oldsck = 1
49 self.oldws = 1
50 self.bitcount = 0
51 self.data = 0
52 self.samplesreceived = 0
780a5bee 53 self.first_sample = None
2ab416c2 54 self.start_sample = None
abbbd2ba 55 self.wordlength = -1
2ab416c2
JH
56
57 def start(self, metadata):
780a5bee 58 self.samplerate = metadata['samplerate']
2ab416c2
JH
59 self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2s')
60 self.out_ann = self.add(srd.OUTPUT_ANN, 'i2s')
61
2b2b7c30
UH
62 def putpb(self, data):
63 self.put(self.start_sample, self.samplenum, self.out_proto, data)
64
65 def putb(self, data):
66 self.put(self.start_sample, self.samplenum, self.out_ann, data)
67
2ab416c2 68 def report(self):
780a5bee 69
ee3e279c 70 # Calculate the sample rate.
780a5bee
JH
71 samplerate = '?'
72 if self.start_sample != None and \
73 self.first_sample != None and \
74 self.start_sample > self.first_sample:
ee3e279c 75 samplerate = '%d' % (self.samplesreceived *
780a5bee
JH
76 self.samplerate / (self.start_sample -
77 self.first_sample))
78
79 return 'I2S: %d %d-bit samples received at %sHz' % \
80 (self.samplesreceived, self.wordlength, samplerate)
2ab416c2
JH
81
82 def decode(self, ss, es, data):
2b2b7c30 83 for self.samplenum, (sck, ws, sd) in data:
2ab416c2
JH
84
85 # Ignore sample if the bit clock hasn't changed.
86 if sck == self.oldsck:
87 continue
88
89 self.oldsck = sck
ee3e279c 90 if sck == 0: # Ignore the falling clock edge.
2ab416c2
JH
91 continue
92
93 self.data = (self.data << 1) | sd
94 self.bitcount += 1
95
ee3e279c 96 # This was not the LSB unless WS has flipped.
2ab416c2
JH
97 if ws == self.oldws:
98 continue
99
ee3e279c 100 # Only submit the sample, if we received the beginning of it.
2ab416c2
JH
101 if self.start_sample != None:
102 self.samplesreceived += 1
2b2b7c30
UH
103 self.putpb(['data', self.data])
104 self.putb([0 if self.oldws else 1, ['0x%08x' % self.data]])
abbbd2ba 105
ee3e279c 106 # Check that the data word was the correct length.
abbbd2ba 107 if self.wordlength != -1 and self.wordlength != self.bitcount:
2b2b7c30
UH
108 self.putb([2, ['Received %d-bit word, expected %d-bit '
109 'word' % (self.bitcount, self.wordlength)]])
abbbd2ba
JH
110
111 self.wordlength = self.bitcount
2ab416c2
JH
112
113 # Reset decoder state.
114 self.data = 0
115 self.bitcount = 0
2b2b7c30 116 self.start_sample = self.samplenum
780a5bee 117
ee3e279c 118 # Save the first sample position.
780a5bee 119 if self.first_sample == None:
2b2b7c30 120 self.first_sample = self.samplenum
780a5bee 121
2ab416c2 122 self.oldws = ws
ee3e279c 123