From 235babe3623fa5c926ef105742dc7ed2c6e7fc3a Mon Sep 17 00:00:00 2001 From: Lucas Schumacher Date: Mon, 25 Sep 2023 21:40:41 -0400 Subject: [PATCH] serial input added --- Cargo.toml | 2 + README.md | 7 ++-- src/main.rs | 105 ++++++++++++++++++++++++++++++++++++++++++++++++---- 3 files changed, 104 insertions(+), 10 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index f6c2eae..bb84301 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,3 +11,5 @@ esp-backtrace = { version = "0.8.0", features = ["esp32", "panic-handler", "exce esp-println = { version = "0.6.0", features = ["esp32","log"] } log = { version = "0.4.18" } esp-alloc = { version = "0.3.0" } +si5351 = "0.2.0" +embedded-hal = "0.2.7" diff --git a/README.md b/README.md index 66bc13d..27eb713 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,7 @@ -#esp-clockgen +# esp-clockgen -##generate a clock signal with an esp32. -If a Si5351 clock generator is connected to the i2C buss it will be used to generate the signal. +## generate a clock signal with an esp32. + +If a Si5351 clock generator is connected to the i2C buss it will be used to generate the signal on PLL A. Otherwise the PWM output pin of the esp32 is used. Configuration of the clock signal is done via the esp32 serial interface. diff --git a/src/main.rs b/src/main.rs index 3da0e91..a4cae35 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,8 +4,14 @@ extern crate alloc; use core::mem::MaybeUninit; use esp_backtrace as _; -use esp_println::println; -use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, Delay}; +//use esp_println::println; + +use embedded_hal::serial::Read; +use hal::{IO, uart, Uart, /*Delay*/}; +use hal::{clock::ClockControl, peripherals::Peripherals, prelude::*, i2c::I2C}; + +use si5351; +use si5351::{Si5351, Si5351Device}; #[global_allocator] static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty(); @@ -22,9 +28,18 @@ fn init_heap() { fn main() -> ! { init_heap(); let peripherals = Peripherals::take(); - let system = peripherals.DPORT.split(); + //let uart = peripherals.UART0; + let mut system = peripherals.DPORT.split(); + let io = IO::new(peripherals.GPIO, peripherals.IO_MUX); let clocks = ClockControl::max(system.clock_control).freeze(); - let mut delay = Delay::new(&clocks); + //let mut delay = Delay::new(&clocks); + + let mut serial = { + uart::Uart::new( + peripherals.UART0, + &mut system.peripheral_clock_control, + ) + }; // setup logger // To change the log_level change the env section in .cargo/config.toml @@ -32,9 +47,85 @@ fn main() -> ! { // this requires a clean rebuild because of https://github.com/rust-lang/cargo/issues/10358 esp_println::logger::init_logger_from_env(); log::info!("Logger is setup"); - println!("Hello world!"); + //println!("Hello world!"); + + let i2c = I2C::new( + peripherals.I2C0, + io.pins.gpio21, + io.pins.gpio22, + 100u32.kHz(), + &mut system.peripheral_clock_control, + &clocks, + ); + log::info!("I2C Initialized"); + + //i2c.write(0b0110_0000, &[0x64]); + + let mut clock = Si5351Device::new_adafruit_module(i2c); + match clock.init_adafruit_module() { + Ok(_) => log::info!("Si5351 Initialized succesfully"), + Err(si5351::Error::CommunicationError) => log::info!("Could not initialize Si5351: CommunicationError"), + Err(si5351::Error::InvalidParameter) => log::info!("Could not initialize Si5351: InvalidParameter"), + } + + // clock.set_frequency(si5351::PLL::A, si5351::ClockOutput::Clk0, 14_175_000).unwrap(); + loop { - println!("Loop..."); - delay.delay_ms(500u32); + //delay.delay_ms(5000u32); + let freq = read_freq(&mut serial); + log::info!("Got Freq: {}", freq); } } + +// bool inval = false; +// char c = serial.read(); +// for (;;) { + // int freq = 0; + // for (;(c >= '0' && c <= '9') || c == '.' || c == ',' || c == '_'; c = serial.read()) + // if (c >= '0' && c <= '9') + // freq = (freq * 10) + (c - '0'); + + // if (c == 'G' || c=='M' || c=='K') freq = freq * mult(c) + // c = serial.read() + + // for ( ; c != '\n' or '\r'; c = serial.read()) if (!inval && c != ' ' or '\t') inval = true; + + // if !inval && freq > 0 {break;} +// } +// return freq; + +// fn rf>(serial: &mut T) -> u32 { 0 } +fn read_freq(serial: &mut Uart<'_,T>) -> u32 { + let mut freq = 0; + let mut invalid = false; + loop { + match nb::block!(serial.read()) { + Ok(input) if input >= b'0' && input <= b'9' => { + log::info!("Got char: {}", char::from(input)); + freq = (freq * 10) + (input - b'0') as u32; + }, + Ok(input) if invalid && (input == b'\n' || input == b'\r') => { + log::info!("Got bad end: {}", char::from(input)); + freq = 0; + invalid = false; + }, + Ok(input) if !invalid && (input == b'\n' || input == b'\r') => { + log::info!("Got end: {}", char::from(input)); + break; + }, + Ok(input) if input == b'.' || input == b',' || input == b'_' => { + log::info!("Got ignor: {}", char::from(input)); + continue; + }, + Ok(input) => { + log::info!("Got inval: {}", char::from(input)); + invalid = true; + }, + Err(e) => { + log::info!("Got error: {:?}", e); + }, + } + } + + freq +} \ No newline at end of file