Skip to content

Commit

Permalink
Merge pull request #172 from korken89/signatures
Browse files Browse the repository at this point in the history
Added signature module, similar to STM32F4xx HAL
  • Loading branch information
korken89 authored Dec 9, 2020
2 parents 9226cc7 + ee598c7 commit d9d2978
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 0 deletions.
8 changes: 8 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ pub mod serial;
feature = "stm32l4x5",
feature = "stm32l4x6"
))]
pub mod signature;
#[cfg(any(
feature = "stm32l4x1",
feature = "stm32l4x2",
feature = "stm32l4x3",
feature = "stm32l4x5",
feature = "stm32l4x6"
))]
pub mod spi;
#[cfg(any(
feature = "stm32l4x1",
Expand Down
119 changes: 119 additions & 0 deletions src/signature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
//! Device electronic signature
//!
//! (stored in flash memory)
//!
//! Based on the STM32F4xx HAL.

use core::str::from_utf8_unchecked;

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

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..]) }
}

/// As a byte array
pub fn as_bytes() -> &'static [u8; 12] {
unsafe { &*(Self::ptr() as *const _) }
}
}

/// 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 130°C stored at the factory
#[derive(Debug)]
#[repr(C)]
pub struct VtempCal130(u16);
define_ptr_type!(VtempCal130, 0x1FFF_75CA);

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

0 comments on commit d9d2978

Please sign in to comment.