summaryrefslogtreecommitdiff
path: root/decoders/i2s/i2s.py
blob: 23f625d3a54f6e2fded6aa56307e83cf972cd469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
##
## This file is part of the sigrok project.
##
## Copyright (C) 2012 Joel Holdsworth <joel@airwebreathe.org.uk>
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, write to the Free Software
## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
##

# I2S protocol decoder

import sigrokdecode as srd

# Annotation formats
ANN_HEX = 0

class Decoder(srd.Decoder):
    api_version = 1
    id = 'i2s'
    name = 'I2S'
    longname = 'Integrated Interchip Sound'
    desc = 'I2S is an electrical serial bus interface standard used ' \
        'for connecting digital audio devices together.'
    license = 'gplv2+'
    inputs = ['logic']
    outputs = ['i2s']
    probes = [
        {'id': 'sck', 'name': 'SCK', 'desc': 'Bit clock line'},
        {'id': 'ws', 'name': 'WS', 'desc': 'Word select line'},
        {'id': 'sd', 'name': 'SD', 'desc': 'Serial Data line'},
    ]
    annotations = [
        ['ASCII', 'Annotations in ASCII format'],
    ]

    def __init__(self, **kwargs):
        self.oldsck = 1
        self.oldws = 1
        self.bitcount = 0
        self.data = 0
        self.samplesreceived = 0
        self.start_sample = None
        self.samplenum = -1
        self.wordlength = -1

    def start(self, metadata):
        self.out_proto = self.add(srd.OUTPUT_PROTO, 'i2s')
        self.out_ann = self.add(srd.OUTPUT_ANN, 'i2s')

    def report(self):
        return 'I2S: %d samples received' % self.samplesreceived

    def decode(self, ss, es, data):
        for samplenum, (sck, ws, sd) in data:

            # Ignore sample if the bit clock hasn't changed.
            if sck == self.oldsck:
                continue

            self.oldsck = sck
            if sck == 0:   # Ignore the falling clock edge
                continue

            self.data = (self.data << 1) | sd
            self.bitcount += 1

            # This was not the LSB unless WS has flipped
            if ws == self.oldws:
                continue

            # Only submit the sample, if we received the beginning of it
            if self.start_sample != None:
                self.samplesreceived += 1
                self.put(self.start_sample, self.samplenum, self.out_proto,
                    ['data', self.data])
                self.put(self.start_sample, self.samplenum, self.out_ann,
                    [ANN_HEX, ['%s: 0x%08x' % ('L' if self.oldws else 'R',
                   self.data)]])

                # Check that the data word was the correct length
                if self.wordlength != -1 and self.wordlength != self.bitcount:
                    self.put(self.start_sample, self.samplenum, self.out_ann,
                        [ANN_HEX, ['WARNING: Received a %d-bit word, when a '
                        '%d-bit word was expected' % (self.bitcount,
                        self.wordlength)]])

                self.wordlength = self.bitcount

            # Reset decoder state.
            self.data = 0
            self.bitcount = 0
            self.start_sample = self.samplenum
                
            self.oldws = ws