forked from analogdevicesinc/ad936x-filter-wizard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
auto_fast_rates.m
247 lines (203 loc) · 7.37 KB
/
auto_fast_rates.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
function [input,savedFilterConfig] = auto_fast_rates(input,FIR_config)
%auto_fast_rates Configures halfband filters, PLL clock, and
%converter rates in an optimal configuration based on the
%current 'Rdata' property
bounds9361 = rate_bounds;
if input.Rdata>=bounds9361.MAX_DATA_RATE
input.Rdata = (bounds9361.MAX_DATA_RATE);
elseif input.Rdata<=bounds9361.MIN_DATA_RATE
input.Rdata = (bounds9361.MIN_DATA_RATE);
end
currentMaxADCRate = 0;
savedDACDivider = 0;
rate_gov = false;
savedFIR = 0;
savedPLL = 0;
savedFilterConfig = 0;
DR = input.Rdata;
if nargin == 1
FIR_config = [1,2,4];
end
for FIR = FIR_config
FilterConfig = [...
3,2,2,FIR;...
2,2,2,FIR;...
3,2,1,FIR;...
2,2,1,FIR;...
2,1,1,FIR;...
3,1,1,FIR;...
1,1,1,FIR];
for k=1:7
% Update configuration
divs = struct;
divs.RxFIR = FilterConfig(k,4);
divs.RxHB1 = FilterConfig(k,3);
divs.RxHB2 = FilterConfig(k,2);
divs.RxHB3 = FilterConfig(k,1);
divs.TxFIR = FilterConfig(k,4);
divs.TxHB1 = FilterConfig(k,3);
divs.TxHB2 = FilterConfig(k,2);
divs.TxHB3 = FilterConfig(k,1);
rates = gen_rates(divs,DR);
% HB3 cannot be 3 if rate_gov enabled
if rate_gov && (divs.RxHB3==3)
continue;
end
% Check valid rates (ignore some section pre-dacdiv)
valid = validatePathRates(rates);
v = valid.RxRateHB3.pass && valid.RxRateHB2.pass && valid.RxRateHB1.pass && valid.RxRateFIR.pass;
v = v && valid.TxRateHB2.pass && valid.TxRateHB1.pass && valid.TxRateFIR.pass;
if v
% Determine PLL divider
pll = determine_pll_div(rates.ADCRate);
if pll>0
rates.PLLRate = rates.ADCRate * pll;
% Determine DAC divider setting and check ADC/DAC settings
dac_div = check_dac_adc_config(pll,k,rates.PLLRate);
if dac_div>0
rates.DACRate = rates.ADCRate/dac_div;
if rates.ADCRate>currentMaxADCRate
% Final check
[~,v] = validatePathRates(rates);
if ~v
continue
end
currentMaxADCRate = rates.ADCRate;
savedFilterConfig = k;
savedDACDivider = dac_div;
savedFIR = FIR;
savedPLL = pll;
end
end
end
end
end
end
if savedFilterConfig==0
input = [];
return;
end
% Set HBs based on best config found
PLLDivider = savedPLL;
RxFIR = savedFIR;
RxHB1 = FilterConfig(savedFilterConfig,3);
RxHB2 = FilterConfig(savedFilterConfig,2);
RxHB3 = FilterConfig(savedFilterConfig,1);
TxFIR = savedFIR;
TxHB1 = FilterConfig(savedFilterConfig,3);
TxHB2 = FilterConfig(savedFilterConfig,2);
TxHB3 = FilterConfig(savedFilterConfig,1);
% Apply dac div
if (savedFilterConfig<3) && (savedDACDivider>1)
TxHB1 = 1;
elseif (savedFilterConfig<5) && (savedDACDivider>1)
TxHB2 = 1;
end
% Save
input.DAC_div = savedDACDivider;
input.PLL_mult = PLLDivider;
if strcmp(input.RxTx, 'Rx')
input.FIR = RxFIR;
input.HB1 = RxHB1;
input.HB2 = RxHB2;
input.HB3 = RxHB3;
input.converter_rate = currentMaxADCRate;
else
input.FIR = TxFIR;
input.HB1 = TxHB1;
input.HB2 = TxHB2;
input.HB3 = TxHB3;
input.converter_rate = currentMaxADCRate/savedDACDivider;
end
input.PLL_rate = input.converter_rate * input.DAC_div * input.PLL_mult;
% Final check
if strcmp(input.RxTx, 'Rx')
if input.converter_rate > bounds9361.MAX_ADC_CLK || input.converter_rate < bounds9361.MIN_ADC_CLK
error('Invalid ADC Rate');
end
else
if input.converter_rate > bounds9361.MAX_DAC_CLK || input.converter_rate < bounds9361.MIN_DAC_CLK
error('Invalid DAC Rate');
end
end
end
function pll = determine_pll_div(ADCRate)
% Determine necessary PLL multiplier
PLL_mult = 64; %MAX_BBPLL_DIV
bounds9361 = rate_bounds;
while (PLL_mult>1)
rate0 = ADCRate*PLL_mult;
v = (rate0 >= bounds9361.MIN_BBPLL_FREQ) && (rate0 <= bounds9361.MAX_BBPLL_FREQ);
if v
pll = PLL_mult;
return
end
PLL_mult = PLL_mult/2;
end
pll = -1;
end
function r = check_dac_adc_config(PLL_mult,dec_table_index,PLLRate)
with_dd = PLLRate/PLL_mult/2;
without_dd = PLLRate/PLL_mult/1;
bounds9361 = rate_bounds;
a = rangeCheck(with_dd,'DAC Rate',bounds9361.MIN_DAC_CLK,bounds9361.MAX_DAC_CLK);
b = rangeCheck(without_dd,'ADC Rate',bounds9361.MIN_ADC_CLK,bounds9361.MAX_ADC_CLK );
c = rangeCheck(without_dd,'DAC Rate',bounds9361.MIN_DAC_CLK,bounds9361.MAX_DAC_CLK);
if (c.pass && b.pass)
r = 1; % Run without dac div
elseif (a.pass && b.pass && (dec_table_index<6))
r = 2; % Run with dac div
else
r = -1; % All rates invalid
end
end
function e = rangeCheck(val,name,min,max)
if ~isreal(val) || isnan(val) || ~isscalar(val)
e = struct;
e.msg = sprintf('%s must be integer, scalar, and finite',name);
e.pass = false;
elseif val<min || val>max
e = struct;
e.msg = sprintf('%s must be in range [%d %d]',name,int32(min),int32(max));
e.pass = false;
else
e = struct;
e.msg = '';
e.pass = true;
end
end
% Path Rate Validations
function [valid,v] = validatePathRates(rates)
bounds9361 = rate_bounds;
valid = struct;
valid.ADCRate = rangeCheck(rates.ADCRate,'ADC Rate',bounds9361.MIN_ADC_CLK,bounds9361.MAX_ADC_CLK);
% Rates into stage
valid.RxRateHB3 = rangeCheck(rates.RxRateHB3,'Rx HB3',0,bounds9361.MAX_RX.HB3);
valid.RxRateHB2 = rangeCheck(rates.RxRateHB2,'Rx HB2',0,bounds9361.MAX_RX.HB2);
valid.RxRateHB1 = rangeCheck(rates.RxRateHB1,'Rx HB1',0,bounds9361.MAX_RX.HB1);
valid.RxRateFIR = rangeCheck(rates.RxRateFIR,'Rx FIR',0,bounds9361.MAX_FIR);
valid.DACRate = rangeCheck(rates.DACRate,'DAC Rate',bounds9361.MIN_DAC_CLK,bounds9361.MAX_DAC_CLK);
% Rates out of stage
valid.TxRateHB3 = rangeCheck(rates.TxRateHB3,'Tx HB3',0,bounds9361.MAX_TX.HB3);
valid.TxRateHB2 = rangeCheck(rates.TxRateHB2,'Tx HB2',0,bounds9361.MAX_TX.HB2);
valid.TxRateHB1 = rangeCheck(rates.TxRateHB1,'Tx HB1',0,bounds9361.MAX_TX.HB1);
valid.TxRateFIR = rangeCheck(rates.TxRateFIR,'Tx FIR',0,bounds9361.MAX_FIR);
valid.PLLRate = rangeCheck(rates.PLLRate,'PLL Rate',bounds9361.MIN_BBPLL_FREQ,bounds9361.MAX_BBPLL_FREQ);
v = valid.ADCRate.pass && valid.RxRateHB3.pass && valid.RxRateHB2.pass && valid.RxRateHB1.pass && valid.RxRateFIR.pass;
v = v && valid.DACRate.pass && valid.TxRateHB3.pass && valid.TxRateHB2.pass && valid.TxRateHB1.pass && valid.TxRateFIR.pass;
v = v && valid.PLLRate.pass;
end
function rates = gen_rates(divs,DR)
rates.DataRate = DR;
rates.RxRateFIR = rates.DataRate * divs.RxFIR;
rates.RxRateHB1 = rates.RxRateFIR * divs.RxHB1;
rates.RxRateHB2 = rates.RxRateHB1 * divs.RxHB2;
rates.RxRateHB3 = rates.RxRateHB2 * divs.RxHB3;
rates.TxRateFIR = rates.DataRate * divs.TxFIR;
rates.TxRateHB1 = rates.TxRateFIR * divs.TxHB1;
rates.TxRateHB2 = rates.TxRateHB1 * divs.TxHB2;
rates.TxRateHB3 = rates.TxRateHB2 * divs.TxHB3;
rates.ADCRate = rates.RxRateHB3;
rates.PLLRate = -1;
rates.DACRate = -1;
end