1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#![no_std]
use core::slice;
use cortex_m::interrupt;
#[cfg(any(feature = "stm32f0", feature = "stm32f3"))]
mod pointers {
pub const DEVICE_ID_PTR: *const u8 = 0x1FFF_F7AC as _;
pub const FLASH_SIZE_PTR: *const u16 = 0x1FFF_F7CC as _;
}
#[cfg(feature = "stm32f1")]
mod pointers {
pub const DEVICE_ID_PTR: *const u8 = 0x1FFF_F7E8 as _;
pub const FLASH_SIZE_PTR: *const u16 = 0x1FFF_F7E0 as _;
}
#[cfg(feature = "stm32f4")]
mod pointers {
pub const DEVICE_ID_PTR: *const u8 = 0x1FFF_7A10 as _;
pub const FLASH_SIZE_PTR: *const u16 = 0x1FFF_7A22 as _;
}
#[cfg(any(feature = "stm32g0", feature = "stm32l4"))]
mod pointers {
pub const DEVICE_ID_PTR: *const u8 = 0x1FFF_7590 as _;
pub const FLASH_SIZE_PTR: *const u16 = 0x1FFF_75E0 as _;
}
#[cfg(feature = "stm32l0")]
mod pointers {
pub const DEVICE_ID_PTR: *const u8 = 0x1FF8_0050 as _;
pub const FLASH_SIZE_PTR: *const u16 = 0x1FF8_007C as _;
}
use pointers::*;
pub fn device_id() -> &'static [u8] {
unsafe { slice::from_raw_parts(DEVICE_ID_PTR, 12) }
}
pub fn device_id_hex() -> &'static str {
static mut DEVICE_ID_STR: [u8; 24] = [0; 24];
unsafe {
if DEVICE_ID_STR.as_ptr().read_volatile() == 0 {
interrupt::free(|_| {
let hex = "0123456789abcdef".as_bytes();
let device_id = slice::from_raw_parts(DEVICE_ID_PTR, 12);
for i in 0..12 {
let lo = device_id[i] & 0xf;
let hi = (device_id[i] >> 4) & 0xf;
DEVICE_ID_STR[i*2] = hex[lo as usize];
DEVICE_ID_STR[i*2+1] = hex[hi as usize];
}
});
}
core::str::from_utf8_unchecked(&DEVICE_ID_STR)
}
}
pub fn flash_size_kb() -> u16 {
unsafe { *FLASH_SIZE_PTR }
}