Debian Codegraph @

ad9361_calculate_rf_clock_chain.c

raw symbols SCIP
1
2 #include "ad9361.h"
3 #include <stdbool.h>
4 #include <stdio.h>
5 #include <errno.h>
6 #include <math.h>
7
8 #ifndef M_PI
9 #define M_PI 3.14159265358979323846
10 #endif
11
12 #define MIN_ADC_CLK 25000000UL /* 25 MHz */
13 #define MAX_ADC_CLK 640000000UL /* 640 MHz */
14 #define MIN_DAC_CLK 25000000UL /* 25 MHz */
15 #define MAX_DAC_CLK (MAX_ADC_CLK / 2)
16 #define MAX_BBPLL_FREF 70007000UL /* 70 MHz + 100ppm */
17 #define MIN_BBPLL_FREQ 714928500UL /* 715 MHz - 100ppm */
18 #define MAX_BBPLL_FREQ 1430143000UL /* 1430 MHz + 100ppm */
19 #define MAX_BBPLL_DIV 64
20 #define MIN_BBPLL_DIV 2
21 #define MAX_DATA_RATE 61440000UL // Output of FIR (RX)
22 #define MIN_DATA_RATE MIN_ADC_CLK / 48
23 // Output of FIR on RX
24 #define MAX_FIR MAX_DATA_RATE * 2
25 // Associated with outputs of stage
26 #define MAX_RX_HB1 122880000UL
27 #define MAX_RX_HB2 245760000UL
28 #define MAX_RX_HB3 320000000UL
29 // Associated with inputs of stage
30 #define MAX_TX_HB1 122880000UL
31 #define MAX_TX_HB2 245760000UL
32 #define MAX_TX_HB3 320000000UL
33
34 const unsigned long RX_MAX_PATH_RATES[] = {MAX_ADC_CLK, MAX_RX_HB3, MAX_RX_HB2, MAX_RX_HB1, MAX_FIR};
35 const unsigned long TX_MAX_PATH_RATES[] = {MAX_DAC_CLK, MAX_TX_HB3, MAX_TX_HB2, MAX_TX_HB1, MAX_FIR};
36 const unsigned long RX_MIN_PATH_RATES[] = {MIN_ADC_CLK, 0, 0, 0, 0};
37 const unsigned long TX_MIN_PATH_RATES[] = {MIN_DAC_CLK, 0, 0, 0, 0};
38
39 #define check(val,min,max) ( (val)<=(max) ? (val)>=(min) : false )
40
41 unsigned long max_rate_found;
42
43
44 bool check_rates(int FIR, const int *HB_configs, unsigned long samp_rate,
45 unsigned long *rates)
46 {
47 int j;
48 bool c = true;
49
50 rates[5] = samp_rate;
51 for (j=4; j>0; j--) {
52 rates[j] = rates[j+1]*HB_configs[j-1];
53 if (j>1) {
54 c &= check(rates[j],TX_MIN_PATH_RATES[j-1],TX_MAX_PATH_RATES[j-1]);
55 c &= check(rates[j],RX_MIN_PATH_RATES[j-1],RX_MAX_PATH_RATES[j-1]);
56 }
57 }
58 return c;
59 }
60
61 int determine_pll_div(unsigned long *rates)
62 {
63 // Determine necessary PLL multiplier
64 unsigned long long tmp;
65 int PLL_mult = MAX_BBPLL_DIV;
66 while (PLL_mult>1) {
67 tmp = (unsigned long long) rates[1]*PLL_mult;
68 if (check(tmp, MIN_BBPLL_FREQ, MAX_BBPLL_FREQ)) {
69 rates[0] = (unsigned long) rates[1]*PLL_mult;
70 return PLL_mult;
71 }
72 PLL_mult >>= 1;
73 }
74 return -1;
75 }
76
77 int check_dac_adc_config(unsigned long pll_bb, int PLL_mult,
78 int dec_table_index)
79 {
80 // Need to determine if DAC divider is required and if ADC and DAC rates
81 // can be satisfied
82 unsigned long with_dd, without_dd;
83 bool a, b, c;
84
85 with_dd = pll_bb/PLL_mult/2;
86 without_dd = pll_bb/PLL_mult/1;
87
88 a = check(with_dd, MIN_DAC_CLK, MAX_DAC_CLK);
89 b = check(without_dd, MIN_ADC_CLK, MAX_ADC_CLK);
90 c = check(without_dd, MIN_DAC_CLK, MAX_DAC_CLK);
91
92 if (c && b)
93 return 1; // Run without dac div
94 else if (a && b && (dec_table_index<5))
95 return 2; // Run with dac div
96 else
97 return -1;
98 }
99
100 void set_rates(unsigned long *rx_path_clks,
101 unsigned long *tx_path_clks, int DAC_div, unsigned long *rates,
102 int dec_table_index)
103 {
104 int k;
105
106 // Check if ADC will run faster in config
107 if (rates[1]>max_rate_found)
108 max_rate_found = rates[1];
109 else
110 return;
111
112 for (k=0; k<6; k++) {
113 rx_path_clks[k] = rates[k];
114 tx_path_clks[k] = rates[k];
115
116 if (k>0) { // Adjust HB's for DAC divider setting
117 if ((dec_table_index<2) && (k<4))
118 tx_path_clks[k] = rates[k]/DAC_div;
119 else if ((dec_table_index<4) && (k<3))
120 tx_path_clks[k] = rates[k]/DAC_div;
121 }
122 }
123 }
124
125
126 int determine_path_rates_with_fir(unsigned long sample_rate,
127 unsigned long rate_gov,
128 unsigned long *rx_path_clks,
129 unsigned long *tx_path_clks,
130 int FIR)
131 {
132 unsigned long rates[6];
133 int PLL_mult, k;
134
135 max_rate_found = 0UL;
136
137 const int HB_configs[][4] = {
138 {3,2,2,FIR}, //12
139 {2,2,2,FIR}, //8
140 {3,2,1,FIR}, //6
141 {2,2,1,FIR}, //4
142 {2,1,1,FIR}, //2
143 {3,1,1,FIR}, //3
144 {1,1,1,FIR}, //1
145 };
146
147 // RX Path:
148 // BBPLL -> /PLL_div -> /HB3 -> /HB2 -> /HB1 -> /FIR
149 // TX Path:
150 // BBPLL -> /(PLL_div*DAC_div) -> /HB3 -> /HB2 -> /HB1 -> /FIR
151
152 // Cycle through possible decimations from highest to lowest
153 for (k=0; k<7; k++) {
154 // HB3 cannot be 3 if rate_gov enabled
155 if ((rate_gov>0) && HB_configs[k][0]==3)
156 continue;
157 // Check if HB and FIR rates are valid
158 if ( check_rates(FIR, HB_configs[k], sample_rate, rates) ) {
159 // Calculate PLL divider for configuration
160 PLL_mult = determine_pll_div(rates);
161 if (PLL_mult>0) {
162 // Determine DAC divider setting and check ADC/DAC settings
163 int dac_div = check_dac_adc_config(rates[0], PLL_mult, k);
164 // printf("dac_div: %d\n",dac_div);
165 if (dac_div>0)
166 set_rates(rx_path_clks, tx_path_clks, dac_div, rates, k);
167 }
168 }
169 }
170
171 if (max_rate_found==0UL)
172 return -EINVAL;
173 else
174 return 0;
175 }
176
177 int ad9361_calculate_rf_clock_chain(unsigned long sample_rate,
178 unsigned long rate_gov,
179 unsigned long *rx_path_clks,
180 unsigned long *tx_path_clks)
181 {
182 int ret, k;
183 int FIR[] = {4,2,1};
184
185 // Check desired rate within bounds
186 if (!check(sample_rate, MIN_DATA_RATE, MAX_DATA_RATE))
187 return -EINVAL;
188
189 // Rate selection will try to:
190 // 1. Utilize the maximum decimation in the FIR
191 // 2. Run the ADC/DAC as fast as possible
192 // 3. Use the most decimation possible starting with HB3(closest to ADC)->HB1
193
194 // Cycle through available FIR settings
195 for (k=0; k<3; k++) {
196 ret = determine_path_rates_with_fir(sample_rate, rate_gov, rx_path_clks,
197 tx_path_clks, FIR[k]);
198 if (ret==0)
199 break;
200 }
201 return ret;
202 }