Skip to content

Commit

Permalink
PortaPack: Remove weak UI functions, detect and return UI function ta…
Browse files Browse the repository at this point in the history
…ble.

TODO: Side effect was that now blinky has a lot of unreasonable dependencies.
TODO: rad1o breakage is likely...
  • Loading branch information
jboone committed Mar 2, 2019
1 parent 36cca31 commit c32d571
Show file tree
Hide file tree
Showing 11 changed files with 115 additions and 94 deletions.
74 changes: 74 additions & 0 deletions firmware/common/hackrf-ui.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
/*
* Copyright (C) 2019 Jared Boone, ShareBrained Technology, Inc.
*
* This file is part of HackRF.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2, or (at your option)
* any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/

#include "hackrf-ui.h"

#include "ui_portapack.h"

#include <stddef.h>

#define UNUSED(x) (void)(x)

/* Stub functions for null UI function table */
void hackrf_ui_init_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); }
void hackrf_ui_set_filter_bw_null(uint32_t bandwidth) { UNUSED(bandwidth); }
void hackrf_ui_set_lna_power_null(bool lna_on) { UNUSED(lna_on); }
void hackrf_ui_set_bb_lna_gain_null(const uint32_t gain_db) { UNUSED(gain_db); }
void hackrf_ui_set_bb_vga_gain_null(const uint32_t gain_db) { UNUSED(gain_db); }
void hackrf_ui_set_bb_tx_vga_gain_null(const uint32_t gain_db) { UNUSED(gain_db); }
void hackrf_ui_set_first_if_frequency_null(const uint64_t frequency) { UNUSED(frequency); }
void hackrf_ui_set_filter_null(const rf_path_filter_t filter) { UNUSED(filter); }
void hackrf_ui_set_antenna_bias_null(bool antenna_bias) { UNUSED(antenna_bias); }

/* Null UI function table, used if there's no hardware UI detected. Eliminates the
* need to check for null UI before calling a function in the table.
*/
static const hackrf_ui_t hackrf_ui_null = {
&hackrf_ui_init_null,
&hackrf_ui_set_frequency_null,
&hackrf_ui_set_sample_rate_null,
&hackrf_ui_set_direction_null,
&hackrf_ui_set_filter_bw_null,
&hackrf_ui_set_lna_power_null,
&hackrf_ui_set_bb_lna_gain_null,
&hackrf_ui_set_bb_vga_gain_null,
&hackrf_ui_set_bb_tx_vga_gain_null,
&hackrf_ui_set_first_if_frequency_null,
&hackrf_ui_set_filter_null,
&hackrf_ui_set_antenna_bias_null,
};

static const hackrf_ui_t* ui = NULL;

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 ) {
ui = portapack_detect();
if( ui == NULL ) {
ui = &hackrf_ui_null;
}
}

return ui;
}
37 changes: 22 additions & 15 deletions firmware/common/hackrf-ui.h
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
/*
* Copyright (C) 2018 Jared Boone, ShareBrained Technology, Inc.
*
* 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 HACKRF_UI_H
#define HACKRF_UI_H

Expand Down Expand Up @@ -36,20 +57,6 @@ typedef struct {
* In the future, whatever UI was detected will be returned here,
* or NULL if no UI was detected.
*/
const hackrf_ui_t* hackrf_ui(void) __attribute__((weak));

void hackrf_ui_init(void) __attribute__((weak));
void hackrf_ui_setFrequency(uint64_t _freq) __attribute__((weak));
void hackrf_ui_setSampleRate(uint32_t _sample_rate) __attribute__((weak));
void hackrf_ui_setDirection(const rf_path_direction_t _direction) __attribute__((weak));
void hackrf_ui_setFilterBW(uint32_t bw) __attribute__((weak));
void hackrf_ui_setLNAPower(bool _lna_on) __attribute__((weak));
void hackrf_ui_setBBLNAGain(const uint32_t gain_db) __attribute__((weak));
void hackrf_ui_setBBVGAGain(const uint32_t gain_db) __attribute__((weak));
void hackrf_ui_setBBTXVGAGain(const uint32_t gain_db) __attribute__((weak));

void hackrf_ui_setFirstIFFrequency(const uint64_t freq) __attribute__((weak));
void hackrf_ui_setFilter(const rf_path_filter_t filter) __attribute__((weak));
void hackrf_ui_setAntennaBias(bool antenna_bias) __attribute__((weak));
const hackrf_ui_t* hackrf_ui(void);

#endif
4 changes: 2 additions & 2 deletions firmware/common/hackrf_core.c
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ bool sample_rate_frac_set(uint32_t rate_num, uint32_t rate_denom)
uint32_t a, b, c;
uint32_t rem;

hackrf_ui_setSampleRate(rate_num/2);
hackrf_ui()->set_sample_rate(rate_num/2);

/* Find best config */
a = (VCO_FREQ * rate_denom) / rate_num;
Expand Down Expand Up @@ -458,7 +458,7 @@ bool sample_rate_set(const uint32_t sample_rate_hz) {
bool baseband_filter_bandwidth_set(const uint32_t bandwidth_hz) {
uint32_t bandwidth_hz_real = max2837_set_lpf_bandwidth(&max2837, bandwidth_hz);

if(bandwidth_hz_real) hackrf_ui_setFilterBW(bandwidth_hz_real);
if(bandwidth_hz_real) hackrf_ui()->set_filter_bw(bandwidth_hz_real);

return bandwidth_hz_real != 0;
}
Expand Down
8 changes: 4 additions & 4 deletions firmware/common/rf_path.c
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ void rf_path_set_direction(rf_path_t* const rf_path, const rf_path_direction_t d

switchctrl_set(rf_path, rf_path->switchctrl);

hackrf_ui_setDirection(direction);
hackrf_ui()->set_direction(direction);
}

void rf_path_set_filter(rf_path_t* const rf_path, const rf_path_filter_t filter) {
Expand All @@ -416,7 +416,7 @@ void rf_path_set_filter(rf_path_t* const rf_path, const rf_path_filter_t filter)

switchctrl_set(rf_path, rf_path->switchctrl);

hackrf_ui_setFilter(filter);
hackrf_ui()->set_filter(filter);
}

void rf_path_set_lna(rf_path_t* const rf_path, const uint_fast8_t enable) {
Expand All @@ -437,7 +437,7 @@ void rf_path_set_lna(rf_path_t* const rf_path, const uint_fast8_t enable) {

switchctrl_set(rf_path, rf_path->switchctrl);

hackrf_ui_setLNAPower(enable);
hackrf_ui()->set_lna_power(enable);
}

/* antenna port power control */
Expand All @@ -450,5 +450,5 @@ void rf_path_set_antenna(rf_path_t* const rf_path, const uint_fast8_t enable) {

switchctrl_set(rf_path, rf_path->switchctrl);

hackrf_ui_setAntennaBias(enable);
hackrf_ui()->set_antenna_bias(enable);
}
2 changes: 1 addition & 1 deletion firmware/common/tuning.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ bool set_freq(const uint64_t freq)
max2837_set_mode(&max2837, prior_max2837_mode);
if( success ) {
freq_cache = freq;
hackrf_ui_setFrequency(freq);
hackrf_ui()->set_frequency(freq);
#ifndef USER_INTERFACE_PORTAPACK
operacake_set_range(freq_mhz);
#endif
Expand Down
57 changes: 0 additions & 57 deletions firmware/common/ui_portapack.c
Original file line number Diff line number Diff line change
Expand Up @@ -1044,61 +1044,4 @@ const hackrf_ui_t* portapack_detect(void) {
}
}

static const hackrf_ui_t* ui = NULL;

const hackrf_ui_t* hackrf_ui(void) {
return ui;
}

void hackrf_ui_init(void) {
ui = portapack_detect();
if( ui != NULL ) {
ui->init();
}
}

void hackrf_ui_setFrequency(uint64_t frequency) {
if( ui != NULL ) ui->set_frequency(frequency);
}

void hackrf_ui_setSampleRate(uint32_t sample_rate) {
if( ui != NULL ) ui->set_sample_rate(sample_rate);
}

void hackrf_ui_setDirection(const rf_path_direction_t direction) {
if( ui != NULL ) ui->set_direction(direction);
}

void hackrf_ui_setFilterBW(uint32_t bw) {
if( ui != NULL ) ui->set_filter_bw(bw);
}

void hackrf_ui_setLNAPower(bool lna_on) {
if( ui != NULL ) ui->set_lna_power(lna_on);
}

void hackrf_ui_setBBLNAGain(const uint32_t gain_db) {
if( ui != NULL ) ui->set_bb_lna_gain(gain_db);
}

void hackrf_ui_setBBVGAGain(const uint32_t gain_db) {
if( ui != NULL ) ui->set_bb_vga_gain(gain_db);
}

void hackrf_ui_setBBTXVGAGain(const uint32_t gain_db) {
if( ui != NULL ) ui->set_bb_tx_vga_gain(gain_db);
}

void hackrf_ui_setFirstIFFrequency(const uint64_t frequency) {
if( ui != NULL ) ui->set_first_if_frequency(frequency);
}

void hackrf_ui_setFilter(const rf_path_filter_t filter) {
if( ui != NULL ) ui->set_filter(filter);
}

void hackrf_ui_setAntennaBias(bool antenna_bias) {
if( ui != NULL ) ui->set_antenna_bias(antenna_bias);
}

#endif
2 changes: 2 additions & 0 deletions firmware/common/ui_portapack.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,6 @@ typedef struct ui_font_t {
size_t data_stride;
} ui_font_t;

const hackrf_ui_t* portapack_detect(void);

#endif/*__UI_PORTAPACK_H__*/
6 changes: 6 additions & 0 deletions firmware/hackrf-common.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,12 @@ macro(DeclareTargets)
${SRC_M4}
${PATH_HACKRF_FIRMWARE_COMMON}/rffc5071.c
${PATH_HACKRF_FIRMWARE_COMMON}/rffc5071_spi.c
${PATH_HACKRF_FIRMWARE_COMMON}/cpld_jtag.c
${PATH_HACKRF_FIRMWARE_COMMON}/xapp058/lenval.c
${PATH_HACKRF_FIRMWARE_COMMON}/xapp058/micro.c
${PATH_HACKRF_FIRMWARE_COMMON}/xapp058/ports.c
${PATH_HACKRF_FIRMWARE_COMMON}/hackrf-ui.c
${PATH_HACKRF_FIRMWARE_COMMON}/ui_portapack.c
)
endif()

Expand Down
11 changes: 0 additions & 11 deletions firmware/hackrf_usb/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,10 @@ set(SRC_M4
usb_api_sweep.c
"${PATH_HACKRF_FIRMWARE_COMMON}/usb_queue.c"
"${PATH_HACKRF_FIRMWARE_COMMON}/fault_handler.c"
"${PATH_HACKRF_FIRMWARE_COMMON}/cpld_jtag.c"
"${PATH_HACKRF_FIRMWARE_COMMON}/cpld_xc2c.c"
"${PATH_HACKRF_FIRMWARE_COMMON}/crc.c"
"${PATH_HACKRF_FIRMWARE_COMMON}/xapp058/lenval.c"
"${PATH_HACKRF_FIRMWARE_COMMON}/xapp058/micro.c"
"${PATH_HACKRF_FIRMWARE_COMMON}/xapp058/ports.c"
"${PATH_HACKRF_FIRMWARE_COMMON}/rom_iap.c"
"${PATH_HACKRF_FIRMWARE_COMMON}/operacake.c"
)

if(USER_INTERFACE STREQUAL "PORTAPACK")
SET(SRC_M4
${SRC_M4}
"${PATH_HACKRF_FIRMWARE_COMMON}/ui_portapack.c"
)
endif()

DeclareTargets()
2 changes: 1 addition & 1 deletion firmware/hackrf_usb/hackrf_usb.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ int main(void) {

nvic_set_priority(NVIC_USB0_IRQ, 255);

hackrf_ui_init();
hackrf_ui()->init();

usb_run(&usb_device);

Expand Down
6 changes: 3 additions & 3 deletions firmware/hackrf_usb/usb_api_transceiver.c
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ usb_request_status_t usb_vendor_request_set_lna_gain(
if( stage == USB_TRANSFER_STAGE_SETUP ) {
const uint8_t value = max2837_set_lna_gain(&max2837, endpoint->setup.index);
endpoint->buffer[0] = value;
if(value) hackrf_ui_setBBLNAGain(endpoint->setup.index);
if(value) hackrf_ui()->set_bb_lna_gain(endpoint->setup.index);
usb_transfer_schedule_block(endpoint->in, &endpoint->buffer, 1,
NULL, NULL);
usb_transfer_schedule_ack(endpoint->out);
Expand All @@ -168,7 +168,7 @@ usb_request_status_t usb_vendor_request_set_vga_gain(
if( stage == USB_TRANSFER_STAGE_SETUP ) {
const uint8_t value = max2837_set_vga_gain(&max2837, endpoint->setup.index);
endpoint->buffer[0] = value;
if(value) hackrf_ui_setBBVGAGain(endpoint->setup.index);
if(value) hackrf_ui()->set_bb_vga_gain(endpoint->setup.index);
usb_transfer_schedule_block(endpoint->in, &endpoint->buffer, 1,
NULL, NULL);
usb_transfer_schedule_ack(endpoint->out);
Expand All @@ -183,7 +183,7 @@ usb_request_status_t usb_vendor_request_set_txvga_gain(
if( stage == USB_TRANSFER_STAGE_SETUP ) {
const uint8_t value = max2837_set_txvga_gain(&max2837, endpoint->setup.index);
endpoint->buffer[0] = value;
if(value) hackrf_ui_setBBTXVGAGain(endpoint->setup.index);
if(value) hackrf_ui()->set_bb_tx_vga_gain(endpoint->setup.index);
usb_transfer_schedule_block(endpoint->in, &endpoint->buffer, 1,
NULL, NULL);
usb_transfer_schedule_ack(endpoint->out);
Expand Down

0 comments on commit c32d571

Please sign in to comment.