Add option to disable HackRF UI

Fixes #608
This commit is contained in:
Mike Walters
2020-01-22 20:54:25 +00:00
parent 7e025f3e46
commit 4aac303480
12 changed files with 150 additions and 6 deletions

View File

@ -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();
}
}
}

View File

@ -26,6 +26,7 @@
#include <stdint.h>
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

View File

@ -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,

View File

@ -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,

View File

@ -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"

View File

@ -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 =

View File

@ -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 <hackrf_core.h>
#include <hackrf_ui.h>
#include <usb_queue.h>
#include <stddef.h>
#include <stdint.h>
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;
}

View File

@ -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 <usb_type.h>
#include <usb_request.h>
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__ */

View File

@ -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)

View File

@ -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, &register_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);

View File

@ -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

View File

@ -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