diff options
| author | Adam Greig <[email protected]> | 2023-12-03 02:05:55 +0000 |
|---|---|---|
| committer | Adam Greig <[email protected]> | 2023-12-03 23:17:49 +0000 |
| commit | 198ef8183e74543d013acd6b45d0efa861bb33fa (patch) | |
| tree | b527028918046e9912401b7cdac1de4dec563e54 /embassy-stm32/src/uid.rs | |
| parent | a31ae52d1a7e0607edeaea9acf30b03ea510fd83 (diff) | |
STM32: Add UID driver
Diffstat (limited to 'embassy-stm32/src/uid.rs')
| -rw-r--r-- | embassy-stm32/src/uid.rs | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/embassy-stm32/src/uid.rs b/embassy-stm32/src/uid.rs new file mode 100644 index 000000000..6dcfcb96e --- /dev/null +++ b/embassy-stm32/src/uid.rs | |||
| @@ -0,0 +1,29 @@ | |||
| 1 | /// Get this device's unique 96-bit ID. | ||
| 2 | pub fn uid() -> &'static [u8; 12] { | ||
| 3 | unsafe { &*crate::pac::UID.uid(0).as_ptr().cast::<[u8; 12]>() } | ||
| 4 | } | ||
| 5 | |||
| 6 | /// Get this device's unique 96-bit ID, encoded into a string of 24 hexadecimal ASCII digits. | ||
| 7 | pub fn uid_hex() -> &'static str { | ||
| 8 | unsafe { core::str::from_utf8_unchecked(uid_hex_bytes()) } | ||
| 9 | } | ||
| 10 | |||
| 11 | /// Get this device's unique 96-bit ID, encoded into 24 hexadecimal ASCII bytes. | ||
| 12 | pub fn uid_hex_bytes() -> &'static [u8; 24] { | ||
| 13 | const HEX: &[u8; 16] = b"0123456789ABCDEF"; | ||
| 14 | static mut UID_HEX: [u8; 24] = [0; 24]; | ||
| 15 | static mut LOADED: bool = false; | ||
| 16 | critical_section::with(|_| unsafe { | ||
| 17 | if !LOADED { | ||
| 18 | let uid = uid(); | ||
| 19 | for (idx, v) in uid.iter().enumerate() { | ||
| 20 | let lo = v & 0x0f; | ||
| 21 | let hi = (v & 0xf0) >> 4; | ||
| 22 | UID_HEX[idx * 2] = HEX[hi as usize]; | ||
| 23 | UID_HEX[idx * 2 + 1] = HEX[lo as usize]; | ||
| 24 | } | ||
| 25 | LOADED = true; | ||
| 26 | } | ||
| 27 | }); | ||
| 28 | unsafe { &UID_HEX } | ||
| 29 | } | ||
