2020-09-17 09:53:52 +02:00
# pragma once
2021-05-01 18:34:53 +02:00
# include <Cal.hpp>
2020-09-17 09:53:52 +02:00
# include <cstdint>
# include "Protocol.hpp"
2020-11-07 00:50:59 +01:00
# include "FPGA/FPGA.hpp"
2020-11-18 19:19:29 +01:00
# include "max2871.hpp"
# include "Si5351C.hpp"
2020-09-17 09:53:52 +02:00
2021-07-19 00:31:28 +02:00
# define USE_DEBUG_PINS 0
2020-10-03 21:56:09 +02:00
2021-07-19 00:31:28 +02:00
# if USE_DEBUG_PINS
2020-10-03 21:56:09 +02:00
# define DEBUG1_GPIO GPIOA
# define DEBUG1_PIN GPIO_PIN_13
# define DEBUG2_GPIO GPIOA
# define DEBUG2_PIN GPIO_PIN_14
# define DEBUG1_LOW() do {DEBUG1_GPIO->BSRR = DEBUG1_PIN<<16; }while(0)
# define DEBUG1_HIGH() do {DEBUG1_GPIO->BSRR = DEBUG1_PIN; }while(0)
# define DEBUG2_LOW() do {DEBUG2_GPIO->BSRR = DEBUG2_PIN<<16; }while(0)
# define DEBUG2_HIGH() do {DEBUG2_GPIO->BSRR = DEBUG2_PIN; }while(0)
# else
# define DEBUG1_LOW()
# define DEBUG1_HIGH()
# define DEBUG2_LOW()
# define DEBUG2_HIGH()
# endif
2020-09-17 09:53:52 +02:00
namespace HW {
2021-06-02 18:47:03 +02:00
static constexpr uint32_t TCXOFrequency = 26000000 ;
static constexpr uint32_t ExtRefInFrequency = 10000000 ;
static constexpr uint32_t ExtRefOut1Frequency = 10000000 ;
static constexpr uint32_t ExtRefOut2Frequency = 10000000 ;
static constexpr uint32_t SI5351CPLLAlignedFrequency = 832000000 ;
static constexpr uint32_t SI5351CPLLConstantFrequency = 800000000 ;
static constexpr uint32_t FPGAClkInFrequency = 16000000 ;
2022-01-15 16:11:33 +01:00
static constexpr uint32_t DefaultADCSamplerate = 800000 ;
static constexpr uint32_t DefaultIF1 = 62000000 ;
static constexpr uint32_t DefaultIF2 = 250000 ;
2020-09-20 10:13:06 +02:00
static constexpr uint32_t LO1_minFreq = 25000000 ;
static constexpr uint32_t MaxSamples = 130944 ;
2020-09-26 23:34:31 +02:00
static constexpr uint32_t MinSamples = 16 ;
2021-03-22 21:28:03 +01:00
static constexpr uint32_t PLLRef = 104000000 ;
2020-11-18 19:19:29 +01:00
static constexpr uint32_t BandSwitchFrequency = 25000000 ;
2020-09-26 23:34:31 +02:00
2022-01-15 16:11:33 +01:00
static constexpr uint8_t DefaultADCprescaler = FPGA : : Clockrate / DefaultADCSamplerate ;
static_assert ( DefaultADCprescaler * DefaultADCSamplerate = = FPGA : : Clockrate , " ADCSamplerate can not be reached exactly " ) ;
static constexpr uint16_t DefaultDFTphaseInc = 4096 * DefaultIF2 / DefaultADCSamplerate ;
static_assert ( DefaultDFTphaseInc * DefaultADCSamplerate = = 4096 * DefaultIF2 , " DFT can not be computed for 2.IF " ) ;
2020-10-30 19:23:34 +01:00
2021-06-02 18:47:03 +02:00
static constexpr uint16_t _fpga_div = SI5351CPLLConstantFrequency / FPGAClkInFrequency ;
static_assert ( _fpga_div * FPGAClkInFrequency = = SI5351CPLLConstantFrequency & & _fpga_div > = 6 & & _fpga_div < = 254 & & ( _fpga_div & 0x01 ) = = 0 , " Unable to generate FPGA clock input frequency " ) ;
static constexpr uint16_t _ref_out1_div = SI5351CPLLConstantFrequency / ExtRefOut1Frequency ;
static_assert ( _ref_out1_div * ExtRefOut1Frequency = = SI5351CPLLConstantFrequency & & _ref_out1_div > = 6 & & _ref_out1_div < = 254 & & ( _ref_out1_div & 0x01 ) = = 0 , " Unable to generate first reference output frequency " ) ;
static constexpr uint16_t _ref_out2_div = SI5351CPLLConstantFrequency / ExtRefOut2Frequency ;
static_assert ( _ref_out2_div * ExtRefOut2Frequency = = SI5351CPLLConstantFrequency & & _ref_out2_div > = 6 & & _ref_out2_div < = 254 & & ( _ref_out2_div & 0x01 ) = = 0 , " Unable to generate first reference output frequency " ) ;
2020-11-17 23:03:13 +01:00
// approximate output power at low frequencies with different source strength settings (attenuator = 0) in cdbm
static constexpr int16_t LowBandMinPower = - 1350 ;
static constexpr int16_t LowBandMaxPower = - 190 ;
static constexpr int16_t HighBandMinPower = - 1060 ;
static constexpr int16_t HighBandMaxPower = - 160 ;
2020-11-07 13:22:10 +01:00
static constexpr Protocol : : DeviceInfo Info = {
. ProtocolVersion = Protocol : : Version ,
. FW_major = FW_MAJOR ,
. FW_minor = FW_MINOR ,
. FW_patch = FW_PATCH ,
2022-04-03 20:26:30 +02:00
. hardware_version = 1 ,
2020-11-07 13:22:10 +01:00
. HW_Revision = HW_REVISION ,
. limits_minFreq = 0 ,
. limits_maxFreq = 6000000000 ,
2022-01-15 16:11:33 +01:00
. limits_minIFBW = DefaultADCSamplerate / MaxSamples ,
. limits_maxIFBW = DefaultADCSamplerate / MinSamples ,
2020-11-07 13:22:10 +01:00
. limits_maxPoints = FPGA : : MaxPoints ,
. limits_cdbm_min = - 4000 ,
. limits_cdbm_max = 0 ,
2022-01-15 16:11:33 +01:00
. limits_minRBW = ( uint32_t ) ( DefaultADCSamplerate * 2.23f / MaxSamples ) ,
. limits_maxRBW = ( uint32_t ) ( DefaultADCSamplerate * 2.23f / MinSamples ) ,
2021-05-01 18:34:53 +02:00
. limits_maxAmplitudePoints = Cal : : maxPoints ,
2020-12-18 15:03:01 +01:00
. limits_maxFreqHarmonic = 18000000000 ,
2020-09-26 23:34:31 +02:00
} ;
2020-09-17 15:51:20 +02:00
2020-09-17 09:53:52 +02:00
enum class Mode {
Idle ,
Manual ,
VNA ,
SA ,
} ;
2020-09-27 11:49:30 +02:00
bool Init ( ) ;
2020-09-17 09:53:52 +02:00
void SetMode ( Mode mode ) ;
void SetIdle ( ) ;
2020-09-17 15:51:20 +02:00
void Work ( ) ;
2020-12-15 18:03:29 +01:00
bool TimedOut ( ) ;
2022-06-20 01:02:09 +02:00
uint64_t getLastISRTimestamp ( ) ;
2020-09-17 09:53:52 +02:00
2021-02-11 22:49:47 +01:00
void SetOutputUnlevel ( bool unlev ) ;
2020-11-18 19:19:29 +01:00
using AmplitudeSettings = struct _amplitudeSettings {
uint8_t attenuator ;
union {
MAX2871 : : Power highBandPower ;
Si5351C : : DriveStrength lowBandPower ;
} ;
bool unlevel ;
} ;
AmplitudeSettings GetAmplitudeSettings ( int16_t cdbm , uint64_t freq = 0 , bool applyCorrections = false , bool port2 = false ) ;
2020-09-17 09:53:52 +02:00
bool GetTemps ( uint8_t * source , uint8_t * lo ) ;
2022-04-03 20:26:30 +02:00
void getDeviceStatus ( Protocol : : DeviceStatusV1 * status , bool updateEvenWhenBusy = false ) ;
2020-09-17 09:53:52 +02:00
namespace Ref {
bool available ( ) ;
2021-05-01 18:34:53 +02:00
bool usingExternal ( ) ;
2020-09-17 09:53:52 +02:00
// reference won't change until update is called
void set ( Protocol : : ReferenceSettings s ) ;
void update ( ) ;
}
2022-01-15 16:11:33 +01:00
// Acquisition frequency settings
void setAcquisitionFrequencies ( Protocol : : AcquisitionFrequencySettings s ) ;
uint32_t getIF1 ( ) ;
uint32_t getIF2 ( ) ;
uint32_t getADCRate ( ) ;
uint8_t getADCPrescaler ( ) ;
uint16_t getDFTPhaseInc ( ) ;
2020-09-17 09:53:52 +02:00
}