diff --git a/examples/lcd.rs b/examples/lcd.rs index 8a8fc66..e25e3a1 100755 --- a/examples/lcd.rs +++ b/examples/lcd.rs @@ -1,7 +1,11 @@ #![no_main] #![no_std] -extern crate panic_halt; +use defmt::{debug, error, info, println, trace, warn}; +use hal::pwm::Pin; +use panic_probe as _; + +use defmt_rtt as _; // global logger use core::fmt::Write; @@ -10,6 +14,11 @@ use hal::{pac, prelude::*, rcc::Config}; use hd44780_driver::HD44780; use heapless::Vec; +#[defmt::panic_handler] +fn panic() -> ! { + cortex_m::asm::udf() +} + #[entry] fn main() -> ! { let dp = pac::Peripherals::take().unwrap(); @@ -18,37 +27,56 @@ fn main() -> ! { let mut rcc = dp.RCC.freeze(Config::hsi16()); let gpioa = dp.GPIOA.split(&mut rcc); - let _gpiob = dp.GPIOB.split(&mut rcc); + let gpiob = dp.GPIOB.split(&mut rcc); let gpioc = dp.GPIOC.split(&mut rcc); - let d0 = gpioc.pc0.into_push_pull_output(); - let d1 = gpioc.pc1.into_push_pull_output(); - let d2 = gpioc.pc2.into_push_pull_output(); - let d3 = gpioc.pc3.into_push_pull_output(); - let d4 = gpioc.pc4.into_push_pull_output(); - let d5 = gpioc.pc5.into_push_pull_output(); - let d6 = gpioc.pc6.into_push_pull_output(); - let d7 = gpioc.pc7.into_push_pull_output(); + // contrast over A0 analog port + // analog output seems to not be supported somehow? + let mut vO = gpioa.pa0.into_push_pull_output(); - let rs = gpioc.pc8.into_push_pull_output(); - let en = gpioc.pc9.into_push_pull_output(); + // literal D4-D7 ports etc as written on the nucleo board, mapped to the D4-D7 ports of the LCD + // controller + let d4 = gpiob.pb5.into_push_pull_output(); + let d5 = gpiob.pb4.into_push_pull_output(); + let d6 = gpiob.pb10.into_push_pull_output(); + let d7 = gpioa.pa8.into_push_pull_output(); + + // clock enable on D8 + let en = gpioa.pa9.into_push_pull_output(); + // register select on D9, the lib wants that but I'd just put it on ground otherwise + let rs = gpioc.pc7.into_push_pull_output(); + + // See https://en.wikipedia.org/wiki/Hitachi_HD44780_LCD_controller#Interface + // for the pins of the LCD let mut delay = cp.SYST.delay(rcc.clocks); let mut led = gpioa.pa5.into_push_pull_output(); - let mut lcd = HD44780::new_8bit(rs, en, d0, d1, d2, d3, d4, d5, d6, d7, &mut delay).unwrap(); + let mut lcd = HD44780::new_4bit(rs, en, d4, d5, d6, d7, &mut delay) + .expect("could not init HD44780 driver"); + lcd.set_display_mode( + hd44780_driver::DisplayMode { + cursor_visibility: hd44780_driver::Cursor::Visible, + cursor_blink: hd44780_driver::CursorBlink::On, + display: hd44780_driver::Display::On, + }, + &mut delay, + ) + .expect("could not set display properties"); + lcd.reset(&mut delay).expect("could not reset the lcd"); + lcd.write_str("Hello world!", &mut delay) + .expect("could not write to LCD"); let mut i = 0; - let mut buf: Vec = Vec::new(); loop { - lcd.write_str("Hello world!", &mut delay).unwrap(); + vO.set_low().expect("set contrast low boom?"); led.set_high().unwrap(); + info!("Writing to LCD..."); + lcd.write_str("Hello world!\nGoing on", &mut delay) + .expect("could not write to LCD"); - delay.delay_ms(500_u16); - + info!("Done!"); led.set_low().unwrap(); - write!(buf, "hello: {i}").unwrap(); - lcd.write_bytes(&buf, &mut delay).unwrap(); delay.delay_ms(500_u16);