From 980abca0b537bb50e7977cb810396be0312278ef Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 29 Jan 2017 17:29:45 -0700 Subject: [PATCH 01/19] Fixed cmake bug that prevented hackrf_sweep from being built. --- host/hackrf-tools/src/CMakeLists.txt | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/host/hackrf-tools/src/CMakeLists.txt b/host/hackrf-tools/src/CMakeLists.txt index d8f602c1..00a173fc 100644 --- a/host/hackrf-tools/src/CMakeLists.txt +++ b/host/hackrf-tools/src/CMakeLists.txt @@ -25,16 +25,6 @@ set(INSTALL_DEFAULT_BINDIR "bin" CACHE STRING "Appended to CMAKE_INSTALL_PREFIX" INCLUDE(FindPkgConfig) -if(MSVC) - add_library(libgetopt_static STATIC - ../getopt/getopt.c - ) -else() - pkg_check_modules(FFTW REQUIRED fftw3f) - LIST(APPEND TOOLS hackrf_sweep) - LIST(APPEND TOOLS_LINK_LIBS m fftw3f) -endif() - SET(TOOLS hackrf_max2837 hackrf_si5351c @@ -46,6 +36,16 @@ SET(TOOLS hackrf_operacake ) +if(MSVC) + add_library(libgetopt_static STATIC + ../getopt/getopt.c + ) +else() + pkg_check_modules(FFTW REQUIRED fftw3f) + LIST(APPEND TOOLS hackrf_sweep) + LIST(APPEND TOOLS_LINK_LIBS m fftw3f) +endif() + if(NOT libhackrf_SOURCE_DIR) include_directories(${LIBHACKRF_INCLUDE_DIR}) LIST(APPEND TOOLS_LINK_LIBS ${LIBHACKRF_LIBRARIES}) From 9211b707702117bf305cb3aa6ce3fe621fa134d7 Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 29 Jan 2017 17:37:29 -0700 Subject: [PATCH 02/19] Added -h/-? option for help to all command line tools. --- host/hackrf-tools/src/hackrf_cpldjtag.c | 11 +++++++++-- host/hackrf-tools/src/hackrf_max2837.c | 20 ++++++++++++++------ host/hackrf-tools/src/hackrf_operacake.c | 10 +++++++++- host/hackrf-tools/src/hackrf_rffc5071.c | 18 +++++++++++++----- host/hackrf-tools/src/hackrf_si5351c.c | 18 +++++++++++++----- host/hackrf-tools/src/hackrf_spiflash.c | 7 +++++-- host/hackrf-tools/src/hackrf_sweep.c | 8 +++++++- host/hackrf-tools/src/hackrf_transfer.c | 6 ++++-- 8 files changed, 74 insertions(+), 24 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_cpldjtag.c b/host/hackrf-tools/src/hackrf_cpldjtag.c index b76c9349..3c9a89e1 100644 --- a/host/hackrf-tools/src/hackrf_cpldjtag.c +++ b/host/hackrf-tools/src/hackrf_cpldjtag.c @@ -45,6 +45,7 @@ uint8_t data[MAX_XSVF_LENGTH]; static struct option long_options[] = { { "xsvf", required_argument, 0, 'x' }, + { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -79,7 +80,8 @@ int parse_int(char* s, uint32_t* const value) static void usage() { printf("Usage:\n"); - printf("\t-x : XSVF file to be written to CPLD.\n"); + printf("\t-h, --help: this help\n"); + printf("\t-x, --xsvf : XSVF file to be written to CPLD.\n"); printf("\t-d : Serial number of device, if multiple devices\n"); } @@ -97,7 +99,7 @@ int main(int argc, char** argv) ssize_t bytes_read; uint8_t* pdata = &data[0]; - while ((opt = getopt_long(argc, argv, "x:d:", long_options, + while ((opt = getopt_long(argc, argv, "x:d:h?", long_options, &option_index)) != EOF) { switch (opt) { case 'x': @@ -107,8 +109,13 @@ int main(int argc, char** argv) case 'd': serial_number = optarg; break; + case 'h': + case '?': + usage(); + return EXIT_SUCCESS; default: + fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); usage(); return EXIT_FAILURE; } diff --git a/host/hackrf-tools/src/hackrf_max2837.c b/host/hackrf-tools/src/hackrf_max2837.c index d8fb75f5..38ac01be 100644 --- a/host/hackrf-tools/src/hackrf_max2837.c +++ b/host/hackrf-tools/src/hackrf_max2837.c @@ -28,6 +28,7 @@ static void usage() { printf("\nUsage:\n"); + printf("\t-h, --help: this help\n"); printf("\t-n, --register : set register number for subsequent read/write operations\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-w, --write : write register specified by last -n argument with value \n"); @@ -41,6 +42,7 @@ static struct option long_options[] = { { "register", required_argument, 0, 'n' }, { "write", required_argument, 0, 'w' }, { "read", no_argument, 0, 'r' }, + { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -127,16 +129,16 @@ int main(int argc, char** argv) { int result = hackrf_init(); if( result ) { printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; + return EXIT_FAILURE; } result = hackrf_open(&device); if( result ) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; + return EXIT_FAILURE; } - while( (opt = getopt_long(argc, argv, "n:rw:", long_options, &option_index)) != EOF ) { + while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); @@ -157,8 +159,14 @@ int main(int argc, char** argv) { } break; - default: + case 'h': + case '?': usage(); + return EXIT_SUCCESS; + default: + fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); + usage(); + return EXIT_FAILURE; } if( result != HACKRF_SUCCESS ) { @@ -170,10 +178,10 @@ int main(int argc, char** argv) { result = hackrf_close(device); if( result ) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; + return EXIT_FAILURE; } hackrf_exit(); - return 0; + return EXIT_SUCCESS; } diff --git a/host/hackrf-tools/src/hackrf_operacake.c b/host/hackrf-tools/src/hackrf_operacake.c index 086af5cb..6f8e2e1b 100644 --- a/host/hackrf-tools/src/hackrf_operacake.c +++ b/host/hackrf-tools/src/hackrf_operacake.c @@ -33,6 +33,7 @@ typedef int bool; static void usage() { printf("\nUsage:\n"); + printf("\t-h, --help: this help\n"); printf("\t-s, --serial : specify a particular device by serial number\n"); printf("\t-d, --device : specify a particular device by number\n"); printf("\t-o, --address : specify a particular operacake by address [default: 0x00]\n"); @@ -45,6 +46,7 @@ static struct option long_options[] = { { "device", no_argument, 0, 'd' }, { "serial", no_argument, 0, 's' }, { "address", no_argument, 0, 'o' }, + { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -78,7 +80,7 @@ int main(int argc, char** argv) { return -1; } - while( (opt = getopt_long(argc, argv, "d:s:o:a:b:v", long_options, &option_index)) != EOF ) { + while( (opt = getopt_long(argc, argv, "d:s:o:a:b:vh?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'd': device_index = atoi(optarg); @@ -103,9 +105,15 @@ int main(int argc, char** argv) { case 'v': verbose = 1; break; + case 'h': + case '?': + usage(); + return EXIT_SUCCESS; default: + fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); usage(); + return EXIT_FAILURE; } if( result != HACKRF_SUCCESS ) { diff --git a/host/hackrf-tools/src/hackrf_rffc5071.c b/host/hackrf-tools/src/hackrf_rffc5071.c index c9630dd4..7282184a 100644 --- a/host/hackrf-tools/src/hackrf_rffc5071.c +++ b/host/hackrf-tools/src/hackrf_rffc5071.c @@ -29,6 +29,7 @@ static void usage() { printf("\nUsage:\n"); + printf("\t-h, --help: this help\n"); printf("\t-n, --register : set register number for subsequent read/write operations\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-w, --write : write register specified by last -n argument with value \n"); @@ -42,6 +43,7 @@ static struct option long_options[] = { { "register", required_argument, 0, 'n' }, { "write", required_argument, 0, 'w' }, { "read", no_argument, 0, 'r' }, + { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -128,16 +130,16 @@ int main(int argc, char** argv) { int result = hackrf_init(); if( result ) { printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; + return EXIT_FAILURE; } result = hackrf_open(&device); if( result ) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; + return EXIT_FAILURE; } - while( (opt = getopt_long(argc, argv, "n:rw:", long_options, &option_index)) != EOF ) { + while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); @@ -157,9 +159,15 @@ int main(int argc, char** argv) { result = dump_register(device, register_number); } break; + case 'h': + case '?': + usage(); + return EXIT_SUCCESS; default: + fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); usage(); + return EXIT_FAILURE; } if( result != HACKRF_SUCCESS ) { @@ -172,10 +180,10 @@ int main(int argc, char** argv) { result = hackrf_close(device); if( result ) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; + return EXIT_FAILURE; } hackrf_exit(); - return 0; + return EXIT_SUCCESS; } diff --git a/host/hackrf-tools/src/hackrf_si5351c.c b/host/hackrf-tools/src/hackrf_si5351c.c index fae59409..69dedc86 100644 --- a/host/hackrf-tools/src/hackrf_si5351c.c +++ b/host/hackrf-tools/src/hackrf_si5351c.c @@ -33,6 +33,7 @@ typedef int bool; static void usage() { printf("\nUsage:\n"); + printf("\t-h, --help: this help\n"); printf("\t-c, --config: print textual configuration information\n"); printf("\t-n, --register : set register number for subsequent read/write operations\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); @@ -52,6 +53,7 @@ static struct option long_options[] = { { "read", no_argument, 0, 'r' }, { "device", no_argument, 0, 'd' }, { "serial", no_argument, 0, 's' }, + { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -209,10 +211,10 @@ int main(int argc, char** argv) { int result = hackrf_init(); if( result ) { printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; + return EXIT_FAILURE; } - while( (opt = getopt_long(argc, argv, "d:s:cn:rw:", long_options, &option_index)) != EOF ) { + while( (opt = getopt_long(argc, argv, "d:s:cn:rw:h?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); @@ -237,9 +239,15 @@ int main(int argc, char** argv) { case 's': serial_number = optarg; break; + case 'h': + case '?': + usage(); + return EXIT_SUCCESS; default: + fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); usage(); + return EXIT_FAILURE; } if( result != HACKRF_SUCCESS ) { @@ -261,7 +269,7 @@ int main(int argc, char** argv) { if( result ) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; + return EXIT_FAILURE; } if(write) { @@ -282,10 +290,10 @@ int main(int argc, char** argv) { result = hackrf_close(device); if( result ) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); - return -1; + return EXIT_FAILURE; } hackrf_exit(); - return 0; + return EXIT_SUCCESS; } diff --git a/host/hackrf-tools/src/hackrf_spiflash.c b/host/hackrf-tools/src/hackrf_spiflash.c index 49cc2bf9..5acb6646 100644 --- a/host/hackrf-tools/src/hackrf_spiflash.c +++ b/host/hackrf-tools/src/hackrf_spiflash.c @@ -52,6 +52,7 @@ static struct option long_options[] = { { "read", required_argument, 0, 'r' }, { "write", required_argument, 0, 'w' }, { "verbose", no_argument, 0, 'v' }, + { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -86,6 +87,7 @@ int parse_u32(char* s, uint32_t* const value) static void usage() { printf("Usage:\n"); + printf("\t-h, --help: this help\n"); printf("\t-a, --address : starting address (default: 0)\n"); printf("\t-l, --length : number of bytes to read (default: %d)\n", MAX_LENGTH); printf("\t-r : Read data into file.\n"); @@ -115,7 +117,7 @@ int main(int argc, char** argv) bool verbose = false; bool reset = false; - while ((opt = getopt_long(argc, argv, "a:l:r:w:d:vR", long_options, + while ((opt = getopt_long(argc, argv, "a:l:r:w:d:vRh?", long_options, &option_index)) != EOF) { switch (opt) { case 'a': @@ -148,9 +150,10 @@ int main(int argc, char** argv) reset = true; break; + case 'h': case '?': usage(); - return EXIT_FAILURE; + return EXIT_SUCCESS; default: fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); diff --git a/host/hackrf-tools/src/hackrf_sweep.c b/host/hackrf-tools/src/hackrf_sweep.c index da700fa2..c907a390 100644 --- a/host/hackrf-tools/src/hackrf_sweep.c +++ b/host/hackrf-tools/src/hackrf_sweep.c @@ -235,6 +235,7 @@ int rx_callback(hackrf_transfer* transfer) { static void usage() { fprintf(stderr, "Usage:\n"); + fprintf(stderr, "\t[-h] # this help\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"); @@ -276,7 +277,7 @@ int main(int argc, char** argv) { 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 ) { + while( (opt = getopt(argc, argv, "a:f:p:l:g:x:d:n:h?")) != EOF ) { result = HACKRF_SUCCESS; switch( opt ) { @@ -325,6 +326,11 @@ int main(int argc, char** argv) { result = parse_u32(optarg, &num_samples); break; + case 'h': + case '?': + usage(); + return EXIT_SUCCESS; + default: fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); usage(); diff --git a/host/hackrf-tools/src/hackrf_transfer.c b/host/hackrf-tools/src/hackrf_transfer.c index a36166c6..b4a3b18b 100644 --- a/host/hackrf-tools/src/hackrf_transfer.c +++ b/host/hackrf-tools/src/hackrf_transfer.c @@ -469,6 +469,7 @@ int tx_callback(hackrf_transfer* transfer) { static void usage() { printf("Usage:\n"); + printf("\t-h # this help\n"); printf("\t[-d serial_number] # Serial number of desired HackRF.\n"); printf("\t-r # Receive data into file (use '-' for stdout).\n"); printf("\t-t # Transmit data from file (use '-' for stdin).\n"); @@ -544,7 +545,7 @@ 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, "Hwr: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:h?")) != EOF ) { result = HACKRF_SUCCESS; switch( opt ) @@ -673,9 +674,10 @@ int main(int argc, char** argv) { result = parse_u32(optarg, &crystal_correct_ppm); break; + case 'h': case '?': usage(); - return EXIT_FAILURE; + return EXIT_SUCCESS; default: fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); From f494e0b890e1c3c54110adc771daeb7213fbd55c Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 29 Jan 2017 18:21:17 -0700 Subject: [PATCH 03/19] Fixed hackrf_transfer mode control logic. --- host/hackrf-tools/src/hackrf_transfer.c | 68 +++++++++++-------------- 1 file changed, 29 insertions(+), 39 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_transfer.c b/host/hackrf-tools/src/hackrf_transfer.c index b4a3b18b..5a812c13 100644 --- a/host/hackrf-tools/src/hackrf_transfer.c +++ b/host/hackrf-tools/src/hackrf_transfer.c @@ -357,6 +357,8 @@ bool repeat = false; bool crystal_correct = false; uint32_t crystal_correct_ppm ; +int requested_mode_count = 0; + int rx_callback(hackrf_transfer* transfer) { size_t bytes_to_write; size_t bytes_written; @@ -555,15 +557,18 @@ int main(int argc, char** argv) { break; case 'w': receive_wav = true; + requested_mode_count++; break; case 'r': receive = true; + requested_mode_count++; path = optarg; break; case 't': transmit = true; + requested_mode_count++; path = optarg; break; @@ -660,8 +665,8 @@ int main(int argc, char** argv) { break; case 'c': - transmit = true; signalsource = true; + requested_mode_count++; result = parse_u32(optarg, &litude); break; @@ -820,33 +825,22 @@ int main(int argc, char** argv) { } } - if( (transmit == false) && (receive == receive_wav) ) - { - fprintf(stderr, "receive -r and receive_wav -w options are mutually exclusive\n"); + if(requested_mode_count > 1) { + fprintf(stderr, "specify only one of: -t, -c, -r, -w\n"); usage(); return EXIT_FAILURE; } - - if( receive_wav == false ) - { - if( transmit == receive ) - { - if( transmit == true ) - { - fprintf(stderr, "receive -r and transmit -t options are mutually exclusive\n"); - } else - { - fprintf(stderr, "specify either transmit -t or receive -r or receive_wav -w option\n"); - } - usage(); - return EXIT_FAILURE; - } + + if(requested_mode_count < 1) { + fprintf(stderr, "specify one of: -t, -c, -r, -w\n"); + usage(); + return EXIT_FAILURE; } - + if( receive ) { transceiver_mode = TRANSCEIVER_MODE_RX; } - + if( transmit ) { transceiver_mode = TRANSCEIVER_MODE_TX; } @@ -854,7 +848,7 @@ int main(int argc, char** argv) { if (signalsource) { transceiver_mode = TRANSCEIVER_MODE_SS; if (amplitude >127) { - fprintf(stderr, "argument error: amplitude shall be in between 0 and 128.\n"); + fprintf(stderr, "argument error: amplitude shall be in between 0 and 127.\n"); usage(); return EXIT_FAILURE; } @@ -1095,7 +1089,7 @@ int main(int argc, char** argv) { } } } - + result = hackrf_is_streaming(device); if (do_exit) { @@ -1103,25 +1097,22 @@ int main(int argc, char** argv) { } 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) - { - if( receive ) - { + + if(device != NULL) { + if(receive || receive_wav) { result = hackrf_stop_rx(device); if( result != HACKRF_SUCCESS ) { fprintf(stderr, "hackrf_stop_rx() failed: %s (%d)\n", hackrf_error_name(result), result); - }else { + } else { fprintf(stderr, "hackrf_stop_rx() done\n"); } } - - if( transmit ) - { + + if(transmit || signalsource) { result = hackrf_stop_tx(device); if( result != HACKRF_SUCCESS ) { fprintf(stderr, "hackrf_stop_tx() failed: %s (%d)\n", hackrf_error_name(result), result); @@ -1129,19 +1120,18 @@ int main(int argc, char** argv) { fprintf(stderr, "hackrf_stop_tx() done\n"); } } - + result = hackrf_close(device); - if( result != HACKRF_SUCCESS ) - { + if(result != HACKRF_SUCCESS) { fprintf(stderr, "hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); - }else { + } else { fprintf(stderr, "hackrf_close() done\n"); } - + hackrf_exit(); fprintf(stderr, "hackrf_exit() done\n"); } - + if(fd != NULL) { if( receive_wav ) From 0b061134b0be5bb3a237ace765563e3710c97bcb Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 29 Jan 2017 18:32:11 -0700 Subject: [PATCH 04/19] Filled in some missing long options for command line tools that use long options. --- host/hackrf-tools/src/hackrf_cpldjtag.c | 3 ++- host/hackrf-tools/src/hackrf_operacake.c | 3 ++- host/hackrf-tools/src/hackrf_si5351c.c | 2 +- host/hackrf-tools/src/hackrf_spiflash.c | 12 +++++++----- 4 files changed, 12 insertions(+), 8 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_cpldjtag.c b/host/hackrf-tools/src/hackrf_cpldjtag.c index 3c9a89e1..fc82deb4 100644 --- a/host/hackrf-tools/src/hackrf_cpldjtag.c +++ b/host/hackrf-tools/src/hackrf_cpldjtag.c @@ -45,6 +45,7 @@ uint8_t data[MAX_XSVF_LENGTH]; static struct option long_options[] = { { "xsvf", required_argument, 0, 'x' }, + { "device", required_argument, 0, 'd' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -82,7 +83,7 @@ static void usage() printf("Usage:\n"); printf("\t-h, --help: this help\n"); printf("\t-x, --xsvf : XSVF file to be written to CPLD.\n"); - printf("\t-d : Serial number of device, if multiple devices\n"); + printf("\t-d, --device : Serial number of device, if multiple devices\n"); } int main(int argc, char** argv) diff --git a/host/hackrf-tools/src/hackrf_operacake.c b/host/hackrf-tools/src/hackrf_operacake.c index 6f8e2e1b..def90690 100644 --- a/host/hackrf-tools/src/hackrf_operacake.c +++ b/host/hackrf-tools/src/hackrf_operacake.c @@ -39,13 +39,14 @@ static void usage() { printf("\t-o, --address : specify a particular operacake by address [default: 0x00]\n"); printf("\t-a : set port A connection\n"); printf("\t-b : set port B connection\n"); - printf("\t-v: verbose, list available operacake boards\n"); + printf("\t-v, --verbose: 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' }, + { "verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; diff --git a/host/hackrf-tools/src/hackrf_si5351c.c b/host/hackrf-tools/src/hackrf_si5351c.c index 69dedc86..536795be 100644 --- a/host/hackrf-tools/src/hackrf_si5351c.c +++ b/host/hackrf-tools/src/hackrf_si5351c.c @@ -38,7 +38,7 @@ static void usage() { printf("\t-n, --register : set register number for subsequent read/write operations\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-w, --write : write register specified by last -n argument with value \n"); - printf("\t-s, --device : specify a particular device by serial number\n"); + printf("\t-s, --serial : specify a particular device by serial number\n"); printf("\t-d, --device : specify a particular device by number\n"); printf("\nExamples:\n"); printf("\t -n 12 -r # reads from register 12\n"); diff --git a/host/hackrf-tools/src/hackrf_spiflash.c b/host/hackrf-tools/src/hackrf_spiflash.c index 5acb6646..d230a6b6 100644 --- a/host/hackrf-tools/src/hackrf_spiflash.c +++ b/host/hackrf-tools/src/hackrf_spiflash.c @@ -51,6 +51,8 @@ static struct option long_options[] = { { "length", required_argument, 0, 'l' }, { "read", required_argument, 0, 'r' }, { "write", required_argument, 0, 'w' }, + { "device", required_argument, 0, 'd' }, + { "reset", no_argument, 0, 'R' }, { "verbose", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, @@ -90,11 +92,11 @@ static void usage() printf("\t-h, --help: this help\n"); printf("\t-a, --address : starting address (default: 0)\n"); printf("\t-l, --length : number of bytes to read (default: %d)\n", MAX_LENGTH); - printf("\t-r : Read data into file.\n"); - printf("\t-w : Write data from file.\n"); - printf("\t-d : Serial number of device, if multiple devices\n"); - printf("\t-R: Reset HackRF after other operations.\n"); - printf("\t-v: Verbose output.\n"); + printf("\t-r, --read : Read data into file.\n"); + printf("\t-w, --write : Write data from file.\n"); + printf("\t-d, --device : Serial number of device, if multiple devices\n"); + printf("\t-R, --reset: Reset HackRF after other operations.\n"); + printf("\t-v, --verbose: Verbose output.\n"); } int main(int argc, char** argv) From b97bd4d19224c389177372fc3a300851c7205bb6 Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 29 Jan 2017 21:09:27 -0700 Subject: [PATCH 05/19] Fixed write bug in hackrf_si5351c. --- host/hackrf-tools/src/hackrf_si5351c.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/host/hackrf-tools/src/hackrf_si5351c.c b/host/hackrf-tools/src/hackrf_si5351c.c index 536795be..00e7ab57 100644 --- a/host/hackrf-tools/src/hackrf_si5351c.c +++ b/host/hackrf-tools/src/hackrf_si5351c.c @@ -222,6 +222,7 @@ int main(int argc, char** argv) { case 'w': write = true; + result = parse_int(optarg, ®ister_value); break; case 'r': @@ -273,7 +274,6 @@ int main(int argc, char** argv) { } if(write) { - result = parse_int(optarg, ®ister_value); if( result == HACKRF_SUCCESS ) { result = write_register(device, register_number, register_value); } From 1eb34ac765837b998779e5a5b61812cec7c4a9a8 Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 29 Jan 2017 21:27:44 -0700 Subject: [PATCH 06/19] Fixed bug that prevented hackrf_max2837 and hackrf_rffc5071 from displaying help when HackRF is not found. --- host/hackrf-tools/src/hackrf_max2837.c | 69 +++++++++++++++++-------- host/hackrf-tools/src/hackrf_rffc5071.c | 69 ++++++++++++++++--------- 2 files changed, 94 insertions(+), 44 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_max2837.c b/host/hackrf-tools/src/hackrf_max2837.c index 38ac01be..6b118e38 100644 --- a/host/hackrf-tools/src/hackrf_max2837.c +++ b/host/hackrf-tools/src/hackrf_max2837.c @@ -26,6 +26,12 @@ #include #include +#ifndef bool +typedef int bool; +#define true 1 +#define false 0 +#endif + static void usage() { printf("\nUsage:\n"); printf("\t-h, --help: this help\n"); @@ -96,7 +102,7 @@ int dump_registers(hackrf_device* device) { break; } } - + return result; } @@ -125,18 +131,14 @@ int main(int argc, char** argv) { uint16_t register_value; hackrf_device* device = NULL; int option_index = 0; + bool read = false; + bool write = false; int result = hackrf_init(); if( result ) { printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } - - result = hackrf_open(&device); - if( result ) { - printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - return EXIT_FAILURE; - } while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) { switch( opt ) { @@ -145,20 +147,14 @@ int main(int argc, char** argv) { break; case 'w': + write = true; result = parse_int(optarg, ®ister_value); - if( result == HACKRF_SUCCESS ) { - result = write_register(device, register_number, register_value); - } break; - + case 'r': - if( register_number == REGISTER_INVALID ) { - result = dump_registers(device); - } else { - result = dump_register(device, register_number); - } + read = true; break; - + case 'h': case '?': usage(); @@ -168,19 +164,50 @@ int main(int argc, char** argv) { usage(); return EXIT_FAILURE; } - + if( result != HACKRF_SUCCESS ) { printf("argument error: %s (%d)\n", hackrf_error_name(result), result); - break; + usage(); + return EXIT_FAILURE; } } - + + if(write && read) { + fprintf(stderr, "Read and write options are mutually exclusive.\n"); + usage(); + return EXIT_FAILURE; + } + + if(!(write || read)) { + fprintf(stderr, "Specify either read or write option.\n"); + usage(); + return EXIT_FAILURE; + } + + result = hackrf_open(&device); + if(result) { + printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); + return EXIT_FAILURE; + } + + if(write) { + result = write_register(device, register_number, register_value); + } + + if(read) { + if(register_number == REGISTER_INVALID) { + result = dump_registers(device); + } else { + result = dump_register(device, register_number); + } + } + result = hackrf_close(device); if( result ) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } - + hackrf_exit(); return EXIT_SUCCESS; diff --git a/host/hackrf-tools/src/hackrf_rffc5071.c b/host/hackrf-tools/src/hackrf_rffc5071.c index 7282184a..82b3f552 100644 --- a/host/hackrf-tools/src/hackrf_rffc5071.c +++ b/host/hackrf-tools/src/hackrf_rffc5071.c @@ -27,6 +27,12 @@ #include #include +#ifndef bool +typedef int bool; +#define true 1 +#define false 0 +#endif + static void usage() { printf("\nUsage:\n"); printf("\t-h, --help: this help\n"); @@ -126,64 +132,81 @@ int main(int argc, char** argv) { uint16_t register_value; hackrf_device* device = NULL; int option_index = 0; + bool read = false; + bool write = false; int result = hackrf_init(); if( result ) { printf("hackrf_init() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } - - result = hackrf_open(&device); - if( result ) { - printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); - return EXIT_FAILURE; - } while( (opt = getopt_long(argc, argv, "n:rw:h?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); break; - case 'w': + write = true; result = parse_int(optarg, ®ister_value); - if( result == HACKRF_SUCCESS ) { - result = write_register(device, register_number, register_value); - } break; - case 'r': - if( register_number == REGISTER_INVALID ) { - result = dump_registers(device); - } else { - result = dump_register(device, register_number); - } + read = true; break; case 'h': case '?': usage(); return EXIT_SUCCESS; - default: fprintf(stderr, "unknown argument '-%c %s'\n", opt, optarg); usage(); return EXIT_FAILURE; } - + if( result != HACKRF_SUCCESS ) { printf("argument error: %s (%d)\n", hackrf_error_name(result), result); usage(); - break; + return EXIT_FAILURE; } } - + + if(write && read) { + fprintf(stderr, "Read and write options are mutually exclusive.\n"); + usage(); + return EXIT_FAILURE; + } + + if(!(write || read)) { + fprintf(stderr, "Specify either read or write option.\n"); + usage(); + return EXIT_FAILURE; + } + + result = hackrf_open(&device); + if(result) { + printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); + return EXIT_FAILURE; + } + + if(write) { + result = write_register(device, register_number, register_value); + } + + if(read) { + if(register_number == REGISTER_INVALID) { + result = dump_registers(device); + } else { + result = dump_register(device, register_number); + } + } + result = hackrf_close(device); if( result ) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } - + hackrf_exit(); - - return EXIT_SUCCESS; + + return EXIT_SUCCESS; } From 9fcbb6fa55f45e2776ab1808afc1d45d6f461a28 Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Sun, 29 Jan 2017 21:38:06 -0700 Subject: [PATCH 07/19] Clarified hackrf_si5351c control logic --- host/hackrf-tools/src/hackrf_si5351c.c | 36 +++++++++++++++++++++----- 1 file changed, 29 insertions(+), 7 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_si5351c.c b/host/hackrf-tools/src/hackrf_si5351c.c index 00e7ab57..eea39c83 100644 --- a/host/hackrf-tools/src/hackrf_si5351c.c +++ b/host/hackrf-tools/src/hackrf_si5351c.c @@ -257,6 +257,24 @@ int main(int argc, char** argv) { } } + if(write && read) { + fprintf(stderr, "Read and write options are mutually exclusive.\n"); + usage(); + return EXIT_FAILURE; + } + + if(write && dump_config) { + fprintf(stderr, "Config and write options are mutually exclusive.\n"); + usage(); + return EXIT_FAILURE; + } + + if(!(write || read || dump_config)) { + fprintf(stderr, "Specify read, write, or config option.\n"); + usage(); + return EXIT_FAILURE; + } + if(serial_number != NULL) { result = hackrf_open_by_serial(serial_number, &device); } else { @@ -268,7 +286,7 @@ int main(int argc, char** argv) { } } - if( result ) { + if(result) { printf("hackrf_open() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } @@ -277,23 +295,27 @@ int main(int argc, char** argv) { if( result == HACKRF_SUCCESS ) { result = write_register(device, register_number, register_value); } - } else if(read) { + } + + if(read) { if( register_number == REGISTER_INVALID ) { result = dump_registers(device); } else { result = dump_register(device, register_number); } - } else if(dump_config) { + } + + if(dump_config) { dump_configuration(device); } - + result = hackrf_close(device); - if( result ) { + if(result) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); return EXIT_FAILURE; } - + hackrf_exit(); - + return EXIT_SUCCESS; } From 4ca37b8174ae8992f81695c92e4b93a8ff5f09cc Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Mon, 30 Jan 2017 09:31:27 -0700 Subject: [PATCH 08/19] Removed "subsequent" from help for register number options which can now be placed before or after the read/write options. We no longer support chaining operations over multiple registers in a single command. --- host/hackrf-tools/src/hackrf_max2837.c | 2 +- host/hackrf-tools/src/hackrf_rffc5071.c | 2 +- host/hackrf-tools/src/hackrf_si5351c.c | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_max2837.c b/host/hackrf-tools/src/hackrf_max2837.c index 6b118e38..d09a7557 100644 --- a/host/hackrf-tools/src/hackrf_max2837.c +++ b/host/hackrf-tools/src/hackrf_max2837.c @@ -35,7 +35,7 @@ typedef int bool; static void usage() { printf("\nUsage:\n"); printf("\t-h, --help: this help\n"); - printf("\t-n, --register : set register number for subsequent read/write operations\n"); + printf("\t-n, --register : set register number for read/write operations\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-w, --write : write register specified by last -n argument with value \n"); printf("\nExamples:\n"); diff --git a/host/hackrf-tools/src/hackrf_rffc5071.c b/host/hackrf-tools/src/hackrf_rffc5071.c index 82b3f552..11285d83 100644 --- a/host/hackrf-tools/src/hackrf_rffc5071.c +++ b/host/hackrf-tools/src/hackrf_rffc5071.c @@ -36,7 +36,7 @@ typedef int bool; static void usage() { printf("\nUsage:\n"); printf("\t-h, --help: this help\n"); - printf("\t-n, --register : set register number for subsequent read/write operations\n"); + printf("\t-n, --register : set register number for read/write operations\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-w, --write : write register specified by last -n argument with value \n"); printf("\nExamples:\n"); diff --git a/host/hackrf-tools/src/hackrf_si5351c.c b/host/hackrf-tools/src/hackrf_si5351c.c index eea39c83..c4fc0cdb 100644 --- a/host/hackrf-tools/src/hackrf_si5351c.c +++ b/host/hackrf-tools/src/hackrf_si5351c.c @@ -35,7 +35,7 @@ static void usage() { printf("\nUsage:\n"); printf("\t-h, --help: this help\n"); printf("\t-c, --config: print textual configuration information\n"); - printf("\t-n, --register : set register number for subsequent read/write operations\n"); + printf("\t-n, --register : set register number for read/write operations\n"); printf("\t-r, --read: read register specified by last -n argument, or all registers\n"); printf("\t-w, --write : write register specified by last -n argument with value \n"); printf("\t-s, --serial : specify a particular device by serial number\n"); From 255b7b4705046891aca4d983a50664ffc21ce19c Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Mon, 30 Jan 2017 18:30:17 -0700 Subject: [PATCH 09/19] Add USB API versioning --- firmware/hackrf_usb/usb_descriptor.c | 2 +- host/hackrf-tools/src/hackrf_info.c | 31 +++++++---- host/hackrf-tools/src/hackrf_operacake.c | 54 ++++++++++++------ host/hackrf-tools/src/hackrf_spiflash.c | 25 +++++---- host/hackrf-tools/src/hackrf_sweep.c | 3 +- host/hackrf-tools/src/hackrf_transfer.c | 4 +- host/libhackrf/src/hackrf.c | 71 +++++++++++++++++------- host/libhackrf/src/hackrf.h | 11 +++- 8 files changed, 133 insertions(+), 68 deletions(-) diff --git a/firmware/hackrf_usb/usb_descriptor.c b/firmware/hackrf_usb/usb_descriptor.c index 5a9eeb45..6f6560b1 100644 --- a/firmware/hackrf_usb/usb_descriptor.c +++ b/firmware/hackrf_usb/usb_descriptor.c @@ -55,7 +55,7 @@ uint8_t usb_descriptor_device[] = { USB_MAX_PACKET0, // bMaxPacketSize0 USB_WORD(USB_VENDOR_ID), // idVendor USB_WORD(USB_PRODUCT_ID), // idProduct - USB_WORD(0x0101), // bcdDevice + USB_WORD(0x0102), // bcdDevice 0x01, // iManufacturer 0x02, // iProduct 0x04, // iSerialNumber diff --git a/host/hackrf-tools/src/hackrf_info.c b/host/hackrf-tools/src/hackrf_info.c index def5371d..bd487eb7 100644 --- a/host/hackrf-tools/src/hackrf_info.c +++ b/host/hackrf-tools/src/hackrf_info.c @@ -31,6 +31,7 @@ int main(void) int result = HACKRF_SUCCESS; uint8_t board_id = BOARD_ID_INVALID; char version[255 + 1]; + uint16_t usb_version; read_partid_serialno_t read_partid_serialno; uint8_t operacakes[8]; hackrf_device_list_t *list; @@ -55,10 +56,11 @@ int main(void) if (i > 0) printf("\n"); - printf("Found HackRF board %d:\n", i); + printf("Found HackRF\n"); + printf("Index: %d\n", i); if (list->serial_numbers[i]) - printf("USB descriptor string: %s\n", list->serial_numbers[i]); + printf("Serial number: %s\n", list->serial_numbers[i]); device = NULL; result = hackrf_device_list_open(list, i, &device); @@ -83,7 +85,15 @@ int main(void) hackrf_error_name(result), result); return EXIT_FAILURE; } - printf("Firmware Version: %s\n", version); + + result = hackrf_usb_api_version_read(device, &usb_version); + if (result != HACKRF_SUCCESS) { + fprintf(stderr, "hackrf_usb_api_version_read() failed: %s (%d)\n", + hackrf_error_name(result), result); + return EXIT_FAILURE; + } + printf("Firmware Version: %s (API:%x.%02x)\n", version, + (usb_version>>8)&0xFF, usb_version&0xFF); result = hackrf_board_partid_serialno_read(device, &read_partid_serialno); if (result != HACKRF_SUCCESS) { @@ -101,16 +111,17 @@ int main(void) read_partid_serialno.serial_no[3]); result = hackrf_get_operacake_boards(device, &operacakes[0]); - if (result != HACKRF_SUCCESS) { + if ((result != HACKRF_SUCCESS) && (result != HACKRF_ERROR_USB_API_VERSION)) { fprintf(stderr, "hackrf_get_operacake_boards() failed: %s (%d)\n", hackrf_error_name(result), result); - return EXIT_FAILURE; + return EXIT_FAILURE; } - for(j=0; j<8; j++) { - if(operacakes[j] == 0) - break; - printf("Operacake found, address: 0x%02x\n", operacakes[j]); - + if(result == HACKRF_SUCCESS) { + for(j=0; j<8; j++) { + if(operacakes[j] == 0) + break; + printf("Operacake found, address: 0x%02x\n", operacakes[j]); + } } result = hackrf_close(device); diff --git a/host/hackrf-tools/src/hackrf_operacake.c b/host/hackrf-tools/src/hackrf_operacake.c index def90690..91e1e0d2 100644 --- a/host/hackrf-tools/src/hackrf_operacake.c +++ b/host/hackrf-tools/src/hackrf_operacake.c @@ -39,14 +39,14 @@ static void usage() { printf("\t-o, --address : specify a particular operacake by address [default: 0x00]\n"); printf("\t-a : set port A connection\n"); printf("\t-b : set port B connection\n"); - printf("\t-v, --verbose: verbose, list available operacake boards\n"); + printf("\t-l, --list: 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' }, - { "verbose", no_argument, 0, 'v' }, + { "list", no_argument, 0, 'v' }, { "help", no_argument, 0, 'h' }, { 0, 0, 0, 0 }, }; @@ -69,8 +69,10 @@ int main(int argc, char** argv) { int operacake_address = 0; int port_a = 0; int port_b = 0; - int verbose = 0; + bool set_ports = false; + bool list = false; uint8_t operacakes[8]; + uint8_t operacake_count = 0; int i = 0; hackrf_device* device = NULL; int option_index = 0; @@ -81,7 +83,7 @@ int main(int argc, char** argv) { return -1; } - while( (opt = getopt_long(argc, argv, "d:s:o:a:b:vh?", long_options, &option_index)) != EOF ) { + while( (opt = getopt_long(argc, argv, "d:s:o:a:b:lh?", long_options, &option_index)) != EOF ) { switch( opt ) { case 'd': device_index = atoi(optarg); @@ -93,6 +95,7 @@ int main(int argc, char** argv) { case 'o': operacake_address = atoi(optarg); + set_ports = true; break; case 'a': @@ -103,8 +106,8 @@ int main(int argc, char** argv) { port_b = atoi(optarg); break; - case 'v': - verbose = 1; + case 'l': + list = true; break; case 'h': case '?': @@ -123,6 +126,12 @@ int main(int argc, char** argv) { } } + if(!(list || set_ports)) { + fprintf(stderr, "Specify either list or address option.\n"); + usage(); + return EXIT_FAILURE; + } + if(serial_number != NULL) { result = hackrf_open_by_serial(serial_number, &device); } else { @@ -139,20 +148,31 @@ int main(int argc, char** argv) { 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]); + if(list) { + result = hackrf_get_operacake_boards(device, operacakes); + if (result != HACKRF_SUCCESS) { + fprintf(stderr, "hackrf_get_operacake_boards() failed: %s (%d)\n", + hackrf_error_name(result), result); + return EXIT_FAILURE; } + printf("Operacakes found: "); + for(i=0; i<8; i++) { + if(operacakes[i] !=0) { + printf("\n%d", operacakes[i]); + operacake_count++; + } + } + if(!operacake_count) + printf("None"); 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; + if(set_ports) { + 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); @@ -160,8 +180,6 @@ int main(int argc, char** argv) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); return -1; } - hackrf_exit(); - return 0; } diff --git a/host/hackrf-tools/src/hackrf_spiflash.c b/host/hackrf-tools/src/hackrf_spiflash.c index d230a6b6..fabdb382 100644 --- a/host/hackrf-tools/src/hackrf_spiflash.c +++ b/host/hackrf-tools/src/hackrf_spiflash.c @@ -118,6 +118,7 @@ int main(int argc, char** argv) bool write = false; bool verbose = false; bool reset = false; + uint16_t usb_api; while ((opt = getopt_long(argc, argv, "a:l:r:w:d:vRh?", long_options, &option_index)) != EOF) { @@ -309,13 +310,22 @@ int main(int argc, char** argv) } } + if (fd != NULL) { + fclose(fd); + fd = NULL; + } + if(reset) { result = hackrf_reset(device); if (result != HACKRF_SUCCESS) { - fprintf(stderr, "hackrf_reset() failed: %s (%d)\n", - hackrf_error_name(result), result); - fclose(fd); - fd = NULL; + if (result == HACKRF_ERROR_USB_API_VERSION) { + hackrf_usb_api_version_read(device, &usb_api); + fprintf(stderr, "Reset is not supported by firmware API %x.%02x\n", + (usb_api>>8)&0xFF, usb_api&0xFF); + } else { + fprintf(stderr, "hackrf_reset() failed: %s (%d)\n", + hackrf_error_name(result), result); + } return EXIT_FAILURE; } } @@ -324,16 +334,9 @@ int main(int argc, char** argv) if (result != HACKRF_SUCCESS) { fprintf(stderr, "hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); - fclose(fd); - fd = NULL; return EXIT_FAILURE; } hackrf_exit(); - - if (fd != NULL) { - fclose(fd); - } - return EXIT_SUCCESS; } diff --git a/host/hackrf-tools/src/hackrf_sweep.c b/host/hackrf-tools/src/hackrf_sweep.c index c907a390..6c044203 100644 --- a/host/hackrf-tools/src/hackrf_sweep.c +++ b/host/hackrf-tools/src/hackrf_sweep.c @@ -448,7 +448,7 @@ int main(int argc, char** argv) { 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); + fprintf(stderr, "hackrf_start_rx() failed: %s (%d)\n", hackrf_error_name(result), result); usage(); return EXIT_FAILURE; } @@ -457,7 +457,6 @@ int main(int argc, char** argv) { if( result != HACKRF_SUCCESS ) { fprintf(stderr, "hackrf_init_sweep() failed: %s (%d)\n", hackrf_error_name(result), result); - usage(); return EXIT_FAILURE; } diff --git a/host/hackrf-tools/src/hackrf_transfer.c b/host/hackrf-tools/src/hackrf_transfer.c index 5a812c13..ab211993 100644 --- a/host/hackrf-tools/src/hackrf_transfer.c +++ b/host/hackrf-tools/src/hackrf_transfer.c @@ -961,12 +961,10 @@ int main(int argc, char** argv) { } } - fprintf(stderr, "call hackrf_set_hw_sync_mode(%d)\n", - hw_sync); + 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; } diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 30c604f2..5d0025ab 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -1012,6 +1012,27 @@ int ADDCALL hackrf_version_string_read(hackrf_device* device, char* version, } } +extern ADDAPI int ADDCALL hackrf_usb_api_version_read(hackrf_device* device, + uint16_t* version) +{ + int result; + libusb_device* dev; + struct libusb_device_descriptor desc; + dev = libusb_get_device(device->usb_device); + result = libusb_get_device_descriptor(dev, &desc); + if (result < 0) + return HACKRF_ERROR_LIBUSB; + + *version = desc.bcdDevice; + return HACKRF_SUCCESS; +} + +#define USB_API_REQUIRED(device, version) \ +uint16_t usb_version = 0; \ +hackrf_usb_api_version_read(device, &usb_version); \ +if(usb_version < version) \ + return HACKRF_ERROR_USB_API_VERSION; + typedef struct { uint32_t freq_mhz; /* From 0 to 6000+MHz */ uint32_t freq_hz; /* From 0 to 999999Hz */ @@ -1577,26 +1598,6 @@ int ADDCALL hackrf_close(hackrf_device* device) return result1; } -int ADDCALL hackrf_set_hw_sync_mode(hackrf_device* device, const uint8_t value) { - int result = libusb_control_transfer( - device->usb_device, - LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, - HACKRF_VENDOR_REQUEST_SET_HW_SYNC_MODE, - value, - 0, - NULL, - 0, - 0 - ); - - if( result != 0 ) - { - return HACKRF_ERROR_LIBUSB; - } else { - return HACKRF_SUCCESS; - } -} - const char* ADDCALL hackrf_error_name(enum hackrf_error errcode) { switch(errcode) @@ -1634,6 +1635,9 @@ const char* ADDCALL hackrf_error_name(enum hackrf_error errcode) case HACKRF_ERROR_STREAMING_EXIT_CALLED: return "HACKRF_ERROR_STREAMING_EXIT_CALLED"; + case HACKRF_ERROR_USB_API_VERSION: + return "feature not supported by installed firmware"; + case HACKRF_ERROR_OTHER: return "HACKRF_ERROR_OTHER"; @@ -1744,9 +1748,33 @@ uint32_t ADDCALL hackrf_compute_baseband_filter_bw(const uint32_t bandwidth_hz) return p->bandwidth_hz; } +/* All features below require USB API version 0x1002 or higher) */ + +int ADDCALL hackrf_set_hw_sync_mode(hackrf_device* device, const uint8_t value) { + USB_API_REQUIRED(device, 0x0102) + int result = libusb_control_transfer( + device->usb_device, + LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_SET_HW_SYNC_MODE, + value, + 0, + NULL, + 0, + 0 + ); + + if( result != 0 ) + { + return HACKRF_ERROR_LIBUSB; + } else { + return HACKRF_SUCCESS; + } +} + /* Initialise sweep mode with alist of frequencies and dwell time in samples */ int ADDCALL hackrf_init_sweep(hackrf_device* device, uint16_t* frequency_list, int length, uint32_t dwell_time) { + USB_API_REQUIRED(device, 0x0102) int result, i; int size = length * sizeof(frequency_list[0]); @@ -1776,6 +1804,7 @@ int ADDCALL hackrf_init_sweep(hackrf_device* device, uint16_t* frequency_list, i */ int ADDCALL hackrf_get_operacake_boards(hackrf_device* device, uint8_t* boards) { + USB_API_REQUIRED(device, 0x0102) int result; result = libusb_control_transfer( device->usb_device, @@ -1802,6 +1831,7 @@ int ADDCALL hackrf_set_operacake_ports(hackrf_device* device, uint8_t port_a, uint8_t port_b) { + USB_API_REQUIRED(device, 0x0102) int result; /* Error checking */ if((port_a > OPERACAKE_PB4) || (port_b > OPERACAKE_PB4)) { @@ -1831,6 +1861,7 @@ int ADDCALL hackrf_set_operacake_ports(hackrf_device* device, } int ADDCALL hackrf_reset(hackrf_device* device) { + USB_API_REQUIRED(device, 0x0102) int result = libusb_control_transfer( device->usb_device, LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 06826aad..d1a57c4d 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -59,6 +59,7 @@ enum hackrf_error { HACKRF_ERROR_STREAMING_THREAD_ERR = -1002, HACKRF_ERROR_STREAMING_STOPPED = -1003, HACKRF_ERROR_STREAMING_EXIT_CALLED = -1004, + HACKRF_ERROR_USB_API_VERSION = -1005, HACKRF_ERROR_OTHER = -9999, }; @@ -170,6 +171,7 @@ extern ADDAPI int ADDCALL hackrf_cpld_write(hackrf_device* device, extern ADDAPI int ADDCALL hackrf_board_id_read(hackrf_device* device, uint8_t* value); extern ADDAPI int ADDCALL hackrf_version_string_read(hackrf_device* device, char* version, uint8_t length); +extern ADDAPI int ADDCALL hackrf_usb_api_version_read(hackrf_device* device, uint16_t* version); extern ADDAPI int ADDCALL hackrf_set_freq(hackrf_device* device, const uint64_t freq_hz); extern ADDAPI int ADDCALL hackrf_set_freq_explicit(hackrf_device* device, @@ -198,9 +200,6 @@ extern ADDAPI int ADDCALL hackrf_set_txvga_gain(hackrf_device* device, uint32_t /* antenna port power control */ extern ADDAPI int ADDCALL hackrf_set_antenna_enable(hackrf_device* device, const uint8_t value); -/* set hardware sync mode */ -extern ADDAPI int ADDCALL hackrf_set_hw_sync_mode(hackrf_device* device, const uint8_t value); - extern ADDAPI const char* ADDCALL hackrf_error_name(enum hackrf_error errcode); extern ADDAPI const char* ADDCALL hackrf_board_id_name(enum hackrf_board_id board_id); extern ADDAPI const char* ADDCALL hackrf_usb_board_id_name(enum hackrf_usb_board_id usb_board_id); @@ -210,6 +209,12 @@ extern ADDAPI const char* ADDCALL hackrf_filter_path_name(const enum rf_path_fil extern ADDAPI uint32_t ADDCALL hackrf_compute_baseband_filter_bw_round_down_lt(const uint32_t bandwidth_hz); /* Compute best default value depending on sample rate (auto filter) */ extern ADDAPI uint32_t ADDCALL hackrf_compute_baseband_filter_bw(const uint32_t bandwidth_hz); + +/* All features below require USB API version 0x1002 or higher) */ + +/* set hardware sync mode */ +extern ADDAPI int ADDCALL hackrf_set_hw_sync_mode(hackrf_device* device, const uint8_t value); + /* Start scan mode */ extern ADDAPI int ADDCALL hackrf_init_sweep(hackrf_device* device, uint16_t* frequency_list, From ec432b9d288cd26116746646a9f1611c99b09d82 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Mon, 30 Jan 2017 18:33:44 -0700 Subject: [PATCH 10/19] Remove duplicate serial number from hackrf_info --- host/hackrf-tools/src/hackrf_info.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_info.c b/host/hackrf-tools/src/hackrf_info.c index bd487eb7..0049a3e7 100644 --- a/host/hackrf-tools/src/hackrf_info.c +++ b/host/hackrf-tools/src/hackrf_info.c @@ -104,11 +104,6 @@ int main(void) printf("Part ID Number: 0x%08x 0x%08x\n", read_partid_serialno.part_id[0], read_partid_serialno.part_id[1]); - printf("Serial Number: 0x%08x 0x%08x 0x%08x 0x%08x\n", - read_partid_serialno.serial_no[0], - 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) && (result != HACKRF_ERROR_USB_API_VERSION)) { From 78182194e0612109caabc2ca1b08a89e877d182b Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Tue, 31 Jan 2017 15:54:48 -0700 Subject: [PATCH 11/19] Turn off VBUS discharge to reduce current draw --- firmware/common/usb.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/firmware/common/usb.c b/firmware/common/usb.c index 3444e1c2..f77bc28b 100644 --- a/firmware/common/usb.c +++ b/firmware/common/usb.c @@ -320,8 +320,7 @@ static void usb_controller_set_device_mode() { // Set device-related OTG flags // OTG termination: controls pull-down on USB_DM - // VBUS_Discharge: VBUS discharges through resistor - USB0_OTGSC = USB0_OTGSC_OT | USB0_OTGSC_VD; + USB0_OTGSC = USB0_OTGSC_OT; } usb_speed_t usb_speed( From 215b82da39991db1ee1e0a862161965fadc47628 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Tue, 31 Jan 2017 16:44:59 -0700 Subject: [PATCH 12/19] Add issue template to add structure to issue reporting --- issue_template.md | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 issue_template.md diff --git a/issue_template.md b/issue_template.md new file mode 100644 index 00000000..06f0c9ee --- /dev/null +++ b/issue_template.md @@ -0,0 +1,23 @@ +### Steps to reproduce +1. +2. +3. + +### Expected behaviour +Tell us what you expect should happen + +### Actual behaviour +Tell us what happens instead + +### Version information +**Operating system**: + +**hackrf_info output:** + +If you are reporting a problem that involves third party software +(GNU Radio, Gqrx, etc), please report the version here. + +### Output +``` +Insert any commandline or build output here +``` From 47fd25a707886076b02876c501b3c000fce571a9 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Tue, 31 Jan 2017 16:46:36 -0700 Subject: [PATCH 13/19] Exclude vscode project files --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0c016b75..b2507075 100644 --- a/.gitignore +++ b/.gitignore @@ -18,6 +18,7 @@ Thumbs.db *.swp *.sublime-project *.sublime-workspace +.vscode* # Xilinx tools create an enormous amount of poop: firmware/cpld/**/isim/ From 3c2a32981b58c6b4a8f89544375a9dd80cfae498 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Tue, 31 Jan 2017 17:35:56 -0700 Subject: [PATCH 14/19] Replace error messages with more readable strings --- host/libhackrf/src/hackrf.c | 22 +++++++++++----------- 1 file changed, 11 insertions(+), 11 deletions(-) diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 5d0025ab..e28f7012 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -1609,40 +1609,40 @@ const char* ADDCALL hackrf_error_name(enum hackrf_error errcode) return "HACKRF_TRUE"; case HACKRF_ERROR_INVALID_PARAM: - return "HACKRF_ERROR_INVALID_PARAM"; + return "invalid parameter(s)"; case HACKRF_ERROR_NOT_FOUND: - return "HACKRF_ERROR_NOT_FOUND"; + return "HackRF not found"; case HACKRF_ERROR_BUSY: - return "HACKRF_ERROR_BUSY"; + return "HackRF busy"; case HACKRF_ERROR_NO_MEM: - return "HACKRF_ERROR_NO_MEM"; + return "insufficient memory"; case HACKRF_ERROR_LIBUSB: - return "HACKRF_ERROR_LIBUSB"; + return "USB error"; case HACKRF_ERROR_THREAD: - return "HACKRF_ERROR_THREAD"; + return "transfer thread error"; case HACKRF_ERROR_STREAMING_THREAD_ERR: - return "HACKRF_ERROR_STREAMING_THREAD_ERR"; + return "streaming thread encountered an error"; case HACKRF_ERROR_STREAMING_STOPPED: - return "HACKRF_ERROR_STREAMING_STOPPED"; + return "streaming stopped"; case HACKRF_ERROR_STREAMING_EXIT_CALLED: - return "HACKRF_ERROR_STREAMING_EXIT_CALLED"; + return "streaming terminated"; case HACKRF_ERROR_USB_API_VERSION: return "feature not supported by installed firmware"; case HACKRF_ERROR_OTHER: - return "HACKRF_ERROR_OTHER"; + return "unspecified error"; default: - return "HACKRF unknown error"; + return "unknown error code"; } } From 6912df23ccb3d2db13387c30dfc9c88b93c317ea Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Wed, 1 Feb 2017 14:23:47 -0700 Subject: [PATCH 15/19] Shhh, silence in the library --- host/libhackrf/src/hackrf.c | 13 +------------ 1 file changed, 1 insertion(+), 12 deletions(-) diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index e28f7012..7e3de85c 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -24,7 +24,7 @@ ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSI #include "hackrf.h" #include - +#include #include #ifdef _WIN32 @@ -355,9 +355,6 @@ int ADDCALL hackrf_exit(void) return HACKRF_SUCCESS; } -#include -#include - hackrf_device_list_t* ADDCALL hackrf_device_list() { ssize_t i; @@ -442,8 +439,6 @@ libusb_device_handle* hackrf_open_usb(const char* const desired_serial_number) char serial_number[64]; int serial_number_length; - printf("Number of USB devices: %ld\n", list_length); - if( desired_serial_number ) { /* If a shorter serial number is specified, only match against the suffix. * Should probably complain if the match is not unique, currently doesn't. @@ -461,7 +456,6 @@ libusb_device_handle* hackrf_open_usb(const char* const desired_serial_number) if((device_descriptor.idProduct == hackrf_one_usb_pid) || (device_descriptor.idProduct == hackrf_jawbreaker_usb_pid) || (device_descriptor.idProduct == rad1o_usb_pid)) { - printf("USB device %4x:%4x:", device_descriptor.idVendor, device_descriptor.idProduct); if( desired_serial_number != NULL ) { const uint_fast8_t serial_descriptor_index = device_descriptor.iSerialNumber; @@ -473,23 +467,18 @@ libusb_device_handle* hackrf_open_usb(const char* const desired_serial_number) serial_number_length = libusb_get_string_descriptor_ascii(usb_device, serial_descriptor_index, (unsigned char*)serial_number, sizeof(serial_number)); if( serial_number_length == 32 ) { serial_number[32] = 0; - printf(" %s", serial_number); if( strncmp(serial_number + 32-match_len, desired_serial_number, match_len) == 0 ) { - printf(" match\n"); break; } else { - printf(" skip\n"); libusb_close(usb_device); usb_device = NULL; } } else { - printf(" wrong length of serial number: %d\n", serial_number_length); libusb_close(usb_device); usb_device = NULL; } } } else { - printf(" default\n"); libusb_open(devices[i], &usb_device); break; } From a2857f7383b1e57812534ab4e60b1e75faa0d9e9 Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Wed, 1 Feb 2017 16:01:28 -0700 Subject: [PATCH 16/19] iReplace malloc that we never free with a statically assigned buffer --- host/libhackrf/src/hackrf.c | 26 +++++++++++--------------- 1 file changed, 11 insertions(+), 15 deletions(-) diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 7e3de85c..ba3e5e0c 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -98,17 +98,19 @@ typedef enum { HACKRF_HW_SYNC_MODE_ON = 1, } hackrf_hw_sync_mode; +#define TRANSFER_COUNT 4 +#define TRANSFER_BUFFER_SIZE 262144 + struct hackrf_device { libusb_device_handle* usb_device; struct libusb_transfer** transfers; hackrf_sample_block_cb_fn callback; volatile bool transfer_thread_started; /* volatile shared between threads (read only) */ pthread_t transfer_thread; - uint32_t transfer_count; - uint32_t buffer_size; volatile bool streaming; /* volatile shared between threads (read only) */ void* rx_ctx; void* tx_ctx; + unsigned char buffer[TRANSFER_COUNT * TRANSFER_BUFFER_SIZE]; }; typedef struct { @@ -155,7 +157,7 @@ static int cancel_transfers(hackrf_device* device) if( device->transfers != NULL ) { - for(transfer_index=0; transfer_indextransfer_count; transfer_index++) + for(transfer_index=0; transfer_indextransfers[transfer_index] != NULL ) { @@ -175,7 +177,7 @@ static int free_transfers(hackrf_device* device) if( device->transfers != NULL ) { // libusb_close() should free all transfers referenced from this array. - for(transfer_index=0; transfer_indextransfer_count; transfer_index++) + for(transfer_index=0; transfer_indextransfers[transfer_index] != NULL ) { @@ -194,13 +196,13 @@ static int allocate_transfers(hackrf_device* const device) if( device->transfers == NULL ) { uint32_t transfer_index; - device->transfers = (struct libusb_transfer**) calloc(device->transfer_count, sizeof(struct libusb_transfer)); + device->transfers = (struct libusb_transfer**) calloc(TRANSFER_COUNT, sizeof(struct libusb_transfer)); if( device->transfers == NULL ) { return HACKRF_ERROR_NO_MEM; } - for(transfer_index=0; transfer_indextransfer_count; transfer_index++) + for(transfer_index=0; transfer_indextransfers[transfer_index] = libusb_alloc_transfer(0); if( device->transfers[transfer_index] == NULL ) @@ -212,8 +214,8 @@ static int allocate_transfers(hackrf_device* const device) device->transfers[transfer_index], device->usb_device, 0, - (unsigned char*)malloc(device->buffer_size), - device->buffer_size, + &device->buffer[transfer_index * TRANSFER_BUFFER_SIZE], + TRANSFER_BUFFER_SIZE, NULL, device, 0 @@ -239,7 +241,7 @@ static int prepare_transfers( uint32_t transfer_index; if( device->transfers != NULL ) { - for(transfer_index=0; transfer_indextransfer_count; transfer_index++) + for(transfer_index=0; transfer_indextransfers[transfer_index]->endpoint = endpoint_address; device->transfers[transfer_index]->callback = callback; @@ -526,12 +528,6 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d lib_device->transfers = NULL; lib_device->callback = NULL; lib_device->transfer_thread_started = false; - /* - lib_device->transfer_count = 1024; - lib_device->buffer_size = 16384; - */ - lib_device->transfer_count = 4; - lib_device->buffer_size = 262144; /* 1048576; */ lib_device->streaming = false; do_exit = false; From d61efc0c077145f78a506df7599318f4043a6fed Mon Sep 17 00:00:00 2001 From: Dominic Spill Date: Wed, 1 Feb 2017 16:02:08 -0700 Subject: [PATCH 17/19] Remove experimental firmware code - never used in firmware --- firmware/common/sgpio_dma.c | 142 ------------------------------------ firmware/common/sgpio_dma.h | 48 ------------ 2 files changed, 190 deletions(-) delete mode 100644 firmware/common/sgpio_dma.c delete mode 100644 firmware/common/sgpio_dma.h diff --git a/firmware/common/sgpio_dma.c b/firmware/common/sgpio_dma.c deleted file mode 100644 index dc78ddf1..00000000 --- a/firmware/common/sgpio_dma.c +++ /dev/null @@ -1,142 +0,0 @@ -/* - * Copyright 2013 Jared Boone - * - * 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 - -#include -#include -#include -#include - -#include -#include - -void sgpio_dma_configure_lli( - gpdma_lli_t* const lli, - const size_t lli_count, - const bool direction_transmit, - void* const buffer, - const size_t transfer_bytes -) { - const size_t bytes_per_word = 4; - const size_t transfer_words = (transfer_bytes + bytes_per_word - 1) / bytes_per_word; - - gpdma_lli_create_loop(lli, lli_count); - - for(size_t i=0; icsrcaddr; - GPDMA_CDESTADDR(channel) = (uint32_t)lli->cdestaddr; - GPDMA_CLLI(channel) = (uint32_t)lli->clli; - GPDMA_CCONTROL(channel) = lli->ccontrol; - - /* 1: Memory -> Peripheral - * 2: Peripheral -> Memory */ - const uint_fast8_t flowcntrl = direction_transmit ? 1 : 2; - - GPDMA_CCONFIG(channel) = - GPDMA_CCONFIG_E(0) | - GPDMA_CCONFIG_SRCPERIPHERAL(0) | - GPDMA_CCONFIG_DESTPERIPHERAL(0) | - GPDMA_CCONFIG_FLOWCNTRL(flowcntrl) | - GPDMA_CCONFIG_IE(1) | - GPDMA_CCONFIG_ITC(1) | - GPDMA_CCONFIG_L(0) | - GPDMA_CCONFIG_H(0) - ; - - gpdma_channel_enable(channel); -} - -void sgpio_dma_init() { - /* DMA peripheral/source 0, option 2 (SGPIO14) -- BREQ */ - CREG_DMAMUX &= ~(CREG_DMAMUX_DMAMUXPER0_MASK); - CREG_DMAMUX |= CREG_DMAMUX_DMAMUXPER0(0x2); - - // Disable sync, maybe it is causing max speed (10MT/sec) glitches? - //GPDMA_DMACSYNC = (1 << 0); - //GPDMA_SYNC = GPDMA_SYNC_DMACSYNC(0xFFFF); // TODO: Don't do this, I'm just going nuts here. - - gpdma_controller_enable(); -} - -static const uint_fast8_t dma_channel_sgpio = 0; - -void sgpio_dma_rx_start(const gpdma_lli_t* const start_lli) { - sgpio_dma_enable(dma_channel_sgpio, start_lli, false); -} - -void sgpio_dma_tx_start(const gpdma_lli_t* const start_lli) { - sgpio_dma_enable(dma_channel_sgpio, start_lli, true); -} - -void sgpio_dma_irq_tc_acknowledge() { - gpdma_channel_interrupt_tc_clear(dma_channel_sgpio); -} - -void sgpio_dma_stop() { - gpdma_channel_disable(dma_channel_sgpio); -} - -size_t sgpio_dma_current_transfer_index( - const gpdma_lli_t* const lli, - const size_t lli_count -) { - const uint32_t next_lli = GPDMA_CLLI(dma_channel_sgpio); - for(size_t i=0; i - * - * 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. - */ - -#ifndef __SGPIO_DMA_H__ -#define __SGPIO_DMA_H__ - -#include - -#include - -void sgpio_dma_configure_lli( - gpdma_lli_t* const lli, - const size_t lli_count, - const bool direction_transmit, - void* const buffer, - const size_t transfer_bytes -); - -void sgpio_dma_init(); -void sgpio_dma_rx_start(const gpdma_lli_t* const start_lli); -void sgpio_dma_tx_start(const gpdma_lli_t* const start_lli); -void sgpio_dma_irq_tc_acknowledge(); -void sgpio_dma_stop(); - -size_t sgpio_dma_current_transfer_index( - const gpdma_lli_t* const lli, - const size_t lli_count -); - -#endif/*__SGPIO_DMA_H__*/ From 0eb85c529c9850f57c210c1ca733dc5e8666c4cc Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Wed, 1 Feb 2017 16:21:04 -0700 Subject: [PATCH 18/19] Fixed firmware dwell time bug in sweep mode. Two buffers of sample data are now thrown away at the start of each step in order to allow time for tuning. Fixes #317. --- firmware/hackrf_usb/usb_api_sweep.c | 17 ++++++++++------- host/hackrf-tools/src/hackrf_sweep.c | 7 ++++++- 2 files changed, 16 insertions(+), 8 deletions(-) diff --git a/firmware/hackrf_usb/usb_api_sweep.c b/firmware/hackrf_usb/usb_api_sweep.c index 581faacc..503e7f6a 100644 --- a/firmware/hackrf_usb/usb_api_sweep.c +++ b/firmware/hackrf_usb/usb_api_sweep.c @@ -34,6 +34,7 @@ #define MIN_FREQ 1 #define MAX_FREQ 6000 #define MAX_FREQ_COUNT 1000 +#define THROWAWAY_BUFFERS 2 volatile bool start_sweep_mode = false; static uint64_t sweep_freq; @@ -89,16 +90,18 @@ void sweep_mode(void) { if (transfer) { *(uint16_t*)buffer = 0x7F7F; *(uint16_t*)(buffer+2) = sweep_freq; - usb_transfer_schedule_block( - &usb_endpoint_bulk_in, - buffer, - 0x4000, - NULL, NULL - ); + if (blocks_queued > THROWAWAY_BUFFERS) { + usb_transfer_schedule_block( + &usb_endpoint_bulk_in, + buffer, + 0x4000, + NULL, NULL + ); + } transfer = false; } - if (blocks_queued >= dwell_blocks) { + if ((dwell_blocks + THROWAWAY_BUFFERS) <= blocks_queued) { if(++ifreq >= frequency_count) ifreq = 0; sweep_freq = frequencies[ifreq]; diff --git a/host/hackrf-tools/src/hackrf_sweep.c b/host/hackrf-tools/src/hackrf_sweep.c index 6c044203..13a9c64d 100644 --- a/host/hackrf-tools/src/hackrf_sweep.c +++ b/host/hackrf-tools/src/hackrf_sweep.c @@ -243,7 +243,7 @@ static void usage() { 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"); + fprintf(stderr, "\t[-n num_samples] # Number of samples per frequency, 16384-4294967296\n"); } static hackrf_device* device = NULL; @@ -355,6 +355,11 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } + if (num_samples < 0x4000) { + fprintf(stderr, "warning: num_samples (-n) must be at least 16384\n"); + return EXIT_FAILURE; + } + if( amp ) { if( amp_enable > 1 ) { fprintf(stderr, "argument error: amp_enable shall be 0 or 1.\n"); From e7d459b30f1c4f8bf367959a1552c4edabc22c42 Mon Sep 17 00:00:00 2001 From: Michael Ossmann Date: Wed, 1 Feb 2017 21:00:35 -0700 Subject: [PATCH 19/19] Changed "User cancel" message to "Exiting" because it can happen in situations other than a user action. Fixes #319 investigate "User cancel error" --- host/hackrf-tools/src/hackrf_sweep.c | 2 +- host/hackrf-tools/src/hackrf_transfer.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/host/hackrf-tools/src/hackrf_sweep.c b/host/hackrf-tools/src/hackrf_sweep.c index 13a9c64d..0c6a81df 100644 --- a/host/hackrf-tools/src/hackrf_sweep.c +++ b/host/hackrf-tools/src/hackrf_sweep.c @@ -518,7 +518,7 @@ int main(int argc, char** argv) { result = hackrf_is_streaming(device); if (do_exit) { - fprintf(stderr, "\nUser cancel, exiting...\n"); + fprintf(stderr, "\nExiting...\n"); } else { fprintf(stderr, "\nExiting... hackrf_is_streaming() result: %s (%d)\n", hackrf_error_name(result), result); diff --git a/host/hackrf-tools/src/hackrf_transfer.c b/host/hackrf-tools/src/hackrf_transfer.c index ab211993..88221716 100644 --- a/host/hackrf-tools/src/hackrf_transfer.c +++ b/host/hackrf-tools/src/hackrf_transfer.c @@ -1091,7 +1091,7 @@ int main(int argc, char** argv) { result = hackrf_is_streaming(device); if (do_exit) { - fprintf(stderr, "\nUser cancel, exiting...\n"); + fprintf(stderr, "\nExiting...\n"); } else { fprintf(stderr, "\nExiting... hackrf_is_streaming() result: %s (%d)\n", hackrf_error_name(result), result); }