Skip to content

Commit 186c0dc

Browse files
no111u3mvertescher
authored andcommitted
Adding device signature and access to internal constants
1 parent b100f39 commit 186c0dc

File tree

2 files changed

+115
-0
lines changed

2 files changed

+115
-0
lines changed

src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,9 @@ pub mod time;
112112
#[cfg(feature = "stm32f746")]
113113
pub mod timer;
114114

115+
#[cfg(feature = "stm32f746")]
116+
pub mod signature;
117+
115118
pub mod state {
116119
/// Indicates that a peripheral is enabled
117120
pub struct Enabled;

src/signature.rs

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//! Device electronic signature
2+
//!
3+
//! (stored in flash memory)
4+
5+
use core::str::from_utf8_unchecked;
6+
7+
/// This is the test voltage, in millivolts of the calibration done at the factory
8+
pub const VDDA_CALIB: u32 = 3300;
9+
10+
macro_rules! define_ptr_type {
11+
($name: ident, $ptr: expr) => {
12+
impl $name {
13+
fn ptr() -> *const Self {
14+
$ptr as *const _
15+
}
16+
17+
/// Returns a wrapped reference to the value in flash memory
18+
pub fn get() -> &'static Self {
19+
unsafe { &*Self::ptr() }
20+
}
21+
}
22+
};
23+
}
24+
25+
/// Uniqure Device ID register
26+
#[derive(Hash, Debug)]
27+
#[repr(C)]
28+
pub struct Uid {
29+
x: u16,
30+
y: u16,
31+
waf_lot: [u8; 8],
32+
}
33+
define_ptr_type!(Uid, 0x1FF0_F420);
34+
35+
impl Uid {
36+
/// X coordinate on wafer
37+
pub fn x(&self) -> u16 {
38+
self.x
39+
}
40+
41+
/// Y coordinate on wafer
42+
pub fn y(&self) -> u16 {
43+
self.y
44+
}
45+
46+
/// Wafer number
47+
pub fn waf_num(&self) -> u8 {
48+
self.waf_lot[0]
49+
}
50+
51+
/// Lot number
52+
pub fn lot_num(&self) -> &str {
53+
unsafe { from_utf8_unchecked(&self.waf_lot[1..]) }
54+
}
55+
}
56+
57+
/// Size of integrated flash
58+
#[derive(Debug)]
59+
#[repr(C)]
60+
pub struct FlashSize(u16);
61+
define_ptr_type!(FlashSize, 0x1FF0_F442);
62+
63+
impl FlashSize {
64+
/// Read flash size in kilobytes
65+
pub fn kilo_bytes(&self) -> u16 {
66+
self.0
67+
}
68+
69+
/// Read flash size in bytes
70+
pub fn bytes(&self) -> usize {
71+
usize::from(self.kilo_bytes()) * 1024
72+
}
73+
}
74+
75+
/// ADC VREF calibration value is stored in at the factory
76+
#[derive(Debug)]
77+
#[repr(C)]
78+
pub struct VrefCal(u16);
79+
define_ptr_type!(VrefCal, 0x1FF0_F44A);
80+
81+
impl VrefCal {
82+
/// Read calibration value
83+
pub fn read(&self) -> u16 {
84+
self.0
85+
}
86+
}
87+
88+
/// A temperature reading taken at 30°C stored at the factory
89+
#[derive(Debug)]
90+
#[repr(C)]
91+
pub struct VtempCal30(u16);
92+
define_ptr_type!(VtempCal30, 0x1FF0_F44C);
93+
94+
impl VtempCal30 {
95+
/// Read calibration value
96+
pub fn read(&self) -> u16 {
97+
self.0
98+
}
99+
}
100+
101+
/// A temperature reading taken at 110°C stored at the factory
102+
#[derive(Debug)]
103+
#[repr(C)]
104+
pub struct VtempCal110(u16);
105+
define_ptr_type!(VtempCal110, 0x1FF0_F44E);
106+
107+
impl VtempCal110 {
108+
/// Read calibration value
109+
pub fn read(&self) -> u16 {
110+
self.0
111+
}
112+
}

0 commit comments

Comments
 (0)