Compare commits
6 commits
master
...
feat/tempe
Author | SHA1 | Date | |
---|---|---|---|
b69e9e3fd4 | |||
c82d194206 | |||
65ae71efff | |||
630b2b996e | |||
a5c99f4037 | |||
7d4770dc9e |
4 changed files with 193 additions and 32 deletions
|
@ -24,7 +24,8 @@ fn main() -> ! {
|
||||||
let mut rcc = dp.RCC.freeze(Config::hsi16());
|
let mut rcc = dp.RCC.freeze(Config::hsi16());
|
||||||
let mut delay = cp.SYST.delay(rcc.clocks);
|
let mut delay = cp.SYST.delay(rcc.clocks);
|
||||||
|
|
||||||
compile_error!("The chip does not have an AES unit >:{");
|
// 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 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 ecb_stream = aes.enable(<dyn hal::aes::Mode>::ecb_encrypt(), AES_KEY);
|
||||||
let mut encbuf: [[u8; 16]; 4] = [[0; 16]; 4];
|
let mut encbuf: [[u8; 16]; 4] = [[0; 16]; 4];
|
||||||
|
|
46
examples/blinky-external.rs
Executable file
46
examples/blinky-external.rs
Executable file
|
@ -0,0 +1,46 @@
|
||||||
|
#![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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,49 +1,104 @@
|
||||||
#![no_main]
|
#![cfg_attr(not(test), no_main)]
|
||||||
#![no_std]
|
#![cfg_attr(not(test), no_std)]
|
||||||
|
|
||||||
use defmt::info;
|
#[cfg(not(test))]
|
||||||
use hal::adc::{Adc, Ready, VTemp};
|
extern crate panic_halt;
|
||||||
use hal::gpio::Analog;
|
|
||||||
use hal::gpio::gpiob::PB1;
|
|
||||||
use panic_probe as _;
|
|
||||||
|
|
||||||
use defmt_rtt as _; // global logger
|
use hal::adc::{Adc, Ready, VRef, VTemp};
|
||||||
|
use hal::calibration::{VtempCal30, VtempCal130};
|
||||||
use cortex_m_rt::entry;
|
|
||||||
use hal::{pac, prelude::*, rcc::Config};
|
use hal::{pac, prelude::*, rcc::Config};
|
||||||
|
|
||||||
const MAGIC_TEMPERATURE_NUMBER: f32 = 12.412122;
|
use defmt::{debug, info};
|
||||||
|
use defmt_rtt as _; // global logger
|
||||||
|
|
||||||
#[entry]
|
#[cfg_attr(not(test), cortex_m_rt::entry)] // this is the entrypoint unless testing
|
||||||
fn main() -> ! {
|
fn main() -> ! {
|
||||||
let dp = pac::Peripherals::take().unwrap();
|
let dp = pac::Peripherals::take().unwrap();
|
||||||
let cp = cortex_m::Peripherals::take().unwrap();
|
let cp = cortex_m::Peripherals::take().unwrap();
|
||||||
|
|
||||||
let mut rcc = dp.RCC.freeze(Config::hsi16());
|
let mut rcc = dp.RCC.freeze(Config::hsi16());
|
||||||
let mut adc = dp.ADC.constrain(&mut rcc);
|
let mut adc: Adc<_> = dp.ADC.constrain(&mut rcc);
|
||||||
|
|
||||||
let gpiob = dp.GPIOB.split(&mut rcc);
|
|
||||||
|
|
||||||
let mut temp_pin = gpiob.pb1.into_analog();
|
|
||||||
|
|
||||||
// Get the delay provider.
|
|
||||||
let mut delay = cp.SYST.delay(rcc.clocks);
|
let mut delay = cp.SYST.delay(rcc.clocks);
|
||||||
|
|
||||||
let mut temp;
|
// NOTE: TSEN bit must be enabled for reading the temperature
|
||||||
let mut i = 0;
|
VTemp.enable(&mut adc);
|
||||||
loop {
|
VRef.enable(&mut adc);
|
||||||
if i % 10_000 == 0 {
|
|
||||||
temp = read_temp_c(&mut temp_pin, &mut adc);
|
// reference temperatures from the chips readonly memory
|
||||||
info!("Temperature: {}", temp);
|
// [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);
|
||||||
}
|
}
|
||||||
// delay.delay_ms(200_u16);
|
|
||||||
i += 1;
|
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_c(pin: &mut PB1<Analog>, adc: &mut Adc<Ready>) -> i16 {
|
fn read_temp_mv(adc: &mut Adc<Ready>, vref_factor: f32) -> f32 {
|
||||||
let v: f32 = adc
|
let bare: f32 = adc.read(&mut VTemp).expect("could not read with adc");
|
||||||
.read(pin /* or maybe VTemp from the adc module? */)
|
bare * vref_factor
|
||||||
.expect("could not read with adc");
|
}
|
||||||
(v / MAGIC_TEMPERATURE_NUMBER) as i16 - 50
|
|
||||||
|
// 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
59
examples/test-on-host.rs
Executable file
59
examples/test-on-host.rs
Executable file
|
@ -0,0 +1,59 @@
|
||||||
|
//! 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)
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue