2015-07-08 11:39:24 -04:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Jared Boone, ShareBrained Technology, Inc.
|
|
|
|
*
|
|
|
|
* This file is part of PortaPack.
|
|
|
|
*
|
|
|
|
* 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 "radio.hpp"
|
|
|
|
|
|
|
|
#include "rf_path.hpp"
|
2016-02-16 13:28:13 -05:00
|
|
|
|
|
|
|
#include "rffc507x.hpp"
|
|
|
|
#include "max2837.hpp"
|
2023-02-16 07:09:23 -05:00
|
|
|
#include "max2839.hpp"
|
2015-07-08 11:39:24 -04:00
|
|
|
#include "max5864.hpp"
|
|
|
|
#include "baseband_cpld.hpp"
|
|
|
|
|
|
|
|
#include "tuning.hpp"
|
|
|
|
|
|
|
|
#include "spi_arbiter.hpp"
|
|
|
|
|
|
|
|
#include "hackrf_hal.hpp"
|
|
|
|
#include "hackrf_gpio.hpp"
|
|
|
|
using namespace hackrf::one;
|
|
|
|
|
2017-11-09 17:25:37 -05:00
|
|
|
#include "cpld_update.hpp"
|
|
|
|
|
2015-08-01 16:42:27 -04:00
|
|
|
#include "portapack.hpp"
|
2023-03-22 03:46:58 -04:00
|
|
|
#include "portapack_persistent_memory.hpp"
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
namespace radio {
|
|
|
|
|
|
|
|
static constexpr uint32_t ssp1_cpsr = 2;
|
|
|
|
|
|
|
|
static constexpr uint32_t ssp_scr(
|
|
|
|
const float pclk_f,
|
|
|
|
const uint32_t cpsr,
|
|
|
|
const float spi_f
|
|
|
|
) {
|
|
|
|
return static_cast<uint8_t>(pclk_f / cpsr / spi_f - 1);
|
|
|
|
}
|
|
|
|
|
2023-02-16 07:09:23 -05:00
|
|
|
static constexpr SPIConfig ssp_config_max283x = {
|
2015-07-08 11:39:24 -04:00
|
|
|
.end_cb = NULL,
|
2023-02-16 07:09:23 -05:00
|
|
|
.ssport = gpio_max283x_select.port(),
|
|
|
|
.sspad = gpio_max283x_select.pad(),
|
2015-07-08 11:39:24 -04:00
|
|
|
.cr0 =
|
2023-02-16 07:09:23 -05:00
|
|
|
CR0_CLOCKRATE(ssp_scr(ssp1_pclk_f, ssp1_cpsr, max283x_spi_f))
|
2015-07-08 11:39:24 -04:00
|
|
|
| CR0_FRFSPI
|
|
|
|
| CR0_DSS16BIT
|
|
|
|
,
|
|
|
|
.cpsr = ssp1_cpsr,
|
|
|
|
};
|
|
|
|
|
|
|
|
static constexpr SPIConfig ssp_config_max5864 = {
|
|
|
|
.end_cb = NULL,
|
|
|
|
.ssport = gpio_max5864_select.port(),
|
|
|
|
.sspad = gpio_max5864_select.pad(),
|
|
|
|
.cr0 =
|
|
|
|
CR0_CLOCKRATE(ssp_scr(ssp1_pclk_f, ssp1_cpsr, max5864_spi_f))
|
|
|
|
| CR0_FRFSPI
|
|
|
|
| CR0_DSS8BIT
|
|
|
|
,
|
|
|
|
.cpsr = ssp1_cpsr,
|
|
|
|
};
|
|
|
|
|
2015-08-01 16:42:27 -04:00
|
|
|
static spi::arbiter::Arbiter ssp1_arbiter(portapack::ssp1);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
2023-02-16 07:09:23 -05:00
|
|
|
static spi::arbiter::Target ssp1_target_max283x {
|
2015-07-08 11:39:24 -04:00
|
|
|
ssp1_arbiter,
|
2023-02-16 07:09:23 -05:00
|
|
|
ssp_config_max283x
|
2015-07-08 11:39:24 -04:00
|
|
|
};
|
|
|
|
|
|
|
|
static spi::arbiter::Target ssp1_target_max5864 {
|
|
|
|
ssp1_arbiter,
|
|
|
|
ssp_config_max5864
|
|
|
|
};
|
|
|
|
|
|
|
|
static rf::path::Path rf_path;
|
|
|
|
rffc507x::RFFC507x first_if;
|
2023-02-16 07:09:23 -05:00
|
|
|
max283x::MAX283x *second_if;
|
|
|
|
max2837::MAX2837 second_if_max2837 { ssp1_target_max283x };
|
|
|
|
max2839::MAX2839 second_if_max2839 { ssp1_target_max283x };
|
2015-07-08 11:39:24 -04:00
|
|
|
static max5864::MAX5864 baseband_codec { ssp1_target_max5864 };
|
|
|
|
static baseband::CPLD baseband_cpld;
|
|
|
|
|
|
|
|
static rf::Direction direction { rf::Direction::Receive };
|
2023-02-16 07:09:23 -05:00
|
|
|
static bool baseband_invert = false;
|
|
|
|
static bool mixer_invert = false;
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
void init() {
|
2023-02-16 07:09:23 -05:00
|
|
|
if (hackrf_r9) {
|
|
|
|
gpio_r9_not_ant_pwr.write(1);
|
|
|
|
gpio_r9_not_ant_pwr.output();
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
rf_path.init();
|
|
|
|
first_if.init();
|
2023-02-16 07:09:23 -05:00
|
|
|
second_if = hackrf_r9
|
|
|
|
? (max283x::MAX283x *) &second_if_max2839
|
|
|
|
: (max283x::MAX283x *) &second_if_max2837;
|
|
|
|
second_if->init();
|
2015-07-08 11:39:24 -04:00
|
|
|
baseband_codec.init();
|
|
|
|
baseband_cpld.init();
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_direction(const rf::Direction new_direction) {
|
|
|
|
/* TODO: Refactor all the various "Direction" enumerations into one. */
|
|
|
|
/* TODO: Only make changes if direction changes, but beware of clock enabling. */
|
2017-11-09 17:25:37 -05:00
|
|
|
|
2018-01-07 18:13:08 -05:00
|
|
|
// Hack to fix the CPLD (clocking ?) bug: toggle CPLD SRAM overlay depending on new direction
|
|
|
|
// Use CPLD's EEPROM config when transmitting
|
|
|
|
// Use the SRAM overlay when receiving
|
2021-05-22 15:57:48 -04:00
|
|
|
|
|
|
|
// teixeluis: undone "Hack to fix the CPLD (clocking ?) bug".
|
|
|
|
// Apparently with current CPLD code from the hackrf repo,
|
|
|
|
// toggling CPLD overlay should no longer be necessary:
|
|
|
|
if (direction != new_direction && new_direction == rf::Direction::Transmit) {
|
|
|
|
hackrf::cpld::init_from_eeprom();
|
2017-11-09 17:25:37 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
direction = new_direction;
|
2016-07-26 21:03:40 -04:00
|
|
|
|
2023-02-16 07:09:23 -05:00
|
|
|
if (hackrf_r9) {
|
|
|
|
/*
|
|
|
|
* HackRF One r9 inverts analog baseband only for RX. Previous hardware
|
|
|
|
* revisions inverted analog baseband for neither direction because of
|
|
|
|
* compensation in the CPLD. If we ever simplify the CPLD to handle RX
|
|
|
|
* and TX the same way, we will need to update this baseband_invert
|
|
|
|
* logic.
|
|
|
|
*/
|
|
|
|
baseband_invert = (direction == rf::Direction::Receive);
|
|
|
|
} else {
|
|
|
|
/*
|
|
|
|
* Analog baseband is inverted in RX but not TX. The RX inversion is
|
|
|
|
* corrected by the CPLD, but future hardware or CPLD changes may
|
|
|
|
* change this for either or both directions. For a given hardware+CPLD
|
|
|
|
* platform, baseband inversion is set here for RX and/or TX. Spectrum
|
|
|
|
* inversion resulting from the mixer is tracked separately according
|
|
|
|
* to the tuning configuration. We ask the CPLD to apply a correction
|
|
|
|
* for the total inversion.
|
|
|
|
*/
|
|
|
|
baseband_invert = false;
|
|
|
|
}
|
|
|
|
baseband_cpld.set_invert(mixer_invert ^ baseband_invert);
|
|
|
|
|
|
|
|
second_if->set_mode((direction == rf::Direction::Transmit) ? max283x::Mode::Transmit : max283x::Mode::Receive);
|
2015-07-08 11:39:24 -04:00
|
|
|
rf_path.set_direction(direction);
|
|
|
|
|
|
|
|
baseband_codec.set_mode((direction == rf::Direction::Transmit) ? max5864::Mode::Transmit : max5864::Mode::Receive);
|
2017-01-16 08:36:28 -05:00
|
|
|
|
|
|
|
if (direction == rf::Direction::Receive)
|
|
|
|
led_rx.on();
|
|
|
|
else
|
|
|
|
led_tx.on();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool set_tuning_frequency(const rf::Frequency frequency) {
|
2023-03-22 03:46:58 -04:00
|
|
|
rf::Frequency final_frequency = frequency ;
|
2023-03-27 08:22:07 -04:00
|
|
|
// if feature is enabled
|
|
|
|
if( portapack::persistent_memory::config_converter() ) {
|
|
|
|
//downconvert
|
|
|
|
if( portapack::persistent_memory::config_updown_converter() )
|
|
|
|
{
|
|
|
|
final_frequency = frequency - portapack::persistent_memory::config_converter_freq();
|
|
|
|
}
|
|
|
|
else //upconvert
|
|
|
|
{
|
|
|
|
final_frequency = frequency + portapack::persistent_memory::config_converter_freq();
|
|
|
|
}
|
2023-03-22 03:46:58 -04:00
|
|
|
}
|
|
|
|
const auto tuning_config = tuning::config::create(final_frequency);
|
2015-07-08 11:39:24 -04:00
|
|
|
if( tuning_config.is_valid() ) {
|
|
|
|
first_if.disable();
|
|
|
|
|
|
|
|
if( tuning_config.first_lo_frequency ) {
|
|
|
|
first_if.set_frequency(tuning_config.first_lo_frequency);
|
|
|
|
first_if.enable();
|
|
|
|
}
|
|
|
|
|
2023-02-16 07:09:23 -05:00
|
|
|
const auto result_second_if = second_if->set_frequency(tuning_config.second_lo_frequency);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
rf_path.set_band(tuning_config.rf_path_band);
|
2023-02-16 07:09:23 -05:00
|
|
|
mixer_invert = tuning_config.mixer_invert;
|
|
|
|
baseband_cpld.set_invert(mixer_invert ^ baseband_invert);
|
2015-07-08 11:39:24 -04:00
|
|
|
|
|
|
|
return result_second_if;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void set_rf_amp(const bool rf_amp) {
|
|
|
|
rf_path.set_rf_amp(rf_amp);
|
2017-03-13 00:09:21 -04:00
|
|
|
|
|
|
|
if (direction == rf::Direction::Transmit) {
|
|
|
|
if (rf_amp)
|
|
|
|
led_tx.on();
|
|
|
|
else
|
|
|
|
led_tx.off();
|
|
|
|
}
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_lna_gain(const int_fast8_t db) {
|
2023-02-16 07:09:23 -05:00
|
|
|
second_if->set_lna_gain(db);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
void set_vga_gain(const int_fast8_t db) {
|
2023-02-16 07:09:23 -05:00
|
|
|
second_if->set_vga_gain(db);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2016-08-03 19:12:22 -04:00
|
|
|
void set_tx_gain(const int_fast8_t db) {
|
2023-02-16 07:09:23 -05:00
|
|
|
second_if->set_tx_vga_gain(db);
|
2016-08-03 19:12:22 -04:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void set_baseband_filter_bandwidth(const uint32_t bandwidth_minimum) {
|
2023-02-16 07:09:23 -05:00
|
|
|
second_if->set_lpf_rf_bandwidth(bandwidth_minimum);
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2016-02-05 19:36:36 -05:00
|
|
|
void set_baseband_rate(const uint32_t rate) {
|
|
|
|
portapack::clock_manager.set_sampling_frequency(rate);
|
|
|
|
}
|
|
|
|
|
2016-01-23 02:24:20 -05:00
|
|
|
void set_antenna_bias(const bool on) {
|
|
|
|
/* Pull MOSFET gate low to turn on antenna bias. */
|
2023-02-16 07:09:23 -05:00
|
|
|
if (hackrf_r9) {
|
|
|
|
gpio_r9_not_ant_pwr.write(on ? 0 : 1);
|
|
|
|
} else {
|
|
|
|
first_if.set_gpo1(on ? 0 : 1);
|
|
|
|
}
|
2016-01-23 02:24:20 -05:00
|
|
|
}
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
void disable() {
|
2016-01-23 02:24:20 -05:00
|
|
|
set_antenna_bias(false);
|
2015-07-08 11:39:24 -04:00
|
|
|
baseband_codec.set_mode(max5864::Mode::Shutdown);
|
2023-02-16 07:09:23 -05:00
|
|
|
second_if->set_mode(max2837::Mode::Standby);
|
2015-07-08 11:39:24 -04:00
|
|
|
first_if.disable();
|
|
|
|
set_rf_amp(false);
|
2017-01-16 08:36:28 -05:00
|
|
|
|
|
|
|
led_rx.off();
|
|
|
|
led_tx.off();
|
2015-07-08 11:39:24 -04:00
|
|
|
}
|
|
|
|
|
2016-02-10 19:58:25 -05:00
|
|
|
void enable(Configuration configuration) {
|
|
|
|
configure(configuration);
|
|
|
|
}
|
|
|
|
|
|
|
|
void configure(Configuration configuration) {
|
|
|
|
set_tuning_frequency(configuration.tuning_frequency);
|
|
|
|
set_rf_amp(configuration.rf_amp);
|
|
|
|
set_lna_gain(configuration.lna_gain);
|
|
|
|
set_vga_gain(configuration.vga_gain);
|
|
|
|
set_baseband_rate(configuration.baseband_rate);
|
|
|
|
set_baseband_filter_bandwidth(configuration.baseband_filter_bandwidth);
|
|
|
|
set_direction(configuration.direction);
|
|
|
|
}
|
|
|
|
|
2016-02-16 13:28:13 -05:00
|
|
|
namespace debug {
|
|
|
|
|
|
|
|
namespace first_if {
|
|
|
|
|
|
|
|
uint32_t register_read(const size_t register_number) {
|
|
|
|
return radio::first_if.read(register_number);
|
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace first_if */
|
|
|
|
|
|
|
|
namespace second_if {
|
|
|
|
|
|
|
|
uint32_t register_read(const size_t register_number) {
|
2023-02-16 07:09:23 -05:00
|
|
|
return radio::second_if->read(register_number);
|
2016-02-16 13:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t temp_sense() {
|
2023-02-16 07:09:23 -05:00
|
|
|
return radio::second_if->temp_sense() & 0x1f;
|
2016-02-16 13:28:13 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} /* namespace second_if */
|
|
|
|
|
|
|
|
} /* namespace debug */
|
|
|
|
|
2015-07-08 11:39:24 -04:00
|
|
|
} /* namespace radio */
|