-
Notifications
You must be signed in to change notification settings - Fork 49
/
ad9361_design_taps.c
396 lines (338 loc) · 12.2 KB
/
ad9361_design_taps.c
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
/*
* Copyright (C) 2017 Analog Devices, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*/
#include "ad9361.h"
#include "filterdesigner/internal_design_filter_cg.h"
#include <errno.h>
#include <iio.h>
#include <math.h>
#include <stdbool.h>
#include <stdio.h>
#ifdef _WIN32
#include <windows.h>
#else
#include <unistd.h>
#endif
#ifndef M_PI
#define M_PI 3.14159265358979323846
#endif
#define FIR_BUF_SIZE 8192
#ifdef _MSC_BUILD
#define snprintf sprintf_s
#endif
int ad9361_generate_fir_taps(struct filter_design_parameters *parameters,
short *taps, int *num_taps, int *gain)
{
double dnum_taps = 0;
double dgain = 0;
// Call filter designer
internal_design_filter_cg_initialize();
// Designer will alway return a filter, but it may not meet specifications
internal_design_filter_cg(
parameters->Rdata, parameters->Fpass, parameters->Fstop,
parameters->caldiv, parameters->FIR, parameters->HB1,
parameters->PLL_mult, parameters->Apass, parameters->Astop,
parameters->phEQ, parameters->HB2, parameters->HB3, parameters->Type,
parameters->RxTx, parameters->RFbw, parameters->DAC_div,
parameters->converter_rate, parameters->PLL_rate, parameters->Fcenter,
parameters->wnom, parameters->FIRdBmin, parameters->int_FIR,
parameters->maxTaps, taps, &dnum_taps, &dgain);
internal_design_filter_cg_terminate();
*num_taps = (int)dnum_taps;
*gain = (int)dgain;
// Filter with less than 32 taps is an error
if (*num_taps < 32)
return -EDOM;
else
return 0;
}
double calculate_rfbw(double pll_rate, double caldiv, bool TX,
double *rcaldiv)
{
double rfbw, min_rfbw, max_rfbw, scale;
if (TX) {
scale = 1.6;
min_rfbw = 1250000;
max_rfbw = 40000000;
} else {
scale = 1.4;
min_rfbw = 400000;
max_rfbw = 56000000;
}
rfbw =
(double)round((pll_rate / caldiv) * (2 / (scale * (2 * M_PI) / log(2))));
// If the RF bandwidth is outside the range of acceptable values we modify
// the divider value until it falls into an acceptable range.
while ((rfbw < min_rfbw) || (rfbw > max_rfbw)) {
if (rfbw < min_rfbw)
caldiv = caldiv - 1;
else
caldiv = caldiv + 1;
if ((caldiv < 1) || (caldiv > 511)) {
fprintf(stderr,"Calibration divider out of bounds (1 - 511): %f\n", caldiv);
return -EINVAL;
}
rfbw = calculate_rfbw(pll_rate, caldiv, TX, rcaldiv);
}
*rcaldiv = caldiv;
return rfbw;
}
void set_max_taps(struct filter_design_parameters *fdpTX,
struct filter_design_parameters *fdpRX)
{
// RX side
int N,M,K;
if (fdpRX->HB3 == 3)
N = 16*floor(fdpRX->converter_rate/(fdpRX->Rdata));
else
N = 16*floor(fdpRX->converter_rate/(2*fdpRX->Rdata));
if (N>128)
N = 128;
// TX side
if (fdpTX->FIR==1)
M = 64;
else
M = 128;
K = 16*floor(fdpTX->converter_rate*fdpTX->DAC_div/(2*fdpTX->Rdata));
if (K<M)
M = K;
// Pick the smallest
if (M>N) {
fdpTX->maxTaps = N;
fdpRX->maxTaps = N;
} else {
fdpTX->maxTaps = M;
fdpRX->maxTaps = M;
}
}
int build_configuration(struct filter_design_parameters *fdpTX,
struct filter_design_parameters *fdpRX,
unsigned long sample_rate,
unsigned long Fpass,
unsigned long Fstop,
unsigned long wnomTX,
unsigned long wnomRX)
{
double div, max;
unsigned long rx_path_clk[6];
unsigned long tx_path_clk[6];
unsigned long *path_clk;
struct filter_design_parameters *fdp;
int ret,k;
unsigned long rate_gov = 0;
ret = ad9361_calculate_rf_clock_chain((unsigned long) sample_rate,
rate_gov, rx_path_clk, tx_path_clk);
if (ret < 0)
return -EINVAL;
for (k=0; k<2; k++) {
if (k > 0) {
path_clk = tx_path_clk;
fdp = fdpTX;
fdp->RxTx = "Tx";
fdp->DAC_div = (double) rx_path_clk[1]/tx_path_clk[1];
} else {
path_clk = rx_path_clk;
fdp = fdpRX;
fdp->RxTx = "Rx";
fdp->DAC_div = 1.0;
}
// Map rates and dividers
fdp->PLL_rate = (double) path_clk[0];
fdp->converter_rate = (double) path_clk[1];
fdp->PLL_mult = (double) path_clk[0]/path_clk[1];
fdp->HB3 = (double) path_clk[1]/path_clk[2];
fdp->HB2 = (double) path_clk[2]/path_clk[3];
fdp->HB1 = (double) path_clk[3]/path_clk[4];
fdp->FIR = (double) path_clk[4]/path_clk[5];
// Set default parameters
fdp->Rdata = (double)path_clk[5];
fdp->Type = "Lowpass";
fdp->int_FIR = 1;
fdp->Apass = 0.5;
fdp->Astop = 80;
fdp->phEQ = -1;
fdp->FIRdBmin = 0;
// Define filter design specifications
fdp->Fpass = (double) Fpass;
fdp->Fstop = (double) Fstop;
fdp->Fcenter = 0.0;
if (k>0)
fdp->wnom = (double) wnomTX;
else
fdp->wnom = (double) wnomRX;
// Determine default analog bandwidth
div = ceil((fdp->PLL_rate / fdp->wnom) * (log(2) / (2 * M_PI)));
max = (div > 1) ? div : 1.0;
fdp->caldiv = (max > 511) ? 511.0 : max;
fdp->RFbw = calculate_rfbw(fdp->PLL_rate,fdp->caldiv, k>0, &(fdp->caldiv));
if (fdp->RFbw < 0)
return -EINVAL;
}
set_max_taps(fdpTX,fdpRX);
return 0;
}
int apply_custom_filter(struct iio_device *dev, unsigned dec_tx,
unsigned dec_rx, short *tapsTx,
short *tapsRx, unsigned taps,
unsigned long rate,
int gain_tx, int gain_rx,
unsigned long wnom_tx, unsigned long wnom_rx)
{
struct iio_channel *chanTX, *chanRX;
long long current_rate;
int ret, i, enable, len = 0;
char *buf;
chanTX = iio_device_find_channel(dev, "voltage0", true);
if (chanTX == NULL)
return -ENODEV;
ret = iio_channel_attr_read_longlong(chanTX, "sampling_frequency", ¤t_rate);
if (ret < 0)
return ret;
ret = ad9361_get_trx_fir_enable(dev, &enable);
if (ret < 0)
return ret;
if (enable) {
if (current_rate <= (25000000 / 12))
iio_channel_attr_write_longlong(chanTX, "sampling_frequency", 3000000);
ret = ad9361_set_trx_fir_enable(dev, false);
if (ret < 0)
return ret;
}
buf = (char*) malloc(FIR_BUF_SIZE);
if (!buf)
return -ENOMEM;
len += snprintf(buf + len, FIR_BUF_SIZE - len, "RX 3 GAIN %d DEC %d\n", gain_rx,
dec_rx);
len += snprintf(buf + len, FIR_BUF_SIZE - len, "TX 3 GAIN %d INT %d\n", gain_tx,
dec_tx);
for (i = 0; i < (int)taps; i++)
len += snprintf(buf + len, FIR_BUF_SIZE - len, "%d,%d\n", tapsRx[i], tapsTx[i]);
len += snprintf(buf + len, FIR_BUF_SIZE - len, "\n");
ret = iio_device_attr_write_raw(dev, "filter_fir_config", buf, len);
free (buf);
if (ret < 0)
return ret;
if (rate <= (25000000 / 12)) {
int dacrate, txrate, max;
char readbuf[100];
ret = iio_device_attr_read(dev, "tx_path_rates", readbuf, sizeof(readbuf));
if (ret < 0)
return ret;
ret = sscanf(readbuf, "BBPLL:%*d DAC:%d T2:%*d T1:%*d TF:%*d TXSAMP:%d",
&dacrate, &txrate);
if (ret != 2)
return -EFAULT;
if (txrate == 0)
return -EINVAL;
max = (dacrate / txrate) * 16;
if (max < taps)
iio_channel_attr_write_longlong(chanTX, "sampling_frequency", 3000000);
ret = ad9361_set_trx_fir_enable(dev, true);
if (ret < 0)
return ret;
ret = iio_channel_attr_write_longlong(chanTX, "sampling_frequency", rate);
if (ret < 0)
return ret;
} else {
ret = iio_channel_attr_write_longlong(chanTX, "sampling_frequency", rate);
if (ret < 0)
return ret;
ret = ad9361_set_trx_fir_enable(dev, true);
if (ret < 0)
return ret;
}
chanRX = iio_device_find_channel(dev, "voltage0", false);
if (chanRX == NULL)
return -ENODEV;
ret = iio_channel_attr_write_longlong(chanTX, "rf_bandwidth", wnom_tx);
if (ret < 0)
return ret;
ret = iio_channel_attr_write_longlong(chanRX, "rf_bandwidth", wnom_rx);
if (ret < 0)
return ret;
return 0;
}
int ad9361_calculate_rf_clock_chain_fdp(struct filter_design_parameters *fdpTX,
struct filter_design_parameters *fdpRX,
unsigned long sample_rate)
{
int ret;
// Set default configuration
unsigned long Fpass = sample_rate / 3.0;
unsigned long Fstop = Fpass * 1.25;
unsigned long wnomTX = 1.6 * Fstop;
unsigned long wnomRX = 1.4 * Fstop;
ret = build_configuration(fdpTX, fdpRX, sample_rate, Fpass, Fstop, wnomTX,
wnomRX);
if (ret<0)
return ret;
return 0;
}
int ad9361_set_bb_rate_custom_filter_auto(struct iio_device *dev,
unsigned long rate)
{
struct filter_design_parameters fdpTX;
struct filter_design_parameters fdpRX;
short taps_tx[128];
short taps_rx[128];
int ret, num_taps_tx, num_taps_rx, gain_tx, gain_rx;
unsigned dec_tx, dec_rx, num_taps;
ret = ad9361_calculate_rf_clock_chain_fdp(&fdpTX, &fdpRX, rate);
if (ret < 0)
return ret;
ret = ad9361_generate_fir_taps(&fdpRX, taps_rx, &num_taps_rx, &gain_rx);
if (ret < 0)
return ret;
ret = ad9361_generate_fir_taps(&fdpTX, taps_tx, &num_taps_tx, &gain_tx);
if (ret < 0)
return ret;
dec_tx = (unsigned) fdpTX.FIR;
dec_rx = (unsigned) fdpRX.FIR;
num_taps = (unsigned) fdpTX.maxTaps;
ret = apply_custom_filter(dev, dec_tx, dec_rx, taps_tx, taps_rx, num_taps,
rate, gain_tx, gain_rx, fdpTX.wnom, fdpRX.wnom);
if (ret < 0)
return ret;
return 0;
}
int ad9361_set_bb_rate_custom_filter_manual(struct iio_device *dev,
unsigned long rate, unsigned long Fpass,
unsigned long Fstop, unsigned long wnom_tx, unsigned long wnom_rx)
{
struct filter_design_parameters fdpTX;
struct filter_design_parameters fdpRX;
short taps_tx[128];
short taps_rx[128];
int ret, num_taps_tx, num_taps_rx, gain_tx, gain_rx;
unsigned dec_tx, dec_rx, num_taps;
if (Fpass >= Fstop)
return -EINVAL;
ret = build_configuration(&fdpTX, &fdpRX, rate, Fpass, Fstop, wnom_tx,
wnom_rx);
if (ret<0)
return ret;
ret = ad9361_generate_fir_taps(&fdpRX, taps_rx, &num_taps_rx, &gain_rx);
if (ret < 0)
return ret;
ret = ad9361_generate_fir_taps(&fdpTX, taps_tx, &num_taps_tx, &gain_tx);
if (ret < 0)
return ret;
dec_tx = (unsigned) fdpTX.FIR;
dec_rx = (unsigned) fdpRX.FIR;
num_taps = (unsigned) fdpTX.maxTaps;
ret = apply_custom_filter(dev, dec_tx, dec_rx, taps_tx, taps_rx, num_taps,
rate, gain_tx, gain_rx, wnom_tx, wnom_rx);
if (ret < 0)
return ret;
return 0;
}