]>
Commit | Line | Data |
---|---|---|
1d4fe1c1 JH |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2017 Joel Holdsworth <joel@airwebreathe.org.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 3 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 | |
317eaa7f | 21 | from common.srdhelper import bitpack_lsb |
1d4fe1c1 JH |
22 | |
23 | def disabled_enabled(v): | |
24 | return ['Disabled', 'Enabled'][v] | |
25 | ||
26 | def output_power(v): | |
3851b0c0 | 27 | return '{:+d}dBm'.format([-4, -1, 2, 5][v]) |
1d4fe1c1 | 28 | |
4d86276d | 29 | # Notes on the implementation: |
a6ec0143 GS |
30 | # - A register's description is an iterable of tuples which contain: |
31 | # The starting bit position, the bit count, the name of a field, and | |
32 | # an optional parser which interprets the field's content. Parser are | |
33 | # expected to yield a single text string when they exist. Other types | |
34 | # of output are passed to Python's .format() routine as is. | |
35 | # - TODO Add support for the creation of formatted values, as well as | |
36 | # optional warnings from register field parsers? The current approach | |
37 | # lets invalid register content go unnoticed. | |
4d86276d GS |
38 | # - Bit fields' width in registers determines the range of indices in |
39 | # table/tuple lookups. Keep the implementation as robust as possible | |
40 | # during future maintenance. Avoid Python runtime errors when adjusting | |
41 | # the decoder. | |
1d4fe1c1 | 42 | regs = { |
a6ec0143 GS |
43 | # Register description fields: |
44 | # offset, width, name, parser. | |
45 | 0: ( | |
46 | ( 3, 12, 'FRAC'), | |
47 | # Lower limit is 23 not 32? | |
48 | (15, 16, 'INT', lambda v: 'Not Allowed' if v < 32 else v), | |
49 | ), | |
50 | 1: ( | |
51 | ( 3, 12, 'MOD'), | |
52 | (15, 12, 'Phase'), | |
53 | (27, 1, 'Prescalar', lambda v: ('4/5', '8/9',)[v]), | |
54 | (28, 1, 'Phase Adjust', lambda v: ('Off', 'On',)[v]), | |
55 | ), | |
56 | 2: ( | |
57 | ( 3, 1, 'Counter Reset', disabled_enabled), | |
58 | ( 4, 1, 'Charge Pump Three-State', disabled_enabled), | |
59 | ( 5, 1, 'Power-Down', disabled_enabled), | |
60 | ( 6, 1, 'PD Polarity', lambda v: ('Negative', 'Positive',)[v]), | |
61 | ( 7, 1, 'LDP', lambda v: ('10ns', '6ns',)[v]), | |
62 | ( 8, 1, 'LDF', lambda v: ('FRAC-N', 'INT-N',)[v]), | |
63 | ( 9, 4, 'Charge Pump Current Setting', | |
64 | lambda v: '{curr:0.2f}mA @ 5.1kΩ'.format(curr = ( | |
4d86276d GS |
65 | 0.31, 0.63, 0.94, 1.25, 1.56, 1.88, 2.19, 2.50, |
66 | 2.81, 3.13, 3.44, 3.75, 4.06, 4.38, 4.69, 5.00, | |
67 | )[v])), | |
a6ec0143 GS |
68 | (13, 1, 'Double Buffer', disabled_enabled), |
69 | (14, 10, 'R Counter'), | |
70 | (24, 1, 'RDIV2', disabled_enabled), | |
71 | (25, 1, 'Reference Doubler', disabled_enabled), | |
72 | (26, 3, 'MUXOUT', | |
73 | lambda v: '{text}'.format(text = ( | |
4d86276d GS |
74 | 'Three-State Output', 'DVdd', 'DGND', |
75 | 'R Counter Output', 'N Divider Output', | |
76 | 'Analog Lock Detect', 'Digital Lock Detect', | |
a6ec0143 | 77 | 'Reserved', |
4d86276d | 78 | )[v])), |
a6ec0143 GS |
79 | (29, 2, 'Low Noise and Low Spur Modes', |
80 | lambda v: '{text}'.format(text = ( | |
81 | 'Low Noise Mode', 'Reserved', 'Reserved', 'Low Spur Mode', | |
4d86276d | 82 | )[v])), |
a6ec0143 GS |
83 | ), |
84 | 3: ( | |
85 | ( 3, 12, 'Clock Divider'), | |
86 | (15, 2, 'Clock Divider Mode', | |
87 | lambda v: '{text}'.format(text = ( | |
88 | 'Clock Divider Off', 'Fast Lock Enable', | |
89 | 'Resync Enable', 'Reserved', | |
4d86276d | 90 | )[v])), |
a6ec0143 GS |
91 | (18, 1, 'CSR Enable', disabled_enabled), |
92 | (21, 1, 'Charge Cancellation', disabled_enabled), | |
93 | (22, 1, 'ABP', lambda v: ('6ns (FRAC-N)', '3ns (INT-N)',)[v]), | |
94 | (23, 1, 'Band Select Clock Mode', lambda v: ('Low', 'High',)[v]), | |
95 | ), | |
96 | 4: ( | |
97 | ( 3, 2, 'Output Power', output_power), | |
98 | ( 5, 1, 'Output Enable', disabled_enabled), | |
99 | ( 6, 2, 'AUX Output Power', output_power), | |
100 | ( 8, 1, 'AUX Output Select', | |
101 | lambda v: ('Divided Output', 'Fundamental',)[v]), | |
102 | ( 9, 1, 'AUX Output Enable', disabled_enabled), | |
103 | (10, 1, 'MTLD', disabled_enabled), | |
104 | (11, 1, 'VCO Power-Down', | |
105 | lambda v: 'VCO Powered {ud}'.format(ud = 'Down' if v else 'Up')), | |
106 | (12, 8, 'Band Select Clock Divider'), | |
107 | (20, 3, 'RF Divider Select', lambda v: '÷{:d}'.format(2 ** v)), | |
108 | (23, 1, 'Feedback Select', lambda v: ('Divided', 'Fundamental',)[v]), | |
109 | ), | |
110 | 5: ( | |
111 | (22, 2, 'LD Pin Mode', | |
112 | lambda v: '{text}'.format(text = ( | |
4d86276d GS |
113 | 'Low', 'Digital Lock Detect', 'Low', 'High', |
114 | )[v])), | |
a6ec0143 | 115 | ), |
1d4fe1c1 JH |
116 | } |
117 | ||
118 | ANN_REG = 0 | |
f534ce44 | 119 | ANN_WARN = 1 |
1d4fe1c1 JH |
120 | |
121 | class Decoder(srd.Decoder): | |
b197383c | 122 | api_version = 3 |
1d4fe1c1 JH |
123 | id = 'adf435x' |
124 | name = 'ADF435x' | |
125 | longname = 'Analog Devices ADF4350/1' | |
126 | desc = 'Wideband synthesizer with integrated VCO.' | |
127 | license = 'gplv3+' | |
128 | inputs = ['spi'] | |
6cbba91f | 129 | outputs = [] |
d6d8a8a4 | 130 | tags = ['Clock/timing', 'IC', 'Wireless/RF'] |
1d4fe1c1 JH |
131 | annotations = ( |
132 | # Sent from the host to the chip. | |
e144452b | 133 | ('write', 'Register write'), |
f534ce44 | 134 | ('warning', "Warnings"), |
1d4fe1c1 JH |
135 | ) |
136 | annotation_rows = ( | |
e144452b | 137 | ('writes', 'Register writes', (ANN_REG,)), |
f534ce44 | 138 | ('warnings', 'Warnings', (ANN_WARN,)), |
1d4fe1c1 JH |
139 | ) |
140 | ||
141 | def __init__(self): | |
10aeb8ea GS |
142 | self.reset() |
143 | ||
144 | def reset(self): | |
1d4fe1c1 JH |
145 | self.bits = [] |
146 | ||
147 | def start(self): | |
148 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
149 | ||
e5027632 GS |
150 | def putg(self, ss, es, cls, data): |
151 | self.put(ss, es, self.out_ann, [ cls, data, ]) | |
152 | ||
1d4fe1c1 | 153 | def decode_bits(self, offset, width): |
c4aaef25 | 154 | '''Extract a bit field. Expects LSB input data.''' |
317eaa7f GS |
155 | bits = self.bits[offset:][:width] |
156 | ss, es = bits[-1][1], bits[0][2] | |
157 | value = bitpack_lsb(bits, 0) | |
158 | return ( value, ( ss, es, )) | |
1d4fe1c1 | 159 | |
a6ec0143 | 160 | def decode_field(self, name, offset, width, parser = None): |
c4aaef25 | 161 | '''Interpret a bit field. Emits an annotation.''' |
a6ec0143 | 162 | # Get the register field's content and position. |
e5027632 | 163 | val, ( ss, es, ) = self.decode_bits(offset, width) |
a6ec0143 GS |
164 | # Have the field's content formatted, emit an annotation. |
165 | formatted = parser(val) if parser else '{}'.format(val) | |
166 | if formatted is not None: | |
167 | text = ['{name}: {val}'.format(name = name, val = formatted)] | |
168 | else: | |
169 | text = ['{name}'.format(name = name)] | |
170 | if text: | |
171 | self.putg(ss, es, ANN_REG, text) | |
c4aaef25 GS |
172 | |
173 | def decode_word(self, ss, es, bits): | |
174 | '''Interpret a 32bit word after accumulation completes.''' | |
175 | # SPI transfer content must be exactly one 32bit word. | |
3851b0c0 GS |
176 | count = len(self.bits) |
177 | if count != 32: | |
c4aaef25 | 178 | text = [ |
3851b0c0 | 179 | 'Frame error: Bit count: want 32, got {}'.format(count), |
c4aaef25 GS |
180 | 'Frame error: Bit count', |
181 | 'Frame error', | |
182 | ] | |
183 | self.putg(ss, es, ANN_WARN, text) | |
184 | return | |
185 | # Holding bits in LSB order during interpretation simplifies | |
186 | # bit field extraction. And annotation emitting routines expect | |
187 | # this reverse order of bits' timestamps. | |
188 | self.bits.reverse() | |
189 | # Determine which register was accessed. | |
190 | reg_addr, ( reg_ss, reg_es, ) = self.decode_bits(0, 3) | |
191 | text = [ | |
3851b0c0 GS |
192 | 'Register: {addr}'.format(addr = reg_addr), |
193 | 'Reg: {addr}'.format(addr = reg_addr), | |
194 | '[{addr}]'.format(addr = reg_addr), | |
c4aaef25 GS |
195 | ] |
196 | self.putg(reg_ss, reg_es, ANN_REG, text) | |
197 | # Interpret the register's content (when parsers are available). | |
198 | field_descs = regs.get(reg_addr, None) | |
199 | if not field_descs: | |
200 | return | |
201 | for field_desc in field_descs: | |
a6ec0143 GS |
202 | parser = None |
203 | if len(field_desc) == 3: | |
204 | start, count, name, = field_desc | |
205 | elif len(field_desc) == 4: | |
206 | start, count, name, parser = field_desc | |
207 | else: | |
208 | # Unsupported regs{} syntax, programmer's error. | |
209 | return | |
210 | self.decode_field(name, start, count, parser) | |
1d4fe1c1 JH |
211 | |
212 | def decode(self, ss, es, data): | |
53cbedf5 | 213 | ptype, _, _ = data |
1d4fe1c1 | 214 | |
adb8233a | 215 | if ptype == 'TRANSFER': |
c4aaef25 GS |
216 | # Process accumulated bits after completion of a transfer. |
217 | self.decode_word(ss, es, self.bits) | |
912f4e8a | 218 | self.bits.clear() |
f534ce44 | 219 | |
1d4fe1c1 | 220 | if ptype == 'BITS': |
53cbedf5 | 221 | _, mosi_bits, miso_bits = data |
c4aaef25 GS |
222 | # Accumulate bits in MSB order as they are seen in SPI frames. |
223 | msb_bits = mosi_bits.copy() | |
224 | msb_bits.reverse() | |
225 | self.bits.extend(msb_bits) |