| 1 | //! Unique ID (UID) |
| 2 | |
| 3 | /// Get this device's unique 96-bit ID. |
| 4 | pub fn uid() -> &'static [u8; 12] { |
| 5 | unsafe { &*crate::pac::UID.uid(0).as_ptr().cast::<[u8; 12]>() } |
| 6 | } |
| 7 | |
| 8 | /// Get this device's unique 96-bit ID, encoded into a string of 24 hexadecimal ASCII digits. |
| 9 | pub fn uid_hex() -> &'static str { |
| 10 | unsafe { core::str::from_utf8_unchecked(uid_hex_bytes()) } |
| 11 | } |
| 12 | |
| 13 | /// Get this device's unique 96-bit ID, encoded into 24 hexadecimal ASCII bytes. |
| 14 | pub fn uid_hex_bytes() -> &'static [u8; 24] { |
| 15 | const HEX: &[u8; 16] = b"0123456789ABCDEF" ; |
| 16 | static mut UID_HEX: [u8; 24] = [0; 24]; |
| 17 | static mut LOADED: bool = false; |
| 18 | critical_section::with(|_| unsafe { |
| 19 | if !LOADED { |
| 20 | let uid: &'static [u8; 12] = uid(); |
| 21 | for (idx: usize, v: &u8) in uid.iter().enumerate() { |
| 22 | let lo: u8 = v & 0x0f; |
| 23 | let hi: u8 = (v & 0xf0) >> 4; |
| 24 | UID_HEX[idx * 2] = HEX[hi as usize]; |
| 25 | UID_HEX[idx * 2 + 1] = HEX[lo as usize]; |
| 26 | } |
| 27 | LOADED = true; |
| 28 | } |
| 29 | }); |
| 30 | unsafe { &*core::ptr::addr_of!(UID_HEX) } |
| 31 | } |
| 32 | |