]> sigrok.org Git - libsigrokdecode.git/blame - decoders/tlc5620/pd.py
Rename inter-PD output type to SRD_OUTPUT_PYTHON
[libsigrokdecode.git] / decoders / tlc5620 / pd.py
CommitLineData
92d1aba3 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
92d1aba3
UH
3##
4## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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# Texas Instruments TLC5620 protocol decoder
22
23import sigrokdecode as srd
24
25dacs = {
26 0: 'DACA',
27 1: 'DACB',
28 2: 'DACC',
29 3: 'DACD',
30}
31
32class Decoder(srd.Decoder):
33 api_version = 1
34 id = 'tlc5620'
35 name = 'TI TLC5620'
36 longname = 'Texas Instruments TLC5620'
37 desc = 'Texas Instruments TLC5620 8-bit quad DAC.'
38 license = 'gplv2+'
39 inputs = ['logic']
40 outputs = ['tlc5620']
41 probes = [
42 {'id': 'clk', 'name': 'CLK', 'desc': 'Serial interface clock'},
43 {'id': 'data', 'name': 'DATA', 'desc': 'Serial interface data'},
44 ]
45 optional_probes = [
46 {'id': 'load', 'name': 'LOAD', 'desc': 'Serial interface load control'},
47 {'id': 'ldac', 'name': 'LDAC', 'desc': 'Load DAC'},
48 ]
49 options = {}
50 annotations = [
5f4de613
UH
51 ['dac_select', 'DAC select'],
52 ['gain', 'Gain'],
53 ['value', 'DAC value'],
54 ['data_latch', 'Data latch point'],
55 ['ldac_fall', 'LDAC falling edge'],
92d1aba3
UH
56 ]
57
58 def __init__(self, **kwargs):
17db4008
UH
59 self.oldpins = self.oldclk = self.oldload = self.oldldac = None
60 self.datapin = None
92d1aba3
UH
61 self.bits = []
62 self.ss_dac = self.es_dac = 0
63 self.ss_gain = self.es_gain = 0
64 self.ss_value = self.es_value = 0
17db4008 65 self.dac_select = self.gain = self.dac_value = None
92d1aba3 66
8915b346 67 def start(self):
f2a5df42 68 # self.out_proto = self.add(srd.OUTPUT_PYTHON, 'tlc5620')
92d1aba3
UH
69 self.out_ann = self.add(srd.OUTPUT_ANN, 'tlc5620')
70
71 def report(self):
72 pass
73
74 def handle_11bits(self):
75 s = "".join(str(i) for i in self.bits[:2])
7fb4935e 76 self.dac_select = s = dacs[int(s, 2)]
92d1aba3 77 self.put(self.ss_dac, self.es_dac, self.out_ann,
7fb4935e
UH
78 [0, ['DAC select: %s' % s, 'DAC sel: %s' % s,
79 'DAC: %s' % s, 'D: %s' % s, s, s[3]]])
92d1aba3 80
7fb4935e 81 self.gain = g = 1 + self.bits[2]
92d1aba3 82 self.put(self.ss_gain, self.es_gain, self.out_ann,
7fb4935e 83 [1, ['Gain: x%d' % g, 'G: x%d' % g, 'x%d' % g]])
92d1aba3
UH
84
85 s = "".join(str(i) for i in self.bits[3:])
7fb4935e 86 self.dac_value = v = int(s, 2)
92d1aba3 87 self.put(self.ss_value, self.es_value, self.out_ann,
7fb4935e
UH
88 [2, ['DAC value: %d' % v, 'Value: %d' % v, 'Val: %d' % v,
89 'V: %d' % v, '%d' % v]])
17db4008
UH
90
91 def handle_falling_edge_load(self):
7fb4935e 92 s, v, g = self.dac_select, self.dac_value, self.gain
17db4008 93 self.put(self.samplenum, self.samplenum, self.out_ann,
7fb4935e
UH
94 [3, ['Setting %s value to %d (x%d gain)' % (s, v, g),
95 '%s=%d (x%d gain)' % (s, v, g)]])
17db4008
UH
96
97 def handle_falling_edge_ldac(self):
98 self.put(self.samplenum, self.samplenum, self.out_ann,
7fb4935e 99 [4, ['Falling edge on LDAC pin', 'LDAC fall', 'LDAC']])
17db4008
UH
100
101 def handle_new_dac_bit(self):
102 self.bits.append(self.datapin)
103
104 # Wait until we have read 11 bits, then parse them.
105 l, s = len(self.bits), self.samplenum
106 if l == 1:
107 self.ss_dac = s
108 elif l == 2:
109 self.es_dac = self.ss_gain = s
110 elif l == 3:
111 self.es_gain = self.ss_value = s
112 elif l == 11:
113 self.es_value = s
114 self.handle_11bits()
115 self.bits = []
92d1aba3
UH
116
117 def decode(self, ss, es, data):
118 for (self.samplenum, pins) in data:
119
120 # Ignore identical samples early on (for performance reasons).
121 if self.oldpins == pins:
122 continue
17db4008 123 self.oldpins, (clk, self.datapin, load, ldac) = pins, pins
92d1aba3
UH
124
125 # DATA is shifted in the DAC on the falling CLK edge (MSB-first).
17db4008 126 # A falling edge of LOAD will latch the data.
92d1aba3 127
17db4008
UH
128 if self.oldload == 1 and load == 0:
129 self.handle_falling_edge_load()
130 if self.oldldac == 1 and ldac == 0:
131 self.handle_falling_edge_ldac()
132 if self.oldclk == 1 and clk == 0:
133 self.handle_new_dac_bit()
92d1aba3
UH
134
135 self.oldclk = clk
17db4008
UH
136 self.oldload = load
137 self.oldldac = ldac
92d1aba3 138