Decent CAT support TS-480
This commit is contained in:
146
src/main.rs
146
src/main.rs
@@ -8,13 +8,14 @@ use cortex_m::prelude::_embedded_hal_PwmPin;
|
||||
|
||||
use defmt::{println, warn};
|
||||
use defmt_rtt as _;
|
||||
use fixedstr::{str_format, str32, str48};
|
||||
use fixedstr::{str_format, str48};
|
||||
use panic_probe as _;
|
||||
|
||||
use rp2040_hal::clocks::ClockSource;
|
||||
use rp2040_hal::pac::UART0;
|
||||
use rp2040_hal::pll::PLLConfig;
|
||||
use rp2040_hal::pll::common_configs::PLL_USB_48MHZ;
|
||||
use rp2040_hal::uart::{DataBits, StopBits, UartConfig};
|
||||
use rp2040_hal::uart::{DataBits, Enabled, StopBits, UartConfig, UartPeripheral, ValidUartPinout};
|
||||
// Alias for our HAL crate
|
||||
use rp2040_hal as hal;
|
||||
|
||||
@@ -165,7 +166,7 @@ fn main() -> ! {
|
||||
sh1106_dev.blit_framebuffer(&mut i2c, &gfx_buf);
|
||||
delay.delay_ms(500);
|
||||
|
||||
let si5351_dev = Si5351Dev::new(&mut delay, &mut i2c);
|
||||
let mut si5351_dev = Si5351Dev::new(&mut delay, &mut i2c);
|
||||
|
||||
gfx_buf.clear();
|
||||
gfx_buf.draw_textfield(10, 10, 107, 20, "si5351 initialized.");
|
||||
@@ -201,7 +202,7 @@ fn main() -> ! {
|
||||
// UART RX (characters received by RP2040) on pin 2 (GPIO1)
|
||||
pins.gpio1.into_function(),
|
||||
);
|
||||
let uart = hal::uart::UartPeripheral::new(pac.UART0, uart_pins, &mut pac.RESETS)
|
||||
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,
|
||||
@@ -213,84 +214,18 @@ fn main() -> ! {
|
||||
let mut uart_recv_buf = str48::new();
|
||||
|
||||
loop {
|
||||
// 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)
|
||||
// }
|
||||
// };
|
||||
|
||||
// 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)
|
||||
// );
|
||||
run_uart_stuff(&mut i2c, &mut uart, &mut si5351_dev, &mut uart_recv_buf);
|
||||
|
||||
last_gfx_update = last_gfx_update.saturating_add(1);
|
||||
|
||||
if last_gfx_update < 10_000_000 {
|
||||
if last_gfx_update < 100_000 {
|
||||
continue;
|
||||
}
|
||||
last_gfx_update = 0;
|
||||
|
||||
// current_tune -= 1;
|
||||
// if current_tune < 421 {
|
||||
// current_tune = 695;
|
||||
// }
|
||||
|
||||
// set_tune(current_tune, pwm_x);
|
||||
|
||||
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_mhz = si5351_dev.get_clk_freq() / 1_000_000;
|
||||
let mut tune_str_buf = [0u8; 5];
|
||||
let len = u32_into_str(tuned_freq_mhz, &mut tune_str_buf);
|
||||
@@ -335,9 +270,19 @@ fn main() -> ! {
|
||||
sh1106_dev.blit_framebuffer(&mut i2c, &mut gfx_buf);
|
||||
|
||||
// uart.write_full_blocking(b"UART example\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
fn run_uart_stuff<P>(
|
||||
mut i2c: &mut dyn embedded_hal::i2c::I2c<Error = rp2040_hal::i2c::Error>,
|
||||
uart: &mut UartPeripheral<Enabled, UART0, P>,
|
||||
si5351_dev: &mut Si5351Dev,
|
||||
uart_recv_buf: &mut str48,
|
||||
) where
|
||||
P: ValidUartPinout<UART0>,
|
||||
{
|
||||
if !uart.uart_is_readable() {
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
let mut recv_buf = [0u8; 8];
|
||||
@@ -347,7 +292,7 @@ fn main() -> ! {
|
||||
};
|
||||
|
||||
if len_read < 1 {
|
||||
continue;
|
||||
return;
|
||||
}
|
||||
|
||||
let unreadable_len = match str::from_utf8(&recv_buf[0..len_read]) {
|
||||
@@ -364,8 +309,28 @@ fn main() -> ! {
|
||||
|
||||
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());
|
||||
// let ack = str_format!(str32, "ACK CMD: {:?}", cmd);
|
||||
// println!("{}", ack.to_str());
|
||||
|
||||
let resp = match cmd {
|
||||
RadioCmd::VFOA(f) | RadioCmd::VFOB(f) => {
|
||||
if f != 0 {
|
||||
si5351_dev.set_clk_freq(&mut i2c, f);
|
||||
}
|
||||
str_format!(str48, "FA{:011};", si5351_dev.get_clk_freq())
|
||||
}
|
||||
RadioCmd::TX => str48::from("TX0;"),
|
||||
RadioCmd::RX => str48::from("RX0;"),
|
||||
RadioCmd::PS(_on_off) => str48::from("PS1;"),
|
||||
RadioCmd::IF => {
|
||||
str_format!(str48, "IF{:011}{:023} ;", si5351_dev.get_clk_freq(), 0)
|
||||
}
|
||||
RadioCmd::ID => str48::from("ID020;"),
|
||||
RadioCmd::MD => str48::from("MD2;"),
|
||||
RadioCmd::FW => str_format!(str48, "FW{:04};", 0),
|
||||
RadioCmd::AI => str48::from("AI0;"),
|
||||
RadioCmd::Unknown => str48::from("?;"),
|
||||
};
|
||||
uart.write_full_blocking(resp.as_bytes());
|
||||
uart_recv_buf.clear();
|
||||
} else if unreadable_len > 0 {
|
||||
@@ -373,7 +338,6 @@ fn main() -> ! {
|
||||
uart_recv_buf.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
enum RadioCmd {
|
||||
@@ -381,6 +345,12 @@ enum RadioCmd {
|
||||
VFOB(u32),
|
||||
TX,
|
||||
RX,
|
||||
PS(u8),
|
||||
IF,
|
||||
ID,
|
||||
MD,
|
||||
FW,
|
||||
AI,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
@@ -396,18 +366,15 @@ impl RadioCmd {
|
||||
None => return Self::Unknown,
|
||||
};
|
||||
|
||||
if len < 2 {
|
||||
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),
|
||||
@@ -422,7 +389,16 @@ impl RadioCmd {
|
||||
"FB" => Self::VFOB(0),
|
||||
"TX" => Self::TX,
|
||||
"RX" => Self::RX,
|
||||
_ => Self::Unknown,
|
||||
"PS" => Self::PS(1),
|
||||
"IF" => Self::IF,
|
||||
"ID" => Self::ID,
|
||||
"MD" => Self::MD,
|
||||
"FW" => Self::FW,
|
||||
"AI" => Self::AI,
|
||||
_ => {
|
||||
println!("Unknown cmd: {}", &cmd[0..2]);
|
||||
Self::Unknown
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -175,6 +175,12 @@ impl Si5351Dev {
|
||||
new
|
||||
}
|
||||
|
||||
pub fn set_clk_freq(&mut self, i2c: &mut dyn I2c<Error = Error>, f: u32) {
|
||||
self.target_hz = f;
|
||||
let if_freq = 736_940_000;
|
||||
self.configure_pll_vco(i2c, PLL::B, self.target_hz * 100 - if_freq); // get us up to 14.074MHz
|
||||
}
|
||||
|
||||
pub fn get_clk_freq(&self) -> u32 {
|
||||
return self.target_hz;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user