Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6] Signature #17

Merged
merged 1 commit into from
Jul 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ pub mod prelude;
pub mod rcc;
// pub mod rng;
// pub mod serial;
pub mod signature;
// pub mod spi;
// pub mod stopwatch;
pub mod syscfg;
Expand Down
112 changes: 112 additions & 0 deletions src/signature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
//! Device electronic signature
//!
//! (stored in flash memory)

use core::str::from_utf8_unchecked;

/// This is the test voltage, in millivolts of the calibration done at the factory
pub const VDDA_CALIB: u32 = 3300;

macro_rules! define_ptr_type {
($name: ident, $ptr: expr) => {
impl $name {
fn ptr() -> *const Self {
$ptr as *const _
}

/// Returns a wrapped reference to the value in flash memory
pub fn get() -> &'static Self {
unsafe { &*Self::ptr() }
}
}
};
}

/// Uniqure Device ID register
#[derive(Hash, Debug)]
#[repr(C)]
pub struct Uid {
x: u16,
y: u16,
waf_lot: [u8; 8],
}
define_ptr_type!(Uid, 0x1FFF_7590);

impl Uid {
/// X coordinate on wafer
pub fn x(&self) -> u16 {
self.x
}

/// Y coordinate on wafer
pub fn y(&self) -> u16 {
self.y
}

/// Wafer number
pub fn waf_num(&self) -> u8 {
self.waf_lot[0]
}

/// Lot number
pub fn lot_num(&self) -> &str {
unsafe { from_utf8_unchecked(&self.waf_lot[1..]) }
}
}

/// Size of integrated flash
#[derive(Debug)]
#[repr(C)]
pub struct FlashSize(u16);
define_ptr_type!(FlashSize, 0x1FFF_75E0);

impl FlashSize {
/// Read flash size in kilobytes
pub fn kilo_bytes(&self) -> u16 {
self.0
}

/// Read flash size in bytes
pub fn bytes(&self) -> usize {
usize::from(self.kilo_bytes()) * 1024
}
}

/// ADC VREF calibration value is stored in at the factory
#[derive(Debug)]
#[repr(C)]
pub struct VrefCal(u16);
define_ptr_type!(VrefCal, 0x1FFF_75AA);

impl VrefCal {
/// Read calibration value
pub fn read(&self) -> u16 {
self.0
}
}

/// A temperature reading taken at 30°C stored at the factory
#[derive(Debug)]
#[repr(C)]
pub struct VtempCal30(u16);
define_ptr_type!(VtempCal30, 0x1FFF_75A8);

impl VtempCal30 {
/// Read calibration value
pub fn read(&self) -> u16 {
self.0
}
}

/// A temperature reading taken at 110°C stored at the factory
#[derive(Debug)]
#[repr(C)]
pub struct VtempCal110(u16);
define_ptr_type!(VtempCal110, 0x1FFF_75CA);

impl VtempCal110 {
/// Read calibration value
pub fn read(&self) -> u16 {
self.0
}
}