si5351 experiments
This commit is contained in:
142
src/main.rs
142
src/main.rs
@@ -2,14 +2,18 @@
|
|||||||
#![no_main]
|
#![no_main]
|
||||||
|
|
||||||
mod morse;
|
mod morse;
|
||||||
|
mod si5351;
|
||||||
|
|
||||||
use cortex_m::prelude::_embedded_hal_PwmPin;
|
use cortex_m::prelude::_embedded_hal_PwmPin;
|
||||||
|
|
||||||
use defmt::info;
|
use defmt::info;
|
||||||
|
use defmt::panic;
|
||||||
use defmt::println;
|
use defmt::println;
|
||||||
use defmt_rtt as _;
|
use defmt_rtt as _;
|
||||||
|
use embedded_hal::pwm::SetDutyCycle;
|
||||||
use panic_probe as _;
|
use panic_probe as _;
|
||||||
|
|
||||||
|
use rp2040_hal::I2C;
|
||||||
use rp2040_hal::clocks::ClockSource;
|
use rp2040_hal::clocks::ClockSource;
|
||||||
use rp2040_hal::pll;
|
use rp2040_hal::pll;
|
||||||
use rp2040_hal::pll::PLLConfig;
|
use rp2040_hal::pll::PLLConfig;
|
||||||
@@ -26,6 +30,7 @@ use sh1106_pico_rs::graphics::GraphicsBuf;
|
|||||||
use sh1106_pico_rs::sh1106::SH1106Dev;
|
use sh1106_pico_rs::sh1106::SH1106Dev;
|
||||||
|
|
||||||
use crate::morse::Morse;
|
use crate::morse::Morse;
|
||||||
|
use crate::si5351::Si5351Dev;
|
||||||
|
|
||||||
/// NB if OVERCLOCKING
|
/// NB if OVERCLOCKING
|
||||||
/// you MUST use a BOOT_LOADER which can tolerate the OC
|
/// you MUST use a BOOT_LOADER which can tolerate the OC
|
||||||
@@ -76,8 +81,8 @@ fn main() -> ! {
|
|||||||
pac.PLL_SYS,
|
pac.PLL_SYS,
|
||||||
xosc.operating_frequency(),
|
xosc.operating_frequency(),
|
||||||
PLLConfig {
|
PLLConfig {
|
||||||
vco_freq: 1014.MHz(),
|
vco_freq: 1200.MHz(),
|
||||||
refdiv: 2,
|
refdiv: 1,
|
||||||
post_div1: 6,
|
post_div1: 6,
|
||||||
post_div2: 1,
|
post_div2: 1,
|
||||||
},
|
},
|
||||||
@@ -121,12 +126,12 @@ fn main() -> ! {
|
|||||||
let mut pwm_pin = pins.gpio18;
|
let mut pwm_pin = pins.gpio18;
|
||||||
pwm_pin.set_drive_strength(rp2040_hal::gpio::OutputDriveStrength::TwelveMilliAmps);
|
pwm_pin.set_drive_strength(rp2040_hal::gpio::OutputDriveStrength::TwelveMilliAmps);
|
||||||
pwm_x.channel_a.output_to(pwm_pin);
|
pwm_x.channel_a.output_to(pwm_pin);
|
||||||
pwm_x.channel_a.set_enabled(false);
|
pwm_x.channel_a.set_enabled(true);
|
||||||
|
|
||||||
let current_tune = 8;
|
let mut current_tune = 695;
|
||||||
|
|
||||||
const MAX_TUNE: u16 = 65000u16;
|
// const MAX_TUNE: u16 = 65000u16;
|
||||||
const MIN_TUNE: u16 = 4u16;
|
// const MIN_TUNE: u16 = 4u16;
|
||||||
|
|
||||||
set_tune(current_tune, pwm_x);
|
set_tune(current_tune, pwm_x);
|
||||||
|
|
||||||
@@ -135,13 +140,15 @@ fn main() -> ! {
|
|||||||
pac.I2C0,
|
pac.I2C0,
|
||||||
pins.gpio8.reconfigure(),
|
pins.gpio8.reconfigure(),
|
||||||
pins.gpio9.reconfigure(),
|
pins.gpio9.reconfigure(),
|
||||||
400.kHz(),
|
100.kHz(),
|
||||||
&mut pac.RESETS,
|
&mut pac.RESETS,
|
||||||
&clocks.system_clock,
|
&clocks.system_clock,
|
||||||
);
|
);
|
||||||
|
|
||||||
let mut delay = cortex_m::delay::Delay::new(core.SYST, pll_freq.to_Hz());
|
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 mut sh1106_dev = SH1106Dev::new(&mut delay, &mut i2c);
|
let mut sh1106_dev = SH1106Dev::new(&mut delay, &mut i2c);
|
||||||
sh1106_dev.set_vertical_flip(&mut i2c, true);
|
sh1106_dev.set_vertical_flip(&mut i2c, true);
|
||||||
let sh1106_dev = sh1106_dev;
|
let sh1106_dev = sh1106_dev;
|
||||||
@@ -160,58 +167,85 @@ fn main() -> ! {
|
|||||||
let mut current_code = Morse::from_char('_');
|
let mut current_code = Morse::from_char('_');
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
let mut char_done = false;
|
// let mut char_done = false;
|
||||||
|
|
||||||
while !char_done {
|
// while !char_done {
|
||||||
let (on, md) = match current_code.next() {
|
// let (on, md) = match current_code.next() {
|
||||||
Some((o, d)) => (o, d),
|
// Some((o, d)) => (o, d),
|
||||||
None => {
|
// None => {
|
||||||
char_done = true;
|
// char_done = true;
|
||||||
(false, 0xFF)
|
// (false, 0xFF)
|
||||||
|
// }
|
||||||
|
// };
|
||||||
|
|
||||||
|
// match on {
|
||||||
|
// true => {
|
||||||
|
// led_pin.set_high().unwrap();
|
||||||
|
// pwm_x.channel_a.set_enabled(true);
|
||||||
|
// }
|
||||||
|
// false => {
|
||||||
|
// led_pin.set_low().unwrap();
|
||||||
|
// pwm_x.channel_a.set_enabled(false);
|
||||||
|
// }
|
||||||
|
// }
|
||||||
|
|
||||||
|
// // calculate next delay.
|
||||||
|
// let morse_delay = match md {
|
||||||
|
// 0 => 100_000,
|
||||||
|
// 1 => 200_000,
|
||||||
|
// 2 => 400_000,
|
||||||
|
// 0xFF => 1_000_000,
|
||||||
|
// _ => 400_000,
|
||||||
|
// };
|
||||||
|
|
||||||
|
// // println!("delaying {} us", morse_delay);
|
||||||
|
// delay.delay_us(morse_delay);
|
||||||
|
// }
|
||||||
|
// morse_sequence_index = (morse_sequence_index + 1) % message.len();
|
||||||
|
|
||||||
|
// if morse_sequence_index == 0 {
|
||||||
|
// println!("Delay between beacon calls.");
|
||||||
|
// delay.delay_ms(5_000);
|
||||||
|
// }
|
||||||
|
|
||||||
|
// let mut message_chars = message.chars();
|
||||||
|
// let c = message_chars.nth(morse_sequence_index).unwrap();
|
||||||
|
|
||||||
|
// println!("next char is {}", c);
|
||||||
|
|
||||||
|
// current_code = Morse::from_char(c);
|
||||||
|
|
||||||
|
// current_tune += 1;
|
||||||
|
|
||||||
|
// if current_tune > 222 {
|
||||||
|
// current_tune = 200;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// println!(
|
||||||
|
// "next freq is {}kHz",
|
||||||
|
// (pll_freq.to_kHz() as f32) / (current_tune as f32)
|
||||||
|
// );
|
||||||
|
|
||||||
|
last_gfx_update = last_gfx_update.saturating_add(1);
|
||||||
|
|
||||||
|
if last_gfx_update < 10_000_000 {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
};
|
last_gfx_update = 0;
|
||||||
|
println!("ok");
|
||||||
|
|
||||||
match on {
|
// current_tune -= 1;
|
||||||
true => {
|
// if current_tune < 421 {
|
||||||
led_pin.set_high().unwrap();
|
// current_tune = 695;
|
||||||
pwm_x.channel_a.set_enabled(true);
|
// }
|
||||||
}
|
|
||||||
false => {
|
|
||||||
led_pin.set_low().unwrap();
|
|
||||||
pwm_x.channel_a.set_enabled(false);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// calculate next delay.
|
// set_tune(current_tune, pwm_x);
|
||||||
let morse_delay = match md {
|
|
||||||
0 => 100_000,
|
|
||||||
1 => 200_000,
|
|
||||||
2 => 400_000,
|
|
||||||
0xFF => 1_000_000,
|
|
||||||
_ => 400_000,
|
|
||||||
};
|
|
||||||
|
|
||||||
// println!("delaying {} us", morse_delay);
|
|
||||||
delay.delay_us(morse_delay);
|
|
||||||
}
|
|
||||||
morse_sequence_index = (morse_sequence_index + 1) % message.len();
|
|
||||||
|
|
||||||
if morse_sequence_index == 0 {
|
|
||||||
println!("Delay between beacon calls.");
|
|
||||||
delay.delay_ms(5_000);
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut message_chars = message.chars();
|
|
||||||
let c = message_chars.nth(morse_sequence_index).unwrap();
|
|
||||||
|
|
||||||
println!("next char is {}", c);
|
|
||||||
|
|
||||||
current_code = Morse::from_char(c);
|
|
||||||
|
|
||||||
gfx_buf.clear();
|
gfx_buf.clear();
|
||||||
gfx_buf.draw_string(20, 14, "Beacon running");
|
gfx_buf.draw_string(20, 14, "Beacon running");
|
||||||
|
|
||||||
let tuned_freq = pll_freq.to_kHz() / (current_tune as u32);
|
let tuned_freq = pll_freq.to_Hz() / (current_tune as u32);
|
||||||
|
let tuned_freq = tuned_freq / 100;
|
||||||
let mut tune_str_buf = [0u8; 5];
|
let mut tune_str_buf = [0u8; 5];
|
||||||
let len = u32_into_str(tuned_freq, &mut tune_str_buf);
|
let len = u32_into_str(tuned_freq, &mut tune_str_buf);
|
||||||
let tune_str = str::from_utf8(&tune_str_buf[tune_str_buf.len() - len..]).unwrap();
|
let tune_str = str::from_utf8(&tune_str_buf[tune_str_buf.len() - len..]).unwrap();
|
||||||
@@ -240,7 +274,7 @@ fn main() -> ! {
|
|||||||
}
|
}
|
||||||
|
|
||||||
gfx_buf.redraw();
|
gfx_buf.redraw();
|
||||||
sh1106_dev.blit_framebuffer(&mut i2c, &mut gfx_buf);
|
// sh1106_dev.blit_framebuffer(&mut i2c, &mut gfx_buf);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -248,8 +282,10 @@ fn set_tune(
|
|||||||
tune: u16,
|
tune: u16,
|
||||||
pwm: &mut rp2040_hal::pwm::Slice<rp2040_hal::pwm::Pwm1, rp2040_hal::pwm::FreeRunning>,
|
pwm: &mut rp2040_hal::pwm::Slice<rp2040_hal::pwm::Pwm1, rp2040_hal::pwm::FreeRunning>,
|
||||||
) {
|
) {
|
||||||
|
let tgt_duty = tune / 3;
|
||||||
pwm.set_top(tune - 1);
|
pwm.set_top(tune - 1);
|
||||||
pwm.channel_a.set_duty(tune / 2);
|
pwm.channel_a.set_duty(tgt_duty.max(1));
|
||||||
|
println!("set tune to {} with duty {}", tune, tgt_duty);
|
||||||
}
|
}
|
||||||
|
|
||||||
// returns the length of the final str removing leading zeroes
|
// returns the length of the final str removing leading zeroes
|
||||||
|
|||||||
210
src/si5351.rs
Normal file
210
src/si5351.rs
Normal file
@@ -0,0 +1,210 @@
|
|||||||
|
use cortex_m::delay::Delay;
|
||||||
|
use embedded_hal::i2c::I2c;
|
||||||
|
use rp2040_hal::i2c::Error;
|
||||||
|
|
||||||
|
use defmt::{self as _, panic, println, warn};
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum Si5351Register {
|
||||||
|
OutputsEnable = 0x03,
|
||||||
|
PLLInputSrc = 0x0F,
|
||||||
|
Clock0Ctrl = 0x10,
|
||||||
|
Clock1Ctrl = 0x11,
|
||||||
|
Clock2Ctrl = 0x12,
|
||||||
|
PLLReset = 0xB1,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum DeviceStatusBits {
|
||||||
|
SysInit = 0b1000_0000,
|
||||||
|
LOL_B = 0b0100_0000,
|
||||||
|
LOL_A = 0b0010_0000,
|
||||||
|
LOS = 0b0001_0000,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum CrystalLoadBits {
|
||||||
|
RESERVED = 0b00_010010,
|
||||||
|
CL_6 = 0b01_000000,
|
||||||
|
CL_8 = 0b10_000000,
|
||||||
|
CL_10 = 0b11_000000,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum ClockControlBits {
|
||||||
|
CLK_PDN = 0b1000_0000,
|
||||||
|
MS_INT = 0b0100_0000,
|
||||||
|
MS_SRC = 0b0010_0000,
|
||||||
|
CLK_INV = 0b0001_0000,
|
||||||
|
CLK_SRC_MASK = 0b0000_1100,
|
||||||
|
CLK_SRC_XTAL = 0b0000_0000,
|
||||||
|
CLK_SRC_CLKIN = 0b0000_0100,
|
||||||
|
CLK_SRC_MS_ALT = 0b0000_1000,
|
||||||
|
CLK_DRV_4 = 0b0000_0001,
|
||||||
|
CLK_DRV_6 = 0b0000_0010,
|
||||||
|
CLK_DRV_8 = 0b0000_0011,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[allow(unused)]
|
||||||
|
#[repr(u8)]
|
||||||
|
#[derive(PartialEq, Copy, Clone)]
|
||||||
|
pub enum PLLResetBits {
|
||||||
|
PLLB_RST = 0b1000_0000,
|
||||||
|
PLLA_RST = 0b0010_0000,
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const SI5351_I2C_ADDR: u8 = 0x60;
|
||||||
|
|
||||||
|
pub struct Si5351Dev {
|
||||||
|
ready: bool,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Si5351Dev {
|
||||||
|
pub fn new(delay: &mut Delay, i2c: &mut dyn I2c<Error = Error>) -> Self {
|
||||||
|
let mut rx_buff = [0u8; 1];
|
||||||
|
|
||||||
|
loop {
|
||||||
|
let res = match i2c.write_read(SI5351_I2C_ADDR, &[0], &mut rx_buff) {
|
||||||
|
Ok(_) => true,
|
||||||
|
Err(_) => {
|
||||||
|
warn!("Si5351 no comms");
|
||||||
|
false
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
if res && (rx_buff[0] & 0x80) == 0 {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
println!("Waiting for Si5351 init...");
|
||||||
|
|
||||||
|
delay.delay_ms(250);
|
||||||
|
}
|
||||||
|
|
||||||
|
delay.delay_ms(500);
|
||||||
|
println!("Si5351 initialized");
|
||||||
|
|
||||||
|
// Disable outputs
|
||||||
|
safe_reg_write(i2c, Si5351Register::OutputsEnable as u8, 0xFF);
|
||||||
|
|
||||||
|
// 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,
|
||||||
|
);
|
||||||
|
|
||||||
|
// B. Power up the crystal oscillator stage cleanly (Register 15)
|
||||||
|
// Bit 4 = 0 (XTAL input enabled). Bits 3:2 = 00 (25MHz crystal target reference)
|
||||||
|
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);
|
||||||
|
|
||||||
|
// 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);
|
||||||
|
|
||||||
|
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);
|
||||||
|
|
||||||
|
// clk 1 is the one we actually use..
|
||||||
|
safe_reg_write(i2c, Si5351Register::Clock1Ctrl as u8, 0b0000_0011);
|
||||||
|
|
||||||
|
safe_reg_write(i2c, Si5351Register::OutputsEnable as u8, 0b1111_1101);
|
||||||
|
|
||||||
|
delay.delay_ms(500);
|
||||||
|
|
||||||
|
let mut status_byte = [0u8; 1];
|
||||||
|
i2c.write_read(0x60, &[0x00], &mut status_byte).unwrap();
|
||||||
|
// Print out the raw binary representation of the status register
|
||||||
|
println!("Si5351 Status: {:08b}", status_byte[0]);
|
||||||
|
|
||||||
|
Self { ready: true }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn send_command(i2c: &mut dyn I2c<Error = Error>, register: &Si5351Register, data: u8) {
|
||||||
|
let mut writebuf: [u8; 2] = [0; 2];
|
||||||
|
|
||||||
|
writebuf[0] = register.clone() as u8;
|
||||||
|
writebuf[1] = data;
|
||||||
|
|
||||||
|
match i2c.write(SI5351_I2C_ADDR, &writebuf) {
|
||||||
|
_ => (),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
fn safe_reg_write(i2c: &mut dyn I2c<Error = Error>, reg: u8, val: u8) {
|
||||||
|
let mut rx_buf = [0u8; 1];
|
||||||
|
|
||||||
|
// 1. Attempt the write
|
||||||
|
match i2c.write(SI5351_I2C_ADDR, &[reg, val]) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(_e) => panic!("Register {} write err!", reg),
|
||||||
|
}
|
||||||
|
|
||||||
|
// 2. Read back what the chip actually stored (Using your Repeated Start routine)
|
||||||
|
match i2c.write_read(SI5351_I2C_ADDR, &[reg], &mut rx_buf) {
|
||||||
|
Ok(_) => (),
|
||||||
|
Err(_e) => panic!(
|
||||||
|
"Register {} verification mismatch! Chip rejected the data.",
|
||||||
|
reg
|
||||||
|
),
|
||||||
|
}
|
||||||
|
// 3. Confirm match
|
||||||
|
if rx_buf[0] != val {
|
||||||
|
// If it returns this error, your I2C timing or line logic is corrupted!
|
||||||
|
panic!("Register {} rx_buf indicated I2c error.", reg);
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user