diff --git a/firmware/common/fpga.h b/firmware/common/fpga.h index 1765fcdd1..58152e687 100644 --- a/firmware/common/fpga.h +++ b/firmware/common/fpga.h @@ -26,6 +26,14 @@ #include "ice40_spi.h" +/* Supported Bitstreams. */ +typedef enum { + FPGA_BITSTREAM_STANDARD = 0, + FPGA_BITSTREAM_HALFPREC = 1, + FPGA_BITSTREAM_EXTPREC_RX = 2, + FPGA_BITSTREAM_EXTPREC_TX = 3, +} fpga_bitstream_index_t; + /* Up to 7 registers, each containing up to 8 bits of data */ #define FPGA_NUM_REGS 7 #define FPGA_DATA_REGS_MAX_VALUE 255 diff --git a/firmware/common/radio.c b/firmware/common/radio.c index f8b2e5141..d9a9f80b7 100644 --- a/firmware/common/radio.c +++ b/firmware/common/radio.c @@ -59,6 +59,7 @@ void radio_init(radio_t* const radio) radio->config[RADIO_BANK_TX][RADIO_OPMODE] = TRANSCEIVER_MODE_TX; radio->config[RADIO_BANK_IDLE][RADIO_BIAS_TEE] = false; radio->regs_dirty = 0; + radio->regs_locked = 0; } static inline void mark_dirty(radio_t* const radio, radio_register_t reg) @@ -66,6 +67,11 @@ static inline void mark_dirty(radio_t* const radio, radio_register_t reg) radio->regs_dirty |= (1 << reg); } +static inline bool check_locked(radio_t* const radio, radio_register_t reg) +{ + return radio->regs_locked & (1 << reg); +} + radio_error_t radio_reg_write( radio_t* const radio, const radio_register_bank_t bank, @@ -76,6 +82,10 @@ radio_error_t radio_reg_write( return RADIO_ERR_INVALID_REGISTER; } + if (check_locked(radio, reg)) { + return RADIO_ERR_LOCKED_REGISTER; + } + switch (bank) { case RADIO_BANK_ACTIVE: mark_dirty(radio, reg); @@ -106,6 +116,20 @@ uint64_t radio_reg_read( return radio->config[bank][reg]; } +radio_error_t radio_reg_lock( + radio_t* const radio, + const radio_register_t reg, + const bool locked) +{ + if (reg > RADIO_NUM_REGS) { + return RADIO_ERR_INVALID_REGISTER; + } + + radio->regs_locked = (radio->regs_locked & ~(1 << reg)) | (locked << reg); + + return RADIO_OK; +} + static uint32_t radio_update_direction(radio_t* const radio, uint64_t* bank) { const uint64_t requested = bank[RADIO_OPMODE]; @@ -928,3 +952,68 @@ void radio_switch_opmode(radio_t* const radio, const transceiver_mode_t mode) nvic_enable_irq(NVIC_USB0_IRQ); radio_update(radio); } + +bool radio_set_config_mode(radio_t* const radio, const radio_config_mode_t mode) +{ + // Check if the requested mode is supported. + switch (mode) { + case RADIO_CONFIG_STANDARD: + // supported on all boards + break; +#ifdef IS_PRALINE + case RADIO_CONFIG_HALF_PRECISION: + case RADIO_CONFIG_EXT_PRECISION_RX: + case RADIO_CONFIG_EXT_PRECISION_TX: + // only supported on praline + if (!IS_PRALINE) { + return false; + } + break; +#endif + default: + return false; + } + + // Don't do anything if we're already in the requested mode. + if (mode == radio->config_mode) { + return true; + } + +#if defined(IS_PRALINE) && !(defined(DFU_MODE) || defined(RAM_MODE)) + if (IS_PRALINE) { + fpga_bitstream_index_t bitstream_index; + switch (mode) { + case RADIO_CONFIG_STANDARD: + bitstream_index = FPGA_BITSTREAM_STANDARD; + break; + case RADIO_CONFIG_HALF_PRECISION: + bitstream_index = FPGA_BITSTREAM_HALFPREC; + break; + case RADIO_CONFIG_EXT_PRECISION_RX: + bitstream_index = FPGA_BITSTREAM_EXTPREC_RX; + break; + case RADIO_CONFIG_EXT_PRECISION_TX: + bitstream_index = FPGA_BITSTREAM_EXTPREC_TX; + break; + default: + return false; + } + + // Reset radio state. + for (uint8_t reg = 0; reg < RADIO_NUM_REGS; reg++) { + radio_reg_write(radio, RADIO_BANK_ALL, reg, RADIO_UNSET); + } + + // Load bitstream. + extern struct fpga_loader_t fpga_loader; + if (!fpga_image_load(&fpga_loader, bitstream_index)) { + return false; + } + } +#endif + + // Update radio config mode. + radio->config_mode = mode; + + return true; +} diff --git a/firmware/common/radio.h b/firmware/common/radio.h index 821c7fead..df7b6462d 100644 --- a/firmware/common/radio.h +++ b/firmware/common/radio.h @@ -34,6 +34,7 @@ typedef enum { RADIO_ERR_INVALID_PARAM = -2, RADIO_ERR_INVALID_BANK = -3, RADIO_ERR_INVALID_REGISTER = -4, + RADIO_ERR_LOCKED_REGISTER = -5, RADIO_ERR_UNSUPPORTED_OPERATION = -10, RADIO_ERR_UNIMPLEMENTED = -19, RADIO_ERR_OTHER = -9999, @@ -41,11 +42,12 @@ typedef enum { /* radio configuration modes */ typedef enum { - RADIO_CONFIG_LEGACY = 0, - RADIO_CONFIG_STANDARD = 1, - RADIO_CONFIG_EXT_PRECISION_RX = 2, - RADIO_CONFIG_EXT_PRECISION_TX = 3, - RADIO_CONFIG_HALF_PRECISION = 4, + RADIO_CONFIG_STANDARD = 0, +#ifdef IS_PRALINE + RADIO_CONFIG_EXT_PRECISION_RX = 1, + RADIO_CONFIG_EXT_PRECISION_TX = 2, + RADIO_CONFIG_HALF_PRECISION = 3, +#endif } radio_config_mode_t; typedef struct { @@ -225,6 +227,7 @@ typedef struct { radio_config_mode_t config_mode; uint64_t config[RADIO_NUM_BANKS][RADIO_NUM_REGS]; volatile uint32_t regs_dirty; + uint32_t regs_locked; update_fn update_cb; } radio_t; @@ -248,6 +251,15 @@ uint64_t radio_reg_read( const radio_register_bank_t bank, const radio_register_t reg); +/** + * Lock a register. Prevents any future calls to `radio_reg_write` + * from overwriting the current stored value of the register. + */ +radio_error_t radio_reg_lock( + radio_t* const radio, + const radio_register_t reg, + const bool locked); + /** * Apply changes requested in RADIO_BANK_ACTIVE. * Return true if any changes were applied. @@ -260,6 +272,12 @@ bool radio_update(radio_t* const radio); */ void radio_switch_opmode(radio_t* const radio, const transceiver_mode_t mode); +/** + * Switch to a new configuration mode. + * Return true if the mode was successfully switched. + */ +bool radio_set_config_mode(radio_t* const radio, const radio_config_mode_t mode); + /** * Driver instance. */ diff --git a/firmware/hackrf_usb/CMakeLists.txt b/firmware/hackrf_usb/CMakeLists.txt index 871e0909f..027fc97a8 100644 --- a/firmware/hackrf_usb/CMakeLists.txt +++ b/firmware/hackrf_usb/CMakeLists.txt @@ -60,6 +60,7 @@ set(SRC_M4 usb_api_spiflash.c usb_api_transceiver.c usb_api_operacake.c + usb_api_radio.c usb_api_sweep.c usb_api_selftest.c usb_api_ui.c diff --git a/firmware/hackrf_usb/hackrf_usb.c b/firmware/hackrf_usb/hackrf_usb.c index 813a94539..8cfeabe85 100644 --- a/firmware/hackrf_usb/hackrf_usb.c +++ b/firmware/hackrf_usb/hackrf_usb.c @@ -75,6 +75,7 @@ #include "usb_api_board_info.h" #include "usb_api_m0_state.h" #include "usb_api_operacake.h" +#include "usb_api_radio.h" #include "usb_api_register.h" #include "usb_api_selftest.h" #include "usb_api_spiflash.h" @@ -182,6 +183,10 @@ static usb_request_handler_fn vendor_request_handler[] = { usb_vendor_request_write_radio_reg, usb_vendor_request_read_radio_reg, usb_vendor_request_get_buffer_size, + usb_vendor_request_lock_radio_reg, + usb_vendor_request_open, + usb_vendor_request_close, + usb_vendor_request_set_radio_mode, }; static const uint32_t vendor_request_handler_count = diff --git a/firmware/hackrf_usb/usb_api_radio.c b/firmware/hackrf_usb/usb_api_radio.c new file mode 100644 index 000000000..e85c7d987 --- /dev/null +++ b/firmware/hackrf_usb/usb_api_radio.c @@ -0,0 +1,41 @@ +/* + * Copyright 2026 Great Scott Gadgets + * + * 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 "usb_queue.h" + +usb_request_status_t usb_vendor_request_set_radio_mode( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage) +{ + if (stage == USB_TRANSFER_STAGE_SETUP) { + radio_config_mode_t mode = endpoint->setup.value; + if (!radio_set_config_mode(&radio, mode)) { + return USB_REQUEST_STATUS_STALL; + } + usb_transfer_schedule_ack(endpoint->in); + } + + return USB_REQUEST_STATUS_OK; +} diff --git a/firmware/hackrf_usb/usb_api_radio.h b/firmware/hackrf_usb/usb_api_radio.h new file mode 100644 index 000000000..9eb098d0d --- /dev/null +++ b/firmware/hackrf_usb/usb_api_radio.h @@ -0,0 +1,29 @@ +/* + * Copyright 2026 Great Scott Gadgets + * + * 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. + */ + +#pragma once + +#include +#include + +usb_request_status_t usb_vendor_request_set_radio_mode( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage); diff --git a/firmware/hackrf_usb/usb_api_register.c b/firmware/hackrf_usb/usb_api_register.c index 626c75089..a46a22404 100644 --- a/firmware/hackrf_usb/usb_api_register.c +++ b/firmware/hackrf_usb/usb_api_register.c @@ -416,3 +416,20 @@ usb_request_status_t usb_vendor_request_read_radio_reg( } return USB_REQUEST_STATUS_OK; } + +usb_request_status_t usb_vendor_request_lock_radio_reg( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage) +{ + if (stage == USB_TRANSFER_STAGE_SETUP) { + uint8_t reg = endpoint->setup.index; + bool locked = endpoint->setup.value != 0 ? true : false; + if (reg >= RADIO_NUM_REGS) { + return USB_REQUEST_STATUS_STALL; + } + radio_reg_lock(&radio, reg, locked); + usb_transfer_schedule_ack(endpoint->in); + } + + return USB_REQUEST_STATUS_OK; +} diff --git a/firmware/hackrf_usb/usb_api_register.h b/firmware/hackrf_usb/usb_api_register.h index a5a36550d..7e434c1a7 100644 --- a/firmware/hackrf_usb/usb_api_register.h +++ b/firmware/hackrf_usb/usb_api_register.h @@ -70,3 +70,6 @@ usb_request_status_t usb_vendor_request_write_radio_reg( usb_request_status_t usb_vendor_request_read_radio_reg( usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage); +usb_request_status_t usb_vendor_request_lock_radio_reg( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage); diff --git a/firmware/hackrf_usb/usb_api_transceiver.c b/firmware/hackrf_usb/usb_api_transceiver.c index dbbbf661a..c694d9d43 100644 --- a/firmware/hackrf_usb/usb_api_transceiver.c +++ b/firmware/hackrf_usb/usb_api_transceiver.c @@ -486,6 +486,42 @@ usb_request_status_t usb_vendor_request_get_buffer_size( return USB_REQUEST_STATUS_OK; } +usb_request_status_t usb_vendor_request_open( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage) +{ + uint16_t usb_api_version; + radio_config_mode_t radio_config_mode; + + if (stage == USB_TRANSFER_STAGE_SETUP) { + usb_api_version = endpoint->setup.value; + radio_config_mode = (radio_config_mode_t) endpoint->setup.index; + + // TODO let device know we have a new libhackrf connection and its supported usb_api_version + (void) usb_api_version; + + // switch bitstreams and update radio mode + if (!radio_set_config_mode(&radio, radio_config_mode)) { + return USB_REQUEST_STATUS_STALL; + } + usb_transfer_schedule_ack(endpoint->in); + } + + return USB_REQUEST_STATUS_OK; +} + +usb_request_status_t usb_vendor_request_close( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage) +{ + if (stage == USB_TRANSFER_STAGE_SETUP) { + // TODO do nothing for now + usb_transfer_schedule_ack(endpoint->in); + } + + return USB_REQUEST_STATUS_OK; +} + /* clang-format off */ // Which GPDMA channel to use. diff --git a/firmware/hackrf_usb/usb_api_transceiver.h b/firmware/hackrf_usb/usb_api_transceiver.h index a29dec118..a28b8514e 100644 --- a/firmware/hackrf_usb/usb_api_transceiver.h +++ b/firmware/hackrf_usb/usb_api_transceiver.h @@ -80,6 +80,12 @@ usb_request_status_t usb_vendor_request_set_rx_overrun_limit( usb_request_status_t usb_vendor_request_get_buffer_size( usb_endpoint_t* const endpoint, const usb_transfer_stage_t stage); +usb_request_status_t usb_vendor_request_open( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage); +usb_request_status_t usb_vendor_request_close( + usb_endpoint_t* const endpoint, + const usb_transfer_stage_t stage); void request_transceiver_mode(transceiver_mode_t mode); void transceiver_startup(transceiver_mode_t mode); diff --git a/firmware/hackrf_usb/usb_descriptor.c b/firmware/hackrf_usb/usb_descriptor.c index 005babb35..dde22e6e3 100644 --- a/firmware/hackrf_usb/usb_descriptor.c +++ b/firmware/hackrf_usb/usb_descriptor.c @@ -28,7 +28,7 @@ #define USB_VENDOR_ID (0x1D50) -#define USB_API_VERSION (0x0112) +#define USB_API_VERSION (0x0113) #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 52ede9cb7..49bc31ed1 100644 --- a/host/hackrf-tools/src/hackrf_debug.c +++ b/host/hackrf-tools/src/hackrf_debug.c @@ -545,6 +545,30 @@ int radio_write_register( return result; } +int radio_lock_register( + hackrf_device* device, + const uint16_t register_number, + const bool register_locked) +{ + int result = HACKRF_SUCCESS; + result = hackrf_radio_lock_register( + device, + (uint8_t) register_number, + register_locked); + + if (result == HACKRF_SUCCESS) { + printf("register [%2d] -> %s\n", + register_number, + register_locked ? "locked" : "unlocked"); + } else { + printf("hackrf_radio_lock_register() failed: %s (%d)\n", + hackrf_error_name(result), + result); + } + + return result; +} + int read_register( hackrf_device* device, uint8_t part, @@ -621,6 +645,20 @@ int write_register( return HACKRF_ERROR_INVALID_PARAM; } +int lock_register( + hackrf_device* device, + uint8_t part, + const uint16_t register_number, + const bool register_locked) +{ + switch (part) { + case PART_RADIO: + return radio_lock_register(device, register_number, register_locked); + default: + return HACKRF_ERROR_INVALID_PARAM; + } +} + static const char* mode_name(uint32_t mode) { const char* mode_names[] = {"IDLE", "WAIT", "RX", "TX_START", "TX_RUN"}; @@ -675,6 +713,7 @@ static void usage() 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-L, --lock : lock register specified by last -n argument (0 for unlocked, 1 for locked)\n"); printf("\t-c, --config: print SI5351C multisynth configuration information\n"); printf("\t-d, --device : specify a particular device by serial number\n"); printf("\t-m, --max283x: target MAX283x\n"); @@ -709,6 +748,7 @@ static struct option long_options[] = { {"register", required_argument, 0, 'n'}, {"write", required_argument, 0, 'w'}, {"read", no_argument, 0, 'r'}, + {"lock", required_argument, 0, 'L'}, {"device", required_argument, 0, 'd'}, {"help", no_argument, 0, 'h'}, {"max2837", no_argument, 0, 'm'}, @@ -740,10 +780,12 @@ int main(int argc, char** argv) int bank = -1; uint64_t register_number = REGISTER_INVALID; uint64_t register_value; + uint64_t register_locked; hackrf_device* device = NULL; int option_index = 0; bool read = false; bool write = false; + bool lock = false; bool dump_config = false; bool dump_state = false; uint8_t part = PART_NONE; @@ -782,7 +824,7 @@ int main(int argc, char** argv) while ((opt = getopt_long( argc, argv, - "b:n:rw:d:cmsfgi1:2:C:N:P:ST:R:h?u:l:ta:o", + "b:n:rw:l:d:cmsfgi1:2:C:N:P:ST:R:h?u:l:ta:o", long_options, &option_index)) != EOF) { switch (opt) { @@ -805,6 +847,11 @@ int main(int argc, char** argv) read = true; break; + case 'L': + lock = true; + result = parse_int(optarg, ®ister_locked); + break; + case 'c': dump_config = true; break; @@ -931,8 +978,8 @@ int main(int argc, char** argv) } } - if (write && read) { - fprintf(stderr, "Read and write options are mutually exclusive.\n"); + if ((write && read) || (lock && write) || (lock && read)) { + fprintf(stderr, "Read, write and lock options are mutually exclusive.\n"); usage(); return EXIT_FAILURE; } @@ -949,6 +996,10 @@ int main(int argc, char** argv) return EXIT_FAILURE; } + if (lock && part != PART_RADIO) { + fprintf(stderr, "Lock option is only valid for radio.\n"); + } + if ((bank > -1) && (part != PART_RADIO)) { fprintf(stderr, "Bank valid only for radio.\n"); usage(); @@ -959,11 +1010,11 @@ int main(int argc, char** argv) bank = 0; } - if (!(write || read || dump_config || dump_state || set_tx_limit || + if (!(write || read || lock || dump_config || dump_state || set_tx_limit || set_rx_limit || set_ui || set_leds || set_p1 || set_p2 || set_clkin || set_narrowband || set_fpga_bitstream || read_selftest || test_rtc_osc || read_adc)) { - fprintf(stderr, "Specify read, write, or config option.\n"); + fprintf(stderr, "Specify read, write, lock, or config option.\n"); usage(); return EXIT_FAILURE; } @@ -1016,6 +1067,10 @@ int main(int argc, char** argv) } } + if (lock) { + result = lock_register(device, part, register_number, register_locked); + } + if (dump_config) { si5351c_read_configuration(device); } diff --git a/host/libhackrf/src/hackrf.c b/host/libhackrf/src/hackrf.c index 7bafd5f39..c9d67c905 100644 --- a/host/libhackrf/src/hackrf.c +++ b/host/libhackrf/src/hackrf.c @@ -120,6 +120,10 @@ typedef enum { HACKRF_VENDOR_REQUEST_RADIO_WRITE_REG = 59, HACKRF_VENDOR_REQUEST_RADIO_READ_REG = 60, HACKRF_VENDOR_REQUEST_GET_BUFFER_SIZE = 61, + HACKRF_VENDOR_REQUEST_RADIO_LOCK_REG = 62, + HACKRF_VENDOR_REQUEST_OPEN = 63, + HACKRF_VENDOR_REQUEST_CLOSE = 64, + HACKRF_VENDOR_REQUEST_RADIO_SET_MODE = 65, } hackrf_vendor_request; #define USB_CONFIG_STANDARD 0x1 @@ -713,7 +717,10 @@ libusb_device_handle* hackrf_open_usb(const char* const desired_serial_number) return usb_device; } -static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** device) +static int hackrf_open_setup( + enum radio_config_mode mode, + libusb_device_handle* usb_device, + hackrf_device** device) { int result; hackrf_device* lib_device; @@ -795,6 +802,24 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d lib_device->buffer_size = 32768; } + if (lib_device->usb_api_version >= 0x0113) { + // Send supported usb api version and requested configuration mode to device. + result = libusb_control_transfer( + lib_device->usb_device, + LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | + LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_OPEN, + lib_device->usb_api_version, + (uint16_t) mode, + NULL, + 0, + DEFAULT_REQUEST_TIMEOUT); + if (result != 0) { + last_libusb_error = result; + return HACKRF_ERROR_LIBUSB; + } + } + result = pthread_mutex_init(&lib_device->transfer_lock, NULL); if (result != 0) { free(lib_device); @@ -833,7 +858,7 @@ static int hackrf_open_setup(libusb_device_handle* usb_device, hackrf_device** d return HACKRF_SUCCESS; } -int ADDCALL hackrf_open(hackrf_device** device) +int ADDCALL hackrf_open_mode(const enum radio_config_mode mode, hackrf_device** device) { libusb_device_handle* usb_device; @@ -864,17 +889,18 @@ int ADDCALL hackrf_open(hackrf_device** device) return HACKRF_ERROR_NOT_FOUND; } - return hackrf_open_setup(usb_device, device); + return hackrf_open_setup(mode, usb_device, device); } -int ADDCALL hackrf_open_by_serial( +int ADDCALL hackrf_open_mode_by_serial( + const enum radio_config_mode mode, const char* const desired_serial_number, hackrf_device** device) { libusb_device_handle* usb_device; if (desired_serial_number == NULL) { - return hackrf_open(device); + return hackrf_open_mode(mode, device); } if (device == NULL) { @@ -887,10 +913,11 @@ int ADDCALL hackrf_open_by_serial( return HACKRF_ERROR_NOT_FOUND; } - return hackrf_open_setup(usb_device, device); + return hackrf_open_setup(mode, usb_device, device); } -int ADDCALL hackrf_device_list_open( +int ADDCALL hackrf_device_list_open_mode( + const enum radio_config_mode mode, hackrf_device_list_t* list, int idx, hackrf_device** device) @@ -911,7 +938,30 @@ int ADDCALL hackrf_device_list_open( return HACKRF_ERROR_LIBUSB; } - return hackrf_open_setup(usb_device, device); + return hackrf_open_setup(mode, usb_device, device); +} + +int ADDCALL hackrf_open(hackrf_device** device) +{ + return hackrf_open_mode(RADIO_CONFIG_STANDARD, device); +} + +int ADDCALL hackrf_open_by_serial( + const char* const desired_serial_number, + hackrf_device** device) +{ + return hackrf_open_mode_by_serial( + RADIO_CONFIG_STANDARD, + desired_serial_number, + device); +} + +int ADDCALL hackrf_device_list_open( + hackrf_device_list_t* list, + int idx, + hackrf_device** device) +{ + return hackrf_device_list_open_mode(RADIO_CONFIG_STANDARD, list, idx, device); } int ADDCALL hackrf_device_list_bus_sharing(hackrf_device_list_t* list, int idx) @@ -2442,10 +2492,11 @@ int ADDCALL hackrf_stop_tx(hackrf_device* device) int ADDCALL hackrf_close(hackrf_device* device) { - int result1, result2; + int result1, result2, result3; result1 = HACKRF_SUCCESS; result2 = HACKRF_SUCCESS; + result3 = HACKRF_SUCCESS; if (device != NULL) { result1 = hackrf_stop_cmd(device); @@ -2455,6 +2506,27 @@ int ADDCALL hackrf_close(hackrf_device* device) * also cancel any pending transmit/receive transfers. */ result2 = kill_transfer_thread(device); + + if (device->usb_api_version >= 0x0113) { + /* + * Let the device know it's been closed. + */ + result3 = libusb_control_transfer( + device->usb_device, + LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | + LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_CLOSE, + 0, + 0, + NULL, + 0, + DEFAULT_REQUEST_TIMEOUT); + if (result3 != 0) { + last_libusb_error = result3; + result3 = HACKRF_ERROR_LIBUSB; + } + } + if (device->usb_device != NULL) { libusb_release_interface(device->usb_device, 0); libusb_close(device->usb_device); @@ -2473,7 +2545,12 @@ int ADDCALL hackrf_close(hackrf_device* device) if (result2 != HACKRF_SUCCESS) { return result2; } - return result1; + + if (result1 != HACKRF_SUCCESS) { + return result1; + } + + return result3; } const char* ADDCALL hackrf_error_name(enum hackrf_error errcode) @@ -3574,6 +3651,57 @@ int ADDCALL hackrf_radio_write_register( } } +int ADDCALL hackrf_radio_lock_register( + hackrf_device* device, + const uint8_t register_number, + const bool register_locked) +{ + USB_API_REQUIRED(device, 0x0113); + int result; + + result = libusb_control_transfer( + device->usb_device, + LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | + LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_RADIO_LOCK_REG, + register_locked, + register_number, + NULL, + 0, + DEFAULT_REQUEST_TIMEOUT); + + if (result != 0) { + last_libusb_error = result; + return HACKRF_ERROR_LIBUSB; + } + + return HACKRF_SUCCESS; +} + +int ADDCALL hackrf_radio_set_mode(hackrf_device* device, const enum radio_config_mode mode) +{ + USB_API_REQUIRED(device, 0x0113); + int result; + + result = libusb_control_transfer( + device->usb_device, + LIBUSB_ENDPOINT_OUT | LIBUSB_REQUEST_TYPE_VENDOR | + LIBUSB_RECIPIENT_DEVICE, + HACKRF_VENDOR_REQUEST_RADIO_SET_MODE, + mode, + 0, + NULL, + 0, + DEFAULT_REQUEST_TIMEOUT); + + if (result != 0) { + last_libusb_error = result; + return HACKRF_ERROR_LIBUSB; + } + + return HACKRF_SUCCESS; +} + #ifdef __cplusplus } // __cplusplus defined. #endif diff --git a/host/libhackrf/src/hackrf.h b/host/libhackrf/src/hackrf.h index eeca65e03..60666a3a3 100644 --- a/host/libhackrf/src/hackrf.h +++ b/host/libhackrf/src/hackrf.h @@ -946,6 +946,18 @@ enum clkin_ctrl_signal { CLKIN_SIGNAL_P22 = 1, }; +/** + * HackRF Pro Radio Configuration Mode. + * + * Used by @ref hackrf_open, @ref hackrf_open_mode_by_serial and @ref hackrf_device_list_open_mode to set the active configuration mode. + */ +enum radio_config_mode { + RADIO_CONFIG_STANDARD = 0, + RADIO_CONFIG_EXT_PRECISION_RX = 1, + RADIO_CONFIG_EXT_PRECISION_TX = 2, + RADIO_CONFIG_HALF_PRECISION = 3, +}; + /** * Opaque struct for hackrf device info. Object can be created via @ref hackrf_open, @ref hackrf_device_list_open or @ref hackrf_open_by_serial and be destroyed via @ref hackrf_close * @ingroup device @@ -1199,6 +1211,7 @@ extern ADDAPI hackrf_device_list_t* ADDCALL hackrf_device_list(); /** * Open a @ref hackrf_device from a device list + * @deprecated this function has been replaced by @ref hackrf_device_list_open_mode * @param[in] list device list to open device from * @param[in] idx index of the device to open * @param[out] device device handle to open @@ -1210,6 +1223,24 @@ extern ADDAPI int ADDCALL hackrf_device_list_open( int idx, hackrf_device** device); +/** + * Open a @ref hackrf_device from a device list and initialize it to the given radio configuration mode. + * + * Modes other than RADIO_CONFIG_STANDARD are only supported on HackRF Pro hardware. + * + * @param[in] mode configuration mode. Defaults to RADIO_CONFIG_STANDARD. Available modes are defined in @ref radio_config_mode. + * @param[in] list device list to open device from + * @param[in] idx index of the device to open + * @param[out] device device handle to open + * @return @ref HACKRF_SUCCESS on success, @ref HACKRF_ERROR_INVALID_PARAM on invalid parameters or other @ref hackrf_error variant + * @ingroup device + */ +extern ADDAPI int ADDCALL hackrf_device_list_open_mode( + const enum radio_config_mode mode, + hackrf_device_list_t* list, + int idx, + hackrf_device** device); + /** * Check if a listed HackRF device is sharing its USB bus with other devices. * @@ -1231,14 +1262,30 @@ extern ADDAPI void ADDCALL hackrf_device_list_free(hackrf_device_list_t* list); /** * Open first available HackRF device + * @deprecated this function has been replaced by @ref hackrf_open_mode * @param[out] device device handle * @return @ref HACKRF_SUCCESS on success, @ref HACKRF_ERROR_INVALID_PARAM if @p device is NULL, @ref HACKRF_ERROR_NOT_FOUND if no HackRF devices are found or other @ref hackrf_error variant * @ingroup device */ extern ADDAPI int ADDCALL hackrf_open(hackrf_device** device); +/** + * Open first available HackRF device and initialize it to the given radio configuration mode. + * + * Modes other than RADIO_CONFIG_STANDARD are only supported on HackRF Pro hardware. + * + * @param[in] mode configuration mode. Defaults to RADIO_CONFIG_STANDARD. Available modes are defined in @ref radio_config_mode. + * @param[out] device device handle + * @return @ref HACKRF_SUCCESS on success, @ref HACKRF_ERROR_INVALID_PARAM if @p device is NULL, @ref HACKRF_ERROR_NOT_FOUND if no HackRF devices are found or other @ref hackrf_error variant + * @ingroup device + */ +extern ADDAPI int ADDCALL hackrf_open_mode( + const enum radio_config_mode mode, + hackrf_device** device); + /** * Open HackRF device by serial number + * @deprecated this function has been replaced by @ref hackrf_open_mode_by_serial * @param[in] desired_serial_number serial number of device to open. If NULL then default to first device found. * @param[out] device device handle * @return @ref HACKRF_SUCCESS on success, @ref HACKRF_ERROR_INVALID_PARAM if @p device is NULL, @ref HACKRF_ERROR_NOT_FOUND if no HackRF devices are found or other @ref hackrf_error variant @@ -1248,6 +1295,22 @@ extern ADDAPI int ADDCALL hackrf_open_by_serial( const char* const desired_serial_number, hackrf_device** device); +/** + * Open HackRF device by serial number and initialize it to the given radio configuration mode. + * + * Modes other than RADIO_CONFIG_STANDARD are only supported on HackRF Pro hardware. + * + * @param[in] mode configuration mode. Defaults to RADIO_CONFIG_STANDARD. Available modes are defined in @ref radio_config_mode. + * @param[in] desired_serial_number serial number of device to open. If NULL then default to first device found. + * @param[out] device device handle + * @return @ref HACKRF_SUCCESS on success, @ref HACKRF_ERROR_INVALID_PARAM if @p device is NULL, @ref HACKRF_ERROR_NOT_FOUND if no HackRF devices are found or other @ref hackrf_error variant + * @ingroup device + */ +extern ADDAPI int ADDCALL hackrf_open_mode_by_serial( + const enum radio_config_mode mode, + const char* const desired_serial_number, + hackrf_device** device); + /** * Close a previously opened device * @param[in] device device to close @@ -2385,6 +2448,32 @@ extern ADDAPI int ADDCALL hackrf_radio_write_register( const uint8_t register_number, const uint64_t value); +/** + * Lock or unlock a radio configuration register. + * + * @param[in] device device to write + * @param[in] register_number register number to mask + * @param[out] locked locked state for the register + * @return @ref HACKRF_SUCCESS on success or @ref hackrf_error variant + * @ingroup debug + */ +extern ADDAPI int ADDCALL hackrf_radio_lock_register( + hackrf_device* device, + const uint8_t register_number, + const bool register_locked); + +/** + * Switches the radio configuration mode. + * + * @param[in] device device to configure + * @param[in] mode configuration mode. Defaults to RADIO_CONFIG_STANDARD. Available modes are defined in @ref radio_config_mode. + * @return @ref HACKRF_SUCCESS on success or @ref hackrf_error variant + * @ingroup configuration + */ +extern ADDAPI int ADDCALL hackrf_radio_set_mode( + hackrf_device* device, + const enum radio_config_mode mode); + #ifdef __cplusplus } // __cplusplus defined. #endif