embassy/embassy-nrf/src/util.rs

32 lines
987 B
Rust
Raw Normal View History

use core::mem;
const SRAM_LOWER: usize = 0x2000_0000;
const SRAM_UPPER: usize = 0x3000_0000;
// TODO: replace transmutes with core::ptr::metadata once it's stable
2022-01-19 15:59:06 +01:00
pub(crate) fn slice_ptr_parts<T>(slice: *const [T]) -> (*const T, usize) {
unsafe { mem::transmute(slice) }
}
2022-01-19 15:59:06 +01:00
pub(crate) fn slice_ptr_parts_mut<T>(slice: *mut [T]) -> (*mut T, usize) {
unsafe { mem::transmute(slice) }
}
/// Does this slice reside entirely within RAM?
pub(crate) fn slice_in_ram<T>(slice: *const [T]) -> bool {
let (ptr, len) = slice_ptr_parts(slice);
2022-01-19 15:59:06 +01:00
let ptr = ptr as usize;
ptr >= SRAM_LOWER && (ptr + len * core::mem::size_of::<T>()) < SRAM_UPPER
}
/// Return an error if slice is not in RAM. Skips check if slice is zero-length.
#[cfg(not(feature = "nrf51"))]
pub(crate) fn slice_in_ram_or<T, E>(slice: *const [T], err: E) -> Result<(), E> {
let (_, len) = slice_ptr_parts(slice);
2022-03-08 16:29:42 +01:00
if len == 0 || slice_in_ram(slice) {
Ok(())
} else {
Err(err)
}
}