2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2020 Analog Devices Inc.
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 3 of the License, or
9 ## (at your option) any later version.
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.
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/>.
20 import sigrokdecode as srd
23 0x00: ['GND', 'GND', 'GND', 'G'],
24 0x01: ['FLOAT', 'FLOAT', 'FLOAT', 'F'],
25 0x02: ['VCC', 'VCC', 'VCC', 'V'],
29 0x00: ['Write Input Register', 'Write In Reg', 'Wr In Reg', 'WIR'],
30 0x01: ['Update DAC', 'Update', 'U'],
31 0x03: ['Write and Power Up DAC', 'Write & Power Up', 'W&PU'],
32 0x04: ['Power Down DAC', 'Power Down', 'PD'],
33 0x0F: ['No Operation', 'No Op', 'NO'],
39 0x0F: ['All DACs', 'All'],
42 input_voltage_format = ['%.6fV', '%.2fV']
44 class Decoder(srd.Decoder):
48 longname = 'Linear Technology LTC26x7'
49 desc = 'Linear Technology LTC26x7 16-/14-/12-bit rail-to-rail DACs.'
53 tags = ['IC', 'Analog/digital']
55 {'id': 'chip', 'desc': 'Chip', 'default': 'ltc2607',
56 'values': ('ltc2607', 'ltc2617', 'ltc2627')},
57 {'id': 'vref', 'desc': 'Reference voltage (V)', 'default': 1.5},
60 ('slave_addr', 'Slave address'),
61 ('command', 'Command'),
62 ('address', 'Address'),
63 ('dac_a_voltage', 'DAC A voltage'),
64 ('dac_b_voltage', 'DAC B voltage'),
67 ('addr_cmd', 'Address/command', (0, 1, 2)),
68 ('dac_a_voltages', 'DAC A voltages', (3,)),
69 ('dac_b_voltages', 'DAC B voltages', (4,)),
82 self.out_ann = self.register(srd.OUTPUT_ANN)
84 def convert_ternary_str(self, n):
93 return list(reversed(nums))
95 def handle_slave_addr(self, data):
97 ann = ['Global address', 'Global addr', 'Glob addr', 'GA']
98 self.put(self.ss, self.es, self.out_ann, [0, ann])
100 ann = ['CA2=%s CA1=%s CA0=%s', '2=%s 1=%s 0=%s', '%s %s %s', '%s %s %s']
114 ternary_values = self.convert_ternary_str(addr)
115 for i in range(len(ann)):
116 ann[i] = ann[i] % (slave_address[ternary_values[0]][i],
117 slave_address[ternary_values[1]][i],
118 slave_address[ternary_values[2]][i])
119 self.put(self.ss, self.es, self.out_ann, [0, ann])
121 def handle_cmd_addr(self, data):
122 cmd_val = (data >> 4) & 0x0F
123 self.dac_val = (data & 0x0F)
124 sm = (self.ss + self.es) // 2
126 self.put(self.ss, sm, self.out_ann, [1, commands[cmd_val]])
127 self.put(sm, self.es, self.out_ann, [2, addresses[self.dac_val]])
129 def handle_data(self, data):
130 self.data = (self.data << 8) & 0xFF00
132 if self.options['chip'] == 'ltc2617':
133 self.data = (self.data >> 2)
134 self.data = (self.options['vref'] * self.data) / 0x3FFF
135 elif self.options['chip'] == 'ltc2627':
136 self.data = (self.data >> 4)
137 self.data = (self.options['vref'] * self.data) / 0x0FFF
139 self.data = (self.options['vref'] * self.data) / 0xFFFF
141 for format in input_voltage_format:
142 ann.append(format % self.data)
145 if self.dac_val == 0x0F: # All DACs (A and B).
146 self.put(self.ss, self.es, self.out_ann, [3 + 0, ann])
147 self.put(self.ss, self.es, self.out_ann, [3 + 1, ann])
149 self.put(self.ss, self.es, self.out_ann, [3 + self.dac_val, ann])
151 def decode(self, ss, es, data):
156 if self.state == 'IDLE':
157 # Wait for an I²C START condition.
160 self.state = 'GET SLAVE ADDR'
161 elif self.state == 'GET SLAVE ADDR':
162 # Wait for an address write operation.
163 if cmd != 'ADDRESS WRITE':
166 self.handle_slave_addr(databyte)
168 self.state = 'GET CMD ADDR'
169 elif self.state == 'GET CMD ADDR':
170 if cmd != 'DATA WRITE':
173 self.handle_cmd_addr(databyte)
175 self.state = 'WRITE DATA'
176 elif self.state == 'WRITE DATA':
177 if cmd == 'DATA WRITE':
182 self.handle_data(databyte)