Skip to content

Commit ee598c7

Browse files
committed
Added signature module, similar to STM32F4xx HAL
Clippy fix
1 parent 9226cc7 commit ee598c7

File tree

2 files changed

+127
-0
lines changed

2 files changed

+127
-0
lines changed

src/lib.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,14 @@ pub mod serial;
180180
feature = "stm32l4x5",
181181
feature = "stm32l4x6"
182182
))]
183+
pub mod signature;
184+
#[cfg(any(
185+
feature = "stm32l4x1",
186+
feature = "stm32l4x2",
187+
feature = "stm32l4x3",
188+
feature = "stm32l4x5",
189+
feature = "stm32l4x6"
190+
))]
183191
pub mod spi;
184192
#[cfg(any(
185193
feature = "stm32l4x1",

src/signature.rs

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

0 commit comments

Comments
 (0)