Compare commits

...

5 commits

5 changed files with 503 additions and 7 deletions

139
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",
"rustc_version 0.2.3",
]
[[package]]
@ -58,13 +58,26 @@ checksum = "37b2a672a2cb129a2e41c10b1224bb368f9f37a2b16b612598138befd7b37eb5"
[[package]]
name = "chrono"
version = "0.4.40"
version = "0.4.41"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a7964611d71df112cb1730f2ee67324fcf4d0fc6606acbbe9bfe06df124637c"
checksum = "c469d952047f47f91b68d1cba3f10d63c11d73e4636f24f08daf0278abf01c4d"
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"
@ -155,6 +168,15 @@ 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"
@ -202,6 +224,18 @@ dependencies = [
"stable_deref_trait",
]
[[package]]
name = "itoa"
version = "1.0.15"
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"
[[package]]
name = "nb"
version = "0.1.3"
@ -221,6 +255,8 @@ checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d"
name = "nucleo-l053r8-blink"
version = "0.1.0"
dependencies = [
"chrono",
"compile-time",
"cortex-m",
"cortex-m-rt",
"defmt 1.0.1",
@ -254,6 +290,12 @@ 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"
@ -294,6 +336,21 @@ 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"
@ -310,6 +367,12 @@ 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"
@ -365,7 +428,16 @@ version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "138e3e0acb6c9fb258b19b67cb8abd63c00679d2851805ea151465464fe9030a"
dependencies = [
"semver",
"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",
]
[[package]]
@ -377,12 +449,38 @@ 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"
@ -450,6 +548,39 @@ 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

@ -17,6 +17,11 @@ 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"]
@ -40,3 +45,13 @@ required-features = ["logging"]
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"]

161
examples/lcd-animate.rs Executable file
View file

@ -0,0 +1,161 @@
#![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();
}
}

162
examples/lcd-clock.rs Executable file
View file

@ -0,0 +1,162 @@
#![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

@ -2,6 +2,7 @@
#![no_std]
use defmt::info;
use heapless::String;
use panic_probe as _;
use defmt_rtt as _; // global logger
@ -50,8 +51,8 @@ fn main() -> ! {
.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,
cursor_visibility: hd44780_driver::Cursor::Invisible,
cursor_blink: hd44780_driver::CursorBlink::Off,
display: hd44780_driver::Display::On,
},
&mut delay,
@ -62,10 +63,36 @@ fn main() -> ! {
.expect("could not write to LCD");
let mut i = 0;
let mut buf: String<20> = String::new();
loop {
led.set_high().unwrap();
info!("Writing to LCD...");
lcd.write_str("Hello world!\nGoing on", &mut delay)
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!");