]> sigrok.org Git - libsigrokdecode.git/blame - decoders/tlc5620/pd.py
Use consistent __init__() format across all PDs.
[libsigrokdecode.git] / decoders / tlc5620 / pd.py
CommitLineData
92d1aba3 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
92d1aba3 3##
ce0c47f3 4## Copyright (C) 2012-2015 Uwe Hermann <uwe@hermann-uwe.de>
92d1aba3
UH
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
92d1aba3
UH
21import sigrokdecode as srd
22
23dacs = {
24 0: 'DACA',
25 1: 'DACB',
26 2: 'DACC',
27 3: 'DACD',
28}
29
30class Decoder(srd.Decoder):
12851357 31 api_version = 2
92d1aba3
UH
32 id = 'tlc5620'
33 name = 'TI TLC5620'
34 longname = 'Texas Instruments TLC5620'
35 desc = 'Texas Instruments TLC5620 8-bit quad DAC.'
36 license = 'gplv2+'
37 inputs = ['logic']
38 outputs = ['tlc5620']
6a15597a 39 channels = (
92d1aba3
UH
40 {'id': 'clk', 'name': 'CLK', 'desc': 'Serial interface clock'},
41 {'id': 'data', 'name': 'DATA', 'desc': 'Serial interface data'},
da9bcbd9 42 )
6a15597a 43 optional_channels = (
92d1aba3
UH
44 {'id': 'load', 'name': 'LOAD', 'desc': 'Serial interface load control'},
45 {'id': 'ldac', 'name': 'LDAC', 'desc': 'Load DAC'},
da9bcbd9 46 )
04c310c8
UH
47 options = (
48 {'id': 'vref_a', 'desc': 'Reference voltage DACA (V)', 'default': 3.3},
49 {'id': 'vref_b', 'desc': 'Reference voltage DACB (V)', 'default': 3.3},
50 {'id': 'vref_c', 'desc': 'Reference voltage DACC (V)', 'default': 3.3},
51 {'id': 'vref_d', 'desc': 'Reference voltage DACD (V)', 'default': 3.3},
52 )
da9bcbd9
BV
53 annotations = (
54 ('dac-select', 'DAC select'),
55 ('gain', 'Gain'),
56 ('value', 'DAC value'),
57 ('data-latch', 'Data latch point'),
58 ('ldac-fall', 'LDAC falling edge'),
ce0c47f3 59 ('bit', 'Bit'),
3992b1e1
UH
60 ('reg-write', 'Register write'),
61 ('voltage-update', 'Voltage update'),
62 ('voltage-update-all', 'Voltage update (all DACs)'),
a945c536 63 ('invalid-cmd', 'Invalid command'),
ce0c47f3
UH
64 )
65 annotation_rows = (
66 ('bits', 'Bits', (5,)),
67 ('fields', 'Fields', (0, 1, 2)),
3992b1e1
UH
68 ('registers', 'Registers', (6, 7)),
69 ('voltage-updates', 'Voltage updates', (8,)),
ce0c47f3 70 ('events', 'Events', (3, 4)),
a945c536 71 ('errors', 'Errors', (9,)),
da9bcbd9 72 )
92d1aba3 73
92b7b49f 74 def __init__(self):
17db4008 75 self.oldpins = self.oldclk = self.oldload = self.oldldac = None
92d1aba3 76 self.bits = []
3992b1e1 77 self.ss_dac_first = None
92d1aba3
UH
78 self.ss_dac = self.es_dac = 0
79 self.ss_gain = self.es_gain = 0
80 self.ss_value = self.es_value = 0
17db4008 81 self.dac_select = self.gain = self.dac_value = None
3992b1e1 82 self.dacval = {'A': '?', 'B': '?', 'C': '?', 'D': '?'}
04c310c8 83 self.gains = {'A': '?', 'B': '?', 'C': '?', 'D': '?'}
92d1aba3 84
8915b346 85 def start(self):
be465111 86 self.out_ann = self.register(srd.OUTPUT_ANN)
92d1aba3 87
92d1aba3 88 def handle_11bits(self):
e57813d5
UH
89 # Only look at the last 11 bits, the rest is ignored by the TLC5620.
90 if len(self.bits) > 11:
91 self.bits = self.bits[-11:]
92
a945c536
UH
93 # If there are less than 11 bits, something is probably wrong.
94 if len(self.bits) < 11:
95 ss, es = self.samplenum, self.samplenum
96 if len(self.bits) >= 2:
97 ss = self.bits[0][1]
98 es = self.bits[-1][1] + (self.bits[1][1] - self.bits[0][1])
99 self.put(ss, es, self.out_ann, [9, ['Command too short']])
100 self.bits = []
101 return False
102
e57813d5
UH
103 self.ss_dac = self.bits[0][1]
104 self.es_dac = self.ss_gain = self.bits[2][1]
105 self.es_gain = self.ss_value = self.bits[3][1]
106 self.clock_width = self.es_gain - self.ss_gain
107 self.es_value = self.bits[10][1] + self.clock_width # Guessed.
108
3992b1e1
UH
109 if self.ss_dac_first is None:
110 self.ss_dac_first = self.ss_dac
111
ce0c47f3 112 s = ''.join(str(i[0]) for i in self.bits[:2])
7fb4935e 113 self.dac_select = s = dacs[int(s, 2)]
92d1aba3 114 self.put(self.ss_dac, self.es_dac, self.out_ann,
7fb4935e
UH
115 [0, ['DAC select: %s' % s, 'DAC sel: %s' % s,
116 'DAC: %s' % s, 'D: %s' % s, s, s[3]]])
92d1aba3 117
ce0c47f3 118 self.gain = g = 1 + self.bits[2][0]
92d1aba3 119 self.put(self.ss_gain, self.es_gain, self.out_ann,
7fb4935e 120 [1, ['Gain: x%d' % g, 'G: x%d' % g, 'x%d' % g]])
92d1aba3 121
ce0c47f3 122 s = ''.join(str(i[0]) for i in self.bits[3:])
7fb4935e 123 self.dac_value = v = int(s, 2)
92d1aba3 124 self.put(self.ss_value, self.es_value, self.out_ann,
7fb4935e
UH
125 [2, ['DAC value: %d' % v, 'Value: %d' % v, 'Val: %d' % v,
126 'V: %d' % v, '%d' % v]])
17db4008 127
ce0c47f3
UH
128 # Emit an annotation for each bit.
129 for i in range(1, 11):
130 self.put(self.bits[i - 1][1], self.bits[i][1], self.out_ann,
131 [5, [str(self.bits[i - 1][0])]])
132 self.put(self.bits[10][1], self.bits[10][1] + self.clock_width,
133 self.out_ann, [5, [str(self.bits[10][0])]])
134
e57813d5
UH
135 self.bits = []
136
a945c536
UH
137 return True
138
17db4008 139 def handle_falling_edge_load(self):
a945c536
UH
140 if not self.handle_11bits():
141 return
7fb4935e 142 s, v, g = self.dac_select, self.dac_value, self.gain
17db4008 143 self.put(self.samplenum, self.samplenum, self.out_ann,
ce0c47f3 144 [3, ['Falling edge on LOAD', 'LOAD fall', 'F']])
04c310c8
UH
145 vref = self.options['vref_%s' % self.dac_select[3].lower()]
146 v = '%.2fV' % (vref * (v / 256) * self.gain)
3992b1e1
UH
147 if self.ldac == 0:
148 # If LDAC is low, the voltage is set immediately.
149 self.put(self.ss_dac, self.es_value, self.out_ann,
04c310c8
UH
150 [7, ['Setting %s voltage to %s' % (s, v),
151 '%s=%s' % (s, v)]])
3992b1e1
UH
152 else:
153 # If LDAC is high, the voltage is not set immediately, but rather
154 # stored in a register. When LDAC goes low all four DAC voltages
155 # (DAC A/B/C/D) will be set at the same time.
156 self.put(self.ss_dac, self.es_value, self.out_ann,
04c310c8
UH
157 [6, ['Setting %s register value to %s' % \
158 (s, v), '%s=%s' % (s, v)]])
3992b1e1
UH
159 # Save the last value the respective DAC was set to.
160 self.dacval[self.dac_select[-1]] = str(self.dac_value)
04c310c8 161 self.gains[self.dac_select[-1]] = self.gain
17db4008
UH
162
163 def handle_falling_edge_ldac(self):
164 self.put(self.samplenum, self.samplenum, self.out_ann,
ce0c47f3 165 [4, ['Falling edge on LDAC', 'LDAC fall', 'LDAC', 'L']])
17db4008 166
3992b1e1
UH
167 # Don't emit any annotations if we didn't see any register writes.
168 if self.ss_dac_first is None:
169 return
170
04c310c8
UH
171 # Calculate voltages based on Vref and the per-DAC gain.
172 dacval = {}
173 for key, val in self.dacval.items():
174 if val == '?':
175 dacval[key] = '?'
176 else:
177 vref = self.options['vref_%s' % key.lower()]
178 v = vref * (int(val) / 256) * self.gains[key]
179 dacval[key] = '%.2fV' % v
180
181 s = ''.join(['DAC%s=%s ' % (d, dacval[d]) for d in 'ABCD']).strip()
3992b1e1
UH
182 self.put(self.ss_dac_first, self.samplenum, self.out_ann,
183 [8, ['Updating voltages: %s' % s, s, s.replace('DAC', '')]])
184 self.ss_dac_first = None
185
17db4008 186 def handle_new_dac_bit(self):
ce0c47f3 187 self.bits.append([self.datapin, self.samplenum])
17db4008 188
92d1aba3
UH
189 def decode(self, ss, es, data):
190 for (self.samplenum, pins) in data:
191
192 # Ignore identical samples early on (for performance reasons).
193 if self.oldpins == pins:
194 continue
17db4008 195 self.oldpins, (clk, self.datapin, load, ldac) = pins, pins
3992b1e1 196 self.ldac = ldac
92d1aba3
UH
197
198 # DATA is shifted in the DAC on the falling CLK edge (MSB-first).
17db4008 199 # A falling edge of LOAD will latch the data.
92d1aba3 200
17db4008
UH
201 if self.oldload == 1 and load == 0:
202 self.handle_falling_edge_load()
203 if self.oldldac == 1 and ldac == 0:
204 self.handle_falling_edge_ldac()
205 if self.oldclk == 1 and clk == 0:
206 self.handle_new_dac_bit()
92d1aba3
UH
207
208 self.oldclk = clk
17db4008
UH
209 self.oldload = load
210 self.oldldac = ldac