mirror of
https://github.com/greatscottgadgets/hackrf.git
synced 2026-03-15 03:38:56 +01:00
Merge branch 'master' into streaming
This commit is contained in:
@@ -23,49 +23,42 @@
|
||||
|
||||
set(INSTALL_DEFAULT_BINDIR "bin" CACHE STRING "Appended to CMAKE_INSTALL_PREFIX")
|
||||
|
||||
INCLUDE(FindPkgConfig)
|
||||
pkg_check_modules(FFTW REQUIRED fftw3f)
|
||||
|
||||
if(MSVC)
|
||||
add_library(libgetopt_static STATIC
|
||||
../getopt/getopt.c
|
||||
)
|
||||
add_library(libgetopt_static STATIC
|
||||
../getopt/getopt.c
|
||||
)
|
||||
endif()
|
||||
|
||||
add_executable(hackrf_max2837 hackrf_max2837.c)
|
||||
install(TARGETS hackrf_max2837 RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
|
||||
|
||||
add_executable(hackrf_si5351c hackrf_si5351c.c)
|
||||
install(TARGETS hackrf_si5351c RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
|
||||
|
||||
add_executable(hackrf_transfer hackrf_transfer.c)
|
||||
install(TARGETS hackrf_transfer RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
|
||||
|
||||
add_executable(hackrf_rffc5071 hackrf_rffc5071.c)
|
||||
install(TARGETS hackrf_rffc5071 RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
|
||||
|
||||
add_executable(hackrf_spiflash hackrf_spiflash.c)
|
||||
install(TARGETS hackrf_spiflash RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
|
||||
|
||||
add_executable(hackrf_cpldjtag hackrf_cpldjtag.c)
|
||||
install(TARGETS hackrf_cpldjtag RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
|
||||
|
||||
add_executable(hackrf_info hackrf_info.c)
|
||||
install(TARGETS hackrf_info RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
|
||||
SET(TOOLS
|
||||
hackrf_max2837
|
||||
hackrf_si5351c
|
||||
hackrf_transfer
|
||||
hackrf_rffc5071
|
||||
hackrf_spiflash
|
||||
hackrf_cpldjtag
|
||||
hackrf_info
|
||||
hackrf_operacake
|
||||
hackrf_sweep
|
||||
)
|
||||
|
||||
if(NOT libhackrf_SOURCE_DIR)
|
||||
include_directories(${LIBHACKRF_INCLUDE_DIR})
|
||||
LIST(APPEND TOOLS_LINK_LIBS ${LIBHACKRF_LIBRARIES})
|
||||
include_directories(${LIBHACKRF_INCLUDE_DIR})
|
||||
LIST(APPEND TOOLS_LINK_LIBS ${LIBHACKRF_LIBRARIES})
|
||||
else()
|
||||
LIST(APPEND TOOLS_LINK_LIBS hackrf)
|
||||
LIST(APPEND TOOLS_LINK_LIBS hackrf)
|
||||
endif()
|
||||
|
||||
if(MSVC)
|
||||
LIST(APPEND TOOLS_LINK_LIBS libgetopt_static)
|
||||
LIST(APPEND TOOLS_LINK_LIBS libgetopt_static)
|
||||
endif()
|
||||
|
||||
LIST(APPEND TOOLS_LINK_LIBS m fftw3f)
|
||||
|
||||
target_link_libraries(hackrf_max2837 ${TOOLS_LINK_LIBS})
|
||||
target_link_libraries(hackrf_si5351c ${TOOLS_LINK_LIBS})
|
||||
target_link_libraries(hackrf_transfer ${TOOLS_LINK_LIBS})
|
||||
target_link_libraries(hackrf_rffc5071 ${TOOLS_LINK_LIBS})
|
||||
target_link_libraries(hackrf_spiflash ${TOOLS_LINK_LIBS})
|
||||
target_link_libraries(hackrf_cpldjtag ${TOOLS_LINK_LIBS})
|
||||
target_link_libraries(hackrf_info ${TOOLS_LINK_LIBS})
|
||||
foreach(tool ${TOOLS})
|
||||
add_executable(${tool} ${tool}.c)
|
||||
target_link_libraries(${tool} ${TOOLS_LINK_LIBS})
|
||||
install(TARGETS ${tool} RUNTIME DESTINATION ${INSTALL_DEFAULT_BINDIR})
|
||||
endforeach(tool)
|
||||
|
||||
@@ -32,9 +32,10 @@ int main(void)
|
||||
uint8_t board_id = BOARD_ID_INVALID;
|
||||
char version[255 + 1];
|
||||
read_partid_serialno_t read_partid_serialno;
|
||||
uint8_t operacakes[8];
|
||||
hackrf_device_list_t *list;
|
||||
hackrf_device* device;
|
||||
int i;
|
||||
int i, j;
|
||||
|
||||
result = hackrf_init();
|
||||
if (result != HACKRF_SUCCESS) {
|
||||
@@ -98,7 +99,20 @@ int main(void)
|
||||
read_partid_serialno.serial_no[1],
|
||||
read_partid_serialno.serial_no[2],
|
||||
read_partid_serialno.serial_no[3]);
|
||||
|
||||
|
||||
result = hackrf_get_operacake_boards(device, &operacakes[0]);
|
||||
if (result != HACKRF_SUCCESS) {
|
||||
fprintf(stderr, "hackrf_get_operacake_boards() failed: %s (%d)\n",
|
||||
hackrf_error_name(result), result);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
for(j=0; j<8; j++) {
|
||||
if(operacakes[j] == 0)
|
||||
break;
|
||||
printf("Operacake found, address: 0x%02x\n", operacakes[j]);
|
||||
|
||||
}
|
||||
|
||||
result = hackrf_close(device);
|
||||
if (result != HACKRF_SUCCESS) {
|
||||
fprintf(stderr, "hackrf_close() failed: %s (%d)\n",
|
||||
|
||||
158
host/hackrf-tools/src/hackrf_operacake.c
Normal file
158
host/hackrf-tools/src/hackrf_operacake.c
Normal file
@@ -0,0 +1,158 @@
|
||||
/*
|
||||
* Copyright 2016 Dominic Spill <dominicgs@gmail.com>
|
||||
*
|
||||
* 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 <hackrf.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <getopt.h>
|
||||
|
||||
#ifndef bool
|
||||
typedef int bool;
|
||||
#define true 1
|
||||
#define false 0
|
||||
#endif
|
||||
|
||||
static void usage() {
|
||||
printf("\nUsage:\n");
|
||||
printf("\t-s, --serial <s>: specify a particular device by serial number\n");
|
||||
printf("\t-d, --device <n>: specify a particular device by number\n");
|
||||
printf("\t-o, --address <n>: specify a particular operacake by address [default: 0x00]\n");
|
||||
printf("\t-a <n>: set port A connection\n");
|
||||
printf("\t-b <n>: set port B connection\n");
|
||||
printf("\t-v: verbose, list available operacake boards\n");
|
||||
}
|
||||
|
||||
static struct option long_options[] = {
|
||||
{ "device", no_argument, 0, 'd' },
|
||||
{ "serial", no_argument, 0, 's' },
|
||||
{ "address", no_argument, 0, 'o' },
|
||||
{ 0, 0, 0, 0 },
|
||||
};
|
||||
|
||||
int parse_int(char* const s, uint16_t* const value) {
|
||||
char* s_end = s;
|
||||
const long long_value = strtol(s, &s_end, 10);
|
||||
if( (s != s_end) && (*s_end == 0) ) {
|
||||
*value = (uint16_t)long_value;
|
||||
return HACKRF_SUCCESS;
|
||||
} else {
|
||||
return HACKRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int opt;
|
||||
const char* serial_number = NULL;
|
||||
int device_index = 0;
|
||||
int operacake_address = 0;
|
||||
int port_a = 0;
|
||||
int port_b = 0;
|
||||
int verbose = 0;
|
||||
uint8_t operacakes[8];
|
||||
int i = 0;
|
||||
hackrf_device* device = NULL;
|
||||
int option_index = 0;
|
||||
|
||||
int result = hackrf_init();
|
||||
if( result ) {
|
||||
printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
while( (opt = getopt_long(argc, argv, "d:s:o:a:b:v", long_options, &option_index)) != EOF ) {
|
||||
switch( opt ) {
|
||||
case 'd':
|
||||
device_index = atoi(optarg);
|
||||
break;
|
||||
|
||||
case 's':
|
||||
serial_number = optarg;
|
||||
break;
|
||||
|
||||
case 'o':
|
||||
operacake_address = atoi(optarg);
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
port_a = atoi(optarg);
|
||||
break;
|
||||
|
||||
case 'b':
|
||||
port_b = atoi(optarg);
|
||||
break;
|
||||
|
||||
case 'v':
|
||||
verbose = 1;
|
||||
break;
|
||||
|
||||
default:
|
||||
usage();
|
||||
}
|
||||
|
||||
if( result != HACKRF_SUCCESS ) {
|
||||
printf("argument error: %s (%d)\n", hackrf_error_name(result), result);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if(serial_number != NULL) {
|
||||
result = hackrf_open_by_serial(serial_number, &device);
|
||||
} else {
|
||||
hackrf_device_list_t* device_list = hackrf_device_list();
|
||||
if(device_list->devicecount <= 0) {
|
||||
result = HACKRF_ERROR_NOT_FOUND;
|
||||
} else {
|
||||
result = hackrf_device_list_open(device_list, device_index, &device);
|
||||
}
|
||||
}
|
||||
|
||||
if( result ) {
|
||||
printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if(verbose) {
|
||||
hackrf_get_operacake_boards(device, operacakes);
|
||||
printf("Operacakes found:\n");
|
||||
for(i=0; i<8; i++) {
|
||||
if(operacakes[i] !=0)
|
||||
printf("%d\n", operacakes[i]);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
result = hackrf_set_operacake_ports(device, operacake_address, port_a, port_b);
|
||||
if( result ) {
|
||||
printf("hackrf_set_operacake_ports() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = hackrf_close(device);
|
||||
if( result ) {
|
||||
printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||
return -1;
|
||||
}
|
||||
|
||||
hackrf_exit();
|
||||
|
||||
return 0;
|
||||
}
|
||||
547
host/hackrf-tools/src/hackrf_sweep.c
Normal file
547
host/hackrf-tools/src/hackrf_sweep.c
Normal file
@@ -0,0 +1,547 @@
|
||||
/*
|
||||
* Copyright 2016 Dominic Spill <dominicgs@gmail.com>
|
||||
* Copyright 2016 Mike Walters <mike@flomp.net>
|
||||
*
|
||||
* 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 <hackrf.h>
|
||||
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <getopt.h>
|
||||
#include <time.h>
|
||||
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <fcntl.h>
|
||||
#include <errno.h>
|
||||
#include <fftw3.h>
|
||||
#include <math.h>
|
||||
|
||||
#ifndef bool
|
||||
typedef int bool;
|
||||
#define true 1
|
||||
#define false 0
|
||||
#endif
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <windows.h>
|
||||
|
||||
#ifdef _MSC_VER
|
||||
|
||||
#ifdef _WIN64
|
||||
typedef int64_t ssize_t;
|
||||
#else
|
||||
typedef int32_t ssize_t;
|
||||
#endif
|
||||
|
||||
#define strtoull _strtoui64
|
||||
#define snprintf _snprintf
|
||||
|
||||
int gettimeofday(struct timeval *tv, void* ignored) {
|
||||
FILETIME ft;
|
||||
unsigned __int64 tmp = 0;
|
||||
if (NULL != tv) {
|
||||
GetSystemTimeAsFileTime(&ft);
|
||||
tmp |= ft.dwHighDateTime;
|
||||
tmp <<= 32;
|
||||
tmp |= ft.dwLowDateTime;
|
||||
tmp /= 10;
|
||||
tmp -= 11644473600000000Ui64;
|
||||
tv->tv_sec = (long)(tmp / 1000000UL);
|
||||
tv->tv_usec = (long)(tmp % 1000000UL);
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#if defined(__GNUC__)
|
||||
#include <unistd.h>
|
||||
#include <sys/time.h>
|
||||
#endif
|
||||
|
||||
#include <signal.h>
|
||||
|
||||
#define FD_BUFFER_SIZE (8*1024)
|
||||
|
||||
#define FREQ_ONE_MHZ (1000000ull)
|
||||
|
||||
#define FREQ_MIN_HZ (0ull) /* 0 Hz */
|
||||
#define FREQ_MAX_HZ (7250000000ull) /* 7250MHz */
|
||||
|
||||
#define DEFAULT_SAMPLE_RATE_HZ (20000000) /* 20MHz default sample rate */
|
||||
#define DEFAULT_BASEBAND_FILTER_BANDWIDTH (15000000) /* 5MHz default */
|
||||
|
||||
#define FREQ_STEP (DEFAULT_SAMPLE_RATE_HZ / FREQ_ONE_MHZ)
|
||||
#define MAX_FREQ_COUNT 1000
|
||||
|
||||
#define DEFAULT_SAMPLE_COUNT 0x4000
|
||||
|
||||
#if defined _WIN32
|
||||
#define sleep(a) Sleep( (a*1000) )
|
||||
#endif
|
||||
|
||||
static float TimevalDiff(const struct timeval *a, const struct timeval *b) {
|
||||
return (a->tv_sec - b->tv_sec) + 1e-6f * (a->tv_usec - b->tv_usec);
|
||||
}
|
||||
|
||||
int parse_u32(char* s, uint32_t* const value) {
|
||||
uint_fast8_t base = 10;
|
||||
char* s_end;
|
||||
uint64_t ulong_value;
|
||||
|
||||
if( strlen(s) > 2 ) {
|
||||
if( s[0] == '0' ) {
|
||||
if( (s[1] == 'x') || (s[1] == 'X') ) {
|
||||
base = 16;
|
||||
s += 2;
|
||||
} else if( (s[1] == 'b') || (s[1] == 'B') ) {
|
||||
base = 2;
|
||||
s += 2;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
s_end = s;
|
||||
ulong_value = strtoul(s, &s_end, base);
|
||||
if( (s != s_end) && (*s_end == 0) ) {
|
||||
*value = (uint32_t)ulong_value;
|
||||
return HACKRF_SUCCESS;
|
||||
} else {
|
||||
return HACKRF_ERROR_INVALID_PARAM;
|
||||
}
|
||||
}
|
||||
|
||||
int parse_u32_range(char* s, uint32_t* const value_min, uint32_t* const value_max) {
|
||||
int result;
|
||||
|
||||
char *sep = strchr(s, ':');
|
||||
if (!sep)
|
||||
return HACKRF_ERROR_INVALID_PARAM;
|
||||
|
||||
*sep = 0;
|
||||
|
||||
result = parse_u32(s, value_min);
|
||||
if (result != HACKRF_SUCCESS)
|
||||
return result;
|
||||
result = parse_u32(sep + 1, value_max);
|
||||
if (result != HACKRF_SUCCESS)
|
||||
return result;
|
||||
|
||||
return HACKRF_SUCCESS;
|
||||
}
|
||||
|
||||
volatile bool do_exit = false;
|
||||
|
||||
FILE* fd = NULL;
|
||||
volatile uint32_t byte_count = 0;
|
||||
|
||||
struct timeval time_start;
|
||||
struct timeval t_start;
|
||||
|
||||
bool amp = false;
|
||||
uint32_t amp_enable;
|
||||
|
||||
bool antenna = false;
|
||||
uint32_t antenna_enable;
|
||||
|
||||
uint32_t freq_min;
|
||||
uint32_t freq_max;
|
||||
|
||||
int fftSize;
|
||||
fftwf_complex *fftwIn = NULL;
|
||||
fftwf_complex *fftwOut = NULL;
|
||||
fftwf_plan fftwPlan = NULL;
|
||||
float* pwr;
|
||||
float* window;
|
||||
|
||||
float logPower(fftwf_complex in, float scale)
|
||||
{
|
||||
float re = in[0] * scale;
|
||||
float im = in[1] * scale;
|
||||
float magsq = re * re + im * im;
|
||||
return log2f(magsq) * 10.0f / log2(10.0f);
|
||||
}
|
||||
|
||||
int rx_callback(hackrf_transfer* transfer) {
|
||||
/* This is where we need to do interesting things with the samples
|
||||
* FFT
|
||||
* Throw away unused bins
|
||||
* write output to pipe
|
||||
*/
|
||||
ssize_t bytes_to_write;
|
||||
ssize_t bytes_written;
|
||||
int8_t* buf;
|
||||
float frequency;
|
||||
int i, j;
|
||||
|
||||
if( fd != NULL ) {
|
||||
byte_count += transfer->valid_length;
|
||||
bytes_to_write = transfer->valid_length;
|
||||
buf = (int8_t*) transfer->buffer;
|
||||
for(j=0; j<16; j++) {
|
||||
if(buf[0] == 0x7F && buf[1] == 0x7F) {
|
||||
frequency = *(uint16_t*)&buf[2];
|
||||
}
|
||||
/* copy to fftwIn as floats */
|
||||
buf += 16384 - (fftSize * 2);
|
||||
for(i=0; i < fftSize; i++) {
|
||||
fftwIn[i][0] = buf[i*2] * window[i] * 1.0f / 128.0f;
|
||||
fftwIn[i][1] = buf[i*2+1] * window[i] * 1.0f / 128.0f;
|
||||
}
|
||||
buf += fftSize * 2;
|
||||
fftwf_execute(fftwPlan);
|
||||
for (i=0; i < fftSize; i++) {
|
||||
// Start from the middle of the FFTW array and wrap
|
||||
// to rearrange the data
|
||||
int k = i ^ (fftSize >> 1);
|
||||
pwr[i] = logPower(fftwOut[k], 1.0f / fftSize);
|
||||
}
|
||||
fwrite(&frequency, sizeof(float), 1, stdout);
|
||||
fwrite(pwr, sizeof(float), fftSize, stdout);
|
||||
}
|
||||
|
||||
bytes_written = fwrite(transfer->buffer, 1, bytes_to_write, fd);
|
||||
if (bytes_written != bytes_to_write) {
|
||||
return -1;
|
||||
} else {
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
static void usage() {
|
||||
fprintf(stderr, "Usage:\n");
|
||||
fprintf(stderr, "\t[-d serial_number] # Serial number of desired HackRF.\n");
|
||||
fprintf(stderr, "\t[-a amp_enable] # RX/TX RF amplifier 1=Enable, 0=Disable.\n");
|
||||
fprintf(stderr, "\t[-f freq_min:freq_max # Specify minimum & maximum sweep frequencies (MHz).\n");
|
||||
fprintf(stderr, "\t[-p antenna_enable] # Antenna port power, 1=Enable, 0=Disable.\n");
|
||||
fprintf(stderr, "\t[-l gain_db] # RX LNA (IF) gain, 0-40dB, 8dB steps\n");
|
||||
fprintf(stderr, "\t[-g gain_db] # RX VGA (baseband) gain, 0-62dB, 2dB steps\n");
|
||||
fprintf(stderr, "\t[-x gain_db] # TX VGA (IF) gain, 0-47dB, 1dB steps\n");
|
||||
fprintf(stderr, "\t[-n num_samples] # Number of samples per frequency, 0-4294967296\n");
|
||||
}
|
||||
|
||||
static hackrf_device* device = NULL;
|
||||
|
||||
#ifdef _MSC_VER
|
||||
BOOL WINAPI
|
||||
sighandler(int signum) {
|
||||
if (CTRL_C_EVENT == signum) {
|
||||
fprintf(stderr, "Caught signal %d\n", signum);
|
||||
do_exit = true;
|
||||
return TRUE;
|
||||
}
|
||||
return FALSE;
|
||||
}
|
||||
#else
|
||||
void sigint_callback_handler(int signum) {
|
||||
fprintf(stderr, "Caught signal %d\n", signum);
|
||||
do_exit = true;
|
||||
}
|
||||
#endif
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
int opt, i, result, ifreq = 0;
|
||||
bool odd;
|
||||
const char* path = "/dev/null";
|
||||
const char* serial_number = NULL;
|
||||
int exit_code = EXIT_SUCCESS;
|
||||
struct timeval t_end;
|
||||
float time_diff;
|
||||
unsigned int lna_gain=16, vga_gain=20, txvga_gain=0;
|
||||
uint16_t frequencies[MAX_FREQ_COUNT];
|
||||
uint32_t num_samples = DEFAULT_SAMPLE_COUNT;
|
||||
|
||||
while( (opt = getopt(argc, argv, "a:f:p:l:g:x:d:n:")) != EOF ) {
|
||||
result = HACKRF_SUCCESS;
|
||||
switch( opt )
|
||||
{
|
||||
case 'd':
|
||||
serial_number = optarg;
|
||||
break;
|
||||
|
||||
case 'a':
|
||||
amp = true;
|
||||
result = parse_u32(optarg, &_enable);
|
||||
break;
|
||||
|
||||
case 'f':
|
||||
result = parse_u32_range(optarg, &freq_min, &freq_max);
|
||||
fprintf(stderr, "Scanning %uMHz to %uMHz\n", freq_min, freq_max);
|
||||
frequencies[ifreq++] = freq_min;
|
||||
odd = true;
|
||||
while(frequencies[ifreq-1] <= freq_max) {
|
||||
if (odd)
|
||||
frequencies[ifreq] = frequencies[ifreq-1] + FREQ_STEP / 4;
|
||||
else
|
||||
frequencies[ifreq] = frequencies[ifreq-1] + 3*(FREQ_STEP/4);
|
||||
ifreq++;
|
||||
odd = !odd;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
antenna = true;
|
||||
result = parse_u32(optarg, &antenna_enable);
|
||||
break;
|
||||
|
||||
case 'l':
|
||||
result = parse_u32(optarg, &lna_gain);
|
||||
break;
|
||||
|
||||
case 'g':
|
||||
result = parse_u32(optarg, &vga_gain);
|
||||
break;
|
||||
|
||||
case 'x':
|
||||
result = parse_u32(optarg, &txvga_gain);
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
result = parse_u32(optarg, &num_samples);
|
||||
break;
|
||||
|
||||
default:
|
||||
fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if( result != HACKRF_SUCCESS ) {
|
||||
fprintf(stderr, "argument error: '-%c %s' %s (%d)\n", opt, optarg, hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (lna_gain % 8)
|
||||
fprintf(stderr, "warning: lna_gain (-l) must be a multiple of 8\n");
|
||||
|
||||
if (vga_gain % 2)
|
||||
fprintf(stderr, "warning: vga_gain (-g) must be a multiple of 2\n");
|
||||
|
||||
if (num_samples % 0x4000) {
|
||||
fprintf(stderr, "warning: num_samples (-n) must be a multiple of 16384\n");
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if( amp ) {
|
||||
if( amp_enable > 1 ) {
|
||||
fprintf(stderr, "argument error: amp_enable shall be 0 or 1.\n");
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (antenna) {
|
||||
if (antenna_enable > 1) {
|
||||
fprintf(stderr, "argument error: antenna_enable shall be 0 or 1.\n");
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (ifreq == 0) {
|
||||
fprintf(stderr, "argument error: must specify sweep frequency range (-f).\n");
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fftSize = 64;
|
||||
fftwIn = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize);
|
||||
fftwOut = (fftwf_complex*)fftwf_malloc(sizeof(fftwf_complex) * fftSize);
|
||||
fftwPlan = fftwf_plan_dft_1d(fftSize, fftwIn, fftwOut, FFTW_FORWARD, FFTW_MEASURE);
|
||||
pwr = (float*)fftwf_malloc(sizeof(float) * fftSize);
|
||||
window = (float*)fftwf_malloc(sizeof(float) * fftSize);
|
||||
for (i = 0; i < fftSize; i++) {
|
||||
window[i] = 0.5f * (1.0f - cos(2 * M_PI * i / (fftSize - 1)));
|
||||
}
|
||||
|
||||
result = hackrf_init();
|
||||
if( result != HACKRF_SUCCESS ) {
|
||||
fprintf(stderr, "hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
result = hackrf_open_by_serial(serial_number, &device);
|
||||
if( result != HACKRF_SUCCESS ) {
|
||||
fprintf(stderr, "hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fd = fopen(path, "wb");
|
||||
if( fd == NULL ) {
|
||||
fprintf(stderr, "Failed to open file: %s\n", path);
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
/* Change fd buffer to have bigger one to store or read data on/to HDD */
|
||||
result = setvbuf(fd , NULL , _IOFBF , FD_BUFFER_SIZE);
|
||||
if( result != 0 ) {
|
||||
fprintf(stderr, "setvbuf() failed: %d\n", result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
#ifdef _MSC_VER
|
||||
SetConsoleCtrlHandler( (PHANDLER_ROUTINE) sighandler, TRUE );
|
||||
#else
|
||||
signal(SIGINT, &sigint_callback_handler);
|
||||
signal(SIGILL, &sigint_callback_handler);
|
||||
signal(SIGFPE, &sigint_callback_handler);
|
||||
signal(SIGSEGV, &sigint_callback_handler);
|
||||
signal(SIGTERM, &sigint_callback_handler);
|
||||
signal(SIGABRT, &sigint_callback_handler);
|
||||
#endif
|
||||
fprintf(stderr, "call hackrf_sample_rate_set(%.03f MHz)\n",
|
||||
((float)DEFAULT_SAMPLE_RATE_HZ/(float)FREQ_ONE_MHZ));
|
||||
result = hackrf_set_sample_rate_manual(device, DEFAULT_SAMPLE_RATE_HZ, 1);
|
||||
if( result != HACKRF_SUCCESS ) {
|
||||
fprintf(stderr, "hackrf_sample_rate_set() failed: %s (%d)\n",
|
||||
hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fprintf(stderr, "call hackrf_baseband_filter_bandwidth_set(%.03f MHz)\n",
|
||||
((float)DEFAULT_BASEBAND_FILTER_BANDWIDTH/(float)FREQ_ONE_MHZ));
|
||||
result = hackrf_set_baseband_filter_bandwidth(device, DEFAULT_BASEBAND_FILTER_BANDWIDTH);
|
||||
if( result != HACKRF_SUCCESS ) {
|
||||
fprintf(stderr, "hackrf_baseband_filter_bandwidth_set() failed: %s (%d)\n",
|
||||
hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
result = hackrf_set_vga_gain(device, vga_gain);
|
||||
result |= hackrf_set_lna_gain(device, lna_gain);
|
||||
result |= hackrf_start_rx(device, rx_callback, NULL);
|
||||
if (result != HACKRF_SUCCESS) {
|
||||
fprintf(stderr, "hackrf_start_?x() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
result = hackrf_init_sweep(device, frequencies, ifreq, num_samples);
|
||||
if( result != HACKRF_SUCCESS ) {
|
||||
fprintf(stderr, "hackrf_init_sweep() failed: %s (%d)\n",
|
||||
hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if (amp) {
|
||||
fprintf(stderr, "call hackrf_set_amp_enable(%u)\n", amp_enable);
|
||||
result = hackrf_set_amp_enable(device, (uint8_t)amp_enable);
|
||||
if (result != HACKRF_SUCCESS) {
|
||||
fprintf(stderr, "hackrf_set_amp_enable() failed: %s (%d)\n",
|
||||
hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
if (antenna) {
|
||||
fprintf(stderr, "call hackrf_set_antenna_enable(%u)\n", antenna_enable);
|
||||
result = hackrf_set_antenna_enable(device, (uint8_t)antenna_enable);
|
||||
if (result != HACKRF_SUCCESS) {
|
||||
fprintf(stderr, "hackrf_set_antenna_enable() failed: %s (%d)\n",
|
||||
hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
}
|
||||
|
||||
gettimeofday(&t_start, NULL);
|
||||
gettimeofday(&time_start, NULL);
|
||||
|
||||
fprintf(stderr, "Stop with Ctrl-C\n");
|
||||
while((hackrf_is_streaming(device) == HACKRF_TRUE) && (do_exit == false)) {
|
||||
uint32_t byte_count_now;
|
||||
struct timeval time_now;
|
||||
float time_difference, rate;
|
||||
sleep(1);
|
||||
|
||||
gettimeofday(&time_now, NULL);
|
||||
|
||||
byte_count_now = byte_count;
|
||||
byte_count = 0;
|
||||
|
||||
time_difference = TimevalDiff(&time_now, &time_start);
|
||||
rate = (float)byte_count_now / time_difference;
|
||||
fprintf(stderr, "%4.1f MiB / %5.3f sec = %4.1f MiB/second\n",
|
||||
(byte_count_now / 1e6f), time_difference, (rate / 1e6f) );
|
||||
|
||||
time_start = time_now;
|
||||
|
||||
if (byte_count_now == 0) {
|
||||
exit_code = EXIT_FAILURE;
|
||||
fprintf(stderr, "\nCouldn't transfer any bytes for one second.\n");
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
result = hackrf_is_streaming(device);
|
||||
if (do_exit) {
|
||||
fprintf(stderr, "\nUser cancel, exiting...\n");
|
||||
} else {
|
||||
fprintf(stderr, "\nExiting... hackrf_is_streaming() result: %s (%d)\n",
|
||||
hackrf_error_name(result), result);
|
||||
}
|
||||
|
||||
gettimeofday(&t_end, NULL);
|
||||
time_diff = TimevalDiff(&t_end, &t_start);
|
||||
fprintf(stderr, "Total time: %5.5f s\n", time_diff);
|
||||
|
||||
if(device != NULL) {
|
||||
result = hackrf_stop_rx(device);
|
||||
if(result != HACKRF_SUCCESS) {
|
||||
fprintf(stderr, "hackrf_stop_rx() failed: %s (%d)\n",
|
||||
hackrf_error_name(result), result);
|
||||
} else {
|
||||
fprintf(stderr, "hackrf_stop_rx() done\n");
|
||||
}
|
||||
|
||||
result = hackrf_close(device);
|
||||
if(result != HACKRF_SUCCESS) {
|
||||
fprintf(stderr, "hackrf_close() failed: %s (%d)\n",
|
||||
hackrf_error_name(result), result);
|
||||
} else {
|
||||
fprintf(stderr, "hackrf_close() done\n");
|
||||
}
|
||||
|
||||
hackrf_exit();
|
||||
fprintf(stderr, "hackrf_exit() done\n");
|
||||
}
|
||||
|
||||
if(fd != NULL) {
|
||||
fclose(fd);
|
||||
fd = NULL;
|
||||
fprintf(stderr, "fclose(fd) done\n");
|
||||
}
|
||||
fprintf(stderr, "exit\n");
|
||||
return exit_code;
|
||||
}
|
||||
@@ -83,16 +83,16 @@ int gettimeofday(struct timeval *tv, void* ignored)
|
||||
|
||||
#define FD_BUFFER_SIZE (8*1024)
|
||||
|
||||
#define FREQ_ONE_MHZ (1000000ull)
|
||||
#define FREQ_ONE_MHZ (1000000ll)
|
||||
|
||||
#define DEFAULT_FREQ_HZ (900000000ull) /* 900MHz */
|
||||
#define DEFAULT_FREQ_HZ (900000000ll) /* 900MHz */
|
||||
#define FREQ_MIN_HZ (0ull) /* 0 Hz */
|
||||
#define FREQ_MAX_HZ (7250000000ull) /* 7250MHz */
|
||||
#define IF_MIN_HZ (2150000000ull)
|
||||
#define IF_MAX_HZ (2750000000ull)
|
||||
#define LO_MIN_HZ (84375000ull)
|
||||
#define LO_MAX_HZ (5400000000ull)
|
||||
#define DEFAULT_LO_HZ (1000000000ull)
|
||||
#define FREQ_MAX_HZ (7250000000ll) /* 7250MHz */
|
||||
#define IF_MIN_HZ (2150000000ll)
|
||||
#define IF_MAX_HZ (2750000000ll)
|
||||
#define LO_MIN_HZ (84375000ll)
|
||||
#define LO_MAX_HZ (5400000000ll)
|
||||
#define DEFAULT_LO_HZ (1000000000ll)
|
||||
|
||||
#define DEFAULT_SAMPLE_RATE_HZ (10000000) /* 10MHz default sample rate */
|
||||
|
||||
@@ -107,6 +107,18 @@ int gettimeofday(struct timeval *tv, void* ignored)
|
||||
#define sleep(a) Sleep( (a*1000) )
|
||||
#endif
|
||||
|
||||
typedef enum {
|
||||
TRANSCEIVER_MODE_OFF = 0,
|
||||
TRANSCEIVER_MODE_RX = 1,
|
||||
TRANSCEIVER_MODE_TX = 2,
|
||||
TRANSCEIVER_MODE_SS = 3,
|
||||
} transceiver_mode_t;
|
||||
|
||||
typedef enum {
|
||||
HW_SYNC_MODE_OFF = 0,
|
||||
HW_SYNC_MODE_ON = 1,
|
||||
} hw_sync_mode_t;
|
||||
|
||||
/* WAVE or RIFF WAVE file format containing IQ 2x8bits data for HackRF compatible with SDR# Wav IQ file */
|
||||
typedef struct
|
||||
{
|
||||
@@ -296,6 +308,8 @@ volatile uint32_t byte_count = 0;
|
||||
bool signalsource = false;
|
||||
uint32_t amplitude = 0;
|
||||
|
||||
bool hw_sync = false;
|
||||
|
||||
bool receive = false;
|
||||
bool receive_wav = false;
|
||||
uint64_t stream_size = 0;
|
||||
@@ -309,13 +323,13 @@ struct timeval time_start;
|
||||
struct timeval t_start;
|
||||
|
||||
bool automatic_tuning = false;
|
||||
uint64_t freq_hz;
|
||||
int64_t freq_hz;
|
||||
|
||||
bool if_freq = false;
|
||||
uint64_t if_freq_hz;
|
||||
int64_t if_freq_hz;
|
||||
|
||||
bool lo_freq = false;
|
||||
uint64_t lo_freq_hz = DEFAULT_LO_HZ;
|
||||
int64_t lo_freq_hz = DEFAULT_LO_HZ;
|
||||
|
||||
bool image_reject = false;
|
||||
uint32_t image_reject_selection;
|
||||
@@ -477,6 +491,7 @@ static void usage() {
|
||||
printf("\t[-R] # Repeat TX mode (default is off) \n");
|
||||
printf("\t[-b baseband_filter_bw_hz] # Set baseband filter bandwidth in Hz.\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" );
|
||||
printf("\t[-C ppm] # Set Internal crystal clock error in ppm.\n");
|
||||
printf("\t[-H] # Synchronise USB transfer using GPIO pins.\n");
|
||||
}
|
||||
|
||||
static hackrf_device* device = NULL;
|
||||
@@ -520,11 +535,14 @@ int main(int argc, char** argv) {
|
||||
float time_diff;
|
||||
unsigned int lna_gain=8, vga_gain=20, txvga_gain=0;
|
||||
|
||||
while( (opt = getopt(argc, argv, "wr:t:f:i:o:m:a:p:s:n:b:l:g:x:c:d:C:RS:")) != EOF )
|
||||
while( (opt = getopt(argc, argv, "Hwr:t:f:i:o:m:a:p:s:n:b:l:g:x:c:d:C:RS:")) != EOF )
|
||||
{
|
||||
result = HACKRF_SUCCESS;
|
||||
switch( opt )
|
||||
{
|
||||
case 'H':
|
||||
hw_sync = true;
|
||||
break;
|
||||
case 'w':
|
||||
receive_wav = true;
|
||||
break;
|
||||
@@ -718,7 +736,7 @@ int main(int argc, char** argv) {
|
||||
freq_hz = if_freq_hz;
|
||||
break;
|
||||
case RF_PATH_FILTER_LOW_PASS:
|
||||
freq_hz = abs(if_freq_hz - lo_freq_hz);
|
||||
freq_hz = labs(if_freq_hz - lo_freq_hz);
|
||||
break;
|
||||
case RF_PATH_FILTER_HIGH_PASS:
|
||||
freq_hz = if_freq_hz + lo_freq_hz;
|
||||
@@ -936,6 +954,15 @@ int main(int argc, char** argv) {
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
fprintf(stderr, "call hackrf_set_hw_sync_mode(%d)\n",
|
||||
hw_sync);
|
||||
result = hackrf_set_hw_sync_mode(device, hw_sync ? HW_SYNC_MODE_ON : HW_SYNC_MODE_OFF);
|
||||
if( result != HACKRF_SUCCESS ) {
|
||||
fprintf(stderr, "hackrf_set_hw_sync_mode() failed: %s (%d)\n", hackrf_error_name(result), result);
|
||||
usage();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
|
||||
if( transceiver_mode == TRANSCEIVER_MODE_RX ) {
|
||||
result = hackrf_set_vga_gain(device, vga_gain);
|
||||
result |= hackrf_set_lna_gain(device, lna_gain);
|
||||
|
||||
Reference in New Issue
Block a user