Compare commits

..

1 commit

Author SHA1 Message Date
6f56f584b6
feat: try and fail to make lcd work wiht another library 2025-05-13 16:41:21 +02:00
10 changed files with 159 additions and 612 deletions

View file

@ -4,6 +4,8 @@ target = "thumbv6m-none-eabi"
[target.thumbv6m-none-eabi]
runner = 'probe-rs run --chip STM32L053R8'
rustflags = ["-C", "link-arg=-Tlink.x", "-C", "link-arg=-Tdefmt.x"]
[alias]
cflash = "flash --chip STM32L053R8"

150
Cargo.lock generated
View file

@ -23,7 +23,7 @@ version = "0.2.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5deb64efa5bd81e31fcd1938615a6d98c82eafcbcd787162b6f63b91d6bac5b3"
dependencies = [
"rustc_version 0.2.3",
"rustc_version",
]
[[package]]
@ -58,26 +58,13 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "chrono"
version = "0.4.41"
version = "0.4.40"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
dependencies = [
"num-traits",
]
[[package]]
name = "compile-time"
version = "0.2.0"
source = "git+https://github.com/cscherrNT/compile-time-rs#085aecbf1d9fd593b112fcf1b218ec8a6841d1df"
dependencies = [
"once_cell",
"proc-macro2",
"quote",
"rustc_version 0.4.1",
"semver 1.0.26",
"time",
]
[[package]]
name = "cortex-m"
version = "0.7.7"
@ -168,15 +155,6 @@ dependencies = [
"defmt 1.0.1",
]
[[package]]
name = "deranged"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e"
dependencies = [
"powerfmt",
]
[[package]]
name = "embedded-hal"
version = "0.2.7"
@ -205,15 +183,6 @@ dependencies = [
"byteorder",
]
[[package]]
name = "hd44780-driver"
version = "0.4.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "aab2b13fdeaed7dde9133a57c28b2cbde4a8fc8c3196b5631428aad114857d3a"
dependencies = [
"embedded-hal",
]
[[package]]
name = "heapless"
version = "0.8.0"
@ -225,16 +194,10 @@ dependencies = [
]
[[package]]
name = "itoa"
version = "1.0.15"
name = "lcd"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4a5f13b858c8d314ee3e8f639011f7ccefe71f97f96e50151fb991f267928e2c"
[[package]]
name = "libc"
version = "0.2.172"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d750af042f7ef4f724306de029d18836c26c1765a54a6a3f094cbd23a7267ffa"
checksum = "971d8fd9a162c0bda137b341493cce134a2f55f3317b8533d992934bc523c325"
[[package]]
name = "nb"
@ -255,14 +218,12 @@ checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d"
name = "nucleo-l053r8-blink"
version = "0.1.0"
dependencies = [
"chrono",
"compile-time",
"cortex-m",
"cortex-m-rt",
"defmt 1.0.1",
"defmt-rtt",
"hd44780-driver",
"heapless",
"lcd",
"panic-halt",
"panic-probe",
"stm32l0xx-hal",
@ -290,12 +251,6 @@ dependencies = [
"num-traits",
]
[[package]]
name = "num-conv"
version = "0.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "51d515d32fb182ee37cda2ccdcb92950d6a3c2893aa280e540671c2cd0f3b1d9"
[[package]]
name = "num-integer"
version = "0.1.46"
@ -336,21 +291,6 @@ dependencies = [
"autocfg",
]
[[package]]
name = "num_threads"
version = "0.1.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5c7398b9c8b70908f6371f47ed36737907c87c52af34c268fed0bf0ceb92ead9"
dependencies = [
"libc",
]
[[package]]
name = "once_cell"
version = "1.21.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d"
[[package]]
name = "panic-halt"
version = "1.0.0"
@ -367,12 +307,6 @@ dependencies = [
"defmt 0.3.100",
]
[[package]]
name = "powerfmt"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "439ee305def115ba05938db6eb1644ff94165c5ab5e9420d1c1bcedbba909391"
[[package]]
name = "proc-macro-error-attr2"
version = "2.0.0"
@ -428,16 +362,7 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver 0.9.0",
]
[[package]]
name = "rustc_version"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfcb3a22ef46e85b45de6ee7e79d063319ebb6594faafcf1c225ea92ab6e9b92"
dependencies = [
"semver 1.0.26",
"semver",
]
[[package]]
@ -449,38 +374,12 @@ dependencies = [
"semver-parser",
]
[[package]]
name = "semver"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56e6fa9c48d24d85fb3de5ad847117517440f6beceb7798af16b4a87d616b8d0"
[[package]]
name = "semver-parser"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "388a1df253eca08550bef6c72392cfe7c30914bf41df5269b68cbd6ff8f570a3"
[[package]]
name = "serde"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f0e2c6ed6606019b4e29e69dbaba95b11854410e5347d525002456dbbb786b6"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.219"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5b0276cf7f2c73365f7157c8123c21cd9a50fbbd844757af28ca1f5925fc2a00"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "stable_deref_trait"
version = "1.2.0"
@ -548,39 +447,6 @@ dependencies = [
"syn",
]
[[package]]
name = "time"
version = "0.3.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a7619e19bc266e0f9c5e6686659d394bc57973859340060a69221e57dbc0c40"
dependencies = [
"deranged",
"itoa",
"libc",
"num-conv",
"num_threads",
"powerfmt",
"serde",
"time-core",
"time-macros",
]
[[package]]
name = "time-core"
version = "0.1.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9e9a38711f559d9e3ce1cdb06dd7c5b8ea546bc90052da6d06bb76da74bb07c"
[[package]]
name = "time-macros"
version = "0.2.22"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3526739392ec93fd8b359c8e98514cb3e8e021beb4e5f597b00a0221f8ed8a49"
dependencies = [
"num-conv",
"time-core",
]
[[package]]
name = "unicode-ident"
version = "1.0.18"

View file

@ -12,46 +12,8 @@ hal = { package = "stm32l0xx-hal", version = "0.10.0", features = [
cortex-m = { version = "0.7.7", features = ["critical-section-single-core"] }
cortex-m-rt = "0.7.5"
panic-halt = "1.0.0"
hd44780-driver = "0.4.0"
heapless = "0.8.0"
defmt = {version="1.0.1", optional= true}
defmt-rtt = {version="1.0.0", optional=true}
panic-probe = { version = "0.3", features = ["print-defmt"], optional=true }
compile-time = {git = "https://github.com/cscherrNT/compile-time-rs"}
chrono = { version = "0.4.41", default-features = false }
[profile.release]
debug = "full" # those are not on the board
[features]
logging = ["dep:defmt", "dep:defmt-rtt", "dep:panic-probe"]
[[example]]
name = "logging"
path = "examples/logging.rs"
required-features = ["logging"]
[[example]]
name = "rtc-log"
path = "examples/rtc-log.rs"
required-features = ["logging"]
[[example]]
name = "temperature"
path = "examples/temperature.rs"
required-features = ["logging"]
[[example]]
name = "aes_ecb"
path = "examples/aes_ecb.rs"
required-features = ["logging"]
[[example]]
name = "lcd"
path = "examples/lcd.rs"
required-features = ["logging"]
[[example]]
name = "lcd-clock"
path = "examples/lcd-clock.rs"
required-features = ["logging"]
defmt = "1.0.1"
defmt-rtt = "1.0.0"
panic-probe = { version = "0.3", features = ["print-defmt"] }
lcd = "0.4.1"

View file

@ -1,16 +0,0 @@
# Example Codes for the STM32 NUCLEO-L053R8
This repository contains many example programs that are written to run on the
NUCLEO-L053R8 Board.
## Running
Some programs require additional libraries for logging/formatting that would
make the more basic ones not compile (or rather link) correctly. For those,
enable the `logging` feature.
```bash
cargo run --example=blinky
cargo run --example=rtc-log --features=logging
# etc
```

View file

@ -1,8 +0,0 @@
fn main() {
println!("cargo::rerun-if-changed=Cargo.toml");
println!("cargo::rustc-link-arg=-Tlink.x");
#[cfg(feature = "logging")]
{
println!("cargo::rustc-link-arg=-Tdefmt.x");
}
}

View file

@ -1,5 +1,6 @@
#![no_main]
#![no_std]
extern crate panic_halt;
use cortex_m_rt::entry;
@ -7,27 +8,27 @@ use hal::{pac, prelude::*, rcc::Config};
#[entry]
fn main() -> ! {
// get access to the peripherals
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
// configure the clock
// Configure the clock.
let mut rcc = dp.RCC.freeze(Config::hsi16());
// get access to GPIO Port A
// Acquire the GPIOA peripheral. This also enables the clock for GPIOA in
// the RCC register.
let gpioa = dp.GPIOA.split(&mut rcc);
// configure Pin 5 og GPIO Port A as output
// Configure PA5 as output.
let mut led = gpioa.pa5.into_push_pull_output();
// prepare delays (sleeping)
// Get the delay provider.
let mut delay = cp.SYST.delay(rcc.clocks);
loop {
led.set_high().unwrap(); // light on
delay.delay_ms(500_u16); // wait
led.set_high().unwrap();
delay.delay_ms(500_u16);
led.set_low().unwrap(); // light off
delay.delay_ms(500_u16); // wait
led.set_low().unwrap();
delay.delay_ms(500_u16);
}
}

View file

@ -1,161 +0,0 @@
#![no_main]
#![no_std]
use heapless::{String, Vec};
use panic_probe as _;
use defmt_rtt as _; // global logger
use cortex_m_rt::entry;
use hal::{
delay::Delay,
gpio::{Output, PushPull, gpioa::*, gpiob::*, gpioc::*},
pac,
prelude::*,
rcc::Config,
};
use hd44780_driver::HD44780;
#[defmt::panic_handler]
fn panic() -> ! {
cortex_m::asm::udf()
}
type Lcd = HD44780<
hd44780_driver::bus::FourBitBus<
PA9<Output<PushPull>>,
PC7<Output<PushPull>>,
PB5<Output<PushPull>>,
PB4<Output<PushPull>>,
PB10<Output<PushPull>>,
PA8<Output<PushPull>>,
>,
>;
const FPS: u32 = 12;
const LINES: usize = 4;
const CHARS: usize = 20;
const SIGNS_LEN: usize = 7;
const SIGNS: [char; SIGNS_LEN] = ['N', 'e', 'w', 'T', 'e', 'c', ' '];
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.freeze(Config::hsi16());
let gpioa = dp.GPIOA.split(&mut rcc);
let gpiob = dp.GPIOB.split(&mut rcc);
let gpioc = dp.GPIOC.split(&mut rcc);
// 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 D9
let en = gpioc.pc7.into_push_pull_output();
// register select on D8, the lib wants that but I'd just put it on ground otherwise
let rs = gpioa.pa9.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: 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::Invisible,
cursor_blink: hd44780_driver::CursorBlink::Off,
display: hd44780_driver::Display::On,
},
&mut delay,
)
.expect("could not set display properties");
lcd.reset(&mut delay).expect("could not reset the lcd");
let mut i: usize = 0;
let mut buf: Vec<String<CHARS>, LINES> = Vec::new();
loop {
led.set_high().unwrap();
reset_buf(&mut buf);
animation(&mut buf, i);
display(&buf, &mut lcd, &mut delay, i % LINES);
led.set_low().unwrap();
delay.delay_us(1_000_000 / FPS);
i += 1;
}
}
fn display<const LINES: usize, const CHARS: usize>(
buf: &Vec<String<CHARS>, LINES>,
lcd: &mut Lcd,
delay: &mut Delay,
which: usize,
) {
match which {
0 => {
lcd.set_cursor_pos(0, delay)
.expect("could not set cursor pos");
lcd.write_str(&buf[0], delay)
.expect("could not display string");
}
1 => {
lcd.set_cursor_pos(60, delay)
.expect("could not set cursor pos");
lcd.write_str(&buf[1], delay)
.expect("could not display string");
}
2 => {
lcd.set_cursor_pos(20, delay)
.expect("could not set cursor pos");
lcd.write_str(&buf[2], delay)
.expect("could not display string");
}
3 => {
// line 4 is a bit weird and needs some offset
let mut tmp: String<30> = String::new();
tmp.push_str(" ").unwrap();
tmp.push_str(&buf[3]).unwrap();
lcd.set_cursor_pos(80, delay)
.expect("could not set cursor pos");
lcd.write_str(&tmp, delay)
.expect("could not display string");
}
_ => unreachable!(),
}
}
fn animation<const LINES: usize, const CHARS: usize>(
buf: &mut Vec<String<CHARS>, LINES>,
frame: usize,
) {
for i in 0..CHARS {
for (bi, buf) in buf.iter_mut().enumerate() {
match buf.push(SIGNS[(i + bi * 2 + frame) % SIGNS_LEN]) {
Ok(_) => (),
Err(_e) => {
panic!("Could not push string in animation. i={}, bi={}", i, bi);
}
}
}
}
}
fn reset_buf<const LINES: usize, const CHARS: usize>(buf: &mut Vec<String<CHARS>, LINES>) {
buf.clear();
for _ in 0..LINES {
buf.push(String::new()).unwrap();
}
}

View file

@ -1,162 +0,0 @@
#![no_main]
#![no_std]
use chrono::NaiveDateTime;
use defmt::info;
use heapless::String;
use panic_probe as _;
use defmt_rtt as _; // global logger
use core::fmt::Write;
use cortex_m_rt::entry;
use hal::{
delay::Delay,
gpio::{Output, PushPull, gpioa::*, gpiob::*, gpioc::*},
pac,
prelude::*,
pwr::PWR,
rcc::Config,
rtc::{Datelike, Rtc, Timelike},
};
use hd44780_driver::HD44780;
#[defmt::panic_handler]
fn panic() -> ! {
cortex_m::asm::udf()
}
type Lcd = HD44780<
hd44780_driver::bus::FourBitBus<
PA9<Output<PushPull>>,
PC7<Output<PushPull>>,
PB5<Output<PushPull>>,
PB4<Output<PushPull>>,
PB10<Output<PushPull>>,
PA8<Output<PushPull>>,
>,
>;
const UNIXTIME_OF_COMPILATION: i64 = compile_time::unix_local!();
const BUF_SIZE: usize = 20;
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.freeze(Config::hsi16());
let pwr = PWR::new(dp.PWR, &mut rcc);
let gpioa = dp.GPIOA.split(&mut rcc);
let gpiob = dp.GPIOB.split(&mut rcc);
let gpioc = dp.GPIOC.split(&mut rcc);
// 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 D9
let en = gpioc.pc7.into_push_pull_output();
// register select on D8, the lib wants that but I'd just put it on ground otherwise
let rs = gpioa.pa9.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 buf: String<BUF_SIZE> = String::new();
let mut lcd: 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::Invisible,
cursor_blink: hd44780_driver::CursorBlink::Off,
display: hd44780_driver::Display::On,
},
&mut delay,
)
.expect("could not set display properties");
lcd.reset(&mut delay).expect("could not reset the lcd");
let start_time = chrono::DateTime::from_timestamp(UNIXTIME_OF_COMPILATION, 0)
.expect("The Compilation time was invalid")
.naive_utc();
info!("Compiled time: {}", UNIXTIME_OF_COMPILATION);
let mut rtc = Rtc::new(dp.RTC, &mut rcc, &pwr, Some(start_time)).expect("Could not setup RTC");
let mut i: u32 = 0;
let mut timestamp = rtc.now();
info!("First RTC time: {}", timestamp.and_utc().timestamp());
loop {
led.set_high().unwrap();
timestamp = rtc.now();
display_time(
&timestamp,
&mut lcd,
&mut delay,
&mut buf,
timestamp.second() == 0 || i == 0,
);
led.set_low().unwrap();
delay.delay_us(990_u32);
i += 1;
}
}
fn display_time(
timestamp: &NaiveDateTime,
lcd: &mut Lcd,
delay: &mut Delay,
buf: &mut String<BUF_SIZE>,
full_update: bool,
) {
if full_update {
lcd.clear(delay).expect("could not clear the display");
lcd.set_cursor_pos(0, delay)
.expect("could not move cursor to start");
buf.clear();
write!(
buf,
" {:04}-{:02}-{:02} ",
timestamp.year(),
timestamp.month(),
timestamp.day()
)
.expect("could not format text content for display");
lcd.write_str(buf, delay).expect("could not write to LCD");
lcd.set_cursor_pos(40, delay)
.expect("could not move cursor to start");
buf.clear();
write!(buf, " (UTC) ",).expect("could not format text content for display");
lcd.write_str(buf, delay).expect("could not write to LCD");
}
lcd.set_cursor_pos(20, delay)
.expect("could not move cursor to line 2");
buf.clear();
write!(
buf,
" {:02}:{:02}:{:02} ",
timestamp.hour(),
timestamp.minute(),
timestamp.second()
)
.expect("could not format text content for display");
lcd.write_str(buf, delay).expect("could not write to LCD");
}

View file

@ -1,8 +1,15 @@
#![no_main]
#![no_std]
use defmt::info;
use heapless::String;
use defmt::{debug, info};
use hal::{
delay::Delay,
gpio::{
Output, PushPull,
gpioa::{PA8, PA10},
gpiob::{PB3, PB4, PB5, PB10},
},
};
use panic_probe as _;
use defmt_rtt as _; // global logger
@ -11,7 +18,108 @@ use core::fmt::Write;
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config};
use hd44780_driver::HD44780;
pub type D2PIN = PA10<Output<PushPull>>;
pub type D3PIN = PB3<Output<PushPull>>;
pub type D4PIN = PB5<Output<PushPull>>;
pub type D5PIN = PB4<Output<PushPull>>;
pub type D6PIN = PB10<Output<PushPull>>;
pub type D7PIN = PA8<Output<PushPull>>;
struct LCDHardware<'a> {
d4: &'a mut PB5<Output<PushPull>>,
d5: &'a mut PB4<Output<PushPull>>,
d6: &'a mut PB10<Output<PushPull>>,
d7: &'a mut PA8<Output<PushPull>>,
rs: &'a mut PA10<Output<PushPull>>,
en: &'a mut PB3<Output<PushPull>>,
delay: &'a mut Delay,
}
impl<'a> LCDHardware<'a> {
fn new(
d4: &'a mut D4PIN,
d5: &'a mut D5PIN,
d6: &'a mut D6PIN,
d7: &'a mut D7PIN,
rs: &'a mut D2PIN,
en: &'a mut D3PIN,
delay: &'a mut Delay,
) -> lcd::Display<LCDHardware<'a>> {
let hw: LCDHardware<'a> = LCDHardware {
d4,
d5,
d6,
d7,
rs,
en,
delay,
};
let mut lcd = lcd::Display::new(hw);
lcd.init(lcd::FunctionLine::Line2, lcd::FunctionDots::Dots5x8);
lcd.display(
lcd::DisplayMode::DisplayOn,
lcd::DisplayCursor::CursorOn,
lcd::DisplayBlink::BlinkOn,
);
lcd.entry_mode(
lcd::EntryModeDirection::EntryRight,
lcd::EntryModeShift::NoShift,
);
lcd
}
}
impl lcd::Delay for LCDHardware<'_> {
fn delay_us(&mut self, delay_usec: u32) {
self.delay.delay_us(delay_usec);
}
}
impl lcd::Hardware for LCDHardware<'_> {
fn rs(&mut self, bit: bool) {
self.rs
.set_state(PinState::from(bit))
.expect("could not set LCD RS")
}
fn enable(&mut self, bit: bool) {
self.en
.set_state(PinState::from(bit))
.expect("could not set LCD RS")
}
fn data(&mut self, data: u8) {
let d4 = data & 0b00000001;
let d5 = data & 0b00000010;
let d6 = data & 0b00000100;
let d7 = data & 0b00001000;
debug!("Writing data: d4:{}, d5:{}, d6:{}, d7:{}", d4, d5, d6, d7);
self.d4
.set_state(PinState::from(d4 > 0))
.expect("could not set LCD D4");
self.d5
.set_state(PinState::from(d5 > 0))
.expect("could not set LCD D5");
self.d6
.set_state(PinState::from(d6 > 0))
.expect("could not set LCD D6");
self.d7
.set_state(PinState::from(d7 > 0))
.expect("could not set LCD D7");
}
fn mode(&self) -> lcd::FunctionMode {
lcd::FunctionMode::Bit4
}
fn can_read(&self) -> bool {
false
}
}
#[defmt::panic_handler]
fn panic() -> ! {
@ -27,19 +135,17 @@ fn main() -> ! {
let gpioa = dp.GPIOA.split(&mut rcc);
let gpiob = dp.GPIOB.split(&mut rcc);
let gpioc = dp.GPIOC.split(&mut rcc);
let _gpioc = dp.GPIOC.split(&mut rcc);
// 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 D2
let mut rs: D2PIN = gpioa.pa10.into_push_pull_output();
// register select on D3, the lib wants that but I'd just put it on ground otherwise
let mut en: D3PIN = gpiob.pb3.into_push_pull_output();
// clock enable on D9
let en = gpioc.pc7.into_push_pull_output();
// register select on D8, the lib wants that but I'd just put it on ground otherwise
let rs = gpioa.pa9.into_push_pull_output();
let mut d4: D4PIN = gpiob.pb5.into_push_pull_output();
let mut d5: D5PIN = gpiob.pb4.into_push_pull_output();
let mut d6: D6PIN = gpiob.pb10.into_push_pull_output();
let mut d7: D7PIN = gpioa.pa8.into_push_pull_output();
// See https://en.wikipedia.org/wiki/Hitachi_HD44780_LCD_controller#Interface
// for the pins of the LCD
@ -47,57 +153,18 @@ fn main() -> ! {
let mut delay = cp.SYST.delay(rcc.clocks);
let mut led = gpioa.pa5.into_push_pull_output();
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::Invisible,
cursor_blink: hd44780_driver::CursorBlink::Off,
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");
led.set_high().unwrap();
info!("Writing to LCD...");
let mut lcd = LCDHardware::new(
&mut d4, &mut d5, &mut d6, &mut d7, &mut rs, &mut en, &mut delay,
);
write!(&mut lcd, "Hello World").expect("could not write to lcd");
info!("Done!");
led.set_low().unwrap();
let mut i = 0;
let mut buf: String<20> = String::new();
loop {
led.set_high().unwrap();
info!("Writing to LCD...");
lcd.clear(&mut delay).expect("could not clear the display");
lcd.set_cursor_pos(0, &mut delay)
.expect("could not move cursor to start");
lcd.write_str(" Hello world! ", &mut delay)
.expect("could not write to LCD");
lcd.set_cursor_pos(20, &mut delay)
.expect("could not move cursor to line 2");
buf.clear();
write!(&mut buf, " Iteration: {i:07} ").expect("could not format text content for display");
lcd.write_str(&buf, &mut delay)
.expect("could not write to LCD");
lcd.set_cursor_pos(60, &mut delay)
.expect("could not move cursor to start");
lcd.write_str(" Line 2 ", &mut delay)
.expect("could not write to LCD");
// line 4 is a bit strange and eats the first few spaces
lcd.set_cursor_pos(80, &mut delay)
.expect("could not move cursor to start");
lcd.write_str(" Line 4 ", &mut delay)
.expect("could not write to LCD");
info!("Done!");
led.set_low().unwrap();
delay.delay_ms(500_u16);
i += 1;

View file

@ -25,24 +25,20 @@ fn main() -> ! {
let mut delay = cp.SYST.delay(rcc.clocks);
// Setup the Real-Time-Clock of the Controller.
// starts at 0 (2001-01-01 00:00:00) and resets
// when the Controller is no longer powered.
let mut rtc = Rtc::new(dp.RTC, &mut rcc, &pwr, None).unwrap();
let mut rtc = Rtc::new(dp.RTC, &mut rcc, &pwr, None).unwrap(); // starts at time 0
#[allow(clippy::never_loop)]
loop {
led.set_high().unwrap(); // light on
delay.delay_ms(500_u16); // wait
led.set_high().unwrap();
delay.delay_ms(500_u16);
led.set_low().unwrap(); // light off
delay.delay_ms(500_u16); // wait
led.set_low().unwrap();
delay.delay_ms(500_u16);
// print the current time from the RTC
ptime(&mut rtc);
}
}
/// prints the time to the "host" computer via RTT
fn ptime(rtc: &mut Rtc) {
let time: NaiveDateTime = rtc.now();
info!(