refactor!: fork to new project *crcbench*

This commit is contained in:
cscherr 2025-07-09 14:03:42 +02:00
parent be26f8f688
commit ab9a5a4a55
Signed by: cscherrNT
GPG key ID: 8E2B45BC51A27EA7
16 changed files with 9 additions and 894 deletions

2
Cargo.lock generated
View file

@ -252,7 +252,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d5439c4ad607c3c23abf66de8c8bf57ba8adcd1f129e699851a6e43935d339d"
[[package]]
name = "nucleo-l053r8-blink"
name = "nucleo-l053r8-crcbench"
version = "0.1.0"
dependencies = [
"chrono",

View file

@ -1,5 +1,5 @@
[package]
name = "nucleo-l053r8-blink"
name = "nucleo-l053r8-crcbench"
version = "0.1.0"
edition = "2024"
@ -14,44 +14,15 @@ 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"}
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]
default = ["logging"]
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"]

View file

@ -1,47 +0,0 @@
#![no_main]
#![no_std]
use defmt::{debug, info};
use hal::pwr::PWR;
use hal::rtc::{Datelike, NaiveDateTime, Rtc, Timelike};
use panic_probe as _;
use defmt_rtt as _; // global logger
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config};
const AES_PT: [u8; 16] = [
0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA,
];
const AES_KEY: [u32; 4] = [0x1991, 0x1991, 0xAAAAAAAA, 0xBBBBBBBB];
#[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 mut delay = cp.SYST.delay(rcc.clocks);
// WARN: Make sure your chip has AES
panic!("The chip does not have an AES unit >:");
let aes = hal::aes::AES::new(dp.AES, &mut rcc);
let mut ecb_stream = aes.enable(<dyn hal::aes::Mode>::ecb_encrypt(), AES_KEY);
let mut encbuf: [[u8; 16]; 4] = [[0; 16]; 4];
let mut i = 0;
debug!("Entering Loop");
#[allow(clippy::never_loop)]
loop {
debug!("reading from aes stream");
encbuf[i % 4] = ecb_stream.process(&AES_PT).unwrap();
info!("encbuf[{:02}]: {:02x}", i, encbuf);
if i > 100 {
delay.delay_ms(200_u16);
}
i += 1;
}
}

View file

@ -1,46 +0,0 @@
#![no_main]
#![no_std]
use defmt_rtt as _; // global logger
//
use panic_probe as _;
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config};
#[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 mut builtin_led = gpioa.pa5.into_push_pull_output();
let mut led0 = gpiob.pb5.into_push_pull_output(); // D4
let mut led1 = gpiob.pb4.into_push_pull_output(); // D5
let mut delay = cp.SYST.delay(rcc.clocks);
builtin_led.set_high().unwrap();
led0.set_high().unwrap();
led1.set_high().unwrap();
loop {
builtin_led.set_high().unwrap();
led0.set_low().unwrap();
led1.set_low().unwrap();
delay.delay_ms(100_u16);
led0.set_high().unwrap();
delay.delay_ms(100_u16);
builtin_led.set_low().unwrap();
delay.delay_ms(100_u16);
led0.set_low().unwrap();
led1.set_high().unwrap();
delay.delay_ms(100_u16);
}
}

View file

@ -1,36 +0,0 @@
#![no_main]
#![no_std]
extern crate panic_halt;
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
// Configure the clock.
let mut rcc = dp.RCC.freeze(Config::hsi16());
let gpioa = dp.GPIOA.split(&mut rcc);
let gpioc = dp.GPIOC.split(&mut rcc);
// Configure PA5 as output.
let mut led = gpioa.pa5.into_push_pull_output();
let mut button = gpioc.pc13.into_pull_down_input();
// Get the delay provider.
let mut delay = cp.SYST.delay(rcc.clocks);
loop {
if button.is_low().expect("button.is_low failed") {
led.set_high().unwrap();
} else {
led.set_low().unwrap();
}
delay.delay_ms(10_u16);
}
}

View file

@ -1,45 +0,0 @@
#![no_main]
#![no_std]
extern crate panic_halt;
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
// Configure the clock.
let mut rcc = dp.RCC.freeze(Config::hsi16());
let gpioa = dp.GPIOA.split(&mut rcc);
let gpioc = dp.GPIOC.split(&mut rcc);
// Configure PA5 as output.
let mut led = gpioa.pa5.into_push_pull_output();
let button = gpioc.pc13.into_pull_down_input();
// Get the delay provider.
let mut _delay = cp.SYST.delay(rcc.clocks);
let mut is_on: bool;
let mut was_on: bool = false;
#[allow(unused_variables)] // it is used later??
let mut enable_led: bool = false;
loop {
is_on = button.is_low().unwrap();
if is_on != was_on {
enable_led ^= true;
}
if is_on {
led.set_high().unwrap();
} else {
led.set_low().unwrap();
}
was_on = is_on;
}
}

View file

@ -1,33 +0,0 @@
#![no_main]
#![no_std]
extern crate panic_halt;
use cortex_m_rt::entry;
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
let mut rcc = dp.RCC.freeze(Config::hsi16());
// get access to GPIO Port A
let gpioa = dp.GPIOA.split(&mut rcc);
// configure Pin 5 og GPIO Port A as output
let mut led = gpioa.pa5.into_push_pull_output();
// prepare delays (sleeping)
let mut delay = cp.SYST.delay(rcc.clocks);
loop {
led.set_high().unwrap(); // light on
delay.delay_ms(500_u16); // wait
led.set_low().unwrap(); // light off
delay.delay_ms(500_u16); // wait
}
}

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,105 +0,0 @@
#![no_main]
#![no_std]
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::{pac, prelude::*, rcc::Config};
use hd44780_driver::HD44780;
#[defmt::panic_handler]
fn panic() -> ! {
cortex_m::asm::udf()
}
#[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 = 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");
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

@ -1,57 +0,0 @@
#![no_main]
#![no_std]
use defmt::info;
use hal::pwr::PWR;
use hal::rtc::{Datelike, NaiveDateTime, Rtc, Timelike};
use panic_probe as _;
use defmt_rtt as _; // global logger
use cortex_m_rt::entry;
use hal::{pac, prelude::*, rcc::Config};
#[entry]
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
// Configure the clock.
let mut rcc = dp.RCC.freeze(Config::hsi16());
let pwr = PWR::new(dp.PWR, &mut rcc);
let gpioa = dp.GPIOA.split(&mut rcc);
let mut led = gpioa.pa5.into_push_pull_output();
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();
loop {
led.set_high().unwrap(); // light on
delay.delay_ms(500_u16); // wait
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!(
"Time: {:04}-{:02}-{:02} {:02}:{:02}:{:02}",
time.year(),
time.month(),
time.day(),
time.hour(),
time.minute(),
time.second()
)
}

View file

@ -1,104 +0,0 @@
#![cfg_attr(not(test), no_main)]
#![cfg_attr(not(test), no_std)]
#[cfg(not(test))]
extern crate panic_halt;
use hal::adc::{Adc, Ready, VRef, VTemp};
use hal::calibration::{VtempCal30, VtempCal130};
use hal::{pac, prelude::*, rcc::Config};
use defmt::{debug, info};
use defmt_rtt as _; // global logger
#[cfg_attr(not(test), cortex_m_rt::entry)] // this is the entrypoint unless testing
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
let mut rcc = dp.RCC.freeze(Config::hsi16());
let mut adc: Adc<_> = dp.ADC.constrain(&mut rcc);
let mut delay = cp.SYST.delay(rcc.clocks);
// NOTE: TSEN bit must be enabled for reading the temperature
VTemp.enable(&mut adc);
VRef.enable(&mut adc);
// reference temperatures from the chips readonly memory
// [Source](https://www.st.com/resource/en/datasheet/stm32l053r8.pdf),
// Table 6 in Secion 3.13 "Temperature sensor"
//
// More and better info in the large 1000+ page sheet "Ultra-low-power
// STM32L0x3 advanced Arm®-based 32-bit MCUs" (RM0367), 14.9
//
// This is basically calibration data
info!(
"reading calibration data... If this is the last thing you hear from me something has gone terribly wrong"
);
let vref_cal = hal::calibration::VrefintCal::get().read();
let tsense_cal1 = (30, VtempCal30::get().read());
let tsense_cal2 = (130, VtempCal130::get().read());
info!("tsense_cal1: {:?}", (30, tsense_cal1));
info!("tsense_cal2: {:?}", (130, tsense_cal2));
// read a few values into void, maybe this will help get that thing started
for _ in 0..20 {
let _ = read_temp_mv(&mut adc, 1.0);
delay.delay_ms(10_u16);
}
let vref_actual: u16 = adc.read(&mut VRef).unwrap();
let vref_factor = vref_cal as f32 / vref_actual as f32;
info!(
"vref actual={} calibration={} => factor={}",
vref_actual, vref_cal, vref_factor
);
delay.delay_ms(10_u16);
let mut temp_c;
let mut temp_mv;
loop {
temp_mv = read_temp_mv(&mut adc, vref_factor);
temp_c = temp_mv_to_c(temp_mv, tsense_cal1, tsense_cal2);
info!("Temperature: {:03}mv, {:04}°C", temp_mv, temp_c as i32);
delay.delay_ms(500_u16);
}
}
fn read_temp_mv(adc: &mut Adc<Ready>, vref_factor: f32) -> f32 {
let bare: f32 = adc.read(&mut VTemp).expect("could not read with adc");
bare * vref_factor
}
// This unholy abomination is from the datasheet and does not actually look so bad if it's written
// in Math instead of Rust.
fn temp_mv_to_c(temp: f32, ts_cal_1: (i32, u16), ts_cal_2: (i32, u16)) -> f32 {
((ts_cal_2.0 as f32 - ts_cal_1.0 as f32) / (ts_cal_2.1 as f32 - ts_cal_1.1 as f32))
* (temp - ts_cal_1.1 as f32)
+ ts_cal_1.0 as f32
}
#[cfg(test)]
mod tests {
// run these tests: cargo test --example=temperature --target=x86_64-unknown-linux-gnu
use super::temp_mv_to_c;
#[test]
fn test_mv_to_c() {
// values read out from my board as logged
// after flashing and running
//
// First is the temperature for that calibratoin, second is the measured voltage at that
// temperature
let calibration_data = [(30, 673), (130, 912)];
for caldat in calibration_data {
let degrees: f32 =
temp_mv_to_c(caldat.1 as f32, calibration_data[0], calibration_data[1]);
assert_eq!(caldat.0 as f32, degrees);
dbg!(caldat);
dbg!(degrees);
}
}
}

View file

@ -1,59 +0,0 @@
//! This example shows how a embedded program can be written that is testable on the host with
//! libtest.
//!
//! The tests can be run with:
//! ```bash
//! cargo test --example=test-on-host --target=x86_64-unknown-linux-gnu
//! ```
#![cfg_attr(not(test), no_main)]
#![no_std]
#[cfg(not(test))]
extern crate panic_halt;
use hal::{pac, prelude::*, rcc::Config};
#[cfg_attr(not(test), cortex_m_rt::entry)] // this is the entrypoint unless testing
fn main() -> ! {
let dp = pac::Peripherals::take().unwrap();
let cp = cortex_m::Peripherals::take().unwrap();
// 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.
let gpioa = dp.GPIOA.split(&mut rcc);
// Configure PA5 as output.
let mut led = gpioa.pa5.into_push_pull_output();
// Get the delay provider.
let mut delay = cp.SYST.delay(rcc.clocks);
loop {
led.set_high().unwrap();
delay.delay_ms(500_u16);
let important_number = some_function(19);
delay.delay_ms(important_number as u16);
led.set_low().unwrap();
delay.delay_ms(500_u16);
}
}
fn some_function(num: i32) -> i32 {
num * 2
}
#[cfg(test)]
mod tests {
use crate::some_function;
#[test]
fn test_it_works() {
assert_eq!(some_function(9), 18)
}
}

0
src/crc.rs Executable file
View file

View file

@ -1 +0,0 @@
#![no_std]

View file

@ -1,6 +1,8 @@
#![no_main]
#![no_std]
mod crc;
use defmt::{debug, error, info, println, trace, warn};
use panic_probe as _;
@ -48,7 +50,5 @@ fn main() -> ! {
led.set_low().unwrap();
delay.delay_ms(500_u16);
panic!("This will use the defmt panic handler too");
}
}