]> sigrok.org Git - libsigrokdecode.git/blob - decoders/i2s/pd.py
i2s: Add shorter and longer annotations for GUI use.
[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, 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
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
38 class Decoder(srd.Decoder):
39     api_version = 1
40     id = 'i2s'
41     name = 'I2S'
42     longname = 'Integrated Interchip Sound'
43     desc = 'Serial bus for connecting digital audio devices.'
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'},
50         {'id': 'sd', 'name': 'SD', 'desc': 'Serial data line'},
51     ]
52     optional_probes = []
53     options = {}
54     annotations = [
55         ['left', 'Left channel'],
56         ['right', 'Right channel'],
57         ['warnings', 'Warnings'],
58     ]
59
60     def __init__(self, **kwargs):
61         self.oldsck = 1
62         self.oldws = 1
63         self.bitcount = 0
64         self.data = 0
65         self.samplesreceived = 0
66         self.first_sample = None
67         self.start_sample = None
68         self.wordlength = -1
69
70     def start(self, metadata):
71         self.samplerate = metadata['samplerate']
72         self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2s')
73         self.out_ann = self.add(srd.OUTPUT_ANN, 'i2s')
74
75     def putpb(self, data):
76         self.put(self.start_sample, self.samplenum, self.out_proto, data)
77
78     def putb(self, data):
79         self.put(self.start_sample, self.samplenum, self.out_ann, data)
80
81     def report(self):
82
83         # Calculate the sample rate.
84         samplerate = '?'
85         if self.start_sample != None and \
86             self.first_sample != None and \
87             self.start_sample > self.first_sample:
88             samplerate = '%d' % (self.samplesreceived *
89                 self.samplerate / (self.start_sample -
90                 self.first_sample))
91
92         return 'I2S: %d %d-bit samples received at %sHz' % \
93             (self.samplesreceived, self.wordlength, samplerate)
94
95     def decode(self, ss, es, data):
96         for self.samplenum, (sck, ws, sd) in data:
97
98             # Ignore sample if the bit clock hasn't changed.
99             if sck == self.oldsck:
100                 continue
101
102             self.oldsck = sck
103             if sck == 0:   # Ignore the falling clock edge.
104                 continue
105
106             self.data = (self.data << 1) | sd
107             self.bitcount += 1
108
109             # This was not the LSB unless WS has flipped.
110             if ws == self.oldws:
111                 continue
112
113             # Only submit the sample, if we received the beginning of it.
114             if self.start_sample != None:
115                 self.samplesreceived += 1
116
117                 idx = 0 if self.oldws else 1
118                 c1 = 'Left channel' if self.oldws else 'Right channel'
119                 c2 = 'Left' if self.oldws else 'Right'
120                 c3 = 'L' if self.oldws else 'R'
121                 v = '%08x' % self.data
122                 self.putpb(['DATA', [c3, self.data]])
123                 self.putb([idx, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
124                                  '%s: %s' % (c3, v), c3]])
125
126                 # Check that the data word was the correct length.
127                 if self.wordlength != -1 and self.wordlength != self.bitcount:
128                     self.putb([2, ['Received %d-bit word, expected %d-bit '
129                                    'word' % (self.bitcount, self.wordlength)]])
130
131                 self.wordlength = self.bitcount
132
133             # Reset decoder state.
134             self.data = 0
135             self.bitcount = 0
136             self.start_sample = self.samplenum
137
138             # Save the first sample position.
139             if self.first_sample == None:
140                 self.first_sample = self.samplenum
141
142             self.oldws = ws
143