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