From 4aac303480403715f8bae2167339925c6d9ba685 Mon Sep 17 00:00:00 2001 From: Mike Walters Date: Wed, 22 Jan 2020 20:54:25 +0000 Subject: [PATCH] Add option to disable HackRF UI Fixes #608 --- firmware/common/hackrf_ui.c | 16 ++++++++++- firmware/common/hackrf_ui.h | 3 +++ firmware/common/ui_portapack.c | 6 +++++ firmware/common/ui_rad1o.c | 5 ++++ firmware/hackrf_usb/CMakeLists.txt | 1 + firmware/hackrf_usb/hackrf_usb.c | 2 ++ firmware/hackrf_usb/usb_api_ui.c | 40 ++++++++++++++++++++++++++++ firmware/hackrf_usb/usb_api_ui.h | 33 +++++++++++++++++++++++ firmware/hackrf_usb/usb_descriptor.c | 2 +- host/hackrf-tools/src/hackrf_debug.c | 19 ++++++++++--- host/libhackrf/src/hackrf.c | 27 ++++++++++++++++++- host/libhackrf/src/hackrf.h | 2 ++ 12 files changed, 150 insertions(+), 6 deletions(-) create mode 100644 firmware/hackrf_usb/usb_api_ui.c create mode 100644 firmware/hackrf_usb/usb_api_ui.h diff --git a/firmware/common/hackrf_ui.c b/firmware/common/hackrf_ui.c index 39518f60..6d8a6016 100644 --- a/firmware/common/hackrf_ui.c +++ b/firmware/common/hackrf_ui.c @@ -30,6 +30,7 @@ /* Stub functions for null UI function table */ void hackrf_ui_init_null(void) { } +void hackrf_ui_deinit_null(void) { } void hackrf_ui_set_frequency_null(uint64_t frequency) { UNUSED(frequency); } void hackrf_ui_set_sample_rate_null(uint32_t sample_rate) { UNUSED(sample_rate); } void hackrf_ui_set_direction_null(const rf_path_direction_t direction) { UNUSED(direction); } @@ -49,6 +50,7 @@ bool hackrf_ui_operacake_gpio_compatible_null(void) { return true; } */ static const hackrf_ui_t hackrf_ui_null = { &hackrf_ui_init_null, + &hackrf_ui_deinit_null, &hackrf_ui_set_frequency_null, &hackrf_ui_set_sample_rate_null, &hackrf_ui_set_direction_null, @@ -65,10 +67,11 @@ static const hackrf_ui_t hackrf_ui_null = { }; static const hackrf_ui_t* ui = NULL; +static bool ui_enabled = true; const hackrf_ui_t* hackrf_ui(void) { /* Detect on first use. If no UI hardware is detected, use a stub function table. */ - if( ui == NULL ) { + if( ui == NULL && ui_enabled ) { #ifdef HACKRF_ONE if( portapack_hackrf_ui_init ) { ui = portapack_hackrf_ui_init(); @@ -86,3 +89,14 @@ const hackrf_ui_t* hackrf_ui(void) { } return ui; } + +void hackrf_ui_set_enable(bool enabled) { + if (ui_enabled != enabled) { + ui_enabled = enabled; + hackrf_ui()->deinit(); + ui = NULL; + if (enabled) { + hackrf_ui()->init(); + } + } +} diff --git a/firmware/common/hackrf_ui.h b/firmware/common/hackrf_ui.h index a3252b54..209af4f1 100644 --- a/firmware/common/hackrf_ui.h +++ b/firmware/common/hackrf_ui.h @@ -26,6 +26,7 @@ #include typedef void (*hackrf_ui_init_fn)(void); +typedef void (*hackrf_ui_deinit_fn)(void); typedef void (*hackrf_ui_set_frequency_fn)(uint64_t frequency); typedef void (*hackrf_ui_set_sample_rate_fn)(uint32_t sample_rate); typedef void (*hackrf_ui_set_direction_fn)(const rf_path_direction_t direction); @@ -42,6 +43,7 @@ typedef bool (*hackrf_ui_operacake_gpio_compatible_fn)(void); typedef struct { hackrf_ui_init_fn init; + hackrf_ui_deinit_fn deinit; hackrf_ui_set_frequency_fn set_frequency; hackrf_ui_set_sample_rate_fn set_sample_rate; hackrf_ui_set_direction_fn set_direction; @@ -62,5 +64,6 @@ typedef struct { * or NULL if no UI was detected. */ const hackrf_ui_t* hackrf_ui(void); +void hackrf_ui_set_enable(bool); #endif diff --git a/firmware/common/ui_portapack.c b/firmware/common/ui_portapack.c index 24383b7d..54eca185 100644 --- a/firmware/common/ui_portapack.c +++ b/firmware/common/ui_portapack.c @@ -406,6 +406,11 @@ static void portapack_ui_init(void) { portapack_radio_path_redraw(); } +static void portapack_ui_deinit(void) { + portapack_clear_display(color_background); + portapack_backlight(false); +} + static void portapack_ui_set_frequency(uint64_t frequency) { static char last[10] = " "; @@ -555,6 +560,7 @@ static bool portapack_ui_operacake_gpio_compatible(void) { const hackrf_ui_t portapack_hackrf_ui = { &portapack_ui_init, + &portapack_ui_deinit, &portapack_ui_set_frequency, &portapack_ui_set_sample_rate, &portapack_ui_set_direction, diff --git a/firmware/common/ui_rad1o.c b/firmware/common/ui_rad1o.c index d94fb53b..a725bf31 100644 --- a/firmware/common/ui_rad1o.c +++ b/firmware/common/ui_rad1o.c @@ -40,6 +40,10 @@ static void rad1o_ui_init(void) { hackrf_ui_init(); } +static void rad1o_ui_deinit(void) { + +} + static void rad1o_ui_set_frequency(uint64_t frequency) { hackrf_ui_setFrequency(frequency); } @@ -94,6 +98,7 @@ static bool rad1o_ui_operacake_gpio_compatible(void) { static const hackrf_ui_t rad1o_ui = { &rad1o_ui_init, + &rad1o_ui_deinit, &rad1o_ui_set_frequency, &rad1o_ui_set_sample_rate, &rad1o_ui_set_direction, diff --git a/firmware/hackrf_usb/CMakeLists.txt b/firmware/hackrf_usb/CMakeLists.txt index e97968a0..c918f882 100644 --- a/firmware/hackrf_usb/CMakeLists.txt +++ b/firmware/hackrf_usb/CMakeLists.txt @@ -49,6 +49,7 @@ set(SRC_M4 usb_api_transceiver.c usb_api_operacake.c usb_api_sweep.c + usb_api_ui.c "${PATH_HACKRF_FIRMWARE_COMMON}/usb_queue.c" "${PATH_HACKRF_FIRMWARE_COMMON}/fault_handler.c" "${PATH_HACKRF_FIRMWARE_COMMON}/cpld_jtag.c" diff --git a/firmware/hackrf_usb/hackrf_usb.c b/firmware/hackrf_usb/hackrf_usb.c index 88159db4..251a999d 100644 --- a/firmware/hackrf_usb/hackrf_usb.c +++ b/firmware/hackrf_usb/hackrf_usb.c @@ -46,6 +46,7 @@ #include "operacake.h" #include "usb_api_sweep.h" #include "usb_api_transceiver.h" +#include "usb_api_ui.h" #include "usb_bulk_buffer.h" #include "cpld_xc2c.h" #include "portapack.h" @@ -107,6 +108,7 @@ static usb_request_handler_fn vendor_request_handler[] = { #else NULL, #endif + usb_vendor_request_set_ui_enable, }; static const uint32_t vendor_request_handler_count = diff --git a/firmware/hackrf_usb/usb_api_ui.c b/firmware/hackrf_usb/usb_api_ui.c new file mode 100644 index 00000000..ebb9ab9e --- /dev/null +++ b/firmware/hackrf_usb/usb_api_ui.c @@ -0,0 +1,40 @@ +/* + * Copyright 2020 Mike Walters + * + * 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 "usb_api_ui.h" + +#include +#include +#include + +#include +#include + +usb_request_status_t usb_vendor_request_set_ui_enable( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage +) { + if (stage == USB_TRANSFER_STAGE_SETUP) { + hackrf_ui_set_enable(endpoint->setup.value); + usb_transfer_schedule_ack(endpoint->in); + } + return USB_REQUEST_STATUS_OK; +} diff --git a/firmware/hackrf_usb/usb_api_ui.h b/firmware/hackrf_usb/usb_api_ui.h new file mode 100644 index 00000000..720d74f7 --- /dev/null +++ b/firmware/hackrf_usb/usb_api_ui.h @@ -0,0 +1,33 @@ +/* + * Copyright 2020 Mike Walters + * + * 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 __USB_API_UI_H__ +#define __USB_API_UI_H__ + +#include +#include + +usb_request_status_t usb_vendor_request_set_ui_enable( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage +); + +#endif /* end of include guard: __USB_API_UI_H__ */ diff --git a/firmware/hackrf_usb/usb_descriptor.c b/firmware/hackrf_usb/usb_descriptor.c index 9b8e08cd..fd06a3e2 100644 --- a/firmware/hackrf_usb/usb_descriptor.c +++ b/firmware/hackrf_usb/usb_descriptor.c @@ -36,7 +36,7 @@ #define USB_PRODUCT_ID (0xFFFF) #endif -#define USB_API_VERSION (0x0103) +#define USB_API_VERSION (0x0104) #define USB_WORD(x) (x & 0xFF), ((x >> 8) & 0xFF) diff --git a/host/hackrf-tools/src/hackrf_debug.c b/host/hackrf-tools/src/hackrf_debug.c index 27d03556..64623b6f 100644 --- a/host/hackrf-tools/src/hackrf_debug.c +++ b/host/hackrf-tools/src/hackrf_debug.c @@ -388,6 +388,7 @@ static void usage() { printf("\t-m, --max2837: target MAX2837\n"); printf("\t-s, --si5351c: target SI5351C\n"); printf("\t-f, --rffc5072: target RFFC5072\n"); + printf("\t-u, --ui <1/0>: enable/disable UI\n"); printf("\nExamples:\n"); printf("\thackrf_debug --si5351c -n 0 -r # reads from si5351c register 0\n"); printf("\thackrf_debug --si5351c -c # displays si5351c multisynth configuration\n"); @@ -405,6 +406,7 @@ static struct option long_options[] = { { "max2837", no_argument, 0, 'm' }, { "si5351c", no_argument, 0, 's' }, { "rffc5072", no_argument, 0, 'f' }, + { "ui", required_argument, 0, 'u' }, { 0, 0, 0, 0 }, }; @@ -419,6 +421,8 @@ int main(int argc, char** argv) { bool dump_config = false; uint8_t part = PART_NONE; const char* serial_number = NULL; + bool set_ui = false; + uint16_t ui_enable; int result = hackrf_init(); if(result) { @@ -426,7 +430,7 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - while( (opt = getopt_long(argc, argv, "n:rw:d:cmsfh?", long_options, &option_index)) != EOF ) { + while( (opt = getopt_long(argc, argv, "n:rw:d:cmsfh?u:", long_options, &option_index)) != EOF ) { switch( opt ) { case 'n': result = parse_int(optarg, ®ister_number); @@ -473,6 +477,11 @@ int main(int argc, char** argv) { part = PART_RFFC5072; break; + case 'u': + set_ui = true; + result = parse_int(optarg, &ui_enable); + break; + case 'h': case '?': usage(); @@ -508,13 +517,13 @@ int main(int argc, char** argv) { return EXIT_FAILURE; } - if(!(write || read || dump_config)) { + if(!(write || read || dump_config || set_ui)) { fprintf(stderr, "Specify read, write, or config option.\n"); usage(); return EXIT_FAILURE; } - if(part == PART_NONE) { + if(part == PART_NONE && !set_ui) { fprintf(stderr, "Specify a part to read, write, or print config from.\n"); usage(); return EXIT_FAILURE; @@ -542,6 +551,10 @@ int main(int argc, char** argv) { si5351c_read_configuration(device); } + if(set_ui) { + result = hackrf_set_ui_enable(device, ui_enable); + } + result = hackrf_close(device); if(result) { printf("hackrf_close() failed: %s (%d)\n", hackrf_error_name(result), result); diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 4665da13..12c8bcc2 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -85,6 +85,7 @@ typedef enum { HACKRF_VENDOR_REQUEST_SPIFLASH_CLEAR_STATUS = 34, HACKRF_VENDOR_REQUEST_OPERACAKE_GPIO_TEST = 35, HACKRF_VENDOR_REQUEST_CPLD_CHECKSUM = 36, + HACKRF_VENDOR_REQUEST_UI_ENABLE = 37, } hackrf_vendor_request; #define USB_CONFIG_STANDARD 0x1 @@ -1850,7 +1851,7 @@ 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) */ +/* All features below require USB API version 0x0102 or higher) */ int ADDCALL hackrf_set_hw_sync_mode(hackrf_device* device, const uint8_t value) { USB_API_REQUIRED(device, 0x0102) @@ -2137,6 +2138,30 @@ int ADDCALL hackrf_cpld_checksum(hackrf_device* device, } #endif /* HACKRF_ISSUE_609_IS_FIXED */ +int ADDCALL hackrf_set_ui_enable(hackrf_device* device, const uint8_t value) +{ + USB_API_REQUIRED(device, 0x0104) + int result; + result = libusb_control_transfer( + device->usb_device, + LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_UI_ENABLE, + value, + 0, + NULL, + 0, + 0 + ); + + if (result != 0) + { + last_libusb_error = result; + return HACKRF_ERROR_LIBUSB; + } else { + return HACKRF_SUCCESS; + } +} + #ifdef __cplusplus } // __cplusplus defined. #endif diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index 0dc6b1b2..9cdf2c48 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -258,6 +258,8 @@ extern ADDAPI int ADDCALL hackrf_cpld_checksum(hackrf_device* device, uint32_t* crc); #endif /* HACKRF_ISSUE_609_IS_FIXED */ +extern ADDAPI int ADDCALL hackrf_set_ui_enable(hackrf_device* device, const uint8_t value); + #ifdef __cplusplus } // __cplusplus defined. #endif