Compare commits

..

4 Commits

Author SHA1 Message Date
e6127bcf84 Try polling with delay 2023-10-24 16:44:55 -04:00
6d09ce7bc1 Ignore Cargo.lock 2023-09-26 19:41:36 -04:00
0f2d455a42 Remove unused heap allocator 2023-09-26 14:41:44 -04:00
4200a09d09 LedC works up to 2.5MHz 2023-09-26 14:13:50 -04:00
4 changed files with 87 additions and 52 deletions

View File

@@ -14,4 +14,4 @@ rustflags = [
target = "xtensa-esp32-none-elf" target = "xtensa-esp32-none-elf"
[unstable] [unstable]
build-std = ["alloc", "core"] build-std = ["core"]

1
.gitignore vendored
View File

@@ -2,6 +2,7 @@
# will have compiled files and executables # will have compiled files and executables
debug/ debug/
target/ target/
Cargo.lock
# These are backup files generated by rustfmt # These are backup files generated by rustfmt
**/*.rs.bk **/*.rs.bk

View File

@@ -10,6 +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" }
esp-alloc = { version = "0.3.0" }
si5351 = "0.2.0" si5351 = "0.2.0"
embedded-hal = "0.2.7" embedded-hal = "0.2.7"

View File

@@ -1,38 +1,28 @@
#![no_std] #![no_std]
#![no_main] #![no_main]
extern crate alloc;
use core::mem::MaybeUninit;
use esp_backtrace as _; use esp_backtrace as _;
//use esp_println::println;
use embedded_hal::serial::Read; use embedded_hal::serial::Read;
use hal::{clock::ClockControl, i2c::I2C, peripherals::Peripherals, prelude::*}; use hal::{clock::ClockControl, i2c::I2C, peripherals::Peripherals, prelude::*};
use hal::{uart, Uart /*Delay*/, IO}; use hal::{
ledc::{
channel::{self, ChannelIFace},
timer::{self, TimerIFace},
HighSpeed, LEDC,
},
uart, Delay, Uart, IO,
};
use si5351;
use si5351::{Si5351, Si5351Device}; use si5351::{Si5351, Si5351Device};
#[global_allocator]
static ALLOCATOR: esp_alloc::EspHeap = esp_alloc::EspHeap::empty();
fn init_heap() {
const HEAP_SIZE: usize = 32 * 1024;
static mut HEAP: MaybeUninit<[u8; HEAP_SIZE]> = MaybeUninit::uninit();
unsafe {
ALLOCATOR.init(HEAP.as_mut_ptr() as *mut u8, HEAP_SIZE);
}
}
#[entry] #[entry]
fn main() -> ! { fn main() -> ! {
init_heap();
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) };
@@ -42,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,
@@ -54,28 +43,59 @@ 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(si5351::Error::CommunicationError) => { Err(e) => {
log::info!("Could not initialize Si5351: CommunicationError"); match e {
main_loop(Dummy {}, serial); si5351::Error::InvalidParameter => {
log::warn!("Could not initialize Si5351; Using LedC PWM")
} }
Err(si5351::Error::InvalidParameter) => { si5351::Error::CommunicationError => {
log::info!("Could not initialize Si5351: InvalidParameter"); log::info!("Could not find Si5351 device; Using LedC PWM")
main_loop(Dummy {}, serial); }
}
let ledc = LEDC::new(
peripherals.LEDC,
&clocks,
&mut system.peripheral_clock_control,
);
let led = io.pins.gpio4.into_push_pull_output();
let mut hstimer0 = ledc.get_timer::<HighSpeed>(timer::Number::Timer0);
hstimer0
.configure(timer::config::Config {
duty: timer::config::Duty::Duty5Bit,
clock_source: timer::HSClockSource::APBClk,
frequency: 1u32.MHz(),
})
.unwrap();
let mut channel0 = ledc.get_channel(channel::Number::Channel0, led);
channel0
.configure(channel::config::Config {
timer: &hstimer0,
duty_pct: 50,
pin_config: channel::config::PinConfig::PushPull,
})
.unwrap();
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>(
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_freq(freq) { match clock.set_freq(freq) {
Ok(_) => log::info!("Set frequency to {}", freq), Ok(_) => log::info!("Set frequency to {}", freq),
@@ -87,9 +107,27 @@ 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<(), ()>;
} }
impl<T> Clockable for T
impl Clockable for LEDC<'_> {
fn set_freq(&mut self, freq: u32) -> Result<(), ()> {
let mut hstimer0 = self.get_timer::<HighSpeed>(timer::Number::Timer0);
let res = hstimer0.configure(timer::config::Config {
duty: timer::config::Duty::Duty5Bit,
clock_source: timer::HSClockSource::APBClk,
frequency: freq.Hz(),
});
match res {
Ok(_) => Ok(()),
Err(_) => Err(()),
}
}
}
impl<T> Clockable for Si5351Device<T>
where where
T: Si5351, Si5351Device<T>: Si5351,
{ {
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) {
@@ -98,13 +136,6 @@ where
} }
} }
} }
struct Dummy;
impl Clockable for Dummy {
fn set_freq(&mut self, freq: u32) -> Result<(), ()> {
log::info!("Dummy set freq: {}", freq);
Ok(())
}
}
// bool inval = false; // bool inval = false;
// char c = serial.read(); // char c = serial.read();
@@ -124,34 +155,38 @@ impl Clockable for Dummy {
// 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::info!("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;
} }
Ok(input) if invalid && (input == b'\n' || input == b'\r') => { Ok(input) if invalid && (input == b'\n' || input == b'\r') => {
log::info!("Got bad end: {}", char::from(input)); log::debug!("Got bad end: {}", char::from(input));
freq = 0; freq = 0;
invalid = false; invalid = false;
} }
Ok(input) if !invalid && (input == b'\n' || input == b'\r') => { Ok(input) if !invalid && (input == b'\n' || input == b'\r') => {
log::info!("Got end: {}", char::from(input)); log::debug!("Got end: {}", char::from(input));
break; break;
} }
Ok(input) if input == b'.' || input == b',' || input == b'_' => { Ok(input) if input == b'.' || input == b',' || input == b'_' => {
log::info!("Got ignor: {}", char::from(input)); log::debug!("Got ignor: {}", char::from(input));
continue; continue;
} }
Ok(input) => { Ok(input) => {
log::info!("Got inval: {}", char::from(input)); log::debug!("Got inval: {}", char::from(input));
invalid = true; invalid = true;
} }
Err(e) => { Err(nb::Error::Other(e)) => {
log::info!("Got error: {:?}", e); log::debug!("Got error: {:?}", e);
}
Err(nb::Error::WouldBlock) => {
delay.delay(10);
} }
} }
} }