Lots of improvements, and some serial CAT work
This commit is contained in:
7
Cargo.lock
generated
7
Cargo.lock
generated
@@ -193,6 +193,12 @@ version = "0.6.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "edd0f118536f44f5ccd48bcb8b111bdc3de888b58c74639dfb034a357d0f206d"
|
||||
|
||||
[[package]]
|
||||
name = "fixedstr"
|
||||
version = "0.5.11"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "651755bc34f44f4fef9edc525a2a88d913303b83a75de6de855cce6ef9180188"
|
||||
|
||||
[[package]]
|
||||
name = "frunk"
|
||||
version = "0.4.4"
|
||||
@@ -335,6 +341,7 @@ dependencies = [
|
||||
"defmt",
|
||||
"defmt-rtt",
|
||||
"embedded-hal 1.0.0",
|
||||
"fixedstr",
|
||||
"panic-probe",
|
||||
"rp2040-boot2",
|
||||
"rp2040-hal",
|
||||
|
||||
@@ -9,6 +9,7 @@ cortex-m-rt = "0.7.5"
|
||||
defmt = "1.0.1"
|
||||
defmt-rtt = "1.1.0"
|
||||
embedded-hal = "1.0.0"
|
||||
fixedstr = { version = "0.5.11", features = ["no-alloc"] }
|
||||
panic-probe = { version = "1.0.0", features = [ "print-rtt" ]}
|
||||
rp2040-boot2 = "0.3.0"
|
||||
rp2040-hal = "0.11.0"
|
||||
|
||||
237
src/main.rs
237
src/main.rs
@@ -4,15 +4,17 @@
|
||||
mod morse;
|
||||
mod si5351;
|
||||
|
||||
use cortex_m::prelude::_embedded_hal_PwmPin;
|
||||
use cortex_m::prelude::{_embedded_hal_PwmPin, _embedded_hal_serial_Read};
|
||||
|
||||
use defmt::println;
|
||||
use defmt::{println, warn};
|
||||
use defmt_rtt as _;
|
||||
use fixedstr::{str_format, str8, str16, str32, str48, zstr};
|
||||
use panic_probe as _;
|
||||
|
||||
use rp2040_hal::clocks::ClockSource;
|
||||
use rp2040_hal::pll::PLLConfig;
|
||||
use rp2040_hal::pll::common_configs::PLL_USB_48MHZ;
|
||||
use rp2040_hal::uart::{DataBits, StopBits, UartConfig};
|
||||
// Alias for our HAL crate
|
||||
use rp2040_hal as hal;
|
||||
|
||||
@@ -108,26 +110,29 @@ fn main() -> ! {
|
||||
// Init PWMs
|
||||
let mut pwm_slices = hal::pwm::Slices::new(pac.PWM, &mut pac.RESETS);
|
||||
|
||||
let test_parse: u32 = "4000".parse().unwrap();
|
||||
println!("Parse: {}", test_parse);
|
||||
|
||||
// Configure PWM0
|
||||
let pwm_x = &mut pwm_slices.pwm1;
|
||||
pwm_x.clr_ph_correct();
|
||||
pwm_x.set_div_int(1);
|
||||
pwm_x.set_div_frac(0);
|
||||
pwm_x.clear_interrupt();
|
||||
pwm_x.enable();
|
||||
// let pwm_x = &mut pwm_slices.pwm1;
|
||||
// pwm_x.clr_ph_correct();
|
||||
// pwm_x.set_div_int(1);
|
||||
// pwm_x.set_div_frac(0);
|
||||
// pwm_x.clear_interrupt();
|
||||
// pwm_x.enable();
|
||||
// pwm_x.disable();
|
||||
|
||||
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(true);
|
||||
// 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(true);
|
||||
|
||||
let current_tune = 695;
|
||||
// let current_tune = 695;
|
||||
|
||||
// const MAX_TUNE: u16 = 65000u16;
|
||||
// const MIN_TUNE: u16 = 4u16;
|
||||
|
||||
set_tune(current_tune, pwm_x);
|
||||
// set_tune(current_tune, pwm_x);
|
||||
|
||||
// Create the I²C drive
|
||||
let mut i2c = hal::I2C::i2c0(
|
||||
@@ -141,25 +146,73 @@ fn main() -> ! {
|
||||
|
||||
let mut delay = cortex_m::delay::Delay::new(core.SYST, pll_freq.to_Hz());
|
||||
|
||||
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 mut gfx_buf = GraphicsBuf::new();
|
||||
let mut last_gfx_update = 0u32;
|
||||
|
||||
gfx_buf.sprites[0].x = 114;
|
||||
let mut freq_str_buf = [0u8; 8];
|
||||
let _freq_str_len = u32_into_str(pll_freq.to_kHz(), &mut freq_str_buf);
|
||||
let mut boot_msg = str48::from("Booted at ");
|
||||
boot_msg.push(str::from_utf8(&freq_str_buf).unwrap());
|
||||
boot_msg.push("kHz");
|
||||
println!("{}", boot_msg.to_str());
|
||||
gfx_buf.sprites[0].x = 108;
|
||||
gfx_buf.clear();
|
||||
gfx_buf.draw_textfield(10, 10, 107, 20, boot_msg.to_str());
|
||||
|
||||
gfx_buf.draw_textfield(10, 40, 107, 20, "Initializing si5351");
|
||||
|
||||
sh1106_dev.blit_framebuffer(&mut i2c, &gfx_buf);
|
||||
delay.delay_ms(500);
|
||||
|
||||
let si5351_dev = Si5351Dev::new(&mut delay, &mut i2c);
|
||||
|
||||
gfx_buf.clear();
|
||||
gfx_buf.draw_textfield(10, 10, 107, 20, "si5351 initialized.");
|
||||
let mut status_str = str48::new();
|
||||
let status = si5351_dev.get_status();
|
||||
|
||||
if (si5351::DeviceStatusBits::LOL_A as u8 & status) > 0 {
|
||||
status_str.push("PLLA Lost Lock\n");
|
||||
}
|
||||
|
||||
if (si5351::DeviceStatusBits::LOL_B as u8 & status) > 0 {
|
||||
status_str.push("PLLB Lost Lock\n");
|
||||
}
|
||||
|
||||
if status_str.is_empty() {
|
||||
status_str.push("Setup OK!");
|
||||
}
|
||||
|
||||
gfx_buf.draw_textfield(10, 30, 107, 20, &status_str);
|
||||
sh1106_dev.blit_framebuffer(&mut i2c, &gfx_buf);
|
||||
delay.delay_ms(500);
|
||||
|
||||
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 _morse_sequence_index = 0usize;
|
||||
let _current_code = Morse::from_char('_');
|
||||
|
||||
let uart_pins = (
|
||||
// UART TX (characters sent from RP2040) on pin 1 (GPIO0)
|
||||
pins.gpio0.into_function(),
|
||||
// UART RX (characters received by RP2040) on pin 2 (GPIO1)
|
||||
pins.gpio1.into_function(),
|
||||
);
|
||||
let mut uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||
.enable(
|
||||
UartConfig::new(9600.Hz(), DataBits::Eight, None, StopBits::One),
|
||||
pll_freq,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
uart.write_full_blocking("\r\nREADY.\r\n".as_bytes());
|
||||
|
||||
let mut uart_recv_buf = str48::new();
|
||||
|
||||
loop {
|
||||
// let mut char_done = false;
|
||||
|
||||
@@ -237,37 +290,141 @@ fn main() -> ! {
|
||||
gfx_buf.clear();
|
||||
gfx_buf.draw_string(20, 14, "Beacon running");
|
||||
|
||||
let tuned_freq = pll_freq.to_Hz() / (current_tune as u32);
|
||||
let tuned_freq = tuned_freq / 100;
|
||||
// let tuned_freq = pll_freq.to_Hz() / (current_tune as u32);
|
||||
// let tuned_freq = tuned_freq / 100;
|
||||
let tuned_freq_mhz = si5351_dev.get_clk_freq() / 1_000_000;
|
||||
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();
|
||||
let len = u32_into_str(tuned_freq_mhz, &mut tune_str_buf);
|
||||
let tune_str_mhz = str::from_utf8(&tune_str_buf[tune_str_buf.len() - len..]).unwrap();
|
||||
|
||||
let mut tune_str = str48::from(tune_str_mhz);
|
||||
tune_str.push(".");
|
||||
|
||||
let tuned_freq_khz = si5351_dev.get_clk_freq() - tuned_freq_mhz * 1_000_000;
|
||||
let tuned_freq_khz = (tuned_freq_khz / 1_000).min(999);
|
||||
let mut tune_str_buf = ['0' as u8; 3];
|
||||
let _len = u32_into_str(tuned_freq_khz, &mut tune_str_buf);
|
||||
let tune_str_khz = str::from_utf8(&tune_str_buf).unwrap();
|
||||
tune_str.push(tune_str_khz);
|
||||
tune_str.push("MHz");
|
||||
|
||||
// info!("val {}, len {}", tuned_freq, len);
|
||||
// info!("{}, {}", tune_str, tune_str.len());
|
||||
gfx_buf.draw_string(20, 28, tune_str);
|
||||
gfx_buf.draw_string(20, 28, &tune_str);
|
||||
gfx_buf.sprites[0].y = match gfx_buf.sprites[0].y {
|
||||
48 => 49,
|
||||
_ => 48,
|
||||
14 => 15,
|
||||
_ => 14,
|
||||
};
|
||||
|
||||
let len = u32_into_str(current_tune as u32, &mut tune_str_buf);
|
||||
let tune_str = str::from_utf8(&tune_str_buf[tune_str_buf.len() - len..]).unwrap();
|
||||
// info!("len {}", len);
|
||||
// let len = u32_into_str(current_tune as u32, &mut tune_str_buf);
|
||||
// let tune_str = str::from_utf8(&tune_str_buf[tune_str_buf.len() - len..]).unwrap();
|
||||
// // info!("len {}", len);
|
||||
|
||||
// gfx_buf.draw_string(64 + 6 * (5 - len as u8), 28, tune_str);
|
||||
|
||||
// gfx_buf.draw_string(20, 40, message);
|
||||
gfx_buf.draw_string(20, 40, message);
|
||||
|
||||
// match input_mode {
|
||||
// InputMode::DisplayOnly => (),
|
||||
// InputMode::TuneDigit(d) => {
|
||||
// let x = 64 + 6 * (4 - d) as u8;
|
||||
// gfx_buf.draw_bresenham_line(x, 36, x + 5, 36);
|
||||
// }
|
||||
// }
|
||||
match input_mode {
|
||||
InputMode::DisplayOnly => (),
|
||||
InputMode::TuneDigit(d) => {
|
||||
let x = 64 + 6 * (4 - d) as u8;
|
||||
gfx_buf.draw_bresenham_line(x, 36, x + 5, 36);
|
||||
}
|
||||
}
|
||||
|
||||
// gfx_buf.redraw();
|
||||
// sh1106_dev.blit_framebuffer(&mut i2c, &mut gfx_buf);
|
||||
gfx_buf.redraw();
|
||||
sh1106_dev.blit_framebuffer(&mut i2c, &mut gfx_buf);
|
||||
|
||||
// uart.write_full_blocking(b"UART example\r\n");
|
||||
|
||||
if !uart.uart_is_readable() {
|
||||
continue;
|
||||
}
|
||||
|
||||
let mut recv_buf = [0u8; 8];
|
||||
let len_read = match uart.read_raw(&mut recv_buf) {
|
||||
Ok(n) => n,
|
||||
_ => 0,
|
||||
};
|
||||
|
||||
if len_read < 1 {
|
||||
continue;
|
||||
}
|
||||
|
||||
let unreadable_len = match str::from_utf8(&recv_buf[0..len_read]) {
|
||||
Ok(utf8str) => {
|
||||
// println!("Read a string of len {} from uart", utf8str.len());
|
||||
uart_recv_buf.push(utf8str).len()
|
||||
}
|
||||
_ => {
|
||||
warn!("Uart wasn't valid utf8");
|
||||
1
|
||||
}
|
||||
};
|
||||
// println!("UART CMD:\n{}", uart_recv_buf.to_str());
|
||||
|
||||
if uart_recv_buf.contains(";") {
|
||||
let cmd = RadioCmd::parse_cmd(&uart_recv_buf);
|
||||
let resp = str_format!(str32, "\r\nACK CMD: {:?}\r\n", cmd);
|
||||
println!("{}", resp.to_str());
|
||||
uart.write_full_blocking(resp.as_bytes());
|
||||
uart_recv_buf.clear();
|
||||
} else if unreadable_len > 0 {
|
||||
println!("Input buffer overflow without terminator, clearing");
|
||||
uart_recv_buf.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum RadioCmd {
|
||||
VFOA(u32),
|
||||
VFOB(u32),
|
||||
TX,
|
||||
RX,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
impl RadioCmd {
|
||||
fn parse_cmd(cmd: &str48) -> RadioCmd {
|
||||
let cmd = cmd.to_ascii_upper();
|
||||
let cmd = str48::from(cmd.trim());
|
||||
|
||||
// println!("{}\n\n", cmd.to_str());
|
||||
|
||||
let len = match cmd.to_str().find(";") {
|
||||
Some(n) => n,
|
||||
None => return Self::Unknown,
|
||||
};
|
||||
|
||||
let param = match len {
|
||||
..=3 => str48::new(),
|
||||
n => cmd.substr(2, len),
|
||||
};
|
||||
|
||||
// println!(
|
||||
// "cmd {:?}|\nlen {}|\nparam {}|\n",
|
||||
// cmd.as_bytes(),
|
||||
// len,
|
||||
// param.to_str()
|
||||
// );
|
||||
|
||||
match &cmd[0..2] {
|
||||
"FA" => match len {
|
||||
..=3 => Self::VFOA(0),
|
||||
_n => {
|
||||
let freq_str = param.trim();
|
||||
// println!("freq_str: {}. len {}", freq_str, freq_str.len());
|
||||
let frequ32: u32 = freq_str.parse().unwrap();
|
||||
Self::VFOA(frequ32)
|
||||
}
|
||||
},
|
||||
|
||||
"FB" => Self::VFOB(0),
|
||||
"TX" => Self::TX,
|
||||
"RX" => Self::RX,
|
||||
_ => Self::Unknown,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -73,7 +73,11 @@ pub const SI5351_I2C_ADDR: u8 = 0x60;
|
||||
|
||||
const SI5351_FREQ_XTAL: u32 = 25_000_000;
|
||||
|
||||
pub struct Si5351Dev {}
|
||||
pub struct Si5351Dev {
|
||||
offset_hz: i32,
|
||||
target_hz: u32,
|
||||
status: u8,
|
||||
}
|
||||
|
||||
impl Si5351Dev {
|
||||
pub fn new(delay: &mut Delay, i2c: &mut dyn I2c<Error = Error>) -> Self {
|
||||
@@ -94,7 +98,7 @@ impl Si5351Dev {
|
||||
|
||||
println!("Waiting for Si5351 init...");
|
||||
|
||||
delay.delay_ms(250);
|
||||
delay.delay_ms(500);
|
||||
}
|
||||
|
||||
delay.delay_ms(500);
|
||||
@@ -115,9 +119,11 @@ impl Si5351Dev {
|
||||
Si5351Register::Clock7Ctrl,
|
||||
];
|
||||
|
||||
delay.delay_ms(10);
|
||||
// Power down all clocks
|
||||
for reg in clock_ctrls.iter() {
|
||||
safe_reg_write(i2c, *reg as u8, ClockControlBits::CLK_PDN as u8);
|
||||
delay.delay_ms(10);
|
||||
}
|
||||
|
||||
// both PLLs use onboard XTAL
|
||||
@@ -132,9 +138,14 @@ impl Si5351Dev {
|
||||
|
||||
// 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 mut new = Self {
|
||||
offset_hz: -1140,
|
||||
target_hz: 14_074_000,
|
||||
status: 0,
|
||||
};
|
||||
let if_freq = 736_940_000;
|
||||
configure_pll_vco(i2c, PLL::A, if_freq);
|
||||
configure_pll_vco(i2c, PLL::B, 1_407_286_000 - if_freq); // get us up to 14.074MHz
|
||||
new.configure_pll_vco(i2c, PLL::A, if_freq);
|
||||
new.configure_pll_vco(i2c, PLL::B, new.target_hz * 100 - if_freq); // get us up to 14.074MHz
|
||||
|
||||
// apply 64x multisynth divider
|
||||
set_p1_p2_p3_from_abc(i2c, 100, 0, 1, 42);
|
||||
@@ -157,8 +168,40 @@ impl Si5351Dev {
|
||||
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]);
|
||||
new.status = status_byte[0];
|
||||
|
||||
Self {}
|
||||
new
|
||||
}
|
||||
|
||||
pub fn get_clk_freq(&self) -> u32 {
|
||||
return self.target_hz;
|
||||
}
|
||||
|
||||
pub fn get_status(&self) -> u8 {
|
||||
return self.status;
|
||||
}
|
||||
|
||||
fn configure_pll_vco(&self, i2c: &mut dyn I2c<Error = Error>, pll: PLL, f_vco: u32) {
|
||||
let f_vco = (f_vco as i32).saturating_add(self.offset_hz);
|
||||
let f_vco = f_vco.max(0) as 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -197,26 +240,6 @@ fn safe_reg_write(i2c: &mut dyn I2c<Error = Error>, reg: u8, val: u8) {
|
||||
}
|
||||
}
|
||||
|
||||
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,
|
||||
|
||||
Reference in New Issue
Block a user