Merge pull request #2241 from adamgreig/stm32-uid
STM32: Add UID driver
This commit is contained in:
commit
b867f9b5b6
@ -63,6 +63,8 @@ pub mod sai;
|
|||||||
pub mod sdmmc;
|
pub mod sdmmc;
|
||||||
#[cfg(spi)]
|
#[cfg(spi)]
|
||||||
pub mod spi;
|
pub mod spi;
|
||||||
|
#[cfg(uid)]
|
||||||
|
pub mod uid;
|
||||||
#[cfg(usart)]
|
#[cfg(usart)]
|
||||||
pub mod usart;
|
pub mod usart;
|
||||||
#[cfg(usb)]
|
#[cfg(usb)]
|
||||||
|
29
embassy-stm32/src/uid.rs
Normal file
29
embassy-stm32/src/uid.rs
Normal file
@ -0,0 +1,29 @@
|
|||||||
|
/// Get this device's unique 96-bit ID.
|
||||||
|
pub fn uid() -> &'static [u8; 12] {
|
||||||
|
unsafe { &*crate::pac::UID.uid(0).as_ptr().cast::<[u8; 12]>() }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get this device's unique 96-bit ID, encoded into a string of 24 hexadecimal ASCII digits.
|
||||||
|
pub fn uid_hex() -> &'static str {
|
||||||
|
unsafe { core::str::from_utf8_unchecked(uid_hex_bytes()) }
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get this device's unique 96-bit ID, encoded into 24 hexadecimal ASCII bytes.
|
||||||
|
pub fn uid_hex_bytes() -> &'static [u8; 24] {
|
||||||
|
const HEX: &[u8; 16] = b"0123456789ABCDEF";
|
||||||
|
static mut UID_HEX: [u8; 24] = [0; 24];
|
||||||
|
static mut LOADED: bool = false;
|
||||||
|
critical_section::with(|_| unsafe {
|
||||||
|
if !LOADED {
|
||||||
|
let uid = uid();
|
||||||
|
for (idx, v) in uid.iter().enumerate() {
|
||||||
|
let lo = v & 0x0f;
|
||||||
|
let hi = (v & 0xf0) >> 4;
|
||||||
|
UID_HEX[idx * 2] = HEX[hi as usize];
|
||||||
|
UID_HEX[idx * 2 + 1] = HEX[lo as usize];
|
||||||
|
}
|
||||||
|
LOADED = true;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
unsafe { &UID_HEX }
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user