]> sigrok.org Git - libsigrokdecode.git/blame - decoders/tlc5620/pd.py
tlc5620: Handle the case of less than 11 bits in a command.
[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
BV
46 )
47 annotations = (
48 ('dac-select', 'DAC select'),
49 ('gain', 'Gain'),
50 ('value', 'DAC value'),
51 ('data-latch', 'Data latch point'),
52 ('ldac-fall', 'LDAC falling edge'),
ce0c47f3 53 ('bit', 'Bit'),
3992b1e1
UH
54 ('reg-write', 'Register write'),
55 ('voltage-update', 'Voltage update'),
56 ('voltage-update-all', 'Voltage update (all DACs)'),
a945c536 57 ('invalid-cmd', 'Invalid command'),
ce0c47f3
UH
58 )
59 annotation_rows = (
60 ('bits', 'Bits', (5,)),
61 ('fields', 'Fields', (0, 1, 2)),
3992b1e1
UH
62 ('registers', 'Registers', (6, 7)),
63 ('voltage-updates', 'Voltage updates', (8,)),
ce0c47f3 64 ('events', 'Events', (3, 4)),
a945c536 65 ('errors', 'Errors', (9,)),
da9bcbd9 66 )
92d1aba3
UH
67
68 def __init__(self, **kwargs):
17db4008 69 self.oldpins = self.oldclk = self.oldload = self.oldldac = None
92d1aba3 70 self.bits = []
3992b1e1 71 self.ss_dac_first = None
92d1aba3
UH
72 self.ss_dac = self.es_dac = 0
73 self.ss_gain = self.es_gain = 0
74 self.ss_value = self.es_value = 0
17db4008 75 self.dac_select = self.gain = self.dac_value = None
3992b1e1 76 self.dacval = {'A': '?', 'B': '?', 'C': '?', 'D': '?'}
92d1aba3 77
8915b346 78 def start(self):
be465111 79 self.out_ann = self.register(srd.OUTPUT_ANN)
92d1aba3 80
92d1aba3 81 def handle_11bits(self):
e57813d5
UH
82 # Only look at the last 11 bits, the rest is ignored by the TLC5620.
83 if len(self.bits) > 11:
84 self.bits = self.bits[-11:]
85
a945c536
UH
86 # If there are less than 11 bits, something is probably wrong.
87 if len(self.bits) < 11:
88 ss, es = self.samplenum, self.samplenum
89 if len(self.bits) >= 2:
90 ss = self.bits[0][1]
91 es = self.bits[-1][1] + (self.bits[1][1] - self.bits[0][1])
92 self.put(ss, es, self.out_ann, [9, ['Command too short']])
93 self.bits = []
94 return False
95
e57813d5
UH
96 self.ss_dac = self.bits[0][1]
97 self.es_dac = self.ss_gain = self.bits[2][1]
98 self.es_gain = self.ss_value = self.bits[3][1]
99 self.clock_width = self.es_gain - self.ss_gain
100 self.es_value = self.bits[10][1] + self.clock_width # Guessed.
101
3992b1e1
UH
102 if self.ss_dac_first is None:
103 self.ss_dac_first = self.ss_dac
104
ce0c47f3 105 s = ''.join(str(i[0]) for i in self.bits[:2])
7fb4935e 106 self.dac_select = s = dacs[int(s, 2)]
92d1aba3 107 self.put(self.ss_dac, self.es_dac, self.out_ann,
7fb4935e
UH
108 [0, ['DAC select: %s' % s, 'DAC sel: %s' % s,
109 'DAC: %s' % s, 'D: %s' % s, s, s[3]]])
92d1aba3 110
ce0c47f3 111 self.gain = g = 1 + self.bits[2][0]
92d1aba3 112 self.put(self.ss_gain, self.es_gain, self.out_ann,
7fb4935e 113 [1, ['Gain: x%d' % g, 'G: x%d' % g, 'x%d' % g]])
92d1aba3 114
ce0c47f3 115 s = ''.join(str(i[0]) for i in self.bits[3:])
7fb4935e 116 self.dac_value = v = int(s, 2)
92d1aba3 117 self.put(self.ss_value, self.es_value, self.out_ann,
7fb4935e
UH
118 [2, ['DAC value: %d' % v, 'Value: %d' % v, 'Val: %d' % v,
119 'V: %d' % v, '%d' % v]])
17db4008 120
ce0c47f3
UH
121 # Emit an annotation for each bit.
122 for i in range(1, 11):
123 self.put(self.bits[i - 1][1], self.bits[i][1], self.out_ann,
124 [5, [str(self.bits[i - 1][0])]])
125 self.put(self.bits[10][1], self.bits[10][1] + self.clock_width,
126 self.out_ann, [5, [str(self.bits[10][0])]])
127
e57813d5
UH
128 self.bits = []
129
a945c536
UH
130 return True
131
17db4008 132 def handle_falling_edge_load(self):
a945c536
UH
133 if not self.handle_11bits():
134 return
7fb4935e 135 s, v, g = self.dac_select, self.dac_value, self.gain
17db4008 136 self.put(self.samplenum, self.samplenum, self.out_ann,
ce0c47f3 137 [3, ['Falling edge on LOAD', 'LOAD fall', 'F']])
3992b1e1
UH
138 if self.ldac == 0:
139 # If LDAC is low, the voltage is set immediately.
140 self.put(self.ss_dac, self.es_value, self.out_ann,
141 [7, ['Setting %s voltage to %d (x%d gain)' % (s, v, g),
142 '%s=%d (x%d gain)' % (s, v, g)]])
143 else:
144 # If LDAC is high, the voltage is not set immediately, but rather
145 # stored in a register. When LDAC goes low all four DAC voltages
146 # (DAC A/B/C/D) will be set at the same time.
147 self.put(self.ss_dac, self.es_value, self.out_ann,
148 [6, ['Setting %s register value to %d (x%d gain)' % \
149 (s, v, g), '%s=%d (x%d gain)' % (s, v, g)]])
150 # Save the last value the respective DAC was set to.
151 self.dacval[self.dac_select[-1]] = str(self.dac_value)
17db4008
UH
152
153 def handle_falling_edge_ldac(self):
154 self.put(self.samplenum, self.samplenum, self.out_ann,
ce0c47f3 155 [4, ['Falling edge on LDAC', 'LDAC fall', 'LDAC', 'L']])
17db4008 156
3992b1e1
UH
157 # Don't emit any annotations if we didn't see any register writes.
158 if self.ss_dac_first is None:
159 return
160
161 s = ''.join(['DAC%s=%s ' % (d, self.dacval[d]) for d in 'ABCD']).strip()
162 self.put(self.ss_dac_first, self.samplenum, self.out_ann,
163 [8, ['Updating voltages: %s' % s, s, s.replace('DAC', '')]])
164 self.ss_dac_first = None
165
17db4008 166 def handle_new_dac_bit(self):
ce0c47f3 167 self.bits.append([self.datapin, self.samplenum])
17db4008 168
92d1aba3
UH
169 def decode(self, ss, es, data):
170 for (self.samplenum, pins) in data:
171
172 # Ignore identical samples early on (for performance reasons).
173 if self.oldpins == pins:
174 continue
17db4008 175 self.oldpins, (clk, self.datapin, load, ldac) = pins, pins
3992b1e1 176 self.ldac = ldac
92d1aba3
UH
177
178 # DATA is shifted in the DAC on the falling CLK edge (MSB-first).
17db4008 179 # A falling edge of LOAD will latch the data.
92d1aba3 180
17db4008
UH
181 if self.oldload == 1 and load == 0:
182 self.handle_falling_edge_load()
183 if self.oldldac == 1 and ldac == 0:
184 self.handle_falling_edge_ldac()
185 if self.oldclk == 1 and clk == 0:
186 self.handle_new_dac_bit()
92d1aba3
UH
187
188 self.oldclk = clk
17db4008
UH
189 self.oldload = load
190 self.oldldac = ldac