Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit d4a5c22

Browse files
committed
replaced set_freq_if() with set_freq_explicit() and implemented explicit tuning option in hackrf_transfer
1 parent 08927ab commit d4a5c22

9 files changed

Lines changed: 263 additions & 87 deletions

File tree

firmware/common/rf_path.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ typedef enum {
3737
void rf_path_set_direction(const rf_path_direction_t direction);
3838

3939
typedef enum {
40-
RF_PATH_FILTER_BYPASS,
41-
RF_PATH_FILTER_LOW_PASS,
42-
RF_PATH_FILTER_HIGH_PASS,
40+
RF_PATH_FILTER_BYPASS = 0,
41+
RF_PATH_FILTER_LOW_PASS = 1,
42+
RF_PATH_FILTER_HIGH_PASS = 2,
4343
} rf_path_filter_t;
4444

4545
void rf_path_set_filter(const rf_path_filter_t filter);

firmware/common/tuning.c

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@
2525
#include <rffc5071.h>
2626
#include <max2837.h>
2727

28-
#include "rf_path.h"
29-
3028
#define FREQ_ONE_MHZ (1000*1000)
3129

3230
#define MIN_LP_FREQ_MHZ (0)
@@ -40,6 +38,9 @@
4038
#define MID2_HP_FREQ_MHZ (5100)
4139
#define MAX_HP_FREQ_MHZ (7250)
4240

41+
#define MIN_LO_FREQ_HZ (84375000)
42+
#define MAX_LO_FREQ_HZ (5400000000ULL)
43+
4344
static uint32_t max2837_freq_nominal_hz=2560000000;
4445

4546
uint64_t freq_cache = 100000000;
@@ -54,7 +55,6 @@ bool set_freq(const uint64_t freq)
5455
uint32_t RFFC5071_freq_mhz;
5556
uint32_t MAX2837_freq_hz;
5657
uint64_t real_RFFC5071_freq_hz;
57-
uint32_t tmp_hz;
5858

5959
const uint32_t freq_mhz = freq / 1000000;
6060
const uint32_t freq_hz = freq % 1000000;
@@ -105,11 +105,27 @@ bool set_freq(const uint64_t freq)
105105
return success;
106106
}
107107

108-
bool set_freq_if(const uint32_t freq_if_hz) {
109-
bool success = false;
110-
if( (freq_if_hz >= MIN_BYPASS_FREQ_MHZ) && (freq_if_hz <= MAX_BYPASS_FREQ_MHZ) ) {
111-
max2837_freq_nominal_hz = freq_if_hz;
112-
success = set_freq(freq_cache);
108+
bool set_freq_explicit(const uint64_t if_freq_hz, const uint64_t lo_freq_hz,
109+
const rf_path_filter_t path)
110+
{
111+
if ((if_freq_hz < ((uint64_t)MIN_BYPASS_FREQ_MHZ * FREQ_ONE_MHZ))
112+
|| (if_freq_hz > ((uint64_t)MAX_BYPASS_FREQ_MHZ * FREQ_ONE_MHZ))) {
113+
return false;
113114
}
114-
return success;
115+
116+
if ((path != RF_PATH_FILTER_BYPASS) &&
117+
(lo_freq_hz < MIN_LO_FREQ_HZ) || (lo_freq_hz > MAX_LO_FREQ_HZ)) {
118+
return false;
119+
}
120+
121+
if (path > 2) {
122+
return false;
123+
}
124+
125+
rf_path_set_filter(path);
126+
max2837_set_frequency(if_freq_hz);
127+
if (path != RF_PATH_FILTER_BYPASS) {
128+
(void)rffc5071_set_frequency(lo_freq_hz / FREQ_ONE_MHZ);
129+
}
130+
return true;
115131
}

firmware/common/tuning.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@
2323
#ifndef __TUNING_H__
2424
#define __TUNING_H__
2525

26+
#include "rf_path.h"
27+
2628
#include <stdint.h>
2729
#include <stdbool.h>
2830

2931
bool set_freq(const uint64_t freq);
30-
bool set_freq_if(const uint32_t freq_if_hz);
32+
bool set_freq_explicit(const uint64_t if_freq_hz, const uint64_t lo_freq_hz,
33+
const rf_path_filter_t path);
3134

3235
#endif/*__TUNING_H__*/

firmware/hackrf_usb/hackrf_usb.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,12 +125,13 @@ static const usb_request_handler_fn vendor_request_handler[] = {
125125
usb_vendor_request_set_lna_gain,
126126
usb_vendor_request_set_vga_gain,
127127
usb_vendor_request_set_txvga_gain,
128-
usb_vendor_request_set_if_freq,
128+
NULL, // was set_if_freq
129129
#ifdef HACKRF_ONE
130130
usb_vendor_request_set_antenna_enable,
131131
#else
132132
NULL,
133133
#endif
134+
usb_vendor_request_set_freq_explicit,
134135
};
135136

136137
static const uint32_t vendor_request_handler_count =

firmware/hackrf_usb/usb_api_transceiver.c

Lines changed: 28 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ typedef struct {
4141

4242
set_freq_params_t set_freq_params;
4343

44+
struct set_freq_explicit_params {
45+
uint64_t if_freq_hz; /* intermediate frequency */
46+
uint64_t lo_freq_hz; /* front-end local oscillator frequency */
47+
uint8_t path; /* image rejection filter path */
48+
};
49+
50+
struct set_freq_explicit_params explicit_params;
51+
4452
typedef struct {
4553
uint32_t freq_hz;
4654
uint32_t divider;
@@ -175,19 +183,6 @@ usb_request_status_t usb_vendor_request_set_txvga_gain(
175183
return USB_REQUEST_STATUS_OK;
176184
}
177185

178-
usb_request_status_t usb_vendor_request_set_if_freq(
179-
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage
180-
) {
181-
if( stage == USB_TRANSFER_STAGE_SETUP ) {
182-
if( set_freq_if((uint32_t)endpoint->setup.index * 1000 * 1000) ) {
183-
usb_transfer_schedule_ack(endpoint->in);
184-
} else {
185-
return USB_REQUEST_STATUS_STALL;
186-
}
187-
}
188-
return USB_REQUEST_STATUS_OK;
189-
}
190-
191186
usb_request_status_t usb_vendor_request_set_antenna_enable(
192187
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage)
193188
{
@@ -208,3 +203,23 @@ usb_request_status_t usb_vendor_request_set_antenna_enable(
208203
return USB_REQUEST_STATUS_OK;
209204
}
210205
}
206+
207+
usb_request_status_t usb_vendor_request_set_freq_explicit(
208+
usb_endpoint_t* const endpoint,
209+
const usb_transfer_stage_t stage)
210+
{
211+
if (stage == USB_TRANSFER_STAGE_SETUP) {
212+
usb_transfer_schedule_block(endpoint->out, &explicit_params,
213+
sizeof(struct set_freq_explicit_params), NULL, NULL);
214+
return USB_REQUEST_STATUS_OK;
215+
} else if (stage == USB_TRANSFER_STAGE_DATA) {
216+
if (set_freq_explicit(explicit_params.if_freq_hz,
217+
explicit_params.lo_freq_hz, explicit_params.path)) {
218+
usb_transfer_schedule_ack(endpoint->in);
219+
return USB_REQUEST_STATUS_OK;
220+
}
221+
return USB_REQUEST_STATUS_STALL;
222+
} else {
223+
return USB_REQUEST_STATUS_OK;
224+
}
225+
}

firmware/hackrf_usb/usb_api_transceiver.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ usb_request_status_t usb_vendor_request_set_vga_gain(
4848
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);
4949
usb_request_status_t usb_vendor_request_set_txvga_gain(
5050
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);
51-
usb_request_status_t usb_vendor_request_set_if_freq(
52-
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);
5351
usb_request_status_t usb_vendor_request_set_antenna_enable(
5452
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);
53+
usb_request_status_t usb_vendor_request_set_freq_explicit(
54+
usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage);
5555

5656
#endif/*__USB_API_TRANSCEIVER_H__*/

host/hackrf-tools/src/hackrf_transfer.c

Lines changed: 121 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,11 @@ int gettimeofday(struct timeval *tv, void* ignored)
8686
#define DEFAULT_FREQ_HZ (900000000ull) /* 900MHz */
8787
#define FREQ_MIN_HZ (0ull) /* 0 Hz */
8888
#define FREQ_MAX_HZ (7250000000ull) /* 7250MHz */
89+
#define IF_MIN_HZ (2150000000ull)
90+
#define IF_MAX_HZ (2750000000ull)
91+
#define LO_MIN_HZ (84375000ull)
92+
#define LO_MAX_HZ (5400000000ull)
93+
#define DEFAULT_LO_HZ (1000000000ull)
8994

9095
#define DEFAULT_SAMPLE_RATE_HZ (10000000) /* 10MHz default sample rate */
9196

@@ -240,10 +245,19 @@ bool receive_wav = false;
240245
bool transmit = false;
241246
struct timeval time_start;
242247
struct timeval t_start;
243-
244-
bool freq = false;
248+
249+
bool automatic_tuning = false;
245250
uint64_t freq_hz;
246251

252+
bool if_freq = false;
253+
uint64_t if_freq_hz;
254+
255+
bool lo_freq = false;
256+
uint64_t lo_freq_hz = DEFAULT_LO_HZ;
257+
258+
bool image_reject = false;
259+
uint32_t image_reject_selection;
260+
247261
bool amp = false;
248262
uint32_t amp_enable;
249263

@@ -326,13 +340,16 @@ static void usage() {
326340
printf("\t-t <filename> # Transmit data from file.\n");
327341
printf("\t-w # Receive data into file with WAV header and automatic name.\n");
328342
printf("\t # This is for SDR# compatibility and may not work with other software.\n");
329-
printf("\t[-f set_freq_hz] # Set Freq in Hz between [%lluMHz, %lluMHz].\n", FREQ_MIN_HZ/FREQ_ONE_MHZ, FREQ_MAX_HZ/FREQ_ONE_MHZ);
330-
printf("\t[-a set_amp] # Set Amp 1=Enable, 0=Disable.\n");
331-
printf("\t[-p set_antenna] # Set antenna port power, 1=Enable, 0=Disable.\n");
332-
printf("\t[-l gain_db] # Set lna gain, 0-40dB, 8dB steps\n");
333-
printf("\t[-i gain_db] # Set vga(if) gain, 0-62dB, 2dB steps\n");
334-
printf("\t[-x gain_db] # Set TX vga gain, 0-47dB, 1dB steps\n");
335-
printf("\t[-s sample_rate_hz] # Set sample rate in Hz (8/10/12.5/16/20MHz, default %lldMHz).\n", DEFAULT_SAMPLE_RATE_HZ/FREQ_ONE_MHZ);
343+
printf("\t[-f freq_hz] # Frequency in Hz between [%lluMHz, %lluMHz].\n", FREQ_MIN_HZ/FREQ_ONE_MHZ, FREQ_MAX_HZ/FREQ_ONE_MHZ);
344+
printf("\t[-e if_freq_hz] # Intermediate Frequency (IF) in Hz [%lluMHz to %lluMHz].\n", IF_MIN_HZ/FREQ_ONE_MHZ, IF_MAX_HZ/FREQ_ONE_MHZ);
345+
printf("\t[-o lo_freq_hz] # Front-end Local Oscillator (LO) frequency in Hz [%lluMHz to %lluMHz].\n", LO_MIN_HZ/FREQ_ONE_MHZ, LO_MAX_HZ/FREQ_ONE_MHZ);
346+
printf("\t[-m image_reject] # Image rejection filter selection, 0=bypass, 1=low pass, 2=high pass.\n");
347+
printf("\t[-a amp_enable] # Amplifier 1=Enable, 0=Disable.\n");
348+
printf("\t[-p antenna_enable] # Antenna port power, 1=Enable, 0=Disable.\n");
349+
printf("\t[-l gain_db] # LNA gain, 0-40dB, 8dB steps\n");
350+
printf("\t[-i gain_db] # VGA(IF) gain, 0-62dB, 2dB steps\n");
351+
printf("\t[-x gain_db] # TX VGA gain, 0-47dB, 1dB steps\n");
352+
printf("\t[-s sample_rate_hz] # Sample rate in Hz (8/10/12.5/16/20MHz, default %lldMHz).\n", DEFAULT_SAMPLE_RATE_HZ/FREQ_ONE_MHZ);
336353
printf("\t[-n num_samples] # Number of samples to transfer (default is unlimited).\n");
337354
printf("\t[-b baseband_filter_bw_hz] # Set baseband filter bandwidth in MHz.\n\tPossible values: 1.75/2.5/3.5/5/5.5/6/7/8/9/10/12/14/15/20/24/28MHz, default < sample_rate_hz.\n" );
338355
}
@@ -375,7 +392,7 @@ int main(int argc, char** argv) {
375392
float time_diff;
376393
unsigned int lna_gain=8, vga_gain=20, txvga_gain=0;
377394

378-
while( (opt = getopt(argc, argv, "wr:t:f:a:p:s:n:b:l:i:x:")) != EOF )
395+
while( (opt = getopt(argc, argv, "wr:t:f:e:o:m:a:p:s:n:b:l:i:x:")) != EOF )
379396
{
380397
result = HACKRF_SUCCESS;
381398
switch( opt )
@@ -393,12 +410,27 @@ int main(int argc, char** argv) {
393410
transmit = true;
394411
path = optarg;
395412
break;
396-
413+
397414
case 'f':
398-
freq = true;
415+
automatic_tuning = true;
399416
result = parse_u64(optarg, &freq_hz);
400417
break;
401418

419+
case 'e':
420+
if_freq = true;
421+
result = parse_u64(optarg, &if_freq_hz);
422+
break;
423+
424+
case 'o':
425+
lo_freq = true;
426+
result = parse_u64(optarg, &lo_freq_hz);
427+
break;
428+
429+
case 'm':
430+
image_reject = true;
431+
result = parse_u32(optarg, &image_reject_selection);
432+
break;
433+
402434
case 'a':
403435
amp = true;
404436
result = parse_u32(optarg, &amp_enable);
@@ -457,31 +489,82 @@ int main(int argc, char** argv) {
457489
return EXIT_FAILURE;
458490
}
459491

460-
if( freq ) {
461-
if( (freq_hz >= FREQ_MAX_HZ) || (freq_hz < FREQ_MIN_HZ) )
492+
if (if_freq || lo_freq || image_reject) {
493+
/* explicit tuning selected */
494+
if (!if_freq) {
495+
printf("argument error: if_freq_hz must be specified for explicit tuning.\n");
496+
usage();
497+
return EXIT_FAILURE;
498+
}
499+
if (!image_reject) {
500+
printf("argument error: image_reject must be specified for explicit tuning.\n");
501+
usage();
502+
return EXIT_FAILURE;
503+
}
504+
if (!lo_freq && (image_reject_selection != RF_PATH_FILTER_BYPASS)) {
505+
printf("argument error: lo_freq_hz must be specified for explicit tuning unless image_reject is set to bypass.\n");
506+
usage();
507+
return EXIT_FAILURE;
508+
}
509+
if ((if_freq_hz > IF_MAX_HZ) || (if_freq_hz < IF_MIN_HZ)) {
510+
printf("argument error: if_freq_hz shall be between %llu and %llu.\n", IF_MIN_HZ, IF_MAX_HZ);
511+
usage();
512+
return EXIT_FAILURE;
513+
}
514+
if ((lo_freq_hz > LO_MAX_HZ) || (lo_freq_hz < LO_MIN_HZ)) {
515+
printf("argument error: lo_freq_hz shall be between %llu and %llu.\n", LO_MIN_HZ, LO_MAX_HZ);
516+
usage();
517+
return EXIT_FAILURE;
518+
}
519+
if (image_reject_selection > 2) {
520+
printf("argument error: image_reject must be 0, 1, or 2 .\n");
521+
usage();
522+
return EXIT_FAILURE;
523+
}
524+
if (automatic_tuning) {
525+
printf("warning: freq_hz ignored by explicit tuning selection.\n");
526+
automatic_tuning = false;
527+
}
528+
switch (image_reject_selection) {
529+
case RF_PATH_FILTER_BYPASS:
530+
freq_hz = if_freq_hz;
531+
break;
532+
case RF_PATH_FILTER_LOW_PASS:
533+
freq_hz = abs(if_freq_hz - lo_freq_hz);
534+
break;
535+
case RF_PATH_FILTER_HIGH_PASS:
536+
freq_hz = if_freq_hz + lo_freq_hz;
537+
break;
538+
default:
539+
freq_hz = DEFAULT_FREQ_HZ;
540+
break;
541+
}
542+
printf("explicit tuning specified for %lu Hz.\n", freq_hz);
543+
} else if (automatic_tuning) {
544+
if( (freq_hz > FREQ_MAX_HZ) || (freq_hz < FREQ_MIN_HZ) )
462545
{
463-
printf("argument error: set_freq_hz shall be between [%llu, %llu[.\n", FREQ_MIN_HZ, FREQ_MAX_HZ);
546+
printf("argument error: freq_hz shall be between %llu and %llu.\n", FREQ_MIN_HZ, FREQ_MAX_HZ);
464547
usage();
465548
return EXIT_FAILURE;
466549
}
467-
}else
468-
{
550+
} else {
469551
/* Use default freq */
470552
freq_hz = DEFAULT_FREQ_HZ;
553+
automatic_tuning = true;
471554
}
472555

473556
if( amp ) {
474557
if( amp_enable > 1 )
475558
{
476-
printf("argument error: set_amp shall be 0 or 1.\n");
559+
printf("argument error: amp_enable shall be 0 or 1.\n");
477560
usage();
478561
return EXIT_FAILURE;
479562
}
480563
}
481564

482565
if (antenna) {
483566
if (antenna_enable > 1) {
484-
printf("argument error: set_antenna shall be 0 or 1.\n");
567+
printf("argument error: antenna_enable shall be 0 or 1.\n");
485568
usage();
486569
return EXIT_FAILURE;
487570
}
@@ -645,12 +728,25 @@ int main(int argc, char** argv) {
645728
return EXIT_FAILURE;
646729
}
647730

648-
printf("call hackrf_set_freq(%lu Hz/%.03f MHz)\n", freq_hz, ((double)freq_hz/(double)FREQ_ONE_MHZ) );
649-
result = hackrf_set_freq(device, freq_hz);
650-
if( result != HACKRF_SUCCESS ) {
651-
printf("hackrf_set_freq() failed: %s (%d)\n", hackrf_error_name(result), result);
652-
usage();
653-
return EXIT_FAILURE;
731+
if (automatic_tuning) {
732+
printf("call hackrf_set_freq(%lu Hz/%.03f MHz)\n", freq_hz, ((double)freq_hz/(double)FREQ_ONE_MHZ) );
733+
result = hackrf_set_freq(device, freq_hz);
734+
if( result != HACKRF_SUCCESS ) {
735+
printf("hackrf_set_freq() failed: %s (%d)\n", hackrf_error_name(result), result);
736+
usage();
737+
return EXIT_FAILURE;
738+
}
739+
} else {
740+
printf("call hackrf_set_freq_explicit() with %lu Hz IF, %lu Hz LO, %s\n",
741+
if_freq_hz, lo_freq_hz, hackrf_filter_path_name(image_reject_selection));
742+
result = hackrf_set_freq_explicit(device, if_freq_hz, lo_freq_hz,
743+
image_reject_selection);
744+
if (result != HACKRF_SUCCESS) {
745+
printf("hackrf_set_freq_explicit() failed: %s (%d)\n",
746+
hackrf_error_name(result), result);
747+
usage();
748+
return EXIT_FAILURE;
749+
}
654750
}
655751

656752
if( amp ) {

0 commit comments

Comments
 (0)