diff --git a/Cargo.toml b/Cargo.toml index 435cb3a..40e4b95 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,7 +5,7 @@ version = "0.1.0" license = "MIT OR Apache-2.0" [dependencies] -cortex-m = "0.7" +cortex-m = {version = "0.7"} cortex-m-rt = "0.7" embedded-hal = { version = "0.2.5", features = ["unproven"] } @@ -17,6 +17,7 @@ panic-probe = { version = "0.3", features = ["print-defmt"] } rp-pico = "0.8" max31855 = "0.1.0" hd44780-driver = "0.4.0" +embedded-alloc = "0.5.1" # cargo build/run [profile.dev] diff --git a/src/main.rs b/src/main.rs index 1b82560..3d8364a 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,8 @@ #![no_std] #![no_main] +extern crate alloc; +use alloc::format; use bsp::{ entry, hal::{ @@ -9,7 +11,9 @@ use bsp::{ }, }; use core::fmt::Write; +use cortex_m::delay::Delay; use defmt_rtt as _; +use embedded_alloc::Heap; use embedded_hal::digital::v2::OutputPin; use embedded_hal::spi::MODE_0; use panic_probe as _; @@ -30,6 +34,19 @@ use bsp::hal::{ use hd44780_driver as hd44780; use max31855::{Max31855, Unit}; +#[global_allocator] +static HEAP: Heap = Heap::empty(); + +fn write_lcd( + lcd: &mut hd44780::HD44780, + delay: &mut Delay, + string: &str, +) { + lcd.reset(delay).unwrap(); + lcd.clear(delay).unwrap(); + lcd.write_str(string, delay).unwrap(); +} + #[entry] fn main() -> ! { let mut pac = pac::Peripherals::take().unwrap(); @@ -51,6 +68,14 @@ fn main() -> ! { .ok() .unwrap(); + //initalize the heap + { + use core::mem::MaybeUninit; + const HEAP_SIZE: usize = 1024; + static mut HEAP_MEM: [MaybeUninit; HEAP_SIZE] = [MaybeUninit::uninit(); HEAP_SIZE]; + unsafe { HEAP.init(HEAP_MEM.as_ptr() as usize, HEAP_SIZE) } + } + let mut delay = cortex_m::delay::Delay::new(core.SYST, clocks.system_clock.freq().to_Hz()); let pins = bsp::Pins::new( @@ -100,18 +125,16 @@ fn main() -> ! { let mut lcd = hd44780::HD44780::new_4bit( pins.gpio16.into_push_pull_output(), //rs - pins.gpio17.into_push_pull_output(), // enable + pins.gpio17.into_push_pull_output(), //enable pins.gpio18.into_push_pull_output(), //d4 pins.gpio19.into_push_pull_output(), //d5 pins.gpio20.into_push_pull_output(), //d6 pins.gpio21.into_push_pull_output(), //d6 &mut delay, - ).unwrap(); - - lcd.reset(&mut delay).unwrap(); - lcd.clear(&mut delay).unwrap(); - lcd.write_str("lcd initalized", &mut delay).unwrap(); + ) + .unwrap(); + write_lcd(&mut lcd, &mut delay, "Starting"); loop { led_pin.set_high().unwrap(); external_led_pin.set_low().unwrap(); @@ -120,7 +143,10 @@ fn main() -> ! { external_led_pin.set_high().unwrap(); delay.delay_ms(500); match spi.read_thermocouple(&mut thermometer_spi_csn, Unit::Celsius) { - Ok(v) => writeln!(uart, "Temp: {} \r", v).unwrap(), + Ok(v) => { + writeln!(uart, "Current: {} \r", v).unwrap(); + write_lcd(&mut lcd, &mut delay, &format!("{:02.2} C", v)) + } Err(e) => defmt::error!("error reading temp {}", defmt::Debug2Format(&e)), }; } diff --git a/wiring.fzz b/wiring.fzz index c8fdb59..bf2ede4 100644 Binary files a/wiring.fzz and b/wiring.fzz differ