mostly working

This commit is contained in:
JP Stringham
2026-08-25 11:07:42 -04:00
parent d7a6a08803
commit f18f91072e
3 changed files with 2166 additions and 89 deletions

View File

@@ -6,16 +6,11 @@ mod si5351;
use cortex_m::prelude::_embedded_hal_PwmPin;
use defmt::info;
use defmt::panic;
use defmt::println;
use defmt_rtt as _;
use embedded_hal::pwm::SetDutyCycle;
use panic_probe as _;
use rp2040_hal::I2C;
use rp2040_hal::clocks::ClockSource;
use rp2040_hal::pll;
use rp2040_hal::pll::PLLConfig;
use rp2040_hal::pll::common_configs::PLL_USB_48MHZ;
// Alias for our HAL crate
@@ -24,7 +19,6 @@ use rp2040_hal as hal;
use hal::fugit::RateExtU32;
use hal::pac;
use embedded_hal::digital::InputPin;
use embedded_hal::digital::OutputPin;
use sh1106_pico_rs::graphics::GraphicsBuf;
use sh1106_pico_rs::sh1106::SH1106Dev;
@@ -128,7 +122,7 @@ fn main() -> ! {
pwm_x.channel_a.output_to(pwm_pin);
pwm_x.channel_a.set_enabled(true);
let mut current_tune = 695;
let current_tune = 695;
// const MAX_TUNE: u16 = 65000u16;
// const MIN_TUNE: u16 = 4u16;
@@ -147,24 +141,24 @@ fn main() -> ! {
let mut delay = cortex_m::delay::Delay::new(core.SYST, pll_freq.to_Hz());
let mut si5351_dev = Si5351Dev::new(&mut delay, &mut i2c);
let _si5351_dev = Si5351Dev::new(&mut delay, &mut i2c);
let mut sh1106_dev = SH1106Dev::new(&mut delay, &mut i2c);
sh1106_dev.set_vertical_flip(&mut i2c, true);
let sh1106_dev = sh1106_dev;
let _sh1106_dev = sh1106_dev;
let mut gfx_buf = GraphicsBuf::new();
let mut last_gfx_update = 0u32;
gfx_buf.sprites[0].x = 114;
let mut input_mode = InputMode::default();
let mut input_mode_chg_input = pins.gpio12.into_pull_up_input();
let input_mode = InputMode::default();
let _input_mode_chg_input = pins.gpio12.into_pull_up_input();
println!("Booted at {}kHz", pll_freq.to_kHz());
let message = "VA3FWE/B";
let mut morse_sequence_index = 0usize;
let mut current_code = Morse::from_char('_');
let _morse_sequence_index = 0usize;
let _current_code = Morse::from_char('_');
loop {
// let mut char_done = false;
@@ -232,7 +226,6 @@ fn main() -> ! {
continue;
}
last_gfx_update = 0;
println!("ok");
// current_tune -= 1;
// if current_tune < 421 {

View File

@@ -8,12 +8,23 @@ use defmt::{self as _, panic, println, warn};
#[repr(u8)]
#[derive(PartialEq, Copy, Clone)]
pub enum Si5351Register {
OutputsEnable = 0x03,
PLLInputSrc = 0x0F,
Clock0Ctrl = 0x10,
Clock1Ctrl = 0x11,
Clock2Ctrl = 0x12,
PLLReset = 0xB1,
OutputsEnable = 3,
PLLInputSrc = 15,
Clock0Ctrl = 16,
Clock1Ctrl = 17,
Clock2Ctrl = 18,
Clock3Ctrl = 19,
Clock4Ctrl = 20,
Clock5Ctrl = 21,
Clock6Ctrl = 22,
Clock7Ctrl = 23,
MSNAP3Base = 26,
MSNAP1Base = 28,
MSNAP3P2Upper = 31,
MSNBP3Base = 34,
SpreadSpecEnable = 149,
PLLReset = 177,
CrystalInternalLoadCapacitance = 183,
}
#[allow(unused)]
@@ -53,19 +64,16 @@ pub enum ClockControlBits {
CLK_DRV_8 = 0b0000_0011,
}
#[allow(unused)]
#[repr(u8)]
#[derive(PartialEq, Copy, Clone)]
pub enum PLLResetBits {
PLLB_RST = 0b1000_0000,
PLLA_RST = 0b0010_0000,
enum PLL {
A,
B,
}
pub const SI5351_I2C_ADDR: u8 = 0x60;
pub struct Si5351Dev {
ready: bool,
}
const SI5351_FREQ_XTAL: u32 = 25_000_000;
pub struct Si5351Dev {}
impl Si5351Dev {
pub fn new(delay: &mut Delay, i2c: &mut dyn I2c<Error = Error>) -> Self {
@@ -94,74 +102,50 @@ impl Si5351Dev {
// Disable outputs
safe_reg_write(i2c, Si5351Register::OutputsEnable as u8, 0xFF);
safe_reg_write(i2c, Si5351Register::SpreadSpecEnable as u8, 0);
// Power down CLK 0 and CLK 2
safe_reg_write(
i2c,
Si5351Register::Clock0Ctrl as u8,
ClockControlBits::CLK_PDN as u8,
);
safe_reg_write(
i2c,
Si5351Register::Clock2Ctrl as u8,
ClockControlBits::CLK_PDN as u8,
);
let clock_ctrls = [
Si5351Register::Clock0Ctrl,
Si5351Register::Clock1Ctrl,
Si5351Register::Clock2Ctrl,
Si5351Register::Clock3Ctrl,
Si5351Register::Clock4Ctrl,
Si5351Register::Clock5Ctrl,
Si5351Register::Clock6Ctrl,
Si5351Register::Clock7Ctrl,
];
// B. Power up the crystal oscillator stage cleanly (Register 15)
// Bit 4 = 0 (XTAL input enabled). Bits 3:2 = 00 (25MHz crystal target reference)
// Power down all clocks
for reg in clock_ctrls.iter() {
safe_reg_write(i2c, *reg as u8, ClockControlBits::CLK_PDN as u8);
}
// both PLLs use onboard XTAL
safe_reg_write(i2c, Si5351Register::PLLInputSrc as u8, 0x00);
// C. Set the internal capacitance load to 8pF to stabilize the clock core
safe_reg_write(i2c, 183, 0x92);
// Set internal capacitance load to 8pF
safe_reg_write(
i2c,
Si5351Register::CrystalInternalLoadCapacitance as u8,
CrystalLoadBits::CL_8 as u8,
);
// D. Apply your 600 MHz PLL A Parameters (Byte-by-Byte via verification helper)
safe_reg_write(i2c, 26, 0x00);
safe_reg_write(i2c, 27, 0x01);
safe_reg_write(i2c, 28, 0x00);
safe_reg_write(i2c, 29, 0x0A);
safe_reg_write(i2c, 30, 0x00);
safe_reg_write(i2c, 31, 0x00);
safe_reg_write(i2c, 32, 0x00);
safe_reg_write(i2c, 33, 0x00);
// P1, P2 and P3 are split in very weird ways and determine PLLA aka VCO freq
// See AN619 for a terrible but thorough explanation.
let if_freq = 737_100_000;
configure_pll_vco(i2c, PLL::A, if_freq);
configure_pll_vco(i2c, PLL::B, 1_407_400_000 - if_freq); // get us up to 14.074MHz
safe_reg_write(i2c, 34, 0x00);
safe_reg_write(i2c, 35, 0x01); // P3[7:0]
safe_reg_write(i2c, 36, 0x00); // P1[17:16]
safe_reg_write(i2c, 37, 0x0A); // P1[15:8]
safe_reg_write(i2c, 38, 0x00); // P1[7:0]
safe_reg_write(i2c, 39, 0x00); // P3/P2 upper
safe_reg_write(i2c, 40, 0x00); // P2[15:8]
safe_reg_write(i2c, 41, 0x00);
send_command(i2c, &Si5351Register::PLLReset, 0xAC);
// E. Apply your 48x MultiSynth Divider Parameters (12.5 MHz Output Target)
safe_reg_write(i2c, 50, 0x00);
safe_reg_write(i2c, 51, 0x01);
safe_reg_write(i2c, 52, 0x00);
safe_reg_write(i2c, 53, 0x16);
safe_reg_write(i2c, 54, 0x00);
safe_reg_write(i2c, 55, 0x00);
safe_reg_write(i2c, 56, 0x00);
safe_reg_write(i2c, 57, 0x00);
// F. Shut down cross-talk pathways
safe_reg_write(i2c, 16, 0x80); // Power down CLK0
safe_reg_write(i2c, 18, 0x80); // Power down CLK2
// G. Configure CLK1 Control to latch onto PLL A in Integer Mode
safe_reg_write(i2c, 17, 0x43);
// H. CRUCIAL: Strobe the execution reset bit (Register 177)
// Send a 0xAC to execute an explicit hard global reset of both synthesis blocks.
// I. Unmask CLK1 to release your clean fundamental frequency
safe_reg_write(i2c, 3, 0xFD);
// apply 64x multisynth divider
set_p1_p2_p3_from_abc(i2c, 100, 0, 1, 50);
set_p1_p2_p3_from_abc(i2c, 100, 0, 1, 58);
// clk 1 is the one we actually use..
safe_reg_write(i2c, Si5351Register::Clock1Ctrl as u8, 0b0000_0011);
safe_reg_write(i2c, Si5351Register::Clock1Ctrl as u8, 0b0000_1100);
safe_reg_write(i2c, Si5351Register::Clock2Ctrl as u8, 0b0010_1100);
send_command(i2c, &Si5351Register::PLLReset, 0xAC);
safe_reg_write(i2c, Si5351Register::OutputsEnable as u8, 0b1111_1101);
safe_reg_write(i2c, Si5351Register::OutputsEnable as u8, 0b1111_1001);
delay.delay_ms(500);
@@ -170,7 +154,7 @@ impl Si5351Dev {
// Print out the raw binary representation of the status register
println!("Si5351 Status: {:08b}", status_byte[0]);
Self { ready: true }
Self {}
}
}
@@ -208,3 +192,84 @@ fn safe_reg_write(i2c: &mut dyn I2c<Error = Error>, reg: u8, val: u8) {
panic!("Register {} rx_buf indicated I2c error.", reg);
}
}
pub fn configure_pll_vco(i2c: &mut dyn I2c<Error = Error>, pll: PLL, f_vco: u32) {
// 1. Calculate traditional Fractional-N parameters (a + b/c)
let a = f_vco / SI5351_FREQ_XTAL;
let remainder = f_vco % SI5351_FREQ_XTAL;
let (b, c) = if remainder == 0 {
(0u32, 1u32) // Strict Integer Mode
} else {
// Find best fractional approximation using Continued Fractions (Max Denominator 2^20 - 1)
rational_approximation(remainder, SI5351_FREQ_XTAL, 1_048_575)
};
let base_reg = match pll {
PLL::A => Si5351Register::MSNAP3Base as u8,
PLL::B => Si5351Register::MSNBP3Base as u8,
};
set_p1_p2_p3_from_abc(i2c, a, b, c, base_reg);
}
pub fn set_p1_p2_p3_from_abc(
i2c: &mut dyn I2c<Error = Error>,
a: u32,
b: u32,
c: u32,
base_reg: u8,
) {
// 2. Encode to Silicon Labs P1, P2, P3 parameters
// We use u64 intermediates to safely avoid arithmetic overflow during multiplication
let p1: u32 = ((128 * a as u64) + ((128 * b as u64) / c as u64) - 512) as u32;
let p2: u32 = ((128 * b as u64) - (c as u64 * ((128 * b as u64) / c as u64))) as u32;
let p3: u32 = c;
// 3. Disassemble the discontinuous bitfields into the 8-byte array budget
let mut payload = [0u8; 9];
payload[0] = base_reg; // Starting register pointer
payload[1] = ((p3 >> 8) & 0xFF) as u8; // P3[15:8]
payload[2] = (p3 & 0xFF) as u8; // P3[7:0]
payload[3] = ((p1 >> 16) & 0x03) as u8; // P1[17:16] (Masked to strict 18-bit max)
payload[4] = ((p1 >> 8) & 0xFF) as u8; // P1[15:8]
payload[5] = (p1 & 0xFF) as u8; // P1[7:0]
payload[6] = (((p3 >> 16) & 0x0F) << 4) as u8 | (((p2 >> 16) & 0x0F) as u8); // P3[19:16] | P2[19:16]
payload[7] = ((p2 >> 8) & 0xFF) as u8; // P2[15:8]
payload[8] = (p2 & 0xFF) as u8; // P2[7:0]
// 4. Fire the single atomic 8-byte burst write to the target peripheral
i2c.write(SI5351_I2C_ADDR, &payload).unwrap();
}
/// Helper function to find a fractional representation (num/den) matching a remainder ratio
fn rational_approximation(num: u32, den: u32, max_den: u32) -> (u32, u32) {
let mut m00 = 1u64;
let mut m01 = 0u64;
let mut m10 = 0u64;
let mut m11 = 1u64;
let mut x = num as u64;
let mut y = den as u64;
while y != 0 {
let q = x / y;
let t = x % y;
x = y;
y = t;
let t0 = m00 * q + m01;
let t1 = m10 * q + m11;
if t1 > max_den as u64 {
break;
}
m01 = m00;
m00 = t0;
m11 = m10;
m10 = t1;
}
(m00 as u32, m10 as u32)
}

2019
src/si5351/freqs.rs Normal file

File diff suppressed because it is too large Load Diff