File: tuning.c

package info (click to toggle)
hackrf 2026.01.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 38,216 kB
  • sloc: ansic: 60,696; python: 6,072; xml: 3,424; perl: 2,730; makefile: 601; asm: 514; vhdl: 319; sh: 179; awk: 20
file content (240 lines) | stat: -rw-r--r-- 7,333 bytes parent folder | download
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
/*
 * Copyright 2012-2022 Great Scott Gadgets <info@greatscottgadgets.com>
 * Copyright 2012 Jared Boone
 * Copyright 2013 Benjamin Vernoux
 *
 * This file is part of HackRF.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2, or (at your option)
 * any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; see the file COPYING.  If not, write to
 * the Free Software Foundation, Inc., 51 Franklin Street,
 * Boston, MA 02110-1301, USA.
 */

#include "tuning.h"
#include "hackrf_ui.h"
#include "hackrf_core.h"
#include "mixer.h"
#include "max2831.h"
#include "max2837.h"
#include "max2839.h"
#include "sgpio.h"
#include "operacake.h"
#include "platform_detect.h"

#ifndef PRALINE

	#define MIN_LP_FREQ_MHZ (0)
	#define MAX_LP_FREQ_MHZ (2170ULL)

	#define ABS_MIN_BYPASS_FREQ_MHZ (2000ULL)
	#define MIN_BYPASS_FREQ_MHZ     (MAX_LP_FREQ_MHZ)
	#define MAX_BYPASS_FREQ_MHZ     (2740ULL)
	#define ABS_MAX_BYPASS_FREQ_MHZ (3000ULL)

	#define MIN_HP_FREQ_MHZ  (MAX_BYPASS_FREQ_MHZ)
	#define MID1_HP_FREQ_MHZ (3600ULL)
	#define MID2_HP_FREQ_MHZ (5100ULL)
	#define MAX_HP_FREQ_MHZ  (7250ULL)

	#define MIN_LO_FREQ_HZ (84375000ULL)
	#define MAX_LO_FREQ_HZ (5400000000ULL)

#else

	#define MIN_LP_FREQ_MHZ (0)
	#define MAX_LP_FREQ_MHZ (2320ULL)

	#define ABS_MIN_BYPASS_FREQ_MHZ (2000ULL)
	#define MIN_BYPASS_FREQ_MHZ     (MAX_LP_FREQ_MHZ)
	#define MAX_BYPASS_FREQ_MHZ     (2580ULL)
	#define ABS_MAX_BYPASS_FREQ_MHZ (3000ULL)

	#define MIN_HP_FREQ_MHZ (MAX_BYPASS_FREQ_MHZ)
	#define MAX_HP_FREQ_MHZ (7250ULL)

	#define MIN_LO_FREQ_HZ (84375000ULL)
	#define MAX_LO_FREQ_HZ (5400000000ULL)

#endif

#ifndef PRALINE
static uint32_t max2837_freq_nominal_hz = 2560000000;

/*
 * Set freq/tuning between 0MHz to 7250 MHz (less than 16bits really used)
 * hz between 0 to 999999 Hz (not checked)
 * return false on error or true if success.
 */
bool set_freq(const uint64_t freq)
{
	bool success;
	uint64_t mixer_freq_hz;
	uint64_t real_mixer_freq_hz;

	const uint32_t freq_mhz = freq / FREQ_ONE_MHZ;

	success = true;

	max283x_mode_t prior_max283x_mode = max283x_mode(&max283x);
	max283x_set_mode(&max283x, MAX283x_MODE_STANDBY);
	if (freq_mhz < MAX_LP_FREQ_MHZ) {
		rf_path_set_filter(&rf_path, RF_PATH_FILTER_LOW_PASS);
	#ifdef RAD1O
		max2837_freq_nominal_hz = 2300 * FREQ_ONE_MHZ;
	#else
		/* IF is graduated from 2650 MHz to 2340 MHz */
		max2837_freq_nominal_hz = (2650 * FREQ_ONE_MHZ) - (freq / 7);
	#endif
		mixer_freq_hz = max2837_freq_nominal_hz + freq;
		/* Set Freq and read real freq */
		real_mixer_freq_hz = mixer_set_frequency(&mixer, mixer_freq_hz);
		max283x_set_frequency(&max283x, real_mixer_freq_hz - freq);
		sgpio_cpld_set_mixer_invert(&sgpio_config, 1);
	} else if ((freq_mhz >= MIN_BYPASS_FREQ_MHZ) && (freq_mhz < MAX_BYPASS_FREQ_MHZ)) {
		rf_path_set_filter(&rf_path, RF_PATH_FILTER_BYPASS);
		/* mixer_freq_mhz <= not used in Bypass mode */
		max283x_set_frequency(&max283x, freq);
		sgpio_cpld_set_mixer_invert(&sgpio_config, 0);
	} else if ((freq_mhz >= MIN_HP_FREQ_MHZ) && (freq_mhz <= MAX_HP_FREQ_MHZ)) {
		if (freq_mhz < MID1_HP_FREQ_MHZ) {
			/* IF is graduated from 2170 MHz to 2740 MHz */
			max2837_freq_nominal_hz = (MIN_BYPASS_FREQ_MHZ * FREQ_ONE_MHZ) +
				(((freq - (MAX_BYPASS_FREQ_MHZ * FREQ_ONE_MHZ)) * 57) /
				 86);
		} else if (freq_mhz < MID2_HP_FREQ_MHZ) {
			/* IF is graduated from 2350 MHz to 2650 MHz */
			max2837_freq_nominal_hz = (2350 * FREQ_ONE_MHZ) +
				((freq - (MID1_HP_FREQ_MHZ * FREQ_ONE_MHZ)) / 5);
		} else {
			/* IF is graduated from 2500 MHz to 2738 MHz */
			max2837_freq_nominal_hz = (2500 * FREQ_ONE_MHZ) +
				((freq - (MID2_HP_FREQ_MHZ * FREQ_ONE_MHZ)) / 9);
		}
		rf_path_set_filter(&rf_path, RF_PATH_FILTER_HIGH_PASS);
		mixer_freq_hz = freq - max2837_freq_nominal_hz;
		/* Set Freq and read real freq */
		real_mixer_freq_hz = mixer_set_frequency(&mixer, mixer_freq_hz);
		max283x_set_frequency(&max283x, freq - real_mixer_freq_hz);
		sgpio_cpld_set_mixer_invert(&sgpio_config, 0);
	} else {
		/* Error freq_mhz too high */
		success = false;
	}
	max283x_set_mode(&max283x, prior_max283x_mode);
	if (success) {
		hackrf_ui()->set_frequency(freq);
	#ifdef HACKRF_ONE
		operacake_set_range(freq_mhz);
	#endif
	}
	return success;
}

#else

bool tuning_set_frequency(
	const tune_config_t* cfg,
	const uint64_t freq,
	const uint32_t offset)
{
	uint64_t mixer_freq_hz;
	uint64_t real_mixer_freq_hz;

	if (freq > (MAX_HP_FREQ_MHZ * FREQ_ONE_MHZ)) {
		return false;
	}

	const uint16_t freq_mhz = freq / FREQ_ONE_MHZ;

	uint64_t rf = freq;
	if (cfg->shift == FPGA_QUARTER_SHIFT_MODE_DOWN) {
		if (offset > rf) {
			rf = offset - rf;
		} else {
			rf = rf - offset;
		}
	} else if (cfg->shift == FPGA_QUARTER_SHIFT_MODE_UP) {
		rf = rf + offset;
	}

	max2831_mode_t prior_max2831_mode = max2831_mode(&max283x);
	max2831_set_mode(&max283x, MAX2831_MODE_STANDBY);

	if (cfg->if_mhz == 0) {
		rf_path_set_filter(&rf_path, RF_PATH_FILTER_BYPASS);
		max2831_set_frequency(&max283x, rf);
		sgpio_cpld_set_mixer_invert(&sgpio_config, 0);
	} else if (cfg->if_mhz > freq_mhz) {
		rf_path_set_filter(&rf_path, RF_PATH_FILTER_LOW_PASS);
		if (cfg->high_lo) {
			mixer_freq_hz = FREQ_ONE_MHZ * cfg->if_mhz + rf;
			real_mixer_freq_hz = mixer_set_frequency(&mixer, mixer_freq_hz);
			max2831_set_frequency(&max283x, real_mixer_freq_hz - rf);
			sgpio_cpld_set_mixer_invert(&sgpio_config, 1);
		} else {
			mixer_freq_hz = FREQ_ONE_MHZ * cfg->if_mhz - rf;
			real_mixer_freq_hz = mixer_set_frequency(&mixer, mixer_freq_hz);
			max2831_set_frequency(&max283x, real_mixer_freq_hz + rf);
			sgpio_cpld_set_mixer_invert(&sgpio_config, 0);
		}
	} else {
		rf_path_set_filter(&rf_path, RF_PATH_FILTER_HIGH_PASS);
		mixer_freq_hz = rf - FREQ_ONE_MHZ * cfg->if_mhz;
		real_mixer_freq_hz = mixer_set_frequency(&mixer, mixer_freq_hz);
		max2831_set_frequency(&max283x, rf - real_mixer_freq_hz);
		sgpio_cpld_set_mixer_invert(&sgpio_config, 0);
	}

	max2831_set_mode(&max283x, prior_max2831_mode);
	hackrf_ui()->set_frequency(freq);
	operacake_set_range(freq_mhz);
	return true;
}
#endif

bool set_freq_explicit(
	const uint64_t if_freq_hz,
	const uint64_t lo_freq_hz,
	const rf_path_filter_t path)
{
	if ((if_freq_hz < ((uint64_t) ABS_MIN_BYPASS_FREQ_MHZ * FREQ_ONE_MHZ)) ||
	    (if_freq_hz > ((uint64_t) ABS_MAX_BYPASS_FREQ_MHZ * FREQ_ONE_MHZ))) {
		return false;
	}

	if ((path != RF_PATH_FILTER_BYPASS) &&
	    ((lo_freq_hz < MIN_LO_FREQ_HZ) || (lo_freq_hz > MAX_LO_FREQ_HZ))) {
		return false;
	}

	if (path > 2) {
		return false;
	}

	rf_path_set_filter(&rf_path, path);
#ifdef PRALINE
	max2831_set_frequency(&max283x, if_freq_hz);
#else
	max283x_set_frequency(&max283x, if_freq_hz);
#endif
	if (lo_freq_hz > if_freq_hz) {
		sgpio_cpld_set_mixer_invert(&sgpio_config, 1);
	} else {
		sgpio_cpld_set_mixer_invert(&sgpio_config, 0);
	}
	if (path != RF_PATH_FILTER_BYPASS) {
		(void) mixer_set_frequency(&mixer, lo_freq_hz);
	}
	return true;
}