si5351 experiments

This commit is contained in:
JP Stringham
2026-08-19 14:43:07 -04:00
parent 146b5b9310
commit d7a6a08803
3 changed files with 297 additions and 51 deletions

View File

@@ -2,14 +2,18 @@
#![no_main]
mod morse;
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;
@@ -26,6 +30,7 @@ use sh1106_pico_rs::graphics::GraphicsBuf;
use sh1106_pico_rs::sh1106::SH1106Dev;
use crate::morse::Morse;
use crate::si5351::Si5351Dev;
/// NB if OVERCLOCKING
/// you MUST use a BOOT_LOADER which can tolerate the OC
@@ -76,8 +81,8 @@ fn main() -> ! {
pac.PLL_SYS,
xosc.operating_frequency(),
PLLConfig {
vco_freq: 1014.MHz(),
refdiv: 2,
vco_freq: 1200.MHz(),
refdiv: 1,
post_div1: 6,
post_div2: 1,
},
@@ -121,12 +126,12 @@ fn main() -> ! {
let mut pwm_pin = pins.gpio18;
pwm_pin.set_drive_strength(rp2040_hal::gpio::OutputDriveStrength::TwelveMilliAmps);
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 MIN_TUNE: u16 = 4u16;
// const MAX_TUNE: u16 = 65000u16;
// const MIN_TUNE: u16 = 4u16;
set_tune(current_tune, pwm_x);
@@ -135,13 +140,15 @@ fn main() -> ! {
pac.I2C0,
pins.gpio8.reconfigure(),
pins.gpio9.reconfigure(),
400.kHz(),
100.kHz(),
&mut pac.RESETS,
&clocks.system_clock,
);
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);
sh1106_dev.set_vertical_flip(&mut i2c, true);
let sh1106_dev = sh1106_dev;
@@ -160,58 +167,85 @@ fn main() -> ! {
let mut current_code = Morse::from_char('_');
loop {
let mut char_done = false;
// let mut char_done = false;
while !char_done {
let (on, md) = match current_code.next() {
Some((o, d)) => (o, d),
None => {
char_done = true;
(false, 0xFF)
}
};
// while !char_done {
// let (on, md) = match current_code.next() {
// Some((o, d)) => (o, d),
// None => {
// char_done = true;
// (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);
}
}
// 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,
};
// // 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);
// // 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;
}
morse_sequence_index = (morse_sequence_index + 1) % message.len();
last_gfx_update = 0;
println!("ok");
if morse_sequence_index == 0 {
println!("Delay between beacon calls.");
delay.delay_ms(5_000);
}
// current_tune -= 1;
// if current_tune < 421 {
// current_tune = 695;
// }
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);
// set_tune(current_tune, pwm_x);
gfx_buf.clear();
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 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();
@@ -240,7 +274,7 @@ fn main() -> ! {
}
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,
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.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