stm32g473_hal_oppe/
signature.rs

1//! Device electronic signature
2//!
3//! (stored in flash memory)
4
5use core::str::from_utf8_unchecked;
6
7/// This is the test voltage, in millivolts of the calibration done at the factory
8pub const VDDA_CALIB: u32 = 3300;
9
10macro_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)]
28pub struct Uid {
29    x: u16,
30    y: u16,
31    waf_lot: [u8; 8],
32}
33define_ptr_type!(Uid, 0x1FFF_7590);
34
35impl 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)]
60pub struct FlashSize(u16);
61define_ptr_type!(FlashSize, 0x1FFF_75E0);
62
63impl 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)]
78pub struct VrefCal(u16);
79define_ptr_type!(VrefCal, 0x1FFF_75AA);
80
81impl 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)]
91pub struct VtempCal30(u16);
92define_ptr_type!(VtempCal30, 0x1FFF_75A8);
93
94impl 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)]
104pub struct VtempCal110(u16);
105define_ptr_type!(VtempCal110, 0x1FFF_75CA);
106
107impl VtempCal110 {
108    /// Read calibration value
109    pub fn read(&self) -> u16 {
110        self.0
111    }
112}