]> sigrok.org Git - libsigrokdecode.git/blob - decoders/tdm_audio/pd.py
tdm_audio: Add initial decoder
[libsigrokdecode.git] / decoders / tdm_audio / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2019 Ben Dooks <ben.dooks@codethink.co.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
22 MAX_CHANNELS = 8
23
24 class Decoder(srd.Decoder):
25     api_version = 3
26     id = 'tdm_audio'
27     name = 'TDM audio'
28     longname = 'Time division multiplex audio'
29     desc = 'TDM multi-channel audio protocol.'
30     license = 'gplv2+'
31     inputs = ['logic']
32     outputs = []
33     tags = ['Audio']
34     channels = (
35         { 'id': 'clock', 'name': 'Bitclk', 'desc': 'Data bit clock' },
36         { 'id': 'frame', 'name': 'Framesync', 'desc': 'Frame sync' },
37         { 'id': 'data', 'name': 'Data', 'desc': 'Serial data' },
38     )
39     options = (
40         {'id': 'bps', 'desc': 'Bits per sample', 'default': 16 },
41         {'id': 'channels', 'desc': 'Channels per frame', 'default': MAX_CHANNELS },
42         {'id': 'edge', 'desc': 'Clock edge to sample on', 'default': 'rising', 'values': ('rising', 'falling') }
43     )
44     annotations = tuple(('ch%d' % i, 'Ch%d' % i) for i in range(MAX_CHANNELS))
45
46     def __init__(self):
47         self.reset()
48
49     def reset(self):
50         self.samplerate = None
51         self.channels = MAX_CHANNELS
52         self.channel = 0
53         self.bitdepth = 16
54         self.bitcount = 0
55         self.samplecount = 0
56         self.lastsync = 0
57         self.lastframe = 0
58         self.data = 0
59         self.ss_block = None
60
61     def metdatadata(self, key, value):
62         if key == srd.SRD_CONF_SAMPLERATE:
63             self.samplerate = value
64
65     def start(self):
66         self.out_ann = self.register(srd.OUTPUT_ANN)
67         self.bitdepth = self.options['bps']
68         self.edge = self.options['edge']
69
70     def decode(self):
71         while True:
72             # Wait for edge of clock (sample on rising/falling edge).
73             clock, frame, data = self.wait({0: self.edge[0]})
74
75             self.data = (self.data << 1) | data
76             self.bitcount += 1
77
78             if self.ss_block is not None:
79                 if self.bitcount >= self.bitdepth:
80                     self.bitcount = 0
81                     self.channel += 1
82
83                     c1 = 'Channel %d' % self.channel
84                     c2 = 'C%d' % self.channel
85                     c3 = '%d' % self.channel
86                     if self.bitdepth <= 8:
87                         v = '%02x' % self.data
88                     elif self.bitdepth <= 16:
89                         v = '%04x' % self.data
90                     else:
91                         v = '%08x' % self.data
92
93                     if self.channel < self.channels:
94                         ch = self.channel
95                     else:
96                         ch = 0
97
98                     self.put(self.ss_block, self.samplenum, self.out_ann,
99                              [ch, ['%s: %s' % (c1, v), '%s: %s' % (c2, v),
100                                    '%s: %s' % (c3, v)]])
101                     self.data = 0
102                     self.ss_block = self.samplenum
103                     self.samplecount += 1
104
105             # Check for new frame.
106             # Note, frame may be a single clock, or active for the first
107             # sample in the frame.
108             if frame != self.lastframe and frame == 1:
109                 self.channel = 0
110                 self.bitcount = 0
111                 self.data = 0
112                 if self.ss_block is None:
113                     self.ss_block = 0
114
115             self.lastframe = frame