1 Commits

Author SHA1 Message Date
e6127bcf84 Try polling with delay 2023-10-24 16:44:55 -04:00
2 changed files with 20 additions and 24 deletions

View File

@@ -10,5 +10,5 @@ hal = { package = "esp32-hal", version = "0.15.0" }
esp-backtrace = { version = "0.8.0", features = ["esp32", "panic-handler", "exception-handler", "print-uart"] } esp-backtrace = { version = "0.8.0", features = ["esp32", "panic-handler", "exception-handler", "print-uart"] }
esp-println = { version = "0.6.0", features = ["esp32","log"] } esp-println = { version = "0.6.0", features = ["esp32","log"] }
log = { version = "0.4.18" } log = { version = "0.4.18" }
si5351 = { git = "http://192.168.1.41:3000/lks/si5351.git" } si5351 = "0.2.0"
embedded-hal = "0.2.7" embedded-hal = "0.2.7"

View File

@@ -11,8 +11,7 @@ use hal::{
timer::{self, TimerIFace}, timer::{self, TimerIFace},
HighSpeed, LEDC, HighSpeed, LEDC,
}, },
uart, Uart, /*Delay*/ uart, Delay, Uart, IO,
IO,
}; };
use si5351::{Si5351, Si5351Device}; use si5351::{Si5351, Si5351Device};
@@ -20,11 +19,10 @@ use si5351::{Si5351, Si5351Device};
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
let peripherals = Peripherals::take(); let peripherals = Peripherals::take();
//let uart = peripherals.UART0;
let mut system = peripherals.DPORT.split(); let mut system = peripherals.DPORT.split();
let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); let io = IO::new(peripherals.GPIO, peripherals.IO_MUX);
let clocks = ClockControl::max(system.clock_control).freeze(); let clocks = ClockControl::max(system.clock_control).freeze();
//let mut delay = Delay::new(&clocks); let delay = Delay::new(&clocks);
let serial = { uart::Uart::new(peripherals.UART0, &mut system.peripheral_clock_control) }; let serial = { uart::Uart::new(peripherals.UART0, &mut system.peripheral_clock_control) };
@@ -34,7 +32,6 @@ fn main() -> ! {
// this requires a clean rebuild because of https://github.com/rust-lang/cargo/issues/10358 // this requires a clean rebuild because of https://github.com/rust-lang/cargo/issues/10358
esp_println::logger::init_logger_from_env(); esp_println::logger::init_logger_from_env();
log::info!("Logger is setup"); log::info!("Logger is setup");
//println!("Hello world!");
let i2c = I2C::new( let i2c = I2C::new(
peripherals.I2C0, peripherals.I2C0,
@@ -46,13 +43,12 @@ fn main() -> ! {
); );
log::info!("I2C Initialized"); log::info!("I2C Initialized");
//i2c.write(0b0110_0000, &[0x64]);
let mut clock = Si5351Device::new_adafruit_module(i2c); let mut clock = Si5351Device::new_adafruit_module(i2c);
match clock.init_adafruit_module() { match clock.init_adafruit_module() {
Ok(_) => { Ok(_) => {
log::info!("Si5351 Initialized succesfully"); log::info!("Si5351 Initialized succesfully");
main_loop(clock, serial); clock.set_freq(1_000_000).unwrap();
main_loop(clock, serial, delay);
} }
Err(e) => { Err(e) => {
match e { match e {
@@ -88,17 +84,20 @@ fn main() -> ! {
}) })
.unwrap(); .unwrap();
main_loop(ledc, serial); main_loop(ledc, serial, delay);
} }
} }
} }
fn main_loop<T: Clockable, U: _esp_hal_uart_Instance>(mut clock: T, mut serial: Uart<'_, U>) -> ! { fn main_loop<T: Clockable, U: _esp_hal_uart_Instance>(
clock.set_freq(1_000_000).unwrap(); mut clock: T,
mut serial: Uart<'_, U>,
mut delay: Delay,
) -> ! {
loop { loop {
let freq = read_freq(&mut serial); let freq = read_freq(&mut serial, &mut delay);
log::info!("Got Freq: {}", freq); log::info!("Got Freq: {}", freq);
match clock.set_quad(freq) { match clock.set_freq(freq) {
Ok(_) => log::info!("Set frequency to {}", freq), Ok(_) => log::info!("Set frequency to {}", freq),
Err(_) => log::warn!("Could not set frequency to {}", freq), Err(_) => log::warn!("Could not set frequency to {}", freq),
} }
@@ -107,13 +106,9 @@ fn main_loop<T: Clockable, U: _esp_hal_uart_Instance>(mut clock: T, mut serial:
trait Clockable { trait Clockable {
fn set_freq(&mut self, freq: u32) -> Result<(), ()>; fn set_freq(&mut self, freq: u32) -> Result<(), ()>;
fn set_quad(&mut self, freq: u32) -> Result<f32, ()>;
} }
impl Clockable for LEDC<'_> { impl Clockable for LEDC<'_> {
fn set_quad(&mut self, _freq: u32) -> Result<f32, ()> {
Err(())
}
fn set_freq(&mut self, freq: u32) -> Result<(), ()> { fn set_freq(&mut self, freq: u32) -> Result<(), ()> {
let mut hstimer0 = self.get_timer::<HighSpeed>(timer::Number::Timer0); let mut hstimer0 = self.get_timer::<HighSpeed>(timer::Number::Timer0);
@@ -134,9 +129,6 @@ impl<T> Clockable for Si5351Device<T>
where where
Si5351Device<T>: Si5351, Si5351Device<T>: Si5351,
{ {
fn set_quad(&mut self, freq: u32) -> Result<f32, ()> {
Si5351::set_quad(self, freq).map_err(|_| ())
}
fn set_freq(&mut self, freq: u32) -> Result<(), ()> { fn set_freq(&mut self, freq: u32) -> Result<(), ()> {
match self.set_frequency(si5351::PLL::A, si5351::ClockOutput::Clk0, freq) { match self.set_frequency(si5351::PLL::A, si5351::ClockOutput::Clk0, freq) {
Ok(_) => Ok(()), Ok(_) => Ok(()),
@@ -163,11 +155,12 @@ where
// return freq; // return freq;
// fn rf<T: embedded_hal::serial::Read<u8>>(serial: &mut T) -> u32 { 0 } // fn rf<T: embedded_hal::serial::Read<u8>>(serial: &mut T) -> u32 { 0 }
fn read_freq<T: _esp_hal_uart_Instance>(serial: &mut Uart<'_, T>) -> u32 { fn read_freq<T: _esp_hal_uart_Instance>(serial: &mut Uart<'_, T>, delay: &mut Delay) -> u32 {
let mut freq = 0; let mut freq = 0;
let mut invalid = false; let mut invalid = false;
loop { loop {
match nb::block!(serial.read()) { //match nb::block!(serial.read()) {
match serial.read() {
Ok(input) if input >= b'0' && input <= b'9' => { Ok(input) if input >= b'0' && input <= b'9' => {
log::debug!("Got char: {}", char::from(input)); log::debug!("Got char: {}", char::from(input));
freq = (freq * 10) + (input - b'0') as u32; freq = (freq * 10) + (input - b'0') as u32;
@@ -189,9 +182,12 @@ fn read_freq<T: _esp_hal_uart_Instance>(serial: &mut Uart<'_, T>) -> u32 {
log::debug!("Got inval: {}", char::from(input)); log::debug!("Got inval: {}", char::from(input));
invalid = true; invalid = true;
} }
Err(e) => { Err(nb::Error::Other(e)) => {
log::debug!("Got error: {:?}", e); log::debug!("Got error: {:?}", e);
} }
Err(nb::Error::WouldBlock) => {
delay.delay(10);
}
} }
} }