mirror of
https://github.com/portapack-mayhem/mayhem-firmware.git
synced 2026-08-16 04:43:28 +00:00
Legacy Initialization, Analog Audio App, and Aliasing fixes. (#3052)
* Added PRO button to analog_audio.cpp for debugging metallic ringing sounds in Audio app. Ensured consitency to 4MHz in analog_audio.cpp * Restored blutooth after loss. * Added several sampling rates to analog_audio for testing. * Ran format-code.sh * Fixed 0x03 mode for DC/Q-INV/Q-SHFT being set at every tuning and sample rate change. This was the root cause for loss of bluetooth before. Sample rate, and frequency can now be changed without need for manually resetting DC/Q-INV/Q-SHIFT settings. Updated method for setting frequncies in praline so that we have more testing options. * Ran format-code.sh and cleaned up stale comments. * Addressed comments, and removed commented line, opting for higher register values, 5E and 5D. Added WFM Debug View to support testing demodulation ringing. * Addressed comments in PR conversation to clean comments and ensure consistency at initializtion accross updated methods and displays. * Ran format-code.sh * Set initial legacy state. Improved readability of clocking initialization settings. Updated set_sampling_frequency, and udpate_bandwidth to set decimation values in the fpga_registers to avoid aliasing in low band frequencies. * Updated clock_manager to use correct clock and clock parameters for audio pll. Ran format-code.sh.
This commit is contained in:
@@ -109,6 +109,15 @@ constexpr si5351::MultisynthFractional si5351_ms_0_20m {
|
||||
};
|
||||
constexpr auto si5351_ms_0_20m_reg = si5351_ms_0_20m.reg(0);
|
||||
*/
|
||||
|
||||
constexpr si5351::MultisynthFractional si5351_ms_0_4m{
|
||||
.f_src = si5351_vco_f, // 800,000,000 Hz
|
||||
.a = 100, // Integer divider 100
|
||||
.b = 0,
|
||||
.c = 1,
|
||||
.r_div = 1 // Final R-divider: 2^1 = 2
|
||||
};
|
||||
|
||||
constexpr si5351::MultisynthFractional si5351_ms_0_8m{
|
||||
.f_src = si5351_vco_f,
|
||||
.a = 50,
|
||||
@@ -398,12 +407,35 @@ void ClockManager::init_clock_generator() {
|
||||
#endif
|
||||
|
||||
#ifdef PRALINE
|
||||
/* PRALINE uses Si5351A with:
|
||||
* CLK0 = AFE_CLK (codec/FPGA sample clock)
|
||||
* CLK1 = SCT_CLK (FPGA timing clock at 2x sample rate)
|
||||
* CLK4 = second IF (MAX2831)
|
||||
* CLK5 = first IF (RFFC5072)
|
||||
* Uses PLLA on XTAL only (no CLKIN support).
|
||||
|
||||
/* * Praline HackRF Pro Clock Assignments (800 MHz VCO Configuration)
|
||||
* VCO Frequency: 800,000,000 Hz (Master Reference)
|
||||
* * CLK0: AFE_CLK (MAX5864 Codec & FPGA ADC Interface)
|
||||
* - Initialized to: 4,000,000 Hz (si5351_ms_4m)
|
||||
* - Divider: 100 (Integer), R_DIV: 2 (r_div=1)
|
||||
* - Note: Defines hardware sample rate. Essential for WFM purity.
|
||||
* * CLK1: SCT_CLK (iCE40 FPGA System/Timing Clock)
|
||||
* - Initialized to: 10,000,000 Hz (si5351_ms_10m)
|
||||
* - Divider: 80 (Integer), R_DIV: 1 (r_div=0)
|
||||
* - Note: Timing for SGPIO data bus; scales to 2x SR in wideband modes.
|
||||
* * CLK2: MCU_CLKIN (LPC43xx MCU External Clock Input)
|
||||
* - Initialized to: 10,000,000 Hz (si5351_ms_10m)
|
||||
* - Divider: 80 (Integer), R_DIV: 1 (r_div=0)
|
||||
* - Note: Synchronizes MCU processing to the RF clock tree.
|
||||
* * CLK3: SG_CLK (Switching Regulator/Internal Logic Sync)
|
||||
* - Initialized to: 10,000,000 Hz (si5351_ms_10m)
|
||||
* - Divider: 80 (Integer), R_DIV: 1 (r_div=0)
|
||||
* - Note: Used for internal FPGA logic/gateware synchronization.
|
||||
* * CLK4: P_CLK (Peripheral/Expansion Clock)
|
||||
* - Configured via: si5351c_ms_4_reg
|
||||
* - Note: Routed to expansion headers for external hardware sync.
|
||||
* * CLK5: AUX_CLK (Auxiliary reference for secondary logic)
|
||||
* - Configured via: si5351c_ms_5_reg
|
||||
* - Note: Provides additional timing flexibility for the iCE40 FPGA.
|
||||
* * CLK6/7: Unused / Power-Down
|
||||
* - State: Disabled (si5351a_ms6_7_off_reg)
|
||||
* - Note: Kept OFF to reduce EMI/RFI near the RF front-end.
|
||||
* * CLKOUT: Optional external clock output on the header.
|
||||
*/
|
||||
|
||||
/* Step 1: Write PLL A configuration (800 MHz VCO from 25 MHz XTAL) */
|
||||
@@ -417,25 +449,13 @@ void ClockManager::init_clock_generator() {
|
||||
}
|
||||
|
||||
/* Step 2: Write multisynth configurations using single-byte writes */
|
||||
clock_generator.write_ms_single_byte(0, si5351_ms_0_8m); // MS0 = divider 50, r_div=1 for 8 MHz
|
||||
clock_generator.write_ms_single_byte(1, si5351_ms_16m); // MS1 = divider 50, r_div=0 for 16 MHz
|
||||
|
||||
/* CLK4 and CLK5 - use single-byte writes too */
|
||||
{
|
||||
const auto& ms4_regs = si5351c_ms_4_reg;
|
||||
const uint8_t base_reg = ms4_regs[0];
|
||||
for (size_t i = 1; i < ms4_regs.size(); i++) {
|
||||
clock_generator.write_register(base_reg + i - 1, ms4_regs[i]);
|
||||
}
|
||||
}
|
||||
{
|
||||
const auto& ms5_regs = si5351c_ms_5_reg;
|
||||
const uint8_t base_reg = ms5_regs[0];
|
||||
for (size_t i = 1; i < ms5_regs.size(); i++) {
|
||||
clock_generator.write_register(base_reg + i - 1, ms5_regs[i]);
|
||||
}
|
||||
}
|
||||
clock_generator.write(si5351a_ms6_7_off_reg); // MS6/7 off - short write is OK
|
||||
// These cover all active channels on the Praline board
|
||||
clock_generator.write_ms_single_byte(0, si5351_ms_0_4m); // CLK0: Codec (4 MHz)
|
||||
clock_generator.write_ms_single_byte(1, si5351_ms_10m); // CLK1: FPGA Timing (10 MHz)
|
||||
clock_generator.write_ms_single_byte(2, si5351_ms_10m); // CLK2: MCU Input (10 MHz)
|
||||
clock_generator.write_ms_single_byte(3, si5351_ms_10m); // CLK3: Logic Sync (10 MHz)
|
||||
clock_generator.write_ms_single_byte(4, si5351_ms_40m); // CLK4: First IF (40 MHz)
|
||||
clock_generator.write_ms_single_byte(5, si5351_ms_40m); // CLK5: Second IF (40 MHz)
|
||||
|
||||
/* Step 3: NOW set clock control registers (AFTER multisynths per HackRF reference) */
|
||||
const auto ref_pll = ClockControl::MultiSynthSource::PLLA;
|
||||
@@ -489,21 +509,6 @@ void ClockManager::init_clock_generator() {
|
||||
|
||||
// CRITICAL: Add delay to ensure Si5351 writes complete before I2C bus stops
|
||||
chThdSleepMilliseconds(100);
|
||||
|
||||
// ===== ADD THIS SAFETY BLOCK =====
|
||||
// Pre-configure CLK4/CLK5 with correct settings including inversion.
|
||||
// This ensures the registers have correct values even if enable_if_clocks()
|
||||
// is not called or fails. The clocks will still be powered off until
|
||||
// enable_if_clocks() enables the outputs.
|
||||
|
||||
// CLK5: OFF (for now), Integer, PLLA, INVERTED, MS_Self, 6mA = 0xDE
|
||||
// (Same as 0x5E but with bit 7 set for power off)
|
||||
clock_generator.write_register(21, 0xDE);
|
||||
|
||||
// CLK4: OFF (for now), Integer, PLLA, INVERTED, MS_Self, 4mA = 0xDD
|
||||
clock_generator.write_register(20, 0xDD);
|
||||
// ===== END SAFETY BLOCK =====
|
||||
|
||||
#else
|
||||
// Wait for PLL(s) to lock - with timeout to prevent hang
|
||||
uint8_t device_status_mask = hackrf_r9
|
||||
@@ -642,27 +647,6 @@ void ClockManager::disable_codec_clocks() {
|
||||
|
||||
void ClockManager::enable_if_clocks() {
|
||||
#ifdef PRALINE
|
||||
/* PRALINE: CLK4=MAX2831, CLK5=RFFC5072
|
||||
*
|
||||
* Force-write complete configuration to guarantee correct setup.
|
||||
* Added force-write to verify correct registers are applying necessary register clock settings.
|
||||
*/
|
||||
|
||||
// Configure and enable CLK4 (MAX2831)
|
||||
// Register 20: ON, Integer, PLLA, INVERTED, MS_Self, 4mA = 0x5D
|
||||
clock_generator.write_register(20, 0x5D);
|
||||
|
||||
// Configure and enable CLK5 (RFFC5072) - CRITICAL!
|
||||
// Register 21: ON, Integer, PLLA, INVERTED, MS_Self, 6mA = 0x5E
|
||||
clock_generator.write_register(21, 0x5E);
|
||||
|
||||
// Enable outputs (register 3, bits 4 and 5 = 0)
|
||||
uint8_t reg3 = clock_generator.read_register(3);
|
||||
reg3 &= ~0x30;
|
||||
clock_generator.write_register(3, reg3);
|
||||
|
||||
chThdSleepMilliseconds(10);
|
||||
|
||||
/* PRALINE uses CLK5 (first IF) and CLK4 (second IF) */
|
||||
clock_generator.enable_clock(clock_generator_output_og_first_if);
|
||||
clock_generator.enable_output_mask(1U << clock_generator_output_og_first_if);
|
||||
@@ -710,15 +694,45 @@ void ClockManager::set_sampling_frequency(const uint32_t frequency) {
|
||||
/* PRALINE: Match HackRF USB sample_rate_frac_set()
|
||||
* Reference: hackrf_usb radio.c lines 29-91, hackrf_core.c lines 501-685 */
|
||||
|
||||
_base_band_frequency = frequency;
|
||||
_base_band_frequency = frequency; // Store frequency for StatusViews
|
||||
|
||||
// Set FPGA decimation to 0 (no decimation) for direct passthrough
|
||||
fpga_debug_register_write(2, 0x00);
|
||||
/*
|
||||
* PRALINE sample rate strategy from GSG hackrf_usb radio.c:
|
||||
*
|
||||
* 1. Run ADC at the highest rate possible (up to 40 MHz)
|
||||
* 2. Use FPGA decimation to achieve desired output rate
|
||||
* 3. This makes the analog LPF effective at rejecting aliases
|
||||
* 4. Re-apply frequency after to reconfigure LPF bandwidth
|
||||
*/
|
||||
|
||||
// 20 MHz, since GSG reference of 40MHz caused shifts at certain values.
|
||||
constexpr uint32_t MAX_AFE_RATE = 20000000;
|
||||
constexpr uint8_t MAX_N = 5; // Max decimation = 2^5 = 32
|
||||
|
||||
// Calculate optimal decimation factor for RX
|
||||
// Start with n=1 (minimum decimation of 2) per reference
|
||||
uint8_t n = 1;
|
||||
uint32_t afe_rate_x2 = 2 * frequency;
|
||||
|
||||
while ((afe_rate_x2 <= MAX_AFE_RATE) && (n < MAX_N)) {
|
||||
afe_rate_x2 <<= 1;
|
||||
n++;
|
||||
}
|
||||
|
||||
// Store decimation factor for potential use elsewhere
|
||||
_resampling_n = n;
|
||||
|
||||
// The actual AFE rate = frequency * 2^n
|
||||
uint32_t afe_rate = frequency << n;
|
||||
|
||||
// Set FPGA RX decimation register
|
||||
fpga_debug_register_write(2, n);
|
||||
radio::invalidate_spi_config();
|
||||
|
||||
// The following was originally from @kitty. Adopting for testing radio.
|
||||
clock_generator.set_ms_frequency(0, frequency * 2, si5351_vco_f, 1); // CLK0: r_div=1 (÷2)
|
||||
clock_generator.set_ms_frequency(1, frequency * 2, si5351_vco_f, 0); // CLK1: r_div=0 (÷1)
|
||||
// Configure Si5351 clocks
|
||||
clock_generator.set_ms_frequency(0, afe_rate * 2, si5351_vco_f, 1); // CLK0: AFE_CLK
|
||||
clock_generator.set_ms_frequency(1, afe_rate * 2, si5351_vco_f, 0); // CLK1: SCT_CLK
|
||||
|
||||
#else
|
||||
/* Codec clock is at sampling frequency, CPLD and SGPIO clocks are at
|
||||
* twice the frequency, and derived from the MS0 synth. So it's only
|
||||
@@ -797,27 +811,37 @@ void ClockManager::start_audio_pll() {
|
||||
* 12MHz * 1024 / 25 = 491.52MHz
|
||||
* MSEL=1024, NSEL=25, PSEL=20
|
||||
*/
|
||||
cgu::pll0audio::ctrl({
|
||||
.pd = 1,
|
||||
.bypass = 0,
|
||||
.directi = 0,
|
||||
.directo = 0,
|
||||
.clken = 0,
|
||||
.frm = 0,
|
||||
.autoblock = 1,
|
||||
.pllfract_req = 0,
|
||||
.sel_ext = 1,
|
||||
.mod_pd = 1,
|
||||
.clk_sel = cgu::CLK_SEL::XTAL,
|
||||
});
|
||||
|
||||
cgu::pll0audio::mdiv({
|
||||
.mdec = 22625UL, // MDEC for MSEL=1024
|
||||
.mdec = 22625UL, // Encoded value for MSEL=1024
|
||||
});
|
||||
cgu::pll0audio::np_div({
|
||||
.pdec = 31, // PSEL=20
|
||||
.ndec = 69, // NDEC for NSEL=25
|
||||
.pdec = 31, // Encoded value for PSEL=20
|
||||
.ndec = 69, // Encoded value for NSEL=25
|
||||
});
|
||||
|
||||
cgu::pll0audio::frac({
|
||||
.pllfract_ctrl = 0,
|
||||
});
|
||||
|
||||
cgu::pll0audio::power_up();
|
||||
|
||||
// Praline Fix: Wait for lock with a safety timeout
|
||||
{
|
||||
uint32_t timeout = 100000;
|
||||
while (!cgu::pll0audio::is_locked() && timeout > 0) {
|
||||
timeout--;
|
||||
}
|
||||
}
|
||||
|
||||
cgu::pll0audio::clock_enable();
|
||||
|
||||
/* Route the 12.288MHz PLL to the Base Audio Clock */
|
||||
// PD = 0 enables the clock; AUTOBLOCK = 1 prevents glitches during clock switching
|
||||
LPC_CGU->BASE_AUDIO_CLK.PD = 0;
|
||||
LPC_CGU->BASE_AUDIO_CLK.AUTOBLOCK = 1;
|
||||
LPC_CGU->BASE_AUDIO_CLK.CLK_SEL = toUType(cgu::CLK_SEL::PLL0AUDIO);
|
||||
|
||||
#else
|
||||
cgu::pll0audio::ctrl({
|
||||
.pd = 1,
|
||||
|
||||
Reference in New Issue
Block a user