Compare commits
10 commits
fix/lcd-di
...
master
Author | SHA1 | Date | |
---|---|---|---|
be26f8f688 | |||
87a8b04c35 | |||
93abdfb521 | |||
41736b1d23 | |||
d1f9420703 | |||
b56c8709d7 | |||
0b1c72e345 | |||
2794f7ec8a | |||
d24370dd03 | |||
69d2c65076 |
10 changed files with 577 additions and 40 deletions
|
@ -4,8 +4,6 @@ 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"
|
||||
|
|
139
Cargo.lock
generated
139
Cargo.lock
generated
|
@ -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"
|
||||
|
|
44
Cargo.toml
44
Cargo.toml
|
@ -14,6 +14,44 @@ cortex-m-rt = "0.7.5"
|
|||
panic-halt = "1.0.0"
|
||||
hd44780-driver = "0.4.0"
|
||||
heapless = "0.8.0"
|
||||
defmt = "1.0.1"
|
||||
defmt-rtt = "1.0.0"
|
||||
panic-probe = { version = "0.3", features = ["print-defmt"] }
|
||||
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"]
|
||||
|
|
16
README.md
Executable file
16
README.md
Executable file
|
@ -0,0 +1,16 @@
|
|||
# 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
|
||||
```
|
8
build.rs
Executable file
8
build.rs
Executable file
|
@ -0,0 +1,8 @@
|
|||
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");
|
||||
}
|
||||
}
|
|
@ -1,6 +1,5 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
extern crate panic_halt;
|
||||
|
||||
use cortex_m_rt::entry;
|
||||
|
@ -8,27 +7,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());
|
||||
|
||||
// Acquire the GPIOA peripheral. This also enables the clock for GPIOA in
|
||||
// the RCC register.
|
||||
// get access to GPIO Port A
|
||||
let gpioa = dp.GPIOA.split(&mut rcc);
|
||||
|
||||
// Configure PA5 as output.
|
||||
// configure Pin 5 og GPIO Port A as output
|
||||
let mut led = gpioa.pa5.into_push_pull_output();
|
||||
|
||||
// Get the delay provider.
|
||||
// prepare delays (sleeping)
|
||||
let mut delay = cp.SYST.delay(rcc.clocks);
|
||||
|
||||
loop {
|
||||
led.set_high().unwrap();
|
||||
delay.delay_ms(500_u16);
|
||||
led.set_high().unwrap(); // light on
|
||||
delay.delay_ms(500_u16); // wait
|
||||
|
||||
led.set_low().unwrap();
|
||||
delay.delay_ms(500_u16);
|
||||
led.set_low().unwrap(); // light off
|
||||
delay.delay_ms(500_u16); // wait
|
||||
}
|
||||
}
|
||||
|
|
161
examples/lcd-animate.rs
Executable file
161
examples/lcd-animate.rs
Executable 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
162
examples/lcd-clock.rs
Executable 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(
|
||||
×tamp,
|
||||
&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");
|
||||
}
|
|
@ -1,8 +1,8 @@
|
|||
#![no_main]
|
||||
#![no_std]
|
||||
|
||||
use defmt::{debug, error, info, println, trace, warn};
|
||||
use hal::pwm::Pin;
|
||||
use defmt::info;
|
||||
use heapless::String;
|
||||
use panic_probe as _;
|
||||
|
||||
use defmt_rtt as _; // global logger
|
||||
|
@ -12,7 +12,6 @@ use core::fmt::Write;
|
|||
use cortex_m_rt::entry;
|
||||
use hal::{pac, prelude::*, rcc::Config};
|
||||
use hd44780_driver::HD44780;
|
||||
use heapless::Vec;
|
||||
|
||||
#[defmt::panic_handler]
|
||||
fn panic() -> ! {
|
||||
|
@ -30,10 +29,6 @@ fn main() -> ! {
|
|||
let gpiob = dp.GPIOB.split(&mut rcc);
|
||||
let gpioc = dp.GPIOC.split(&mut rcc);
|
||||
|
||||
// contrast over A0 analog port
|
||||
// analog output seems to not be supported somehow?
|
||||
let mut vO = gpioa.pa0.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();
|
||||
|
@ -41,10 +36,10 @@ fn main() -> ! {
|
|||
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();
|
||||
// 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
|
||||
|
@ -56,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,
|
||||
|
@ -68,11 +63,36 @@ fn main() -> ! {
|
|||
.expect("could not write to LCD");
|
||||
|
||||
let mut i = 0;
|
||||
let mut buf: String<20> = String::new();
|
||||
loop {
|
||||
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)
|
||||
|
||||
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!");
|
||||
|
|
|
@ -25,20 +25,24 @@ fn main() -> ! {
|
|||
|
||||
let mut delay = cp.SYST.delay(rcc.clocks);
|
||||
|
||||
let mut rtc = Rtc::new(dp.RTC, &mut rcc, &pwr, None).unwrap(); // starts at time 0
|
||||
// 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();
|
||||
|
||||
#[allow(clippy::never_loop)]
|
||||
loop {
|
||||
led.set_high().unwrap();
|
||||
delay.delay_ms(500_u16);
|
||||
led.set_high().unwrap(); // light on
|
||||
delay.delay_ms(500_u16); // wait
|
||||
|
||||
led.set_low().unwrap();
|
||||
delay.delay_ms(500_u16);
|
||||
led.set_low().unwrap(); // light off
|
||||
delay.delay_ms(500_u16); // wait
|
||||
|
||||
// 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!(
|
||||
|
|
Loading…
Add table
Reference in a new issue